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 · 6 min read ·Core ·Intermediate

Visual quick review

Visual first · depth when needed

Distinguish self-attention from cross-attention by tracing which sequence supplies Q, K, and V and predicting the resulting attention-matrix shape.

Preparing the visual…

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

Which sequence supplies Q, K, and V?

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
Read it this way: 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.

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.