Repository navigation
fix(load): hold the global-state writer only for the default-dtype window (#5304) - #5329
Merged
Merged
Conversation
…ndow (#5304) #5291 took the model-global-state writer around whole transformers `from_pretrained`/`from_config` calls, and #5301 moved it into `_hf_loader_from_pretrained_with_dynamic_module_retry` so no call site could miss it. Only the `local_torch_dtype` window inside those calls is process-global state; the rest of a dense `from_pretrained` is weight I/O, dtype casts, device placement, packing and kernel post-init. A warm `optimize()`d decode step therefore stalled for the entire load: 23-36 s for a 7B model on the reporting host. The writer now belongs to the window. `install_model_global_state_dtype_guard()` wraps transformers' `local_torch_dtype` once at package import and takes the writer while the window is open, so every construction path (and any new one) is covered without wrapping the call. The per-call-site writer is removed from the loader helper, `from_config` sites, `build_shell_model`, the save-side shell rebuild, the compressed-tensors dequant path, the TT block-float path, the norm-tuning teacher and the DeepSeek-V4 MTP shell. Measured on a 1B GPTQ 4-bit checkpoint with a warm compiled decode loop and a concurrent dense `GPTQModel.load` of a 1.5B checkpoint: writer hold 1.50 s -> 21 ms, worst decode step 1516 ms -> 164 ms with only one 21 ms reader wait; the load itself is unchanged. The dtype window still excludes compiled forwards (#5127/#5209), verified by the existing race suites. `tests/test_compile_load_scope_5304.py` fails on the previous head for the load body (writer held during weight I/O) and passes here; the #5301 suite is rewritten from "the helper holds the writer" to "only the window does", and the #5268/#5209/#4204 suites pass unchanged. Fixes #5304
ZX-ModelCloud
force-pushed
the
fix/5304-global-state-writer-dtype-window
branch
from
November 19, 2026 02:02
62f3a30 to
31d496c
Compare
ZX-ModelCloud
marked this pull request as ready for review
November 19, 2026 02:03
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.
Summary
Fixes #5304.
model_global_state_lock()is a process-global reader/writer lock: compiled calls take the reader side, and the writer excludes them so a re-trace cannot capture and restore a construction'storch.set_default_dtypewindow (#5127, #5209).#5291 gave the writer to each transformers construction call, and #5301 moved it into
_hf_loader_from_pretrained_with_dynamic_module_retryso a new call site could not miss it. That scope is the bug: for a dense load the call is the entirefrom_pretrainedbody — weight I/O, dtype casts, device placement, packing, kernel post-init — while the process-global state only changes inside transformers'local_torch_dtypewindow. Everyoptimize()d forward in the process therefore waited for the whole load.Confirmed on current
main(4cf5c383a) before writing the fix: with a warm compiled decode loop (1B GPTQ 4-bit,optimize(backend="eager")) and a concurrent denseGPTQModel.loadof a 1.5B checkpoint, the writer was held for 1.50 s of a 1.92 s load and the worst decode step was 1516 ms against a 15 ms p50.What Changed
The writer now belongs to the dtype window itself, not to any call site.
gptqmodel/utils/torch.py:install_model_global_state_dtype_guard()wrapstransformers.modeling_utils.local_torch_dtypeonce per process and takes the writer while the window is open;model_global_state_dtype_guard_installed()reports the state.gptqmodel/__init__.py: installs that guard next to the other transformers patches at import time._hf_loader_from_pretrained_with_dynamic_module_retry(initial call and retry),from_quantized's twofrom_configsites,build_shell_model, the save-side shell rebuild (ModelWriter.get_model_with_quantize), the compressed-tensors dequant path, the TT block-float path, the norm-tuning teacher and the DeepSeek-V4 MTP shell. Those paths still serialize through_MODEL_LOAD_LOCK; only the window is exclusive against compiled calls.TorchLinear.optimize()keeps the writer while it installs the dynamo wrapper, unchanged.docs/concurrency.md,docs/torch-compile-fallback.md: new flow, before/after table and measured numbers.Behaviour is unchanged for correctness: the window is still exclusive against compiled forwards, and nested windows (e.g.
from_quantized→from_config) are re-entrant.Old flow (removed)
flowchart TD A["thread 1: GPTQModel.load"] --> B["_MODEL_LOAD_LOCK: whole load"] B --> C["global-state writer around the whole from_pretrained / from_config call (#5291, #5301)"] C --> D["weight I/O, dtype casts, device placement, local_torch_dtype, packing, kernel post-init"] E["thread 2: optimize()d forward"] --> F["global-state reader"] F -. "blocked for the entire load" .-> C style C fill:#f8d7da,stroke:#b02a37New flow (shipped)
flowchart TD A["thread 1: GPTQModel.load"] --> B["_MODEL_LOAD_LOCK: whole load"] B --> C["weight I/O, dtype casts, device placement: no writer"] C --> D["install_model_global_state_dtype_guard wraps local_torch_dtype"] D --> E["local_torch_dtype opens: acquire global-state writer"] E --> F["transformers sets default dtype; construct model"] F --> G["window closes: release writer"] G --> H["packing, kernel post-init, assert_loaded_model_state"] I["thread 2: optimize()d forward"] --> J["global-state reader"] J -. "waits only for the window" .-> E C -. "overlaps" .-> J H -. "overlaps" .-> J style E fill:#d1e7dd,stroke:#146c43from_pretrained/from_configcall site (gptqmodel/models/loader.pyhelper,utils/hf.py,models/writer.py,utils/model_dequant.py,utils/tt_blockfloat_quant.py,models/base.py,models/definitions/deepseek_v4.pyat4cf5c383a)local_torch_dtypewindow (gptqmodel/utils/torch.py:install_model_global_state_dtype_guard)Tests
tests/test_compile_load_scope_5304.py(new): a fake loader whose body stands in for weight I/O and whose construction opens a reallocal_torch_dtypewindow. It asserts (1) a reader enters while the load body runs, (2) a reader waits while the window is open, and (3) the helper does not hold the writer during the body.tests/test_quantize_load_global_state_5301.py(rewritten): the old contract ("the helper holds the writer for every attempt") is inverted to "only the window holds the writer", plus AST checks that noloader.from_pretrained/from_configsite re-widens it and that the guard is installed ontransformers.modeling_utils.local_torch_dtype.Red evidence on
origin/mainat4cf5c383a(same test file, fix reverted):Green:
Skips and failures that are not from this change (both reproduce on unmodified
origin/mainwith the same commands):tests/test_concurrency_guards.py::test_cpu_bf16x3_capability_probe_respects_an_active_scope— fails identically before the change (1 failed, 8 passed on base).tests/test_model_load_lock_4204.py::test_first_compile_cannot_restore_dtype_over_a_concurrent_loadandtests/test_compile_load_race_5209.py::test_optimized_forward_survives_repeated_concurrent_loads— segfault (exit 139 / -11) inside dynamo's compiled-code evaluation on the base commit too; the crash is in the interpreter/dynamo combination, not in the lock scope. Both are free-threaded-only suites.Measurement (same host, same checkpoint, three runs;
optimize(backend="eager")so the numbers reflect the lock and not inductor):The remaining spikes (100-400 ms) are the load's host work on a free-threaded interpreter; a control run with the same CPU burn and no load still shows them, and the reader-wait instrumentation records exactly one 21 ms wait (the window) in the whole load.
Not verified: no ROCm or multi-GPU run was needed for a CPU-host-side lock scope change; kernel numerics are untouched and the numeric suites are unaffected.
Model Support / Module Plan
Not applicable: no model type, alias, definition, module plan or fusion change. The DeepSeek-V4 MTP edit only removes an outer lock.
AWQ onboarding safety
Not applicable: no AWQ grouping, search, replay or folding change.
Logic Flow Documentation
docs/concurrency.mdanddocs/torch-compile-fallback.mdrepeat the diagrams; both are already linked fromREADME.md.Diagram summary: above (old flow, new flow, before/after table); the same diagrams are in
docs/concurrency.md§4 anddocs/torch-compile-fallback.md.Review Requirements
Kernel Accuracy Requirements
Not applicable: no kernel, dispatch or fusion change.
Notes