LLM Architecture and Parameter Scale: A Practical Guide to Estimation and Deployment

    This article explains how the architecture and parameter scale of modern large language models affect inference cost and memory use. It covers the common decoder-only Transformer stack, attention variants such as grouped-query attention and KV-cache compression, dense versus mixture-of-experts models, parameter-count estimation from configuration, and formulas for weight and KV-cache memory. It also offers practical guidance for model selection, capacity planning, and cost evaluation.

    1. Why Start with Architecture and Parameter Scale

    When selecting or deploying a large language model, many questions depend on the model’s architecture and parameter scale. Parameter scale determines model capacity and memory footprint, while architecture determines the computation required per token during inference and how quickly the cache grows in long-context scenarios. Two models with the same parameter count can differ several times in actual inference cost.

    Understanding model architecture therefore requires attention to three things at once: how much knowledge the model can store, how many parameters are actually used in each computation, and how much memory is needed when processing long text.

    2. The Common Skeleton of Mainstream Architectures

    Most current large language models are based on decoder-only Transformer variants. A typical model is built by stacking the following components:

    1. Token embedding layer: maps discrete tokens to high-dimensional vectors, with the dimension usually denoted as d.
    2. Repeated decoder layers: each layer contains a self-attention module and a feed-forward network module, both with residual connections and normalization.
    3. Position encoding: the current mainstream approach is rotary position embedding (RoPE), which injects position information into query and key vectors and makes it easier to extrapolate to longer contexts.
    4. Output layer: maps the final hidden state back to a score vector of vocabulary size and usually shares weights with the token embedding layer.

    For normalization, the industry often uses root mean square normalization (RMSNorm), which omits the mean calculation and requires less computation. Feed-forward networks generally use a gated activation structure such as SwiGLU, which uses three projection matrices instead of the traditional two. It has stronger expressiveness but also increases the parameter count accordingly.

    3. Evolution of Attention Mechanisms

    Attention is the core source of inference cost. Standard multi-head attention maintains independent keys and values for each head, and inference requires caching all historical key-value pairs, known as the KV cache.

    To reduce cache pressure, several common optimizations are used:

    • Grouped-query attention (GQA): multiple query heads share the same set of key-value heads. The number of KV heads is reduced from being equal to the number of query heads to a fraction of that number, and cache size falls proportionally, usually with small accuracy loss.
    • KV low-rank compression: keys and values are projected into a low-dimensional latent space for storage and restored when used. It trades a small amount of extra computation for a smaller cache footprint.
    • Sliding-window and sparse attention: each token attends only to a local window or selected key positions, changing computation from quadratic growth with sequence length to approximately linear growth.
    • Linear attention and hybrid structures: some layers use linear-complexity attention and alternate with standard attention layers, balancing quality and speed in long-text scenarios.

    The common goal of these methods is to reduce memory and computation overhead per inference, rather than simply increasing parameters.

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

    According to how the feed-forward network is organized, models can be divided into two types.

    In a dense model, every token passes through the same set of feed-forward parameters, and the total parameter count is the number of parameters used in each computation. The structure is simple, and training and inference behavior are easy to predict.

    A mixture-of-experts (MoE) model splits the feed-forward network into multiple experts, and a router network selects a small number of experts for each token to participate in computation. It has two key metrics:

    • Total parameter count: the sum of all experts and shared parts, which determines the model storage footprint.
    • Activated parameter count: the parameters actually involved in computation for a single token, which determines inference compute demand.

    The router network needs to work with a load-balancing mechanism to prevent a few experts from being overused. The following schematic illustrates the difference between the two (the numbers are hypothetical and are used only to explain the calculation method):

    • Suppose the total parameter count is 100B and 13B are activated each time, giving an activation ratio of about 13%.
    • Storage still requires loading all 100B parameters, so memory demand is close to that of a dense 100B model.
    • Single-step computation is close to that of a 13B dense model.

    Therefore, when evaluating an MoE model, you should look at both storage requirements and activation scale, not just one of the numbers.

    5. How to Calculate Parameter Count

    The most direct way to understand parameter scale is to estimate the parameter count from configuration items. Take a dense model with standard multi-head attention and a gated feed-forward network as an example:

    • Attention module: four projection matrices for Q, K, V, and O, each about d × d, for a total of about 4d².
    • Feed-forward module: three projection matrices, each about d × d_ff, for a total of about 3 × d × d_ff.
    • Parameters per layer are about 4d² + 3 × d × d_ff, multiplied by the number of layers L.
    • Token embedding parameters are vocabulary size × d; if shared with the output layer, they are counted only once.

    Below is a directly runnable estimation function that takes model configuration as input and outputs the parameter count. It does not depend on any specific framework and uses only basic arithmetic.

    1. def estimate_dense_params(vocab_size: int, d_model: int, n_layers: int,
    2. d_ff: int, tied_embedding: bool = True) -> int:
    3. """Estimate the parameter count of a dense decoder model with gated FFN (ignoring small terms such as normalization)."""
    4. attn_per_layer = 4 * d_model * d_model # Q, K, V, O projections
    5. ffn_per_layer = 3 * d_model * d_ff # Three matrices in gated FFN
    6. layer_total = n_layers * (attn_per_layer + ffn_per_layer)
    7. embed = vocab_size * d_model
    8. head = 0 if tied_embedding else vocab_size * d_model
    9. return layer_total + embed + head
    10. if __name__ == "__main__":
    11. # Example config: 32 layers, hidden dimension 4096, FFN dimension 11008, vocab 32000
    12. total = estimate_dense_params(32000, 4096, 32, 11008)
    13. print(f"Estimated parameter count: {total / 1e9:.2f} B") # about 6.61 B

    If grouped-query attention is used, the K and V projection parameters must also be reduced according to the number of KV heads, and query and KV dimensions need to be handled separately in the calculation.

    6. Estimating Memory and Cache

    Parameter scale directly determines the memory occupied by weights. At half precision (2 bytes per parameter), weight memory is about parameter count × 2 bytes. The example model above needs about 13 GB to store weights, not including activations and runtime overhead.

    KV cache memory is proportional to sequence length. The formula is:

    Bytes of cache per token = 2 × layers × KV heads × head dimension × bytes per element

    Using 32 layers, head dimension 128, and half precision as an example:

    • With all 32 KV heads (standard multi-head attention): about 0.5 MB per token, and a single sequence with 32K context takes about 16 GB.
    • With 8 KV heads (grouped-query attention): about 128 KB per token, and a single sequence with 32K context takes about 4 GB.

    The two differ by a factor of four, showing that architecture design has a large impact on deployment cost for long contexts. With batch concurrency, cache memory also accumulates linearly with the number of requests, so actual capacity planning must consider weights, cache, and concurrency together.

    7. How to Understand “How Large” a Parameter Scale Is

    Common model scales in the industry can be roughly divided into several ranges: small models usually have hundreds of millions to billions of parameters and are suitable for edge devices and low-cost scenarios; medium models have tens of billions to hundreds of billions of parameters and are often used for general services and code assistance; large models reach hundreds of billions of parameters and require multi-GPU parallel inference; models using a mixture-of-experts structure can reach trillions of total parameters, but activated parameters are often only tens of billions to hundreds of billions.

    Note that parameter scale is only one dimension. Training data quality, post-training strategy, context length, and inference optimization also significantly affect final results. Models of the same scale can vary widely in quality.

    8. Practical Recommendations for Selection and Evaluation

    1. Define constraints first, then look at metrics: clarify the memory limit, latency target, and context length requirements, then screen candidate models.
    2. Look at total and activated parameters together: storage cost depends on the total, while compute cost depends on the activated amount.
    3. Estimate long-context cache overhead: use the formula above to calculate cache usage for a single sequence and under concurrency.
    4. Evaluate on your own data: public metrics may not match a specific business well; use real task samples for small-scale validation.
    5. Record configurations and versions: any change in model architecture, precision, or inference parameters can change cost and results, so these should be included in version management.

    Understanding architecture and parameter scale essentially turns “how capable a model is” into “what can be done under a given set of resources.” With the estimation methods above, readers can make more informed judgments in model selection, capacity planning, and cost evaluation.