The NVFP4 recipe: block sizes, rotation, and one command for the KLD#
A correct NVFP4 build of a 27B should lose almost nothing. The peer-reviewed NVFP4-W4A4 result on this
exact architecture (Qwen3.8-27B, all 496 linears including the 48 GatedDeltaNet layers) is
5-task average 85.10 against BF16's 85.62 (arXiv 2609.04098),
and Basalt's QUASAR-NVFP4 27B pack sits at KLD 0.0262 / top-1 92.82 %. Our leaderboard row,
Qwen3.8-27B-iMatrix-NVFP4-MTP.gguf, reads KLD 0.03217 / top-1 92.89 % while UD-Q4_K_XL at the
same bpw reads 0.00524 / 96.94 %. That gap is not NVFP4's price; it is a broken build. This page is
the recipe that replaces it, the rule the recipe enforces, and what the rotation data actually says.
The recipe, in full#
# 1. a foldable source (F16/BF16/F32). The fold needs 16-bit weights; a Q4_K_M file is refused.
third_party/llama.cpp/build/bin/llama-quantize base-Q4_K_M.gguf f16.gguf F16 --allow-requantize
# 2. fold + quantize in one command, with a provenance stamp next to the output
tools/nvfp4_recipe.py f16.gguf nvfp4-b64.gguf --block 64 \
--quantize-bin third_party/llama.cpp/build/bin/llama-quantize
tools/nvfp4_recipe.py f16.gguf nvfp4-plain.gguf --no-rotate \
--quantize-bin third_party/llama.cpp/build/bin/llama-quantize
tools/nvfp4_recipe.py runs tools/hadamard_fold.py first and llama-quantize second, because that is
the only supported order: the fold needs F32/F16/BF16, and llama-quant.cpp copies the prism.hadamard.*
keys into the quantized file so src/llama-hadamard.cpp still rotates the activation feeding each folded
weight. It writes DST.provenance.json (source sha256 and size, block size, fold flags, quantizer argv,
output sha256, tensor counts) and prints one provenance line on stdout for the shell log.
The source can be a streamed disk file of any size: the tool reads headers only to plan, and both child steps work a tensor at a time, so quantizing a 27B does not need the model in RAM.
The block-size rule#
NVFP4 is a 64-value block with four 16-value sub-blocks (QK_NVFP4 = 64, QK_NVFP4_SUB = 16,
ggml/src/ggml-common.h): four UE4M3 scales in one block_nvfp4, each covering 16 values, then 32 bytes
of E2M1 codes. The rotation therefore has to be a multiple of 64 — a rotation that straddles a block
boundary moves energy between two independently scaled sub-blocks, which is the exact direction a 4-bit
block format does not want — and being a power of two (a Walsh-Hadamard matrix is built by doubling) it
comes down to 64, 128, 256 and 1024. All four are inside the range HRX's fused rotation kernel
covers, blocks 2^6 … 2^12 (ggml/src/ggml-hrx/dispatch_registration/common/dispatch-hadamard.cpp,
if (k < 6 || k > 12)), so a folded NVFP4 file runs the kernel. The fold tool's historical default,
block 32, is below that range: it runs as a dense F32 MUL_MAT, which is what made rotation look
expensive in the first place.
tools/nvfp4_recipe.py refuses (exit 2, no output file) when
- the block is not a power of two, not a multiple of 64 (32 for
--format mxfp4), or not one of the 64/128/256/1024 candidates; - a foldable tensor's input width (
ne[0]) does not divide the block. It refuses rather than folding some tensors and leaving the rest in the plain basis: a model half in each basis is a model whose next token nobody can predict, and llama-hadamard would rotate every activation the file names; - a foldable tensor is not F32/F16/BF16 (message names the
llama-quantize … F16command); - the source already carries
prism.hadamard.*keys (folding twice is not the identity); --format mxfp8: MXFP8 has no ggml type yet, so there is no file to write;- after quantizing, the written
general.file_typeis not thellama_ftypeof the format (39 NVFP4, 38 MXFP4). That last one is a live check of the enum defect below, not a formality.
What rotation does, per format#
The rotation lever is not format-neutral, and the measurements say so:
| format | rotation | result |
|---|---|---|
| MXFP4 (block 32) | Hadamard | positive, +1.17 average over the sweep |
| NVFP4 (per-16 E4M3 scale) | Hadamard | negative, −0.45 average; negative in 8 of 9 configs, best +0.10 |
| MXFP8 | none | lossless-class: never rotate |
The mechanism is visible in the format: NVFP4's per-16 E4M3 scale already encodes the outliers inside each sub-block, so rotation does not buy headroom — it spreads the outlier's energy into the neighbours and the residual error ends up on the near-maximal values, where E2M1's step is widest (4 → 6). MXFP4's single E8M0 scale per 32 values cannot do that, so there the rotation is worth its cost. MXFP8 has no quantization error worth redistributing.
Two consequences for this repository:
- The Hadamard experiment belongs on (a) MXFP4 tensors, (b) FP4 KV cache, and (c) as a controlled A/B
on NVFP4 at the native block size — which is exactly what
--block 64versus--no-rotateis, on the same source, produced by the same command. - The NVFP4 candidates are
--no-rotateand--block 64; 128/256/1024 exist for the A/B, not as a presumed default.
The fold axis (fixed on this branch)#
tools/hadamard_fold.py used to decode a tensor as flat.reshape(dims[0], -1) and rotate along axis 0.
In a GGUF the fastest-varying axis is ne[0], so that view is not the logical matrix: the rotation ran
along a strided axis, mixing different output rows at the same input offset. Its --check could not see
it, because the check rotated the activation through the same view — a fold along any axis is
self-consistent with itself.
The loader's contract is not negotiable: src/llama-hadamard.cpp reads prism.hadamard.block_size and
requires w->ne[0] % block == 0, and llama_mul_mat_hadamard (src/llama-impl.h) reshapes the
activation to (block, n/block) and multiplies by H — i.e. blocks of block consecutive values of
ne[0]. So the weight must be H applied along ne[0], which is what the tool does now: it views the
tensor as (rows, ne[0]) and rotates the last axis. Measured on a fixture (block 64, F16, ne[0]=256):
- before: the stored weight was ~1.4 (≈ √2, i.e. uncorrelated) away from
Halongne[0], and the file a quantizer produced from it could not reproducex·Wat all; - after:
dequantised NVFP4versusH(SW)is 0.097 relative — pure NVFP4 quantization error — andx'·W'deqis 0.093 fromx·W.
tests/nvfp4_recipe_test.py re-derives the rotation with its own three lines of numpy rather than calling
the tool, and tests/hadamard_fold_test.py checks the loader's axis, so neither can pass by agreeing
with the writer.
general.file_type: one numbering, or the file lies about itself#
Three declarations describe the same number:
| where | what it says |
|---|---|
ggml/include/ggml.h enum ggml_ftype |
the ggml-side spelling, decoded by ggml_ftype_to_ggml_type() |
include/llama.h enum llama_ftype |
what llama-quantize writes into general.file_type |
gguf-py/gguf/constants.py LlamaFileType |
what every python tool writes |
They disagreed: ggml had MXFP4 = 25 / NVFP4 = 26, one Q3_K where llama.h has Q3_K_S/M/L, no
TQ1_0/TQ2_0, and PQ2_0 = 128 against llama.h's 141. 25 and 26 are IQ4_NL and IQ3_S in the
numbering the file actually uses, so ggml_ftype_to_ggml_type((enum ggml_ftype) file_type) either
returned the wrong quantization or hit GGML_ASSERT(wtype != GGML_TYPE_COUNT) — and a python-written
NVFP4 file (39) was not a case at all. All three now carry the same 41 values, gguf-py gained the
MOSTLY_PQ2_0_LEGACY = 142 it was missing, and the retired MOSTLY_Q4_1_SOME_F16 = 4 is commented out
in both C headers instead of living in one.
tests/ftype_enum_agree_test.py parses all four sources (both enums, the python enum, and the decode
switch) and fails when they drift: same names, same values, every named value decoded, and each decode
the ggml type the name carries. It parses third_party/llama.cpp in place, so it runs on every pin bump.
One command for the KLD#
The number the leaderboard shows is llama-perplexity's KL divergence against the base logits of the
reference file, which is the same measurement scripts/hw-gate.sh uses:
# once per reference build: capture the base logits (512 tokens x 8 chunks of wiki.test.raw)
B=third_party/llama.cpp/build/bin/llama-perplexity
$B -m f16.gguf -f wiki.test.raw -c 512 --chunks 8 -b 512 -ub 512 --kl-divergence-base base.bin
# then every candidate, one line per file: mean KLD and same-top-p are the leaderboard's two columns
for f in nvfp4-plain.gguf nvfp4-b64.gguf; do
echo "== $f"
$B -m "$f" -f wiki.test.raw -c 512 --chunks 8 -b 512 -ub 512 \
--kl-divergence-base base.bin --kl-divergence | grep -E "Mean KLD|Same top p"
done
tools/hw_gate_manifest.py judge parses the same output against the manifest's thresholds, and
tools/logits_kld.py compares --dump-logits directories when the two runs are different routes rather
than different files.
What this branch does not do#
- No 27B measurement. No F16/BF16 27B GGUF is in the tree:
/home/bcloud/hf-stageholds only README and LICENSE for the threeQwen3.8-27B-*repos, and the only 27B file on the box is/home/bcloud/models/dflash2/Qwen3.8-27B-DFlash2-Q4_K_M.gguf, which the recipe refuses (already quantized) — by design. The fixture gate proves the pipeline; the KLD above is the measurement to run on strixhalo. - No importance-aware NVFP4.
quantize_nvfp4()inggml/src/ggml-quants.cdiscards itsquant_weightsargument, so--imatrixchanges nothing for this format. That is the next lever for the 0.005-class KLD the XL row already reaches. - MXFP8 has no ggml type, so it has no file, no KLD row and nothing to rotate.