LLMS & TRANSFORMERS · INTERVIEW QUESTION
How does multi-head attention differ from single-head attention? Why use multiple heads?
🟡 intermediate · Transformer Architecture
Hint: Think about attending to different representation subspaces.
Answer
Single-head attention computes one set of attention weights. Multi-head attention runs h parallel attention operations with separate learned projections (W_Q^i, W_K^i, W_V^i). Each head can attend to different positions and capture different types of relationships (syntactic in one head, semantic in another). Outputs are concatenated and linearly projected: MultiHead(Q,K,V) = Concat(head_1,...,head_h)W_O.
Benefits:
- Richer representations from different subspaces
- More stable gradients
- Parallel computation. Typically h=8 or h=16 with d_model/h dimensions per head. Grouped Query Attention (GQA) in modern LLMs shares K,V heads across query heads to reduce KV-cache memory.
Related questions
- Explain the self-attention mechanism in detail. What are Q, K, V and why do we use scaled dot-product attention?
- Explain positional encoding in Transformers. Compare sinusoidal, learned, RoPE, and ALiBi.
- Explain FlashAttention. How does it achieve sub-quadratic memory while maintaining exact attention?
- What are Large Language Models (LLMs) and how do they work?
- Explain the difference between LoRA, full fine-tuning, and prompt tuning. When would you use each?