Vramkit: lossless weight + KV-cache compression for LLM inference, drop-in PyTorch for ROCm - seeking MI300X/Radeon validation

Hi AMD Developer Community,

Sharing vramkit — an open-source toolkit that shrinks the two things that eat VRAM during LLM
inference: model weights and the KV-cache. Posting here because long-context inference memory is a
known ROCm pain point, and this is pure PyTorch + numpy, so it drops straight into a ROCm stack.
Every number below comes from a runnable benchmark with a bit-exact roundtrip assertion.

== Weights - lossless, beats zstd ==
A from-scratch 32-bit range coder with a 2-D adaptive context (each 4-bit weight conditioned on its
left+up neighbour - structure zstd/xz can’t see). Across EVERY weight-tensor type of Qwen2.5-0.5B
(q/k/v/o-proj, mlp gate/down): mean 2.92 bits/weight, −27% vs raw 4-bit, lossless, beating zstd-19
on 4 of 5 (best tensor: o_proj at −33%). Sits on the 2-D entropy floor.

== KV-cache — drop-in, device-agnostic ==
from vramkit.kvquant import quantize_kv, dequantize_kv
packed = quantize_kv(key, mode=“outlier-int4”) # ~3.6x smaller than fp16
key_hat = dequantize_kv(packed)
Per-token int8 = 1.97x at cos-sim 0.9998. Naive int4 loses quality on KV’s outlier channels; keeping
just 4 of 128 channels in int8 (“outlier-aware int4”) recovers cos-sim 0.958 → 0.999 at ~int4 memory
(3.66x). Runs on ROCm/CUDA/MPS/CPU since it’s plain PyTorch.

== Why it matters on AMD ==
On ROCm, decode throughput degrades hard past ~64k context and FP8-KV is still maturing. KV-cache
memory is the lever: 2-3.6x smaller KV = more concurrent requests or longer context per Instinct/
Radeon GPU, directly on $/token.

== Honest limits + the ask ==
I built and benchmarked this entirely on an Apple-Silicon MacBook (+ torch CPU). The logic is
ROCm-ready but I have NOT yet benchmarked it on Instinct/Radeon. I’d love to collaborate with anyone
who has MI300X / W7900 time to:

  1. measure real end-to-end VRAM + throughput at 64k-256k context with KV compression on,
  2. validate KV quality as perplexity on a real model (via vLLM-ROCm),
  3. prototype a HIP/Triton dequant kernel so the compressed form is used live in attention.

Repo (MIT): GitHub - SRKRZ23/vramkit: Measurable, lossless-verified VRAM compression for LLM inference: a weight codec (-22% vs 4-bit, beats zstd) + KV-cache compression (int8 ~2x near-lossless, outlier-aware int4 at cos-sim 0.999). Built on a MacBook. · GitHub

Question for the room: at long context on ROCm, what’s hurting more right now - KV-cache memory,
attention-backend selection, or FP8 accuracy? That’s where I’d point the next kernel.

Thanks - happy to answer anything.

Quick update: I validated the KV compression as real perplexity on a MacBook (Qwen2.5-0.5B,
compression applied to every layer’s K/V).

Result:
int8 KV: +0.02% perplexity - end-task near-lossless, and 2x smaller. This is the one to ship.
outlier-int4: +9.5% perplexity - honest correction: the cos-sim proxy (0.999) overstated int4;
perplexity is the metric that counts, and it’s still a work-in-progress (per-head/grouped scales

  • the range coder are the next levers).
    Reproducible: bench/validate_ppl_mac.py. Still looking for MI300X/Radeon time to measure this
    end-to-end at 64k-256k context with the compressed cache live.

Update — I validated the KV-cache claim as real end-task perplexity on a MacBook (Qwen2.5-0.5B,
KV compressed on every layer, 4 windows of diverse prose), and it sharpened the headline.

The quant AXIS is the whole game. Keys carry persistent per-channel outliers, so a per-token scale
gets dominated by them; keys want a per-CHANNEL scale, values stay per-token:

int8 per-token K&V (naive) 2.00x +4.6% perplexity
int8 per-CHANNEL K + per-token V 2.00x +0.1% (near-lossless)
int8 K(per-chan) + int4 V(per-tok) 2.67x +1.4%
int4 per-channel K + per-token V 4.00x +25.6%

So a near-lossless 2x on KV (2.67x with int4 values) for ROCm long-context decode, pure PyTorch.
int4 is a values-only lever here. Streaming decode needs KIVI-style token-group quant for the
per-channel key scales — that plus a ROCm dequant kernel are the open work. Still seeking MI300X/
Radeon time to validate on a 7B+ model and co-build the kernel.
Repro + code: GitHub - SRKRZ23/vramkit: VRAM compression for LLM inference, measured on a MacBook: lossless weight codec (-33% vs 4-bit on real Qwen weights, beats zstd) + drop-in PyTorch KV-cache compression. Key finding: quantize KV keys per-CHANNEL (not per-token) -> int8 KV near-lossless, +0.1% real perplexity at 2x. ROCm/CUDA/MPS/CPU. · GitHub (bench/validate_ppl_mac.py).

The study loooks good.

Awesome job, and really nice to see you post the perplexity correction instead of burying it. For the MI300X time, you can get some yourself: the AMD AI Developer Program offers $100 in AMD Developer Cloud credits for MI300X, approved per use case. You request them under Member Perks, and your GitHub works as the public profile. One thing I noticed: validate_ppl_mac.py hooks k_proj, so it quantizes keys before RoPE, while the transformers cache stores them after RoPE. KVQuant quantizes keys pre-RoPE on purpose, so your result likely holds if you cache pre-RoPE keys and rotate after dequant, but a post-RoPE run would show what a standard cache gets.

Thanks Hakob — you’re right, and that was worth checking. The k_proj hook quantizes keys before RoPE, while past_key_values.update receives rotated keys. I added --keys pre|post|both: the post path wraps the model’s apply_rotary_pos_emb, so it quantizes exactly what the cache stores (with a call-count check so a no-op patch can’t look lossless).

Qwen2.5-0.5B, 4 windows of real prose:

  • int8 per-channel K + per-token V: +0.10% pre-RoPE, +0.13% post-RoPE → the 2× result holds for a standard drop-in cache.
  • int4 per-channel K: +25.6% pre vs +27.4% post. Pre-RoPE helps a little at low bits, as in KVQuant, but int4 keys don’t hold up either way.

Commit: KV validation: measure keys pre- and post-RoPE · SRKRZ23/vramkit@6eafaec · GitHub

And thanks for the pointer on the Developer Program credits. I’ll request MI300X time under Member Perks to validate this on real hardware.

1 Like

Thanks Raj! A follow-up check (keys measured after RoPE, as a standard cache stores them) is in the thread above. The int8 result held.

The per-channel-K / per-token-V result is the right finding, and it matches what the KV-quantization literature converges on — keys have persistent channel-wise outliers from the RoPE rotation plus a handful of “sink” channels, so a per-token scale gets dominated by whichever channel happens to spike, while values are much closer to iid per token. Your 2.00x at +0.1% PPL is the number I would ship.

Three things that will bite when you move this from MacBook MPS to ROCm, in the order they will hit you:

1. torch.bfloat16 and fp16 differ on ROCm in ways that change your baseline. CDNA (MI300X) has fast bf16 but fp16 with fp32 accumulate is not always the faster path, and on RDNA3 the situation flips. Pin the baseline explicitly so your +0.1% is not measuring a dtype change:

torch.backends.cuda.matmul.allow_tf32 = False   # keep it off, or your ref is not fp16
model = AutoModelForCausalLM.from_pretrained(name, torch_dtype=torch.bfloat16).to("cuda")
torch.cuda.synchronize()

Run the PPL reference and the quantized run in the same process with the same seed and the same window set, otherwise the delta is noise.

2. Your dequant path is the actual benchmark, not the compression ratio. A per-channel int8 dequant that materializes key_hat as a fresh contiguous fp16 tensor adds a full copy of the KV-cache in peak memory — at 64k context on a 70B that can be gigabytes, and it will erase the win you measured. The honest metric is peak torch.cuda.max_memory_allocated() across a decode step, not steady-state size:

torch.cuda.reset_peak_memory_stats()
out = model.generate(**inputs, max_new_tokens=256)
print(torch.cuda.max_memory_allocated() / 2**30, "GiB peak")

If peak barely moves, you have a bandwidth win (fewer bytes read per step) but not a capacity win, and those are different pitches.

3. Decode at long context is bandwidth-bound, so measure tok/s at fixed batch, not just VRAM. On MI300X the interesting axis is concurrent sequences: 2x smaller KV means 2x the batch before you hit the capacity wall, and that is where the $/token story actually lives. Measure max_num_seqs at a fixed 32k context with compression on and off.

For the HIP/Triton dequant kernel, start with a fused dequant-in-attention rather than a standalone kernel — a separate dequant pass writes the fp16 cache back to HBM, which is exactly the traffic you are trying to avoid. A Triton kernel that loads the packed int8, applies the per-channel scale, and feeds tl.dot in registers keeps it in the compute pipe.

If you want MI300X time and do not have a box, AMD Developer Cloud hands out credits through the AI Developer Program — that is the cheapest route to a real 64k-context number, and it is the one claim in your post that is still unproven.

Follow-up with numbers, since your point 2 deserved a measurement rather than a promise.

I built a decode-step memory benchmark (real MPS allocations, per-channel-K / per-token-V, SDPA attention) on a 7B-GQA config (28 layers, 4 KV-heads, d=128). Measuring the KV footprint of one decode step, not the packed ratio:

ctx 32k: fp16 1.75 GiB → int8 resident 0.88 GiB (1.98x); streamed decode-step footprint 0.94 GiB (1.85x less than fp16)
ctx 64k: fp16 3.50 GiB → int8 resident 1.76 GiB (1.98x); streamed decode-step footprint 1.89 GiB (1.85x less than fp16)

Your warning is exactly right and now quantified: if I dequantise the whole cache up front, peak goes to 2.63 / 5.26 GiB - above fp16 - and the win is gone. It only holds if I dequantise one layer at a time; the streamed transient is a single layer (64 MiB at 32k, 128 MiB at 64k). So at a fixed VRAM budget that’s ~1.85x more concurrent sequences at 64k.

Honest caveat: on Apple MPS the dequant is a ~3.7-4.4x compute overhead per step, so this proves the capacity win, not a speed-up. The throughput / $-per-token win is bandwidth-bound and I won’t claim it until I have it on a memory-bound GPU - the MI300X run, plus the fused dequant-in-attention kernel you suggested (clearly right vs a standalone pass writing fp16 back to HBM), is the next step via the AMD Dev Program credits.

Benchmark + updated writeup are in the repo (bench/mem_decode_mac.py). Thanks again - this review materially sharpened the claim.