Skip to content

fix(looper,gptq): restore EXL3 AUTO_NF12 and the in-memory v2 qzero layout (#4063) - #4069

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4063-exl3mem
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4063-exl3mem

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Part of #4063

TL;DR

Two accuracy/behaviour regressions from #4063, both "the saved checkpoint is right, the in-process model is wrong":

  1. EXL3 + default auto_dtype_cast=auto_nf12 crashes because the layer inventory replaces NamedModule entries with raw modules, which the deferred-weight sweep skips.
  2. A fresh GPTQ pack leaves v1 qzeros in memory for kernels that require v2, so every forward after 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 NamedModule wrappers. ruff check gptqmodel docs/eora tests setup.py passes.

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_quantization restored the dense quant source.
  • gptqmodel/looper/loop_processor.py:105-109 (prepare_layer_runtime_weights_for_forward) skips every inventory entry that is not a NamedModule, because deferred replay state lives in NamedModule.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"]
Loading

Fix

Keep the NamedModule in full at 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 unwraps candidate.module for its direct-META exclusion, and _prepare_named_module_for_quantization updates NamedModule.module in 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).

cast before after
auto_nf12 (default) NotImplementedError: Cannot copy out of meta tensor; no data! quantize+save+reload OK, finite logits
off OK OK

Instrumented sweep on the same run:

before: DIAG prepared=0 raw_META=['self_attn.q_proj', 'self_attn.k_proj', 'self_attn.v_proj']
after:  DIAG prepared=3 raw_META=[] named_META=['self_attn.q_proj', 'self_attn.k_proj', 'self_attn.v_proj']
        DIAG prepared=1 raw_META=[] named_META=['self_attn.o_proj']
        DIAG prepared=2 raw_META=[] named_META=['mlp.gate_proj', 'mlp.up_proj']

Qwen2.5-0.5B-Instruct EXL3 4.0, wikitext-2 train calibration (128 x 512), PPL on 80 x 512 test windows (RTX 4090):

cast PPL
off 18.9100
auto_nf12 (default) 18.9100

The issue's off reference 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 to off.

2. Item 3 — in-memory v1 qzeros are decoded as v2

Root cause

gptqmodel/utils/model.py:1379-1392 (pre-fix) called convert_gptq_v2_to_v1_format_module when the export format was FORMAT.GPTQ and the kernel declared REQUIRES_FORMAT_V2. The packer writes logical (v2) words; the conversion rewrote the live buffer to v1 and set qzero_format() == 1. TorchLinear.forward reads 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"]
Loading

Fix

Keep the runtime layout in memory and make serialization convert a copy:

  • pack_module (gptqmodel/utils/model.py:1381-1400) now calls module.qzero_format(format=2) and sets module._checkpoint_format_on_disk = FORMAT.GPTQ instead of converting the live buffer.
  • GPTQQuantLinear.checkpoint_layout_qzeros (gptqmodel/nn_modules/qlinear/__init__.py:1250) returns the converted copy; _write_checkpoint_layout_qzeros uses it, so state_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 by get_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 from main.

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 --> E
Loading

Before / after

Qwen2.5-0.5B-Instruct, RTNConfig(bits=4, group_size=128, offload_to_disk=False), BACKEND.TORCH, generation right after quantize():

output
before The capital of France isensisareaневélectionịaensis...
after The capital of France is Paris. The capital of the United States is Washington D.C. ...
after save+reload 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 (was 1), qzeros_equal=True, in-memory vs reloaded max logit delta 0.62 caused 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) and qzeros match exactly.

3. Failure and ambiguity contract

Situation Before After
AUTO_NF12 projection packed by an earlier subset raw inventory entry skipped, META reached the forward NamedModule stays in the inventory and is refreshed
FORMAT.GPTQ export + REQUIRES_FORMAT_V2 kernel live buffer left in v1, in-process inference wrong live buffer stays v2, serialization converts a copy
FORMAT.GPTQ_V2 export unchanged unchanged
state_dict() / save_pretrained() live buffer written unchanged checkpoint-layout copy written
Norm tuning right after a fresh requantize blocked with "save then reload" because the module was v1 allowed: the module is v2 and safe to consume in process
Kernel does not require v2 no conversion no conversion

4. Change inventory

File Purpose
gptqmodel/looper/stage_subset.py keep NamedModule in the layer inventory (item 2)
gptqmodel/utils/model.py mark the checkpoint qzero layout at pack; convert live buffers in the writer
gptqmodel/nn_modules/qlinear/__init__.py checkpoint_layout_qzeros; _write_checkpoint_layout_qzeros uses it
tests/test_exl3_auto_nf12_recovery.py new bf16 EXL3 default-AUTO_NF12 regression
tests/test_inmemory_qzero_format.py new in-memory vs reloaded logits/qzeros regression
tests/test_format_conversion_flow.py pack now marks the checkpoint layout instead of converting
tests/test_norm_lifecycle.py fresh GPTQ pack is v2, so reload-only tuning guard no longer applies
docs/exl3-runtime-weights.md AUTO_NF12 inventory flow
docs/gptq-qzero-runtime-layout.md new runtime-vs-checkpoint layout page
docs/gptq-qzero-device-conversion.md serialization-time conversion flow
README.md links to both pages

5. Tests / guarantees

Test Invariant
test_exl3_auto_nf12_recovery.py::test_exl3_bf16_quantizes_with_the_default_source_cast[default-auto-nf12] a bf16 source completes quantize+save+reload on the default path
test_exl3_auto_nf12_recovery.py::test_exl3_bf16_quantizes_with_the_default_source_cast[off] the off reference still works
test_inmemory_qzero_format.py::test_pack_keeps_the_kernel_runtime_qzero_layout live kernel buffer is v2 with a v1 checkpoint marker
test_inmemory_qzero_format.py::test_state_dict_emits_the_checkpoint_qzero_layout independent nibble oracle: (serialized + 1) & 0xF == live
test_inmemory_qzero_format.py::test_in_memory_logits_match_the_reloaded_checkpoint in-memory logits and qzeros are bit-identical to the reloaded checkpoint
test_format_conversion_flow.py (updated) pack marks the checkpoint layout, never converts the live buffer

Fail-before evidence (same tests on ad0525cca with the fix stashed): 4 failed, 2 passed.

FAILED tests/test_exl3_auto_nf12_recovery.py::...[default-auto-nf12]   NotImplementedError: Cannot copy out of meta tensor
FAILED tests/test_inmemory_qzero_format.py::test_pack_keeps_the_kernel_runtime_qzero_layout   assert 1 == 2
FAILED tests/test_inmemory_qzero_format.py::test_state_dict_emits_the_checkpoint_qzero_layout
FAILED tests/test_inmemory_qzero_format.py::test_in_memory_logits_match_the_reloaded_checkpoint

6. Validation

CUDA (RTX 4090 GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e, /opt/venvs/cu130t):

pytest tests/test_exl3_auto_nf12_recovery.py tests/test_inmemory_qzero_format.py \
       tests/test_format_conversion_flow.py tests/test_norm_lifecycle.py \
       tests/test_qzero_pack_device.py tests/test_auto_dtype_cast.py \
       tests/test_exl3_generic_geometry.py -q
96 passed

Broader related set (qzero/format/writer/planar/AUTO_NF12/EXL3 geometry/norm/interop/HF packing):

pytest tests/test_qzero_pack_device.py tests/test_qzero_offsets.py \
       tests/test_format_conversion_flow.py tests/test_save_state_dict_buffer_collection.py \
       tests/test_writer_state_overlay.py tests/test_torch_fused_asym_qzeros.py \
       tests/test_planar_format_gptq_p.py tests/test_planar_bits_567.py \
       tests/test_auto_dtype_cast.py tests/test_exl3_generic_geometry.py \
       tests/test_norm_lifecycle.py tests/test_checkpoint_interop_4053.py \
       tests/test_hf_packing_api.py -q
286 passed, 3 skipped

ROCm (R9700, HIP_VISIBLE_DEVICES=1, /opt/venvs/rocm101t):

pytest tests/test_exl3_auto_nf12_recovery.py tests/test_inmemory_qzero_format.py -q
6 passed

Lint:

ruff check gptqmodel docs/eora tests setup.py
All checks passed!

Docs: EXL3 runtime weight lifecycle and GPTQ qzeros runtime vs checkpoint layout.

7. Reviewer notes

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.
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.

1 participant