Repository navigation
fix(looper,gptq): restore EXL3 AUTO_NF12 and the in-memory v2 qzero layout (#4063) - #4069
Merged
Merged
Conversation
AUTO_NF12 replaced the layer inventory entry with the raw live module after the capture forward and again after the quant source was restored. prepare_layer_runtime_weights_for_forward only reaches a processor's deferred replay state through NamedModule, so EXL3's META placeholder from an earlier subset reached the next layer forward and raised NotImplementedError. Keep the NamedModule in full; _prepare_layer_direct_state_for_forward already unwraps .module for its direct-META exclusion. Add a bf16 EXL3 recovery test that takes the default auto_nf12 path.
…ation (#4063) pack_module converted packed v2 qzeros to v1 for a FORMAT.GPTQ export and left them there. A REQUIRES_FORMAT_V2 kernel decodes v2, so every in-process forward after quantize() read each zero point one step low while the saved checkpoint reloaded correctly. Leave the runtime v2 words in the live buffer, record the checkpoint layout in _checkpoint_format_on_disk, and convert a copy in state_dict()/save(). Add an in-memory vs reloaded logits regression and update the pack/format contract tests.
This was referenced Nov 14, 2026
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.
Part of #4063
TL;DR
Two accuracy/behaviour regressions from #4063, both "the saved checkpoint is right, the in-process model is wrong":
auto_dtype_cast=auto_nf12crashes because the layer inventory replacesNamedModuleentries with raw modules, which the deferred-weight sweep skips.quantize()decodes each zero point one step low.The live model now keeps the kernel's runtime qzero layout, and serialization emits a converted copy. The layer inventory keeps its
NamedModulewrappers.ruff check gptqmodel docs/eora tests setup.pypasses.1. Item 2 — EXL3 is bypassed by the default AUTO_NF12 path
Root cause
gptqmodel/looper/stage_subset.py:929-942(pre-fix) replaced the inventory entry with the raw live module after the capture forward:full[named_module.name] = named_module.module.gptqmodel/looper/stage_subset.py:1443-1450(pre-fix) did the same after_prepare_named_module_for_quantizationrestored the dense quant source.gptqmodel/looper/loop_processor.py:105-109(prepare_layer_runtime_weights_for_forward) skips every inventory entry that is not aNamedModule, because deferred replay state lives inNamedModule.state.So a projection packed by subset n kept EXL3's META placeholder for subset n+1's layer forward. Every bf16 checkpoint takes the AUTO_NF12 path.
flowchart TD A["create_named_modules: full[name] = NamedModule"] --> B["AUTO_NF12 swaps the live submodule"] B --> C["_prepare_named_module_for_forward"] C --> D{"inventory entry type?"} D -- "raw module (before)" --> E["prepare_layer_runtime_weights_for_forward skips it"] E --> F["META weight reaches HookedLinear.forward"] F --> G["NotImplementedError: Cannot copy out of meta tensor"] style G fill:#ffd6d6,stroke:#b00020,stroke-width:2px D -- "NamedModule (after)" --> H["processor.prepare_runtime_weight_for_forward"] H --> I["dense replay weight materialized on the forward device"] I --> J["layer forward runs every projection"]Fix
Keep the
NamedModuleinfullat both refresh sites. This is the first option in the issue. Why it is safe:_prepare_layer_direct_state_for_forward(gptqmodel/looper/module_looper.py:1367-1372) already unwrapscandidate.modulefor its direct-META exclusion, and_prepare_named_module_for_quantizationupdatesNamedModule.modulein step with the module it installs in the tree. A raw entry bought nothing and only hid the deferred state.Before / after
Command:
python repro_exl3.py <cast>(tiny 2-layer bf16 Llama,EXL3Config(bits=4.0, device="cuda:0"), quantize + save + reload).auto_nf12(default)NotImplementedError: Cannot copy out of meta tensor; no data!offInstrumented sweep on the same run:
Qwen2.5-0.5B-Instruct EXL3 4.0, wikitext-2 train calibration (128 x 512), PPL on 80 x 512 test windows (RTX 4090):
offauto_nf12(default)The issue's
offreference is 19.08 with its C4 calibration; this run uses wikitext-2 train calibration, so the absolute value differs, but the default now completes and is bit-identical in accuracy tooff.2. Item 3 — in-memory v1 qzeros are decoded as v2
Root cause
gptqmodel/utils/model.py:1379-1392(pre-fix) calledconvert_gptq_v2_to_v1_format_modulewhen the export format wasFORMAT.GPTQand the kernel declaredREQUIRES_FORMAT_V2. The packer writes logical (v2) words; the conversion rewrote the live buffer to v1 and setqzero_format() == 1.TorchLinear.forwardreads those words as v2, so every zero point is one low.save()wrote the v1 words, which is why reloading looked correct.flowchart TD A["pack_module: packer writes v2 words"] --> B["convert_gptq_v2_to_v1_format_module (before)"] B --> C["live qzeros = v1, qzero_format() = 1"] C --> D["TorchLinear.forward reads v1 as v2"] D --> E["every zero point one step low"] style E fill:#ffd6d6,stroke:#b00020,stroke-width:2px C --> F["save() writes v1 words"] F --> G["reload converts v1 to v2"] G --> H["reloaded model is correct"]Fix
Keep the runtime layout in memory and make serialization convert a copy:
pack_module(gptqmodel/utils/model.py:1381-1400) now callsmodule.qzero_format(format=2)and setsmodule._checkpoint_format_on_disk = FORMAT.GPTQinstead of converting the live buffer.GPTQQuantLinear.checkpoint_layout_qzeros(gptqmodel/nn_modules/qlinear/__init__.py:1250) returns the converted copy;_write_checkpoint_layout_qzerosuses it, sostate_dict()/save_pretrained()stay v1 for third-party readers._checkpoint_layout_buffer(gptqmodel/utils/model.py:2530) applies the same conversion to live buffers collected byget_state_dict_for_save/_collect_state_dict_with_offload.The disk-offload bundle is written from
state_dict()and therefore stays in checkpoint layout; the writer copies those refs unchanged. The saved file bytes are unchanged frommain.flowchart TD A["quantizer writes logical v2 qzeros"] --> B["pack_module"] B --> C["qzero_format(2) + checkpoint marker"] C --> D{"consumer"} D -- "forward" --> E["TorchLinear decodes v2"] D -- "state_dict / save_pretrained" --> F["checkpoint_layout_qzeros converts a copy"] D -- "save() / sharded save" --> G["get_state_dict_for_save converts live buffers"] D -- "offload_to_disk" --> H["state_dict-based bundle is v1"] F --> I["v1 safetensors"] G --> I H --> I I --> J["loader converts v1 to v2"] J --> EBefore / after
Qwen2.5-0.5B-Instruct,
RTNConfig(bits=4, group_size=128, offload_to_disk=False),BACKEND.TORCH, generation right afterquantize():The capital of France isensisareaневélectionịaensis...The capital of France is Paris. The capital of the United States is Washington D.C. ...The capital of France is Paris. The capital of the United States is Washington D.C. ...On the same run:
KERNEL TorchLinear qzero_format=2 REQUIRES_FORMAT_V2=True(was1),qzeros_equal=True, in-memory vs reloaded max logit delta0.62caused only by the bf16 reload casting the fp16 scales (live_scales_dtype=torch.float16,reload_scales_dtype=torch.bfloat16).The exact invariant is covered by the FP16 regression: with an FP16 source and an FP16 reload, in-memory logits are bit-identical (
max|inmem-reloaded| = 0.0) andqzerosmatch exactly.3. Failure and ambiguity contract
NamedModulestays in the inventory and is refreshedFORMAT.GPTQexport +REQUIRES_FORMAT_V2kernelFORMAT.GPTQ_V2exportstate_dict()/save_pretrained()4. Change inventory
gptqmodel/looper/stage_subset.pyNamedModulein the layer inventory (item 2)gptqmodel/utils/model.pygptqmodel/nn_modules/qlinear/__init__.pycheckpoint_layout_qzeros;_write_checkpoint_layout_qzerosuses ittests/test_exl3_auto_nf12_recovery.pytests/test_inmemory_qzero_format.pytests/test_format_conversion_flow.pytests/test_norm_lifecycle.pydocs/exl3-runtime-weights.mddocs/gptq-qzero-runtime-layout.mddocs/gptq-qzero-device-conversion.mdREADME.md5. Tests / guarantees
test_exl3_auto_nf12_recovery.py::test_exl3_bf16_quantizes_with_the_default_source_cast[default-auto-nf12]test_exl3_auto_nf12_recovery.py::test_exl3_bf16_quantizes_with_the_default_source_cast[off]offreference still workstest_inmemory_qzero_format.py::test_pack_keeps_the_kernel_runtime_qzero_layouttest_inmemory_qzero_format.py::test_state_dict_emits_the_checkpoint_qzero_layout(serialized + 1) & 0xF == livetest_inmemory_qzero_format.py::test_in_memory_logits_match_the_reloaded_checkpointtest_format_conversion_flow.py(updated)Fail-before evidence (same tests on
ad0525ccawith the fix stashed): 4 failed, 2 passed.6. Validation
CUDA (RTX 4090
GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e,/opt/venvs/cu130t):Broader related set (qzero/format/writer/planar/AUTO_NF12/EXL3 geometry/norm/interop/HF packing):
ROCm (R9700,
HIP_VISIBLE_DEVICES=1,/opt/venvs/rocm101t):Lint:
Docs: EXL3 runtime weight lifecycle and GPTQ qzeros runtime vs checkpoint layout.
7. Reviewer notes
_prepare_runtime_for_tuningused to reject a freshly requantized GPTQ model because its live qzeros were v1. With the fix the module is v2, so in-process norm tuning is valid and the guard no longer fires. The guard still rejects genuinely v1 modules (for example one converted by the publicconvert_gptq_v2_to_v1_formatHF helper).state_dict()and therefore stay in checkpoint layout; this PR does not change the saved checkpoint bytes.undo_offload_to_diskwas not changed: it already restores raw tensors, and its qzero-format interaction predates this PR.