Skip to content

fix(packing): repair hf_repack_layer and hf_pack_layer GPTQ v1 qzero format alignment (#4095) - #4096

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Prajwal7-2002:fix/hf-repack-qzero-format-alignment
Nov 15, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Prajwal7-2002:fix/hf-repack-qzero-format-alignment

Conversation

@Prajwal7-2002

@Prajwal7-2002 Prajwal7-2002 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Part of #4095 (items 1-2)

Summary

This PR covers items 1 and 2 of #4095:

  1. Repair hf_repack_layer zero-point alignment: copy live named buffers and parameters directly instead of calling source.state_dict(). source.state_dict() produces checkpoint-converted v1 zero points, which caused repacked modules that decode v2 zero points to decode every zero point one step low.
  2. Propagate _checkpoint_format_on_disk from source to target during _copy_packed_state() so targets preserve their serialization layout.
  3. In _restore_gptq_runtime_qzeros(), set _checkpoint_format_on_disk = FORMAT.GPTQ when converting v1 checkpoint words to v2, ensuring save_pretrained() and state_dict() emit proper v1 words instead of v2 words under a v1 label.
  4. In hf_pack_layer(), record _checkpoint_format_on_disk for GPTQ formats.

What Changed

  • gptqmodel/utils/packing.py:
    • Updated _copy_packed_state() to copy raw named parameters and buffers rather than calling source.state_dict().
    • Added propagation of _checkpoint_format_on_disk from source to target in _copy_packed_state().
    • In _restore_gptq_runtime_qzeros(), marked module._checkpoint_format_on_disk = FORMAT.GPTQ.
    • In hf_pack_layer(), recorded module._checkpoint_format_on_disk = requested_format.
  • tests/test_hf_packing_api.py:
    • Added regression test test_hf_repack_layer_preserves_gptq_v1_zero_points verifying both live runtime and serialized state_dict() zero points match identically without off-by-one shifts.
    • Added assertions to test_hf_pack_layer_post_init_restores_v2_runtime_layout and test_hf_post_init_restores_gptq_v2_runtime_layout verifying _checkpoint_format_on_disk and v1 serialized layout parity.

Tests

  • Unit tests have been executed and passed, or the reason they are not applicable is documented.
  • I added a new simple/fast unit test for this change, or documented why that is not applicable.
pytest tests/test_hf_packing_api.py -k "test_hf_repack_layer_preserves_gptq_v1_zero_points or test_hf_pack_layer_post_init_restores_v2_runtime_layout or test_hf_post_init_restores_gptq_v2_runtime_layout" -v

Output:

tests\test_hf_packing_api.py::test_hf_repack_layer_preserves_gptq_v1_zero_points PASSED [ 25%]
tests\test_hf_packing_api.py::test_hf_pack_layer_post_init_restores_v2_runtime_layout PASSED [ 50%]
tests\test_hf_packing_api.py::test_hf_post_init_restores_gptq_v2_runtime_layout[False] PASSED [ 75%]
tests\test_hf_packing_api.py::test_hf_post_init_restores_gptq_v2_runtime_layout[True] PASSED [100%]
================ 4 passed, 51 deselected, 3 warnings in 9.71s =================

Model Support / Module Plan

Not applicable (quantization packing utility bugfix).

Logic Flow Documentation

Not applicable (bugfix aligning buffer copying and format marker persistence with existing runtime/serialization specifications).

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Tested zero-point equivalence across in-memory buffers and serialized state_dict() outputs.

…format alignment (ModelCloud#4095)

In _copy_packed_state(), copy live named buffers and parameters directly instead of calling source.state_dict(). state_dict() produces checkpoint-converted v1 zero points, which previously caused repacked modules that decode v2 zero points to decode every zero point one step low.

Propagate _checkpoint_format_on_disk from source to target during _copy_packed_state() so targets preserve their serialization layout.

In _restore_gptq_runtime_qzeros(), set _checkpoint_format_on_disk = FORMAT.GPTQ when converting v1 checkpoint words to v2, ensuring save_pretrained() and state_dict() emit proper v1 words instead of v2 words under a v1 label.

In hf_pack_layer(), record _checkpoint_format_on_disk for GPTQ formats.

Add regression test test_hf_repack_layer_preserves_gptq_v1_zero_points and assertions to test_hf_pack_layer_post_init_restores_v2_runtime_layout and test_hf_post_init_restores_gptq_v2_runtime_layout.

Signed-off-by: Prajwal7-2002 <[email protected]>
@Qubitium
Qubitium merged commit c73793c into ModelCloud:main Nov 15, 2026
1 check passed
Qubitium added a commit that referenced this pull request Nov 15, 2026
main now carries #4101 (d7a41a1) and #4096 (c73793c) in squashed form.
The only conflict is tests/test_checkpoint_interop_4053.py, which both #4101
and this branch extend. Resolution:

* kept both new test sets: main's
  test_hf_from_pretrained_refuses_every_refused_method (#4091 item 5) and this
  branch's test_runtime_layout_state_dict_is_readable_but_checkpoint_writes_refuse
  (item 4) + test_state_dict_copy_between_v1_and_v2_models (item 3).
* dropped only test_runtime_layout_kernels_refuse_state_dict, which #4101
  inherited byte-identical from main and never edited. It asserts an
  unconditional raise from Marlin/ExLlamaV2 `_save_to_state_dict`, which is the
  exact contract item 4 changes. Both of its assertions (marlin and exllamav2
  raise "cannot be reconstructed") are preserved and strengthened by the
  parametrized replacement, which asserts the same raise under
  `checkpoint_serialization()` *and* that a read-only collection succeeds.
* removed one duplicate copy of test_hf_from_pretrained_refuses_every_refused_method:
  this branch already carried it from the earlier pr4101 merge, so taking main's
  copy as well defined it twice (ruff F811). One copy remains, identical to main.

Merged test inventory: 15 test functions / 26 collected cases, none of this
branch's and none of main's new tests lost.
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.

2 participants