techlist.io
OpenAI2021-07-274 min read

Curated summary

Introducing Triton: Open-source GPU programming for neural networks

Read original(opens in new tab)

Triton: Open-Source Python-Like GPU Programming for Neural Networks

Triton 1.0 is an open-source, Python-like language that lets researchers with little or no CUDA experience write GPU kernels that usually approach expert-level CUDA performance. Its main claim is that it automates the hardest parts of GPU optimization, such as memory coalescing, shared memory management, and within-SM scheduling, so developers can focus on high-level logic. The post argues this makes it practical to write fused kernels, including FP16 matrix multiplication that matches cuBLAS in under 25 lines, some up to 2x faster than equivalent PyTorch implementations.

Why Fused Kernels Matter

  • Research code typically chains native framework operators, which creates many temporary tensors and data movement that hurt performance at scale.
  • Custom GPU kernels can fix this, but writing them is difficult.
  • Existing alternatives were either too verbose, too inflexible, or produced code noticeably slower than hand-tuned baselines.
  • Triton extends an earlier language and compiler of the same name, originally created by Philippe Tillet, who now works at OpenAI.

The Challenges of GPU Programming

Optimizing CUDA code requires attention to three components of modern GPUs:

  • DRAM: memory transfers must be coalesced into large transactions to use the wide memory bus.
  • SRAM (shared memory): data must be staged manually for reuse and arranged to avoid bank conflicts.
  • ALUs: computations must be partitioned and scheduled across and within Streaming Multiprocessors (SMs) to exploit parallelism and specialized units like tensor cores.

Triton automates memory coalescing, shared memory management, and within-SM scheduling. It deliberately leaves cross-SM scheduling to the developer, since tiling and inter-SM synchronization remain algorithmic decisions that should stay flexible.

Programming Model

  • Triton resembles Numba: kernels are decorated Python functions launched over a grid of program instances identified by program_id.
  • Unlike Numba, Triton parallelizes within an instance through operations on blocks (small power-of-two-sized arrays) rather than the SIMT thread model. This hides intra-block concurrency issues such as coalescing, shared memory synchronization, and tensor core scheduling.
  • Element-wise operations gain little, but complex kernels become much simpler.
  • Fused softmax example: each instance loads and normalizes one row using NumPy-like primitives, avoiding the explicit inter-thread synchronization that a CUDA version requires.
  • Triton JIT treats tensors as raw pointers, preserving low-level control needed for complex structures such as block-sparse tensors.
  • This softmax keeps each row in SRAM throughout normalization, maximizing reuse for up to about 32K columns. PyTorch's general-purpose CUDA implementation uses temporary memory, making it more flexible but slower. The post frames the lesson as simplifying specialized kernels, not Triton being inherently superior.
  • The Torch JIT (v1.9) performed worse on the same operation, illustrating how hard it is to automatically generate efficient CUDA from high-level tensor operations.

Matrix Multiplication

  • Fused element-wise operations and reductions are necessary, but matrix multiplication is central to neural networks.
  • A Triton matmul reaches peak performance in about 25 lines of Python. The equivalent CUDA would take considerably more effort and would likely be slower.
  • The kernel groups programs together to improve L2 cache hit rates, loads blocks of A and B in a loop, accumulates with tl.dot, and stores results with boundary masks.
  • Handwritten kernels can be customized with fused input transformations (such as slicing) and output transformations (such as Leaky ReLU), which would be out of reach for most developers without Triton.

System Architecture and Compiler

  • Triton's performance comes from Triton-IR, an LLVM-based intermediate representation where multi-dimensional blocks of values are first-class.
  • The @triton.jit decorator walks the Python AST and builds Triton-IR using an SSA construction algorithm.
  • The compiler simplifies and optimizes the IR, automatically parallelizes it, and lowers it through LLVM-IR to PTX for recent NVIDIA GPUs.
  • Stashing data in shared memory is automated by analyzing operands of block-level operations like tl.dot, with allocation and synchronization handled by standard liveness analysis.
  • Programs are parallelized across SMs by running kernel instances concurrently, and within SMs by partitioning block-level iteration spaces across SIMD units.
  • At the time of the post, CPUs and AMD GPUs were unsupported, and the authors invited community contributions.

Conclusion

Triton is open source on GitHub and the authors want it to become a community-driven project. For developers who need custom GPU kernels but lack deep CUDA expertise, it offers a practical path to near-expert performance with far less code.

Continue with another curated summary.