Unit 9 of 10 · about 6 hours · implement
SwiGLU MLP and epilogue fusion boundaries
Pointwise fusion can remove launches and intermediate traffic. Fusing a complete MLP or normalization into a GEMM is harder because consumers need completed reductions and different data reuse.
You'll be able to
- Implement a gated MLP path with the correct projection orientations and activation order.
- Fuse a bounded activation or residual epilogue without changing required outputs or dtype boundaries.
- Derive removed intermediate traffic and explain reduction and resource limits on larger fusion groups.
Covers
Read
In this order, only the parts listed
0 of 2 done
- LeetGPU 84: SwiGLU MLP Block specificationLeetGPU - Attention and Transformer Challenges
Commit 9c9dbe5:
reference_impl,get_solve_signatureandgenerate_example_test; inspectgenerate_functional_testbefore implementing - FlashInfer residual/RMSNorm mutation contractFlashInfer
Commit f2794d5:
fused_add_rmsnormdocstring, Step 1, Step 2 and parameter contractRead the ordinary fused operation only. Do not enable architecture-specific launch mechanisms or quantized variants for this unit.
Focus on
- The challenge computes , , and . Its weights use input-by-output orientation, unlike the stored weight of PyTorch's linear module.
- A fused pointwise SiLU/multiply kernel can read the two projection outputs and write one hidden tensor. A projection epilogue can remove more traffic only if the needed gate and up results are available to the same owner after their reductions finish.
- Residual addition, affine scaling and activation are pointwise after a completed GEMM result. RMSNorm and LayerNorm require a row reduction across all normalized features; one output tile is generally not the entire row.
- FlashInfer's fused residual/RMSNorm updates the residual buffer to the sum and the input buffer to its normalized version. Preserve both outputs if the model needs them. Include saved residual writes and changed rounding points in a fusion comparison.
Common mistakes · 3
- Computing SiLU of the elementwise gate/up product instead of multiplying the activated gate by the up projection. These are different functions.
- Eliminating a residual write even though the next layer needs the unnormalized residual stream. A removed output is not a legitimate bandwidth saving.
- Assuming fewer launches guarantee a faster MLP. Fusion may duplicate GEMM work, spill registers or reduce occupancy, and the down projection still needs its full reduction.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why can a pointwise residual epilogue be easier than a norm epilogue?Answer
Each completed output element needs only its corresponding residual. Norm needs statistics across the entire normalization row, which may span several output tiles.
2.Does SwiGLU with zero input return zero here?Answer
Yes. These projections have no bias, so both branches are zero and the hidden tensor and down projection remain zero. Biased projections would require a different answer.
3.Why can fused and unfused outputs differ even with the same mathematical order?Answer
The unfused path may round intermediate tensors to their storage dtype. A fused path can retain a higher-precision value until the final store; compare against an explicit numerical contract, not bitwise equality.
Practice
- LeetGPU 84: SwiGLU MLP Block
Example and ten
generate_functional_testcases, using the challenge'ssolvesignature and tolerancesRun equivalent public cases in your local harness; reuse your earlier validated GEMM kernels and add one fused SiLU/multiply kernel. Cases include zero input, single rows and non-power-of-two dimensions. Skip the large performance case initially; the functional cases fit a small GPU. Keep a library/compiled MLP as the baseline. The section mini-project covers a residual/scale epilogue and residual/norm mutation tests. This is the second selected transformer challenge; do not add the other candidates as extra required practice.
You're done when
All challenge 84 functional cases pass, one bounded fusion matches its independent reference, and your traffic diagram explains why a full-row norm cannot be treated as a pointwise epilogue.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 11 more in the section
- LeetGPU - Attention and Transformer ChallengesAlphaGPU · required
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.
- FlashInferFlashInfer Project · support
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.
Also in this section
- FlashAttentionTri Dao and collaborators · primary
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 · required
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 · required
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 · required
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. - TensorTonic Language Modeling from Scratch - Blockwise Online SoftmaxTensorTonic · support
Solve it before the tiled attention kernel as a check on the rescaling recurrence.
- GPU MODE - Attention and Fused KernelsGPU MODE · support
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 · depth
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 · depth
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 · reference
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. - Triton 3.8.0 Layer NormalizationTriton Project · required
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. - vLLM serving latency and goodput definitionsvLLM Project · reference
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.