feat: INT8 KV cache quantization (~48% memory reduction)#184
Open
dzhengAP wants to merge 1 commit into
Open
Conversation
- Add kv_quant.py with two Triton kernels: - store_kvcache_int8_kernel: quantize K/V to INT8 at store time - dequant_kvcache_kernel: dequantize to FP16 at decode time - Per-(token, head) symmetric INT8 with FP32 scale - Config.kv_quant=False by default (fully backwards-compatible) - ~48% KV cache memory reduction vs BF16 baseline - Allows ~2x more concurrent sequences on same GPU budget - estimate_memory_savings() utility for pre-run estimation Usage: LLM(model_path, kv_quant=True)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
On an 8GB GPU, the KV cache is the primary memory bottleneck for serving
multiple concurrent sequences. This PR adds INT8 quantization of the KV cache,
reducing its memory footprint by ~48% and allowing ~2× more sequences to be
batched simultaneously.
Design
copy or Python overhead on the hot path
flash_attn_with_kvcachescale = max(|x|) / 127, stored as FP32kv_quant=Falseby default — fully backwards-compatibleMemory savings
(measured at 500 blocks × 256 block_size × 28 layers × 8 kv_heads × 128 head_dim)
Usage
Files changed
nanovllm/layers/kv_quant.py— new: Triton store + dequant kernels + memory estimatornanovllm/layers/attention.py— wire in INT8 path alongside existing FP16 pathnanovllm/engine/model_runner.py— allocate INT8 cache + FP32 scale tensorsnanovllm/config.py— addkv_quant: bool = False