Large Language Model Architecture: Scale, Attention, and Hardware Constraints

    Transformer-based language models are evolving to support larger parameter counts and longer contexts within hardware limits. This article explains how compute, memory bandwidth, and interconnect bandwidth shape architectural choices, including sparse activation, KV cache compression, long-context training, and mixture-of-experts routing. It also shows how to estimate per-token computation and KV cache memory for deployment planning.

    1. Why Architecture Matters for Large Language Models

    Over the past decade, Transformer-based language models have undergone continuous architectural changes. These changes share a common goal: enabling the same hardware to train and serve larger models while handling longer contexts.

    Viewed individually, these changes may seem unrelated. From a hardware perspective, however, they address three constraints:

    • Compute capacity determines speed during training and prefill.
    • Memory bandwidth determines speed during token-by-token decoding.
    • Interconnect bandwidth determines how efficiently multiple accelerators and nodes work together.

    Understanding these constraints helps explain whether an architectural choice is intended to reduce computation, save accelerator memory, or limit communication between devices. It also provides a basis for estimating how a model will perform on particular hardware.

    2. Model Scale: Total Parameters vs. Active Parameters

    Model scale is commonly described using two metrics. Total parameter count determines model capacity and the memory required to store its weights. Active parameter count determines how much computation is actually performed for each token.

    Separating these metrics allows a model to increase its capacity without a proportional increase in per-token computation.

    For a dense model, a forward pass requires roughly 2N floating-point operations per token, where N is the number of parameters participating in the computation. For a sparsely activated model, N is replaced by the active parameter count. Per-token computation therefore grows with active parameters, while the cost of storing the model still grows with total parameters.

    The following example uses hypothetical values to illustrate this relationship:

    1. def flops_per_token(active_params: float) -> float:
    2. # Each active parameter corresponds to roughly one multiply and one add
    3. return 2 * active_params
    4. dense = flops_per_token(70e9) # Dense model: all 70 billion parameters are active
    5. sparse = flops_per_token(20e9) # Sparse model: more total parameters, but only 20 billion active per token
    6. print(f"Dense: {dense:.2e} FLOPs/token, Sparse: {sparse:.2e} FLOPs/token")

    All parameters in a sparse model must still reside in accelerator memory or another storage tier. Deployment planning must therefore account for both per-token computation and storage capacity.

    3. Attention and the KV Cache: Major Costs of Long Contexts

    During autoregressive generation, each new token attends to the keys and values of all previous tokens. To avoid recomputing those historical keys and values, the inference system stores each layer’s K and V tensors in accelerator memory. This stored data is called the KV cache.

    As the context grows, so does the KV cache. Each generated token also requires reading more cached data.

    The KV cache size per token can be estimated as:

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

    The factor of 2 accounts for separate K and V tensors. The following code estimates the memory needed for a 128K-token context using a hypothetical configuration:

    1. def kv_cache_bytes(layers, kv_heads, head_dim, bytes_per_elem, seq_len, batch=1):
    2. per_token = 2 * layers * kv_heads * head_dim * bytes_per_elem # Separate K and V tensors
    3. return per_token * seq_len * batch
    4. # Hypothetical configuration: 80 layers, 8 KV heads, head dimension 128,
    5. # BF16 (2 bytes), a 128K-token context, and one request
    6. size = kv_cache_bytes(80, 8, 128, 2, 128 * 1024)
    7. print(f"{size / 2**30:.1f} GiB") # Outputs 40.0 GiB

    These values illustrate the scale of the memory requirement; they do not represent the published specifications of any particular model.

    In this example, a single 128K-token request requires approximately 40 GiB of KV cache memory. Eight concurrent requests require approximately 320 GiB. At that point, the KV cache can exceed the memory occupied by the weights of a 70B-class model. Context length and request concurrency are therefore critical variables in memory planning.

    Common Approaches to Reducing KV Cache Size

    1. Grouped-Query Attention (GQA)
      Multiple query heads share the same group of keys and values, reducing the number of KV heads to a fraction of the original count. This directly reduces cache size with little additional computation.

    2. Low-rank compression
      Keys and values are projected into a lower-dimensional latent space for storage. They are then reconstructed during computation, or attention is computed directly in the latent space. The trade-off is an additional projection operation.

    3. A mix of local and global attention
      Some layers use sliding-window attention and cache only the keys and values within the most recent window. With fewer global-attention layers, the total cache size falls substantially. This approach requires evaluating how effectively the model uses information outside the window.

    4. KV cache quantization
      Reducing cache precision from BF16 to FP8 or INT8 cuts its size by half, or close to half. Quantization introduces precision loss, so it should be evaluated separately on long-context tasks.

    Long Contexts Also Require Training Support

    Long-context capability depends on the training process, not just cache management. A common approach is to pretrain on shorter sequences and then gradually increase sequence length, while adjusting the base frequency of positional encoding or using methods such as position interpolation to extend the supported context.

    Simply enlarging the context window without corresponding training typically leads to a substantial decline in the model’s ability to retrieve information across long distances.

    4. Mixture of Experts: Sparse Activation and Routing Granularity

    A Mixture of Experts (MoE) architecture divides the feed-forward network into multiple experts. For each token, a router selects a small subset of those experts to participate in the computation.