Skip to content

fix(structure): publish materialized parameters atomically (#5088) - #5132

Merged
Qubitium merged 1 commit into
mainfrom
fix/5088-atomic-materialize
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5088-atomic-materialize

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5088.

A Ctrl-C (or any exception) during lazy checkpoint materialization could leave a
module owning torch.empty weights. ModelLoader._copy_checkpoint_tensors_into_submodule
allocated replacement storage, installed it on the module with setattr, and
only then copied the checkpoint values. The installed tensor is no longer
meta, so run-exit recovery — whose only "not loaded" test is is_meta —
treated the module as materialized. quantize() re-raised the interrupt and
the in-session model forwarded with uninitialized weights, silently producing
NaN or plausible-looking wrong logits.

This affects the weight-only post-loop tail (RTN/GGUF/FP8) and the GPTQ
first-capture layer, both of which materialize through the same helper. The
defect reproduces on current main.

What Changed

  • gptqmodel/utils/structure.py: new _publish_checkpoint_param_ allocates and
    fills the replacement parameter, then publishes it with setattr. Both the
    concatenated-shard and grouped-shard branches call it. An interrupt during
    the copy leaves the old meta parameter, which the existing recovery already
    re-materializes.
  • Buffer materialization already copied into a local tensor before
    register_buffer; unchanged.
  • tests/test_run_exit_recovery_contract_5060.py: two regression nodes inject
    a KeyboardInterrupt at the first checkpoint copy inside materialization,
    one for the weight-only post-loop tail and one for the GPTQ first-capture
    layer. They assert the injection fired, every floating parameter is finite,
    and the recovered weights still match the checkpoint.
  • docs/cancelled-run-contract.md: records the atomic-publish contract, the
    old/new state flow and the before/after table.

Tests

Red evidence on the pre-fix revision:

python -m pytest tests/test_run_exit_recovery_contract_5060.py -q -rs -k materialization_interrupt
# 2 failed
#  non-finite parameters after recovery: ['model.embed_tokens.weight']
#  non-finite parameters after recovery: ['model.layers.0.self_attn.q_proj.weight']

Green with the fix:

python -m pytest tests/test_run_exit_recovery_contract_5060.py -q -rs -k materialization_interrupt
# 2 passed

python -m pytest \
  tests/test_run_exit_recovery_contract_5060.py \
  tests/test_post_loop_tail_failure_5023.py \
  tests/test_weight_only_failed_run_device_5011.py \
  tests/test_lazy_turtle_materialize_map.py \
  tests/test_offload_base_modules_restored.py -q -rs
# 41 passed

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

A native GPU run of the same two interrupt windows (weight-only post-loop tail
and GPTQ first-capture layer) left no meta or non-finite parameters and the
recovered forward was finite; the pre-fix code produced NaN in both windows.
The new pytest nodes are CPU-only so they stay in the default suite.

Pre-existing, unrelated: tests/test_auto_dtype_cast.py has three failures on
the unmodified base revision (NF12 fused-diff and GPU-encoding tests); they are
not touched by this change.

Model Support / Module Plan

Not applicable: no model type, alias, definition, module-plan, or fusion change.

AWQ onboarding safety

Not applicable: no AWQ grouping, search, replay, or fold change.

Logic Flow Documentation

  • This change does not alter a major logic flow, or the required diagrams
    and docs page are included below.
  • New-flow Mermaid diagram(s) describe the shipped code using real symbol names.
  • The removed/replaced mechanism is shown or tabulated with file:line evidence.
  • Failure and ambiguity paths are marked and visually distinct from success.
  • A reference page under docs/ repeats the diagrams, titled for the
    mechanism, and is linked from README.md.

Diagram summary: this is a local ordering fix inside one materialization helper,
but it changes the failure state of tensor publication, so the state flow and a
before/after table are recorded in docs/cancelled-run-contract.md (already
linked from README.md).

stateDiagram-v2
    state "module owns meta parameter" as Meta
    state "replacement filled off-module" as Filling
    state "module owns loaded parameter" as Loaded
    [*] --> Meta
    Meta --> Filling: allocate storage + copy checkpoint
    Filling --> Loaded: publish (setattr / register_parameter)
    Filling --> Meta: interrupt (discard replacement)
    Loaded --> [*]
Loading
Materialization step Before After
Lazy parameter fill (gptqmodel/utils/structure.py::_copy_checkpoint_tensors_into_submodule, concatenated and grouped-shard branches) torch.empty_like was installed with setattr and only then filled by copy_. An interrupt between them left non-meta torch.empty weights that recovery skipped, so the forward silently returned NaN or wrong logits. gptqmodel/utils/structure.py::_publish_checkpoint_param_ allocates and fills the replacement, then publishes it. An interrupt leaves the old meta parameter, which recovery re-materializes.

Review Requirements

I have:

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

Not applicable: no kernel or fusion change.

Notes

The regression nodes use a tiny real Llama checkpoint and inject the interrupt
at the first Tensor.copy_ inside _copy_checkpoint_tensors_into_submodule, so
they exercise the materialization window itself rather than a helper boundary.

A lazy shell owns meta parameters. `_copy_checkpoint_tensors_into_submodule`
allocated replacement storage, installed it on the module, and copied the
checkpoint values in a second step. An interrupt between the two left
`torch.empty` weights that are no longer meta, so run-exit recovery (whose
only "not loaded" test is `is_meta`) treated the module as materialized and
the in-session model forwarded with uninitialized weights.

Fill the replacement before publishing it. An interrupt during the copy now
leaves the old meta parameter, which the recovery already re-materializes.
Buffer materialization already copied before `register_buffer` and is
unchanged.

Add regression nodes for the weight-only post-loop tail and the GPTQ
first-capture layer; both assert every floating parameter is finite and the
recovered weights still match the checkpoint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants