Skip to content
mlmentorship

Self-attention vs cross-attention

Self-attention reads from one sequence; cross-attention reads from another. This input choice determines encoder-only, decoder-only, and encoder-decoder structures.

Published · 7 min read ·Core ·Intermediate

Summary

Self-attention computes where are all derived from the same sequence. Cross-attention uses from one sequence and from another. Same kernel, different routing.

This input choice separates encoder-only models (BERT), decoder-only models (GPT), and encoder-decoder models (T5, the original Transformer, and Whisper). Multimodal architectures also use cross-attention to connect image, text, or audio representations.

If you can write the matrix multiplications and explain why a layer uses one form, you understand the attention structure of common transformer architectures.

Self-attention

Inputs: a single sequence .

Each token attends to every other token (subject to masking). Used in:

  • BERT encoder layers: bidirectional self-attention.
  • GPT decoder layers: causal self-attention. The mask zeros out positions so token cannot attend to future tokens.

Cross-attention

Inputs: a query sequence and a key-value sequence .

Same softmax, same scaling. The shape of the attention matrix is now .

Used wherever the model needs to “look up” information from a different source:

  • Encoder-decoder transformers: decoder layers cross-attend to the encoder output. Translation, summarization, speech-to-text.
  • Diffusion models with text conditioning: image-side latents cross-attend to text embeddings. Stable Diffusion, DiT.
  • Perceiver / Q-Former: a small set of learned latent queries cross-attend to a large input (image patches, audio frames) to compress it.
  • RAG architectures with separate memory: model states can cross-attend to retrieved document representations. Many decoder-only RAG systems instead concatenate retrieved text into the prompt and use self-attention.

Learning objective

Queries determine output rows even when keys and values come from another sequence

Queries determine the output rowsTwo query positions attending to three source positions produce two output rows. The number of source keys does not create an extra output query.The query destinations determineoutput rowsK0K1K2Q0Output 0Q1Output 1
Derivation
Self-attention uses one source while cross-attention routes queries and key-values from different sources The upper panel shows one three-token sequence X supplying query, key, and value projections, producing a square three-by-three attention matrix whose rows and columns both refer to X. The lower panel shows a two-token sequence X supplying queries while a three-token sequence Y supplies keys and values, producing a rectangular two-by-three matrix. In both panels the same scaled dot-product attention kernel maps one output row to each query token. SELF-ATTENTION · ONE SOURCE X3 tokens Q = XWQ K = XWK V = XWV keys: X (3 columns)queries:X(3 rows) scores: 3 × 3each X token reads from X CROSS-ATTENTION · TWO SOURCES X2 queries Y3 memory tokens Q = XWQ K = YWK V = YWV keys: Y (3 columns)queries:X(2 rows) scores: 2 × 3output: 2 rows each X query reads from Y · same attention kernel
follow the projection inputs, not the operation name. In self-attention, one sequence X fans out to Q, K, and V, so three query tokens scored against three key tokens form a square 3 × 3 matrix. In cross-attention, X supplies two queries while Y supplies three key-value pairs, so the same kernel forms a rectangular 2 × 3 matrix and returns one row per X query. Source labels, arrows, and matrix dimensions carry the distinction without color. Original schematic, checked against Attention Is All You Need.
Check: Why does the cross-attention example return two rows despite reading three memory tokens?
Read it this way: Two query positions attending to three source positions produce two output rows. The number of source keys does not create an extra output query.
Do three source keys create a third output when there are only two queries?

Where each lives in a transformer block

Encoder block (BERT, T5 encoder):

  1. Self-attention.
  2. FFN.

Decoder block (GPT):

  1. Causal self-attention.
  2. FFN.

Encoder-decoder block (T5 decoder, original Transformer decoder):

  1. Causal self-attention.
  2. Cross-attention to encoder output.
  3. FFN.

The decoder reads its own past tokens (self-attention) and the encoder’s output (cross-attention) at every layer.

Tradeoffs

  • Compute: self-attention is . Cross-attention is , which can be much cheaper if is small (compressed conditioning) or much larger (cross-attending to a long context).
  • KV-cache: at encoder-decoder inference, the decoder can cache both forms. Cross-attention K/V are computed once from the fixed encoder output and reused for every decoded token; self-attention K/V grow with the decoded sequence.

Variants

  • Masked cross-attention: padding masks hide invalid K/V positions. A triangular mask is meaningful only when query and K/V positions share an ordered alignment; separate source and target sequences do not imply one automatically.
  • Cross-attention with caching: precompute from a fixed conditioning sequence (system prompt, retrieved docs) and reuse across decoding steps.
  • Asymmetric cross-attention: in Perceiver, the queries are a small learned set (e.g. 256), the K/V are massive (e.g. all image patches). The model compresses high-dim input into a fixed-size latent.

Common pitfalls

  • Calling decoder self-attention “cross-attention.” They are different. Self-attention reads from the same sequence (the previously generated tokens); cross-attention reads from another sequence (encoder output, retrieved docs).
  • Forgetting that decoder-only LLMs do not use cross-attention. Their conditioning is the prompt prefix, attended via self-attention, not a separate cross-attention path.
  • Conflating attention masks with attention types. The mask shape differs (causal mask is square and triangular; cross-attention is rectangular and usually unmasked) but the operation is the same.