r/llamacpp • • 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):

TaskMetricBF16 Basev2 (data-free)v2-geckoQ4_K_M (llama.cpp)
MMLU (14,042)acc (group)70.63%68.06%69.02%69.40%
GSM8K (1,319)strict-match85.90%83.93%85.06%84.84%
GSM8K (1,319)flexible-extract83.85%17.59%21.83%21.08%
HellaSwag (10,042)acc_norm69.05%67.19%67.36%68.39%
IFEval (541)prompt_strict57.86%45.47%46.95%—

Observations:

  • v2-gecko vs Q4_K_M is a statistical tie across all measured tasks (every gap <= 1.6 sigma). Both improve over data-free v2 on every metric.
  • The GSM8K flexible-extract drop (83.85% -> 17.6–21.8%) occurs identically in Q4_K_M and both phi-lattice variants, 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):

CheckpointSQNR6-text PPLHellaSwag acc_normMMLUGSM8K strictIFEval prompt_strict
BF16 reference—90.8239.29%25.08%2.43%24.21%
v2 (1-stream, ~4.5 bpw)19.48 dB161.2737.93%25.94%0.61%17.74%
Q4_K_M (~4.5 bpw)26.80 dB99.3039.12%24.88%1.36%17.56%
v2 + cashback (~8.5 bpw)39.17 dB90.5939.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 configEmbed sizeArchive sizeSQNR vs BF166-text PPLMMLUHellaSwag acc_norm
BF16 (original gecko)741.9 MiB2,583.2 MiB—7.39569.02% +/- 0.3667.36%
1-stream 4-bit (no residual)197.1 MiB2,038.4 MiB19.68 dB7.712 (degraded)——
2-stream cashback embeds394.1 MiB2,235.5 MiB39.28 dB7.37068.92% +/- 0.3767.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**:** on Qwen3-4B (tg128, greedy), native llama-bench Q4_K_M reaches 85.02 tok/s (2,990 MiB VRAM). Our PyTorch unrolled_bf16 mode runs at 23.76 tok/s (8,254 MiB VRAM), eager low_vram_4bit at 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,920 vs 5,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 BF16 nn.Embedding at load time (~2.58 GiB resident).
  • Runtime support: PyTorch/Triton loaders only; no GGUF/llama.cpp integration.

Links (archives, loaders, raw eval JSONs)

Feedback on the evaluation methodology or Triton GEMV kernel optimization for 4-bit LUT dequantization is welcome.

1 Upvotes

3 comments sorted by

2

u/ionsago 1d ago

Really you should be basing off bf16 measurements of Qwen3 4B.

1

u/reqqddit 1d ago

Yeah, on Qwen3-4B I only tested cashback on the embeddings so far, not the full model. I'm running the full Qwen3-4B benchmarks right now and will update the post once they finish.

1

u/ionsago 1d ago

The next thing is imatrix because really most public quants use it so a comparison without calibrations would not paint the whole picture.