Unit 3 of 10 · about 5 hours · implement, measure

Specialization, autotuning and compile-cost accounting

Autotuning buys speed once and charges compile time on every new shape. Without a key and a gate, it silently serves the wrong fast kernel.

You'll be able to

  • Design an autotune space of block sizes, warp counts and stages with a key that separates every varying shape.
  • Gate every candidate on a correctness check before timing, and reject invalid configurations.
  • Separate cold compile and autotune time from warm steady-state time in every reported number.

Covers

Compile-time meta-parameters and specializationAutotune configuration, keys, pruning and cache identityCorrectness-gated candidates and invalid configuration rejectionCompile, autotune, cold-cache and warm-cache accounting
1

Read

In this order, only the parts listed

0 of 3 done

  1. Matrix Multiplication: tuning and cache reuseTriton Programming Guide and Tutorials

    L2 Cache Optimizations: grouped pid mapping with GROUP_SIZE_M; Final Result: get_cuda_autotune_config block sizes with num_stages and num_warps, and @triton.autotune with key=['M', 'N', 'K']

  2. Autotuning and compile costAI Systems Performance Engineering - Chapter 14

    Chapter 14, Autotuning with TorchInductor and Autotuning Triton Kernels: per-kernel config cache, representative warmup inputs, and max-autotune compile cost

  3. PA2 Part 1: grid search and local testingUCSD CSE 291A/DSC 291 (Spring 2026) PA2 Part 1 - Triton matmul, add and ReLU

    Part 1, sections 1.1 to 1.6: tile assignment through grid search over BLOCK_M, BLOCK_N, BLOCK_K, num_warps and num_stages; Part 1 Testing with python student_local_test.py student_kernel.py

    The remote grader needs a course token. Replace its speedup score with your own interleaved timing against the PyTorch reference on your GPU.

2

Focus on

  • The key must list every launch-varying value. A missing dimension reuses a fast config for the wrong shape.
  • Candidates are correctness-gated: a config that fails the reference never reaches timing.
  • First-call cost mixes compilation, autotuning and execution. Only warm calls measure the kernel.
  • Tune on a representative workload, not on the final benchmark shapes.
Common mistakes · 3
  • Benchmarking candidates without warmup. Compile time then votes for the smallest tile, not the fastest kernel.
  • One config for all shapes. A tall-skinny winner can lose on square shapes, and the key exists to stop that.
  • Treating the course grader number as portable. Retime the submitted KERNEL_CONFIGS on your own GPU before claiming anything.
3

Try it yourself

Closed-book, before checking anything

Time torch.compile(lambda x, y: torch.relu(x + y) * 2) on 1024 by 1024 fp32 tensors, host-timed with synchronization: with PyTorch 2.14.1 and an empty Inductor cache on an RTX 3050 6 GB with CUDA 13.4, the first call took about 1.2 to 1.9 s and warm calls about 0.1 ms; yours will differ, so compare cold against warm on your GPU. State which interval pays for compilation and what a warm Inductor cache changes, then run the matmul tutorial autotuned kernel twice on one shape and show the second launch skips benchmarking.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Two shapes share and but differ in , and the key is ['M', 'N']. What goes wrong?Answer

    The second shape reuses the first shape winner without benchmarking. changes tile reuse, so it belongs in the key.

  2. 2.A candidate is twice as fast but fails the reference at ATOL=0.15. Keep it?Answer

    No. Correctness gates timing, so a fast wrong kernel is rejected.

  3. 3.A cold call takes seconds and warm calls take under a millisecond. Which part is the kernel?Answer

    The warm calls. The cold call pays compilation and autotuning once per key.

  4. 4.Why tune on production-like shapes instead of the evaluation set?Answer

    Tuning on the evaluation set overfits the selection. Unseen shapes then pay for a config chosen for another distribution.

5

Practice

  • UCSD CSE 291A PA2 Part 1: Triton matmul-add-ReLU

    Implement matmul_add_relu_kernel_fp16 for the fused add-plus-ReLU epilogue with fp16 inputs and output and an fp32 accumulator, plus 1 to 5 KERNEL_CONFIGS; pass student_local_test.py with 512-wide correctness and 2048-wide timing

    The remote grader needs a course token, so replace its speedup score with your own interleaved timing against the PyTorch reference on your GPU.

You're done when

Your configs pass the local correctness and timing tests, and every reported number names its key, its warmup, and whether the cache was cold or warm.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 9 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.

  • 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.

  • 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

  • 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.