Articles
LLM (Attention – Advanced Topics) (4)
This guide covers advanced attention topics in large language models, including transformer self-attention, multi-head attention, positional…
Attention Optimization in Modern Transformers
A learning-oriented resource for understanding why attention evolved - and how those ideas generalize
1. Why “vanilla” Attention became a bottleneck
The original scaled dot-product attention works well conceptually, but it does not scale gracefully. Its computational and memory complexity grow quadratically with sequence length. That alone places a hard ceiling on context size, regardless of how powerful the hardware is.
The second issue is practical rather than theoretical. During inference, attention requires storing key–value (KV) tensors for every token in the context. As models grow larger and context windows stretch into tens or hundreds of thousands of tokens, KV cache becomes the dominant memory cost. At that point, attention is no longer just slow - it actively constrains model deployment.
These two problems - context length limits and inference inefficiency - drive almost every attention optimization you see today.
2. Two core optimization directions
Most attention research follows one (or both) of these paths:
-
Increase effective context length Either by reducing quadratic cost or by structuring attention so the model does not need to attend to everything equally.
-
Reduce computation and memory overhead Especially during inference, where latency and KV cache size matter more than raw FLOPs.
Nearly all modern variants are different trade-offs along these axes.
3. Families of Attention optimizations
Rather than memorizing techniques, it helps to group them by idea.
Sparse Attention limits which tokens can attend to each other. By introducing structure (local windows, global tokens, patterns), it avoids computing unnecessary interactions. This idea extends naturally to vision, long-document modeling, and multimodal systems.
Linear Attention reorders or reformulates attention so complexity becomes linear in sequence length. The key insight is that softmax attention can be approximated or decomposed, allowing attention to scale to very long contexts - at the cost of exactness.
Low-rank methods approximate the attention matrix itself. Instead of computing a full interaction matrix, they assume the matrix lies near a low-dimensional subspace. This idea generalizes well beyond attention, appearing in model compression and efficient fine-tuning.
Attention approximation replaces exact probability distributions with cheaper surrogates. This trades theoretical precision for practical scalability.
Multi-head structure redesigns rethink how queries, keys, and values are distributed across heads. This is where MQA and GQA fit in.
4. Multi-Query Attention (MQA)
4.1 Why standard Multi-Head Attention struggles
Multi-Head Attention (MHA) was originally introduced to let different heads focus on different relational patterns. In practice, increasing the number of heads does not linearly improve learning capacity, especially in very large models.
At inference time, the cost is clearer. Each head maintains its own key and value cache. As head count grows, memory usage explodes, and inference throughput collapses. For large-scale deployment, this becomes unacceptable.
4.2 What MQA changes
Multi-Query Attention keeps multiple query heads but shares a single key and value across all heads.
The model still benefits from diverse query projections, but the KV cache is no longer multiplied by the number of heads. This is a surgical change with an outsized impact on inference efficiency.
4.3 Conceptual difference
- MHA: each head has its own Q, K, and V
- MQA: each head has its own Q, but all heads share K and V
This design shifts capacity away from redundant memory usage and toward more useful computation.
4.4 Why MQA matters in practice
The primary gain is dramatically smaller KV cache, which directly translates into faster decoding and lower memory pressure. This is one of the key enablers for large-context, production-scale language models.
4.5 Where it is used
MQA appears in models such as PaLM, ChatGLM2, and Falcon, though each integrates it differently. Some reallocate saved parameters into larger feed-forward networks, while others increase model width to preserve expressivity.
5. Grouped-Query Attention (GQA)
Grouped-Query Attention sits between MHA and MQA. Instead of one global key–value pair, it uses a small number of KV groups, shared by subsets of query heads.
This design acknowledges a practical reality: fully shared KV may reduce representational diversity too much for some models, while per-head KV is wasteful. GQA provides a tunable middle ground.
Large models such as LLaMA-2 (34B and 70B) and ChatGLM2 adopt this compromise to balance inference efficiency and model quality.
6. FlashAttention
FlashAttention addresses a different bottleneck: memory bandwidth, not math.
The core idea is to compute attention in blocks, performing softmax incrementally so intermediate results never need to be written to high-bandwidth memory. Instead, computation is structured to maximize on-chip SRAM reuse.
The result is attention that is:
- faster,
- more memory-efficient,
- and numerically stable.
Techniques such as kernel fusion, recomputation, and blocked softmax are not attention-specific - they reflect a broader trend of algorithm–hardware co-design that will only become more important.
Models like LLaMA and Falcon rely on FlashAttention to make training and inference feasible at scale.
7. Parallel Transformer Blocks
Traditional Transformer blocks execute attention first, then the feed-forward netw
How to Use This Page as a Prep Plan
Do not treat this as passive reading. Convert the ideas in this page into a short weekly loop: learn one idea, practice it under interview conditions, then write down what changed. That is the fastest way to turn advice into visible interview behavior.
| Prep area | What you need to prove | Practice artifact |
|---|---|---|
| Understand | Turn the prompt into a concrete goal. | Clarifying questions and success criteria. |
| Practice | Use realistic constraints and timed reps. | Worked examples with edge cases. |
| Explain | Make reasoning visible. | Tradeoffs, assumptions, and test strategy. |
| Improve | Review misses quickly. | A short feedback log and next action. |
For LLM (Attention – Advanced Topics) (4), the strongest candidates usually do three things well: they make their assumptions explicit, they use concrete examples instead of vague claims, and they review mistakes quickly enough that the next practice rep is better than the last one.
Video Walkthrough
This verified YouTube video gives a second pass on the same preparation area. Use it after reading the guide, then come back and turn the advice into a practice artifact.
Video: Transformers, the tech behind LLMs | Deep Learning Chapter 5
FAQ
How should I use this guide?
Read it once for the structure, then turn each section into a practice task with a visible artifact.
What should I do if I am short on time?
Prioritize the skills most likely to be tested, then do one mock or timed drill to expose the largest gap.
How do I know I am ready?
You can explain your approach clearly, recover from hints, and name tradeoffs without relying on memorized wording.