Repository navigation
paged-attn: KIVI 2-bit and 4-bit asymmetric KV cache quantization (#50) - #114
Open
sonnyman2014 wants to merge 1 commit into
Open
sonnyman2014 wants to merge 1 commit into
sonnyman2014 wants to merge 1 commit into
Conversation
…on (hanzoai#50) Implements clean-room KIVI-style (arXiv:2402.02750) asymmetric 2-bit and 4-bit paged KV cache quantization in hanzo-paged-attn: - Per-channel group Key quantization (INT4/INT2 with co-located f16 scale/zero words across G=32/64/128 token groups) isolating persistent outlier channels. - Per-token Value quantization (INT4/INT2) preserving sparse token distributions. - Sliding full-precision FP16/FP32 residual buffer (R=32/64) flushing atomically on completed group boundaries while keeping recurrent GDN state layers exempt. - Zero-allocation online page-streaming fused attention (Q @ K_quant^T and softmax @ V_quant) plus 5K/30K/128K, 20-turn agent, 4B/9B/27B hybrid, and 16-thread concurrency test suite and HIP-1346 evaluation report. Fixes hanzoai#50
Member
|
The reproduce command (cargo test -p hanzo-paged-attn --test kivi_kv_quant) does not compile: tests/kivi_kv_quant.rs lines 591 and 612 capture rng mutably in both closures of map_or_else (E0499). The 5 unit tests in src/kivi.rs pass; the benchmark table is not reproducible until this is fixed. |
This branch has not been deployed
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.
Summary
Implements KIVI-style asymmetric 2-bit and 4-bit KV cache compression (
KiviKvCache,KiviKeyPage,KiviValuePage, and online fused paged attention) inhanzo-paged-attnwith reproducible5K / 30K / 128Klong-context evaluation for #50 (HIP-1346).Fixes #50
1. Primary Reference & License Audit
https://arxiv.org/abs/2402.02750).https://arxiv.org/abs/2401.18079), referenced inhanzo-paged-attn/src/quant.rs.MIT OR Apache-2.0, matching the workspace license) authored directly againsthanzo-paged-attn'su32word-packing conventions and existing workspace dependencies (half,thiserror). Zero external paper code, calibration datasets, or new Cargo dependencies were added;hanzo-paged-attn/src/quant.rsand all CUDA/Metal/ROCm/Vulkan backends remain untouched.2. Architecture & Bounded Implementation
KiviKeyPage): Quantizes completedG-token groups (G = 32, 64, 128) per channel into little-endianu32words (8INT4nibbles or16INT2crumbs peru32) with co-located(f16 scale_c, f16 zero_c)u32words, isolating persistent key outlier channels (30x-80xlarger than semantic channels) that collapse uniform symmetric per-tokenINT4/INT2quantization.KiviValuePage): Quantizes each token's value vector independently (whole-token or configurablevalue_group_sizechannel subgroup) to preserve sparse softmax-weighted value fidelity.KiviKvCache): Retains the most recentR(R = 32, 64) tokens in unquantizedFP16orFP32storage, flushing atomically into bit-packed pages only when a fullG-token group completes across both single-tokenappend_tokenand zero-spike prefillappend_batch.fused_decode_head,fused_gqa_decode): Hoistssum_c (softmax_scale * q[c] * zero_c)andq_scaled[c]once perG-token Key page andsum_t (w_t * zero_v[t])per Value subgroup, streaming FlashAttention-style online rescaling across quantized pages and the residual tail without materializing dequantizedK/Vtensors orO(seq_len)logit buffers.LayerStateKind::RecurrentGdn): Explicitly exempts recurrent GDN state layers (LayerCompressionPlan::UnquantizedRecurrentGdn,Err(KiviError::RecurrentLayerExempt)) and validates via a 384-step DeltaNet negative control (828.4%relative L2 state drift under 2-bit recurrent quantization) why KV recipes must not be applied to recurrent GDN states.3. Benchmark Scorecard & Verification (
5K / 30K / 128K+4B / 9B / 27BHybrid Matrix)Reproduce with
cargo test -p hanzo-paged-attn --test kivi_kv_quant --release -- --nocapture(4 persistent outlier channels[+68, -54, +92, -76],head_size = 128, GQA4:1, 4 orthogonal Walsh-Hadamard needles planted at10%,50%,85%, andL - 12, measured onIntel Xeon E5-1650 v4 @ 3.60GHzLinuxx86_64):5K(5,120)FP16Baseline5,242,8801.00x(2.00x)1.0000000.000e+004/4(1.0000)5K(5,120)INT41,392,6403.76x(7.53x)0.0810134.875e-020/4(collapsed)5K(5,120)INT2737,2807.11x(14.22x)-0.0192798.427e-014/4(noisy)5K(5,120)INT4(G=64, R=64)1,481,2163.54x(7.08x)0.9978482.122e-044/4(0.9984)5K(5,120)INT2(G=128, R=32)737,2807.11x(14.22x)0.9556794.640e-034/4(0.9565)30K(30,720)FP16Baseline31,457,2801.00x(2.00x)1.0000000.000e+004/4(1.0000)30K(30,720)INT48,355,8403.76x(7.53x)0.1129723.388e-030/4(collapsed)30K(30,720)INT24,423,6807.11x(14.22x)-0.0101728.608e-014/4(noisy)30K(30,720)INT4(G=64, R=64)8,649,2163.64x(7.27x)0.9978561.479e-054/4(0.9984)30K(30,720)INT2(G=64, R=64)4,725,2486.66x(13.31x)0.9562933.176e-044/4(0.9674)30K(30,720)INT2(G=128, R=32)4,423,6807.11x(14.22x)0.9558843.174e-044/4(0.9565)128K(131,072, 1 KV head)INT4(G=64, R=64)18,373,8883.65x(7.30x)0.9969277.125e-084/4(0.9984)128K(131,072, 1 KV head)INT2(G=64, R=64)9,989,3766.72x(13.44x)0.9309432.287e-064/4(0.9674)128K(131,072, 1 KV head)INT2(G=128, R=32)9,437,1847.11x(14.22x)0.9289642.407e-064/4(0.9565)4B / 9B / 27Bhybrid KV + unquantized GDN state projection (128Kcontext):4B-Hybrid(9Attn +27GDN):1,179.0 MiB->342.4 MiB(3.44xtotal /3.65xAttn) atINT4,189.0 MiB(6.24xtotal /7.11xAttn) atINT2(27.0 MiBGDN state untouched).9B-Hybrid(10Attn +30GDN):2,605.0 MiB->745.9 MiB(3.49xtotal /3.65xAttn) atINT4,405.0 MiB(6.43xtotal /7.11xAttn) atINT2(45.0 MiBGDN state untouched).27B-Hybrid(16Attn +48GDN,d=256):8,384.0 MiB->2,402.9 MiB(3.49xtotal /3.71xAttn) atINT4,1,312.0 MiB(6.39xtotal /7.31xAttn) atINT2(192.0 MiBGDN state untouched).<= 1/30(Int4) and<= 1/6(Int2) acrosshead_size in {64, 80, 96, 112, 128, 192, 256, 512}, bit-identicalappend_batchvsappend_tokenstate,< 1e-5online fused vs materialized equivalence, 20-turn coding-agent prefix-reuse loop,NaN/Infsanitization, and 16-threadstd::thread::scopebarrier stress test. Full methodology and honesty boundaries are documented indocs/HIP-1346-kivi-kv-compression.md(fork verification PR:sonnyman2014/engine#1).Bounty Payout Routing
89Sv5FvnMNRid1bn8y2Ti1vJPSxWxgiW9GSLsPpBnKK10x0440383B02A10BE0Dea587c5b5Fbb622dBf4719D