Repository navigation
fix(concurrency): refcount global guards and serialize Triton JIT (#4094) - #4102
Merged
Merged
Conversation
This was referenced Nov 15, 2026
This was referenced Nov 18, 2026
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.
Fixes #4094
TL;DR
Three concurrency defects, one branch, net +732/-45 lines across the guards, the Triton patch, and the compiler-failure classifier. Process-global BLAS/TF32 guards are now reference-counted, Triton JIT compilation is single-flight and globally serialized, and dynamo guard assertions fall back to eager instead of aborting a forward.
1. The problem
718256fd7quant_blas_backendsnapshots/restores the process-global BLAS backend on every entry; no count, no lockgptqmodel/utils/rocm.py:240-283, entered atgptqmodel/models/base.py:2128tf32_high_precision_guardsnapshots/restores the process-global TF32 state on every entrygptqmodel/utils/torch.py:1021, entered atgptqmodel/looper/module_looper.py:1777TritonPatchpatched onlyAutotuner;JITFunction._do_compilewas unguardedgptqmodel/utils/nogil_patcher.py:57_is_compiler_failurechecked frame origin only forRuntimeError; dynamo's guardAssertionErrorescapedgptqmodel/utils/torch.py:332Old guard flow (wrong when two quantizes overlap):
flowchart TD A["quantize A enters"] --> B["snapshot Cublaslt, set Cublas"] B --> C["quantize B enters"] C --> D["sees Cublas, skips override"] D --> E["A exits, restores Cublaslt"] E --> F["B's remaining layers run on Cublaslt"] F --> G["checkpoint differs from serial run"]Before/after measured on ROCm gfx1201: an outer quantize guard exited while a real 32-sample Qwen2.5-0.5B quantize was still running. 76 of its Hessian inverses ran after that exit.
CublasltCublasThe issue measured 177-210 of 794 on its checkpoint layout; the count differs with the model/sample shape, the defect is the same.
2. The new flow
Reference-counted BLAS/TF32 guards
flowchart TD A["guard enter"] --> B{"depth == 0"} B -- yes --> C["snapshot user setting"] C --> D["apply override"] D --> E["depth = 1"] B -- no --> F["depth += 1, keep override"] E --> G["caller runs"] F --> G G --> H["guard exit: depth -= 1"] H --> I{"depth == 0"} I -- yes --> J["restore snapshot"] I -- no --> K["another caller still inside"]_QUANT_BLAS_GUARD_LOCK/_QUANT_BLAS_GUARD_DEPTH/_QUANT_BLAS_GUARD_PREVIOUS(gptqmodel/utils/rocm.py:240-319) and_TF32_GUARD_LOCK/_TF32_GUARD_DEPTH/_TF32_GUARD_PREVIOUS(gptqmodel/utils/torch.py:313-320, guard at:1030-1062) implement this. The first entry owns the snapshot; only the last exit restores it. Concurrentquantize()calls stay concurrent; nothing is serialized.This composes with #4092's narrower Hessian/linalg placement: the count depends only on entry/exit, not on which phase wraps the context. It also works when the two callers enter from different phases.
Single-flight, globally serialized Triton JIT
flowchart TD A["JITFunction.run cache miss"] --> B["patched_do_compile"] B --> C["acquire _TRITON_COMPILE_LOCK"] C --> D{"kernel cache has key"} D -- yes --> E["return cached kernel"] D -- no --> F["libtriton compile"] F --> G["store kernel"] G --> H["release lock"] E --> Hpatch_triton_jit_compile(gptqmodel/utils/nogil_patcher.py:97-122) wrapsJITFunction._do_compile;_single_flight_triton_compile(:80-94) rechecks the per-device kernel cache under_TRITON_COMPILE_LOCK(:77). Same-key misses collapse into one compile; different keys are serialized because the unsafe nanobind state is process-global inside libtriton. The lock is taken only on a cache miss. The MACaQ Triton kernels launch through the sameJITFunction.runpath and are covered by the same wrapper.Dynamo guard assertions are compiler failures
flowchart TD A["compiled callable raises"] --> B["_is_compiler_failure"] B --> C{"control flow exception"} C -- yes --> D["propagate"] C -- no --> E{"BackendCompilerFailed or TorchRuntimeError"} E -- yes --> F["eager fallback"] E -- no --> G{"fullgraph"} G -- yes --> H["propagate"] G -- no --> I{"cause chain has TorchDynamoException"} I -- yes --> F I -- no --> J{"innermost frame in torch compiler package"} J -- yes --> F J -- no --> K["propagate user error"]_is_compiler_failure(gptqmodel/utils/torch.py:338-382) now applies the innermost-frame origin check to every non-control-flow exception, not onlyRuntimeError. A dynamo guardAssertionErrororiginates intorch/_dynamo/guards.py, so the eager path runs. User code raisingAssertionErrorkeeps its own frame at the origin and still propagates;fullgraph=Truestill fails closed.3. Failure and ambiguity contract
fullgraph=Truecapture failure4. Change inventory
gptqmodel/utils/rocm.pygptqmodel/utils/torch.pygptqmodel/utils/torch.pygptqmodel/utils/nogil_patcher.pydocs/concurrency.mdREADME.mdpytorch_gil_0_compat.mdtests/test_concurrency_guards.pytests/test_awq_triton_cold_jit.pytests/test_triton_patch_api.pytests/test_torch_compile_fallback.py5. Tests and guarantees
test_quant_blas_backend_is_reference_countedtest_quant_blas_backend_nested_entries_restore_oncetest_tf32_high_precision_guard_is_reference_countedtest_single_flight_triton_compile_compiles_one_key_oncetest_single_flight_triton_compile_serializes_distinct_keystest_triton_jit_compile_patch_is_installedJITFunction._do_compileis wrappedtest_macaq_triton_kernels_use_the_patched_jit_functionJITFunctiontest_cold_concurrent_awq_triton_jit_exits_cleanlyPYTHON_GIL=0, empty cache, 24 threads, exit 0, repeated twicetest_dynamo_guard_assertion_falls_back_to_eagerAssertionErrorfrom a dynamo frame runs eagertest_user_assertion_is_not_misread_as_a_compiler_failureFail-before evidence (old
718256fd7worktree at/tmp/main4094):tests/test_concurrency_guards.py: 3 failed.tests/test_triton_patch_api.py -k "single_flight or jit_compile_patch": 3 failed (no attribute '_single_flight_triton_compile').tests/test_awq_triton_cold_jit.py: 1 failed, child exited -6 (SIGABRT).tests/test_torch_compile_fallback.py -k "dynamo_guard": 1 failed with the guardAssertionError.6. Validation
Hardware:
gfx1201,HIP_VISIBLE_DEVICES=0only, torch2.16.0.dev20261025+rocm10.1, Python 3.14.10t,/opt/venvs/rocm101t/bin/python.GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42, torch2.16.0.dev+cu130, Python 3.14.10t,/opt/venvs/cu130t/bin/python. The RTX 5090 was never touched.Item 1 (ROCm, two quantizes overlapping; 32-sample Qwen2.5-0.5B, 76 Hessian inverses after the first guard exit):
Item 2 (ROCm,
PYTHON_GIL=0, emptyTRITON_CACHE_DIR, 24 threadsgenerate()onAWQ-Llama-3.2-1B-g128-gemm):Cold-start cost of the one-time compile, single-threaded on the same model: first
generate()2.60 s, second (warm) 0.33 s. The lock serializes only this cold compile; steady-state launches are unchanged.Item 3 (CUDA, 40 compile/race iterations per mode):
Natural 3-thread
GPTQModel.load+generatewithTorchLinearon CUDA:ok=3 failed=0after the fix.Committed test suites, after merging
origin/main:7. Reviewer notes
quant_blas_backendremains scoped where callers enter it. [BUG][ROCm] Follow-up on #4072/#4075: CUDA-only JIT loads hipify over tracked tt_blockfloat_hip sources, AWQ Triton split-K nondeterministic (AUTO on RDNA4), forced rocBLAS 8-14x slower fp16/bf16 GEMM, hipblaslt opt-in non-reproducible #4092 can move that scope to the Hessian/linalg phase; the refcount does not depend on the phase._is_compiler_failurenow classifies any exception whose innermost frame is insidetorch/_dynamo,torch/_inductor, ortorch/_functorch. A user exception keeps its own origin frame and is not retried;fullgraph=Truestill propagates capture failures.docs/concurrency.mdis committed and linked from the README Triton-patch section.pytorch_gil_0_compat.mdrecords the AWQ cold-JIT row and manual finding 10.GPTQMODEL_*behaviour switch was added. No whole-quantize()serialization. No dense weight cache was introduced.