Unit 2 of 10 · about 3 hours · implement
Triton reductions and dot-product precision
Softmax and matmul carry the two numerical contracts of this section: exact tails for reductions and stated precision for products.
You'll be able to
- Implement row reductions with
tl.maxandtl.sumover axis 0, with tail policies that keep each reduction exact. - Explain why
tl.dotaccumulates in fp32 and when its default tf32 input precision changes the result. - Predict fused versus unfused traffic for a rowwise softmax from element counts.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Fused Softmax: kernel and testTriton Programming Guide and Tutorials
Motivations: the 5MN plus 2M read and 3MN plus 2M write count; Compute Kernel: row loop with
tl.num_programs,BLOCK_SIZEas the next power of two,other=-float('inf'),tl.maxandtl.sumover axis 0; Unit Test with the 1823 by 781 matrix - Matrix Multiplication: accumulator and testTriton Programming Guide and Tutorials
Final Result: fp32
accumulator,tl.dotper K block, cast to fp16 at the epilogue; Unit Test: 512 by 512 fp16 comparison withatol=1e-2 tl.dotinput precisionTriton Programming Guide and Tutorialstl.dot:input_precisiondefaulttf32for f32 inputs on tensor-core hardware, withieeefor full fp32 inputs (still rounded at every step, so not exact)
Focus on
- Reductions run over axis 0 of a block. The max tail uses minus infinity while a sum tail uses 0, and swapping them corrupts the result.
- The fused traffic claim is , about 4 for wide rows. Check the element counts before quoting the speedup.
- Matmul accumulates in fp32 and casts once. The cast position, not just the input dtype, sets the error.
- The matmul tutorial compares fp16 outputs at
atol=1e-2, set by fp16 inputs and the final cast. The softmax tutorial uses thetorch.allclosedefaults;tl.expis fast but approximate, so state your softmax tolerance separately.
Common mistakes · 3
- Passing
other=0.0to a row-max load. Masked lanes then cap the maximum at 0 and break all-negative rows. - Comparing fp16 matmul output with default tolerances. Use the tutorial
atol=1e-2and justify it from input precision. - Assuming
tl.doton fp32 inputs matches fp32 math. The default tf32 path truncates inputs, so setinput_precision='ieee'when the test needs full fp32 inputs, and still compare with a tolerance.
Try it yourself
Closed-book, before checking anything
tl.dot loop with input_precision='tf32' gives a maximum absolute error of 0.0237 against fp64, while 'ieee' gives 7.6e-06. Measured on an RTX 3050 6 GB with CUDA 13.4 and Triton 3.8.0; yours will differ in detail. Reproduce the gap direction on your GPU and explain why tf32 input truncation, not accumulation order, dominates it.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.A row holds
[-3.0, -1.0]with a block of 4 and a max-loadother=0.0. What breaks?AnswerThe max becomes 0.0 instead of -1.0, so every output is wrong. Max tails need minus infinity.
2.Naive softmax moves elements and fused moves . What is the claimed speedup for large ?Answer
About 4. The term vanishes next to as grows.
3.Why does the matmul tutorial accumulate in fp32 and cast at the end?Answer
Each K block adds its rounding once in fp32. Casting inputs or partial sums earlier compounds the error, while the final cast costs one rounding.
4.Your fp32 Triton matmul differs from fp64 by 0.02. Is that a bug?Answer
Not necessarily with default precision: tf32 input truncation alone explains errors of that size on a 64-wide reduction. Rerun with
'ieee'before debugging indexing.
Practice
- TensorTonic: Tiled Triton Row Sum
Tiled row reduction for strided CUDA tensors with fixed-width tiles and float32 sums
- TensorTonic: Triton Row-Wise Softmax
Stable rowwise softmax for padded rows and partial tiles in float32, float16 and bfloat16
Rerun both with non-multiple widths including a 781-wide row, and compare fp16 and bfloat16 outputs against fp32 with a stated tolerance.
You're done when
Both problems pass, and you can state each reduction tail value and the dot precision behind every reported error.
Only when you can do this without notes.
Leads to
Notes
Resources · 1 for this unit, 11 more in the section
- Triton Programming Guide and TutorialsTriton Project · primary
Complete the vector add, fused softmax, matrix multiplication, LayerNorm and fused-attention tutorials that run on your hardware. For each, add adversarial tails, independent references, cold and warm timing, resource inspection and a correct fallback.
Also in this section
- UCSD CSE 291A/DSC 291 (Spring 2026) PA2 Part 1 - Triton matmul, add and ReLUUC San Diego · required
Implement the fused kernel and its configuration search, and test with
student_local_test.pyon your own GPU. The remote grader needs a course token, so replace its speedup score with your own interleaved timing against the PyTorch reference. - TensorTonic Language Modeling from Scratch - Triton problemsTensorTonic · support
Solve Masked Triton GELU Kernel, Tiled Triton Row Sum, Triton Row-Wise Softmax and Fused Tiled Matmul and ReLU, and rerun each with your own adversarial shapes.
- AI Systems Performance Engineering - Chapter 14Chris Fregly · required
Read Chapter 14 on the PyTorch compiler, OpenAI Triton and XLA backends. Trace one framework operation from graph capture through generated GPU code, and identify graph breaks and specialization guards.
- AI Systems Performance Engineering companion code - Chapters 9, 10, 13 and 14Chris Fregly · support
Use
code/ch09,code/ch10,code/ch13andcode/ch14selectively for Triton, warp specialization, persistent kernels and compiler comparisons. Strip local compatibility shims when diagnosing upstream failures and record every guard in the measurement context. - CUTLASS 3.x Design and CuTe Layout AlgebraNVIDIA · required
Study the collective and kernel hierarchy, then the CuTe layout algebra guide. Reconstruct one GEMM configuration down to layouts, copy atoms and MMA atoms and compare it with the equivalent Triton blocked program.
- LeetGPU - CUDA and Triton Parity SetAlphaGPU · required
Re-solve one primitive, one GEMM and one transformer challenge from GPU-4 through GPU-6 in Triton. Compare CUDA, Triton, compiler and library paths on identical tests and hardware; leaderboard rank does not replace local raw evidence.
- GPU MODE - Triton, CuTe and Machine CodeGPU MODE · support
Use Lectures 14, 15, 29, 37, 57, 86, 103, 104 and 114 selectively. Assign one lecture to each actual implementation question rather than watching the collection linearly.
- PTX ISA and CUDA Binary UtilitiesNVIDIA · reference
Inspect generated PTX and SASS for the matched CUDA and Triton kernels. Check instruction, memory-space and target assumptions in the versioned ISA and use binary tools to confirm which device code ships.
- CUDA Tile IR Programming ModelNVIDIA · depth
Read the programming model and inspect the official repository after CUDA, Triton and CuTe mappings are clear. Implement one tiny parity kernel only if the toolchain supports it and record compiler-version constraints.
- Modern GPU Programming for MLSys - TIRxMLC · depth
Use Parts II and III plus the compiler reference as a Blackwell-specific comparison of layouts, dispatch, TMA pipelines and persistent scheduling. Do not make TIRx a required dependency for the portable track.
- MIT 6.S894 Lecture 10 - Domain Languages and AcceleratorsMIT · support
Use the Higher-level kernel languages slides comparing Triton, Pallas, cuTile (Tile IR) and the CuTe DSL on mapping, effort and performance for the programming-model comparison. Do not use the TPU lab.