Four Key Techniques for Lightweight Large Language Model Inference
Lightweight LLM inference is about more than shrinking parameter counts. This article covers four techniques targeting four distinct bottlenecks: KV cache growth, MoE compute overhead, repeated local patterns, and autoregressive latency. For each technique, we explain how it works, what it costs, how to validate it, and when to use it, followed by a comparison table and a pre-deployment checklist. All numbers are illustrative examples, not official specifications of any specific model; refer to each model's technical report for actual figures.
Lightweight large language models typically do not reduce costs by simply cutting parameters. The four techniques in this article each address a different problem: KV cache growth, expert compute overhead, repeated local patterns, and generation latency. Below, we first explain what each technique does, then cover its costs, validation methods, and selection criteria. All numbers in this article are hypothetical examples, not official specifications of any particular model. For real values, refer to each model’s technical report.
Layered Attention: Controlling KV Cache Growth
The traditional design applies full attention over the entire token history at every layer. This leaves no retrieval blind spots, but the KV cache grows linearly with sequence length, and compute grows quadratically.
A layered design splits the network into two types of layers. The first type is sliding window layers, which only attend to a fixed number of the most recent tokens, for example a window of 512. Their cache size is determined by the window and does not grow with total sequence length. The second type is sparse global layers, which use an indexer to select the most relevant tokens from the full history, for example at most 2,048 tokens.
It is important to be precise here: sparse global layers still need to store the KV (or some compressed index) of the entire history, so the overall cache still grows with context length. The benefit of layering is a significantly lower growth slope compared to applying global attention at every layer, not a constant-size cache.
Consider a hypothetical configuration: of 42 layers, 30 are sliding window layers and 12 are sparse global layers. When the model analyzes a 200,000-token code repository and needs to recall a function defined 80,000 tokens earlier, the sliding window layers handle syntactic structure near the current position, while the sparse global layers use the indexer to locate the distant function definition. This division of labor preserves long-range retrieval while reducing the average compute per token.
Low-Dimensional Routing: Balancing MoE Parameters and Overhead
Mixture of Experts (MoE) scales model capacity with multiple expert networks, but both expert parameters and activation compute add overhead. Low-dimensional routing first projects hidden representations from a high-dimensional space into a low-dimensional one, then hands them to the router for expert assignment.
This design often leads to a common misconception: doubling the number of experts seems like it should increase compute. In fact, total expert parameters are approximately the product of three factors: the number of experts, the input dimension, and the internal width of each expert. In a hypothetical configuration, increasing the expert count from 256 to 512 while halving the input dimension from 3,072 to 1,536 leaves total expert parameters essentially unchanged. Total parameters only decrease if the internal expert width is also reduced.
The same logic applies to per-token expert compute. Doubling the number of activated experts from 8 to 16 while halving each expert’s input dimension keeps the expert feed-forward compute roughly on par with the original design. Any savings may come from other modules or from adjustments to expert width, and must be verified through measurement.
Low-dimensional projection also introduces an information bottleneck risk. A common practice is to add a residual connection alongside the projection layer, so high-dimensional information can bypass the dimensionality-reduction path. Whether this works should be judged by quality regression results on downstream tasks.
N-gram Memory Tables: Which Computations Can Be Replaced by Lookup
A large share of text consists of repeated local fragments: function templates, fixed code structures, common phrases. The next-token output following these fragments is often highly predictable. An N-gram memory table hashes consecutive token sequences and looks up pre-stored representation vectors, which are then added to the model’s hidden states.
On a memory-table hit, the model can skip part of the attention or feed-forward computation. This is effective for code completion and structured data generation, where repeated fragments are common.
The costs are also significant. Table size is determined by the number of N-grams and the vector dimension; large tables must reside in host memory or remote storage. A hash lookup has O(1) theoretical average complexity, but actual latency depends on memory bandwidth and cache miss rates, so it cannot be directly equated with compute savings. The hit rate depends on how repetitive the workload data is; general conversation typically has much lower hit rates than code workloads.
The memory table and the attention mechanism are complementary. Attention handles contextual semantics and long-range dependencies, while the memory table provides fast responses to highly predictable local patterns. They serve different roles and cannot replace each other.
Speculative Decoding: How Much Time Does a Deeper Draft Network Save?
Autoregressive generation produces one token per step, which makes it hard to fully utilize GPU parallelism. Speculative decoding lets a draft network propose k candidate tokens, which the main model then verifies in parallel with a single forward pass.
A rough formula can estimate the speedup. Suppose the draft network proposes k tokens per round, each draft token costs c times the main model’s per-step cost, and each round yields an average of a accepted tokens (including the one corrected by the main model). Then the relative cost per output token is approximately (1 + c·k) / a, and the speedup ratio is approximately a / (1 + c·k).
This formula illustrates the trade-off of making the draft network deeper. A deeper draft network increases c, but may also increase the acceptance length a. Deepening only pays off when the gain in a outweighs the increase in draft cost.
In this scheme, the draft network can reuse the main model’s KV cache and sparse attention index, so it does not need to maintain a separate copy of KV states, saving additional GPU memory. It still consumes compute resources; what it primarily saves is memory.
Comparison and Selection
| Technique | Problem Solved | Main Cost | Applicable Boundary | Key Validation Metrics |
|---|---|---|---|---|
| Layered attention | KV cache and compute growth with length | Index structures must be maintained; sparse layers still grow with length | Long contexts with clear long-range dependencies | Long-context retrieval accuracy, KV memory usage |
| Low-dimensional MoE routing | Expert compute and memory overhead | Projection may create an information bottleneck | Memory-constrained deployments with stable expert routing | Downstream task quality, expert load balancing |
| N-gram memory table | Redundant compute on repeated local patterns | Extra memory and memory-access overhead | Highly repetitive scenarios such as code and structured text | Hit rate, latency on the hit path |
| Speculative decoding | Autoregressive generation latency | Draft compute cost, unstable acceptance rate | Long outputs where the draft predicts well | Average acceptance length, output tokens per second |
Pre-Deployment Checklist
- Profile the distribution of input and output token lengths in real traffic, focusing on P50, P95, and P99.
- Break down the memory budget and estimate the footprint of weights, KV cache, the indexer, and the memory table separately.
- Measure the N-gram memory table’s hit rate on real business data, not just public benchmarks.
- Record the draft network’s acceptance rate and average acceptance length to determine whether speculative decoding is worthwhile.
- Establish baselines for throughput, time to first token, and P99 latency, and compare against them after every change.
- Run quality regression tests for long-context retrieval and code tasks to confirm that saved resources did not come at the cost of quality.
Conclusion
The common thread across these four techniques is reducing redundant computation and unnecessary storage, rather than simply compressing parameters. Each technique addresses a different bottleneck and introduces its own overhead. When selecting among them, first identify your primary bottleneck, then use the metrics above to judge which technique is worth adopting.
