An encoder-decoder translation service receives N requests that all translate the same English source sentence into different target languages (French, Spanish, German, Japanese). Identify the attention side optimization the runtime can apply, and explain why it is structurally available in encoder-decoder but only partially in decoder-only.
Run the encoder once and reuse the cached cross-attention K, V across all N decoder generations; decoder-only can do this partially via prefix caching.
Imagine a museum tour guide who has memorized a long script about one painting. If ten visitors all want to hear about that painting in ten different languages, the guide does not need to look at the painting ten times. They look once, form one mental description, and then translate that description into each language. The painting is the source sentence; the mental description is the encoder output; the translations are the decoder runs. Encoder-decoder transformers do exactly this. Decoder-only models work more like a guide who has to re-look at the painting every time they start a new translation, unless they have a clever notebook that remembers what they saw last time when the visitor description matches exactly.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Encoder K, V caching is the cleanest example of architectural amortization in transformer serving. It only works in encoder-decoder models, it only matters when one source feeds multiple downstream generations, and when those conditions hit it can cut serving cost by 30-50%. Decoder-only architectures dominate the LLM market because their workloads are usually one-shot per source, but the moment you have heavy source reuse, translation, ASR beam search, multi-modal cross-attention into a fixed image, the encoder-decoder design's caching story becomes structurally superior.
This deep dive walks the mechanics of cross-attention K, V caching, quantifies the savings, contrasts with prefix caching in modern decoder-only serving stacks, and closes with a guide for when the encoder-decoder design is structurally the right choice.
What is actually cached
When people say 'cache the encoder output' for translation, they are being imprecise. Let's get specific.
The encoder produces hidden states
Given a source sentence of L tokens, the encoder produces a (L, d_model) tensor at each of its layers. The final-layer output is the one the decoder consumes.
The decoder's cross-attention layers project K and V
In each decoder block, the cross-attention sub-layer has its own W_K and W_V matrices. These project the encoder's final-layer hidden states to produce (L, d_model) K and V tensors per decoder layer.
Those K and V are what gets cached
For a decoder with D layers, the cache is D pairs of (L, num_heads, d_head) tensors, one (K, V) per cross-attention sub-layer per layer.
Why this granularity matters
The Q in cross-attention is projected from the decoder hidden state, which changes per generated token. So Q must be re-computed every step. But K and V depend only on the encoder output, which is fixed once the source is set. So they can be projected once and reused for every Q across every decoder step across every target language.
The cache is per-layer per-(K or V). Not per-head, not per-token, the granularity is layer-shaped tensors.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
| Aspect | Encoder-decoder K, V reuse | Decoder-only prefix caching |
|---|---|---|
| Granularity of sharing | Whole encoder output | Exact token-level prefix match |
| Robustness to per-request diff | High, decoder state is independent | Low, any divergence ends sharing |
| Cache structure | Per-layer cross-attn K, V | Per-layer self-attn K, V blocks |
| Typical use case | Translation, ASR beam search | Chat with shared system prompt |
| Saving for N parallel runs | Encoder cost / N (large) | Depends on prefix overlap |
Real products, models, and research that use this idea.
- Google's production NMT service for translation: encoder K, V cache shared across multi-target translation requests.
- Whisper (encoder-decoder ASR): the audio encoder runs once per utterance and the decoder beam search reads the same cached cross-attention K, V for every hypothesis.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is cross-attention K, V cacheable but cross-attention Q is not?
K and V depend only on the encoder output, which depends only on the source. Q is projected from the decoder hidden state, which changes for every generated token in every target language. So Q must be re-computed per request and per token; K and V are fixed once the source is set.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Claiming decoder-only can do exact encoder-output reuse via prefix caching. Prefix caching only works for exact token-level prefix matches; cross-attention reuse works regardless of how decoder runs diverge.
60 second bullets to scan on the way to the call.
What gets cached in encoder-decoder reuse (per-layer cross-attention K, V)
Why the encoder pass is reusable (output depends only on source)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.