Attention and Transformer Fusion
Attention combines matrix multiplication, reduction, numerical stability, masks and expensive data movement. This stage derives the IO-aware algorithm, implements a bounded version and connects kernel behavior to prefill, decode and fused transformer operations.
Section 6 of 10 · 10 units · about 53 hours
By the end you can Derive and implement or reconstruct IO-aware attention with stable online softmax, causal and edge-tile correctness, then explain prefill and decode performance across a shape matrix and show one end-to-end gain plus one losing regime.
Units
- Attention shapes, prefill and decode reuse, and materialized traffic5h
- Online softmax state, query and key-value tiling and output tolerance6h
- Causal and padding masks and MHA, GQA and MQA grouping5h
- FlashAttention IO complexity and FlashAttention-2 work partitioning4h
- Backward attention state and recomputation5h
- Variable-length, paged and decode attention5h
- Key-value append, gather, layout conversion and quantized loads5h
- Stable RMSNorm and LayerNorm, and RoPE fusion6h
- SwiGLU MLP and epilogue fusion boundaries6h
- Attention baselines, shape matrices, end-to-end translation and losing regimes6h
Build it
Mini project · about 14 to 20 hours
IO-aware attention with paged decode and fused transformer paths
Build IO-aware attention with paged decoding and fused transformer paths, each gated on a dense reference and the public suites before any timing. The attention kernels, paged decode and fused paths feed the endpoint and the final report.
- 1Write the attention IO and FLOP ledger for one prefill and one decode shape you choose, naming byte categories you leave out, with a schedule sketch. Check the budget against event timings and
cuobjdump -res-usage, and pass LeetGPU challenge 6 in your own harness. - 2Implement online-softmax tiling in PyTorch, then Triton, checked against a float64 dense reference on odd shapes and tile sizes you choose. Pass the CS336
flash_forwardtests before timing anything. - 3Add causal masking and grouped KV heads. Show that changing a future token leaves earlier outputs unchanged, and pass TensorTonic
causal_gqaand LeetGPU 53. - 4Optional: implement the backward pass and match autograd in float64 for all three gradients, with causal masking included.
- 5Build a paged decode kernel with a contiguous gather as the reference. Test page permutation, partial pages and request isolation, with KV append, layout conversion, sentinel and stream-order checks, plus optional dequantized loads.
- 6Fuse norm, RoPE and the MLP epilogue, and count the intermediate traffic the fusion removes. Benchmark against eager and compiled SDPA with event timings, adding
sudo ncucounters where you have access, and keep one regime where your kernel loses.
What you hand in
- Tiled attention with masks, the IO ledger, tile budget and optional backward, checked against dense references.
- Paged decode path with gather oracle, KV append, layout conversion and isolation tests.
- Fused norm, RoPE and MLP paths with traffic counts and the baseline sweep with a losing regime.
How to check it
- Self-grade with the rubric below
- LeetGPU attention challenges against the reference implementation
Pass 6 Softmax Attention, 53 Causal Attention, 61 RoPE and 84 SwiGLU MLP Block in your own harness; keep tests beyond the hosted score.
- CS336 Assignment 2 attention tests
Run
flash_forward,flash_backwardandflash_benchmarkingintests/test_attention.pyon shapes that fit your GPU's memory.
How it's graded
Tiled correctness
Pass: Tiled softmax, causal and grouped masks and paged gather each match dense or framework references from your own tests, with future-token and permutation checks passing.
Great: Closed-book notes rederive the tile merge and the grid size.
Fusion honesty
Pass: Fused norm, RoPE and MLP paths match separate execution in your own tests, with removed traffic counted at its dtype.
Great: Diagrams name every buffer fusion removes and one it cannot, with the dtype of each.
Regime evidence
Pass: Variants are gated on the public suites before timing, runner metrics stay separate from endpoint metrics, and one losing regime is kept with an explanation.
Great: The report states when the library or compiler path wins and why.
Resources
- FlashAttentionTri Dao and collaborators
Read the IO-awareness argument, exact attention algorithm and complexity analysis. Derive the online state and block dataflow, then implement a reduced forward kernel or trace a trusted implementation across at least causal and noncausal cases.
- FlashAttention-2Tri Dao
Study reduced non-matmul work, sequence parallelism and warp work partitioning. Compare the schedule with the first paper and predict which shapes benefit before checking measurements.
- Online Normalizer Calculation for SoftmaxOnline Softmax Authors
Re-derive the recurrence inside tiled attention and prove rescaling correctness. Add extreme-logit, all-masked, odd-head and partial-tile tests before any performance comparison.
- CS336 Assignment 2 - Section 4 FlashAttention-2 in TritonStanford University
Do the weighted-sum warm-up, then
flash_forward(a PyTorch tiled version and the Triton kernel) andflash_backwardagainsttests/test_attention.py. Runflash_benchmarkingon sizes that fit your GPU instead of the handout's B200 sweep. - LeetGPU - Attention and Transformer ChallengesAlphaGPU
Solve 6 Softmax Attention and 12 Multi-Head Attention or 53 Causal Attention. Then choose two of 50 RMS Normalization, 54 SwiGLU, 61 RoPE, 80 Grouped Query Attention, 83 Fused Residual Add RMS Norm and 84 SwiGLU MLP Block. Keep correctness tests beyond the hosted score.
- Triton 3.8.0 Layer NormalizationTriton Project
Motivations and
_layer_norm_fwd_fusedin tutorial 05, pinned to v3.8.0. Study centered variance, float32 reductions, valid-column masks and the forward wrapper's supported-width guard. Defer backward and the benchmark sweep. - TensorTonic Language Modeling from Scratch - Blockwise Online SoftmaxTensorTonic
Solve it before the tiled attention kernel as a check on the rescaling recurrence.
- FlashInferFlashInfer Project
Inspect the attention, sampling and serving-kernel interfaces. Reconstruct one dispatch path and compare its layout and workspace contracts with your reduced kernel instead of copying the optimized implementation.
- GPU MODE - Attention and Fused KernelsGPU MODE
Use Lectures 12, 13, 18, 36 and 40 for FlashAttention, Ring Attention, fusion, FlashAttention-3 and FlashInfer. Reproduce one schedule or fusion idea under the stage shape matrix and a strong baseline.
- FlashAttention-3FlashAttention Authors
Study Hopper-specific asynchronous execution and low-precision design after the portable kernel. Implement only with the required architecture, and separate algorithmic from hardware-specific gains.
- Modern GPU Programming for MLSys - Flash Attention 4MLC
Read the complete Blackwell attention chapter, including two MMAs, online softmax, causal masking and GQA. Run it only on compatible sm_100a hardware with the pinned TIRx stack; otherwise use the code for architecture reconstruction.
- PyTorch SDPA contracts and integration tutorialPyTorch
The PyTorch 2.14 SDPA API page gives mask polarity, rectangular causal alignment, GQA and backend limitations. The SDPA tutorial (https://docs.pytorch.org/tutorials/intermediate/scaled_dot_product_attention_tutorial.html, read 2026-10-13) gives Explicit Dispatcher Control, Causal Self Attention, Using SDPA with
torch.compileand Using SDPA withattn_biassubclasses. Correct the tutorial's evaluation-time causal flag before integration. - vLLM serving latency and goodput definitionsvLLM Project
vLLM v0.31.0: Benchmark CLI, Understanding the Latency Metrics and Load Pattern Configuration; serving benchmark CLI, goodput service-level objectives. Use definitions to translate kernel evidence into request metrics, not as a required serving-engine installation.