Bring the mental model from Scaled Dot-Product Attention & Transformer Layers; this page will reuse it instead of restarting from zero.
Attention & Transformers
Efficient Attention at Scale: KV Cache, GQA & FlashAttention
How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.

Concept Structure
Efficient Attention at Scale: KV Cache, GQA & FlashAttention
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learning map
Efficient Attention at Scale: KV Cache, GQA & FlashAttentionConceptual Bridge
What should feel connected as you move through this page.
How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.
The next edge should feel earned: use the demo prediction here before following LLM Serving at Scale: Prefill, Decode & Continuous Batching.
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
"Attention is " is true on paper, but in production the pain often feels different:
- During decoding, you only compute attention for the new token, but you must attend over all past tokens.
- You therefore store past keys/values in a KV cache. At long context, the KV cache can dominate memory.
Modern serving is mostly a game of managing memory bandwidth and cache size:
- KV cache avoids recomputing keys/values for all past tokens.
- Grouped-query attention (GQA) reduces KV cache size by sharing keys/values across multiple query heads.
- FlashAttention keeps the exact attention math but changes how it is computed so the attention matrix is never materialized in slow memory.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
With a KV cache, the attention output for the new token at time is:
where are cached.
KV cache memory scaling
Across a batch of sequences and layers, the KV cache stores keys and values for all positions:
The factor of 2 is for storing both and .
Grouped-query attention (GQA)
Let there be query heads but only key/value heads, with a mapping from query head to a KV head:
This reduces the KV cache to roughly of full multi-head attention. Equivalently: the KV cache becomes about of full multi-head attention (so it’s reduced by roughly when ).
For the dedicated head-sharing invariant, selected Q-to-KV mapping, and cache-ratio prediction exercise, see Grouped-Query Attention.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
def kv_cache_gb(T, layers, h_kv, d_head, batch=1, bytes_per_elem=2):
elems = batch * layers * T * h_kv * d_head * 2 # keys + values
return elems * bytes_per_elem / 1e9
T, layers, d_head, batch = 128_000, 80, 128, 1
full = kv_cache_gb(T, layers, h_kv=64, d_head=d_head, batch=batch)
for h_q, h_kv in [(64, 64), (64, 8), (64, 1)]:
gb = kv_cache_gb(T, layers, h_kv=h_kv, d_head=d_head, batch=batch) # fp16
print(f"Hq={h_q:>2} Hkv={h_kv:>2} KV ~ {gb:6.1f} GB ({gb/full:.1%} of full)")
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Use the demo to explore how KV cache size grows with context length, and how GQA changes the memory/latency tradeoff without changing the basic attention mechanism.
Live Concept Demo
Explore Efficient Attention at Scale: KV Cache, GQA & FlashAttention
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what Efficient Attention at Scale: KV Cache, GQA & FlashAttention should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.

Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Efficient Attention at Scale: KV Cache, GQA & FlashAttention should make visible.
Visual Inquiry
Make the image answer a mathematical question
How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.
Which visible object should carry the first intuition?
Pick the cue that should make Efficient Attention at Scale: KV Cache, GQA & FlashAttention easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Grounds the KV-cache bandwidth motivation for sharing keys and values across query heads during decoding.
Open sourceIntroduces grouped-query attention as the practical middle point between multi-head quality and MQA memory savings.
Open sourceGrounds the IO-aware attention kernel: tiling plus online softmax avoids materializing the full attention matrix.
Open sourceClaim Review
How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.
Claims without a substantive review badge still need exact source-support review.
shazeer-2019-mqa, ainslie-2023-gqa, dao-2022-flashattention
Use equation, code, and demo objects to check whether the source support is operational.
Shazeer identifies repeated loading of large key/value tensors as an incremental-decoding bottleneck and proposes sharing keys and values across heads; Ainslie et al. present GQA as an intermediate between MHA quality and MQA speed/memory savings.
Sources: Fast Transformer Decoding: One Write-Head is All You Need, GQA: Training Generalized Multi-Query Transformer Models from Multi-Head CheckpointsThis checks KV-sharing memory/bandwidth and cache-size scaling for autoregressive decoding only; it is not a universal latency, quality, hardware, checkpoint-conversion, or model-adoption guarantee.A bounded review summary is present; still check caveats and exact source scope.Shazeer supports the bottleneck/mechanism: incremental decoding reloads large K/V tensors, so memory bandwidth can dominate, and MQA removes the K/V heads dimension while keeping query heads. Ainslie et al. support GQA/MQA: MQA uses one KV head; GQA shares KV heads per query-head group, reducing KV-cache size and loads roughly in proportion to Hkv/Hq.
Reviewer: codex+oracle; reviewed 2026-05-07Source support candidates
paper 2019Fast Transformer Decoding: One Write-Head is All You NeedGrounds the KV-cache bandwidth motivation for sharing keys and values across query heads during decoding.
paper 2023GQA: Training Generalized Multi-Query Transformer Models from Multi-Head CheckpointsIntroduces grouped-query attention as the practical middle point between multi-head quality and MQA memory savings.
paper 2022FlashAttention: Fast and Memory-Efficient Exact Attention with IO-AwarenessGrounds the IO-aware attention kernel: tiling plus online softmax avoids materializing the full attention matrix.
Practice Loop
Try the idea before it explains itself
How attention becomes practical at long context: KV caching for decoding, grouped-query attention, and IO-aware kernels like FlashAttention.
Before touching the demo, predict one visible change that should happen in Efficient Attention at Scale: KV Cache, GQA & FlashAttention.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to an exact object
Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.Open the draft below to save one note and next action in this browser.
Efficient Attention at Scale: KV Cache, GQA & FlashAttention
What is the smallest example that makes Efficient Attention at Scale: KV Cache, GQA & FlashAttention click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays locally in this browser for concept:attention-transformers/efficient-attention.
- Source ids to inspect: shazeer-2019-mqa, ainslie-2023-gqa, dao-2022-flashattention
- Definition, prerequisite, and contrast concept links
- The equation or code witness that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Efficient Attention at Scale: KV Cache, GQA & FlashAttention Object key: concept:attention-transformers/efficient-attention Context: Attention & Transformers Anchor id: concept/concept-notebook/attention-transformers/efficient-attention Open question: What is the smallest example that makes Efficient Attention at Scale: KV Cache, GQA & FlashAttention click without losing the math? Evidence to inspect: - Source ids to inspect: shazeer-2019-mqa, ainslie-2023-gqa, dao-2022-flashattention - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/attention-transformers/efficient-attention
concept:attention-transformers/efficient-attention