PQCache: Product Quantization-based KVCache for Long Context LLM Inference

Hailin Zhang, Xiaodong Ji, Yilin Chen, Fangcheng Fu, Xupeng Miao, Xiaonan Nie, Weipeng Chen, Bin Cui

Abstract

PQCache addresses the massive memory bottleneck of Key-Value (KV) Caching in long-context Large Language Models (LLMs) by reframing the problem as one of information retrieval. By applying Product Quantization (PQ) to compress keys and selectively fetching only the most relevant tokens from CPU memory, PQCache achieves a 4.60% performance boost on long-context benchmarks while maintaining low inference latency.

1. Introduction: The KVCache Bottleneck

As LLMs move toward "infinite" context windows (128k to 1M+ tokens), the KVCache—which stores intermediate token representations to avoid redundant computation—has become the primary memory bottleneck. A 7B model processing 128k tokens can require 1TB of KVCache, far exceeding the capacity of high-end GPUs like the H100.

Current solutions typically involve dropping "unimportant" tokens, which risks losing critical information, or offloading the cache to the CPU, which introduces massive latency. PQCache identifies a middle ground: keeping the entire context offloaded to the CPU but only retrieving the relevant parts for computation without sacrificing system speed.

2. Methodology: PQCache Architecture

PQCache is a system-algorithm co-designed approach that integrates Product Quantization—a staple of vector database indexing—directly into the LLM inference pipeline.

PQ Construction and Searching process

2.1 Product Quantization (PQ)

PQ breaks high-dimensional key vectors into smaller sub-vectors and clusters them. Instead of storing the full vector, the system stores a short "code" representing the nearest cluster center, significantly reducing the data footprint.

2.2 Prefilling and Decoding

During the Prefilling Phase, KV pairs are offloaded to the CPU while an Adaptive K-Means cost model builds the PQ index. This model limits clustering iterations based on the GPU's current workload to prevent slowing down the prefilling process. In the Decoding Phase, for each new token generated, the model performs a search using the PQ index to identify the top-$k$ most relevant preceding tokens and fetches only those specific KV pairs from the CPU.

2.3 System Optimizations

To minimize transfers, frequently accessed tokens are kept in a small block-level GPU cache using an LFU/LRU policy. Additionally, the system prefetches PQ codes for the next layer while the current layer is still computing, effectively hiding communication latency.

3. Key Insights: Self-Attention as Retrieval

The most significant conceptual shift in this work is reframing the self-attention mechanism as an Approximate Nearest Neighbor Search (ANNS).

Comparison between traditional Information Retrieval and LLM Inference with Selective Attention

By treating the CPU as an active indexing engine rather than a passive storage medium, PQCache ensures robustness regardless of where information is placed in the prompt. Unlike "dropping" methods that assume importance is concentrated at the end of a document, PQCache indexes the whole context, leading to a 7.10% performance gain when questions are placed at the beginning or middle of a document.

4. Experimental Results

PQCache was evaluated on Llama-3.1 and Mistral models across rigorous benchmarks including LongBench and InfiniteBench (100k tokens).

  • Accuracy: PQCache achieved a 4.60% score improvement over state-of-the-art methods like SPARQ and InfLLM on InfiniteBench, staying within 0.70% of "Oracle" (perfect retrieval) performance.
  • Latency: The system maintains a stable Time Per Output Token (TPOT) that does not scale linearly with sequence length, consistently delivering tokens faster than average human reading speed.
  • Needle-in-a-Haystack: PQCache showed near-perfect retrieval across the entire 128k context window, whereas block-based offloading methods often failed to find the "needle."

5. Conclusion and Future Work

PQCache demonstrates that treating KVCache as a retrieval problem can effectively scale LLM context windows. Future work may explore more complex retrieval structures like HNSW (graph-based) or IVF (inverted files) to further scale context limits and investigate dynamic index updates for extremely long generation tasks.

Summary updated

BibTeX
@inproceedings{zhang2024pqcache,
  author = {Hailin Zhang and Xiaodong Ji and Yilin Chen and Fangcheng Fu and Xupeng Miao and Xiaonan Nie and Weipeng Chen and Bin Cui},
  title = {{PQCache: Product Quantization-based KVCache for Long Context LLM Inference}},
  booktitle = {Proc. ACM Manag. Data},
  year = {2024},
}
Figure