Skip to content

fix(load): take the global-state writer inside the construction helper (#5301) - #5303

Merged
Qubitium merged 1 commit into
mainfrom
fix/quantize-load-global-state-lock-5301
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/quantize-load-global-state-lock-5301

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5301. Regression from #5291 (which fixed #5268).

Problem

#5291 replaced the load RLock that compiled calls took with a reader/writer
lock, so construction coverage became per-site. The quantize-path load with
offload_to_disk=False (loader.py, "loading model directly to CPU") called
_hf_loader_from_pretrained_with_dynamic_module_retry without the writer, while
its neighbours did take it.

transformers runs construction inside local_torch_dtype (a process-global
default-dtype window), and dynamo's preserve_global_state restores the dtype
around every trace. A compiled forward re-tracing inside that window left bf16
installed after the load restored fp32, or tripped
assert_loaded_model_state -- the #5127/#5209 failure mode, back on this path.

Change

The writer now lives inside the helper, around each
loader.from_pretrained attempt, so every construction site is covered and a
new one cannot be added unguarded. The two call sites that already wrapped the
helper are de-indented (the lock is writer-reentrant, so keeping them would be
harmless but redundant). The file-repair step of the retry stays outside the
writer, so a force_download retry does not hold it across the copy.

Tests

New tests/test_quantize_load_global_state_5301.py:

  • test_helper_holds_the_writer_for_every_attempt -- a fake loader records
    model_global_state_lock()._writer and the calling thread id during both the
    first construction and the force_download=True retry; both must be the
    current thread, and the writer must be released afterwards.
  • test_helper_releases_the_writer_when_the_load_fails -- a non-retryable error
    propagates and the writer is not left held.
  • test_every_model_construction_site_is_guarded -- AST guard over
    gptqmodel/models/loader.py: every loader.from_pretrained/from_config
    call must sit inside a with model_global_state_lock(): block (which is how
    the helper holds it), and the helper must still be used by >= 3 construction
    sites. This is the "a new site cannot bypass it" check the issue asks for.

The first and third nodes fail on main; the second is a release guard.
Existing lock/race suites still pass: test_compile_lock_scope_5268,
test_compile_load_race_5209 (12 passed), and the quantize-load path itself
(test_weight_only, test_weight_only_looper: 96 passed, 9 skipped).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment