Introduction to Self-attention and multi-head attention
Self-attention allows each token in a sequence to consider information from other tokens in the same sequence. It produces contextual representations that reflect relationships across the input.
Multi-head attention extends this mechanism by running multiple attention operations in parallel, allowing the model to combine information from different representation subspaces.
Core concepts and architecture
Understand how a token representation is projected into query, key, and value vectors and how attention weights determine the contribution of other sequence positions.
Learn how multi-head attention divides or projects representations into multiple heads, performs attention independently, concatenates the results, and applies an output projection.
Understand causal masks in decoder models and how they prevent a token from attending to future positions during autoregressive generation.
Practical implementation
Implement scaled dot-product self-attention with tensor operations and verify the dimensions of query, key, value, and output tensors.
Build a simplified multi-head attention module and experiment with different numbers of heads and embedding dimensions.
Apply causal and padding masks to understand how attention behaviour changes for language generation and variable-length input sequences.
Challenges and trade-offs
Increasing sequence length can significantly increase attention memory and computational requirements.
More attention heads do not automatically improve model quality. Head dimensions, model capacity, training, and architectural configuration all matter.
Incorrect masking or tensor reshaping can silently produce incorrect attention behaviour and unstable model outputs.
Interview preparation
Compare self-attention with cross-attention and explain how multi-head attention differs from a single attention operation.
Describe query, key, and value projections, head splitting, attention calculation, concatenation, and output projection.
Explain causal masking, padding masks, attention complexity, and the importance of tensor dimensions.
Key takeaways
- Understand the purpose and main concepts of Self-attention and multi-head attention.
- Understand how Self-attention and multi-head attention works and how its components interact.
- Be prepared to explain practical implementation and real-world use cases.
- Understand common challenges, limitations, and design trade-offs.
- Practise explaining Self-attention and multi-head attention clearly in a technical interview.