Sparse Large Language Models: Technical Evolution and Engineering Practices
This article explains why sparsification has become a core approach for scaling large language models, focusing on how it decouples total capacity from per-token computation. It covers Mixture-of-Experts (MoE) design choices such as expert granularity, shared experts, routing strategies, and capacity factors, along with the distributed communication challenges of expert dispatch. It then describes Multi-head Latent Attention (MLA) for compressing the KV cache, pretraining data and supervised fine-tuning practices, a layered evaluation framework, and inference deployment strategies for cost control. The article also outlines key considerations for running sparse models on domestic AI processors and common pitfalls to avoid.
1. Why Sparsification Has Become a Key Direction for Large Models
Progress in large models has long relied on scaling up parameter counts. However, in a dense architecture every token passes through all parameters, so compute and memory costs grow linearly or even faster as the model scales. The core idea of sparsification is to decouple “total capacity” from “per-token computation”: the model holds a very large parameter pool, but each forward pass activates only a small portion of it. This provides stronger knowledge capacity at the model level while keeping compute costs manageable.
A common misconception needs clarification: sparse does not mean “eliminating computation.” Total parameters still occupy storage and host memory, and expert routing, communication, and scheduling introduce new system complexity. What sparsification actually optimizes is the floating-point operations and memory access pressure per token, at the cost of greater engineering complexity.
2. Mixture of Experts: Decoupling Capacity from Activation
A Mixture of Experts (MoE) architecture typically stacks attention sublayers and expert sublayers alternately. Attention handles information exchange between tokens, while the expert layer consists of multiple parallel feed-forward networks, with a lightweight routing network selecting a few experts for each token.
Key design points include:
- Expert granularity: Fine-grained experts are more numerous and individually smaller, and usually achieve more stable routing than a small number of giant experts.
- Shared experts: Keeping a few always-active experts to carry general knowledge, reducing the routing burden.
- Routing strategy: Top-K selection combined with a load-balancing loss prevents a few experts from being overused while others are “starved.”
- Capacity factor: Controls the upper limit of tokens each expert can receive; excess tokens are dropped or bypassed, directly affecting training stability.
The routing logic can be abstracted schematically as:
# Pseudocode: illustrates the routing and weighted aggregation flow onlylogits = router(hidden_states) # [tokens, num_experts]topk_val, topk_idx = topk(logits, k=2) # select 2 expertsweights = softmax(topk_val, dim=-1)out = zeros_like(hidden_states)for slot in range(k):idx = topk_idx[:, slot]expert_out = dispatch_and_compute(experts, hidden_states, idx)out += weights[:, slot:slot+1] * expert_out
The real engineering difficulty lies in dispatch_and_compute: in distributed training, different experts reside on different devices, requiring two collective communications (dispatch and gather). Communication volume, synchronization waits, and long-tail experts all reduce utilization.
3. Multi-head Latent Attention: Compressing the KV Cache
The main bottleneck for extending context length is not computation but the KV cache memory during inference. Multi-head Latent Attention (MLA) projects keys and values into a low-dimensional latent space, caches only this low-dimensional representation, and projects it back up when needed. This significantly reduces cache size while preserving the expressive power of multiple attention heads.
The benefits are threefold: first, memory usage drops substantially under long contexts; second, reduced memory access improves throughput during the decoding stage; third, batching can accommodate more concurrent requests. Note that low-rank compression introduces extra projection computation and some accuracy loss, so the dimension choice must balance quality against cost.
4. Pretraining Data and Supervised Fine-Tuning
High-quality corpora determine the upper bound of model capability. In practice, a multi-source corpus on the order of trillions of tokens is typically built, covering general text, code, math, multilingual, and domain-specific content, combined with strict deduplication, quality scoring, toxicity filtering, and data-mixing experiments. Data mixing often yields more noticeable differences in results than changes to model architecture.
The supervised fine-tuning stage emphasizes instruction diversity and difficulty tiers, covering scenarios such as question answering, writing, code, reasoning, role-playing, and tool calling, while retaining “refusal and safety” samples in the data. A reusable way to organize fine-tuning data is shown below:
[{"messages": [{"role": "system", "content": "You are a rigorous technical assistant"},{"role": "user", "content": "Explain how low-rank attention reduces memory usage"},{"role": "assistant", "content": "..."}],"meta": {"domain": "technical Q&A", "difficulty": 2}}]
5. Evaluation: By Language, Capability, and Scenario
A single leaderboard is not enough to demonstrate a model’s usability. A three-layer evaluation is recommended: a foundational capability layer (language understanding, math, code, long-text recall), an alignment layer (helpfulness, harmlessness, honesty, instruction following), and a business layer (pass rate on the enterprise’s own scenario sets and human preference win rate). Evaluation sets should be decontaminated, and inference parameters should be fixed to ensure comparability.
6. Inference Deployment and Cost Governance
The key deployment decision is the parallelism strategy: single-node multi-GPU tensor parallelism reduces per-GPU memory pressure, while multi-node expert parallelism supports extremely large parameter pools; the two are often combined. For serving, it is recommended to focus on:
- Prefix cache reuse to reduce redundant computation;
- Continuous batching and dynamic batch sizes to improve decoding throughput;
- Quantization and operator fusion to compress weights and memory access;
- Tiered routing, where simple requests go to small models and complex requests are escalated to large models;
- Comprehensive logging, monitoring and alerting, and rate limiting with quotas to avoid single-point overload.
The cost model should be calculated on a unified “per million input/output tokens” basis, incorporating cache hit rate, average output length, and peak concurrency into capacity planning. Baidu AI Cloud provides general-purpose capabilities for model serving and compute scheduling, such as container platforms, object storage, logging services, and monitoring and alerting, which can serve as the implementation foundation for the engineering elements above.
7. Adapting to Domestic AI Processors
Running sparse models on domestic AI processors requires close attention to operator coverage, communication library efficiency, low-precision support, and graph compiler compatibility. The practical path is to first verify numerical consistency on a single card, then gradually scale to multi-card and multi-node setups, while keeping a fallback version to quickly locate accuracy or performance regressions.
8. Common Pitfalls and Practical Recommendations
First, do not look only at total parameters; also evaluate activated parameters, context length, and memory usage. Second, the throughput advantage of sparse models depends on high concurrency, with limited benefit under low concurrency. Third, routing imbalance can significantly degrade quality, so expert load distribution must be monitored during training. Fourth, long-context capability requires dedicated position-encoding extrapolation and recall evaluation. Fifth, cost optimization should start from the request structure rather than relying solely on price reductions.
