Skip to content

ggml: imatrix-aware NVFP4 quantization (scale search) + wire NVFP4 ftype - #25153

Open
avifenesh wants to merge 4 commits into
ggml-org:masterfrom
avifenesh:nvfp4-imatrix-scale-search
Open

avifenesh wants to merge 4 commits into
ggml-org:masterfrom
avifenesh:nvfp4-imatrix-scale-search

Conversation

@avifenesh

@avifenesh avifenesh commented Jun 30, 2026 •

Copy link
Copy Markdown

Overview

In short, this PR improves NVFP4 quantization quality by replacing the fixed amax/6
per-sub-block scale with a small scale search that minimizes (optionally imatrix-weighted)
reconstruction error, and it wires up the NVFP4 ftype so llama-quantize can actually
produce NVFP4 GGUFs.

Today quantize_nvfp4 does GGML_UNUSED(quant_weights) and quantizes each 16-element
sub-block with a fixed scale amax/6. The imatrix is ignored, and LLAMA_FTYPE_MOSTLY_NVFP4
is defined but unreferenced - there is no CLI type, no ftype->ggml_type mapping, and no
per-tensor selection - so NVFP4 GGUFs can only come from external/convert tools, not from
llama-quantize.

This change:

  • adds quantize_row_nvfp4_core(x, y, k, quant_weights, search); the existing
    quantize_row_nvfp4_ref becomes core(..., NULL, 0), so there is one encoder, not two.
  • for each sub-block, searches UE4M3 scale codes in [base-search, base+search] around the
    amax/6 baseline and keeps the code with the lowest reconstruction error; with an imatrix
    the error is importance-weighted per input column. The winning E2M1 nibbles are cached
    during the search and reused for packing.
  • wires LLAMA_FTYPE_MOSTLY_NVFP4: CLI type in quantize.cpp, ftype->GGML_TYPE_NVFP4
    mapping, per-tensor selection (2D weights -> NVFP4, output/embed/1D -> Q8_0), and a
    tensor_type_fallback case (ne[0] % 64 != 0 -> Q8_0) so odd-shaped tensors do not abort
    the run.

The output is ordinary NVFP4, so the result runs unmodified on the existing NVFP4 kernels.
With search > 0 and no imatrix the scale search alone already improves quality; an imatrix
adds further on top.

Results below are on Qwen3.5-9B, Gemma-4-26B-A4B, and Qwen3.6-27B (+ MTP), the last being
my own daily-driver model and including MTP speculative-decoding acceptance.

Benchmark Setup

  • Base: upstream/master at c818263f2
  • Patch commit: c73069749
  • Hardware: NVIDIA GeForce RTX 5090 Laptop GPU, 24463 MiB VRAM
  • Driver: 595.71.05
  • Build: CUDA 13.1, build/bin/{llama-quantize,llama-imatrix,llama-perplexity}
  • Quantization: source f16 GGUF -> llama-quantize ... NVFP4
  • imatrix: llama-imatrix over a calibration corpus, 40 chunks
  • Perplexity: llama-perplexity -f wiki.test.raw --chunks <N>
  • Three arms compared:
    • RTN: legacy amax/6 scale (search window 0, no imatrix) - baseline
    • scale-search: scale search, no imatrix
    • scale-search + imatrix: scale search using the imatrix

Results

Lower perplexity is better. All three GGUFs for a given model are identical in size (the
change is in scale selection only, not the format).

Qwen3.5-9B (wikitext-2 test, 60 chunks, imatrix on wiki.train)

arm PPL vs RTN
RTN (amax/6) 8.497 +/- 0.180 -
scale-search 8.247 +/- 0.172 -2.9%
scale-search + imatrix 7.984 +/- 0.165 -6.0%

Gemma-4-26B-A4B (60 chunks, imatrix built on a code corpus)

Cross-domain check: the imatrix was built on code yet improves both eval corpora.

corpus RTN scale-search scale-search + imatrix
wikitext-2 5.888 +/- 0.109 5.827 (-1.0%) 5.675 +/- 0.103 (-3.6%)
code 1.933 +/- 0.024 1.909 (-1.2%) 1.885 +/- 0.023 (-2.5%)

Qwen3.6-27B + MTP (hybrid SSM/attn, daily-driver model; 40 chunks, imatrix on code)

corpus RTN scale-search scale-search + imatrix
wikitext-2 6.123 +/- 0.149 6.128 (+0.1%) 6.167 (+0.7%)
code 1.950 +/- 0.031 1.925 (-1.3%) 1.903 +/- 0.030 (-2.4%)

Here the imatrix was built on code and the wikitext numbers are off-domain: scale-search is
roughly flat on wikitext and helps on code, while the code-built imatrix improves the code
eval (-2.4%) but slightly regresses wikitext (+0.7%). So scale-search is domain-neutral and
safe on its own; the imatrix benefit is largest when the calibration domain matches the
target workload. (Unlike Gemma above, where a code imatrix happened to also help wikitext,
this hybrid model does not generalize the code imatrix to wikitext.)

MTP acceptance, same draft model in both arms (so the comparison isolates the requantized
trunk), code/reasoning prompts, greedy decoding:

arm draft_n accepted accept_rate
RTN (amax/6) 525 343 65.3%
scale-search + imatrix 642 489 76.2%

On the matching (code) domain the better-quantized trunk raises MTP acceptance by ~11 points,
which directly reduces speculative verification passes.

Notes on the scale search

  • Window width NVFP4_SCALE_SEARCH = 12. The optimal code sits within a few codes of
    amax/6, reoriented positive (the non-uniform E2M1 codes {..6,8,12} mean scaling up to clamp
    an outlier and fit the bulk is often optimal). Narrowing to +/-8 measurably regressed
    9B search-only PPL (8.240 -> 8.287), so the margin is kept.
  • An iterative coordinate-descent solve (make_qkx-style: assign nibbles -> weighted-LSQ scale
    -> snap to UE4M3 -> repeat) was prototyped and performed worse than the brute-force window;
    The non-uniform code spacing creates local boundaries that the dense window steps over.

Validation

  • git diff --check
  • cmake --build build --target llama-quantize llama-perplexity test-quant-type-selection -j 8
  • Verified the refactor is byte-identical to the pre-refactor implementation for both the
    scale-search and scale-search+imatrix outputs (so the measured PPL is unchanged by the
    cleanup).
  • A/B baseline: a local build with the search window set to 0 reproduces the legacy amax/6
    reference exactly (this is what the RTN arm above was quantized with).
  • test-quant-type-selection: the pre-existing MXFP4_MOE/IQ snapshot diffs are present on
    master without this change; NVFP4 sections are unaffected.

AI usage disclosure: YES - Claude opus 4.8 xhigh was used to write this code. It assisted with code writing, local code review, benchmarking, cleanup, and preparing the MD-styled parts of this PR, like the tables.

While an LLM wrote the code, I did the design, the research, and the codebase reading, I directed, reviewed and steered during development, designed the benchmarks, reviewed the code more than once and instructed changes, and I'm doing the final signing on this code and PR.
I own this code, and I'm responsible for the output of the tools I use.

quantize_nvfp4 previously did GGML_UNUSED(quant_weights) and used a fixed
amax/6 per-sub-block scale. Add quantize_row_nvfp4_impl: search a window of
UE4M3 scale codes around amax/6, minimizing (optionally imatrix-weighted)
sub-block reconstruction error. Output format unchanged -> runs on stock
NVFP4 kernels. NVFP4_NO_SEARCH=1 forces legacy amax/6 for A/B.

Also wire LLAMA_FTYPE_MOSTLY_NVFP4 (was a dead enum, value 39, unreferenced):
add to llama-quantize CLI table, ftype->ggml_type map, and per-tensor type
selection (2D weights -> NVFP4, output/embed/1D -> Q8_0). llama-quantize can
now emit NVFP4 GGUFs for the first time.

Qwen2.5-0.5B wikitext-2 PPL (100 chunks): RTN 22.03 -> scale-search 19.40
(-11.9%) -> +imatrix 19.37. Gap ~6x error bar.
…orse)

Tried an iterative coordinate-descent scale solve (make_qkx-style) and a
narrower window. Findings, now in the comment:
- iterative CD gets stuck in local minima from NVFP4's non-uniform codes;
  -26% (single-seed) / -5% (multi-seed) vs brute-force window on weight-MSE.
- optimal code offset from amax/6 is empirically [-2,+7]; +/-12 is ample.
- +/-8 regressed real 9B search-only PPL (8.240 -> 8.287), so keep +/-12.
No code change to the search itself; window stays +/-12 brute force.
Review fixes (all 6):
1. llama-quant.cpp: add GGML_TYPE_NVFP4 case to tensor_type_fallback() - 2D
   weights with ne[0] % 64 != 0 now fall back to Q8_0 instead of throwing
   'no tensor type fallback defined' and aborting the whole quantize.
2. fix the false comment that claimed a fallback existed when it did not.
3. drop the NVFP4_NO_SEARCH getenv() A/B flag - was read once per row on the
   hot path and is the wrong mechanism for a library; search width is now a
   plain parameter to the core encoder.
4. (same) env-var removed from production code path.
5. cache the winning sub-block E2M1 nibbles during the scale search instead of
   recomputing best_index_mxfp4 for the chosen scale after the loop.
6. fold quantize_row_nvfp4_ref + the importance-aware path into one
   quantize_row_nvfp4_core(x,y,k,weights,search); ref = core(...,NULL,0). No
   more two divergent copies of the amax/pack encode.

Verified byte-identical NVFP4 output to the pre-refactor commit (search and
search+imatrix GGUFs), so all measured PPL results are unchanged. Net -16 lines.
- shrink the verbose header/justification comments to terse one-liners matching
  neighboring quantize_row_*_impl functions; move the 'why window=12' rationale
  out of the code (belongs in the PR, not a permanent comment).
- remove the explicit amax==0 early-out: base=ue4m3(0)=0 skips the whole search
  window, leaving the zero-initialized best_code/best_idx -> same zero block, one
  code path instead of two.
- match the sibling quantize_q*_K wrapper idiom (src += n_per_row).
Verified byte-identical NVFP4 output (search + search+imatrix) vs prior commit.
@github-actions github-actions Bot added examples ggml changes relating to the ggml tensor library for machine learning labels Jun 30, 2026
@avifenesh
avifenesh marked this pull request as ready for review June 30, 2026 02:04
@avifenesh
avifenesh requested a review from ggerganov as a code owner June 30, 2026 02:04
@krampenschiesser

Copy link
Copy Markdown
Contributor

Tested KLD on qwen36 35a3b with wiki.test.raw and the corresponding imatrix.
Quantized the following way: only experts are nvfp4, attn is q8 and single dim tensors are bf16.

this branch:

====== Perplexity statistics ======
Mean PPL(Q)                   :   6.114854 ±   0.038104
Mean PPL(base)                :   5.897773 ±   0.036705
Cor(ln(PPL(Q)), ln(PPL(base))):  98.35%
Mean ln(PPL(Q)/PPL(base))     :   0.036146 ±   0.001131
Mean PPL(Q)/PPL(base)         :   1.036807 ±   0.001173
Mean PPL(Q)-PPL(base)         :   0.217081 ±   0.006935

====== KL divergence statistics ======
Mean    KLD:   0.068268 ±   0.000522
Maximum KLD:  16.731131
99.9%   KLD:   2.473410
99.0%   KLD:   0.723394
95.0%   KLD:   0.236887
90.0%   KLD:   0.141153
Median  KLD:   0.027034
10.0%   KLD:   0.000472
 5.0%   KLD:   0.000125
 1.0%   KLD:   0.000016
 0.1%   KLD:   0.000002
Minimum KLD:  -0.000021

====== Token probability statistics ======
Mean    Δp: -1.120 ± 0.020 %
Maximum Δp: 99.771%
99.9%   Δp: 45.074%
99.0%   Δp: 18.310%
95.0%   Δp:  7.274%
90.0%   Δp:  3.771%
75.0%   Δp:  0.472%
Median  Δp: -0.047%
25.0%   Δp: -2.047%
10.0%   Δp: -7.198%
 5.0%   Δp: -11.909%
 1.0%   Δp: -28.779%
 0.1%   Δp: -68.928%
Minimum Δp: -99.770%
RMS Δp    :  7.764 ± 0.055 %
Same top p: 89.345 ± 0.080 %

master branch (221f0f6):

====== Perplexity statistics ======
Mean PPL(Q)                   :   6.080182 ±   0.037819
Mean PPL(base)                :   5.897773 ±   0.036705
Cor(ln(PPL(Q)), ln(PPL(base))):  98.37%
Mean ln(PPL(Q)/PPL(base))     :   0.030460 ±   0.001123
Mean PPL(Q)/PPL(base)         :   1.030928 ±   0.001158
Mean PPL(Q)-PPL(base)         :   0.182409 ±   0.006816

====== KL divergence statistics ======
Mean    KLD:   0.069380 ±   0.000524
Maximum KLD:  14.815773
99.9%   KLD:   2.531513
99.0%   KLD:   0.739422
95.0%   KLD:   0.242704
90.0%   KLD:   0.143861
Median  KLD:   0.027112
10.0%   KLD:   0.000461
 5.0%   KLD:   0.000119
 1.0%   KLD:   0.000015
 0.1%   KLD:   0.000002
Minimum KLD:  -0.000007

====== Token probability statistics ======
Mean    Δp: -1.041 ± 0.020 %
Maximum Δp: 99.738%
99.9%   Δp: 50.332%
99.0%   Δp: 19.016%
95.0%   Δp:  7.500%
90.0%   Δp:  3.912%
75.0%   Δp:  0.516%
Median  Δp: -0.033%
25.0%   Δp: -1.982%
10.0%   Δp: -7.131%
 5.0%   Δp: -11.862%
 1.0%   Δp: -28.642%
 0.1%   Δp: -69.285%
Minimum Δp: -99.205%
RMS Δp    :  7.837 ± 0.056 %
Same top p: 89.303 ± 0.080 %

mean kld got teensy tiny bit better, max kld got worse :(

@avifenesh

Copy link
Copy Markdown
Author

Hi @krampenschiesser,

Thanks for the comment. It is relevant because it tests a larger MoE model. The results are mixed: PPL worsened by 0.57%, while mean KLD improved by 1.60%. The upper KLD percentiles (p90-p99.9), RMS delta-p, and same-top improved, while mean delta-p worsened by 0.079 percentage points. Although maximum KLD worsened, it is an extreme-value statistic, p99.9 KLD improved by 2.30%.

However, this is not a controlled comparison. The branches differ by hundreds of unrelated commits, and the evaluation lacks a search-only arm. Experts-only testing also does not validate the PR's default behavior across all eligible 2D weight tensors.

A proper follow-up would compare RTN, search-only, and search+imatrix on one revision, using identical tensor inventories, evaluator, corpus, and base logits.

I lack the original F16/BF16 source and the capacity for the full base-logits run. If you can pick this up, I'd appreciate it.

@krampenschiesser

Copy link
Copy Markdown
Contributor

@avifenesh uploaded the kld here: https://huggingface.co/krampenschiesser/kld-baselines-gguf/blob/main/qwen36-35-a3b-bf16.kld
based on scripts/get-wikitext-2.sh with flash attention and 2048 context length. hope that helps you for evaluation

bobvious added a commit to bobvious/llama.cpp that referenced this pull request Sep 9, 2026
Replaces the flat `new_type = GGML_TYPE_NVFP4` branch in llama_tensor_get_type_impl
with Q4_K_M's bit-allocation map using NVFP4 as the base type: attn_v-class and
ffn_down take Q6_K on the ~40% of layers use_more_bits() selects, fused QKV would
take Q5_K (unreachable in practice -- category_is_attn_v catches it first), and
everything else stays NVFP4. Counters are advanced locally because this branch runs
before the Q4_K_M branches that normally do it.

MEASURED on Qwen3.5-9B Instruct, KLD vs its own BF16 over 292 chunks, held-out
imatrix, scored on an UNPATCHED runtime so the encoder is the only variable:

  NVFP4 + PR ggml-org#25153            5,450,280,960 B   0.198670   top-1 87.266
  + Fix 1 (this commit)        5,780,090,880 B   0.181971   top-1 88.243   -8.4%
  + Fix 1, Q8_0 head           6,026,424,320 B   0.181744   top-1 88.270

34 body tensors promoted (17 attn_qkv + 17 ffn_down), exactly what use_more_bits
selects from 32 layers. -8.4% is an order of magnitude more than promoting the
embeddings buys (-0.85% for +262 MB, measured separately), so the review doc was
right that allocation is where the remaining value sits.

It does NOT close the gap: stock Q4_K_M is 0.142930 at 5,680,522,464 B, so this
file is still 27% behind while being 1.75% larger. Whether the residual is the
codec or llama.cpp's W4A4 activation path is NOT settled by this commit -- that
is the C3 (GGML_CUDA_FORCE_CUBLAS) experiment.

Note this map is Q4_K_M's, PORTED. Nobody has designed an allocation map for
NVFP4's own error profile (per-16 symmetric scales, heavy tails); that remains
unexplored and is a plausible source of further gain.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012aDVsWFaEsEE9PMYNAQXUv

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants