LLM Architecture Evolution and Model Size Selection Guide
Explore the architectural choices behind modern large language models, from decoder-only Transformers and positional encoding to MHA, MQA, and GQA. Learn how attention design affects KV cache requirements and why GPU memory, concurrency, and latency should guide model size and quantization choices.
Most mainstream large language models use decoder-only Transformers. Grouped-query attention (GQA) is widely adopted, and many large-scale models use mixture-of-experts (MoE) architectures. When selecting a model, start by estimating GPU memory requirements, concurrency, and latency, then choose the parameter count and quantization approach.
1. Core Architecture: Decoder-Only Transformers and Key Components
The original Transformer used an encoder–decoder architecture. Most modern generative LLMs retain only the decoder, generating tokens one at a time through autoregressive prediction. Each decoder layer contains three main components: multi-head attention, a feed-forward network (FFN), and layer normalization.
Normalization Placement and Training Stability
Early Transformers used post-layer normalization (Post-LN), placing normalization after the residual connection. Training deep Post-LN networks can be unstable and requires carefully designed learning-rate warmup.
Modern decoder models commonly use pre-layer normalization (Pre-LN), often with RMSNorm, to improve training stability and support deeper networks. The outcome still depends on model depth and the training recipe, so architecture selection should account for both stability and final model quality.
Positional Encoding with RoPE
Rotary positional embeddings (RoPE) have largely replaced absolute positional embeddings in modern LLMs. RoPE encodes position through rotations applied to queries and keys, allowing attention to represent relative distances more naturally.
However, performance can still decline when inference sequences exceed the lengths used during training. Common remedies include positional interpolation and continued training on long-context data.
2. Attention Mechanisms: MHA, MQA, and GQA
In standard multi-head attention (MHA), each query head has its own key (K) and value (V) head. During inference, the model stores keys and values for previous tokens in a KV cache.
Multi-query attention (MQA) and grouped-query attention (GQA) reduce the number of KV heads to lower cache memory requirements.
| Method | Number of KV Heads | KV Cache Memory | Quality Impact | Typical Trade-off |
|---|---|---|---|---|
| MHA | Equal to the number of query heads | Highest | Baseline | Strong quality, but the greatest memory demand |
| MQA | 1 | Lowest | May reduce quality | Lowest memory use, with greater quality risk |
| GQA | G groups, where 1 < G < query head count | Approximately G / query head count of MHA | Close to MHA | A common balance between quality and memory use |
Attention Compute and KV Cache Scale Differently
Attention computation and cache memory do not grow at the same rate. For a sequence of length n, the attention computation in a single layer grows approximately as n².
KV cache memory, by contrast, grows linearly with batch size, sequence length, layer count, and KV head count:
KV cache size in bytes =2 × batch size× sequence length× number of layers× number of KV heads× head dimension× bytes per element
For example, consider a model with 32 layers, 8 KV heads, a head dimension of 128, and FP16 storage at 2 bytes per element. A single sequence of 4,096 tokens requires approximately 0.5 GiB of KV cache. Increasing the batch size to 16 raises that requirement to approximately 8 GiB.
