LLM Technical Routes: Long Context, Lightweighting, and Multimodality
This article examines three common technical directions for large language models: sparse Mixture-of-Experts architectures for expanding capacity, long-context mechanisms for managing attention and KV-cache costs, and compression plus multimodal fusion for lightweighting and capability expansion. It explains the trade-offs, implementation logic, and engineering constraints behind these routes, with emphasis on choosing among them based on scenario requirements.
1. Start from Requirements to Understand Technical Routes
There is no single optimal solution for large language model development. Even for general-purpose language models, different teams make trade-offs among capability, inference cost, response latency, and deployment environment based on the target scenario. Some directions focus on complex code generation and long-document understanding, some focus on low-cost or on-device deployment, and others bring image, speech, and other modalities into the same model. To understand these differences, first identify the constraints, then determine which type of technology best solves the problem.
This article focuses on three common directions: expanding model capacity through sparse structures, handling very long inputs through long-context mechanisms, and achieving lightweighting and capability expansion through compression and multimodal fusion.
2. Mixture of Experts: Expand Capacity with Sparse Activation
The core idea of Mixture of Experts (MoE) is to split the feed-forward network into multiple expert subnetworks, then use a router to select a small number of experts for each input token. The total parameter count can be large, but each forward pass activates only a subset, balancing parameter scale and compute cost.
The gating process usually has three steps:
- Compute gating scores: apply a linear transformation to the input vector to obtain a score for each expert.
- Select Top-K: keep only the K experts with the highest scores.
- Normalize and weight: apply softmax to the scores of these K experts to obtain combination weights, then compute a weighted sum of their outputs.
Below is a simplified illustrative implementation:
import numpy as npdef top_k_gating(x, W_gate, k=2, temperature=1.0):"""Return the selected expert indices and their combined weights."""logits = x @ W_gate # One score per expertscaled = logits / temperature # Temperature scaling changes only the sharpness of the weight distributiontop_idx = np.argsort(scaled)[-k:] # Hard Top-K selection with a fixed number ktop_logits = scaled[top_idx]weights = np.exp(top_logits - top_logits.max())weights /= weights.sum() # Normalize across selected expertsreturn top_idx, weightsx = np.random.randn(512)W_gate = np.random.randn(512, 8) # 8 expertsidx, w = top_k_gating(x, W_gate, k=2, temperature=0.5)print(idx, w)
A common misunderstanding should be clarified: softmax temperature does not change the number of activated experts. Top-K is a hard selection, and the number is determined by the hyperparameter k. Temperature only affects the weight distribution among the selected experts. A lower temperature concentrates weights on the highest-scoring expert, while a higher temperature makes the weights closer to uniform.
MoE has three main engineering challenges. First, memory footprint is still related to the total parameter count, because all experts must reside on devices or be distributed across multiple GPUs through parallelism strategies. Second, routing can become imbalanced: a few experts receive many requests while the rest remain idle for long periods. During training, an auxiliary load-balancing loss is often added, or a capacity factor is set to constrain the number of tokens processed by each expert. Third, in distributed inference, tokens must be dispatched to and collected from devices, and communication overhead can significantly affect latency.
3. Long Context: Manage Attention Compute and KV Cache
The compute cost of standard self-attention grows quadratically with sequence length n. During autoregressive generation, the memory used by the key-value (KV) cache of generated content grows linearly with length. Therefore, long-context capability is mainly limited by two types of resources: compute and memory.
Common engineering methods include:
- Grouped-Query Attention: multiple query heads share fewer key-value heads, significantly reducing the KV cache.
- Sliding-window or sparse attention: limit the range each position can attend to, lowering computational complexity.
- KV-cache quantization: compress the cache from 16-bit floating point to 8-bit or lower precision, trading a small accuracy loss for longer context.
- Positional encoding extrapolation: adjust rotary positional encoding frequencies or scaling factors so the model remains stable on text beyond the training length.
- Chunked prefill: split long inputs into multiple segments to avoid a single computation occupying all resources, and to improve scheduling fairness with other requests.
Estimating KV-cache memory is the first step in deployment planning. The approximation takes the number of layers, number of KV heads, head dimension, sequence length, batch size, and bytes per element as inputs. Keys and values are each stored once, so both are counted. The default bytes_per_elem=2 corresponds to FP16.
