kv-quant-attn

Single-token decode attention over an int8 or int4 KV cache, loadable through kernels. The reference baselines are fp32 attention on the dequantized cache (kernel residual at the bf16 rounding floor) and unquantized fp32 attention (quantization error 1.0e-2 at int8, cosine 0.99995). CPU sibling: cpu-attn.

On a device small enough to want a compressed model, the cache and not the weights is what bounds long context: a 27B-class model with 16 full-attention layers, 4 KV heads, and head dimension 256 spends 64 KiB per token in bf16, so its own 262,144-token context is 16 GiB of cache on top of a few GB of weights. Storing the cache at int8 halves that and int4 quarters it, and past a few thousand tokens reading half or a quarter of the bytes also makes decode faster than bf16 attention.

Three cache tanks fill as the context odometer climbs, bf16 crossing the 8 GB board line, beside live latency curves crossing near 4k tokens

The context odometer sweeps to 262,144 tokens: the bf16 cache crosses an 8 GB board's budget while int4 idles at a quarter of it, and the measured decode-latency curves cross near 4,096 tokens, reaching 2.78 ms (bf16 SDPA) against 0.85 ms (int8 kernel) at 65,536.

Usage

import torch
from kernels import get_kernel

kv = get_kernel("phanerozoic/kv-quant-attn", version=1, trust_remote_code=True)

cache = kv.QuantizedKVCache(batch=1, heads_kv=4, head_dim=256,
                            max_tokens=262144, bits=8)

for step in range(n):
    k, v = project_kv(hidden)          # [B, Hkv, D] bf16
    cache.append(k, v)
    out = cache.attend(q)              # q is [B, Hq, D] bf16 -> [B, Hq, D]

version selects the release branch; trust_remote_code is required by kernels for publishers without the trusted-publisher mark. One cache object per layer.

API

Symbol Purpose
QuantizedKVCache(batch, heads_kv, head_dim, max_tokens, bits) per-layer cache, bits 8 or 4
.append(k, v) quantize one step and store it
.attend(q, sm_scale) one decode step over the stored cache
.dequantize() reconstruct in fp32, for tests and reference paths
.nbytes bytes held, scales included
append_kv(...) / decode_attn(...) the raw ops, if you own the buffers
cache_bytes(layers, heads_kv, head_dim, tokens, bits) footprint arithmetic

Method

Every key and value row carries its own absmax scale, computed when the row is written, so dequantization needs one scalar per row, appending never rewrites earlier rows, and no calibration pass is required. Decode is flash-decoding over the quantized cache: a block owns a (batch, KV head) pair, so under grouped-query attention all query heads sharing a KV head are resolved from one read of it; warps carry private (max, sumexp, accumulator) state with no barrier or atomic in the loop; and the sequence is split across blocks with partials merged in a fixed order, so the result is bitwise reproducible.

Measured

Bonsai 27B full-attention geometry (Hq = 24, Hkv = 4, D = 256, batch 1), against F.scaled_dot_product_attention on an unquantized bf16 cache of the same contents:

context bf16 SDPA int8 int4 int8 vs bf16
1,024 0.0419 ms 0.1226 ms 0.1277 ms 0.34x
4,096 0.1636 ms 0.1309 ms 0.1351 ms 1.25x
16,384 0.6872 ms 0.2629 ms 0.2301 ms 2.61x
65,536 3.0750 ms 0.8923 ms 0.8660 ms 3.45x

The crossover is near 4,000 tokens; below it the split-and-combine overhead exceeds the read it saves.

Cache footprint for the same model, scales included:

tokens bf16 int8 int4
8,192 0.50 GiB 0.25 GiB 0.13 GiB
32,768 2.00 GiB 1.02 GiB 0.52 GiB
131,072 8.00 GiB 4.06 GiB 2.06 GiB
262,144 16.00 GiB 8.12 GiB 4.12 GiB

Correctness

  • Against fp32 attention on the dequantized cache, which separates kernel correctness from quantization error, the residual is 1.6e-3 to 1.7e-3 relative across both bit widths and every shape tested (bf16 output rounding).
  • Quantization error against unquantized fp32 attention is 1.0e-2 relative at int8 (cosine 0.99995) and 1.8e-1 at int4 (cosine 0.984); int4 is a real approximation.
  • Grouped-query ratios 1:1 through 8:1 match a replicated-head reference; ragged lengths verified down to one token; appends round-trip within half a quantization step.
  • Deterministic: no atomics and a fixed combine order, so repeated decodes are bitwise identical.

Requirements and limits

  • NVIDIA GPU with compute capability 8.0+.
  • head_dim at most 256 and a multiple of 32; grouped-query ratio at most 8; bf16 queries, keys and values.
  • Decode only; prefill wants a tiled attention kernel, then hand the results to append.
  • Slower below roughly 4,000 tokens; route short contexts to a dense path.
  • Per-row absmax only: no group-wise scaling or outlier channels, which would narrow the int4 gap at some metadata cost.

References

Dao et al., "FlashAttention-2" (2023) and the flash-decoding split; Hooper et al., "KVQuant" (2024); Liu et al., "KIVI" (2024); Ainslie et al., "GQA" (2023).

License

Apache-2.0.

Downloads last month
2
apache-2.0
Supported hardwares new
CUDA
8.08.68.99.010.012.0
GPU
B300
288GB
NVIDIA SXM
B200
192GB
NVIDIA SXM
H200
141GB
NVIDIA SXM
H100
80GB
GPU
H800
80GB
GPU
H20
96GB
GPU
L40s
48GB
GPU
L40
48GB
GPU
L20
48GB
GPU
L4
24GB
DGX Spark
GB10
128GB
GPU
RTX PRO 6000 WS
96GB
GPU
RTX PRO 6000 Max-Q
96GB
GPU
RTX PRO 5000
48GB
GPU
RTX PRO 4500 WS
32GB
GPU
RTX PRO 4000
24GB
GPU
RTX PRO 4000 SFF
24GB
GPU
RTX PRO 2000
16GB
GPU
RTX 6000 Ada
48GB
GPU
RTX 5880 Ada
48GB
RTX
RTX 5000 Ada
32GB
GPU
RTX 4500 Ada
24GB
RTX
RTX 4000 Ada
20GB
RTX
RTX 4000 SFF Ada
20GB
GPU
RTX 3500 Ada Mobile
12GB
GPU
RTX 2000 Ada
16GB
GPU
RTX A6000
48GB
GPU
RTX A5000
8GB
GPU
RTX A5000 Max-Q
16GB
GPU
RTX A5000 Mobile
16GB
GPU
RTX A4000
16GB
GPU
RTX A4000 Max-Q
8GB
GPU
RTX A4000 Mobile
8GB
GPU
RTX A3000 Mobile
6GB
GPU
RTX A2000
6GB
GPU
RTX A2000 Embedded
4GB
GPU
RTX A2000 Max-Q
4GB
GPU
RTX A2000 Mobile
4GB
GPU
A800
40GB
GPU
A100
80GB
GPU
A40
48GB
GPU
A30
24GB
GPU
A10
24GB
GPU
A2
16GB
RTX
RTX 5090
32GB
RTX
RTX 5090 D
32GB
RTX
RTX 5090 Mobile
24GB
RTX
RTX 5080
16GB
RTX
RTX 5080 Mobile
16GB
RTX
RTX 5070
12GB
RTX
RTX 5070 Mobile
8GB
RTX
RTX 5070 Ti
16GB
RTX
RTX 5070 Ti Mobile
12GB
RTX
RTX 5060 Ti
16GB
RTX
RTX 5060
8GB
RTX
RTX 5060 Mobile
8GB
RTX
RTX 5050
8GB
RTX
RTX 5050 Mobile
8GB
RTX
RTX 4090
24GB
RTX
RTX 4090D
24GB
RTX
RTX 4090 Mobile
16GB
RTX
RTX 4080 SUPER
16GB
RTX
RTX 4080
16GB
RTX
RTX 4080 Mobile
12GB
RTX
RTX 4070
12GB
RTX
RTX 4070 Mobile
8GB
RTX
RTX 4070 Ti
12GB
RTX
RTX 4070 Super
12GB
RTX
RTX 4070 Ti Super
16GB
RTX
RTX 4060
8GB
RTX
RTX 4060 Ti
8GB
RTX
RTX 4090 Laptop
16GB
RTX
RTX 4080 Laptop
12GB
RTX
RTX 4070 Laptop
8GB
RTX
RTX 4060 Laptop
8GB
RTX
RTX 4050 Laptop
6GB
RTX
RTX 3090
24GB
RTX
RTX 3090 Ti
24GB
RTX
RTX 3080
12GB
RTX
RTX 3080 Ti
12GB
RTX
RTX 3080 Mobile
16GB
RTX
RTX 3070
8GB
RTX
RTX 3070 Ti
8GB
RTX
RTX 3070 Ti Mobile
8GB
RTX
RTX 3060 Ti
8GB
RTX
RTX 3060
12GB
RTX
RTX 3060 Mobile
6GB
RTX
RTX 3050 Mobile
4GB
GPU
RTX 2050 Mobile
4GB
Jetson
Jetson AGX Orin 64GB
64GB
Jetson
Jetson AGX Orin 32GB
32GB
Jetson
Jetson Orin NX 16GB
16GB
Jetson
Jetson Orin NX 8GB
8GB
Jetson
Jetson Orin Nano 8GB
8GB
Jetson
Jetson Orin Nano 4GB
4GB
OS
linux
Arch
x86_64
Kernel Builder
19aaa64