Skip to content

fix(load): gate post_init on each kernel's checkpoint payload (#5261) - #5276

Merged
Qubitium merged 1 commit into
mainfrom
fix/5261-post-init-payload
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5261-post-init-payload

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

fix(load): gate post_init on each kernel's checkpoint payload (#5261)

Summary

#5247 added a general _post_init_offloaded for non-Marlin kernels so a
disk-offloaded layer could be materialized through its Accelerate hook before
post_init. The helper opened with if not module.qweight.is_meta:, but most
kernel families do not store their payload in qweight. On a plain
single-GPU load (no offload at all) every NF12, QQQ, VPTQ, BitsAndBytes,
MacaQ, ... checkpoint raised:

AttributeError: 'NF12Linear' object has no attribute 'qweight'

before post_init was ever reached. This PR replaces the name probe with a
kernel-level payload contract.

Root cause

gptqmodel_post_init (gptqmodel/utils/model.py) routed every
BaseQuantLinear through _post_init_offloaded. That helper assumed the GPTQ
storage name qweight, so it raised AttributeError for every family whose
payload is named differently:

Family Payload
NF12 nf12_packed, nf12_escapes, nf12_exp_base
QQQ B, s_channel, s_group
VPTQ indices + codebooks / norm / permutation
FP8 weight, weight_scale_inv (has a PEFT qweight property)
BitsAndBytes weight + quant-state buffers
MacaQ W_packed, W_scales, BH, pre_scale

The regression is load-time only: quantization and save already worked.

Before

flowchart TD
    A["gptqmodel_post_init(model)"] --> B["for each BaseQuantLinear"]
    B --> C["_post_init_offloaded(module)"]
    C --> D["module.qweight.is_meta"]
    D -- "GPTQ / Marlin / EXL3" --> E["resident -> post_init()"]
    D -- "NF12 / QQQ / VPTQ / BnB" --> F["AttributeError:<br/>no attribute 'qweight'"]
    F --> G["GPTQModel.load() fails<br/>even on a plain cuda:0 load"]
    style F fill:#f8d7da,stroke:#b02a37,stroke-width:2px
    style G fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

After

flowchart TD
    A["gptqmodel_post_init(model)"] --> B["for each BaseQuantLinear"]
    B --> C["module.checkpoint_payload_is_meta()"]
    C --> D["checkpoint_payload_tensors()<br/>resolve CHECKPOINT_REQUIRED_BUFFERS"]
    D --> E{"any payload tensor<br/>is meta?"}
    E -- "no, resident" --> F["post_init() in place<br/>(exactly once)"]
    E -- "yes, disk-offloaded" --> G{"SUPPORTS_CPU_OFFLOAD?"}
    G -- "no" --> H["RuntimeError<br/>fail closed (#5194)"]
    G -- "yes" --> I["hook.pre_forward<br/>-> post_init()<br/>-> hook.post_forward"]
    style F fill:#d1e7dd,stroke:#146c43,stroke-width:2px
    style I fill:#d1e7dd,stroke:#146c43,stroke-width:2px
    style H fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

What changed

File Change
gptqmodel/nn_modules/qlinear/__init__.py new BaseQuantLinear.checkpoint_payload_tensors() and checkpoint_payload_is_meta(); dotted-name resolver _named_tensor
gptqmodel/utils/model.py _post_init_offloaded and _post_init_cpu_offloaded_marlin call the probe instead of module.qweight.is_meta
tests/test_post_init_payload_5261.py new regression matrix + contract/meta/offload unit tests
tests/test_hf_bridge_cpu_offload_5022.py the fake _OffloadableLayer implements the new contract
docs/kernel-dispatch-cpu.md new "The post-init payload probe" section with before/after diagrams and the per-family payload table

The contract deliberately reuses CHECKPOINT_REQUIRED_BUFFERS, the same
declaration validate_checkpoint_qweights already enforces, so the probe
cannot drift from the loader's required payload. A kernel that declares
nothing (or whose declared tensors were replaced by a derived cache) falls
back to every tensor the module registers. Dotted names are supported because
VPTQ's codebooks are submodule tensors (centroids.weight).

Resident layers are unchanged: the probe is False, post_init runs in
place, and the materialize/restore path is never entered. Disk-offloaded
layers still run post_init while materialized and are returned to meta, so
the offload map stays authoritative (#5194/#5247 behaviour preserved).

Red -> green evidence

Repro: .scratch/repro_5261.py, one 2-layer Qwen2.5-0.5B-Instruct checkpoint
per family, then GPTQModel.load(ckpt, device="cuda:0") + forward.

Format Loaded kernel main c1807f889 e24743120^ this branch
GPTQ (control) MarlinLinear OK, finite OK, finite OK, finite
NF12 NF12Linear AttributeError OK, finite OK, finite
QQQ QQQLinear AttributeError OK, finite OK, finite
VPTQ VPTQCudaLinear AttributeError OK, finite OK, finite
FP8 TorchFP8Linear OK, finite (qweight property) OK, finite OK, finite
BitsAndBytes BitsAndBytesLinear AttributeError OK, finite OK, finite

tests/test_post_init_payload_5261.py:

Run Result
pristine main 14 failed, 2 passed
this branch 16 passed

The two rows that pass on main are the GPTQ/Marlin control and FP8 (its
qweight property hides the bug). The four AttributeError rows plus the
contract/meta/offload unit tests are the red->green delta.

Test counts

Suite Result
tests/test_post_init_payload_5261.py (new) 16 passed
tests/test_hf_bridge_cpu_offload_5022.py (#5247) 16 passed
load tests (test_load_*, test_loader_*) 42 passed, 7 skipped
offload tests (test_offload_{base,quantized,disk,run_namespace}) 27 passed
QQQ + NF12 checkpoint tests 37 passed
FP8 tests (test_fp8* selection) 42 passed
VPTQ -k "roundtrip or reload or save" 46 passed
family sweep (-k "load or save or reload or roundtrip or checkpoint" over test_*nf12*/*qqq*/*vptq*/*fp8*) 160 passed, 4 skipped, 1078 deselected
BitsAndBytes GPTQModel.load reload 1 passed
uvx [email protected] check gptqmodel docs/eora tests setup.py passed

Not verified

  • MacaQ is covered by the CPU payload-contract unit test only; no end-to-end
    MacaQ checkpoint load was run.
  • FP4 (TorchFP4Linear) is a plain nn.Module, not a BaseQuantLinear, so
    it never enters this branch; W4A* inherits the GPTQ payload. Both were
    reasoned about, not loaded.
  • Disk offload (device_map="disk") for non-Marlin families was not exercised
    end-to-end. The meta materialize/restore path is covered by a real NF12
    layer on CPU (accelerate.cpu_offload(..., offload_buffers=True)) and by
    fake layers; the fail-closed branch is covered as well.
  • tests/test_offload_files.py (2595 lines) and tests/test_loader.py were
    not run in full; the focused files above were.
  • All verification used the offline HF cache; no network paths were tested.

`_post_init_offloaded` decided whether a layer was a disk-offloaded meta
placeholder by reading `module.qweight`. NF12, QQQ, VPTQ, BitsAndBytes,
MacaQ and other families store their payload under different names, so every
plain single-GPU `GPTQModel.load()` of such a checkpoint raised
`AttributeError: '<Kernel>' object has no attribute 'qweight'` before
`post_init` ran.

- `BaseQuantLinear.checkpoint_payload_tensors()` resolves the family's
  `CHECKPOINT_REQUIRED_BUFFERS` (the names the checkpoint validator already
  requires, dotted submodule names included) and falls back to every
  registered tensor.
- `checkpoint_payload_is_meta()` reports whether Accelerate replaced that
  payload with meta placeholders.
- `_post_init_offloaded` and `_post_init_cpu_offloaded_marlin` use the probe,
  so resident layers still run `post_init` once and disk-offloaded layers are
  still materialized, initialized, and returned to meta.
- Regression tests load one checkpoint per family (GPTQ/Marlin control, NF12,
  QQQ, VPTQ, FP8, BitsAndBytes), assert a family-specific post-init side
  effect and a finite forward, and cover the meta/offload and fail-closed
  branches. Docs describe the payload contract.
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