Skip to content

[BUG] Concurrency: parallel quantize() races process-global BLAS/TF32 guards (non-reproducible ckpt), cold concurrent AWQ Triton JIT aborts under PYTHON_GIL=0, TorchLinear compile guard AssertionError during concurrent load #4094

Closed

Description

@Qubitium

Concurrency audit on main @ a15e6bf: free-threaded CPython 3.14 (PYTHON_GIL=0), ROCm 10.1, R9700 gfx1201, with the #4079 default env. Findings 1 and 3 also reproduce with PYTHON_GIL=1, so they are logic races, not free-threading races.

1. Concurrent quantize() calls overwrite each other's process-global BLAS backend and TF32 settings, so the later-finishing model switches backend mid-run (medium)

Two scoped guards save a process-global torch setting on entry and restore it on exit, with no counting or locking:

  • quant_blas_backend (utils/rocm.py:240-283, around quantize at models/base.py:2128);
  • tf32_high_precision_guard (utils/torch.py:1021, used at looper/module_looper.py:1777).

When quantize A returns, it restores hipBLASLt while quantize B is still running. B's remaining layers then run on the backend whose per-process kernel selection #4075/#4079 removed. The TF32 guard races the same way: B finishes in the wrong precision mode, and the user's TF32 setting is lost for the rest of the process.

two Qwen2.5-0.5B GPTQ 4-bit quantizations in two threads (first model 4 samples):
  BLAS seen by GPTQ.hessian_inverse: {('before','Cublas'): 140, ('after_first_quantize_returned','Cublaslt'): 52}
  -> 177/794 tensors differ from a serial run (GIL=1: 210/794); first diverging module: 15.9% of 4-bit codes differ, scales up to 30% rel
  seen in 5/6 runs (identical models) and 4/4 (NS0=4)
  with rocBLAS pinned before both threads: 0/794 (x2)
TF32 (USER_TF32=1, staggered start): B's last 69 Hessian-inverse calls ran with fp32_precision=tf32; process ends 'ieee' though the user set 'tf32'

On gfx1201 the TF32 part did not change the checkpoint, but on CUDA Ampere+ the Hessian inverse would run in TF32. PPL is unchanged here (21.462 vs 21.472), but cross-process reproducibility is lost for the later-finishing model.

Fix: keep a process-wide count of active overrides under a lock. Set the value on 0→1 and restore it on 1→0, for both guards. Alternatively, serialize quantize(). Add a two-thread enter/exit test for both guards.

2. With the GIL off, a cold concurrent first call of the AWQ Triton GEMM aborts the process (high, crash)

Several threads hit the first launch of awq_gemm_kernel (quantization/awq/modules/triton/gemm.py:413) with an empty Triton cache, so Triton compiles it concurrently. libtriton isn't free-thread safe:

PYTHON_GIL=0, empty TRITON_CACHE_DIR, 12 threads generate() on AWQ-Llama-3.2-1B-g128-gemm (AUTO = AwqGEMMTritonLinear):
  Critical nanobind error: inst_dealloc("triton._C.libtriton.ir.location"): attempted to delete an unknown instance   rc=134   (4/4 runs)
same with PYTHON_GIL=1: OK | warm cache, GIL=0: OK (2/2) | GPTQ TorchLinear alone, GIL=0: OK

utils/nogil_patcher.py:57 (TritonPatch) patches only the autotuner, not JIT compilation, even though gptqmodel recommends PYTHON_GIL=0. The MACaQ Triton kernels show the same nanobind collision under cold concurrent JIT (inst_new_int(): unexpected collision!, 16 threads). docs/macaq.md does say that path isn't qualified.

Fix: serialize Triton JIT compilation in TritonPatch with a global lock, or single-flight it per (kernel, specialization key). The other option is serializing the first launch per plan in the AWQ, GPTQ-fused and MACaQ Triton wrappers.

3. A TorchLinear forward raises AssertionError when another thread changes torch's default dtype during its first compile, e.g. a concurrent GPTQModel.load (medium-low, crash)

nn_modules/qlinear/torch.py:393 compiles dequantize_weight lazily on the first call. transformers from_pretrained toggles the process-global default dtype (modeling_utils.py:237/240). A forward in another thread that is compiling at that moment gets:

AssertionError: Guard failed on the same frame it was created ... GLOBAL_STATE changed: default_dtype
dtype_race.py: forwards ok 0, failed 5  (PYTHON_GIL=0 and PYTHON_GIL=1); seen naturally with 3 threads doing concurrent load+forward

_is_compiler_failure (utils/torch.py:332) doesn't classify this dynamo AssertionError as a compiler failure, so the eager fallback never runs. Fix: treat exceptions raised from torch/_dynamo frames, including guard assertions, as compiler failures. Alternatively, compile dequantize_weight once, under a lock, at optimize()/post-init.

Checked, no defect found (default env, GIL off)

  • Determinism: GPTQ is deterministic within a process and across processes (0/794), including with GPTQMODEL_CUDA_WORKERS 1/8 and GPTQMODEL_MAX_PACKERS 1/16. AWQ is deterministic across processes (0/626). The round-6 nondeterminism is gone.
  • Load/forward/del cycles: 12 cycles each for GPTQ (TorchLinear) and AWQ (Triton) show no HSA faults, and g_idx/qweight are intact. Host RSS is flat and device memory is freed.
  • Concurrent generate and load: generate() from 8 threads with batch sizes 1–8 has 0/24 mismatches. Concurrent load/unload with 3 threads (GPTQ+AWQ, 24 loads) has no faults.
  • Shared state: threadx DeviceThreadPool (fix(multi-gpu): release worker task refs, StopForward tracebacks, and ExLlamaV2 device (#4078) #4088) stays correct under task exceptions, concurrent wait() and shutdown mid-stream. The AWQ _PLAN_CACHE and _DYNAMIC_* caches are locked correctly, and StopForward raises use fresh instances.

Plausible, not verified

In _DeviceWorker._run (threadx.py), a fut.cancel() landing between the cancelled() check and set_result would raise InvalidStateError and run _on_task_finished twice. That could let wait() return early. Not hit in 20k attempts.

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions