Skip to content
Inference Academy

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.

4 questions·Model answers and follow-upsFree
Share

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 S=QK⊤S = Q K^{\top}, writes it to HBM, reads it back to apply the softmax, writes the probabilities, then reads them to multiply by VV. For a sequence of length nn, that is memory traffic and storage proportional to n2n^2 per head, and the element-wise steps are memory-bound.

FlashAttention is IO-aware:

  • It splits QQ, KK and VV 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 n×nn \times n 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 ww 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 nn tokens with model dimension dd:

  • Prefill attention computes scores for every pair of tokens, about 2n2d2 n^2 d 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 24nd224 n d^2 FLOPs, so the ratio of attention to weight work is roughly n/6dn / 6d: for d=4096d = 4096 they break even around 25,000 tokens, which is why attention matters mostly at long context.
  • Decode at position tt does about 4td4 t d attention FLOPs per layer and reads tt cached keys and values per KV head. Summed over nn 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 →

Articles on this topic

Related interview questions

All interview topics →