r/llamacpp • u/reqqddit • 1d ago
My custom 4-bit quantization method from scratch
I have been developing and benchmarking a custom 4-bit non-uniform weight quantization format(phi-lattice v2: 15-level golden-ratio codebook, uint8 nibble packing, fp16 per-group scales at group_size=64) across dense LLMs, an MoE model, and an embedding model. All checkpoints, standalone PyTorch/Triton loaders, and raw lm-eval JSONs are public on Hugging Face.
Below is a factual summary of what worked, what regressed, and where the current implementation still lags behind native runtimes.
1. Same-engine comparison vs llama.cpp Q4_K_M (Qwen3-4B-Instruct-2507)
To remove cross-engine variance, Q4_K_M (unsloth GGUF, 2,382 MiB) was dequantized to bf16 via the transformers GGUF loader and evaluated with the exact same lm-eval HFLM pipeline (batch_size=8, CUDA L4) as BF16, data-free v2, and v2-gecko (block-64 calibrated Hessian pass, 198 s quantization time on L4, 12.52% exact zeros):
| Task | Metric | BF16 Base | v2 (data-free) | v2-gecko | Q4_K_M (llama.cpp) |
|---|---|---|---|---|---|
| MMLU (14,042) | acc (group) | 70.63% | 68.06% | 69.02% | 69.40% |
| GSM8K (1,319) | strict-match | 85.90% | 83.93% | 85.06% | 84.84% |
| GSM8K (1,319) | flexible-extract | 83.85% | 17.59% | 21.83% | 21.08% |
| HellaSwag (10,042) | acc_norm | 69.05% | 67.19% | 67.36% | 68.39% |
| IFEval (541) | prompt_strict | 57.86% | 45.47% | 46.95% | — |
Observations:
v2-geckovsQ4_K_Mis a statistical tie across all measured tasks (every gap <= 1.6 sigma). Both improve over data-freev2on every metric.- The
GSM8K flexible-extractdrop (83.85% -> 17.6–21.8%) occurs identically inQ4_K_Mand bothphi-latticevariants, indicating a 4-bit quantization artifact on this model rather than a codebook-specific issue.
2. Negative result on embedding models (why calibration hurt retrieval)
Applying the same calibrated block-Hessian pass (v2-gecko) to google/embeddinggemma-2 produced a split result:
- STS-B test (similarity, 768d): improved slightly (
0.8670 -> 0.8681). - SciFact nDCG@10 (retrieval, 768d): regressed (
0.8398 -> 0.8316), even with mixed STS + SciFact calibration prompts.
Because retrieval degraded, v2-gecko was not promoted for embedding models.
3. Two-stream residual ("cashback") — data-free recovery to BF16 parity
Instead of calibration, storing the exact first-pass quantization error R = W_bf16 - W_v2 as a second 4-bit nibble stream (W = unpack(stream1) + unpack(stream2), same codebook, data-free) raises weight SQNR from ~19.5 dB to ~39.2 dB at ~8.5 bpw effective.
Scope note: full-model two-stream cashback (all quantized linear layers) has only been built and fully evaluated on the Gemma models (gemma-3-270m-it and embeddinggemma-2). On Qwen3-4B, cashback was applied only to the token embeddings (the 252 linear layers remain single-stream v2-gecko), and only MMLU and HellaSwag were re-run on that archive (GSM8K and IFEval were not re-evaluated).
A. Full-model two-stream (gemma-3-270m-it and embeddinggemma-2)
At 270M, single-stream 4-bit quantization causes heavy degradation. Comparing v2, Q4_K_M, and full v2 + cashback (421.9 MiB, all 126 linear layers) on unsloth/gemma-3-270m-it (same L4 lm-eval engine):
| Checkpoint | SQNR | 6-text PPL | HellaSwag acc_norm | MMLU | GSM8K strict | IFEval prompt_strict |
|---|---|---|---|---|---|---|
| BF16 reference | — | 90.82 | 39.29% | 25.08% | 2.43% | 24.21% |
v2 (1-stream, ~4.5 bpw) | 19.48 dB | 161.27 | 37.93% | 25.94% | 0.61% | 17.74% |
Q4_K_M (~4.5 bpw) | 26.80 dB | 99.30 | 39.12% | 24.88% | 1.36% | 17.56% |
v2 + cashback (~8.5 bpw) | 39.17 dB | 90.59 | 39.31% | 24.93% | 2.20% | 23.11% |
Note: Q4_K_M (~4.5 bpw) and v2 + cashback (~8.5 bpw) sit in different size tiers; the point of the comparison is verifying that the data-free residual stream closes the quantization gap to BF16 parity. The same holds for full-model two-stream embeddinggemma-2 (217 text-tower layers, 659.5 MiB, 39.21 dB SQNR): STS-768 0.8745 vs BF16 0.8743, SciFact nDCG@10 0.8552 vs BF16 0.8569.
B. Embeddings-only cashback (Qwen3-4B-Instruct-2507)
Keeping all 252 v2-gecko linear layers byte-identical and applying two-stream cashback only to the 741.9 MiB BF16 embed_tokens (tied with lm_head):
| Embedding config | Embed size | Archive size | SQNR vs BF16 | 6-text PPL | MMLU | HellaSwag acc_norm |
|---|---|---|---|---|---|---|
| BF16 (original gecko) | 741.9 MiB | 2,583.2 MiB | — | 7.395 | 69.02% +/- 0.36 | 67.36% |
| 1-stream 4-bit (no residual) | 197.1 MiB | 2,038.4 MiB | 19.68 dB | 7.712 (degraded) | — | — |
| 2-stream cashback embeds | 394.1 MiB | 2,235.5 MiB | 39.28 dB | 7.370 | 68.92% +/- 0.37 | 67.39% |
A single 4-bit stream on embeddings measurably degrades perplexity; adding the residual stream eliminates the penalty on the measured tasks (MMLU and HellaSwag tie within 1 stderr; GSM8K/IFEval not re-run) and reduces total archive size by 348 MiB (below the 2,382 MiB Q4_K_M GGUF).
4. Scaling to 35B MoE (Qwen3.6-35B-A3B)
Packing all 80 3D routed expert banks (32.21B weights) + 2D projections (1.72B weights) in 4-bit phi-lattice v2 (keeping routers, norms, and embeddings in BF16/FP32) reduces Qwen3.6-35B-A3B from 65.39 GiB BF16 to 18.98 GiB resident (19.33 GiB peak during 512-token generation on a single 24 GB NVIDIA L4), with 19.63 dB SQNR and 13.11% exact zeros. Only the 8 active experts per token are dequantized during forward.
5. Current limitations (measured on L4)
- Decode throughput lags
llama.cpp**:** onQwen3-4B(tg128, greedy), nativellama-benchQ4_K_M reaches 85.02 tok/s (2,990 MiB VRAM). Our PyTorchunrolled_bf16mode runs at 23.76 tok/s (8,254 MiB VRAM), eagerlow_vram_4bitat 1.65 tok/s (3,516 MiB VRAM), and our initial Triton LUT-GEMM kernel (phi_lut_gemm.py) at ~16.2 tok/s (136.8 GB/s). Prefill (pp512) is within 7% (4,920vs5,288 tok/s). The decode bottleneck is kernel/loader engineering rather than archive byte count. - Resident VRAM of cashback embeddings: in
low_vram_4bit, cashback embeddings save 348 MiB on disk/download, but are currently unpacked into BF16nn.Embeddingat load time (~2.58 GiB resident). - Runtime support: PyTorch/Triton loaders only; no GGUF/llama.cpp integration.
Links (archives, loaders, raw eval JSONs)
- Qwen3-4B
v2-gecko(+ cashback embeddings default): https://huggingface.co/ununununununununununununununununununun/qwen3-4b-instruct-2507-phi-lattice-v2-gecko - Qwen3-4B
Q4_K_Msame-engine benchmark & perf JSONs: https://huggingface.co/ununununununununununununununununununun/qwen3-4b-instruct-2507-q4km-bench - Gemma-3-270M
v2 + cashback(with Q4_K_M comparison): https://huggingface.co/ununununununununununununununununununun/gemma-3-270m-it-phi-lattice-v2-cashback - EmbeddingGemma-2
v2 + cashback: https://huggingface.co/ununununununununununununununununununun/embeddinggemma-2-phi-lattice-v2-cashback - Qwen3.6-35B-A3B
v2(24 GB GPU MoE): https://huggingface.co/ununununununununununununununununununun/qwen3.6-35b-a3b-phi-lattice-v2 - Qwen2.5-Coder-7B
v2: https://huggingface.co/ununununununununununununununununununun/qwen2.5-coder-7b-instruct-phi-lattice-v2
Feedback on the evaluation methodology or Triton GEMV kernel optimization for 4-bit LUT dequantization is welcome.
2
u/ionsago 1d ago
Really you should be basing off bf16 measurements of Qwen3 4B.