FlashAttention interview questions
Interview questions on attention optimisation for LLM inference: how FlashAttention works, online softmax, decode attention kernels, sliding-window and long-context attention, with model answers.
Attention is the part of the transformer whose cost grows with context length, so long-context serving is mostly an attention problem. These questions check that you understand why FlashAttention is fast without being an approximation, and how decode attention differs from prefill attention.
Question 1
How does FlashAttention work, and why is it faster?
Model answer
Standard attention computes the score matrix , writes it to HBM, reads it back to apply the softmax, writes the probabilities, then reads them to multiply by . For a sequence of length , that is memory traffic and storage proportional to per head, and the element-wise steps are memory-bound.
FlashAttention is IO-aware:
- It splits , and into tiles that fit in on-chip SRAM (shared memory).
- For each block of queries it loops over blocks of keys and values, computing partial scores and accumulating the output in registers.
- It uses an online softmax: it keeps a running maximum and running sum for each row, and rescales the accumulated output whenever the maximum changes, so it never needs the full row of scores at once.
- The matrix is never written to HBM. For training, the backward pass recomputes it from saved statistics instead of storing it.
The result is exact attention with memory linear in sequence length and much less HBM traffic, typically several times faster than unfused attention. Later versions (FlashAttention-2 and 3) improved parallelism across the sequence, work partitioning between warps, and on Hopper use asynchronous copies and FP8.
Likely follow-up questions
- Derive how the running output is rescaled when a new block raises the row maximum.
- Why is FlashAttention not an approximation, unlike sparse or linear attention?
- What limits the tile size?
Question 2
Why do decode steps need a different attention kernel from prefill?
Model answer
In decode each sequence has a single query token attending to a long KV cache. A prefill-style kernel parallelises over query blocks, but with one query there is only one block per head, so for small batches most of the GPU's streaming multiprocessors sit idle while one of them reads a long cache.
Split-KV kernels such as FlashDecoding split the KV cache along the sequence into chunks, compute partial attention for each chunk in parallel on different SMs, and combine the results with a small reduction that uses the same max-and-sum rescaling as online softmax. That spreads the cache read across the whole GPU.
Decode attention is memory-bound (each cached key and value is read once per step and used for very few queries), so the goal is to reach full HBM bandwidth. GQA helps because several query heads share each loaded KV head, raising arithmetic intensity, and kernels are written to process a whole query group per load.
Likely follow-up questions
- How would you choose the number of KV splits?
- Why does MLA make decode attention more compute-heavy than GQA?
- How does speculative decoding change the shape of decode attention?
Question 3
How do long-context models keep attention affordable?
Model answer
Several techniques, often combined:
- Sliding-window attention: some or all layers attend only to the last tokens, so their KV cache is bounded. Models interleave local and global layers so information still flows across the whole context.
- Fewer or smaller KV entries: GQA, MLA and KV quantisation cut bytes per token.
- Hybrid architectures: replace most attention layers with linear-attention or state-space layers that keep a fixed-size state, and keep a few full attention layers for precise recall.
- Sparse or selective attention: attend to a selected subset of past tokens chosen by a cheap scoring step.
- Context parallelism: for very long prompts, split the sequence across GPUs and pass KV blocks around in a ring during prefill.
- Serving-side: prefix caching for repeated documents, chunked prefill to protect other users, and offloading old KV blocks to CPU memory.
Each trades something (recall at distance, quality, complexity) for memory and speed, so evaluate on long-context retrieval tasks, not only short benchmarks.
Likely follow-up questions
- Why do pure linear-attention models struggle with exact recall tasks?
- What does an engine need to change to support a model with mixed sliding-window and global layers?
- How does rotary position embedding scaling relate to serving longer contexts than the model was trained on?
Question 4
What is the computational complexity of attention in prefill and decode?
Model answer
For a sequence of tokens with model dimension :
- Prefill attention computes scores for every pair of tokens, about FLOPs per layer for the scores and the same again for multiplying by values (halved with causal masking in efficient kernels). A standard layer's weight matmuls cost about FLOPs, so the ratio of attention to weight work is roughly : for they break even around 25,000 tokens, which is why attention matters mostly at long context.
- Decode at position does about attention FLOPs per layer and reads cached keys and values per KV head. Summed over output tokens that is quadratic in total, but per step it is linear in context length, and memory-bound.
The practical consequence: at short context, decode cost is dominated by reading weights; at long context, by reading the KV cache. Prefill at long context is dominated by attention FLOPs.
Likely follow-up questions
- How does that break-even point move for a model with a wider MLP or with GQA?
- How does GQA change the decode memory cost but not the FLOPs?
- Why does causal masking let kernels skip about half the work?
The lesson behind these answers · 16 min · Pro, first part free
Attention optimisations: FlashAttention, GQA, paged KV and sliding windows
Worked numbers, step-through diagrams and a quiz, so you can derive the answer in the interview instead of reciting it.
Read the lesson →- Attention kernels for serving: MLA, split-KV decoding and FlashAttention 3 and 4 · Bottlenecks and Hardware18 min · Pro
- Flash attention and long-context performance · Performance Tuning20 min · Pro
- Long-context serving: KV memory, prefill cost, context parallelism and cheaper attention · Performance Techniques17 min · Pro
- Linear attention, DeltaNet and KDA: how hybrid models keep the cache small · Bottlenecks and Hardware18 min · Pro
Articles on this topic
- FlashAttention explained simply: same maths, far less memory traffic (8 min read)
- MHA vs GQA vs MQA vs MLA: how attention design decides your KV cache (8 min read)
- PagedAttention explained: virtual memory for the KV cache (7 min read)
- Tensor vs pipeline vs data vs expert parallelism for LLM inference (7 min read)
Related interview questions
- KV cache interview questions (5 questions)
- Roofline model interview questions for LLM inference (5 questions)
- PagedAttention interview questions (5 questions)