Model Architecture Trade-offs: From MoE to KV Cache

    This article explains the key architectural decisions behind large language models, focusing on how hardware constraints—compute, memory bandwidth, and communication—shape choices such as mixture-of-experts (MoE), expert parallelism, and KV cache compression. It covers the asymmetric growth of total vs. active parameters, routing and communication costs, and practical methods for reducing KV cache memory in long-context inference.

    The Transformer architecture was introduced in 2017. Since then, large language model (LLM) design has evolved from simply scaling up parameters to more refined structural choices. At first glance, changes across different models seem scattered, but most revolve around a small set of hardware constraints: compute, memory access, and communication. Understanding these three constraints clarifies the reasoning behind many architectural trade-offs.

    1. Why Total Parameters and Active Parameters Grow Asymmetrically

    In recent years, total parameter counts of large models have grown from tens of billions to trillions. In contrast, the number of active parameters actually used during a single inference pass generally remains in the range of a few billion to tens of billions. The gap between these two figures is largely due to the widespread adoption of mixture-of-experts (MoE).

    Traditional dense models activate all parameters for every token, so both compute and memory access costs scale directly with parameter count. MoE replaces the feed-forward network (FFN) with a set of parallel expert networks. A router selects only a few experts for each token, significantly reducing the per-token compute compared to what the total parameter count would imply.

    This design has two direct consequences:

    1. Total parameters determine the model’s knowledge capacity. More experts allow the model to store more domain-specific patterns and knowledge.
    2. Active parameters determine the computational cost per token. Since only a few experts are activated, per-token compute remains manageable.

    For illustration (numbers are hypothetical): a model with 1000B total parameters may activate only about 50B parameters per token. Its compute cost is similar to that of a 50B dense model, but its storage requirement is close to that of a 1000B model. This asymmetry shows that MoE constraints often first appear in GPU memory capacity rather than compute.

    MoE therefore places significant stress on memory bandwidth. Experts are distributed across devices, and all expert weights must reside somewhere. Common approaches include:

    • Sharding experts across multiple GPUs or multiple machines;
    • Using weight quantization to reduce bytes per parameter;
    • Hierarchical loading or CPU offloading, where less frequently used weights are placed in slower storage.

    Hierarchical loading and CPU offloading introduce additional memory access latency, making them more suitable for latency-insensitive offline tasks or throughput-oriented scenarios. Online services should measure latency first before adopting these strategies.

    2. Routing and Communication Overhead from Increasing Expert Counts

    Early MoE models typically used a few to dozens of experts. Recent models have grown the number of routed experts to hundreds. The exact count varies by model version and design—such as total experts, experts activated per token, and whether shared experts are included—so readers should refer to the respective technical reports. This article does not cite specific numbers.

    As the number of experts increases, routing granularity also becomes finer. Fine-grained experts help separate representations across domains, reducing interference between different types of knowledge. However, finer granularity means each expert handles fewer tokens, shrinking the matrix multiplication size and potentially lowering GPU utilization. This is a trade-off that requires empirical validation.

    Communication overhead is a more direct cost. In an MoE layer, each token must first be sent to the device hosting its selected experts (dispatch), and after computation, results are gathered and combined (combine). Both steps typically rely on all-to-all communication. In multi-node environments, the bandwidth and latency of this communication often become the throughput bottleneck.

    To balance compute and communication, modern systems typically combine several parallel strategies:

    • Expert parallelism (EP): distributing different experts across different devices;
    • Data parallelism (DP): replicating the model and splitting batches of data;
    • Tensor parallelism (TP): splitting individual matrix operations, useful for large matrices within a layer.

    More parallel strategies are not always better. Each additional layer of parallelism adds synchronization or communication. The right combination depends on cluster interconnect topology, model structure, and batch size.

    3. Long Context and KV Cache Compression

    Context windows have expanded from tens of thousands of tokens to 128K, and some models support 1M tokens or more. Long-document reading and codebase analysis depend on long context, but this also creates memory pressure.

    During autoregressive generation, the model must cache the keys (K) and values (V) of history tokens, forming the KV cache. Its size can be estimated as:

    KV cache bytes ≈ 2 × layers × KV heads × head dimension × context length × bytes per element

    The factor of 2 accounts for both K and V tensors. Doubling the context length doubles the KV cache size. Concurrent requests multiply this further. Therefore, memory estimation should use the maximum context length and maximum concurrency, not averages.

    Common compression and optimization methods fall into several categories.

    1. Architecture-level KV head sharing. Grouped query attention (GQA) and multi-query attention (MQA) let multiple query heads share fewer K and V heads, reducing KV cache size from the model design stage. These methods must be chosen during training; they cannot be applied to an already-trained model.
    2. Quantization. Quantizing the KV cache from FP16 or BF16 to INT8 or even INT4 can significantly reduce memory usage. Precision impact depends on quantization granularity, calibration data, and the specific task. Some accuracy loss is possible, so it is essential to compare quantized and unquantized results on your own evaluation sets.
    3. Paged management. PagedAttention borrows the idea of virtual memory from operating systems, splitting the KV cache into fixed-size blocks that can be allocated on demand. It mainly solves memory fragmentation and improves utilization, but it does not reduce the KV cache size per token.
    4. Sparse or local attention. Methods such as sliding windows restrict each token to attend to only a subset of previous tokens, reducing compute and cache. These methods change the model’s attention behavior and must be adapted during training or fine-tuning, with validation on long-range dependency tasks.

    One common misconception deserves clarification. Activation recomputation is a training-phase technique. It avoids saving some intermediate activations during forward propagation and recomputes them during backpropagation, trading time for training memory. It is not a KV cache compression strategy for inference and does not reduce KV cache usage during generation.

    4. Hardware Constraints and Corresponding Architectural Choices

    Hardware Constraint Typical Problem Common Architecture / Strategy Choice Main Cost
    Compute Total pre-training compute remains very large MoE, sparse attention, lower-precision training May increase communication or reduce accuracy
    Memory bandwidth KV cache and model weights dominate bandwidth GQA/MQA, KV cache quantization, CPU offloading Added latency or slight accuracy loss
    Memory capacity GPU memory limits model size and context length Expert parallelism, tensor parallelism, paged attention Extra communication or complexity
    Communication All-to-all and synchronization overhead Balanced parallelism, asynchronous dispatch Increased implementation complexity