Skip to content

feat(nvfp4): add NVFP4 weight-only quantization with Schur Replay - #6486

Merged
ZX-ModelCloud merged 10 commits into
mainfrom
feat/schur-replay-nvfp4
Nov 30, 2026
Merged

ZX-ModelCloud merged 10 commits into
mainfrom
feat/schur-replay-nvfp4

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds METHOD.NVFP4 / FORMAT.NVFP4 so GPT-QModel can produce NVIDIA NVFP4
weight-only checkpoints instead of only loading them. Weights are E2M1 codes
with one E4M3 block scale per 16 input columns and an FP32 per-tensor
multiplier. The solve is GPTQ's sequential
error-feedback recursion; every 16-column group picks its block scale with
Schur Replay (arXiv:2609.36654) or the
max-scaled baseline.

Net diff across the ten commits: 26 files, +3259 / −5. One new format; no
existing quantization path is rewritten.

No tracking issue; this is a new feature.

What Changed

  • METHOD.NVFP4, FORMAT.NVFP4, NVFP4Config, BACKEND.NVFP4_TORCH, and a
    ProcessorStep.NVFP4 lifecycle that reuses the shared Hessian capture.
  • gptqmodel/quantization/nvfp4.py: clean-room code tables (126-point E4M3
    grid, round-to-nearest-even E2M1), the Schur-Replay selector, E2M1 packing,
    decode, and NVFP4Task (per-device X^T X, shared-input adoption,
    max-scaled RTN fallback when a layer has no calibration signal).
  • gptqmodel/nn_modules/qlinear/nvfp4.py: NVFP4Linear keeps only packed
    storage (weight uint8 [out, in/2], weight_scale uint8 [out, in/16],
    weight_scale_2 scalar). The forward decodes output-channel tiles to FP32,
    adds the bias once after the reduction, and rounds once to the activation
    dtype. Declared for CPU, NVIDIA CUDA and AMD ROCm.
  • gptqmodel/looper/nvfp4_processor.py: capture → solve → pack per layer.
  • Checkpoint: weight / weight_scale / weight_scale_2 under ModelOpt's
    tensor names, with the block scales stored as raw E4M3 code bytes rather
    than ModelOpt's typed FP8 tensors, quant_method: nvfp4, and a producer
    marker in meta.quantizer. nvfp4 is added to the refused-plain-HF list so
    transformers fails instead of silently returning random weights.
  • Docs: docs/nvfp4.md (flows below), docs/schur-replay-ab.md,
    support matrix row, README entry.

Intentionally out of scope: W4A4 activations, a fused dequant-GEMM kernel
(the portable Torch forward is the correctness path), and the paper's
tiered-memory / tensor-parallel infrastructure.

Flow

Old flow: NVFP4 was load-only.

Area Before After
Weight format INT4 affine (GPTQ) or MXFP4 (MACaQ); NVFP4 only loadable FORMAT.NVFP4: E2M1 nibbles + E4M3 block-16 scales + FP32 multiplier
Solver — GPTQ recursion with a per-group scale selector
Checkpoint — weight / weight_scale / weight_scale_2 (raw E4M3 scale bytes)
Kernel TorchFP4Linear (load-only) NVFP4Linear packs, runs and re-saves
flowchart TD
    A["QuantizeConfig method=nvfp4"] --> B["_resolve_quantize_config_class"]
    B --> C["NVFP4Config validates bits=4, group_size=16, desc_act=False"]
    C --> D["QuantizationRunPlan picks ProcessorStep.NVFP4"]
    D --> E["NVFP4Processor captures X^T X per device"]
    E --> F["NVFP4Task.quantize"]
    F --> G["Damped inverse gives U = chol of inverse H"]
    G --> H["Per 16-column group: schur_replay_group"]
    H --> I["Full conditional curvature; score replay candidates, keep the old scale on ties"]
    I --> J["GPTQ error feedback into the remaining columns"]
    J --> K["Result: packed bytes, E4M3 codes, global scale"]
    K --> L["NVFP4Linear.pack"]
    L --> M["Checkpoint: weight, weight_scale, weight_scale_2"]
Loading
stateDiagram-v2
    [*] --> Dense
    Dense --> Calibrating: quantize starts
    Calibrating --> Packed: NVFP4Linear.pack
    Packed --> Packed: forward decodes tiles in FP32
    Packed --> Saved: save writes packed NVFP4 tensors
    Saved --> Packed: GPTQModel.load rebuilds the kernel
    Packed --> [*]
Loading

Failure contracts: desc_act=True and static_groups are rejected (the block
scale is bound to 16 consecutive input columns); bits must be 4, group_size
16, in_features divisible by 16; the LM head is not quantized.

Tests

ruff check gptqmodel gptqmodel/models/ports/native/auto/__init__.py \
  gptqmodel/models/ports/native/auto/_registry.py docs/eora tests setup.py
# All checks passed!

python -m pytest -q tests/qcfg tests/test_rocm_kernel_contracts.py \
  tests/test_nvfp4_schur_replay.py tests/test_nvfp4_kernel.py \
  tests/test_nvfp4_lifecycle.py
# 933 passed (NVIDIA CUDA)
# 933 passed (AMD ROCm)

python -m pytest -q tests/test_auto_module_decoder.py \
  tests/test_model_dequant_fp8.py tests/test_rocm_kernel_contracts.py \
  tests/test_serialization.py tests/test_transformers_unpatched.py \
  tests/test_model_port_policy.py
# 599 passed

The same NVFP4 subset (solver, kernel, save/reload lifecycle, ROCm
kernel-contract audit) passes on the AMD ROCm runtime with the same count.

New tests pin: the 126-point E4M3 grid, E2M1 round-to-nearest-even at and
around every threshold, pack/unpack bytes, decode == solver reconstruction,
forward vs. an independent dense FP32 oracle in FP16/BF16/FP32, bias added
once after the FP32 reduction, packed-only persistent storage, state-dict
round trip, and a quantize → save → reload lifecycle that compares packed
tensors and logits.

Known unrelated failures: tests/test_docs_contract_refs.py reports 7
failures and 235 passes on a clean origin/main checkout in this
environment (docs/measurement.md::3, the two docs/weight-loader.md
examples, and four contract-reference checks). The same 7 fail on this
branch, so none are introduced here.

Measured Impact

Gate: same calibration Hessians and held-out windows for both selectors;
weights evaluated through the dequantized path (W4A16). Current selector,
which scores the full conditional curvature from the upper Cholesky factor,
on Qwen3-0.6B with 64 x 512 calibration and 32 x 512 held-out windows:

Model Selector Weight rel. MSE Held-out loss Perplexity Improved layers
Qwen3-0.6B max 1.4414e-2 2.973543 19.5611 -
Qwen3-0.6B schur_replay 1.0790e-2 2.949580 19.0979 196/196

All 196 layers improved (worst 7.4 %, best 35.3 %, mean 24.7 %), 68.5 % of
row-groups changed scale, the surrogate score dropped 29.7 %, and the solve
took 200 s against 182 s for max (about 1.1x). Historical rows from the
initial diagonal-curvature selector are kept in docs/schur-replay-ab.md.

The lifecycle test quantizes, saves, reloads and compares the packed tensors
and logits, so the checkpoint round trip is covered by an exact assertion
rather than by a sampled loss.

These numbers are not a reproduction of the paper's 70B/397B results; the
models and calibration sets are small and W4A4 is out of scope.

End-to-end scores

Same protocol as the INT4 comparison in #6063: WikiText-2 test perplexity with
24 x 2048 calibration windows from the train split, 32768 evaluation tokens in
2048-token windows, and every checkpoint saved to disk and reloaded through
GPTQModel.load before scoring. GPTQ uses group_size=128; NVFP4 uses its
fixed block of 16. Lower is better.

Model dtype FP16/BF16 GPTQ g128 NVFP4 max NVFP4 Schur Quant s (GPTQ / max / Schur)
SmolLM2-135M fp16 14.7108 18.8363 17.2005 16.4614 42.8 / 85.2 / 147.1
Qwen2.5-0.5B-Instruct fp16 13.7697 14.9897 14.5516 14.2146 23.0 / 168.3 / 234.6
Llama-3.2-1B-Instruct bf16 13.7819 — 14.9419 14.4305 — / 191.9 / 344.1

The dense and GPTQ rows reproduce the values reported in #6063
(14.711 / 18.836 and 13.770 / 14.990), so the harness is directly comparable.
Schur Replay lowers perplexity by 4.3% / 2.3% / 3.4% versus max-scaled NVFP4
and by 12.6% / 5.2% versus GPTQ on the first two models.

ARC-Easy, Qwen2.5-0.5B-Instruct, 400 test questions, fixed prompt:

Config acc acc_norm
dense 0.6275 0.5950
GPTQ g128 0.5775 0.5500
NVFP4 max 0.6250 0.6150
NVFP4 Schur 0.6175 0.5750

At 400 samples the 1-sigma binomial noise is about 2.4 points, so the NVFP4
rows are within noise of dense, and the max-versus-Schur gaps (0.8 points on
acc, 4 points on acc_norm) stay inside two sigma; the perplexity gap is
the stronger signal.

Reproduce:

python scripts/benchmark_nvfp4_ppl.py \
  --model /path/to/Qwen2.5-0.5B-Instruct \
  --configs dense,gptq,nvfp4-max,nvfp4-schur \
  --output-dir /tmp/nvfp4_ppl_qwen05

python scripts/benchmark_nvfp4_arc_easy.py \
  --dense /path/to/Qwen2.5-0.5B-Instruct \
  --checkpoints gptq=/tmp/nvfp4_ppl_qwen05/gptq,nvfp4-max=/tmp/nvfp4_ppl_qwen05/nvfp4-max,nvfp4-schur=/tmp/nvfp4_ppl_qwen05/nvfp4-schur \
  --samples 400 \
  --output /tmp/arc_qwen05.json

Llama-3.2-1B was scored in bf16 (the checkpoint's training dtype) and its
GPTQ row is omitted because the INT4 baseline for that checkpoint is
pathological in this build (39.2 fp16 / 45.4 bf16 against 13.78 dense); that
path does not involve NVFP4. Schur Replay costs about 1.4-1.8x the max-scaled
solve on the portable Torch path; these are single-process runs that include
calibration, packing and saving, so wall-clock varies with host load and the
ratio is the meaningful part.

Model Support / Module Plan

Not applicable: no new model, alias or definition, and no module-plan change.
The lifecycle reuses the existing per-layer processor framework.

AWQ onboarding safety

Not applicable: no AWQ grouping, search, replay or fold change.

Contracts

  • Forward dtype: output dtype equals input dtype; one dtype per module; packed
    buffers are uint8 and never re-cast.
  • Bias: kept at checkpoint width and added once to the FP32 reduction before
    the single output cast.
  • Storage: the module persists only packed NVFP4 state; decoded weight tiles
    are local to one call.

@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review November 30, 2026 08:01

@ZX-ModelCloud ZX-ModelCloud left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4d8af81. Schur Replay now scores the full conditional curvature from the upper Cholesky factor. NVFP4 scale bytes round-trip through reload, requantization, and dense export, including subnormal scales. Reload preserves the stored bias, deferred packing holds only the packed payload, and direct repacking respects the forward lock.

format/check.sh passed. The CPU suite passed 1,095 tests with three accelerator-only skips. The docs suite reproduced the existing docs/measurement.md example failure. The published benchmark numbers predate the curvature fix.

@ZX-ModelCloud ZX-ModelCloud left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 07c8412. Casting a model to FP16 before its first forward could narrow the NVFP4 global scale and bias. Copy-loading a state dict could also narrow the bias or fail for modules constructed in inference mode. The regression now reproduces the exact cancellation result of -1 and preserves the saved values. Bias-free layers also honor meta-device construction.

format/check.sh passed. CPU validation passed 1,105 tests, including quantize/save/reload. Four accelerator-dependent tests were skipped.

@ZX-ModelCloud
ZX-ModelCloud force-pushed the feat/schur-replay-nvfp4 branch 2 times, most recently from 0217385 to c250596 Compare November 30, 2026 11:46
Add the NVFP4 weight block-scale core from arXiv:2609.36654:
- 126-point E4M3 scale grid and round-to-nearest-even E2M1 rounding
- proposal/replay/commit selection with old-scale retention
- GPTQ NVFP4 solve with a max-scale baseline
- unit tests for the grid, rounding, replay/commit consistency and the
  per-group surrogate guarantee

Add scripts/ab_schur_replay_nvfp4.py and docs/schur-replay-ab.md with a
small-model W4A16 A/B. On Qwen3-0.6B and Llama-3.2-1B the selector reduces
weight reconstruction error by about 25% and improves held-out loss, with
no per-layer regressions.

This is the gate for the full NVFP4 checkpoint/kernel work; it does not add
the checkpoint format or an inference kernel yet.
Quantize dense weights to the NVIDIA/ModelOpt NVFP4 layout (E2M1 codes,
one E4M3 scale per 16 input columns, FP32 per-tensor multiplier) with
GPTQ's sequential error-feedback solve. Each 16-column group picks its
block scale with Schur Replay (arXiv:2609.36654) or the max-scaled
baseline, and the old scale is always a candidate, so a group cannot be
worse than the baseline in the calibration metric.

- METHOD.NVFP4 / FORMAT.NVFP4, NVFP4Config, BACKEND.NVFP4_TORCH, and a
  ProcessorStep.NVFP4 lifecycle that reuses the GPTQ Hessian capture
- NVFP4Task: per-device X^T X capture, shared-input adoption, and a
  max-scaled RTN fallback for layers without calibration signal
- NVFP4Linear: packed storage only, tiled FP32 decode, bias added once
  after the reduction; declares CPU, NVIDIA CUDA and AMD ROCm
- ModelOpt-compatible checkpoint tensors (weight, weight_scale,
  weight_scale_2) plus a refused-plain-HF entry
- desc_act and static groups are rejected: the block scale is bound to
  16 consecutive input columns
- tests: code-table and kernel oracles, the solver's per-group
  guarantee, and a save/reload lifecycle; run on CUDA and ROCm
@ZX-ModelCloud
ZX-ModelCloud force-pushed the feat/schur-replay-nvfp4 branch from c250596 to d31d37c Compare November 30, 2026 12:39
@ZX-ModelCloud
ZX-ModelCloud merged commit de19a44 into main Nov 30, 2026
6 checks passed
@ZX-ModelCloud
ZX-ModelCloud deleted the feat/schur-replay-nvfp4 branch November 30, 2026 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant