Skip to content

fix(export): refuse an hf export whose method has no dense decoder (#4786) - #4824

Merged
Qubitium merged 1 commit into
mainfrom
fix/hf-export-undeclared-method
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/hf-export-undeclared-method

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

GPTQModel.export(path, out, "hf") reported success and wrote a dense checkpoint with wrong weights for Paro and MACaQ checkpoints. detect_format() guessed from tensor names before it looked at the declared quant_method:

checkpoint decoded as why result
Paro (method: "paro") awq .qweight with no .g_idx theta/pairs/channel_scales dropped, rotated codes read as plain AWQ: silent garbage (nll 1.49 -> 13.28)
MACaQ (method: "macaq") mxfp4 substring mxfp4 of format: "mxfp4_macaq" BH/pre_scale dropped: silent garbage (nll 1.73 -> 12.64)
GGUF (method: "gguf") awq same as Paro loud but misleading KeyError: Incomplete AWQ buffers ... missing {'qzeros', 'scales'}
QQQ / NF12 / VPTQ / EXL3 none loud ValueError: Unable to detect quantization format

The exported config carries no quantization_config, so nothing downstream can tell the weights are wrong.

A GPT-QModel method owns its packed layout, and its format is only a sub-format of that method — a rotation-free MACaQ checkpoint stores format: "mxfp4" next to method: "macaq" (docs/macaq.md). Neither an mxfp4 substring nor a .qweight-without-.g_idx sniff can separate those from a real MXFP4/AWQ checkpoint.

Fixes #4786.

What Changed

  • gptqmodel/utils/model_dequant.py
    • New _UNDECODABLE_METHODS: paro, macaq, gguf, gptqmodel_gguf, qqq, nf12, vptq, exl3, tt_blockfloat.
    • detect_format() returns the declared method for those, before the MX/FP8 string guesses and before the tensor-name sniffing, so the caller can refuse it instead of guessing a decoder.
    • dequantize_model() raises NotImplementedError naming the method and listing the formats that do have a dense decoder. The check runs before output_path.mkdir(), so a refused export leaves no half-built directory (it also now runs before the directory is created for any other pre-dispatch failure).
  • docs/export-hf-format-detection.md (new) — Mermaid flow of the new decision order with the fail-closed exit marked, plus the before/after table. Linked from README.md.
  • tests/test_export.py — test_export_hf_refuses_a_method_without_a_dense_decoder, 8 nodes over Paro, MACaQ (mxfp4_macaq and rotation-free mxfp4), GGUF, QQQ, NF12, VPTQ, EXL3.

Behaviour change: export(..., "hf") now refuses these methods instead of writing wrong weights. GPTQModel.load() and the quantized checkpoints are unaffected; there is still no dense decoder for them, so the refusal message points at GPTQModel.load().

Tests

cd tests
python -m pytest test_export.py -q -ra
# 12 passed in 17.44s   (8 new)

python -m pytest test_export.py test_model_dequant_fp8.py test_mimo_mixed_source.py test_fp8.py -q -ra
# 108 passed, 1 failed
#   the failure is test_ignored_layers_are_honored_by_non_fp8_converters:
#   RuntimeError: ... requires the 'bitsandbytes' package. It fails identically
#   on the unmodified base (verified by stashing the change).

python -m pytest test_exllamav3.py -q -ra -k detect_format
# 1 passed   (still classifies EXL3 as "exl3")

python -m pytest test_macaq.py test_paro.py -q -ra
# 333 passed, 5 skipped (compiled flips unqualified on this GPU; one 6 GiB case)

ruff check gptqmodel docs/eora tests setup.py (the format/check.sh gate): All checks passed.

Red / green evidence

Reverting only the model_dequant.py hunk, keeping the new test:

Case Broken code With fix
[paro-paro] FAIL Failed: DID NOT RAISE NotImplementedError PASS
[macaq-mxfp4_macaq] FAIL DID NOT RAISE PASS
[macaq-mxfp4] FAIL DID NOT RAISE PASS
[gguf-q4_k_m] FAIL KeyError: Incomplete AWQ buffers ... missing {'qzeros', 'scales'} PASS
[qqq-qqq], [nf12-nf12], [vptq-vptq], [exl3-exl3] FAIL same misleading KeyError PASS

Red run: 8 failed. Green run: 12 passed. The two DID NOT RAISE cases are the reported silent-garbage bug: the broken export completes and writes the directory.

Logic Flow Documentation

Required and included, because this introduces a fail-closed contract:

  • docs/export-hf-format-detection.md (linked from README.md) holds the flow diagram, with the two fail-closed nodes (E classification, K refusal) filled red.
  • New flow, using real symbols: dequantize_model() -> detect_format() -> declared method in _UNDECODABLE_METHODS? -> return method; otherwise the declared MX/FP8 semantics, otherwise tensor-name sniffing; then dequantize_model() raises NotImplementedError if fmt in _UNDECODABLE_METHODS before output_path.mkdir().
  • Replaced mechanism, with file:line evidence: the old detect_format() read "mxfp4" in declared (a substring of the joined method format variant quant_algo) and then sniffed .qweight without .g_idx as AWQ. The table above records what each method resolved to before and after.
  • Failure and ambiguity paths are the red nodes and the before/after table; the success path (G/J) is unstyled.

Model Support / Module Plan

Not applicable — no MODEL_MAP entry, definition or fusion config is touched.

Review Requirements

  • Reviewed this PR.
  • Matches existing project structure and conventions (tests/test_export.py already covers the other methods' export path; docs page follows the docs/*.md Mermaid convention).
  • No monkeypatching; the test builds a real minimal safetensors checkpoint and calls the public dequantize_model().
  • Minimal surface: one constant, one early return, one refusal.
  • Regression considered: 108 dequant/export tests pass, MXFP4/NVFP4 detection without a declared method still works (test_model_dequant_fp8.py), EXL3 still classifies as exl3.

Notes

  • The refusal is deliberately in dequantize_model() and not in detect_format(): detect_format() stays a pure classifier (tests call it directly), and returning the method name is what lets the caller refuse.
  • Adding a dense decoder for any of these later means removing it from _UNDECODABLE_METHODS and adding a fmt == ... branch.

…4786)

detect_format() guessed from tensor names before it looked at the declared
quant_method. Paro carries .qweight without .g_idx and was decoded as AWQ, and
MACaQ's format 'mxfp4_macaq' matched the 'mxfp4' substring, so both dropped
their rotation, reported success and wrote a dense checkpoint whose weights ran
but were garbage (nll 1.5 -> 13). GGUF failed with a misleading AWQ KeyError.

A GPT-QModel method owns its packed layout and its format is only a sub-format
of it, so the declared method now decides: detect_format() reports the method,
and dequantize_model() refuses it with NotImplementedError before it creates
the output directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant