When quantizing LLM KV caches, use group_size=64 asymmetric scales so outlier Key channels don't spike perplexity.
TL;DR30-second simplified summary▾
Unlocks 4x batch concurrency and 3.2x decode throughput on H100 GPUs.
The 320 KB/Token FP16 HBM Wall.
Why this matters: Quantify why FP16 KV caches starve GPU tensor cores.
For 80 layers, 8 GQA KV heads, 128 head_dim, FP16 uses 2 * 80 * 8 * 128 * 2 = 320 KB/token. Per-group INT4 reduces this to 80 KB/token.
| Choice | What it buys | What it costs |
|---|---|---|
| FP16 KV Cache | Zero quantization error | 320 KB/tok; HBM OOM at small batch |
| Per-Group Asymmetric INT4 | 4x compression (80 KB/tok); isolates outliers | Tiny scale/zero-point metadata + dequant ALU ops |
def quantize_group_int4(x, group_size: int = 64):
g = x.reshape(-1, group_size)
x_min = g.min(axis=1, keepdims=True)
x_max = g.max(axis=1, keepdims=True)
scale = (x_max - x_min) / 15.0 + 1e-8
q = ((g - x_min) / scale).round().clip(0, 15).astype("uint8")
return q, scale, x_min
Outlier-Safe Dequantization & Trade-Offs.
Why this matters: Explain why group-wise scaling preserves perplexity while fusing dequantization.
Fusing INT4->FP16 dequantization inside SRAM during FlashAttention avoids writing dequantized FP16 back to HBM, keeping the kernel compute-light.
- Problem
- How to 4x LLM decode batch concurrency on an 80 GB H100 without degrading long-context reasoning accuracy.
- Naive Solution
- Apply a single per-tensor INT4 scale factor across the entire attention layer.
- Why It Fails
- Key tensors exhibit 10x magnitude outlier channels; a global scale crushes normal values into zero, spiking perplexity.
- Alternatives
- Keep FP16 (low batch), per-tensor INT4 (high perplexity loss), or per-group (64) asymmetric INT4/FP8 quantization.
- Engineering Decision
- Quantize Keys per-group (group_size=64) with asymmetric min/max scale and zero-point, and fuse dequantization into SRAM.
- Trade-Offs
- Adds ~6% scale/zero-point metadata overhead, but achieves 4x memory savings and <0.1% perplexity drift.
Per-tensor INT4 KV quantization spikes perplexity on your 70B model. What is the minimal architectural fix?
public static float[] dequantizeGroup(byte[] q, float scale, float zeroMin) {
float[] out = new float[q.length];
for (int i = 0; i < q.length; i++) {
out[i] = (q[i] & 0x0F) * scale + zeroMin;
}
return out;
}
Practice: Make the trade-off yourself.
Why does per-tensor INT4 quantization degrade LLM accuracy compared to per-group quantization?