Skip to content

fix(compile): let compiled calls share the model-global-state lock (#5268) - #5291

Merged
Qubitium merged 1 commit into
mainfrom
fix/5268-compile-lock-scope
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5268-compile-lock-scope

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

fix(compile): scope the model-global lock to the dtype window (#5268)

Fixes #5268. Regression from #5251 (fix of #5209).

Problem

#5251 made every compiled call hold model_global_state_lock(), which is
the process-wide _MODEL_LOAD_LOCK RLock a load holds for its whole
body. Two consequences:

  1. warm compiled forwards serialize across threads and models;
  2. every compiled forward stalls for the whole duration of any concurrent
    load or save (weight I/O, packing, kernel post-init), which can be minutes.

The dtype hazard #5251 closed only exists while transformers'
local_torch_dtype saves/sets/restores torch.set_default_dtype during model
construction; the rest of a load does not touch it.

Before

flowchart TD
    A["thread A: GPTQModel.load"] --> B["_MODEL_LOAD_LOCK held for whole load"]
    B --> C["from_config / from_pretrained sets default dtype bf16"]
    C --> D["weight I/O, packing, kernel post-init"]
    D --> E["assert_loaded_model_state"]

    F["thread B: warm compiled call"] --> G["model_global_state_lock() = _MODEL_LOAD_LOCK"]
    G --> H["compiled forward"]
    B -. "serializes every compiled call" .-> G
    style G fill:#f8d7da,stroke:#b02a37
Loading

After

flowchart TD
    A["thread A: GPTQModel.load"] --> B["_MODEL_LOAD_LOCK held for whole load"]
    B --> C["global-state writer around construction only"]
    C --> D["weight I/O, packing, kernel post-init (no writer)"]
    D --> E["assert_loaded_model_state"]

    F["thread B: warm compiled call"] --> G["global-state reader"]
    G --> H["compiled forward"]
    I["thread C: warm compiled call"] --> J["global-state reader"]
    J --> K["compiled forward"]
    C -. "excludes only the dtype window" .-> G
    D -. "does not exclude" .-> G
    G --> L["readers run concurrently"]
    J --> L
    style G fill:#d1e7dd,stroke:#146c43
    style J fill:#d1e7dd,stroke:#146c43
Loading

Change

_MODEL_GLOBAL_STATE_LOCK (gptqmodel/models/loader.py) is a process-global
reader/writer lock; model_global_state_lock() (gptqmodel/utils/torch.py)
resolves it.

  • Compiled proxies take the reader side for the call
    (_install_callable_compile_fallback.call,
    _install_module_compile_fallback.forward).
  • Construction takes the writer side for the construction call only:
    from_pretrained, from_quantized's from_config (+ retry),
    build_shell_model, the save-side shell rebuild, compressed-tensors dequant,
    TT block-float, the norm-tuning teacher and the DeepSeek-V4 MTP shell.
    TorchLinear.optimize() takes it while installing the dynamo wrapper.
  • Writer is re-entrant for its thread (a load may run compiled code); reader
    nests. Reader->writer upgrade raises, and a load started from inside a
    compiled call raises instead of risking a lock-order deadlock.
  • Writer preference keeps a construction window from starving.
  • from_quantized still does not call torch._dynamo.reset().

Docs: docs/concurrency.md section 4 and docs/torch-compile-fallback.md
describe the new flow; the "one uncontended acquire per call" claim is replaced
with the reader/writer contract.

Evidence

Red (worktree at 0aa2d4b with the new test file):

tests/test_compile_lock_scope_5268.py
  test_two_compiled_forwards_overlap
    AssertionError: the two compiled forwards serialized:
      per-call {'t1': 0.2, 't2': 0.4}
  test_compiled_forward_does_not_wait_for_the_load_mutex
    AssertionError: a compiled forward waited for the whole load lock; only
      the default-dtype construction window may block it

Green: the same file is 4 passed, and tests/test_compile_load_race_5209.py
is 5 passed (the #5209 contract is unchanged).

Timing (CPU, warm compiled callable, 3 interleaved runs):

probe before after
two threads x one 200 ms compiled call, wall 0.401 / 0.401 / 0.401 s 0.201 / 0.201 / 0.201 s
per-thread wall 0.200 + 0.400 s 0.200 + 0.200 s
per-call overhead, 50k warm calls 9.95 / 10.83 us 10.00 / 10.07 / 10.91 us
warm call behind a held _MODEL_LOAD_LOCK 300.3 / 300.3 ms (blocked) 0.184 / 0.401 ms
warm call behind the writer (dtype window) 300.9 / 300.2 ms (blocked) 300.2 / 300.6 ms (blocked, intended)

Validation:

command result
uvx [email protected] check gptqmodel docs/eora tests setup.py All checks passed
pytest compile + optimize families + loader/concurrency/docs (15 files) 102 passed, 8 skipped
pytest tests/test_compile_load_race_5209.py tests/test_compile_lock_scope_5268.py tests/test_lazy_turtle_conversion_cache.py tests/test_loader_tokenizer_flag_4941.py 22 passed
pytest tests/test_deepseek_v4_support.py 43 passed

Reviewer notes

…5268)

#5251 fixed #5209 by taking `model_global_state_lock()` around every compiled
call. That resolved to `_MODEL_LOAD_LOCK`, the `RLock` a load holds for its
whole body, so two warm compiled forwards could never overlap and a warm
forward waited for an unrelated load's weight I/O, packing and kernel
post-init (#5268). The dtype hazard only exists while transformers'
`local_torch_dtype` saves/sets/restores `torch.set_default_dtype` during
construction, not for the rest of a load.

Replace the shared `RLock` with a process-global reader/writer lock
(`_MODEL_GLOBAL_STATE_LOCK`):

* compiled callable/module proxies take the shared reader side for the call;
* every transformers construction site takes the writer for the construction
  call only: from_pretrained, from_quantized's from_config (+ retry),
  build_shell_model, the save-side shell rebuild, compressed-tensors dequant,
  TT block-float, the norm-tuning teacher and the DeepSeek-V4 MTP shell;
  `TorchLinear.optimize()` takes it while installing the dynamo wrapper;
* the writer is re-entrant for its thread, readers nest, a reader->writer
  upgrade raises, and a load started inside a compiled call raises instead of
  risking a lock-order deadlock;
* `from_quantized` still never calls `torch._dynamo.reset()`.

Two warm compiled forwards now overlap (0.401 s -> 0.201 s for two 200 ms
calls) while a warm forward still waits for a construction's dtype window
(300 ms held -> 300 ms) and no longer waits for the load mutex (300 ms held ->
0.2 ms). Per-call overhead is unchanged (~10 us). `tests/test_compile_lock_scope_5268.py`
is 2 behavioral failures on 0aa2d4b and 4 passed after; the #5209 suite
stays 5 passed.
Comment thread gptqmodel/utils/torch.py Resolved
Comment thread tests/test_compile_lock_scope_5268.py Resolved
Comment thread tests/test_compile_lock_scope_5268.py Resolved
Comment thread tests/test_compile_lock_scope_5268.py Resolved
Comment thread tests/test_compile_lock_scope_5268.py Resolved
@Qubitium
Qubitium merged commit c9bb73f into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5268-compile-lock-scope branch November 18, 2026 23:37
ZX-ModelCloud added a commit that referenced this pull request Nov 19, 2026
…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
Qubitium pushed a commit that referenced this pull request Nov 19, 2026
…ndow (#5304) (#5329)

#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant