What this video shows
About this walkthrough
A correct, parallel softmax kernel can still read the same input three times. The walkthrough combines the maximum and normalization passes with online normalization and stores the row in shared memory, so the final pass does not fetch it again. Issued global-memory sectors across four launches fall from 2,263,040 to 1,131,520. The matching 4,096-row launch goes from 77.28 to 72.45 microseconds, from one capture per version.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Learn Online Softmax
You probably know softmax. In this lab, we'll calculate the same result while reading GPU memory less often, using a technique called online softmax. Here, online means updating the maximum and normalization sum as values arrive. We'll make the change in GPUFlight and compare memory requests and kernel time.
0:25What softmax does
Softmax turns a row of scores into positive weights that add up to one. Larger scores get larger weights. For example, scores of one, two, and three become roughly nine, twenty-four, and sixty-seven percent. Attention uses weights like these to combine information from different tokens. To keep the calculation numerically stable, subtract the row's largest score before taking exponentials. Then divide each exponential by their sum. We are optimizing that calculation, not running a complete language model.
1:03The task: keep the answer, remove rereads
Open Online Softmax in GPUFlight Performance Lab. The large case has 4,096 rows, with 1,024 values in each row. The starter is already parallel and numerically stable. Each block owns one row, and its 256 threads share the work. Our goal is to preserve correctness while reading each input from global memory once. The smaller test cases must still pass.
1:34Find the three input passes
In the first loop, each thread reads its part of the row to find a maximum. The block then combines those partial maxima. The next loop reads the row again to sum the exponentials, using that maximum. Another reduction gives the denominator. The final loop reads the same input a third time and writes the normalized result. From the source, that is three input sweeps and one output sweep. Let's check the profiler.
2:05Profile the unchanged baseline
The starter passes correctness on the RTX 3090, but misses the memory-sector target. Open its session, choose Kernels, and expand softmax rows. The SASS memory-traffic table reports 2,263,040 global sectors across all four launches. A sector is a 32-byte unit of a memory request. This counter includes requests that may hit cache. It does not tell us how many bytes reached the GPU's external memory. For timing, select the large launch: 4,096 blocks, with 256 threads each. It takes 77.28 µs in this trace.
2:52Track the maximum and sum together
Online means updating running statistics as we process values. Here, we track the maximum and normalization sum together, computing the same softmax result. When a larger value raises the maximum, rescale the sum accumulated so far, then add the new value's contribution. That keeps every term on the same exponential scale. This combines the maximum and sum passes, but the final output still needs the input values. We keep the row in shared memory, so the output loop reuses it without another global-memory read.
3:31Edit 1: cache the row and update the pair
Add a shared array for the row, plus shared arrays for each thread's partial maximum and sum. These belong to the block. Now replace the separate maximum and sum loops with one loop. Read x once and store it in the row cache. Next max chooses the larger of the old maximum and x. This exponential factor rescales the old sum. The second term adds x on that same scale. Save each thread's pair, then synchronize so all threads can safely combine the partial results.
4:07Edit 2: merge the partial results
The reduction now combines pairs instead of single numbers. Choose the larger of the two maxima, rescale both partial sums to it, and add them. Keep the synchronization outside the conditional. All threads in the block must reach it before the next reduction step. This gives the row's maximum and denominator. The calculation remains stable, although floating-point reduction order can change rounding. We check correctness within the lab's tolerance.
4:39Edit 3: normalize from shared memory
Finally, the output loop reads values from the shared row cache instead of the original global input. The formula for the output stays the same. The row cache uses 4 KiB for this lab's 1,024 floats. The two reduction arrays add another 2 KiB, calculated from the source. This fixed-size cache is specific to the supported row width. Online softmax by itself does not promise one read for arbitrarily large rows. Run the completed version and check the results.
5:16Verify the memory-request reduction
Correctness passes, and the optimized version meets the sector target. Select the baseline and optimized runs, then open Compare. Across the same four launches, issued sectors fall from 2,263,040 to 1,131,520. Exactly half. That matches the source change: three reads plus one write become one read plus one write. The lab introduction's reference numbers describe only the large case; this table totals all four.
5:53Compare the matching kernel time
Open the optimized session and inspect the same large launch. The grid and block dimensions match the baseline. Its trace duration is 72.45 µs, compared with 77.28 before. That is about 6% less time in this pair of captures. This is one matching kernel launch per version, not a repeated timing benchmark or a whole-model speedup. The harness runtime also includes setup and correctness work, so we keep it separate.
6:28Half the requests is not half the time
Why did halving the requests produce such a small timing change? Issued sectors count load and store requests, even when caches serve them. The new kernel also performs rescaling arithmetic and uses shared storage and synchronization. These costs can offset some of the savings. This capture does not isolate each cause. What it verifies is fewer issued requests, correct output, and a modest timing difference. Repeat the timing across representative shapes before claiming a consistent speedup.
7:05Profile, change, verify
We started with a correct parallel kernel, found repeated input reads, and removed them with online normalization and a shared row cache. Try the lab in GPUFlight Performance Lab. Profile your GPU code, inspect what the counters mean, and check the timing as well as the result. The link is in the description. Thanks for watching!
Measure it yourself
Run the same experiment on a real GPU
Edit the kernel in your browser, run it on managed hardware, and read the profile the video walks through.
Run the softmax lab