Repository navigation
fix(hf): defer a legacy qzeros only on a real floor-width mismatch - #6409
Merged
Merged
Conversation
added 2 commits
November 29, 2026 14:11
`_hf_legacy_qzeros_quant_linear` wraps every symmetric checkpoint from a pre-7.3.4 producer with `_HF_DEFER_SYM_QZEROS = True`. The deferral hides `qzeros` from the loader's expected-key set, which is only needed when the checkpoint stores the pre-#2983 floor width (`out_features // 32 * bits`). The two widths are identical for a 32-aligned projection, but the buffer was still hidden: the module kept its constructor (meta) `qzeros`, and `_materialize_hf_sym_qzeros` only runs from `hf_gptqmodel_post_init`, which Optimum calls *after* `accelerate.dispatch_model`. A GPU/disk placement then failed with ValueError: qzeros is on the meta device, we need a `value` to put in on 0. Measured on the published `ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1` (`meta.quantizer: gptqmodel:1.7.4`; all 196 `qzeros` already ceil-width) with `max_memory={0: "3GiB", "cpu": "14GiB"}`: the load fails before this change and loads with finite logits after it. The constructor now clears the deferral for a 32-aligned layer, so the checkpoint tensor is loaded normally; a genuine floor-width layer keeps the deferral and is still regenerated after loading. `tests/test_deferred_qzeros_dispatch_6396.py` pins both branches, including a GPU-gated mixed-map load+forward on the real checkpoint. Two `test_hf_bridge_cpu_offload_5022.py` fixtures that hand-build a legacy Marlin layer use a floor-width shape (or re-apply the loader marker), because a 32-aligned layer no longer defers.
A 32-aligned legacy checkpoint no longer defers `qzeros`, so the old "legacy deferred qzeros on CPU offload still raises" node no longer describes it: the refusal only guards a genuine floor-width buffer. Rename it to pin the new outcome (the checkpoint loads), and keep the floor-width deferral covered by the resident-CPU node in `test_deferred_qzeros_dispatch_6396.py`. The GPU-gated real-checkpoint node keeps only the 3GiB map: a 1GiB cap reselected all 196 layers to the portable kernel, whose optional inductor compile makes the node a multi-minute run without adding coverage of the dispatch failure (the 3GiB map still exercises it).
Qubitium
marked this pull request as ready for review
November 29, 2026 14:25
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
Fixes #6396Refs #6383 (mixed-map family; its
GPTQ Marlin requires CUDA tensorshalflanded in #6391)
A published pre-7.3.4 GPTQ checkpoint loaded with a GPU cap that forces
offload died inside
accelerate.dispatch_model:Exact repro (
/tmp/legacyoff/mixed.py 3GiB,ModelCloud/DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1,meta.quantizer: ["gptqmodel:1.7.4"],checkpoint_format: "gptq", bits=4,group_size=32, sym=true, desc_act=true; all 196
qzerostensors are already32-aligned and ceil-width):
origin/main6b25d2c693GiB FAIL ValueError qzeros is on the meta device, we need a 'value' to put in on 0.3GiB OK {'MarlinLinear': 196}_hf_legacy_qzeros_quant_lineardeferred every symmetric legacycheckpoint:
PackedGroupedQuantLinear._save_to_state_dictpoppedqzerosfromthe loader's expected-key set, so the checkpoint tensor was never loaded and
the module kept its constructor (meta) buffer.
_materialize_hf_sym_qzerosonly runs from
hf_gptqmodel_post_init, which Optimum calls frompost_init_model— afteraccelerate.dispatch_modelhas already tried toplace the meta buffer.
The deferral exists only to hide the pre-#2983 floor-width mismatch
(
out_features // 32 * bitsvsceil(out_features * bits / 32)). Both widthsare identical for a 32-aligned projection, so there is nothing to defer there.
What Changed
gptqmodel/nn_modules/qlinear/__init__.py(GPTQQuantLinear.__init__):when the [BUG] loader/HF bridge: GPTQ checkpoints saved by GPT-QModel before #2983 (e.g. published
ModelCloud/Granite-4.0-H-350M-GPTQMODEL-W4A16) no longer load: legacyqzeroswidth for out_features % 32 != 0 is rejected #6178 compatibility wrapper is built for a 32-alignedout_features, clear_HF_DEFER_SYM_QZEROSon the instance so thecheckpoint tensor is loaded normally. A genuine floor-width projection
(
out_features % 32 != 0) keeps the deferral and is still regenerated inhf_gptqmodel_post_init; the asymmetric floor-width rejection is unchanged._prepare_legacy_qzeros_state_dict: that hookruns in
_load_from_state_dict, i.e. after Transformers built theexpected-key set, so it cannot un-defer the key. The wrapper subclass is
constructed per layer with
out_featuresknown, which is the last pointbefore the key set exists. Moving
_materialize_hf_sym_qzerosearlier isunsafe because the kernel class can still change during post-init
(fix(hf): reselect offload-safe kernels for Marlin at post-init (#6152) #6253/fix(hf): reselect a meta Marlin layer whose resolved placement is CPU (#6383) #6387/fix(offload): reselect resident CPU Marlin with deferred qzeros (#6152 follow-up) #6391).
qzerosinstead of regenerating the constant. The values are identical (the
symmetric zero point is a constant) and the v1→v2 conversion still runs. A
matching-width checkpoint offloaded to CPU no longer trips the
"cannot be materialized on a CPU/disk-offloaded layer" refusal — it has
nothing to materialize.
Tests
New
tests/test_deferred_qzeros_dispatch_6396.py:test_matching_width_legacy_layer_keeps_its_qzeros_buffer— a 32-alignedlegacy layer keeps the marker cleared and
qzerosinstate_dict(); a112-wide one keeps the deferral.
test_matching_width_legacy_checkpoint_has_no_meta_qzeros_before_post_init—a synthetic pre-7.3.4 checkpoint loads with no resident layer whose
qzerosis meta, inspected at the post-init entry (the statedispatch_modelsees).test_floor_width_legacy_checkpoint_still_defers_and_loads— floor-widthcontrol (
intermediate_size=112): the deferral still fires, the buffer isrebuilt to
(2, 14)with one constant value, and the forward is finite.test_real_legacy_checkpoint_loads_on_a_mixed_gpu_cpu_map(GPU-gated): thepublished checkpoint at
max_memory={0: "3GiB", "cpu": "14GiB"}loads andforwards finite.
Fixture updates for the new behavior:
tests/test_hf_bridge_cpu_offload_5022.py: the two hand-built legacyMarlinLinearfixtures use a floor-widthout_features(112) or re-applythe loader marker, because a 32-aligned layer no longer defers.
tests/test_hf_bridge_legacy_qzeros_marlin_6274.py:test_legacy_deferred_qzeros_on_cpu_offload_still_raisesasserted therefusal for this aligned fixture; it is now
test_matching_width_legacy_checkpoint_loads_on_cpu_offload, pinning thenew outcome.
Red on
origin/main6b25d2c69(new file only, fix not applied):Green:
6b25d2c69(main)The 15 failures are identical on both trees: the pre-existing #6386
tokenizer-fixture rejection in
test_checkpoint_interop_4053.py(
ValueError: The tokenizer ... is unusable), plus the one AWQ node whoseexpected "Missing required quantized weights" message that fixture now
preempts.
test_cpu_offload_*skip on a CPU-only run (they need one CUDAdevice plus the CPU); they pass on the pinned GPU run below.
Also verified: a current (7.3.4+) checkpoint loads with no legacy/deferral
marker, no meta
qzeros, and finite logits.Measured Impact
Published
DeepSeek-R1-Distill-Qwen-7B-gptqmodel-4bit-vortex-v1, cuda:0(RTX 4090),
device_map="auto":6b25d2c69(main){0: "3GiB", "cpu": "14GiB"}ValueError: qzeros is on the meta device...MarlinLinear, finite logits{0: "1GiB", "cpu": "14GiB"}TorchLinear)TorchLinear, post-init reselect){0: "350MiB", "cpu": "14GiB"},offload_folderdevice_map="cpu"The 7B 1GiB/disk/CPU forwards reselect all 196 layers to the portable kernel.
On this host (load average ~1600 while measuring) one 7B forward at those caps
exceeds the measurement budget, so the offload caps are additionally verified
on the smaller published legacy checkpoint
ModelCloud/Llama-3.2-1B-Instruct-gptqmodel-4bit-vortex-v1(
meta.quantizer: gptqmodel:1.1.0, same layout, 112 layers), cuda:0:{0: "1GiB", "cpu": "8GiB"}MarlinLinear, finite logits{0: "350MiB", "cpu": "8GiB"},offload_folderTorchLinear, finite logitsdevice_map="cpu"TorchCpuAvx512Linear, finite logitsThe portable-kernel inductor compile was disabled in the measurement harness
for these three rows (it is per-layer, orthogonal to this change, and dominated
the wall time on the loaded host); everything else is the default HF/Optimum
path.
No VRAM or speed change: the loaded buffer replaces a regenerated constant of
the same shape and dtype.
Logic Flow Documentation
construction-time compatibility marker; the post-init rebuild path is
unchanged.
Review Requirements
nn_modules/qlinear) and the existing markerconventions (
_HF_LEGACY_QZEROS,_HF_CHECKPOINT_SYM).premise no longer holds.
32-aligned width is always ceil-equal, so no checkpoint loses the
translation path.
Kernel Accuracy Requirements
Not applicable: no kernel change. The loaded values are the checkpoint's own
symmetric constant (bit-identical to the regenerated word).
Notes
Measured on
6b25d2c69, which includes #6391. #6387 is not required for thiscase: the failure happens during dispatch, before any post-init reselect.