Skip to content

[PERF] #5291 (fix of #5268) incomplete: the global-state writer wraps whole dense from_pretrained calls (weight I/O), not the 14 ms dtype window - an optimize()d decode step stalls 23-36 s during a concurrent dense GPTQModel.load #5304

Closed

Description

@Qubitium

Found on main @ c03416c, RTX 5090 (sm_120), torch 2.16 + CUDA 13.0, Python 3.14t (PYTHON_GIL=0), transformers 5.22.

What is wrong

#5291 (the fix for #5268) holds the model-global-state writer "for the construction call only". That works for from_quantized, whose construction call is from_config: the writer was held for 7 ms in my run. But three sites wrap a whole transformers from_pretrained:

  • gptqmodel/models/loader.py:2410 (GPTQModel.load(dense_model) with no quantize_config);
  • loader.py:2545 (the meta-shell fallback);
  • models/base.py:3137 (the norm-tuning teacher).

from_pretrained is the entire dense load: weight I/O, dtype casts and device placement. The process-global dtype window that needs excluding is transformers' local_torch_dtype(...) inside get_init_context, which only covers model construction on the meta device. For Mistral-7B-v0.3 that window is 14 ms of a 1.36 s (page cache warm) to 23 s (cold) from_pretrained.

So every optimize()d forward in the process still stalls for the full duration of any concurrent dense GPTQModel.load(...). That is the #5268 symptom, unchanged for dense loads. #5301 needs the same writer around the offload_to_disk=False quantize-path from_pretrained (loader.py:2596), which would add another full-load stall.

Measurement

Thread A runs a warm optimize()d decode loop (Llama-3.2-1B GPTQ 4-bit, MarlinLinear, DynamicCache, ~15 ms/step). Thread B loads another model. The writer's acquire/release was instrumented.

thread B load time writer held worst decode step in A
GPTQModel.load("Mistral-7B-v0.3", device="cpu", dtype=fp16) (cold) 23.45 s 23 252 ms 23 278 ms
GPTQModel.load("Mistral-7B-v0.1", device="cpu", dtype=fp16) (cold) 36.44 s (not instrumented) 36 263 ms
GPTQModel.load(<GPTQ Llama-3.1-8B 8-layer ckpt>, device="cuda:0") (from_quantized) 1.49 s 7 ms 531 ms (GPU contention, no lock)
nothing n/a n/a 31.6 ms

transformers' own dtype window inside that from_pretrained (local_torch_dtype timed by monkeypatch): [14.1 ms] out of 1359 ms total.

Repro

import threading, time, torch
from transformers import DynamicCache
from gptqmodel import GPTQModel, BACKEND
m = GPTQModel.load("<Llama-3.2-1B GPTQ 4bit>", backend=BACKEND.AUTO, dtype=torch.float16, device="cuda:0"); m.optimize()
ids = m.tokenizer("Hello there", return_tensors="pt").input_ids.cuda()
@torch.inference_mode()
def loop(rec):
    c = DynamicCache(); o = m.model(input_ids=ids, past_key_values=c, use_cache=True); nxt = o.logits[:, -1:].argmax(-1)
    for _ in range(48):
        t = time.perf_counter(); o = m.model(input_ids=nxt, past_key_values=c, use_cache=True)
        nxt = o.logits[:, -1:].argmax(-1); torch.cuda.synchronize(); rec.append(time.perf_counter() - t)
loop([]); loop([])                                    # warm all decode-length graphs
stop, rec = threading.Event(), []
th = threading.Thread(target=lambda: [loop(rec) for _ in iter(lambda: stop.is_set(), True)]); th.start()
time.sleep(3); GPTQModel.load("mistralai/Mistral-7B-v0.3", device="cpu", dtype=torch.float16); time.sleep(3)
stop.set(); th.join(); print("worst step ms", max(rec) * 1e3)    # ~ the whole load time

Suggested fix

Take the writer around the dtype window itself, not around from_pretrained. For example, wrap transformers' local_torch_dtype (or PreTrainedModel.get_init_context) for the duration of the GPTQModel load call, so the writer is acquired when the window opens and released when it closes. Keep the outer from_pretrained unlocked. That would also cover #5301's site and the teacher load without re-introducing the stall.

Test that should have caught it

tests/test_compile_lock_scope_5268.py only covers from_quantized/from_config windows (a 300 ms fake hold). Add a case that runs a warm compiled call concurrently with a dense GPTQModel.load(...) whose weight loading is slowed (for example a patched safetensors read), and assert the compiled call does not wait for the weight I/O.

Severity

Medium (perf). Any process that serves an optimized model and loads a dense model (evaluation baseline, norm-tuning teacher, a second model) freezes inference for the whole load: tens of seconds for a 7B model, minutes for larger ones. This is the #5268 behaviour, which #5291 fixed only for quantized checkpoints.

🤖 Generated with Claude Code

Activity

  1. ZX-ModelCloud commented on Nov 19, 2026

    @ZX-ModelCloud
    Collaborator

    Agent claim released: agent-2803856bdf8a finished this issue.

    Pull request: #5329

    Verifying on current main whether dense GPTQModel.load still holds the global-state writer across from_pretrained; reproducing with a warm compiled decode loop

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions