Evolution of Large Language Models: Long Context and Mixture of Experts Architectures
As large language model applications shift from single-turn Q&A towards long document analysis, codebase understanding, and multi-step agent tasks, handling long context and scaling training have become critical. This article explores the technical evolution of large models, focusing on the computational and memory bottlenecks of long context processing, training stability challenges in Mixture of Experts (MoE) architectures, optimizer selection strategies, and pre-training data quality. Readers will gain insights into how attention mechanisms, expert networks, and optimization rules interact to impact model capabilities and engineering costs.
1. Why We Need to Focus on Both Long Context and Scaled Training
Large model applications are shifting from single-turn Q&A to long document analysis, codebase understanding, and multi-step agent tasks. These tasks share two common characteristics: the input text is very long, and the model needs to maintain a massive amount of intermediate states during a single inference pass. Meanwhile, as the parameter scale expands, numerical stability during training and compute utilization have become primary bottlenecks.
Both types of issues share the same design space, including the computation method of the attention mechanism, the organization of expert networks, the update rules of optimizers, and the execution efficiency of underlying hardware. Understanding how these components interact is fundamental to evaluating model capabilities and engineering costs.
2. Computation and Memory Bottlenecks in Long Context
The computation of standard self-attention scales quadratically with the sequence length. Increasing the sequence length from 10,000 to 1,000,000 scales the attention matrix size by about 10,000 times. During the inference phase, the Key-Value (KV) cache must be saved for each layer, and its memory footprint is proportional to the sequence length, number of layers, number of KV heads, and head dimension.
The estimation method is as follows:
def kv_cache_bytes(seq_len, n_layers, n_kv_heads, head_dim, bytes_per_elem=2):# K and V are each stored once, so multiply by 2return 2 * seq_len * n_layers * n_kv_heads * head_dim * bytes_per_elem# Assumed configuration: 32 layers, 8 KV heads, head dimension 128, FP16 storage, 1 million token contextsize = kv_cache_bytes(1_000_000, 32, 8, 128)print(f"{size / 1e9:.1f} GB") # Approximately 131.1 GB
This example illustrates that even if model weights can be sharded across multiple GPUs, the cache itself can become a memory bottleneck. Common mitigation strategies include the following:
- Sparse Attention: Each query token only attends to a subset of historical tokens, such as using a sliding window, block selection, or importance filtering. Computation can drop from quadratic to near-linear, but care must be taken to prevent key information from being omitted.
- Compression and Merging: Merge the KV of adjacent tokens along the sequence dimension into fewer representations to reduce the cache footprint. The higher the compression ratio, the greater the loss of fine-grained information, which requires validation through long-text retrieval tasks.
- Hybrid Attention: Some layers retain fine-grained neighbor information, while other layers cover long-range dependencies at a lower resolution. The ratio between the two types of layers is a critical hyperparameter that requires ablation studies based on the specific task.
- Cache Quantization: Reduce the KV cache from 16-bit to 8-bit or lower precision, which is relatively common in inference deployment. It requires checking for precision degradation on long sequences.
From an engineering perspective, three metrics should be measured simultaneously: inference computation per token, peak memory footprint, and accuracy on long-range dependency tasks. Looking at only one of these can easily lead to biased conclusions.
3. Training Stability in Mixture of Experts (MoE) Architectures
Mixture of Experts (MoE) models use a routing network to select a small number of expert subnetworks for each token. The total parameter count can be massive, while only a fraction is activated during a single forward pass, allowing for greater knowledge capacity under the same compute budget. This structure also introduces new training challenges.
The main risks fall into the following three categories:
- Load Imbalance: A few experts receive the majority of tokens, while the remaining experts are undertrained, leading to “expert collapse.” Common solutions include adding load balancing auxiliary losses or employing routing strategies with capacity limits.
- Routing Oscillation: Minor changes in the routing network can alter the token assignment results, causing frequent switches in the gradient direction. This can be mitigated by smoothing the routing outputs, injecting moderate noise, or lowering the learning rate of the routing layer.
- Numerical Overflow: Deep residual streams exhibit continuous magnitude growth in the later stages of training, making gradient explosions or loss spikes prone to occur under low-precision computation.
At the residual connection level, one approach is to apply geometric constraints to the residual path, such as limiting the spectral norm of its transformation matrix, to keep the signal bounded across multi-layer propagation. The benefits of such methods must be measured by the fluctuation amplitude of the loss curve and the frequency of training interruptions, rather than just looking at the final metrics.
For training monitoring, it is recommended to simultaneously record the following information: activation norms across layers, expert load distribution, and gradient norm quantiles. Anomaly alerts should be set based on these metrics, and rollback-capable checkpoints should be retained.
4. Optimizer Selection and Pre-training Data
Adaptive optimizers like Adam remain the baseline for most large model training. Recently, a class of methods has attempted to utilize the structure of weight matrices for updates, such as applying approximate orthogonalization to the momentum matrix before applying the update. The goal is to make the update magnitude more balanced across different directions, thereby improving convergence efficiency. The benefits of these methods depend on the joint tuning of the learning rate, weight decay, and batch size; directly replacing the baseline usually does not yield equivalent results.
Regarding pre-training data, quality is typically more important than simply increasing quantity. A typical pipeline includes the following steps:
- Deduplication: Combining exact and approximate deduplication to reduce the risk of the model memorizing training data.
- Quality Filtering: Combining rules
