What this video shows
About this walkthrough
In attention, different query tokens read the same keys and values again and again. This walkthrough follows a real run from the starter kernel to a tiled version that reuses data across 32 queries. The first shared-memory layout was slower, and adding one padding column to two arrays changed that. In the final 2,048-token test the attention call takes 7.635 ms for the reference and 2.097 ms with tiling and padding, with all four correctness checks passing.
Read along
Transcript
Select a time to play the video from that chapter.
0:00Online attention in CUDA C++
Imagine three tokens that need the same data to compute attention. Our starter kernel loads the same keys and values separately for each token. Can we load that data once and share it? Let's try it in CUDA C++ and measure the difference.
0:17Queries, keys, and values
First, what is attention? Queries, keys, and values are lists of numbers derived from the tokens. Think of a query as what the token is looking for. It compares with keys to produce matching scores. Softmax turns those scores into weights that add up to one. The output is a weighted mixture of the values: the information the token gathers. This lab uses causal attention, so a token can look at itself and earlier tokens, but not future ones.
0:48Run the starter
Open Online Attention in GPUFlight Performance Lab. This project uses CUDA C++ and the RTX 3090. The lab supplies a decoder block, a building block of a language model. We replace just its attention function. The main timing case has 2,048 tokens. The goal is correct output and at least 1.8 times the baseline's speed. Run the unchanged starter first.
1:19Where the starter rereads data
Here, one thread block handles one query row for one attention head. It reads keys up to the current token, calculates scores, then combines the corresponding values. This shared array holds one row of scores. The next query block needs many of the same keys and values, so it reads them again. That repeated work gives us an idea to test.
1:43Check the baseline timings
The starter produces the correct answer, but runs at roughly baseline speed. It misses the performance target. The driver times a trusted baseline and our implementation in the same Trace pass. These CUDA event timings cover the attention call. The separate runtime card measures the full test program without profiling.
2:06Reuse a tile across query rows
Instead of giving a block one query, give it 32. A tile is just a small chunk of a larger array. Load a tile of 32 keys and values into shared memory, which the threads in that block can access together. The query rows reuse this tile before the block loads the next one. Each block has its own shared memory tile.
2:29How online softmax carries state
There is one complication. Softmax needs information from all the allowed scores, but now we only have one tile at a time. Online means processing the data incrementally, one tile at a time. For each query, keep a running maximum, a weight sum, and an output accumulator. The maximum keeps the softmax calculation numerically stable. When a new maximum appears, rescale both the old weight sum and the old accumulator before adding the new tile. At the end, divide by the weight sum. We compute the same attention, allowing for floating point rounding.
3:09Stage and reuse shared-memory tiles
Back in the editor, replace the row kernel with the tiled version. These arrays hold the queries, keys, values, and a small score tile. Load the query tile once. Inside this loop, load the next key and value tile. The barrier makes the block's threads wait until the shared data is ready. Then calculate scores from the staged data, keeping the mask that excludes future tokens.
3:35Rescale and normalize
This is the important update. Alpha converts the previous totals to the scale of the new maximum. Multiply the old denominator by alpha here, and the output accumulator by alpha here. Forgetting either changes the answer when a row spans multiple tiles. Finally, normalize the output and launch one block per query tile and head.
3:59Fix shared-memory bank conflicts with padding
Here is an important twist. Our first tiled version is correct, but takes about 11.7 milliseconds, compared with 7.8 for its baseline. Shared memory is divided into banks that can serve requests in parallel. For these column reads, threads request different values from the same bank. Those requests must take turns. That is a bank conflict. We can see the access pattern by reading the code. Add one unused column to the key tile and the score tile. Each row now starts in a different bank. This changes the layout while preserving the attention math. Let us measure it again.
4:40Verify correctness and speedup
Run the padded version on the same RTX 3090. The attention and final decoder outputs pass at 1, 17, 33, and 2,048 tokens. In this Trace pass, the trusted attention baseline took about 7.6 milliseconds. Our padded tiled version took about 2.1 milliseconds. That is a 3.64 times speedup for this attention call. The rest of the decoder stays unchanged, so this measures the attention improvement.
5:14Find exact kernel durations in the profile
Now open the profiling session and its timeline. The program includes small correctness cases, warmups, and the rest of the decoder. Focus on the 2,048-token attention calls. For exact durations, open the individual kernel launches. Choose the second large launch; the first is its warmup. The starter's measured attention launch is 7.84 milliseconds. In the optimized capture, the padded launch is 2.08 milliseconds. These are GPU kernel activity durations. They differ slightly from the CUDA event timings shown in the console. PC sampling reruns the program for deeper diagnostics. We use the Trace pass for the timing comparison. The timeline helps locate the work. The kernel launch details provide the durations we compare. Neither measures the number of bytes saved here.
6:12What to try next
The attention calculation stayed the same. The way we moved and reused its data changed. Stage a tile, share it across queries, and carry the softmax state forward correctly. Our first attempt also showed why the shared memory layout matters. Measure each change. Try the Online Attention lab in GPUFlight Performance Lab, and inspect what your own changes do. 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 online attention lab