CSCE 471 Natural Language Processing · Spring 2027

Cross-Attention

Kuan-Hao Huang · Course Website

Note: This page was created with the help of AI as a supplementary reading. It may contain errors or simplifications. The lecture slides are the primary reference for this course; please refer to them first.

To translate “How are you doing” into Chinese, a model has to do two things at once: understand the English sentence, and write the Chinese sentence one token at a time. While writing each Chinese token, it must keep looking back at the English to decide what to say next.

A Transformer encoder is good at the first job, and a Transformer decoder is good at the second. Cross-attention is the bridge that connects them: it lets every position of the decoder look at every word of the source sentence. This reading explains how cross-attention works, where it sits in the full Transformer, and why this encoder–decoder design was such a success for machine translation.

After reading this, you should be able to describe the encoder–decoder (sequence-to-sequence) setup · explain where the queries, keys, and values of cross-attention come from · give the shape of the cross-attention matrix · list the three sublayers of a Transformer decoder layer in an encoder–decoder model · compare encoder self-attention, masked decoder self-attention, and cross-attention · compare the cost of self-attention, recurrent, and convolutional layers

1Sequence-to-sequence with Transformers

Many tasks map one sequence to another: translation, summarization, question answering with a written answer. The standard design has two parts:

Something is missing. With only masked self-attention, the decoder sees nothing but the target tokens produced so far; it has no idea what the source sentence says. It needs a way to read the encoder's output. That is exactly what cross-attention provides.

2How cross-attention works

Cross-attention is ordinary attention in which the three roles are split between the two sequences:

Let $H_{\text{dec}}\in\mathbb{R}^{m\times d}$ hold the decoder's vectors for the $m$ target positions (the output of its masked self-attention sublayer), and $H_{\text{enc}}\in\mathbb{R}^{n\times d}$ hold the encoder's final output vectors for the $n$ source words. Then

$$Q = H_{\text{dec}}W^Q,\qquad K = H_{\text{enc}}W^K,\qquad V = H_{\text{enc}}W^V,$$ $$\text{CrossAttention} = \text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V.$$

A few things to notice:

Decoder query:
rows: target positions (queries) · columns: source words (keys)
Figure 1 (interactive). Cross-attention while translating How are you doing into 你 好 嗎. The decoder input is <bos> 你 好 嗎, and each position predicts the next token. Pick a query to see how much it attends to each source word (line thickness and the highlighted row of the $4\times 4$ matrix). The weights are illustrative, chosen to resemble what a trained model might learn.
Same idea as attention in recurrent encoder–decoders. In a recurrent sequence-to-sequence model with attention, the decoder's hidden state $s_t$ is compared with every encoder hidden state $h_i$, the scores are softmaxed, and a weighted sum of the $h_i$ is fed back to the decoder. That is cross-attention: $s_t$ plays the query and the $h_i$ play the keys and values. The Transformer version adds learned projections $W^Q, W^K, W^V$, multiple heads, and computes all target positions at once.

3Three kinds of attention

An encoder–decoder Transformer uses attention in three different places. They differ only in where the queries, keys, and values come from, and whether a mask is applied:

AttentionQueries fromKeys & values fromMaskMatrix shape
Encoder self-attentionencoderencodernone$n\times n$
Decoder masked self-attentiondecoderdecodercausal$m\times m$
Cross-attentiondecoderencoder outputnone$m\times n$
rows: queries · columns: keys (blue = allowed)
Figure 2 (interactive). Which keys each query may attend to in the three kinds of attention, for the source How are you doing (green) and the decoder input <bos> 你 好 嗎 (red).

4The full Transformer

Putting everything together gives the original Transformer architecture. The encoder is a stack of $N$ identical layers, each with two sublayers. The decoder is a stack of $N$ layers, each with three sublayers; cross-attention is the one in the middle.

Figure 3 (interactive). The encoder–decoder Transformer. Click a component to see what it does. The arrow from the top of the encoder into every decoder layer is where cross-attention reads the source.

Training and generation

5Results and cost

The Transformer was introduced for machine translation, evaluated with BLEU: a score from 0 to 100 measuring how many $n$-grams of the system's translation also appear in human reference translations (higher is better). On the WMT 2014 English–German and English–French benchmarks it beat the previous best systems, including ensembles, at a fraction of the training cost:

ModelBLEU EN–DEBLEU EN–FRTraining cost EN–DE (FLOPs)
GNMT + RL (recurrent)24.639.92$2.3\cdot 10^{19}$
ConvS2S (convolutional)25.1640.46$9.6\cdot 10^{18}$
ConvS2S ensemble26.3641.29$7.7\cdot 10^{19}$
Transformer (base)27.338.1$3.3\cdot 10^{18}$
Transformer (big)28.441.8$2.3\cdot 10^{19}$

Numbers from Vaswani et al. (2017), Table 2.

Why is it cheaper to train?

The paper compares three kinds of layers for a sequence of length $n$ with dimension $d$ (and kernel size $k$ for convolutions):

LayerComputation per layerSequential stepsMax path length between two words
Self-attention$O(n^2\cdot d)$$O(1)$$O(1)$
Recurrent$O(n\cdot d^2)$$O(n)$$O(n)$
Convolutional$O(k\cdot n\cdot d^2)$$O(1)$$O(\log_k n)$ (stacked dilated convolutions)
Sequence length $n$:Dimension $d$:
Figure 4 (interactive). Computation per layer for self-attention ($n^2 d$) and a recurrent layer ($n d^2$), ignoring constants. Self-attention wins for short sequences and loses once $n$ exceeds $d$.

6Summary

7Practice

Exercise 1: shapes

A source sentence has $n=12$ tokens and the target has $m=9$ tokens, with $d=512$ and $d_k=d_v=64$ for one head. Give the shapes of $Q$, $K$, $V$, $QK^\top$, and the cross-attention output.

$Q$: $9\times 64$ (from the decoder). $K$, $V$: $12\times 64$ (from the encoder). $QK^\top$: $9\times 12$. Output: $9\times 64$, one vector per target position.

Exercise 2: which attention?

For each statement, name the kind of attention: (a) the query is 好 and the keys are <bos>, 你, 好; (b) the query is you and the keys are all four English words; (c) the query is 好 and the keys are all four English words.

(a) Decoder masked self-attention. (b) Encoder self-attention. (c) Cross-attention.

Exercise 3: cost

With $d=512$, for which sequence lengths is a self-attention layer cheaper than a recurrent layer (ignoring constants)?

$n^2 d \lt n d^2 \iff n < d$, so for $n \lt 512$.

Quick quiz

8Further reading