Skip to main content
← CONCEPTS / AI-MACHINE-LEARNING
An end-to-end domain learning journey

LLM KV-Cache Quantization &
FP8/INT4 Memory Compression.

FP16 KV caches consume 320 KB/token on 70B models, exhausting 80 GB HBM at batch=20; per-group asymmetric INT4/FP8 quantization compresses KV memory 4x with <0.1% perplexity drift.
Start the journey2 sections· Last modified Dec 2, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Autoregressive KV Caching
Stores past Key/Value projections to avoid recomputing attention for prior tokens.
GQA (Grouped-Query Attention)
Shares K/V heads across query groups to shrink KV footprint.
Full walkthrough · 2 stages
01
STAGE 01intermediate

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.

Before · FP16 KV Cache (320 KB/tok)
After · Per-Group INT4/FP8 (80 KB/tok)
ChoiceWhat it buysWhat it costs
FP16 KV CacheZero quantization error320 KB/tok; HBM OOM at small batch
Per-Group Asymmetric INT44x compression (80 KB/tok); isolates outliersTiny 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
02
STAGE 02advanced

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.

Engineering Decision Framework
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.
Decision Exercise · Make the Call

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 · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why does per-tensor INT4 quantization degrade LLM accuracy compared to per-group quantization?

Recommended Learning Flow