Efficient Large Model Architectures: Sparse MoE and Hybrid Attention

    This article explains how sparse Mixture-of-Experts (MoE), shallow network designs, hybrid attention, index compression, and native multimodal support can reduce inference costs for large language models. It focuses on the trade-offs between total and active parameters, long-context attention complexity, KV cache usage, routing efficiency, and the engineering considerations involved in deploying these architectures.

    1. Why Inference Cost Has Become a Bottleneck

    Large language models are now widely used for code generation, AI agents, and multimodal understanding. Increasing the parameter count often improves model capability, but it also raises memory consumption and computation requirements for every generated token. Once a standard dense model reaches tens of billions of parameters or more, reducing the cost of a single inference request becomes difficult.

    Developers typically face two major sources of pressure:

    1. High request volume. Code review in CI/CD pipelines, batch rewriting, log analysis, and similar tasks can generate a large number of requests every day. Even a small difference in per-request cost can significantly affect the monthly bill.
    2. Longer contexts. Analyzing code repositories and retrieving information from large documents may require hundreds of thousands or even millions of tokens. Standard full attention has quadratic complexity with respect to sequence length, which is the fundamental reason long-context inference is expensive.

    As a result, a major direction in recent model design has been to improve efficiency through architecture rather than simply increasing the parameter count. This article examines several of the main approaches: sparse MoE, shallow networks, hybrid attention, index compression, and native multimodal support.

    2. Sparse MoE: Large Total Parameter Count, Few Active Parameters

    2.1 Basic Principle

    A Mixture-of-Experts (MoE) model replaces the feed-forward network (FFN) layer with multiple expert networks. A router scores each token and selects only the top-k experts for computation. Their outputs are then combined using the corresponding weights.

    The following simplified pseudocode illustrates the basic logic:

    1. # Pseudocode: simplified MoE forward pass
    2. # Load balancing and capacity constraints are omitted
    3. def moe_forward(x, experts, router, top_k=8):
    4. scores = router(x) # Compute routing scores
    5. top_idx, top_w = topk(scores, top_k) # Select the top-k experts
    6. top_w = top_w / top_w.sum() # Normalize selected weights
    7. out = 0
    8. for i, w in zip(top_idx, top_w):
    9. out += w * experts[i](x) # Combine expert outputs
    10. return out

    A production implementation must also address load balancing. If most tokens are routed to only a few experts, some experts remain idle while others become overloaded. Training commonly uses an auxiliary loss and a capacity factor to limit the number of tokens each expert can receive. The system must also define how to handle tokens that exceed an expert’s capacity.

    2.2 Key Design Trade-offs

    The goal of an extremely sparse design is to maintain a very large total parameter count while using only a small portion of those parameters for each token. For example, if a model has approximately 1,000 billion total parameters and activates approximately 20 billion parameters per token, the active ratio is about 2%. The ratio depends on the total parameter count, active parameter count, top-k setting, and expert granularity. There is no universal value, so the specific model configuration should always be used as the reference.

    Common design choices include:

    • A large number of fine-grained experts. A single layer may contain hundreds of routed experts, while each token is sent to only a small number of them. Fine-grained partitioning can encourage specialization, but it also increases routing and communication overhead.
    • Dense layers at the bottom of the network. The first few layers may retain a dense structure to produce more stable low-level representations and avoid making immature routing decisions at an early stage.
    • Fewer layers with more complex residual connections. The network may use a relatively small number of layers and compensate with more sophisticated residual information mixing.

    From an engineering perspective, MoE separates storage cost from computation cost. All model parameters still need to be stored in GPU memory or system memory, creating substantial memory pressure. However, the computation required for each token is related only to the active parameters. This structure is better suited to centralized API services and high-throughput workloads. Edge devices often cannot accommodate the storage requirements of the complete model.

    3. Hybrid Attention: A Key to Long-Context Processing

    Sparse MoE primarily reduces the computation required by FFN layers. The quadratic complexity of attention remains. When the context reaches the million-token scale, both the computation required by full attention and the memory consumed by the KV cache become difficult to manage.

    3.1 Dividing Responsibilities Between Linear and Sparse Attention

    A common approach is to alternate between two types of attention:

    • Linear attention. Its computational complexity can be reduced to O(n), making it suitable for maintaining broad coverage of a long sequence at a lower cost. It typically compresses historical information into a fixed-size state. This compression introduces some information loss. For tasks that require accurately recalling a specific detail, its performance may be weaker than that of full attention.
    • Sparse attention. A sparse attention layer is inserted periodically and uses a selection mechanism to focus only on certain important positions. This enables more precise retrieval of important long-range information. For example, a design may use one sparse attention layer for every three linear attention layers, although the exact ratio depends on the model architecture.

    The underlying idea is that most layers maintain global information at low cost, while a smaller number of layers retrieve specific details when needed. Long-context modeling is therefore divided into two complementary tasks: coarse-grained global memory and fine-grained, on-demand retrieval.

    3.2 Index Compression and the KV Cache

    Sparse attention can also compress the keys used for retrieval. For example, keys may be pooled at a 4:1 ratio. The compressed index is first used to roughly filter candidate positions, after which precise attention computation is performed on the selected positions. This reduces the computation required during retrieval.

    It is important to distinguish what is being compressed. If only the index keys used for retrieval are compressed while the values and other KV components remain unchanged, the overall reduction in KV cache depends on how much of the cache is occupied by the index. For example, if the index accounts for 40% of the KV cache and is compressed to one-quarter of its original size, the total cache may be reduced to approximately 70% of the original size. If both keys and values are compressed at a 4:1 ratio, the overall cache size may approach one-quarter of the original. The actual savings must be determined by the specific implementation and measured results rather than applied as a fixed assumption.

    For teams operating their own inference services, the KV cache is often one of the largest sources of GPU memory usage during long-context inference. Reducing it can support more concurrent sessions or lower the cost of serving a model on each GPU.

    3.3 Compensating for a Shallower Network

    When the number of network layers is reduced, each layer must handle more representational work. Hyper-Connections are one class of method that expands the width of residual pathways. Manifold-Constrained Hyper-Connections (mHC) further applies constraints to information mixing in order to improve training stability.

    These methods remain research-oriented approaches. Their specific implementations and results should be evaluated against the original papers or official technical reports rather than treated as universally applicable best practices.

    Conceptually, they aim to use richer lateral information flow to compensate for the reduced network depth.