↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Decoder layer 1 of 6 8 heads · 64-D · scores q·k with no 1/√d (T5 folds it into its weights) · no mask
Loading recorded attention…

What cross-attention is

Self-attention mixes tokens of one sequence with each other. An encoder–decoder model has two sequences: the encoder reads the source, a sentence to translate, and the decoder writes the target one token at a time. Cross-attention connects them. Its queries come from the decoder's tokens, its keys and values from the encoder's output, so each target token can look up the parts of the source it needs.

This page records it in T5-small (60M), in all 6 decoder layers with 8 heads of 64 dimensions each, on three translations: English to German with the verb at the end, a short English to German sentence, and English to French. The toolbar shows the model's greedy translation; the decoder's input is that translation after T5's start token, which is its padding token.

Queries from one sequence, keys from another

The Encoder output strips at the top left are the encoder's final vectors, one per source token; the Decoder input strips below them are the decoder's vectors after its RMSNorm. The bank projects queries from the decoder and keys and values from the encoder, with this layer's own weights:

with the decoder input and the encoder output. The matrix is no longer square: it has one row per decoder token and one column per encoder token. There is no mask, because the whole source is known before decoding starts. And since the encoder output does not change while the decoder writes, each layer can compute its keys and values once and reuse them for every new target token.

T5's details

Two things differ from the formula on the MHA tab. T5 does not divide the scores by ; it folds that scale into the initialisation of its weights, so the scores are plain , as the row popup's formula shows. And T5's self-attention adds a learned bias for each bucket of relative distance, but its cross-attention adds none: the recording checks that the position bias in cross-attention is exactly zero. Positions of source tokens reach the decoder only through what the encoder's own self-attention put into its output.

Alignment

A decoder token usually needs the source words it is translating, so cross-attention weights often look like a word alignment. In the English → German · verb last example, decoder layer 2 head 8 is such a head: “ nie” puts nearly all its weight on “ never”, “ Garten” 0.998 on “ garden” and “gesehen” 0.991 on “ seen”, although German moves the participle to the end of the sentence. Many other heads, above all in the last decoder layer, spread their weight over several source tokens.

These weights show where the decoder looked, which is not always a clean alignment. Garg et al. extracted alignments from a translation model's attention and trained one head on them alongside translation, which made its alignments competitive with those of the statistical aligner GIZA++.

Where it came from

Cross-attention is where attention began. Bahdanau et al. (2014) let an RNN decoder take a weighted average of the encoder's states at every step, with weights from a small network (the Bahdanau attention tab of the Before Transformers explainer), and Luong et al. (2015) replaced that network with a dot product. Vaswani et al. (2017) kept it as the encoder–decoder attention in every decoder layer of the Transformer, beside self-attention, and T5 (2019) framed every text task as translation from input text to output text. The same mechanism lets Whisper's decoder read audio and the U-Net of latent diffusion models read a text prompt. The Transformer Explainer's encoder–decoder tab shows a whole T5 layer around it.

Papers

  1. Neural Machine Translation by Jointly Learning to Align and Translate Dzmitry Bahdanau, Kyunghyun Cho, Yoshua Bengio · ICLR 2015 Introduces attention from a decoder to the encoder's states, the first cross-attention.
  2. Attention Is All You Need Ashish Vaswani et al. · NeurIPS 2017 Scaled dot-product encoder–decoder attention in every decoder layer, the form T5 uses.
  3. Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer Colin Raffel et al. · JMLR 2020 T5, the encoder–decoder model this page records.
  4. Effective Approaches to Attention-based Neural Machine Translation Minh-Thang Luong, Hieu Pham, Christopher D. Manning · EMNLP 2015 Dot-product and other scores for attention over the source sentence.
  5. Jointly Learning to Align and Translate with Transformer Models Sarthak Garg, Stephan Peitz, Udhyakumar Nallasamy, Matthias Paulik · EMNLP 2019 Extracts word alignments from cross-attention and trains a head on them alongside translation.
  6. High-Resolution Image Synthesis with Latent Diffusion Models Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, Björn Ommer · CVPR 2022 Cross-attention from image latents to a text encoder, the conditioning of Stable Diffusion.

See also