Unit 1 of 10 · about 4 hours · implement
Triton programs, program IDs, pointers and masks
Every later Triton kernel reuses this mapping. A wrong program ID or an unmasked tail corrupts memory silently, so the discipline starts here.
You'll be able to
- Map a 1D tensor operation to Triton program IDs, block offsets, masks and memory transactions.
- Implement a masked Triton map kernel that is correct for non-multiple shapes and matches a PyTorch reference where the arithmetic is exact.
- Explain how one Triton program maps to one CUDA thread block and how the compiler splits block work across threads.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Vector Addition: Compute KernelTriton Programming Guide and Tutorials
Compute Kernel:
tl.program_id,tl.arange, maskedtl.loadandtl.store; helperaddwith the grid function overmeta['BLOCK_SIZE']andBLOCK_SIZE=1024; correctness check withsize = 98432 - Vector Addition: BenchmarkTriton Programming Guide and Tutorials
Benchmark:
triton.testing.do_benchwith quantiles and the GB/s helper over3 * numel * element_sizebytesRead it as a timing pattern, not as a number to reproduce. Valid timing discipline comes from the measurement section.
- Triton Programming ModelAI Systems Performance Engineering - Chapter 14
Chapter 14, Writing Custom Kernels with OpenAI Triton through Triton Programming Model:
tl.constexprblock sizes, the grid function, and one program per CUDA thread blockStop before Accessing Shared Memory in Triton. Descriptors and pipelined copies belong to the atoms unit.
Focus on
- One Triton program handles one block.
tl.program_id(axis=0)selects it andtl.arangebuilds its lane indices. BLOCK_SIZEis atl.constexpr, so each value compiles its own specialization. The grid function reads it frommetaso the launch grid always matches the value in use, including one an autotuner picks.- The mask guards both loads and the store. Masked-out load lanes yield
otherand must never reach the output. - Pointer arithmetic stays in elements (
x_ptr + offsets), never in bytes.
Common mistakes · 3
- Using a non-power-of-two
BLOCK_SIZE. Each block must hold a power-of-two number of elements, so pad the row and mask the tail. - Closing over a Python
BLOCK_SIZEin the grid instead of readingmeta['BLOCK_SIZE']. The grid then disagrees with the kernel whenever the launch or an autotuner uses another block size, and blocks go missing or overlap. - Masking the loads but not the store. Dead lanes then write out of bounds on the final partial block.
Try it yourself
Closed-book, before checking anything
add_kernel and its grid function for with . State the grid (1,) from triton.cdiv, and predict a maximum absolute difference of 0.0 against x + y, as measured on an RTX 3050 6 GB with CUDA 13.4 and Triton 3.8.0 for , and .Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why does the grid function take
metainstead of a fixed block size?AnswerThe same source specializes per
BLOCK_SIZEvalue. Readingmeta['BLOCK_SIZE']keeps the grid correct for every compiled variant.2.For and , which lanes are masked and what do their loads return?Answer
Lanes 1000 to 1023 are masked. Their loads return
otherand their stores are skipped, so only the first 1000 outputs are written.3.One program covers 1024 elements. How many threads run it?Answer
That is a compiler decision, controlled by
num_warps, not byBLOCK_SIZE. The program maps to one CUDA thread block.4.Why is
x_ptr + offsetscorrect without multiplying by 4 for float32?AnswerTriton pointer arithmetic is in elements of the pointed-to type, so the element size is implicit.
Practice
- TensorTonic: Masked Triton GELU Kernel
Implement masked tanh-approximate GELU for contiguous CUDA tensors with partial final blocks
Rerun with your own non-multiple shapes and at least two dtypes, and compare against
torch.nn.functional.geluwith the matching approximation.
You're done when
Your GELU kernel matches the reference on partial-block shapes, and you can state the grid, mask and other policy for any without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 10 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.
- 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.
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 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.