Where Should the N-gram Memory Table Live: Coordinating GPU VRAM and Host Memory
Modern LLMs often carry large auxiliary memory tables—such as N-gram embedding tables—that compete with model parameters and optimizer states for scarce GPU VRAM, while abundant, low-cost host memory sits idle. This article explains why N-gram memory is so expensive, distinguishes parametric memory from statistical memory, and shows how count-based lookup tables can be offloaded to host memory. It also covers practical engineering concerns including transfer latency, storage structure selection, and how to fuse statistical features into the model.
Where Should the N-gram Memory Table Live: Coordinating GPU VRAM and Host Memory
An Overlooked Resource Mismatch
Modern large language models carry more than just their core parameters—they often come with a large body of “auxiliary memory.” To give models a finer-grained grasp of phrase-level collocation patterns, engineers introduce dedicated embedding tables that record N-gram statistics. For example, the model needs to know that “New York” is highly likely to be followed by “Times” or “University,” and this kind of knowledge is encoded into standalone vector tables.
The problem is a resource mismatch. Because these tables require frequent reads and writes, they are typically placed in expensive GPU VRAM, where they compete with model parameters and optimizer states for every last gigabyte. Meanwhile, the large-capacity, low-cost host memory DIMMs on the server motherboard sit idle most of the time.
One research team zeroed in on exactly this contradiction. They observed that a classic technique decades old—phrase count statistics—is inherently independent of GPU parallel compute and can live entirely in host memory. They built a system that stitches classic statistical methods together with modern neural networks, letting “memory” and “computation” each return to where they belong. This line of thinking is valuable for any developer constrained by a VRAM budget.
Why N-gram Memory Is So Expensive
Combinatorial Explosion: Astronomical Storage Requirements
To grasp the severity of the problem, first clarify what an N-gram is: a contiguous sequence of N words. “New York” is a bigram; “New York Times” is a trigram. If a language model can remember “what word most often follows a given N-gram,” its prediction accuracy improves significantly.
But assigning an independent vector to every possible N-gram immediately runs into the wall of combinatorial explosion. Assume a vocabulary of just 100,000 words:
- The number of possible bigrams is on the order of 10^10;
- The number of possible trigrams is on the order of 10^15.
No hardware can store that. Even if each vector takes only a few dozen bytes, the total far exceeds the entire storage capacity of a single server.
Hash Compression: The Compromise and Its Costs
The common industry practice is hash mapping: use a hash function to compress the massive N-gram space into a fixed-size table, forcing colliding N-grams to share the same row of parameters. This does bring storage down to a manageable scale, but it introduces two hidden costs:
- Hash collisions introduce noise: distinct phrases are forced to share parameters, diluting fine-grained collocation knowledge;
- Parameters must be trained from scratch: these embeddings are randomly initialized learnable parameters that are frequently read and written by backpropagation during training, so they must reside in GPU VRAM—competing with backbone parameters and optimizer states (typically 2–3× the parameter count) for space.
The second point is the root of the VRAM crisis: a table meant to serve as “external memory” ends up becoming the burden that breaks VRAM.
A Different Angle: Statistical Memory Doesn’t Need Gradients
The key insight that breaks the deadlock is distinguishing two types of memory:
- Parametric memory: embedding vectors learned via gradient descent; they require backpropagation during training and cannot leave the GPU;
- Statistical memory: frequency information obtained by counting directly over the corpus; essentially a read-only lookup table that is never updated after construction and needs no gradients.
Phrase counting—the classic method—belongs to the latter category. It can be built offline on the CPU, stored in host memory or even on SSDs, and queried at inference time with a single lookup whose result is injected into the neural network as a feature. Lookup is an extremely lightweight operation that the CPU handles with ease, and host memory capacity is typically several to a dozen times that of a single GPU’s VRAM—at a much lower cost per gigabyte.
The overall system architecture can be summarized as:
Training/inference pipeline:1. Input text → extract N-gram features2. CPU side: query the count table in host memory → obtain statistical feature vectors3. Statistical features → transferred to the GPU (or read directly via zero-copy)4. GPU side: the neural network fuses statistical features with contextual representations → output prediction
In this way, the GPU focuses on the dense matrix operations it excels at, while host memory shoulders the large-capacity read-only memory—each playing to its strengths.
Key Engineering Considerations
Controlling Data Transfer Latency
Once the table moves to host memory, the biggest engineering risk is CPU-to-GPU transfer latency. Common optimizations include using pinned memory to accelerate transfers, pipelining the lookup operation to overlap with the previous layer’s GPU computation, and maintaining a small cache of high-frequency N-grams in VRAM. In practice, because statistical feature vectors are far smaller than model activations, the transfer overhead can usually be hidden.
Choosing the Storage Structure
The count table itself also requires careful design. Common options include hash maps, Bloom-filter-style probabilistic structures, and frequency-tiered storage (exact storage for high-frequency items, approximation or dropping for low-frequency ones). When a single machine’s memory is insufficient, sharding via object storage or distributed in-memory caches is possible, but network overhead must be weighed.
Integration with the Training Pipeline
How statistical features are injected into the model also matters: they can be concatenated at the embedding layer, fused at intermediate layers via gating mechanisms, or used at the output layer to interpolate and adjust vocabulary probabilities. Gated fusion is generally more robust, because it lets the model learn for itself when to trust the statistical memory and when to rely on contextual reasoning.
For teams looking to quickly validate this kind of heterogeneous architecture, Baidu AI Cloud offers general-purpose cloud computing and GPU compute services that can be flexibly combined with different memory and VRAM configurations, together with general product capabilities such as object storage and monitoring/alerting—enough to support the full path from prototype experiments to production deployment.
Conclusion
The essence of VRAM anxiety is often not that the problem is too big, but that resources are placed in the wrong location. Migrating gradient-free statistical memory from GPU VRAM to host memory both frees up precious compute resources and buys far larger memory capacity at low cost. This principle of heterogeneous coordination—“let the right hardware do the right job”—is well worth putting into practice for every engineer pursuing model capability on a limited budget.
