Modern Large Language Models: Architecture Evolution and Parameter Scaling Explained

    This article explains how to understand large language models by first examining their architectural structure, then breaking down where parameters live in the model, and finally comparing dense versus mixture-of-experts designs. It covers the decoder-only Transformer backbone, attention mechanism variants and their memory costs, KV cache optimization techniques like GQA and sliding windows, and the trade-offs between total parameters and active parameters in MoE models.

    1. Understand the Architecture Before Talking About Parameters

    Discussing parameter counts without considering architecture is often misleading. Two models with the same number of parameters can have very different inference costs if one is dense and the other uses sparse activation. This article starts by outlining the common skeleton of mainstream models, then explains where parameters are distributed within that structure, and finally gives guidance on size estimation and model selection.

    2. The Common Skeleton: Decoder-Only Transformer

    Most current large language models for text generation use a decoder-only Transformer architecture. The model is composed of a stack of identical layers, each typically containing two sub-modules: a self-attention module and a feed-forward network module. Input text is first converted into vectors through an embedding layer, passed through the layers, and finally mapped to a probability distribution over the vocabulary by the output layer, which generates tokens one by one.

    Common engineering details in each layer include:

    • Normalization: Most models use RMSNorm, which omits mean computation and is cheaper than traditional LayerNorm.
    • Activation function: The feed-forward network commonly uses gated activations like SwiGLU. With similar parameter counts, it usually performs better than ReLU.
    • Positional encoding: Rotary Position Embedding (RoPE) is the mainstream choice. It encodes position information as vector rotations, which helps model relative positions and provides flexibility for extending context length.

    A simplified per-layer computation flow looks like this:

    1. for layer in range(L):
    2. x = x + Attention(RMSNorm(x)) # attention sublayer with residual connection
    3. x = x + FFN(RMSNorm(x)) # feed-forward sublayer with residual connection

    With this skeleton in mind, the main sources of parameters become clear: attention projection matrices and feed-forward network weights account for the vast majority, while the embedding and output layers take up the rest.

    3. Attention Mechanism Evolution and Memory Cost

    Standard multi-head attention keeps separate keys (K) and values (V) for each attention head. During inference, these caches grow linearly with context length and are the dominant memory overhead in long-context scenarios. The KV cache size can be estimated with the following formula:

    KV cache bytes = 2 × layers × KV heads × head dimension × sequence length × bytes per element

    The factor 2 accounts for storing both K and V. Example code:

    1. def kv_cache_bytes(layers, kv_heads, head_dim, seq_len, bytes_per_elem=2):
    2. # factor 2 stores both K and V; bytes_per_elem=2 for BF16 precision
    3. return 2 * layers * kv_heads * head_dim * seq_len * bytes_per_elem
    4. # Example: 32 layers, 8 KV heads, head dim 128, 8192 tokens
    5. size = kv_cache_bytes(32, 8, 128, 8192)
    6. print(size / 1024**3, "GiB") # outputs 1.0 GiB

    To address this memory pressure, several common optimization strategies have emerged.

    Grouped Query Attention (GQA): Multiple query heads share the same set of key/value heads. For instance, 32 query heads can share 8 KV heads, reducing the KV cache by a factor of four with relatively modest structural changes.

    Low-rank compressed cache: Key and value projections are first transformed into a low-dimensional latent space for caching, then restored during computation. This further reduces cache size but adds extra computation—essentially trading compute for memory.

    Sliding window attention: Each token only attends to a fixed-length window of recent tokens, so the cache length is bounded. Its limitation is that distant information cannot be accessed directly, so it is usually interleaved with global attention layers or combined with other mechanisms to preserve long-range dependencies.

    4. Dense Models vs. Mixture-of-Experts Models

    Dense models use all parameters in every forward pass. They are simple in structure and have mature training and deployment pipelines. However, more parameters mean more computation per token.

    Mixture-of-Experts (MoE) models introduce a router. The router selects a small number of expert networks for each token, leaving other experts idle. To understand MoE, it is important to distinguish two concepts:

    • Total parameters: The sum of all expert and shared parameters, which determines the model’s storage footprint.
    • Active parameters: The parameters actually involved in computing a single token, which determines the computation cost per inference step.

    As a hypothetical example: a model might have 100 billion total parameters but activate only 12 billion per token. Its per-step compute is roughly similar to a 12-billion-parameter dense model, but storage and memory requirements are closer to the 100-billion-parameter model. This structure suits scenarios where compute is limited