FlashAttention

The paper introduced an IO-aware exact attention algorithm that avoids materializing the full attention matrix in high-bandwidth memory, making Transformer attention faster and more memory-efficient without changing model semantics.

Metadata

FieldValue
PaperFlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
AuthorsTri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Re
Year2022
VenueNeurIPS 2022
arXiv2205.14135
OpenReviewH4DqfPSibmx
CodeDao-AILab/flash-attention
Statusfull note started

One-Line Takeaway

FlashAttention keeps exact softmax attention but changes the computation schedule: tile , stream blocks through fast on-chip memory, maintain online softmax statistics, and avoid writing the attention matrix to HBM.

Question

Standard scaled dot-product attention is:

where:

The attention matrix is:

The architecture/systems question is:

FlashAttention answers yes by making attention IO-aware.

Main Claim

Transformer attention is often bottlenecked by memory traffic, not only by FLOPs. An exact attention algorithm that minimizes reads and writes between GPU HBM and SRAM can reduce memory use and wall-clock time while preserving the same mathematical attention output.

The durable pattern is:

Architecture Contract

ItemContract
Inputquery, key, value matrices
Outputsame exact attention output as standard softmax attention in real arithmetic
Main tricktiling, online softmax, recomputation for backward pass
Does it approximate attention?no, the main algorithm computes exact attention
Main resource targetHBM reads/writes and activation memory
Architecture roledrop-in attention primitive for Transformers
Domainlong-context language models, vision Transformers, sequence models, attention-heavy architectures

This is not a new attention score:

It is a new way to compute that expression.

Why Standard Attention Wastes Memory

Naive attention often materializes intermediate matrices:

The matrix has entries:

Even if matrix multiplication is fast, moving and between memory levels can become the bottleneck.

Memory levelRoleProblem
SRAM / shared memoryfast, small, on-chipcannot hold full attention matrix
HBMslower, large GPU memoryexpensive to repeatedly read/write
registersfastest, very smallonly hold partial reductions

FlashAttention optimizes the schedule around this hierarchy.

Tiled Attention

Split into blocks:

For a query block and key/value block , compute local scores:

Instead of writing all and to HBM, FlashAttention keeps block-level quantities in faster memory and updates the output incrementally.

The high-level loop is:

Online Softmax

Softmax seems to require all scores at once:

FlashAttention uses online normalization so blocks can be processed without storing the full row.

For one query row, maintain:

and the weighted value accumulator:

When a new block arrives with block maximum , update:

The final output is:

This preserves exact softmax semantics while streaming blocks.

Backward Pass and Recomputation

Training needs gradients through attention. A naive implementation stores large intermediate matrices for backward:

FlashAttention avoids storing and by recomputing block-level attention pieces during the backward pass. This trades extra compute for much lower memory traffic.

The key tradeoff is:

On modern accelerators, this can be faster because memory movement is often more expensive than the additional arithmetic.

Exact vs Approximate Attention

Many efficient-attention papers reduce cost by changing the attention function:

RouteExampleTradeoff
low-rank approximationapproximate may change model quality
kernelized attentionapproximate softmax kernelmay change numerical behavior
sparse attentionrestrict visible tokenschanges receptive field
FlashAttentionexact attention, new schedulesame function, hardware-aware implementation

This distinction matters. FlashAttention is not a new inductive bias like local attention or sparse attention. It is a computational primitive that makes the standard Transformer more practical.

Relation to Other Architecture Notes

PaperSimilarityDifference
Attention Is All You Needdefines scaled dot-product attention as the Transformer coreFlashAttention changes implementation, not the mathematical layer
Performertargets efficient long-sequence attentionPerformer approximates softmax attention with random features
Transformer-XLhelps long-context sequence modelingTransformer-XL changes recurrence/memory; FlashAttention changes kernel scheduling
S4long-sequence efficiency motivationS4 changes sequence layer family; FlashAttention keeps attention
Mambaresponds to attention scaling limitsMamba replaces attention with selective state-space layers
LLaMAmodern decoder-only Transformer recipeLLaMA-style models often rely on efficient attention kernels in practice

Evidence to Read

EvidenceWhat it supportsWhat it does not prove
IO complexity analysisHBM access matters and can be reducedevery hardware/kernel implementation is equally efficient
wall-clock training speedupsexact attention can be faster in real systemsspeedup is constant across all sequence lengths and GPUs
memory savingslonger contexts or larger batches become feasiblemodel quality improves automatically
long-range benchmarksefficient exact attention helps enable longer-context experimentsattention is always better than SSMs, recurrence, or sparse alternatives

Why This Matters

FlashAttention is a key architecture paper because it changes how to read Transformer scaling. The bottleneck is not only:

It is also:

For long-context LLMs, the difference between “the layer is mathematically possible” and “the model can actually train or serve” often depends on kernels like FlashAttention.

Limitations

FlashAttention does not remove the arithmetic cost of dense exact attention. It reduces memory traffic and activation memory, but dense attention still scales quadratically in sequence length.

Speedups depend on hardware, sequence length, head dimension, batch shape, precision, masking pattern, kernel implementation, and framework integration. A paper using FlashAttention should still report the exact setup.

FlashAttention is not an alignment, reasoning, retrieval, or agent method. It makes attention cheaper; it does not solve whether the model has the right data, objective, evaluation, or tool-use behavior.

Common Misreadings

”FlashAttention is approximate attention.”

No. The main FlashAttention algorithm computes exact softmax attention, but schedules it differently.

”FlashAttention makes attention linear.”

No. Dense exact attention still has quadratic pairwise interactions. FlashAttention reduces memory traffic and memory footprint.

”FlashAttention is only a systems trick.”

It is a systems-aware architecture primitive. It changes what Transformer configurations are practical, especially for long-context training and inference.

What to Remember

When reading a Transformer paper that claims long context or efficient attention, ask:

  • Is attention exact, approximate, sparse, recurrent, or replaced?
  • Does the method reduce FLOPs, memory traffic, activation memory, or all three?
  • Is speed measured as kernel throughput, training throughput, or end-to-end latency?
  • Does the result depend on sequence length, head dimension, batch shape, GPU generation, or precision?
  • Are causal masks, attention bias, dropout, and backward pass included?
  • Is the memory bottleneck HBM traffic, KV cache, activations, or parameters?

The compact mental model is: