Triton is an open-source language and compiler for GPU programming that lets developers write high-performance GPU kernels in a Python-like syntax instead of low-level CUDA C++. Originally created by Philippe Tillet and maintained by OpenAI, which is why it is often called OpenAI Triton, it belongs to the AI frameworks & tools ecosystem of artificial intelligence as the layer that turns tensor operations into efficient machine code.
Its central idea is block-level programming: the developer describes what one program instance does with a tile of data, and the compiler decides how threads, registers and shared memory carry out that work. Not to be confused with Triton Inference Server, which is an unrelated NVIDIA model-serving product.
How Triton compiles block-level kernels
A Triton kernel is a Python function decorated with @triton.jit that the compiler translates into native GPU code at first call. The function never runs in the Python interpreter; Triton parses its source, builds an intermediate representation and caches the compiled binary for each combination of argument types and compile-time constants.
The compilation pipeline is built on MLIR. Triton source becomes the Triton IR, then a GPU-specific dialect where layouts, memory coalescing and tensor core usage are decided, then LLVM IR, and finally PTX for NVIDIA hardware or AMDGCN for AMD hardware. The table below summarizes each stage and its purpose.
| Stage | Representation | Main decisions |
|---|---|---|
| Front end | Python AST | Type inference, constant specialization |
| Triton IR | MLIR dialect | Block-level tensor operations |
| TritonGPU IR | MLIR dialect | Data layouts, pipelining, coalescing |
| LLVM IR | LLVM | Register allocation, scalar optimization |
| Backend | PTX or AMDGCN | Final machine code for the device |
At launch time, the developer specifies a launch grid, the number of program instances to run. Inside the kernel, tl.program_id identifies the current instance, tl.arange builds index vectors, and tl.load and tl.store move whole blocks between global memory and registers. A boolean mask guards loads and stores at the edges of tensors whose sizes are not multiples of the block size.
Core abstractions and parameters
Triton exposes a small set of primitives that cover most deep learning kernels. Block sizes are declared as tl.constexpr parameters, which means they are known at compile time and the compiler can specialize the generated code for each value. The following table lists the parameters that most strongly influence performance.
| Parameter | Typical values | What it affects |
|---|---|---|
| BLOCK_SIZE | 64 to 1024 elements | Work per program instance, register pressure |
| num_warps | 2, 4 or 8 | Threads per program, latency hiding |
| num_stages | 2 to 5 | Software pipelining of memory loads |
| Tile shape (M, N, K) | 16 to 256 per dimension | Tensor core utilization in matrix multiplication |
| Mask | Boolean block | Correctness at tensor boundaries |
The @triton.autotune decorator accepts a list of candidate configurations and benchmarks them on the target device, then stores the fastest one for each input shape key. Autotuning matters because the best tile size depends on the GPU generation, the data type and the problem size, and a configuration that is optimal on one accelerator can be noticeably slower on another.
Triton also offers reductions such as tl.sum and tl.max, the tl.dot operation that maps to tensor core instructions, and atomic operations for accumulation across program instances. Recent versions add experimental lower-level control over memory layouts for developers who need it.
Triton compared with CUDA and other compilers
Triton occupies a middle ground between handwritten CUDA and fully automatic compilers. CUDA gives total control over every thread, while graph compilers such as XLA or Apache TVM generate code from a model description with little developer input. Triton keeps the developer in charge of the algorithm and leaves thread-level scheduling to the compiler.
| Approach | Programming unit | Control level | Typical user |
|---|---|---|---|
| Triton | Block of data | High for algorithm, automatic for threads | ML researchers and framework engineers |
| CUDA C++ | Individual thread | Complete | Performance specialists |
| CUTLASS | C++ templates for tiles | Very high | Library authors |
| XLA | Whole computation graph | Low, compiler-driven | JAX and TensorFlow users |
| cuDNN | Prebuilt operators | None, call only | Framework developers |
Inside PyTorch 2, the TorchInductor backend of torch.compile emits Triton kernels automatically for fused pointwise and reduction operations. As a result, many users run Triton-generated code without writing any Triton themselves, while hand-written Triton kernels remain the tool of choice for custom operators.
When to write custom Triton kernels
A custom Triton kernel is worthwhile when an operation is memory-bound and fusable, or when no vendor library implements it efficiently. Kernel fusion reduces round trips to high-bandwidth memory, which is often the dominant cost in transformer workloads. The decision table below summarizes common situations.
| Situation | Recommended choice | Reason |
|---|---|---|
| Standard dense matrix multiplication | Vendor library | cuBLAS and similar are highly tuned |
| Fused normalization and activation | Triton kernel | Removes intermediate memory traffic |
| Custom attention variant | Triton kernel | Fast iteration with good performance |
| Whole-model speedup with no new code | torch.compile | Generates Triton kernels automatically |
| Warp-level tricks or new hardware features | CUDA or CUTLASS | Needs control below block level |
Well-known examples include FlashAttention-style kernels, fused layer normalization, rotary position embeddings, quantized matrix multiplication and fused optimizers. Fine-tuning libraries such as Unsloth rely on Triton kernels to reduce memory use and training time on consumer GPUs.
Limitations and common mistakes
Triton trades some control for productivity, and that trade-off has practical consequences. Hardware support centers on recent NVIDIA GPUs and AMD Instinct accelerators through ROCm, with other backends maturing more slowly. Kernels that need explicit warp-level communication or the newest hardware features may still require CUDA.
- Missing masks at tensor edges cause out-of-bounds reads or silently wrong results.
- Oversized blocks exhaust registers, force spills to local memory and reduce occupancy.
- Benchmarking without warm-up measures compilation time instead of kernel time.
- Skipping autotuning leaves performance dependent on one fixed, often suboptimal configuration.
- Ignoring numerical precision in reductions with float16 produces accuracy drift; accumulate in float32.
Debugging is harder than in ordinary Python because the kernel runs as compiled code. The interpreter mode, enabled with the TRITON_INTERPRET=1 environment variable, executes kernels on the CPU for step-by-step inspection, at the cost of much lower speed.
The idea in brief
A graphics card can do thousands of small calculations at once. Programming it directly is hard. You must tell each tiny worker exactly what to do. Triton makes this easier. You describe the job for one chunk of data. The compiler then organizes the workers for you.
Think of a kitchen manager. You hand over a recipe for one tray of cookies. The manager decides which cooks use which ovens. The result is fast code with far less effort, written in a style close to Python.
A practical scenario
A machine learning team trains a mid-sized language model on eight data center GPUs. Profiling shows that the sequence of layer normalization, a bias addition and a GELU activation launches three separate kernels. Each kernel reads and writes the full activation tensor, so the GPUs spend more time moving data than computing.
An engineer writes a single Triton kernel of about 60 lines that performs all three steps on one block of rows in registers. After autotuning over block sizes of 1024 and 2048 and four or eight warps, the fused kernel cuts memory traffic for that part of the model by roughly two thirds. The overall training step becomes several percent faster, which saves many GPU hours across a multi-week run without any change to the model architecture.