Skip to content

[BUG] Concurrency: concurrent GPTQModel.load() of dense models races transformers' global default dtype / meta-init (mixed fp16/fp32, meta lm_head, untied weights, default dtype stuck fp16) -> concurrent quantize() crashes or differs #4129

Closed

Description

@Qubitium

Audit of main @ ffa0502 on an R9700 (ROCm 10.1, CPython 3.14t, PYTHON_GIL=0). The race also reproduces with PYTHON_GIL=1 and is not ROCm-specific. Follow-up to #4094/#4102.

Concurrent GPTQModel.load() of dense models corrupts the loaded models: mixed dtypes, meta lm_head, broken ties, and the process default dtype left at fp16. This crashes or silently changes concurrent quantize() runs (medium-high)

docs/concurrency.md and #4102 present overlapping quantize() as supported. But models/loader.py:1218-1220 (_hf_loader_from_pretrained_with_dynamic_module_retry → loader.from_pretrained, reached from ModelLoader.from_pretrained at :1639) calls transformers from_pretrained without any lock. That call mutates process-wide state: it saves, sets and restores torch.set_default_dtype (modeling_utils.py:235-240), and it enters the meta-device / no-init contexts. Two dense loads in different threads (the normal way into quantize()), or a dense load next to a quantized-checkpoint load, interleave those changes. #4094 item 3 fixed only one symptom of this (the dynamo guard).

two threads, GPTQModel.load(Qwen2.5-0.5B-Instruct | Qwen3-0.6B, GPTQConfig(4, 128, offload_to_disk=False), dtype=float16):
  ROUND 0 BAD {Qwen2.5: {fp16: 252, fp32: 38}, meta ['lm_head.weight'], UNTIED; Qwen3: UNTIED}  default_dtype_after torch.float16
  ...  6/6 rounds bad (another run on an idler host: 0/3, so it is timing-dependent)
  same loads serially (SERIAL=1): 0 bad, all fp16, tied, default dtype stays float32
  plain AutoModelForCausalLM.from_pretrained in two threads: 5/6 bad -> the root is upstream, but gptqmodel adds no serialization

Downstream effects:

  • Each thread runs load(dense) then quantize() (AWQ Qwen2.5 + GPTQ-asym Qwen3, staggered 10/40 s): 7/14 runs fail with one of:

    • F.linear: expected mat1 and mat2 to have the same dtype, float != c10::Half
    • Cannot copy out of meta tensor in save() (utils/tensor.py:29)
    • a checkpoint with an extra lm_head.weight (899 tensors instead of 898)

    With the loads serialized and quantize still concurrent: 3/3 clean, matching the serial run (0/626 and 0/898 tensors differ).

  • A dense fp16 load while another thread loops GPTQModel.load(<gptq-4bit ckpt>): in 2/5 rounds, 243-267 tensors silently come out bf16.

  • Quantize Qwen2.5 GPTQ4 while other threads load+generate: 330/794 tensors differ from serial in 4/7 runs. With the dense load done before the threads start: 0/794 in 7/7.

Fix:

  • Take a process-wide lock around the dense from_pretrained/from_config section in ModelLoader, and the quantized shell build.
  • As a safety net, assert after load that every floating parameter has the requested dtype, no tied weight is meta, and torch.get_default_dtype() is unchanged.
  • Document that loads are serialized.

tests/test_concurrency_guards.py's overlapping-quantize check loads its models serially, so it can't catch this.

Plausible, not verified

  • models/base.py:2569 writes os.environ["AWQ_BATCH_SIZE"] during quantize(). gptqmodel_post_init (utils/model.py:1754) reads it to size the AwqExllamaV2 scratch, so concurrent runs can size scratch from another run.
  • utils/hf.py:304 suspend_hf_weight_init patches torch.nn.init.* and modeling_utils._init_weights without counting, so overlapping entries could leave init disabled. It is only reached from get_model_with_quantize.
  • ParoQuantTritonLinear picks fused vs dense per module by wall-clock timing inside the forward, so under contention the plan, and the output bits, depend on other threads (1/10 outputs differed from serial).

Checked, clean (ROCm, GIL off)

  • fix(concurrency): refcount global guards and serialize Triton JIT (#4094) #4102 Triton JIT lock: 16-thread cold-cache stress over AWQ/GGUF/Paro/MACaQ Triton passed 3/3, bit-identical to serial. Without the patch, 1/2 runs aborted with nanobind inst_new_int(): unexpected collision!.
  • BLAS/TF32 refcount: 0 violations under a 16-thread enter/exit/nesting/exception race.
  • Concurrent quantize once loads are serialized: 0 tensors differ, and every Hessian ran on rocBLAS.
  • Other global state: no os.chdir; the JIT loader is guarded by locks plus flock.

🤖 Generated with Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    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