Cross-Attention & Conditioning
Attention between two sequences: encoder-decoder cross-attention, text-to-image conditioning (Stable Diffusion), retrieval-augmented generation, adapter cross-attention, and the mathematical properties of cross vs self attention.
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Cross-Attention Formulation
- Encoder-Decoder Cross-Attention
- Text-to-Image Conditioning
- Retrieval-Augmented Generation (RAG)
- Cross-Attention vs Self-Attention
- Computational Properties
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive cross-attention and distinguish it from self-attention.
- Explain the asymmetry: queries from one sequence, keys/values from another.
- Show how cross-attention enables text conditioning in diffusion models.
- Analyze the computational complexity of cross-attention.
- Explain why cross-attention KV-cache doesn't grow during generation.
Notation
- — primary sequence (e.g., decoder states)
- — context sequence (e.g., encoder output)
- — length of primary sequence
- — length of context sequence
Core Intuition
Self-attention asks: "what parts of my own sequence are relevant?" Cross-attention asks: "what parts of another sequence are relevant to me?" The query comes from one sequence, while keys and values come from a different sequence — enabling information flow between two modalities or representations.
Cross-Attention
Cross-Attention Formulation
Decomposition:
- Queries from primary: .
- Keys from context: .
- Values from context: .
- Attention matrix: (rectangular, not square).
- Output: (same length as primary sequence).
Key difference from self-attention: K, V come from a different sequence than Q. No causal masking (context is fully available).
Encoder-Decoder Cross-Attention
In T5/BART-style models:
- Encoder processes input (bidirectional self-attention) → .
- Decoder generates output one token at a time:
- Causal self-attention over decoder tokens.
- Cross-attention: decoder queries, encoder keys/values.
Layer structure of decoder block:
KV-cache for cross-attention: Since the encoder output is fixed during generation, the K, V projections are computed once (during prefill) and reused for all decoder steps. Cross-attention KV-cache doesn't grow during generation.
Text-to-Image Conditioning
In Stable Diffusion / DALL-E, cross-attention injects text information into the image generation process:
- Context : Text encoder output (e.g., CLIP embeddings of the prompt), tokens.
- Primary : Spatial features of the noisy image at each diffusion step.
Interpretation: Each spatial position in the image "looks at" the text and decides which words are relevant for generating that patch. The word "dog" will receive high attention from spatial positions where the dog should appear.
Classifier-free guidance works by comparing cross-attended vs unconditioned outputs:
where amplifies the cross-attention signal.
Retrieval-Augmented Generation (RAG)
Cross-attention can integrate retrieved documents:
- Primary : Current generation state.
- Context : Concatenated retrieved passages.
RETRO architecture: Interleave self-attention layers with cross-attention layers that attend to retrieved neighbors:
Advantage: The model can access a vast knowledge base without storing everything in parameters.
Cross-Attention vs Self-Attention
Mathematical properties:
- Self-attention (): , symmetric input, causal masking possible.
- Cross-attention (): , asymmetric, no causal constraint on context.
Gradient flow:
- Self-attention: Gradients flow between all positions bidirectionally (or causally).
- Cross-attention: Gradients flow from decoder → encoder (during training), enabling end-to-end learning.
Permutation properties:
- Self-attention: equivariant to permutation of input.
- Cross-attention: equivariant to permutation of primary sequence, but NOT to permutation of context (unless we want it to be).
Computational Properties
Complexity: — scales with product of sequence lengths.
Typically: is much smaller than (e.g., text prompt = 77 tokens, image = 4096 spatial positions). So cross-attention is cheap relative to self-attention on the primary sequence.
Memory: Attention matrix is . For : only 315K entries (vs 16.7M for self-attention).
Common Pitfalls
Pitfall 1. Applying causal masking to cross-attention. The context (encoder output) is fully available to all decoder positions — masking it defeats the purpose.
Pitfall 2. Forgetting that cross-attention K/V can be pre-computed. Since doesn't change during generation, computing K/V for cross-attention at every step wastes compute.
Pitfall 3. Assuming cross-attention order doesn't matter. The order of self-attention → cross-attention → FFN in a decoder block matters: self-attention first allows the decoder to build context, then cross-attention injects external information.
Summary
- Cross-attention: Q from one sequence, K/V from another → information bridge between sequences.
- Encoder-decoder: enables sequence-to-sequence tasks (translation, summarization).
- Text-to-image: spatial features query text embeddings for conditioning.
- RAG: generation state queries retrieved passages for knowledge.
- KV-cache for cross-attention is fixed (doesn't grow during generation).
- Complexity: — typically cheap since .
Exercises
Exercise 1. For an encoder-decoder model with encoder length and decoder length : compute the cross-attention matrix size and compare to decoder self-attention.
Exercise 2. Explain why cross-attention KV-cache doesn't grow during autoregressive generation, while self-attention KV-cache does.
Exercise 3. In Stable Diffusion with image resolution 64×64 and text length 77: compute the cross-attention matrix size and FLOPs per layer.
Exercise 4. Derive the gradient through the cross-attention operation.
Exercise 5. Design an architecture that uses cross-attention to combine visual and audio features for a multimodal model.