Module 23 — Advanced Attention
Modern Large Language Models (LLMs) improve the original Transformer attention mechanism using advanced techniques that increase context length, reduce memory usage, and accelerate inference.
These attention mechanisms are widely used in models such as LLaMA, Gemma, Qwen, Mistral, DeepSeek, Phi, Falcon, Mixtral, and many other state-of-the-art Transformers.
Topics
- Rotary Position Embedding (RoPE)
- ALiBi
- FlashAttention
- Grouped Query Attention (GQA)
- Multi Query Attention (MQA)
1. Rotary Position Embedding (RoPE)
RoPE encodes positional information by rotating the Query and Key vectors in the embedding space.
Instead of adding positional embeddings, RoPE applies a rotation matrix.
Formula
Rotation Matrix
For each token position
Rotated Query
Rotated Key
Attention
RoPE is used in
- LLaMA
- Gemma
- Qwen
- DeepSeek
- Phi
- Mistral
2. ALiBi (Attention with Linear Biases)
ALiBi adds a linear positional bias directly to the attention scores.
Formula
where
- = Learned slope
- = Relative token distance
Final Attention
ALiBi enables efficient extrapolation to longer context lengths without learned positional embeddings.
3. FlashAttention
FlashAttention computes exact attention while minimizing GPU memory usage through block-wise computation.
Standard attention
FlashAttention computes the same result but processes Query, Key, and Value matrices in memory-efficient blocks.
Mathematically,
but with optimized memory complexity and significantly faster GPU execution.
4. Grouped Query Attention (GQA)
Grouped Query Attention reduces memory usage by allowing multiple Query heads to share the same Key and Value groups.
Query
Shared Key Group
Shared Value Group
Attention
where
- Multiple Query Heads share one Key-Value group.
GQA is used in
- LLaMA 2
- LLaMA 3
- Gemma
- Qwen
- Mistral
5. Multi Query Attention (MQA)
Multi Query Attention shares a single Key and Value across all Query heads.
Query
Shared Key
Shared Value
Attention
Advantages
- Lower KV Cache Memory
- Faster Inference
- Reduced GPU Bandwidth
MQA is commonly used in
- PaLM
- Falcon
- Gemini
- Some GPT variants
6. Comparison
| Method | Key Idea |
|---|---|
| RoPE | Rotates Query and Key vectors |
| ALiBi | Adds linear positional bias |
| FlashAttention | Memory-efficient exact attention |
| GQA | Groups Key-Value heads |
| MQA | Shares one Key-Value pair across all Query heads |
7. Complete Advanced Attention Pipeline
The generalized attention computation becomes
8. Applications
These attention mechanisms are widely used in
- GPT-4
- LLaMA 2
- LLaMA 3
- Gemma
- Gemma 2
- Qwen
- Mistral
- Mixtral
- DeepSeek
- Phi
- Falcon
- Claude
- Gemini
- Large Language Models (LLMs)
- Vision-Language Models
- Multimodal Transformers
Summary
| Concept | Formula |
|---|---|
| RoPE | |
| Rotation Matrix |