﻿# Cross-attention connects a decoder to an encoder representation

Encoder-decoder cross-attention uses decoder queries against encoder keys and values. Follow source and target axes to distinguish it from decoder self-attention.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/encoder-decoder-cross-attention/
Language: en

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

- [BERT: Pre-training of Deep Bidirectional Transformers](https://arxiv.org/abs/1810.04805).

- [Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer](https://arxiv.org/abs/1910.10683).

- [Attention Is All You Need](https://arxiv.org/abs/1706.03762).

## Related engineering context

[Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
