Zenaique

Translating one source sentence into many target languages, what attention side trick scales?

Short answer·Medium·4.0 · 0·~3 min·Asked atDatabricksTcs·Relevant atAi4bharatCerebrasMicrosoftReplicate
Attempt it

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.

Free · 2 AI evals / day
TL;DR

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.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

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.

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.

Quantifying the win
Why decoder-only cannot do this exactly
Prefix caching in modern serving stacks
Decision guide: which architecture for which workload
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
AspectEncoder-decoder K, V reuseDecoder-only prefix caching
Granularity of sharingWhole encoder outputExact token-level prefix match
Robustness to per-request diffHigh, decoder state is independentLow, any divergence ends sharing
Cache structurePer-layer cross-attn K, VPer-layer self-attn K, V blocks
Typical use caseTranslation, ASR beam searchChat with shared system prompt
Saving for N parallel runsEncoder 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.
Sign in to see more production examples.

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?
A

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.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

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.

Sign in to see all red flags and common mistakes.

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)

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium