Commit Β·
abfc070
1
Parent(s): 0774ec2
feat: Update Readme
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152531 unmodified line55565758596061626364656667686970717221 unmodified lines9495969798991001011021031042 unmodified lines1071081091101111121131141151161171183 unmodified lines12212312412512612712812913 unmodified lines143144145146147148149150151152153154155156157158159160161162163164165166167168169170# Per-Head Mixed-Precision KV Cache Compression> Calibrate once. Compress smarter. Same quality.Most KV cache quantization treats every attention head equally.This is wrong. Some heads are **26x more sensitive** to quantization than others.We measure this, allocate bits accordingly, and get better compression than uniform 8-bit with zero quality loss.---## Results| Model | Method | Avg Bits | KV @ 8K | vs FP16 | vs 8-bit | Perplexity | Speed ||-------|--------|----------|---------|---------|---------|------------|-------|| Mistral-7B | FP16 Baseline | 16 | 1073 MB | 1.0x | β | 14.23 | 37.2 t/s || Mistral-7B | Uniform 8-bit | 8 | 537 MB | 2.0x | 1.0x | ~same | ~same || Mistral-7B | **Per-Head Mixed (Ours)** | **6.95** | **467 MB** | **2.3x** | **1.15x** | **14.23** | **37.2 t/s** || Llama-3-8B | FP16 Baseline | 16 | 1073 MB | 1.0x | β | 20.7 | 36.7 t/s || Llama-3-8B | Uniform 8-bit | 8 | 537 MB | 2.0x | 1.0x | ~same | ~same || Llama-3-8B | **Per-Head Mixed (Ours)** | **7.84** | **526 MB** | **2.04x** | **1.02x** | **20.7** | **36.7 t/s** |---## Long Context Results| Context | FP16 (Mistral) | Ours (Mistral) | FP16 (Llama) | Ours (Llama) ||---------|---------------|----------------|--------------|--------------|| 8K | 1,074 MB | 467 MB | 1,074 MB | 526 MB || 16K | 2,147 MB | 933 MB | 2,147 MB | 1,053 MB || 32K | 4,295 MB | 1,866 MB | OOM | ~2,106 MB |Llama-3-8B FP16 runs out of memory at 32K context. Our method fits.---## The Key InsightEach cell is one attention head. Darker means more sensitive, which means it needs higher precision.The variance is massive β heads in the same layer need completely different treatment.Uniform quantization ignores this entirely.---1 unmodified line**Step 1 β Calibrate (once, ~20 minutes)**Run 256 WikiText samples through the model. For each attention head measure reconstruction error at 4-bit and 8-bit. Save the optimal bit allocation to a JSON file (~1KB).**Step 2 β Compress (every inference)**Load the bit allocation. Quantize each head to its optimal precision. 4-bit heads use half the memory. 8-bit heads stay accurate.**Step 3 β Results**- 2.3x memory reduction on Mistral-7B- 2.04x memory reduction on Llama-3-8B- Zero perplexity degradation on both models- Same decode speed at 37 tokens/sec---21 unmodified linesRun step by step:make baseline MODEL=mistral-7bmake calibrate MODEL=mistral-7bmake integrate MODEL=mistral-7bmake benchmark MODEL=mistral-7bmake benchmark-long MODEL=mistral-7bmake visualize---2 unmodified lineskv-cache-compression/βββ kernel/β βββ quant_cache.py mixed-precision quantize/dequantizeβββ scripts/β βββ baseline.py FP16 baseline measurementsβ βββ calibrate.py per-head sensitivity calibrationβ βββ integrate.py quantized inference integrationβ βββ benchmark.py full benchmark suiteβ βββ benchmark_long_context.py 16K/32K context benchmarksβ βββ visualize_results.py benchmark graphsβ βββ visualize_long_context.py long context graphs3 unmodified linesβ βββ run_mistral.sh full Mistral pipelineβ βββ run_llama.sh full Llama pipelineβββ results/β ββοΏ½οΏ½οΏ½ mistral-7b/ baseline, calibration, benchmarkβ βββ llama-3-8b/ baseline, calibration, benchmarkβββ figures/ all generated graphsβββ requirements.txt pip dependenciesβββ Makefile one-command pipeline13 unmodified lines## Limitations- Current 4-bit implementation stores values in uint8 which wastes half the space. True bit-packing via Triton kernel is in progress on the triton-kernel branch.- Calibration uses WikiText-2. Domain-specific calibration may improve results for specialized use cases.- Tested on 7-8B models only. Larger models need validation.- Integration is HuggingFace only. vLLM integration is planned.<!-- ---## What's Next- True Triton 4-bit bit-packing kernel (triton-kernel branch)- vLLM PagedAttention integration- 32K and 128K context experiments- Llama-3-70B and Qwen-72B validation- Dynamic per-token bit allocation at decode time- ArXiv paper with full evaluation --><!-- ---## Citation@misc{kvcache-perhead-2026,title = {Per-Head Mixed-Precision KV Cache Compression},author = {Your Name},year = {2026},url = {https://github.com/YOURUSERNAME/kv-cache-compression}123451 unmodified line78910111213141516171819202122232425262721 unmodified lines49505152535455565758592 unmodified lines626364656667686970717273743 unmodified lines787980818283848513 unmodified lines99100101102103104105106107108109110111112113114115116117118119120121122123124125This is why the Triton kernel is essential β not just faster, but the only wayto realize the theoretical memory savings from 4-bit quantization.---1 unmodified line**Step 1 β Calibrate (once, ~20 minutes)**Run 256 WikiText samples through the model. For each attention head measurereconstruction error at 4-bit and 8-bit. Save optimal bit allocation to JSON (~1KB).**Step 2 β Compress (every inference)**Load the bit allocation. Use Triton kernel to truly pack 4-bit heads (2 values per byte).Keep 8-bit heads at full precision. Result: genuine memory reduction.**Step 3 β Results**- 2.30x memory reduction on Mistral-7B (vs 2.00x for naive/uniform)- 2.04x memory reduction on Llama-3-8B- Zero perplexity degradation on both models- Same decode speed at 37 tokens/sec- Triton kernel is 10-12% faster than naive PyTorch implementation---21 unmodified linesRun step by step:make baseline MODEL=mistral-7bmake calibrate MODEL=mistral-7bmake integrate MODEL=mistral-7bmake benchmark MODEL=mistral-7bmake benchmark-long MODEL=mistral-7bmake visualize---2 unmodified lineskv-cache-compression/βββ kernel/β βββ quant_cache.py naive uint8 implementationβ βββ quant_cache_triton.py true Triton 4-bit bit-packingβββ scripts/β βββ baseline.py FP16 baseline measurementsβ βββ calibrate.py per-head sensitivity calibrationβ βββ integrate.py naive vs triton comparisonβ βββ benchmark.py full 4-method benchmark suiteβ βββ benchmark_long_context.py 16K/32K context benchmarksβ βββ visualize_results.py benchmark graphsβ βββ visualize_long_context.py long context graphs3 unmodified linesβ βββ run_mistral.sh full Mistral pipelineβ βββ run_llama.sh full Llama pipelineβββ results/β βββ mistral-7b/ all mistral results and JSONsβ βββ llama-3-8b/ all llama results and JSONsβββ figures/ all generated graphsβββ requirements.txt pip dependenciesβββ Makefile one-command pipeline13 unmodified lines## Limitations- Tested on 7-8B models only. Larger models (70B+) need validation.- Calibration uses WikiText-2. Domain-specific calibration may improve results.- Integration is HuggingFace only. vLLM integration is planned.- Llama-3-8B Triton compression (2.04x) is modest due to high head sensitivity.---## What's Next- vLLM PagedAttention integration- 32K and 128K context experiments- Llama-3-70B and Qwen-72B validation- Dynamic per-token bit allocation at decode time- ArXiv paper with full evaluation---<!-- ## Citation@misc{kvcache-perhead-2026,title = {Per-Head Mixed-Precision KV Cache Compression with True Triton Bit-Packing},author = {Your Name},year = {2026},url = {https://github.com/YOURUSERNAME/kv-cache-compression}
README.md+27-72