Storing the Web in Memory: Space Efficient Language Models with Constant Time Retrieval

David Guthrie, Mark HeppleEMNLP 2010

Introduction

In Natural Language Processing (NLP), $n$-gram language models—which predict the probability of a word based on its preceding context—are foundational for tasks like machine translation and speech recognition. However, as training datasets have expanded to include the entire web (such as the Google Web1T corpus), these models have grown to encompass billions of entries, creating a significant engineering challenge. Standard trie-based structures consume excessive memory, while disk-based storage is too slow for real-time applications. This paper introduces three novel methods for storing massive $n$-gram models in RAM using minimal perfect hashing and tiered data structures, achieving storage costs as low as 1.66 bytes per $n$-gram.

The MPHR Framework

The authors propose architectures based on Minimal Perfect Hashing (MPH). An MPH function maps a set of $N$ keys to a unique range of integers from $0$ to $N-1$ with zero collisions. By utilizing a perfect hash, the system avoids storing the $n$-gram strings themselves, which is the primary source of bloat in traditional models.

Single Minimal Perfect Hash Ranking (S-MPHR)

In the basic S-MPHR model, the system uses the Hash, Displace, and Compress (CHD) algorithm to map each $n$-gram to a unique index. As shown in the architecture below, the system stores a small "fingerprint" (a hash of the $n$-gram) to detect membership and a "rank" that points to the actual probability or count value in a separate array.

The general architecture of the Minimal Perfect Hash Ranking (MPHR) system, showing the mapping from keys to fingerprints and ranks.

Compressed MPHR (C-MPHR)

The authors observed that $n$-gram counts follow a highly skewed distribution. C-MPHR exploits this by using variable-length coding for the rank array, assigning shorter bit-sequences to the most frequent counts, further reducing the memory footprint without sacrificing retrieval speed.

Advanced Tiering: T-MPHR

The most sophisticated version, Tiered MPHR (T-MPHR), partitions data into multiple levels. Because a small number of counts account for the vast majority of the data, T-MPHR stores these common values in a top-level tier using very few bits. Rare or complex counts are "redirected" to secondary hash tables.

The Tiered MPHR (T-MPHR) architecture, which uses redirects to secondary hash functions to handle rare counts efficiently.

This tiered approach allows the model to use, for example, only 8 bits for 85% of the data, invoking the more expensive 20-bit storage only when absolutely necessary. This maintains $O(1)$ constant-time lookups while maximizing compression.

Performance and Results

The methods were evaluated on industry-standard benchmarks, including the Google Web1T 5-gram corpus (3.7 billion $n$-grams) and the Gigaword Corpus (1.5 billion $n$-grams).

Key quantitative results include:

  • Space Efficiency: On the Gigaword dataset, the authors achieved a storage cost of 1.66 bytes per $n$-gram, a 70% reduction compared to previous state-of-the-art Bloomier filter approaches (~5.38 bytes).
  • Memory Footprint: The entire Google Web1T corpus was compressed into just 10.50 GB, allowing it to fit into the RAM of a standard server.
  • Speed: The C-MPHR model sustained over 500,000 queries per second, making it 15 times faster than popular Bloom-filter-based competitors like randLM.

Conclusion and Limitations

While these models offer unprecedented efficiency, they are inherently static. Because Minimal Perfect Hashing requires knowing all keys upfront, the models are read-only and cannot be updated without recomputing the hash function. Additionally, while the error rate is controllable via fingerprint size, the models can produce rare false positives. Despite these trade-offs, the MPHR framework represents a significant leap forward in making web-scale language models accessible for real-time, in-memory applications.

Summary updated

BibTeX
@inproceedings{guthrie2010storing,
  author = {David Guthrie and Mark Hepple},
  title = {{Storing the Web in Memory: Space Efficient Language Models with Constant Time Retrieval}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2010},
}
Figure