MoE and Hybrid Attention in Large Language Models: Architecture Selection and Resource Estimation

    This article explains how to evaluate a large language model release through its architecture, not just benchmark scores and prices. It covers the parameter structure of sparse Mixture-of-Experts (MoE) models, top-k routing and its engineering issues, the trade-offs between linear and sparse attention, KV cache growth, a method for estimating memory and cost, and a checklist for deployment decisions.

    1. Starting Point for Reading a Model Release

    When a large language model is released, the announcement usually focuses on two kinds of numbers: benchmark scores and prices. Both are useful, but reading them alone can lead to incomplete conclusions. Benchmark scores depend on the task distribution of the evaluation set, the prompt templates, and the scoring method, so the same model can rank very differently under different evaluations. Prices are comparable only when output length, cache hit rate, and calling pattern are taken into account.

    A more effective approach starts with the architecture. Which MoE design is used? How are linear attention and sparse attention traded off? How should resources be estimated? The answers to these questions are in the architecture. The architecture determines a model’s compute, memory footprint, and how it handles long text. These factors set the upper bounds on inference cost and latency.

    A release can be broken down along four dimensions:

    1. Parameter structure: total parameters, active parameters, number of experts, and routing strategy;
    2. Attention mechanism: computational complexity, how the KV cache grows, and long-context capability;
    3. Training and data: training stages, post-training methods, and the task types covered by the data;
    4. Practical metrics: latency, throughput, unit cost, stability, and cache hit rate.

    This article discusses parameter structure first, then attention mechanisms, and finally gives a resource estimation method and a production checklist. The training and data dimension is not covered here, because public information on it is usually limited and hard to translate into engineering implications.

    2. Fundamentals of Sparse Mixture of Experts

    2.1 Total and Active Parameters

    In a dense model, nearly all parameters take part in computing each generated token. A Mixture of Experts (MoE) model splits the feed-forward network into multiple expert sub-networks. A router selects a small subset of experts for each token. As a result, the model can have a very large total parameter count, while each token activates only a fraction of those parameters.

    Distinguishing the two matters, but each metric covers a different scope. Total parameters set the lower bound on weight memory. Actual inference memory also depends on the KV cache, activations, parallelism strategy, and communication buffers. Active parameters mainly determine the compute per token and directly affect decoding speed.

    The number of experts, the number of activated experts, and the routing strategy together determine the activation ratio. Suppose a layer has 64 experts and each token selects 6. Then 6/64, or about 9.4%, of that layer’s expert feed-forward parameters participate in computing a single token. This is an arithmetic example and does not represent any specific model. In practice, the actual ratio must also account for shared experts, attention layers, and embedding parameters.

    2.2 Top-k Routing

    The router is typically a linear layer. For each token, it computes scores for all experts, applies softmax, selects the k experts with the highest scores, and sums their outputs weighted by those scores. A larger k gives each token more computation, but it also increases compute and communication volume.

    Routing introduces three categories of engineering problems.

    The first is expert capacity and overflow. Systems usually set a capacity limit for each expert. Capacity is approximately the capacity factor multiplied by (tokens per batch × k ÷ number of experts). When an expert receives more tokens than its capacity, the excess tokens are either dropped or passed only through the residual connection without going through that expert. A smaller capacity factor makes memory and communication easier to control, but it raises the drop rate, which may reduce model quality.

    The second is load balancing. If routing sends most tokens to a few experts, those experts become bottlenecks while the others sit idle. During training, an auxiliary loss is often used to penalize unbalanced assignment. Some systems instead balance load by dynamically adjusting bias terms. In inference, load imbalance shows up as higher tail latency, because the completion time of a batch depends on the slowest expert.

    The third is routing noise. Adding a small amount of noise to routing scores during training gives more experts a chance to receive training signal and prevents routing from locking in too early.

    Differences between training and inference also need attention. In inference, token dropping is often disabled, or a larger capacity factor is used, so that outputs are complete. Routing results also depend on the other requests in the batch, so the same prompt may produce slightly different outputs under different batch compositions. With expert parallelism, tokens must be sent to the devices hosting their target experts through all-to-all communication, and communication latency rises as the number of devices increases.

    3. Hybrid Attention and Layered Attention Design

    3.1 Sources of Attention Cost

    Standard full attention has a prefill compute cost that grows quadratically with sequence length n, roughly O(n²·d), where d is the head dimension. During decoding, each generated token requires reading the KV cache of all previous tokens, so the memory access per step grows linearly with context length. In long-context scenarios, KV cache memory often becomes the bottleneck before compute does.

    The KV cache size can be estimated as follows:

    KV cache bytes ≈ 2 × number of layers × number of KV heads × head dimension × sequence length × batch size × bytes per element

    Suppose a model has 48 layers, 8 KV heads, and a head dimension of 128. With FP16 (2 bytes per element), a sequence length of 32,768, and a batch size of 1, the KV cache is about 6.0 GiB. This number only illustrates the order of magnitude and does not represent any real model. Grouped-query attention (GQA) and multi-query attention (MQA) reduce this term directly by lowering the number of KV heads.

    3.2 Linear Attention and Sparse Attention

    Linear attention approximates softmax with a kernel function and reorders the computation. It accumulates outer products of keys and values first, then multiplies the result by queries. Its per-layer state has a fixed size that does not depend on sequence length, so decoding memory does not grow with context. The trade-off is that exact retrieval is usually weaker than in full attention, and precise long-range matches may be lost.

    Sparse attention computes attention only between selected pairs of positions, for example through sliding windows, block selection, or content-based dynamic selection. It reduces compute, but if it still stores all KV entries, the memory savings are limited. Memory drops noticeably only when sparse attention is combined with compression, or when only the KV entries within the window are kept.

    3.3 Principles of Interleaved Layouts

    Hybrid architectures typically interleave a few full-attention or sparse-attention layers between multiple linear-attention layers. Linear layers handle most local and global information at low cost, while full-attention layers handle exact retrieval. Because only some layers must store KV entries that grow with length, the overall KV cache is noticeably smaller than in a model where all layers use full attention.

    The interleaving ratio is a design choice. More full-attention layers make exact retrieval more reliable, but they raise KV cache size and compute cost. Using only linear layers minimizes cost, but recall over long contexts may suffer. Engineering also has to consider operator maturity. Kernels for linear and sparse attention are complex to implement, and performance can vary widely across hardware and inference frameworks.

    Long-context capability should not be judged by the advertised context length alone. Teams should test recall rate and reasoning quality at the target length, using tasks such as needle-in-a-haystack retrieval and multi-hop question answering. They should also record latency and memory usage during the same tests.

    4. Resource Estimation and Production Metrics

    4.1 Memory Estimation

    Memory ≈ weights + KV cache + activations + communication buffers + framework overhead

    The weight term depends on precision and parameter count. The KV cache depends on sequence length, batch size, and layer structure. Activations depend on batch size and sequence length, and they are typically larger during prefill. Communication buffers cannot be ignored under expert parallelism.

    4.2 Architecture Comparison

    Dimension Dense model MoE Linear attention Sparse attention
    Compute complexity Grows linearly with parameter count Grows with active parameters, weakly related to total parameters Approximately linear in sequence length Depends on sparsity pattern, usually lower than full attention
    KV cache growth Grows with layers, heads, and