TECHNOLOGY

Cross-attention connects a decoder to an encoder representation

In this article
THE QUICK TAKE

Cross-attention connects two sequence representations: the decoder asks questions of encoder states while causal self-attention organizes the target prefix.

An encoder-decoder Transformer has separate representations for source text and the target being produced. Cross-attention is the bridge between them. Its query, key and value origins explain why source length and target length are different axes, and why its masking rules differ from the decoder’s causal self-attention.

The encoder represents the source sequence

The encoder processes source tokens into contextual states according to its architecture and attention mask. In a common text-to-text setup, source positions can use surrounding source context rather than only a generated prefix. These states provide the information that the decoder can consult. They are not a prewritten target answer and do not need to have the same number of positions as the output. A translation task, for example, can map a short source phrase to a target phrase with a different tokenization and length.

Queries come from the target side

Cross-attention projects queries from decoder representations and keys and values from encoder states. Each target query compares with eligible source keys, and the normalized weights combine source values. The score matrix therefore has target positions along one sequence axis and source positions along the other. This distinguishes it from decoder self-attention, where queries, keys and values originate on the target side. Naming these origins avoids a common misconception that cross-attention merely means another head within the same target-token mixture.

A source-target example clarifies the masks

Imagine an encoder representing a weather report while a decoder writes its summary. The current target state can consult eligible source positions, including source text appearing late in the report. That is not a violation of target causality: the entire report is already available as input. The decoder’s self-attention must still avoid future target tokens during causal training. Source padding needs its own exclusion rule. A single triangular mask copied everywhere would confuse these different dependencies and can incorrectly hide available source information.

Trace both sequence axes when validating

For a small fixture, label source tokens, target-prefix tokens and the origins of every projected tensor. Check source padding and target causal access independently, then compare cross-attention outputs under controlled changes to the source. A model may use different architectural variants, so the checkpoint contract remains authoritative. The mechanism explains information flow, not a guarantee that the model attends to the most relevant source or produces a faithful summary. Faithfulness requires evaluation of generated claims in addition to correct source-target tensor alignment.

Primary research and implementation references

Reproducible inference stacks: preserving a GPU pilot’s result; A GPU inference benchmark you can trust.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles