v9.1.1
Menu

Chapter 3: Introduction to Triton

Week 2-3

Meet Triton: what it is and why it exists, how it compares to writing raw CUDA or staying in PyTorch, its block-oriented programming model, the vocabulary of programs / grids / tiles, and the @triton.jit decorator that turns a Python function into a GPU kernel.

Topics
5
Demo
1
Videos
3
Min read
16

Chapter Overview

You now understand the hardware. The question is how to program it without drowning in low-level detail. Triton is the answer this course is built around: a Python-embedded language and compiler, originally from OpenAI, that lets you write GPU kernels that rival hand-tuned CUDA — in a fraction of the code.

The key idea is that Triton raises the unit of programming from the thread to the block. In CUDA you write code from the viewpoint of a single thread and manually manage indexing, shared memory, and synchronization. In Triton you write code from the viewpoint of a program that operates on whole tiles of data as vectors and matrices. The compiler handles the painful parts — thread mapping, memory coalescing, shared-memory allocation, and synchronization — while leaving you in control of the decisions that actually matter for performance: how big the tiles are and how data moves between DRAM and SRAM.

This chapter is the conceptual bridge between the hardware chapters and the hands-on kernels that follow. By the end you'll be able to read a Triton kernel and understand exactly what each line does and where it runs.

This chapter covers:

  • What Is Triton?: a Python DSL + compiler for GPU kernels
  • Triton vs CUDA vs PyTorch: the right tool for each job
  • Block-Oriented Programming: thinking in tiles, not threads
  • Programs, Grids & Tiles: the core vocabulary
  • The @triton.jit Decorator: how a Python function becomes a kernel

Chapter Roadmap

Click any topic to jump in

Explainer videoInteractive demo
1
What Is Triton?

A Python DSL + compiler that emits fast GPU kernels from tile-level code.

A Python DSL with a Real Compiler
Where Triton fits
2
Triton vs CUDA vs PyTorch

Control where it matters, automation where it's painful — between raw CUDA and stock PyTorch ops.

The Flexibility–Performance Spectrum
The model it gives you
3
Block-Oriented Programming

Reason about tiles of data as vectors/matrices; the compiler maps them onto threads.

Tiles, Not Threads
The vocabulary you launch with
4
Programs, Grids & Tiles

A program owns a tile; the grid = ceil(work / tile) covers all the data.

The Core Vocabulary
How it becomes a kernel
5
The @triton.jit Decorator

Marks a kernel, enables [grid] launch, and specializes on tl.constexpr constants.

From Python Function to GPU Kernel

Topics

5 topics in this chapter

Chapter 2 ended with an uncomfortable conclusion: most deep-learning kernels are memory-bound, so speed comes from moving fewer bytes, staging tiles in shared memory, coalescing loads and fusing operations. Doing that in CUDA means managing thread indices, shared-memory buffers, barriers and vector widths by hand, and a fast matmul written that way runs to hundreds of lines. Most ML researchers never write one, so they are stuck with whatever kernels the framework ships. Triton was built to close that gap. It is a Python-embedded language in which a kernel is written over blocks of data, plus a compiler that turns those block operations into the thread-level code an expert would write. This topic explains what Triton actually is: a decorator, a small tl library of tile operations, and a multi-stage compiler pipeline. On an RTX 2060, a 13-line Triton vector add compiles in about 1.7 seconds on first call and then matches torch.add at about 280 GB/s. The next topic places Triton between PyTorch and CUDA, and the rest of the chapter builds its vocabulary: tiles, programs, grids and the @triton.jit boundary.

Definition

Triton is an open-source, Python-embedded domain-specific language and compiler for GPU kernels. A Triton kernel is a function decorated with @triton.jit whose body manipulates blocks of values through triton.language operations. The compiler lowers it through Triton IR, TritonGPU IR and LLVM IR to PTX and a GPU binary, deciding thread mapping, vectorization and shared-memory use itself.

1 of 1
A Python DSL with a Real Compiler

Triton kernels are Python functions decorated with @triton.jit. Inside, you call triton.language (tl) operations such as tl.load, tl.store, tl.arange, tl.dot and tl.sum, which act on whole tiles rather than scalars. The Python is never interpreted on the GPU. On first launch, Triton parses the function into Triton IR, lowers it to TritonGPU IR (where it picks a thread layout, vector widths and shared-memory staging), then to LLVM IR, then PTX, and finally a cubin binary that the driver loads. On the RTX 2060, a vector add yields 232 lines of PTX using 128-bit ld.global.v4 loads. You never wrote a thread index or __syncthreads.

Mathematical Intuition

Think of Triton as raising the abstraction level without hiding the performance model. A CUDA kernel exposes one decision per thread: which index it touches, when it synchronizes. A Triton kernel exposes one decision per tile and lets the compiler expand each tile op into SIMT code. For a 1,024-element tile with the default 4 warps, that expansion is 1024/128=81024 / 128 = 8 elements per thread, issued as two 4-wide vector loads per input. You keep the high-leverage knobs (tile shape, what to load, what to fuse) and shed the low-leverage ones.

Example:

Why can a researcher prototype a custom fused kernel in Triton in an afternoon, when the equivalent CUDA kernel might take days?