Sparse Activation and Hybrid Attention Architectures in Large Language Models

    This article explains why large language model architectures are moving toward sparsity, how Mixture-of-Experts models distinguish total parameters from active parameters, and how linear and sparse attention can reduce the computational and memory costs of long-context inference. It also provides practical formulas and code for estimating routing behavior and KV cache usage.

    1. Why Large Model Architectures Are Moving Toward Sparsity

    In recent years, improvements in model capability have relied heavily on larger parameter counts and more training data. However, as models grow, their computational and memory requirements during inference also increase. In production environments with high concurrency and long contexts, the compute consumed by each request directly affects service costs. As a result, architecture design is gradually shifting from simply “adding more parameters” to ensuring that each token uses only the computation it needs. This is the core idea behind sparsity.

    Sparsity is mainly applied at three levels:

    • Parameter level: Sparse Mixture-of-Experts architectures provide a very large total parameter count, while activating only a subset of parameters for each token.
    • Attention level: Linear or sparse attention replaces full attention to reduce the quadratic computational cost caused by increasing sequence length.
    • Cache level: The amount of intermediate state that must be stored is reduced, including the size of the key-value (KV) cache.

    2. Sparse Mixture-of-Experts: Total Parameters vs. Active Parameters

    Sparse Mixture-of-Experts (MoE) architectures divide the feed-forward network into multiple expert subnetworks. A router selects a small number of experts for each token, while the remaining experts stay inactive.

    The mechanism can be summarized in three steps:

    1. The router produces a score for every expert.
    2. It selects the top-k experts according to those scores.
    3. It computes a weighted sum of the outputs from the selected experts.

    Example code:

    1. import torch
    2. import torch.nn.functional as F
    3. def route_tokens(hidden, gate_weight, top_k=8):
    4. # hidden: [tokens, dim]; gate_weight: [experts, dim]
    5. logits = hidden @ gate_weight.T
    6. probs = F.softmax(logits, dim=-1)
    7. topk_probs, topk_idx = probs.topk(top_k, dim=-1)
    8. topk_probs = topk_probs / topk_probs.sum(dim=-1, keepdim=True)
    9. return topk_idx, topk_probs

    Two points are especially important.

    First, the total parameter count determines model capacity and storage requirements, while the compute required for a single inference is primarily determined by the active parameters.

    Second, routing can lead to load imbalance. Some experts may become overloaded while others remain inactive for long periods. In production systems, auxiliary losses or bias terms are commonly used to balance the workload. After deployment, teams should monitor the hit rate of each expert.

    3. Where Attention Costs Come From

    The computational cost of standard self-attention is proportional to the square of the sequence length, n. When the context grows from several thousand tokens to millions of tokens, this component becomes a primary bottleneck.

    Inference introduces another cost: the KV cache. For every generated token, the model must store key and value vectors for each layer. KV cache memory can be estimated with the following formula:

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

    The following is an example assumption rather than a measurement from any specific model: 48 layers, 8 KV heads, a head dimension of 128, BF16 with 2 bytes per element, and a sequence length of 1,000,000. Substituting these values gives:

    2 × 48 × 8 × 128 × 2 × 1,000,000 ≈ 196 GB

    This represents the cache for a single request and does not include model weights.

    Example estimation code:

    1. def kv_cache_bytes(layers, kv_heads, head_dim, seq_len,
    2. bytes_per_elem=2, full_attn_layer_ratio=1.0):
    3. # full_attn_layer_ratio is the proportion of layers retaining full KV cache
    4. effective_layers = int(layers * full_attn_layer_ratio)
    5. return 2 * effective_layers * kv_heads * head_dim * seq_len * bytes_per_elem

    If only one-quarter of the layers retain a full KV cache, the cache size in the example falls to approximately 49 GB. This shows that reducing the number of layers requiring full caching has a direct impact on the memory budget for long-context deployment.

    4. Dividing Responsibilities Between Linear and Sparse Attention

    Linear attention accumulates historical information through a recurrent state. Its computational complexity grows linearly with sequence length, while its state size remains fixed, resulting in lower cache overhead. Its limitation is that historical information is compressed into a fixed-size state, which makes precise retrieval of distant information more difficult.

    Sparse attention calculates attention only over a small number of relevant positions for each query. It preserves the ability to access historical information precisely, but requires an indexing process to determine which positions to select. The index itself also introduces computational and storage costs.

    The two mechanisms are often combined in an interleaved architecture. Most layers use linear attention to process continuous context, while sparse attention is inserted every few layers to support precise retrieval. There is no universally optimal interleaving ratio. It must be determined based on the target task’s long-context retrieval performance.

    Index compression is another practical direction.