Cross-Attention Between Encoder and Decoder
Self-attention lets a sequence look at itself; cross-attention is the transformer mechanism that lets a decoder look at an entirely different sequence — the encoder's output — while generating its own.
Prerequisites: Queries, Keys and Values, Bahdanau and Luong Attention
Self-attention, the mechanism inside most transformer blocks, lets every position in a sequence attend to every other position within the same sequence. That's exactly right for understanding one passage of text on its own. But some tasks fundamentally involve two sequences — translating a sentence from one language to another, summarizing a document into a shorter passage, transcribing audio into text — where the output being generated needs to repeatedly refer back to a different sequence, the input, while it's being produced. Self-attention alone has no mechanism for that; you need attention between two distinct sequences, not within one.
The analogy: writing a summary with the source document open beside you
Self-attention is like proofreading a paragraph by re-reading itself, checking how its own sentences relate to each other. Cross-attention is like writing a summary of a completely different, longer document: as you write each sentence, you keep glancing back at the source, checking which part is relevant to the sentence you're currently drafting. The summary in progress is one sequence; the source document is a separate, fixed sequence you keep referring back to — never confusing "what I've written" with "what the source says," but constantly relating the two.
The mechanics: queries from one sequence, keys and values from another
Cross-attention uses the same score-softmax-blend attention formula as self-attention, but with a crucial difference in where each of the three inputs — queries, keys, and values — comes from:
In words: the queries come from the decoder's current hidden states — "what am I trying to produce right now" — while the keys and values come from the encoder's finished output — "what does the source contain, and what should be retrieved." Self-attention, by contrast, draws , , all from the same sequence. That's the single structural difference — the direct architectural descendant of Bahdanau and Luong Attention, generalized into the query/key/value framework.
A decoder block therefore contains three sub-layers: masked self-attention over its own partial output (so it doesn't peek ahead), cross-attention over the encoder's output (so it can consult the source), and a feedforward layer — versus an encoder block, which only needs self-attention.
Worked example 1: translation, step by step
Translating "the cat sat," three words in, the decoder is producing its third output word, built from what it has generated so far ("le chat…", say). Cross-attention computes this query against keys built from all three encoder positions ("the," "cat," "sat"), suppose yielding scores after scaling. Softmax gives weights roughly — the decoder correctly looks almost entirely at "sat" while producing the verb, even though "sat" is the last encoder position. Cross-attention has no notion of matching sequence positions; it retrieves whichever encoder position is relevant, regardless of where it sits in the source order.
Worked example 2: multiple cross-attention layers refining the same query
A 6-layer decoder performs cross-attention at every layer, not just once. Suppose at layer 1 the weights for a given decoder position are diffuse, — the model hasn't yet resolved what's relevant. By layer 6, after the decoder's representation is refined through several self-attention and feedforward steps, the same computation might sharpen to . The decoder query gets progressively more informative through layers, so later cross-attention calls retrieve more precisely — the encoder's keys and values stay fixed throughout, but what the decoder asks for gets sharper.
Think of the encoder's positions as fixed points and the decoder's evolving queries as sweeping across them: cross-attention measures, at every decoder step, which encoder points the query currently lines up with most strongly — a relationship that sharpens layer by layer, much like a fit line summarizing a scatter better as information accumulates.
What this means in practice
Cross-attention is the architectural feature that defines an encoder-decoder transformer as distinct from an encoder-only (BERT-style) or decoder-only (GPT-style) model, discussed further in Encoder-Only, Decoder-Only and Encoder-Decoder Designs — a decoder-only model has no separate source to cross-attend to, so it uses only masked self-attention. It's essential wherever generation is conditioned on a distinct input: translation, summarization, image captioning, speech-to-text.
Cross-attention reuses the exact score-softmax-blend attention mechanism, but draws its queries from the decoder (what's currently being generated) and its keys and values from the encoder (a separate, already-processed source sequence) — letting a decoder repeatedly and selectively refer back to a fixed input while producing its own, differently-ordered output.
Practice
- State which of Q, K, V come from the decoder and which from the encoder in cross-attention.
- Explain why a decoder-only (GPT-style) model has no need for cross-attention.
- In the translation example, why can the decoder attend most strongly to the last encoder position while only on its third output word?
The common confusion is thinking cross-attention requires the decoder and encoder sequences to be the same length, or that positions must roughly correspond (position 3 in the output attending mostly to position 3 in the input). Neither is true — cross-attention has no built-in notion of matching positions across the two sequences at all; it is a fully learned, content-based retrieval over the entire encoder output, computed fresh at every decoder step, regardless of how the two sequences' lengths or word orders relate. Assuming positional correspondence leads people to misread attention-weight visualizations as broken when the model is, in fact, correctly attending to a reordered or shorter/longer relevant span.
Related concepts
Practice in interviews
Further reading
- Vaswani et al., Attention Is All You Need (2017)