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

Triton blocked-program and SPMD execution modelProgram IDs, logical tiles and launch gridsPointer, stride, block pointer and layout arithmeticMasked loads and stores with safe boundary behavior
1

Read

In this order, only the parts listed

0 of 3 done

  1. Vector Addition: Compute KernelTriton Programming Guide and Tutorials

    Compute Kernel: tl.program_id, tl.arange, masked tl.load and tl.store; helper add with the grid function over meta['BLOCK_SIZE'] and BLOCK_SIZE=1024; correctness check with size = 98432

  2. Vector Addition: BenchmarkTriton Programming Guide and Tutorials

    Benchmark: triton.testing.do_bench with quantiles and the GB/s helper over 3 * numel * element_size bytes

    Read it as a timing pattern, not as a number to reproduce. Valid timing discipline comes from the measurement section.

  3. Triton Programming ModelAI Systems Performance Engineering - Chapter 14

    Chapter 14, Writing Custom Kernels with OpenAI Triton through Triton Programming Model: tl.constexpr block sizes, the grid function, and one program per CUDA thread block

    Stop before Accessing Shared Memory in Triton. Descriptors and pipelined copies belong to the atoms unit.

2

Focus on

  • One Triton program handles one block. tl.program_id(axis=0) selects it and tl.arange builds its lane indices.
  • BLOCK_SIZE is a tl.constexpr, so each value compiles its own specialization. The grid function reads it from meta so 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 other and 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_SIZE in the grid instead of reading meta['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.
3

Try it yourself

Closed-book, before checking anything

Closed-book, write 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 .
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why does the grid function take meta instead of a fixed block size?Answer

    The same source specializes per BLOCK_SIZE value. Reading meta['BLOCK_SIZE'] keeps the grid correct for every compiled variant.

  2. 2.For and , which lanes are masked and what do their loads return?Answer

    Lanes 1000 to 1023 are masked. Their loads return other and their stores are skipped, so only the first 1000 outputs are written.

  3. 3.One program covers 1024 elements. How many threads run it?Answer

    That is a compiler decision, controlled by num_warps, not by BLOCK_SIZE. The program maps to one CUDA thread block.

  4. 4.Why is x_ptr + offsets correct without multiplying by 4 for float32?Answer

    Triton pointer arithmetic is in elements of the pointed-to type, so the element size is implicit.

5

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.gelu with 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.py on 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/ch13 and code/ch14 selectively 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.