[论文解读] KVQuant: Towards 10 Million Context Length LLM Inference with KV Cache Quantization
KVQuant 引入按通道、预 RoPE、非均匀的逐向量密集-稀疏 KV 缓存量化,配合 Q-Norm,实现超长上下文推理(高达 10M)几乎不增加困惑度并带来可测量的加速。
LLMs are seeing growing use for applications which require large context windows, and with these large context windows KV cache activations surface as the dominant contributor to memory consumption during inference. Quantization is a promising approach for compressing KV cache activations; however, existing solutions fail to represent activations accurately in sub-4-bit precision. Our work, KVQuant, facilitates low precision KV cache quantization by incorporating several novel methods: (i) Per-Channel Key Quantization, where we adjust the dimension along which we quantize the Key activations to better match the distribution; (ii) Pre-RoPE Key Quantization, where we quantize Key activations before the rotary positional embedding to mitigate its impact on quantization; (iii) Non-Uniform KV Cache Quantization, where we derive per-layer sensitivity-weighted non-uniform datatypes that better represent the distributions; and (iv) Per-Vector Dense-and-Sparse Quantization, where we isolate outliers separately for each vector to minimize skews in quantization ranges. By applying our method to the LLaMA, Llama-2, Llama-3, and Mistral models, we achieve < 0.1 perplexity degradation with 3-bit quantization on both Wikitext-2 and C4, outperforming existing approaches. Our method enables serving LLaMA-7B with a context length of up to 1 million on a single A100-80GB GPU and up to 10 million on an 8-GPU system. We develop custom CUDA kernels for KVQuant, showing that we can achieve up to ~1.7x speedups, compared to baseline fp16 matrix-vector multiplications, for the LLaMA-7B model.
研究动机与目标
- 推动在长上下文 LLM 推理过程中降低 KV 缓存内存瓶颈。
- 展示在长序列和批处理场景中 KV 缓存激活对内存的主导作用。
- 提出一套量化技术,实现在极低精度下且困惑度损失最小化。
- 展示在 LLaMA、LLaMA-2 和 Mistral 模型上的端到端改进,包括长上下文评估。
- 提供内核级实现与开源工具,以便于实际部署。
提出的方法
- 分析 KV 缓存分布以识别异常值与 RoPE 效应。
- 对 Keys 进行按通道量化,对 Values 进行逐 token 量化。
- 在 RoPE 之前对 Keys 进行量化,以缓解 RoPE 引起的分布偏移。
- 开发 nuqX:一种通过离线标定得到的敏感度加权非均匀数据类型。
- 引入逐向量的密集与稀疏量化,以在每个向量中隔离异常值。
- 提出 Q-Norm,使量化后的质心与量化前的统计量对齐。
- 实现自定义 CUDA 内核,在推理过程中加速 KV 量化与反量化。
实验结果
研究问题
- RQ1在大型 LLM 上,KV 缓存激活是否可量化到低于 4 位且困惑度损失可忽略?
- RQ2按通道和 RoPE 之前的量化策略是否比现有方法更好地保留 Key 的分布?
- RQ3非均匀(nuqX)量化和逐向量异常值处理对 KV 缓存准确性的效果如何?
- RQ4在多 GPU 设置下,超长上下文推理(高达 10M tokens)可实现的内存与加速有哪些?
- RQ5KVQuant 与权重量化和端到端部署的兼容性如何?
主要发现
- 3-bit KV 缓存量化在 LLaMA-7B/13B/30B/65B 上对 Wikitext-2 和 C4 的困惑度下降不到 0.1。
- KV 缓存内存占用在 3-bit 精度下约降低 4.8x。
- 使用自定义 CUDA 内核,Key/Value 矩阵向量运算提速高达 1.4x。
- 在单卡 A100-80GB 上可达 1M 的长上下文推理;在 8 GPUs 下可达 10M,KVQuant 使之成为可行。
- RoPE 之前的 Key 量化和按通道策略显著优于先前方法。
- 通过逐向量的密集-稀疏量化和 1% 异常值剔除的离群值处理,保持接近 FP16 的性能。
- Q-Norm 通过使反量化分布与量化前统计量对齐,提升至 2-bit 量化的准确性。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。