Skip to content

fix(hf): restore instance-scoped save_pretrained layout guard (#6137) - #6144

Merged
Qubitium merged 1 commit into
mainfrom
fix/6137-hf-save-layout-guard
Nov 27, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/6137-hf-save-layout-guard

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #6137.

#4620 removed the PreTrainedModel.save_pretrained wrapper that entered
checkpoint_serialization(). After that, an HF-loaded Marlin/ExLlamaV2 model
wrote its private runtime layout (tile/shuffled qweight, empty qzeros/g_idx)
and *.qzeros_format markers under an ordinary checkpoint_format: gptq
config. The directory could not be reloaded by GPTQModel.load or by
AutoModelForCausalLM.from_pretrained.

This restores the refusal without patching Transformers: hf_gptqmodel_post_init
(the bridge Transformers' GPTQ and AWQ quantizers already call) now wraps
save_pretrained on the loaded instance only. No transformers.* class or
module is touched, and GPTQModel.load models keep their existing writer path.

What Changed

  • gptqmodel/utils/model.py: new _attach_checkpoint_serialization_save_guard()
    and a call from hf_gptqmodel_post_init(). The wrapper is idempotent and runs
    the original instance writer inside checkpoint_serialization().
  • Runtime-layout kernels therefore raise the existing actionable fix(checkpoint): refuse runtime-layout HF saves, emit AWQ snapshots, carry layout in a tensor (#4125) #4134 error
    from _save_to_state_dict instead of publishing an unloadable checkpoint.
  • Checkpoint-layout kernels keep writing valid tensors, and the
    qzeros_format runtime marker is dropped from the HF payload (fix(checkpoint): stop HF save_pretrained from writing qzeros_format markers (#4152) #4155).
  • docs/checkpoint-interop.md: section 3 and its diagrams now describe the
    bridge-instance guard instead of claiming HF save_pretrained() is read-only.
  • Tests in tests/test_checkpoint_interop_4053.py: synthetic GPTQ checkpoint
    loaded through AutoModelForCausalLM; valid CPU round trip, Marlin and
    ExLlamaV2 refusal, and a native GPTQModel.save() control.

Out of scope: save_pretrained on a model loaded by GPTQModel.load is not
wrapped (it is not the HF bridge, and an instance attribute there would make
torch.save(model) unpicklable). GPTQModel.save() remains the supported
writer.

Tests

Environment: /root/venv (torch 2.15.0+cu132, transformers 5.23.0),
CUDA_VISIBLE_DEVICES=0 on an RTX 4090 (sm89). No sm80 device was available in
this worker; the defect is in the save dispatch, not kernel arithmetic, and both
Marlin and ExLlamaV2 run natively here.

Green:

cd tests
CUDA_VISIBLE_DEVICES=0 PYTHONPATH=<clone>:<clone>/tests /root/venv/bin/python -m pytest \
  test_checkpoint_interop_4053.py -q -rs
# 24 passed in 31.44s

... -k "hf_bridge or native_writer"
# 5 passed in 14.82s

PYTHONPATH=<clone>:<clone>/tests /root/venv/bin/python -m pytest test_transformers_unpatched.py -q -rs
# 1 passed

PYTHONPATH=<clone>:<clone>/tests /root/venv/bin/python -m pytest test_model_port_policy.py -q -rs
# 10 passed

CUDA_VISIBLE_DEVICES=0 ... pytest test_checkpoint_state_invariant.py -q -rs
# 22 passed, 3 skipped (machete/swordfish/bitblas runtimes unavailable)

CUDA_VISIBLE_DEVICES=0 ... pytest test_hf_bridge_cpu_offload_5022.py -q -rs
# 11 passed

CUDA_VISIBLE_DEVICES=0 ... pytest test_marlin_cpu_offload_post_init.py -q -rs
# 5 passed

CUDA_VISIBLE_DEVICES=0 ... pytest test_save_dispatch.py -q -rs
# 5 passed

Lint (same command as CI):

/root/venv/bin/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!

Red (new test file copied onto a clean worktree at 43b999585, fix absent):

pytest test_checkpoint_interop_4053.py -q -rs -k "hf_bridge"
# 3 failed
# - CPU payload: "save_pretrained wrote the runtime qzeros_format marker"
# - marlin: "DID NOT RAISE RuntimeError"
# - exllama_v2: "DID NOT RAISE RuntimeError"

Real-model reproduction at HEAD (before the fix), Qwen/Qwen2.5-0.5B-Instruct-GPTQ-Int4:

modules: MarlinLinear x168
save_pretrained: SUCCEEDED
model.layers.0.self_attn.q_proj.qweight  (56, 1792)   # source (112, 896)
model.layers.0.self_attn.q_proj.qzeros   (0,)
model.layers.0.self_attn.q_proj.g_idx    (0,)
qzeros_format keys: 168
GPTQModel.load: ValueError: Trying to set a tensor of shape torch.Size([0]) in "g_idx" ...
AutoModelForCausalLM: RuntimeError: ... size mismatch ...

After the fix the same script raises at save:

RuntimeError: Marlin rewrites `qweight` into its runtime tile layout and replaces
`qzeros`/`g_idx`; the original checkpoint tensors cannot be reconstructed. Use
`GPTQModel.save()`/`save_quantized()` ...

and leaves only config.json/generation_config.json, no model.safetensors.

Pre-#4620 confirmation: git show 8c71625b5:gptqmodel/__init__.py defines and
calls _patch_transformers_save_pretrained_runtime_layout_guard(), whose wrapper
runs checkpoint_serialization() around the original writer; git show 6f9ad69e6 documents the #4134 refusal. The old class-wide patch is not restored.

Native writer control (GPTQModel.save() on Marlin and ExLlamaV2, reload +
forward parity):

marlin: saved+reloaded max diff 0.000e+00
exllama_v2: saved+reloaded max diff 0.000e+00

Skips

  • test_checkpoint_state_invariant.py: machete, swordfish and bitblas runtimes
    are not installed in this venv.
  • No sm80 device was available; native Marlin/ExLlamaV2 ran on sm89.
  • CUDA refusal tests skip when torch.cuda.is_available() is false.

#4620 removed the PreTrainedModel.save_pretrained wrapper that entered
checkpoint_serialization(), so an HF-loaded Marlin/ExLlamaV2 model wrote its
irreversible runtime layout (tile/shuffled qweight, empty qzeros/g_idx) plus
qzeros_format markers under an ordinary checkpoint_format: gptq config. The
directory could not be reloaded by GPTQModel.load or Transformers.

Attach the guard to the loaded model instance in hf_gptqmodel_post_init, the
bridge Transformers' GPTQ/AWQ quantizers already call. It is instance-scoped,
so no Transformers class is touched. Bridge saves now refuse on runtime-layout
kernels with the #4134 error and drop qzeros_format for checkpoint-layout
kernels; GPTQModel.load/GPTQModel.save and read-only state_dict() callers are
unchanged.
@Qubitium
Qubitium marked this pull request as ready for review November 27, 2026 11:07
@Qubitium
Qubitium merged commit b3f6810 into main Nov 27, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/6137-hf-save-layout-guard branch November 27, 2026 12:20
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