FlashAttention
Follow tiled exact attention and the online-softmax invariant that avoids a full intermediate score matrix.
Jump to a section
Why this matters
FlashAttention is an implementation strategy for exact softmax attention. It reduces data movement between GPU high-bandwidth memory (HBM) and on-chip memory by tiling the computation. It does not replace softmax with a linear attention rule.
That distinction matters when reading model papers: an attention architecture and the kernel that executes it are separate choices. A dense attention layer can use an efficient kernel while still doing quadratic arithmetic in sequence length.
Build the mental model
A conventional implementation can materialize scores and probabilities for every query–key pair. FlashAttention processes blocks and keeps running row statistics, avoiding those full intermediate matrices in HBM. The visual compares an illustrative score tile with the full matrix; it is not a GPU memory profiler.
The hard part is not splitting a matrix into tiles. It is combining softmax contributions when a later tile contains a larger score. Earlier contributions must be rescaled into the new exponent range before adding the next block.
Same attention. Less memory traffic.
Tiles of Q, K and V move through fast on-chip memory. An online softmax combines their contributions without storing the full score or probability matrix in high-bandwidth memory.
- Dense score entries / head
- 16,777,216
- One 64 × 64 score tile
- 4,096
Element counts with fixed head width 64. Excludes model weights, other intermediates, batching and layers; not peak GPU memory or a speed benchmark.
Exact dense softmax attention up to floating-point rounding. Arithmetic remains quadratic in sequence length; this is not a linear-attention architecture. Dao et al., 2022
Work through the math
For one query row, keep the maximum , denominator , and unnormalized weighted-value numerator . For a new score block with values :
After the final block, output . If you store a normalized output instead of , its update must also include the old denominator. Mixing those two conventions is a common implementation bug.
For fixed head width, dense attention still forms a quadratic number of pairwise interactions. Avoiding a stored matrix changes intermediate storage and memory traffic, not that arithmetic count.
Read the implementation
This executable scalar-value example checks the online invariant. It is a correctness illustration, not a GPU kernel.
1from math import exp
2scores, values = [1., 2., 1001., 1002.], [2., -1., 4., 8.]
3m, denominator, numerator = float('-inf'), 0., 0.
4for start in range(0, len(scores), 2):
5 block = scores[start:start + 2]
6 new_m = max(m, max(block))
7 scale = exp(m - new_m)
8 denominator *= scale
9 numerator *= scale
10 for offset, score in enumerate(block):
11 weight = exp(score - new_m)
12 denominator += weight
13 numerator += weight * values[start + offset]
14 m = new_m
15online = numerator / denominator
16weights = [exp(s - max(scores)) for s in scores]
17reference = sum(w*v for w,v in zip(weights, values)) / sum(weights)
18assert abs(online - reference) < 1e-12
19print(online)Questions and trade-offs
Why use it? Memory traffic and temporary storage can limit practical attention performance. Tiling, fusion, and recomputation target those implementation costs. Actual speed depends on the GPU, shapes, dtype, masking, and kernel version.
Is it sparse attention? Standard FlashAttention computes dense attention. Sparsity specifies which interactions are retained; it is a separate design dimension.
Is DeepSeek V4.1-Flash a FlashAttention paper? No. The model name is not an attention taxonomy. Its documented attention system includes compression and sparse indexing; read the model breakdown separately.
Check your understanding
A new tile raises the running maximum from 5 to 10. What must happen to the previous numerator and denominator? Both are multiplied by before adding the new tile's contributions. Rescaling only one changes the result.
Sources: FlashAttention, Dao et al., official implementation and supported kernels. Compare the architectural alternative in linear attention.
Oliver Perrin
Machine Learning Engineer · Founder, LiminalML
LiminalML brings together concept explanations, mathematical examples, and working visuals. Find more writing by Oliver on the LiminalML Substack.
Keep exploring the idea.
Ask a follow-up in a guided session, work through the self-check, or open the visual in Studio to build on it.
Related concepts
Deep Learning
Linear Attention
Understand recurrent memory, gated delta updates, and why modern hybrids retain some full-attention layers.
Deep Learning
Attention Mechanisms
Turn compatibility scores into attention weights, then combine values. Explore the geometry, normalization, and limits of the mechanism.
Model breakdowns
DeepSeek V4.1-Flash
Separate the model's causal encoder–decoder memory sharing from FlashAttention and linear attention.