Modern LLM Architecture: Attention, Experts, and Compute Estimation

    This article presents a unified view of modern large language model architecture, focusing on the trade-offs among compute, memory access, and communication. It reviews the divergence between total and active parameters, context-length growth and KV cache pressure, and finer-grained expert routing. It then provides a practical framework for estimating training and inference compute, analyzing attention and KV cache designs, understanding MoE routing and load balancing, and checking FP8 mixed-precision training stability. The goal is to support model selection and capacity planning without requiring readers to memorize every model's specifications.

    1. Why Analyze Architecture as a Whole

    A decade after the Transformer was introduced, most surveys list its modifications chronologically. If we compare structural differences across models, however, we find that although these changes are scattered, at the hardware level they almost all point to the same set of constraints: compute, memory access, and communication.

    Model architecture evolution can be viewed as a search for engineering optima under given hardware constraints. Understanding this explains why different teams make similar technical choices. For model selection or compute planning, this perspective can be used directly to estimate GPU memory, bandwidth, and cluster scale, rather than memorizing every technical detail from each team.

    2. Three Evolution Themes Worth Tracking

    1. Divergence between total and active parameters. Take the DeepSeek-V3 technical report as an example: the model has about 671B total parameters and activates about 37B per token. Total parameters determine how much knowledge the model needs to store, while active parameters determine how much computation each inference requires. The sparse approach uses a large parameter pool to store knowledge and a smaller active path to control inference cost.

    2. The tension between context length and KV cache. Context windows are expanding from the 128K scale toward the million-token scale. KV cache memory grows linearly with context length, so long-context inference quickly becomes limited by GPU memory. As a result, multi-head latent compression, shared prefixes, paged management, and similar methods are widely adopted. For specific specifications, refer to each model’s official technical report.

    3. More experts and finer routing granularity. Again taking DeepSeek-V3 as an example, it contains 256 routed experts and 1 shared expert, and each token activates 8 of the routed experts. Individual experts are made smaller, and routing granularity is finer. The purpose is to improve parameter-use flexibility and reduce per-token memory access while keeping total capacity similar.

    3. A General Framework for Architecture Analysis

    A modern large model can be analyzed by expanding the following four stages.

    3.1 Scale and Compute Estimation

    First confirm the basic configuration: total parameters (N{total}), active parameters (N{active}), number of layers, hidden dimension, number of attention heads, training tokens (D), and inference-generated tokens (T).

    Training compute for a Dense model can be approximated as:

    [
    C_{train} \approx 6 \times N \times D
    ]

    where (N) is the total number of parameters. A MoE model passes only the activated part of its parameters through each token, so training compute is mainly determined by active parameters:

    [
    C{train} \approx 6 \times N{active} \times D
    ]

    This approximation does not include routing computation, communication between experts, or attention overhead that grows with context; actual runtime is usually longer. Total parameter count mainly affects memory capacity, weight storage, and checkpoint size, and does not directly increase per-token computation proportionally.

    Inference compute can be approximated as:

    [
    C{infer} \approx 2 \times N{active} \times T
    ]

    For example, a 70B Dense model trained on 2T tokens requires approximately (6 \times 70 \times 10^9 \times 2 \times 10^{12} \approx 8.4 \times 10^{23}) FLOPs. If replaced by a MoE model with 671B total parameters and 37B active parameters trained on 14.8T tokens, it requires approximately (6 \times 37 \times 10^9 \times 14.8 \times 10^{12} \approx 3.3 \times 10^{24}) FLOPs. Both are lower-bound estimates; actual requirements should be corrected according to real utilization and communication overhead.

    3.2 Attention Mechanisms and KV Cache

    Standard multi-head attention has significant memory-access pressure in long-context scenarios. There are three common directions for improvement.

    • Reduce KV heads: Grouped-query attention (GQA) lets multiple query heads share the same set of K and V, reducing cache size proportionally.
    • Compress KV dimensions: Multi-head latent attention (MLA) first compresses the hidden state into a shared low-dimensional latent vector; at inference time, only this latent vector and a small RoPE positional component are cached.
    • Sparse or local attention: apply global attention only to selected key positions and local windows to the rest, reducing computational complexity.

    The core idea of MLA can be expressed with the following pseudocode:

    1. c_kv = W_DKV @ h # Generate shared latent from hidden state h; cache only c_kv (dimension d_c)
    2. k_c = W_UK @ c_kv # Up-project to content key at compute time
    3. v = W_UV @ c_kv # Up-project to value at compute time
    4. k_r = rope(W_KR @ h) # Compute and cache positional component separately (dimension d_r)
    5. k = concat(k_c, k_r) # Concatenate for attention computation

    Cache size is calculated per layer and per token. MLA needs to cache (dc + d_r) elements per layer per token, while GQA needs to cache (2 \times n{kv} \times d_{head}) elements. Take the following assumed configuration: 60 layers, BF16 (2 bytes per element), and a 128K context.

    • GQA configuration: (n{kv} = 8), (d{head} = 128). Per token per layer: (2 \times 8 \times 128 \times 2 = 4096) bytes; all layers are about 240 KB; a single 128K sequence requires about 30 GiB.
    • MLA configuration: (d_c = 512), (d_r = 64). Per token per layer: ((512 + 64) \times 2 = 1152) bytes; all layers are about 68 KB; a single 128K sequence requires about 8.4 GiB.

    These two configurations only illustrate the calculation method and do not represent any specific model. When selecting a model, substitute the real configuration.

    For position encoding, interpolation and extrapolation of rotary position encoding (RoPE) can reduce the need to retrain from scratch, but extrapolation quality depends on the interpolation method, the amount of continued pretraining data, and evaluation results. It does not guarantee that no training is needed.

    3.3 Mixture-of-Experts Routing

    A modern MoE layer usually replaces the original FFN with several experts plus a router. Key design points include the following.

    • Fine-grained experts: more experts, each smaller. Each token activates more experts, but computation per expert is controlled.
    • Shared experts: some experts are activated for all tokens, handle general knowledge, and reduce redundant learning in other experts.
    • Load balancing: routing can easily concentrate on a few experts, leaving some cards idle and overloading others. Common approaches add an auxiliary loss or use dynamic bias without auxiliary loss.

    The routing process can be written as follows:

    1. scores = softmax(router(x)) # Score for each expert, shape [num_experts]
    2. topk_idx = topk(scores, k) # Select top-k experts
    3. g = scores[topk_idx] / sum(scores[topk_idx]) # Normalize selected experts to obtain weights
    4. y = sum(g[j] * expert[i](x) for j, i in enumerate(topk_idx)) # Weighted aggregation

    A common form of auxiliary loss is (L_{aux} = \alpha \times \sum_i (f_i \times P_i)). Here (f_i) is the proportion of tokens in the current batch assigned to expert (i), (P_i) is the average probability the router assigns to expert (i), and (\alpha) is a small coefficient. DeepSeek-V3’s auxiliary-loss-free scheme adds a dynamic bias to each expert; the bias only affects top-k selection and does not change the weighting coefficients themselves.

    3.4 Numeric Formats and Training Stability

    The move from BF16 to FP8 mixed precision in training is an important change in recent years. FP8 can improve throughput and reduce memory usage, but it is sensitive on some computation paths. A common practice is block scaling—for example, 1×128 blocks for activations and 128×128 blocks for weights, with an independent scaling factor per block. Router scores, normalization, Softmax, and the output head are usually kept at higher precision. Gradient accumulation and optimizer states generally use FP32.

    During training, use the following checklist to troubleshoot issues.

    • Are router scores, normalization, and Softmax computed in FP32?
    • Is scaling done per block rather than using one scale for the entire tensor?
    • Are gradient accumulation and optimizer states in FP32?
    • Do loss spikes, NaN, or Inf occur during training? Such phenomena often indicate that activations in some layer exceed the representable range of FP8.
    • Do small gradients underflow to zero in large numbers, causing some parameters not to update for a long time?

    The table below summarizes common choices and their trade-offs.

    Choice Suitable scenarios Main benefit Main cost
    Dense model Scenarios requiring simple implementation and mature deployment pipelines Fixed computation path per token; easier debugging Parameters and computation are coupled