IIntroduction to AI Systems Performance EngineeringTechnical textbook
Chapter 11

Triton, CUTLASS, and PyTorch Operators

Select, implement, and integrate GPU kernels

Choose the right kernel authoring level, integrate custom operations correctly, and make them composable with autograd and compilation.

Suppose a PyTorch profile shows that layer normalization and a following residual addition take 180 microseconds for a particular tensor shape. A developer can express a fused replacement as ordinary PyTorch operations, ask torch.compile to generate code, write a Triton kernel, assemble a CUTLASS operation, or write CUDA C++. Every route can compute the same numbers. The routes differ in how directly they expose thread assignment, memory layout, synchronization, matrix instructions, automatic differentiation, and integration with the rest of PyTorch.

Chapter 11 follows one optimized operation from a Python call to GPU execution. The first lesson compares implementation options using evidence from the missing capability, not a blanket ranking of languages. The second lesson launches a small Triton vector-add program and connects program identifiers, blocked offsets, masks, and compile-time parameters to the output array. The third lesson registers an operation with PyTorch so fake tensors, autograd, autocast, and torch.compile can reason about the operation without executing unsafe guesses.

Correct output from one contiguous FP16 tensor does not make a production operator complete. Real framework calls can contain odd sizes, transposed strides, non-contiguous views, BF16 values, gradient recording, graph capture, and devices that lack a specialized instruction. An operator test suite must make those cases observable. The fast implementation also needs an eligibility predicate: a Boolean decision that sends only supported inputs to the optimized kernel and sends every other valid input to a correct fallback.

PyTorch code begins on a central processing unit (CPU) and launches the selected work on a graphics processing unit (GPU). CUTLASS commonly builds matrix multiplication (GEMM) kernels. Triton and compiler tools can print an intermediate representation (IR), which is the compiler-owned program form between source code and target instructions. The lesson connects each printed artifact to one measured launch.

The final lesson constructs the evidence expected for a maintained or upstream contribution. A benchmark table covers representative shapes, including shapes with no improvement. Correctness tests state tolerances and edge cases. Compiler logs show whether the operator composes with graph capture. The resulting lesson treats kernel code as one part of a software interface that must remain correct as models, devices, and framework versions change.

Prerequisites

  • Chapters 8–10: kernel schedules and numerical requirements
  • Python, PyTorch tensors, and autograd
  • C++ templates and CUDA builds at a reading level

Learning objectives

  • Select CUDA, Triton, CUTLASS, a library, or compiler generation from required control
  • Implement the metadata and gradient contracts of a PyTorch custom operation
  • Prepare correctness and performance evidence for a maintained kernel path
Lessons

Study this chapter

  1. 11.1
    Kernel implementation options

    Select an implementation level from the required schedule control and complete software lifecycle

    Begin lesson →
  2. 11.2
    Triton programming model

    Map an output tile to Triton program instances, pointer blocks, masks, and configuration parameters

    Begin lesson →
  3. 11.3
    PyTorch custom operator requirements

    Define schema, fake-tensor, autograd, autocast, dispatch, and fallback behavior for a custom operation

    Begin lesson →
  4. 11.4
    Production and upstream integration

    Specify an optimized input region and provide reproducible correctness, performance, and maintenance evidence

    Begin lesson →
Chapter projects

Extended exercises

Complete these projects after the lesson-level practice tasks. Each project combines several mechanisms from the chapter.

01
Check

Triton trio

Implement vector add, fused softmax, and one reduction in Triton.

Evidence: Correctness and configuration sweeps across shapes.

02
Practice

Framework contract

Integrate one custom operation with schema, fake behavior, and autograd or an explicit non-differentiable contract.

Evidence: Operator checks, compile test, and benchmark.

03
Challenge

Upstream candidate

Find and prepare a narrow issue or contribution in PyTorch, Triton, vLLM, or SGLang.

Evidence: Reproducer, tests, benchmark matrix, and a review-ready design note.

References

Sources and further reading

The lesson explanations synthesize primary papers and official references. Follow the originals for proofs, exact APIs, architecture details, and version-sensitive behavior.

  1. Triton Tutorials
  2. NVIDIA CUTLASS
  3. Custom C++ and CUDA Operators
  4. torch.compile Programming Model
  5. Nsight Compute Profiling Guide