Writing Custom Triton Kernels for Memory-Efficient Attention
An in-depth look into GPU memory bandwidth bottlenecks, tile scheduling, and writing custom block-level kernels using OpenAI's Triton compiler.
When scaling transformer training and inference, memory bandwidth is almost always the bottleneck rather than compute floating-point capability. Standard PyTorch implementations of multi-head attention compute intermediate activations into Global Memory (HBM).
For sequence lengths exceeding 8,000 tokens, this leads to quadratic memory growth () and severe bus traffic between DRAM and SRAM.
The Memory Hierarchy Bottleneck
On modern hardware like the NVIDIA A100 or H100, Global Memory is vast but slow (approx. 2TB/s bandwidth), whereas SRAM (L1/Shared Memory) operates at over 19TB/s but is constrained in capacity (192KB per SM).
Our custom Triton kernel aims to:
- Load block-wise tiles of Query (), Key (), and Value () directly into SRAM.
- Compute softmax locally using running max and exponent normalization statistics.
- Stream outputs back to Global Memory without materializing the attention matrix.
import torchimport tritonimport triton.language as tl @triton.jitdef _attn_fwd_kernel( Q, K, V, sm_scale, Out, stride_qz, stride_qh, stride_qm, stride_qk, BLOCK_M: tl.constexpr, BLOCK_N: tl.constexpr): start_m = tl.program_id(0) off_m = start_m * BLOCK_M + tl.arange(0, BLOCK_M) # Load Query tile into SRAM registers q = tl.load(Q + off_m[:, None] * stride_qm)By leveraging this block-level fusion, memory traffic drops from to , unlocking a 2.5x speedup on sequence lengths larger than 4,096.