In this article
Before you start
Why interviewers ask this
FlashAttention is the cleanest example of an optimisation that changes speed without changing the answer, which is why it suits large language model (LLM) depth rounds. The question takes two forms: a conceptual one ("why is standard attention memory-bound and how does FlashAttention fix it") and an implementation one ("write single-head flash attention in NumPy").
A weak answer says "it is a faster attention" or "it approximates attention". A strong answer says the maths is unchanged, names the real bottleneck as memory traffic between HBM and SRAM, and explains the running max and sum that make a block-by-block softmax exact.
FlashAttention from first principles
In plain words
- Tiling
- Tiling is the technique of cutting a large matrix computation into small blocks that fit in fast memory, finishing each block's work before loading the next.
- Memory-bound
- Memory-bound is a property of a computation whose speed is limited by how fast data moves to and from memory, not by how fast the chip can do arithmetic.
Think of marking exams with a small desk and a large warehouse of papers. Carry each paper from the warehouse to the desk, mark it and carry it back, and you spend the day walking. Take one stack, finish all you can with it, then fetch the next, and you spend the day marking.
The analogy breaks here. Marking each paper is independent, but a softmax needs a total over a whole row, so a stack-by-stack method needs extra bookkeeping to get that total right.
In one precise sentence, FlashAttention reorders the same arithmetic so that the large intermediate matrix stays in on-chip memory.
Step 1: Why standard attention is memory-bound
Standard attention is memory-bound because it writes the score matrix to HBM, reads it back for the softmax, writes the weights, and reads them again for the product with the values. The FlashAttention paper describes an A100 with 40 to 80 gigabytes (GB) of HBM at roughly 1.5 to 2.0 terabytes (TB) per second and 192 kilobytes (KB) of SRAM per streaming multiprocessor across 108 of them at roughly 19 TB per second. The SRAM is about ten times faster and many orders of magnitude smaller.
The input is the matrices of queries, keys and values for tokens. The matrix of scores has entries per head. At 8,192 tokens and 2 bytes per entry (fp16) that is 128 MiB for one head, and 4 GiB for one layer of 32 heads1. Moving this to and from HBM costs more time than the multiplications that produced it. The paper's Theorem 2 puts standard attention at HBM accesses, with the head dimension.
Step 2: How online softmax keeps tiled attention exact
Online softmax keeps a running maximum and a running sum for each row; when a new block raises the maximum from to , it multiplies the old sum by . Milakov and Gimelshein introduced this online softmax in 2018 to cut memory accesses, and FlashAttention builds on it.
Take six illustrative scores, 1.0, 3.0, −2.0, 0.5, 2.5 and 4.0, as two blocks of three. After block one the running maximum is 3.0 and the running sum is 1.1421. Block two has a maximum of 4.0, so the maximum rises to 4.0, the old sum is multiplied by , and the new block's sum is added. The final sum is 1.6735, which equals the sum computed on the whole row at once1.
The same rescaling is applied to the partly built output. The input is a block of scores and the previous , and output; the output is updated values that equal the exact softmax result after the last block. In the six-score example with value vectors, the largest difference from the full computation was , which is floating-point rounding1.
Step 3: Tiling keeps every block in SRAM
FlashAttention loops over blocks of keys and values in the outer loop and blocks of queries in the inner loop, loading each block into SRAM and computing its scores there. With keys and values in the outer loop, each query block's partial output and its running max and sum are read from HBM and written back after every key and value block.
That is traffic per pass and no score matrix. FlashAttention-2 swaps the loops, so the output and statistics stay on chip until one final write. Algorithm 1 in the paper sets the key block size to and the query block size to the smaller of that and , where is the SRAM size in elements.
With an illustrative fp16 elements (192 KB) and , the key block holds 192 rows and the query block 128 rows1. Theorem 2 gives HBM accesses. At 8,192 tokens the ratio of the two leading terms is about 6.1, so on that rough count, with constants ignored, the tiled version moves about a sixth of the data1.
Step 4: The backward pass recomputes instead of storing
For training, FlashAttention stores the output and the statistics and , then recomputes the score and weight tiles in on-chip memory during the backward pass. The paper says that even with more arithmetic, recomputation speeds up the backward pass because it reduces HBM accesses.
The trade is more floating-point work for less memory traffic. Because the chip has arithmetic to spare and little bandwidth, the trade wins.
The extra memory is the pair and per row, which is linear in . At 8,192 tokens that is 65,536 bytes per head in fp321. The paper states the algorithm needs only additional memory beyond inputs and output while still doing arithmetic.
Step-by-step description
- Standard attention moves the N by N scores to HBM and back.
- A running max and sum make block-wise softmax exact.
- Tiles stay in SRAM; O, m and l are written back, never the scores.
- The backward pass recomputes tiles instead of storing scores.
- Standard attention writes the N by N score matrix to HBM and reads it back.
- Online softmax keeps a running maximum and a running sum for each row.
- FlashAttention loads tiles of queries, keys and values into SRAM and writes the output and statistics back, never the score matrix.
- The backward pass recomputes the score and weight tiles from the stored output and statistics.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →Worked example
| Tokens (N) | Scores per head | fp16 size per head | 32 heads, one layer |
|---|---|---|---|
| 2,048 | 4,194,304 | 8 MiB | 0.25 GiB |
| 8,192 | 67,108,864 | 128 MiB | 4 GiB |
| 32,768 | 1,073,741,824 | 2 GiB | 64 GiB |
| 131,072 | 17,179,869,184 | 32 GiB | 1,024 GiB |
Every row is entries at 2 bytes each1. Quadrupling the tokens multiplies the size by 16, so at long contexts the matrix soon stops fitting in GPU memory. FlashAttention never builds it.
Text description of the diagram
- At 8,192 tokens one head's fp16 score matrix is 128 MiB, which is 0.125 GiB.
- At 131,072 tokens one head's fp16 score matrix is 32 GiB.
- Quadrupling the tokens multiplies the size by 16.
The exactness check is a full tiled run. With 256 tokens, head dimension 16 and 64-row blocks on random data, the tiled output differed from standard attention by at most . Standard attention holds 65,536 scores at once; the tiled version holds at most 4,096 in a tile1.
Text description of the diagram
- Standard attention holds 65,536 scores at once for 256 tokens.
- The tiled version holds at most 4,096 scores in a tile.
- The tiled output differed from standard attention by at most rounding error.
| Version | Paper | Headline result as reported |
|---|---|---|
| FlashAttention | May 2022 | 15% faster end to end on Bidirectional Encoder Representations from Transformers (BERT) large (512 tokens) than the MLPerf 1.1 record; about 3 times on Generative Pre-trained Transformer (GPT) 2 (1K tokens) |
| FlashAttention-2 | July 2023 | About 2 times faster than version 1; 50 to 73% of theoretical peak on A100 |
| FlashAttention-3 | July 2024 | 1.5 to 2.0 times speedup on H100 GPUs; up to 740 trillion floating-point operations (TFLOPs) per second in 16-bit floating point (FP16) |
The sources are the FlashAttention, FlashAttention-2 and FlashAttention-3 abstracts. These are the authors' own benchmark claims on their hardware, not our measurements.
Trade-offs and edge cases
FlashAttention does not make attention cheaper in arithmetic. It does the same work, so compute still grows with the square of the sequence length; the saving is memory size and memory traffic. It also does not shrink the KV cache that inference stores for every past token, which is a separate memory cost covered in LLM inference.
The gains depend on hardware and data type. The FlashAttention-2 paper reports 25 to 40% of peak arithmetic throughput for version 1, and the FlashAttention-3 paper reports 35% utilisation of an H100 for version 2. The repository states version 2 supports fp16 and bf16, which is why the choice between them in fp16 versus bf16 matters here, and head dimensions up to 256.
Text description of the diagram
- Version 2 supports Ampere, Ada and Hopper GPUs with fp16 and bf16.
- Version 3 is in beta for Hopper GPUs with CUDA 12.3 or higher.
- Version 3 supports FP16 and BF16 forward and backward and an FP8 forward pass.
- ROCm builds cover AMD Instinct MI200x, MI250x, MI300x and MI355x accelerators and RDNA 3 and 4 GPUs.
Fused kernels have input limits. PyTorch's scaled_dot_product_attention selects among FlashAttention-2, a memory-efficient kernel and a plain C++ implementation, and when a fused kernel cannot run it emits a warning listing the reasons. Unusual masks or biases can force the slower path. The PyTorch default is enough unless you need a feature it lacks.
How to answer it in an interview
Why is standard attention memory-bound, and how does FlashAttention fix it without changing the maths?
I would say standard attention writes the full N by N score matrix to GPU main memory and reads it back for the softmax and the value product, so memory traffic, not arithmetic, sets the speed. FlashAttention tiles the computation so each block stays in on-chip SRAM, and it uses an online softmax with a running max and sum so the result is exact. The score matrix is never stored, extra memory is linear in length, and the backward pass recomputes tiles. Compute is still quadratic.
The spoken structure is: name the bottleneck as memory traffic, say tiling plus online softmax, then state that it is exact and that compute stays quadratic.
Common mistakes
- "FlashAttention approximates attention to go faster" (wrong). The output is the same up to floating-point rounding (right).
- "It reduces the number of floating-point operations" (wrong). Arithmetic stays and the backward pass adds recomputation (right: it reduces memory traffic).
- "It removes the KV cache" (wrong). The cache stores past keys and values for generation and is a separate cost (right).
- "Tiling alone is enough" (wrong). Without the running max and sum a block-wise softmax would normalise each block separately and give the wrong weights (right).
- "It works on any GPU with any data type" (wrong). Support depends on the GPU family, the vendor backend and the version (right).
Follow-up questions
What is FlashAttention in an LLM?
It is an exact attention kernel that cuts memory traffic. PyTorch's built-in scaled_dot_product_attention ships a FlashAttention-2 kernel (PyTorch 2.14 documentation, read October 2026), so models that call it can use FlashAttention with no separate install. It makes long contexts practical because it avoids storing the quadratic score matrix. See the transformers hub for where attention sits in the model.
How do I install FlashAttention?
For version 2 the repository gives pip install flash-attn --no-build-isolation, and it lists Compute Unified Device Architecture (CUDA) 12.0 or above, PyTorch 2.2 or later, and the packaging, psutil and ninja packages, with Linux as the supported platform as of October 2026. Often no install is needed, because PyTorch's scaled_dot_product_attention already includes a FlashAttention-2 kernel on CUDA.
Which GPUs support FlashAttention?
It depends on vendor and version. As of October 2026 the repository lists, for NVIDIA CUDA, version 2 on Ampere, Ada and Hopper GPUs such as the A100, GeForce 3090 and 4090 cards and the H100 (for Turing GPUs such as the T4 and 2080 it points to a separate flash-attention-turing project that supports a core subset of features), version 3 in beta on Hopper (H100 and H800) with CUDA 12.3 or higher, and version 4 for Hopper and Blackwell (B200); FlashAttention-3 supports FP16 and brain floating point 16 (BF16) forward and backward and an 8-bit floating point (FP8) forward pass.
For Advanced Micro Devices (AMD) ROCm, it lists AMD Instinct MI200x, MI250x, MI300x and MI355x accelerators and RDNA (AMD's Radeon graphics architecture) 3 and 4 GPUs on a Composable Kernel backend, and a Triton backend for CDNA (AMD's Instinct compute architecture) and RDNA with fp16, bf16 and fp32.
What is FlashAttention 2?
FlashAttention-2 keeps the exact tiled method but cuts non-matmul arithmetic, parallelises over sequence length as well as batch and heads, and splits work between warps to cut shared-memory traffic (Dao, July 2023). The author reports about twice the speed of version 1, reaching 50 to 73% of the theoretical maximum arithmetic throughput on A100 and up to 225 TFLOPs per second per GPU in GPT-style training.
What is FlashAttention 3?
FlashAttention-3 targets Hopper GPUs by overlapping data movement with computation through asynchrony and by using FP8 low-precision arithmetic. The authors report a 1.5 to 2.0 times speedup on H100 GPUs and about 2.6 times lower numerical error than a baseline FP8 attention.
Does FlashAttention make attention linear in compute?
No. The arithmetic is still quadratic in sequence length; only the extra memory becomes linear. Methods that cut compute, such as sparse or windowed attention, change the maths, which FlashAttention does not.
- FlashAttention is exact attention reordered into SRAM-sized tiles; the score matrix is never stored.
- The running max and sum of an online softmax are what make block-by-block softmax exact.
- At 8,192 tokens one head's fp16 score matrix is 128 MiB; the tiled version holds at most one tile.
- Extra memory is linear in length, compute is still quadratic, and the backward pass recomputes.
- Support depends on GPU family, vendor backend and version; on NVIDIA, version 2 covers Ampere, Ada and Hopper, and version 3 is built for Hopper and adds FP8 forward.
Sources
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (arXiv 2205.14135) — arXiv (Dao, Fu, Ermon, Rudra, Re), 27 May 2022
- FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning (arXiv 2307.08691) — arXiv (Dao), 17 Jul 2023
- FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision (arXiv 2407.08608) — arXiv (Shah, Bikshandi, Zhang, Thakkar, Ramani, Dao), 11 Jul 2024
- Online normalizer calculation for softmax (arXiv 1805.02867) — arXiv (Milakov and Gimelshein, NVIDIA), 8 May 2018
- Dao-AILab/flash-attention repository README (read 2026-10-24) — GitHub, Dao-AILab, 24 Oct 2026
- torch.nn.functional.scaled_dot_product_attention, PyTorch 2.14 documentation — PyTorch (page shows version only; read 2026-10-24), 24 Oct 2026