Repository navigation
fix(hf): rebuild a deferred qzeros after a Marlin->Torch class swap - #6395
Merged
Merged
Conversation
`_materialize_hf_sym_qzeros` keyed "this kernel owns its zero point" off
`qzeros` not being a registered buffer. That describes Marlin, but
`_reselect_offloaded_marlin_kernels` swaps a Marlin layer to a portable
kernel by assigning `__class__`, so the replacement inherits Marlin's
zero-point `Parameter`. When the loader left that parameter on meta (a
deferred symmetric `qzeros` is absent from the offload map, so nothing
materializes it) the rebuild replaced it with a zero-length parameter and
the first forward failed in `TorchLinear._decode_qzeros`:
IndexError: Dimension out of range (expected to be in range of [-2, 1],
but got 2)
Rebuild the words in the shape the final kernel reads instead: drop the
inherited parameter and use `qzeros_standard_shape` for the layer's packed
geometry. A real Marlin/AWQ-Marlin layer keeps its own representation.
Measured on `ModelCloud/Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1`
with `device_map="auto"` + `max_memory={0: "350MiB", "cpu": "2GiB"}`:
112 TorchLinear layers and logits identical to the pre-wave 0dd8e8a
offload path (sha1 203615fe...).
Conflicts in gptqmodel/utils/model.py::_materialize_hf_sym_qzeros: take main's #6391 rebuild, but size the rebuilt buffer with qzeros_standard_shape() and drop the inherited Parameter first. _reselect_offloaded_marlin_kernels swaps __class__ in place, so the replacement still owns Marlin's tile-layout qzeros Parameter; copying that shape into the portable kernel is what produced the forward-time IndexError in #6383.
Qubitium
pushed a commit
that referenced
this pull request
Nov 29, 2026
This was referenced Nov 29, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 29, 2026
* test(qzeros): pin the symmetric-qzeros state machine invariants Six changes landed on the symmetric ``qzeros`` path in one day (#6388, #6392, #6391, #6395, #6409 and the pending #6157 normalization). Each was verified on its own; nothing pinned the combination, and a later edit can satisfy one fix while breaking another. The state machine has three markers (``_HF_LEGACY_QZEROS``, ``_HF_CHECKPOINT_SYM``, ``_HF_DEFER_SYM_QZEROS``) and two word encodings, selected by ``_load_target_qzeros_format()``. The tests assert: - a 32-aligned legacy projection never defers, and a real floor-width mismatch always does (#6409 / #6396); - an asymmetric floor-width checkpoint is refused, never silently wrong; - a deferred module materializes to the standard shape with a resolved layout that agrees with the one the kernel decodes; - materialization is idempotent across 2/3/4/8-bit; - the deferral marker and a materialized buffer are mutually exclusive; - a float ``qzeros`` buffer (int4pack affine offsets) is never filled with a packed word -- skipped until the #6157 normalizer exists; - a clean checkpoint's word survives the load, and every pre- and post-#2983 producer lands on the same decoded zero point. ``qzero_format()`` is deliberately not read as a decode answer before ``_qzeros_format_established``: every kernel is ``REQUIRES_FORMAT_V2``, so the constructor placeholder ``1`` disagrees with the decoded ``2`` until a load resolves it (#4377). That distinction is the whole reason the first #6157 draft corrupted clean checkpoints. Test-only. Verified 49 passed / 13 skipped on main (a4471c5) and 50 / 12 with #6419 applied; 68 passed / 17 skipped together with the four existing qzeros test files, so there is no global-state pollution. Every skip is a kernel-geometry or environment gate with a precise reason. * test(qzeros): spell out the unreachable return after a pytest.skip ``_legacy_module_or_skip`` ended with a bare ``pytest.skip(...)`` call, which reads as an implicit ``return None`` to static analysis and made CodeQL flag the function for mixing explicit and implicit returns. ``pytest.skip`` raises, so the added return is unreachable; it exists only to make every non-exceptional path explicit, and the comment says so. Behaviour unchanged: 49 passed, 13 skipped, identical to before. --------- Co-authored-by: agent <agent@local>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_materialize_hf_sym_qzerosdecides "this kernel owns its zero point, skip the rebuild" fromqzerosnot being a registered buffer. That describes Marlin, but_reselect_offloaded_marlin_kernels(#6253) swaps a Marlin layer to a portable kernel by assigning__class__, so the replacement inherits Marlin's zero-pointParameter.When the loader left that parameter on
meta— a deferred symmetricqzeros(#6274) is absent from the offload map, so nothing materializes it — the rebuild replaced it with a zero-length parameter, and the first forward failed inTorchLinear._decode_qzeros:The rebuild now uses the final kernel: a real Marlin/AWQ-Marlin layer keeps its own representation, and a reselected portable kernel drops the inherited parameter and rebuilds the words with
qzeros_standard_shapefor its own packed geometry.Repro (published checkpoint, no synthesis)
ModelCloud/Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1(meta.quantizer: gptqmodel:1.1.0,checkpoint_format: gptq,group_size: 32,desc_act: true,sym: true):0dd8e8ab(pre-wave)TorchLinear, logits sha1203615fe…3082b02d0(main)ValueError: GPTQ Marlin requires CUDA tensors, got cpu(issue #6383)IndexErrorabove on the first forwardTorchLinear, logits sha1203615fe…(identical to pre-wave)Same shape on
ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1(196 layers).Tests
tests/test_hf_bridge_reselected_qzeros_6383.py(new) — fails on main withqzeros == (0,)and passes with the fix.tests/test_hf_bridge_legacy_qzeros_marlin_6274.py::test_deferred_qzeros_are_skipped_for_a_kernel_that_owns_the_zero_point— the old stand-in was a Torch module with aParameterqzeros, which the fix now (correctly) rebuilds; it now uses a Marlin-typed stand-in so the Marlin skip stays covered without CUDA.ruff checkclean. CPU: 6 passed in the two files. GPU (cuda:0):test_hf_bridge_cpu_offload_5022.py,test_marlin_cpu_offload_post_init.py,test_requantize_offload_head_6348.py,test_hf_bridge_grouped_load_cost_6336.pyall pass.Note
This is independent of #6387 (different lines, different lifecycle point). #6387 is still required for the load to get this far on main; without it the layer never reaches the Torch kernel. A separate, narrower case remains: a legacy checkpoint whose layers are dispatched with a GPU placement and a deferred
qzerosnot in the map still fails inside Accelerate'sinit_hook(qzeros is on the meta device); that needs the deferral materialized beforedispatch_model, which is a larger change.