How tiling and kernel fusion eliminate the N x N memory bottleneck, making long-context inference practical.
The key insight: SRAM is ~1000x faster than HBM but ~1000x smaller. FlashAttention keeps data in SRAM.
Standard attention writes the full N x N attention matrix to HBM. For a 128K sequence:
Side by side: standard attention writes everything to HBM. FlashAttention tiles the computation.
FlashAttention processes the attention matrix in tiles that fit in SRAM. Click "Animate" to watch.
The full NxN matrix is never materialized. Instead, we process it tile by tile:
Avoid materializing the full N x N attention matrix in HBM. This is what makes long context possible.
Drag the slider to see how memory usage diverges as sequence length increases.
Each version brought significant improvements in speed and hardware utilization.
By processing attention in tiles that fit in SRAM, FlashAttention avoids the O(N^2) HBM bottleneck entirely.
The mathematical trick: compute softmax incrementally across tiles using running max and sum, then rescale. Exact results, no approximation.
Without FlashAttention, 128K context would need 32 GB per attention head. With it, memory stays constant regardless of sequence length.
vLLM, SGLang, TensorRT-LLM, PyTorch all use FlashAttention by default. It is the single most impactful inference optimization.