FlashAttention: The Algorithm That Beat the Memory Wall

How a tiled, IO-aware CUDA kernel turned exact attention into something GPUs can actually afford to run at scale.

11 to 14 min read • View on GitHub • More from hillfate

A vast stone attention matrix hangs over a GPU city while data streams try to cross a river labeled global memory. A small tiled bridge carries compact blocks into a tiny fast chamber marked SRAM, showing that the bottleneck is moving data, not doing math. The scene explains why FlashAttention wins by keeping work close to the chip.
FlashAttention treats attention as a memory-traffic problem. The win comes from moving less data through slow global memory.

FlashAttention is a fast, memory-efficient exact attention algorithm. It uses tiling to reduce the number of memory reads/writes between GPU High Bandwidth Memory (HBM) and GPU on-chip SRAM.

Tri Dao, Lead Author & Maintainer · Dao-AILab/flash-attention GitHub README
Key Takeaways

The Real Bottleneck Was Never Compute

Standard attention looks like a math problem, but on a GPU it behaves like a traffic problem. The expensive part is not the dot products themselves. It is the constant shuttling of a massive N × N score matrix through slow global memory while the chip waits.

That is the idea FlashAttention turns inside out. Instead of materializing the whole matrix, it streams small tiles of Q, K, and V through fast on-chip memory, does the work there, and writes out only what is needed. Exact attention stays exact. The memory bill drops.

Online softmax is the trick that makes exact attention streamable. The kernel updates normalization incrementally, so it never needs the full score matrix in memory.

A close-up mechanical assembly line moves three adjacent tiles of Q, K, and V through a small chamber. A worker stamps a running max and a normalization constant on a ledger while partial outputs stack forward tile by tile. The full matrix never appears, emphasizing the online softmax update that makes exact attention streamable.
The kernel does not wait for a full row of scores. It updates the softmax state as each tile passes through.

A Tile, a Register File, and a Trick Called Online Softmax

The core algorithm is deceptively simple once you see it. The kernel takes a block of queries, multiplies it against a block of keys, and uses that partial result to update two running values: the maximum score seen so far and the normalization constant for softmax. Then it folds the matching value block into the output accumulator.

That is what lets FlashAttention stay exact without ever storing the full matrix. The algebra is arranged so each tile can revise the previous state instead of forcing a complete replay. The register file and shared memory become the working set. HBM becomes a brief checkpoint, not the main workspace.

for each Q_tile:
  m = running_max
  l = running_lse
  O = running_output

  for each K_tile, V_tile:
    S = Q_tile @ K_tile^T
    m_new = max(m, max(S))
    P = exp(S - m_new)
    l_new = exp(m - m_new) * l + sum(P)
    O = (exp(m - m_new) * l / l_new) * O + (P @ V_tile) / l_new
    m, l = m_new, l_new

write O

How the Repo Connects Python to Bare-Metal CUDA

The repository is a stack, not a single trick. In flash_attn_interface.py, Python registers the operator and handles dispatch. In flash_api.cpp, C++ translates PyTorch tensors into a launchable parameter struct. In flash_fwd_kernel.h, the CUDA kernel executes the tiled algorithm with the right specialization for the hardware.

That bridge matters because performance code fails when the glue is sloppy. The host side has to preserve tensor strides, data types, masking mode, dropout, and sequence layout. The kernel only gets to be fast if the parameters that reach it are already precise.

A WSJ-style hedcut portrait of Tri Dao, preserving his likeness from his GitHub avatar. The portrait is used to anchor the paper and repository work behind FlashAttention and to show that the algorithm came from careful systems research, not a generic optimization pass.
A WSJ-style hedcut portrait of Daniel Y. Fu, preserving his likeness from his GitHub avatar. The portrait supports the historical and research context around the original FlashAttention work.

The major bottleneck in Transformer training is the self-attention mechanism, which is O(T^2) in sequence length. FlashAttention is a critical optimization that makes this O(T) in memory and much faster in practice.

Andrej Karpathy, Founding Member, OpenAI · Let's build GPT: from scratch, in code, spelled out.

Why Templates Matter Here

FlashAttention is not one kernel. It is a family of compiled variants. The code specializes for head sizes, masking modes, dropout, GPU generation, and layout details. That specialization removes inner-loop branches and lets the compiler generate a kernel that matches the shape of the workload.

This is why the repo feels stricter than a normal library. It is not trying to be maximally generic. It is trying to be exact and fast for a narrow but important class of Transformer workloads.

DimensionGeneric attentionFlashAttention
Matrix handlingMaterializes large score matricesStreams tiles and avoids the full matrix
Memory pressureHeavy HBM reads and writesKeeps working data in SRAM and registers
Kernel shapeOne path with more branchingMany specialized templates
ExactnessExactExact
Best use caseConvenience and baseline useLong-context, production-grade training

Varlen Is the Quiet Superpower

Real batches are messy. Some sequences are short, some are long, and padding them all to the same length wastes memory and compute. FlashAttention’s varlen path accepts packed sequences, so the kernel can process uneven inputs without paying for dead tokens.

That matters because production data rarely behaves like a clean benchmark. Varlen is not the headline feature, but it is the kind of feature that decides whether a fast kernel is useful outside a paper.

Batch stylePadded attentionVarlen FlashAttention
Sequence shapeUniform length after paddingPacked variable-length sequences
WasteTokens and memory spent on paddingOnly real tokens are processed
ThroughputDrops as padding growsImproves on messy real batches
PracticalityEasy but expensiveMore complex, but production-friendly

FlashAttention vs. the Old Way

The comparison that matters is not only speed. It is exactness, memory traffic, and whether long context stays viable as sequence length grows. Approximate methods like Reformer or Performer change the math to dodge the quadratic wall. FlashAttention keeps the math and fixes the memory path.

MethodExact?Full matrix materialized?HBM trafficLong-context fit
Standard PyTorch attentionYesYesHighPoor once sequences get long
FlashAttentionYesNoLowStrong
xFormers-style optimized attentionUsually yesSometimes reduced, but not the core breakthroughModerateGood, depending on backend
Approximate methodsNoNoVariesOften good, but with accuracy trade-offs

What the Repo Says About the GPU Era

FlashAttention is bigger than a kernel. It is a reminder that modern performance work is about respecting the memory hierarchy. When hardware gets more capable, the winning software is often the one that moves less, not the one that computes more.

That is why this repo became infrastructure. It is a software pattern disguised as an optimization: tile the problem, keep the hot data close, and make the expensive path disappear.