Improving Transformer Efficiency: Attention Approximation, MHA, MQA, and GQA
In the previous article, we explored how Transformer architectures improved positional understanding using:
Improving Transformer Efficiency: Attention Approximation, MHA, MQA, and GQA
In the previous article, we explored how Transformer architectures improved positional understanding using:
- Positional Embeddings
- RoPE
- ALiBi
- Layer Normalization
But another major challenge still remained.
Attention computation becomes very expensive as context length increases.
As Large Language Models started handling thousands of tokens, researchers began focusing on:
- reducing memory usage
- improving inference speed
- scaling long-context processing efficiently
This led to several important optimizations in modern Transformer architectures.
In this article, let’s understand:
- Attention Approximation
- Sharing Attention Heads
- MHA
- MQA
- GQA
in simple terms.
Why Attention Becomes Expensive
In self-attention, every token interacts with every other token.
If sequence length is: n
then attention complexity becomes: O(n²)
This means:
- doubling sequence length increases computation heavily
- memory usage grows rapidly
- inference becomes slower
Example
If:
- 1,000 tokens → 1 million interactions
- 10,000 tokens → 100 million interactions
This becomes very expensive for modern LLMs.
That is why attention optimization became extremely important.
1. Attention Approximation
Flowchart illustrating how attention scales down from expensive quadratic O(n²) full dense attention to linear O(n) kernels to resolve long-context bottlenecks.
Matrix visualization of standard causal transformer attention, where tokens are masked from attending to future positions.
Architectural schematics contrasting a single Scaled Dot-Product Attention module with a parallelized Multi-Head Attention layer.
Diagram demonstrating how softmax normalization and position scaling bias queries in long sequences to concentrate attention on the very beginning of a document.
Chart mapping how effective context length stacks and expands across subsequent layers when using localized window sizes.
Workflow mapping the Scatterbrain framework, which unifies sparse and low-rank matrix approximations to replicate traditional softmax attention.
System overview of Contextual Priority Attention (CPA), showing how a global context vector dictates adaptive, linear-time sparse attention masks.
Researchers introduced several methods to reduce attention computation.
The main idea:
Instead of calculating full attention everywhere, approximate important interactions efficiently.
Core Intuition
Not every token needs equal attention.
Example: In a long paragraph, a word may only strongly depend on:
- nearby tokens
- important keywords
- relevant context regions
So attention can be approximated intelligently.
Common Approximation Ideas
Different architectures use different strategies:
- Sparse Attention
- Local Attention
- Sliding Window Attention
- Low-rank Approximation
- Kernel-based Attention
Goal remains the same:
- reduce computation
- reduce memory
- improve scalability
Benefits
✅ Faster inference ✅ Lower memory usage ✅ Better long-context handling ✅ More scalable architectures
This became important for:
- long-document processing
- code models
- multimodal systems
- modern long-context LLMs
2. Sharing Attention Heads
A structural comparison mapping the consolidation of Key and Value heads across Multi-head, Grouped-query, and Multi-query attention layouts.
A conceptual diagram showing a single query vector computing dynamic dot-product weights across an array of keys to produce a weighted sum of values.
A schematic breakdown of a standard Transformer block alongside an expanded view of its Multi-head Self Attention substructure.
A token-level workflow tracking how an individual sequence item generates its output embedding using matrix projections and softmax attention weights.
Detailed block diagrams contrasting the basic framework of Scaled Dot-Product Attention with a parallelized Multi-Head Attention layer.
A funnel infographic illustrating the stage-by-stage pipeline of query formation, key matching, and value prioritization to achieve an enhanced contextual understanding.
In the original Transformer architecture, every attention head had separate:
- Query
- Key
- Value
parameters.
Original Multi-Head Attention
Example: 8 attention heads:
- 8 Query matrices
- 8 Key matrices
- 8 Value matrices
This provides strong representation learning, but memory usage becomes large.
Observation
Researchers later observed:
Many attention heads learn similar patterns.
So one important question appeared:
Do we really need separate Key and Value heads for everything?
This led to shared attention mechanisms.
Main Idea
Instead of fully independent heads, some architectures:
- share Key representations
- share Value representations
across multiple Query heads.
This reduces:
- memory usage
- KV cache size
- inference latency
This optimization became very important for production LLM systems.
Understanding MHA, MQA, and GQA
These are some of the most important optimizations in modern Transformer architectures.
3. Multi-Head Attention (MHA)
Architectural comparison tracking KV-cache reductions across MHA, GQA, MQA, and Multi-Head Latent Attention (MLA)
Flowchart of the Multi-Headed Attention pipeline mapping parallel linear transformations to final head concatenation.
Step-by-step workflow showing KV-cache mechanics and token broadcasting during the inference generation phase.
Parallel matrix computation and causal masking layout during the Transformer training phase.
Macro view of a standard Transformer Block side-by-side with its Multi-head Self Attention substructure.
MHA stands for:
Multi-Head Attention
This is the original Transformer attention mechanism.
How MHA Works
Every attention head has separate:
- Query
- Key
- Value
matrices.
Example: 8 heads:
- 8 separate Q
- 8 separate K
- 8 separate V
Why Multiple Heads?
Different heads can learn different relationships.
Example:
- one head learns grammar
- one head learns semantic meaning
- one head learns positional relationships
This improves contextual understanding significantly.
Benefits of MHA
✅ Rich representation learning ✅ Strong contextual understanding ✅ High model capacity
Problems with MHA
❌ Large memory usage ❌ High KV cache size ❌ Slower inference for large models
As LLMs became larger, these problems became expensive.
This led to MQA.
4. Multi-Query Attention (MQA)
High-level comparison structural mapping of head consolidation across Multi-head, Grouped-query, and Multi-query attention methods.
Step-by-step pipeline illustrating KV-cache splits, current token caching, and cross-head value broadcasting during inference.
A funnel infographic breaking down the phase-by-phase pipeline of query formation, key matching, and value prioritization.
Dataflow architecture diagram of Multi-Query Attention executing multiple query heads against a single shared K/V pair.
Breakdown schematic of Multi-Query Attention illustrating the shared projection matrix layers that unify key-value maps prior to final concatenation.
Side-by-side block diagrams contrasting traditional Scaled Dot-Product Attention with a parallelized Multi-Head Attention layer.
MQA stands for:
Multi-Query Attention
The main idea is simple:
- Keep multiple Query heads
- Share one Key and one Value across all heads
Example
Instead of:
8 Query 8 Key 8 Value
MQA uses:
8 Query 1 shared Key 1 shared Value
Why This Helps
KV cache memory becomes much smaller.
This is very important during autoregressive generation in LLMs.
Benefits: ✅ Faster inference ✅ Lower memory usage ✅ Better scalability ✅ Smaller KV cache
Tradeoff
Because all heads share the same Key and Value:
- representation diversity reduces slightly
So efficiency improves, but some modeling capacity may reduce.
This led to GQA.
5. Grouped Query Attention (GQA)
Conceptual breakdown of Grouped-Query Attention (GQA) partitioning 8 query heads into 4 sub-groups, each mapped to a single key-value pair.
A spectrum comparison illustrating how the number of key-value heads scales from Multi-Head Attention down to a single pair in Multi-Query Attention.
Architectural tensor flow chart displaying the linear transformations, slicing, and dot-product pooling phases across query and key-value blocks.
Schematic diagram of a modern Transformer Encoder architecture featuring dual Local Self-Attention (LSA) and Global Self-Attention (GSA) tracks.
Dataflow routing diagram detailing Grouped-Query Attention (GQA) separating active heads into isolated, parallel execution groups.
GQA stands for:
Grouped Query Attention
It is a middle ground between:
- MHA
- MQA
Core Idea
Instead of:
- separate KV for every head or
- one KV for all heads
GQA creates groups.
Example
8 Query heads:
- Group 1 → shared KV
- Group 2 → shared KV
- Group 3 → shared KV
- Group 4 → shared KV
So multiple heads share KV within groups.
Why GQA Became Popular
GQA balances:
- quality
- efficiency
better than MHA and MQA.
Benefits: ✅ Lower memory than MHA ✅ Better representation than MQA ✅ Faster inference ✅ Good scalability
This is why many modern LLMs use GQA today.

Why These Optimizations Matter
Modern LLMs are not only about intelligence.
A huge amount of engineering goes into:
- reducing inference cost
- reducing GPU memory
- improving serving speed
- scaling long-context processing
Without these optimizations:
- large-scale LLM serving becomes extremely expensive
Final Thoughts
Transformer research is continuously evolving.
The original Attention mechanism was powerful, but modern architectures improved it further through:
- approximation techniques
- KV sharing
- efficient attention designs
This is one major reason why modern LLMs became:
- faster
- cheaper
- more scalable
while still handling massive context windows.
메타데이터
- post_id
- 6b9f9368ec52
- slug
- improving-transformer-efficiency-attention-approximation-mha-mqa-and-gqa-6b9f9368ec52
- url
- https://medium.com/@rishi-kumar747/improving-transformer-efficiency-attention-approximation-mha-mqa-and-gqa-6b9f9368ec52
- canonical_url
- https://medium.com/@rishi-kumar747/improving-transformer-efficiency-attention-approximation-mha-mqa-and-gqa-6b9f9368ec52
- author_url
- https://medium.com/@rishi-kumar747
- status
- ok
- fetched_at
- 2026-06-09 15:37:30