Guide

Fast attention

Attention can be made much faster without changing its result, just by moving less data.

Kernels

A kernel is a small program the GPU runs on many threads at once, each on its own piece of the data. A model runs as many kernels, one after another. Each one reads its inputs from HBM and writes its results back. Kernels need parallel work; a chain where each step waits on the last is a poor fit.

Tiling

Tiling splits big matrices into tiles that fit in the small, fast on-chip SRAM. A tile is loaded once and reused many times before the next one comes in. That cuts slow trips to HBM, so the math units stay busy.

Online softmax

For N tokens, attention makes a score for every pair: an N by N table. At 8,000 tokens that's 64 million scores per head. Plain attention writes this table to HBM and reads it back. Online softmax works block by block, keeping a running max and sum, and corrects earlier results as new blocks arrive. It never needs the whole table at once, and the answer is exactly the same.

FlashAttention

FlashAttention combines tiling and online softmax. The score table stays in SRAM tiles and never goes to HBM. The result is exact, the memory traffic drops a lot, and attention runs faster. Its extra memory grows with prompt length, not with its square.

Triton

CUDA is NVIDIA's language for kernels, where you manage every thread by hand. Triton lets you write kernels in Python-like code, one block at a time, and its compiler handles the threads. PyTorch's compiler also writes Triton kernels. It is not the same thing as NVIDIA's Triton Inference Server.

Remember

  • A kernel runs one program on many threads.
  • Tiles keep data in fast SRAM and reuse it.
  • Online softmax works block by block, with exact results.
  • FlashAttention: same answer, far less HBM traffic.
  • Triton makes kernels easier to write than raw CUDA.