What is FlashAttention?

FlashAttention is an attention algorithm that splits the computation into small blocks done in the GPU's fast on-chip memory, cutting reads and writes to the GPU's slower main memory (HBM) while computing the exact result rather than an approximation.

Its relation to the attention mechanism is that of function to implementation: the mechanism defines what the model computes, while FlashAttention performs the same computation with less memory traffic. PagedAttention solves a different problem, splitting the KV cache into pages to reduce memory fragmentation.

Topic
Inference and performance
Also known as
Flash attention
Origin
2022, Dao et al.

How FlashAttention works

  1. Query, key and value matrices are split into blocks moved into the GPU's fast on-chip memory (SRAM).
  2. Attention scores and softmax are computed per block, with softmax totals updated across blocks.
  3. The full attention matrix is never built in main memory; only the result and small intermediate statistics are written.
  4. In training the attention matrix is not stored for backpropagation; blocks are recomputed when needed.

In more detail

Online softmax and recomputation in the backward pass

FlashAttention relies on online softmax rescaling: as each key-value block is loaded into SRAM, running maximums and normalisation denominators are updated incrementally without materialising the full N×N score matrix in HBM. In training, only the final output and log-sum-exp statistics are saved and attention blocks are recomputed during the backward pass.

FlashAttention-2, FlashAttention-3 and the Blackwell generation

FlashAttention-3 (2024) overlaps tensor-core matrix multiplies with asynchronous memory transfers (TMA/WGMMA) on Hopper GPUs and introduces block-wise FP8 support with incoherent processing to keep quantisation error low. FlashAttention-4 extends these IO-aware kernels to Blackwell architectures.

Linear memory versus quadratic compute

While FlashAttention shrinks attention activation memory from O(N^2) to O(N), total compute FLOPs remain O(N^2) (slightly higher during training due to backward recomputation). Therefore, at very long context lengths (e.g. 128K, 1M tokens), prefill latency still grows quadratically unless paired with ring/sequence parallelism or sparse/sliding-window patterns.

Commonly confused terms

TermHow it differs
FlashDecodingFlashAttention computes exact attention by tiling over Q, K and V (effective for training and long-prompt prefill), whereas FlashDecoding splits along the KV sequence length to parallelise single-token autoregressive decoding when batch size is small.
Linear or approximate attentionLinear attention / state-space mechanisms modify the mathematical formulation to reduce compute FLOPs from O(N^2) to O(N), whereas FlashAttention keeps the exact mathematical attention formula with O(N^2) FLOPs and only cuts HBM memory footprint from O(N^2) to O(N) while reducing HBM IO through tiling.

Why FlashAttention matters for on-premises AI

In standard attention the intermediate matrix needs memory growing with the square of sequence length; FlashAttention never stores it, so extra memory stays linear. The same GPU can then handle longer contexts and larger batches, getting more from hardware in in-house systems working with long documents. Results match standard attention mathematically, with only tiny floating-point differences from operation order. Supported GPU generations and version compatibility are checked before installation.

ON-PREMISES EXAMPLE

A software company runs its in-house code assistant on an inference engine with FlashAttention; processing long source files in one prompt leaves room for more concurrent requests on the same GPU.

Common questions

Does FlashAttention change the result?

No, it gives the same result as standard attention mathematically. Only tiny floating-point differences from operation order may appear; it is not an approximation.

How does FlashAttention differ from PagedAttention?

FlashAttention cuts memory traffic in attention computation. PagedAttention splits the KV cache into pages to reduce memory waste; the two can be used together.

Sources

In the glossary: Glossary, D to L

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.