Repository navigation
fix(rocm): scope the deterministic BLAS pin and stage HIP sources (#4092) - #4105
Merged
Merged
Conversation
…he BLAS pin (#4092) ROCm follow-ups to #4072/#4075 after #4079/#4082: - HIP JIT builds stage sources by default for every extension, including include roots inside the checkout, and refuse to hipify a tracked source in place. A CUDA-only extension can no longer overwrite the hand-written tt_blockfloat_hip sources; tt_blockfloat_cuda.prewarm()/_ops() are gated by supported(). - The AWQ Triton generic fused plan uses split_k_iters=1 on RDNA4 and whenever deterministic algorithms are enabled, removing the tl.atomic_add split-K reduction that made repeats differ. The exact-shape RDNA4 rules were already split-K=1. - The deterministic rocBLAS override is scoped to run plans that build a Hessian (ProcessorStep.GPTQ). AWQ/RTN/QQQ keep hipBLASLt, which is 8-14x faster for their fp16/bf16 GEMMs. The GPTQ family keeps the override for its calibration forward and replay because those activations feed the Hessian; pinning only the X^T X GEMM regressed Qwen2.5-0.5B to 332/795 differing tensors. The switch is reference counted for concurrent workers. - The hipBLASLt opt-in logs a warning once and the docs record that it gives up cross-process reproducibility. Cross-process determinism after the change: GPTQ 0/483 and AWQ 0/371 on Llama-3.2-1B (128 samples), and the Qwen2.5-0.5B test at 0/795.
Reconciles the BLAS guard with #4094 (squashed as dd67ee7), which merged first and already carries a reference-counted guard in utils/rocm.py. Resolution: main's primitive wins. Deleted this branch's parallel _BLAS_SCOPE_LOCK / _BLAS_SCOPE_DEPTH / _BLAS_SCOPE_RESTORE and kept _QUANT_BLAS_GUARD_LOCK (threading.RLock) / _QUANT_BLAS_GUARD_DEPTH / _QUANT_BLAS_GUARD_PREVIOUS verbatim. quant_blas_backend()'s body is now byte-identical to main's; only its docstring changed to describe the scoping. The #4092 item-3 substance is unchanged: the override is still entered only for run plans that build a Hessian (ProcessorStep.GPTQ via _needs_deterministic_hessian in models/base.py), so AWQ/RTN/QQQ keep hipBLASLt. The nested per-call contexts in quantization/gptq.py are now free because the guard is reentrant. Re-measured on the merged tree (gfx1201, HIP_VISIBLE_DEVICES=0): - cross-process determinism: GPTQ 0/483, AWQ 0/371 (Llama-3.2-1B, 128 samples) - F.linear TFLOPS rocBLAS -> hipBLASLt: 9.5->139.0 (bf16 4096x4096x14336), 10.2->119.8 (fp16 512x2048x8192), 0.5->7.3 (bf16 16x896x4864), 20.5 vs 16.3 (fp32 8192x4096 X^T X, rocBLAS wins) - pytest (ROCm venv): 91 passed across test_rocm_quant_blas, test_rocm_quant_tuning, test_rocm_default_env, test_concurrency_guards, test_awq_triton_scheduler, test_hip_jit_source_staging - ruff check gptqmodel docs/eora tests setup.py: All checks passed
Re-measured on the reconciled tree (origin/main d7a41a1, gfx1201): - F.linear TFLOPS rocBLAS -> hipBLASLt: 9.5 -> 139.0 (bf16 4096x4096x14336), 10.2 -> 119.8 (fp16 512x2048x8192), 0.5 -> 7.3 (bf16 16x896x4864), 20.5 vs 16.3 (fp32 8192x4096 X^T X; rocBLAS wins). - Cross-process determinism: GPTQ 0/483, AWQ 0/371 (Llama-3.2-1B, 128 samples). End-to-end 58.1/61.4 s (GPTQ) and 50.7/45.5 s (AWQ). - Opt-in evidence is now a 4-run sample for GPTQMODEL_ROCM_QUANT_BLAS=hipblaslt: 222/483 differing between the two kernel-selection groups, 0 within a group; ROCBLAS_USE_HIPBLASLT=1 stays at 224/483.
This was referenced Nov 15, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 15, 2026
…4123) (#4141) * fix(rocm): pin every Hessian builder and serialize mixed BLAS scopes (#4123) #4105 scoped the deterministic rocBLAS pin to `ProcessorStep.GPTQ`, which dropped QQQ (it builds an X^T X Hessian in `add_batch`) and the other Hessian builders. QQQ then accumulated on hipBLASLt and four runs produced three distinct checkpoints differing in 490/627 tensors. - base: gate on a `_HESSIAN_PROCESSOR_STEPS` set (GPTQ, QQQ, VPTQ, MACaQ, EXL3, FP8_GSQ) instead of `ProcessorStep.GPTQ`, and always enter the guard: Hessian builders pin rocBLAS, AWQ/ParoQuant/RTN pin hipBLASLt. - rocm: carry a `_QUANT_BLAS_GUARD_TARGET` on the refcounted guard and make a mixed-backend run wait for the active one instead of flipping the process-global backend mid-run (AWQ vs serial was 319/627 differing). - rocm: warn for the opt-out spellings (""/0/off/default) too, not only the two opt-ins. - cpp: refresh the staged in-checkout include root every call, because the cache fingerprint hashes the staged copy, so a stale stage hid header edits. - docs: correct "AWQ/RTN/QQQ have no Hessian", add the per-method determinism/concurrency tables, and scope the split-K=1 speed claim. * test(rocm): hash 0-dim checkpoint tensors and record merged-tree counts (#4123) Current main adds a 0-dim `qzeros_format` scalar to the checkpoint (#4138), which the cross-process child script could not `view(torch.uint8)`. Flatten before the view so the scalar is hashed too, and record the merged-tree confirmation (GPTQ 0/963, QQQ 0/627 over four runs each). * docs(rocm): correct the guard primitive line range (#4123) --------- Co-authored-by: GPTQModel Bot <[email protected]>
This was referenced Nov 16, 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 #4092
TL;DR
Four ROCm follow-ups in one change: HIP JIT builds stage their sources for every
extension (never hipifying a tracked source in place), the AWQ Triton generic
plan drops the nondeterministic split-K reduction on RDNA4 and under
deterministic algorithms, the deterministic rocBLAS override is scoped to
Hessian-based methods (AWQ/RTN/QQQ keep hipBLASLt, and AWQ is ~2.4x faster end
to end), and the hipBLASLt opt-in now warns that it gives up cross-process
reproducibility. Cross-process determinism stays at 0 differing tensors.
Reconciled with #4094 (squashed as
dd67ee7c1, merged first). That PRalready carries a reference-counted BLAS guard, so this branch deleted its own
parallel
_BLAS_SCOPE_*globals and keeps main's_QUANT_BLAS_GUARD_*primitive verbatim; only where the guard is entered changed. See
Reconciliation.
Hardware: AMD Radeon AI PRO R9700 (
gfx1201, HIP 7.16.26385, torch2.16.0.dev20261025+rocm10.1),
HIP_VISIBLE_DEVICES=0. CUDA cross-check on anRTX 4090 (
GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae, torch 2.16.0.dev+cu130).The RTX 5090 was never touched.
1. The problem
flowchart TD A["TorchOpsJitExtension.load() on HIP"] --> B{"stage_hip_sources"} B -- "False: 5 loaders only" --> C["torch hipify writes next to each source"] C --> D["tt_blockfloat_hip.hip and _torch.cpp overwritten"] C --> E["Machete hipifies 102 tracked headers"] D --> F["next HIP build: tt_blockfloat_cuda.h not found"] E --> F F --> G["silent slower Torch fallback"] style D fill:#ffebee,stroke:#c62828 style E fill:#ffebee,stroke:#c62828 style F fill:#ffebee,stroke:#c62828stage_hip_sources: bool = False(gptqmodel/utils/cpp.py:1152);load()never refused a CUDA-only extension on HIP; include roots were left in the checkout.tt_blockfloat_cuda.prewarm()/_ops()had no ROCm guard (gptqmodel/utils/tt_blockfloat_cuda.py:134,164).legacy_plan(gptqmodel/quantization/awq/modules/triton/scheduler.py:61-67) returnedsplit_k_iters=8for every shape outside the 8 exact RDNA4 rules; the fp32 reduction usestl.atomic_add(gemm.py:252-255), so repeats differ.with quant_blas_backend():wrapped the whole calibrated run (gptqmodel/models/base.py:2128); rocBLAS has no fast fp16/bf16 kernels on gfx1201.GPTQMODEL_ROCM_QUANT_BLAS=hipblaslt/ROCBLAS_USE_HIPBLASLT=1opt-in was documented as faster but not as non-reproducible.2. The new flow
flowchart TD A["GPTQModel.quantize"] --> B{"_needs_deterministic_hessian: ProcessorStep.GPTQ in run_plan.processors"} B -- "yes: GPTQ family" --> C["quant_blas_backend: rocBLAS, #4094 refcount"] C --> D["calibration forward and replay: rocBLAS"] C --> E["Hessian X^T X and rocSOLVER inversion: rocBLAS"] B -- "no: AWQ / RTN / QQQ" --> F["nullcontext: torch default hipBLASLt"] F --> G["fp16/bf16 GEMMs: hipBLASLt, 8-14x faster"] H["HIP JIT load"] --> I{"stage_hip_sources"} I -- "True: default" --> J["copytree tracked sources and checkout include roots into build_root/_src"] J --> K["hipify writes only under build_root"] I -- "False with a checkout path" --> L["RuntimeError: refusing to hipify in place"] style L fill:#ffebee,stroke:#c62828 style G fill:#e8f5e9,stroke:#2e7d32Why the GPTQ family keeps the override on its forward: the calibration forward
and replay produce the activations that are accumulated into the Hessian.
Pinning only the fp32
X^T XGEMM regressed Qwen2.5-0.5B-Instruct to332/795 differing tensors across processes (the #4075 no-pin baseline);
the captured inputs themselves differed. So the override is scoped per method,
not per GEMM: Hessian methods keep it, methods without a Hessian never pay it.
The per-call contexts in
compute_hessian_xtx(gptqmodel/quantization/gptq.py:971,983)and
hessian_inverse(gptqmodel/quantization/gptq.py:1802) remain asdefense-in-depth for direct Hessian calls; because the guard is reentrant they
cost nothing when nested.
Reconciliation with #4094
#4094 merged first and added
_QUANT_BLAS_GUARD_LOCK = threading.RLock(),_QUANT_BLAS_GUARD_DEPTH,_QUANT_BLAS_GUARD_PREVIOUSingptqmodel/utils/rocm.py. This branch had added a parallel_BLAS_SCOPE_LOCK/_BLAS_SCOPE_DEPTH/_BLAS_SCOPE_RESTORE. Resolution:_BLAS_SCOPE_LOCK(this branch)_BLAS_SCOPE_DEPTH(this branch)_BLAS_SCOPE_RESTORE(this branch)_QUANT_BLAS_GUARD_LOCK(threading.RLock, main)_QUANT_BLAS_GUARD_DEPTH,_QUANT_BLAS_GUARD_PREVIOUS(main)quant_blas_backend()body_needs_deterministic_hessian+ method-scopedwith(models/base.py)_warn_if_blas_is_not_reproducible+quantize_tuninghook_REPRODUCIBILITY_WARNEDthreading.RLockis reentrant, which is strictly better here: the nestedper-call contexts in
quantization/gptq.pybecome free while the method-scopedoverride is active. Main's concurrency tests
(
tests/test_concurrency_guards.py) pass unchanged.3. Per-item detail
Item 1 - HIP source staging
stage_hip_sourcesnow defaults toTrue(gptqmodel/utils/cpp.py:1166)._stage_hip_sources(gptqmodel/utils/cpp.py:1236) stages the tracked sourcetree, stages include roots inside the checkout under
_src/_checkout, leavesalready-writable out-of-checkout sources alone, and refuses (RuntimeError) to
hipify a checkout path when a caller explicitly opts out.
tt_blockfloat_cuda.prewarm()returnsFalseand_ops()raises on HIP(
gptqmodel/utils/tt_blockfloat_cuda.py:134,170).Fail-before (
tt_blockfloat_cuda.prewarm()on ROCm):Pass-after:
The hipified CUDA kernel is now generated in the cache, next to (not over) the
tracked HIP sources. A sweep over every registered extension
(
_resolved_sources_and_include_paths()on ROCm) returned 0 sources and 0include roots inside the checkout for all of them, including Machete's 11
sources / 6 include roots.
Item 2 - AWQ Triton determinism
_generic_fused_plan(gptqmodel/quantization/awq/modules/triton/scheduler.py:97)uses
split_k_iters=1when_single_split_required(scheduler.py:73) sees anRDNA4
gcnArchNameortorch.are_deterministic_algorithms_enabled(). Theexact-shape RDNA4 rules were already split-K=1.
mode="legacy"and trainingkeep the historical split-K=8 contract, and the plan cache key now includes the
deterministic flag.
Fail-before, 20 repeats, FP16, deterministic on:
Pass-after:
One distinct result over 20 repeats, and 1.6-2.2x faster.
Item 3 - BLAS scope
Re-measured after merging
maind7a41a1dd(the #4094 merge), on gfx1201.F.linearTFLOPS, median of 50 iterations after 10 warm-ups:X^T XCross-process determinism, two separate processes, every saved tensor hashed
(128 samples, 4-bit, group_size 128):
tests/test_rocm_default_env.py)#4075's baseline was 332/794 without the pin and 0/794 with it. GPTQ's
end-to-end time is unchanged by design (it keeps the old whole-run pin; the
issue measured 61 s / 56 s). AWQ dropped from the issue's 128 s / 124 s.
Item 4 - opt-in reproducibility
_warn_if_blas_is_not_reproducible(gptqmodel/utils/rocm.py:150) logs oncewhen
GPTQMODEL_ROCM_QUANT_BLASishipblaslt/cublasltorROCBLAS_USE_HIPBLASLTis truthy. Re-measured on the reconciled tree,Llama-3.2-1B, 128 samples:
ROCBLAS_USE_HIPBLASLT=1qweightandscales, from layer 0)GPTQMODEL_ROCM_QUANT_BLAS=hipblasltThe issue measured 224/482 for both opt-ins. The 4-run sample is the useful
shape of the failure: runs 1, 2 and 4 matched and run 3 differed in 222/483
tensors, so a single matching pair does not make the opt-in reproducible — the
kernel choice depends on process history. Both docs now state this.
4. Failure and ambiguity contract
prewarm()/_ops()on HIPFalse/ actionableRuntimeError; staging also keeps the tree cleanRuntimeError: refusing to hipify ...before any writeRuntimeErrorGPTQMODEL_ROCM_QUANT_BLAS=hipblasltorROCBLAS_USE_HIPBLASLT=1quantize()calls5. Change inventory
gptqmodel/utils/cpp.pygptqmodel/utils/tt_blockfloat_cuda.pysupported()gate onprewarm()/_ops()gptqmodel/quantization/awq/modules/triton/scheduler.pygptqmodel/models/base.py_needs_deterministic_hessian, method-scoped overridegptqmodel/quantization/gptq.pygptqmodel/utils/rocm.pydocs/rocm-default-environment.md,docs/rocm-rdna4.md,README.mdtests/test_hip_jit_source_staging.py,tests/test_awq_triton_scheduler.py,tests/test_rocm_quant_blas.py,tests/test_rocm_quant_tuning.py6. Tests / guarantees
test_cuda_only_build_on_rocm_never_writes_into_the_checkouttest_opted_out_staging_refuses_to_mutate_the_checkouttest_include_roots_inside_the_checkout_are_stagedtest_generic_fused_plan_drops_split_k_on_rdna4test_deterministic_algorithms_force_a_single_splittest_gptq_hessian_accumulation_enters_the_overridecompute_hessian_xtxenters the overridetest_only_hessian_methods_enter_the_overridetest_nested_scopes_switch_once_and_restore_oncetest_hipblaslt_opt_in_warns_once_about_reproducibilitytests/test_concurrency_guards.py(main, #4094)tests/test_rocm_default_env.py::test_quantization_is_reproducible_across_processes7. Validation
The branch is merged with
origin/main(ffa0502a3) before this revision; theBLAS measurements above were taken after the earlier
d7a41a1ddmerge, and thelater merge touched no BLAS or quantization code (AWQ replay and module-tree
changes only).
8. Reviewer notes
rocBLAS. The issue suggested pinning only the Hessian/linalg phase, but the
captured activations are themselves hipBLASLt-nondeterministic on Qwen2.5-0.5B
(332/795 differing with a Hessian-only pin). GPTQ's end-to-end time is
unchanged relative to fix(rocm): install deterministic ROCm env defaults (#4072, #4075) #4079's default; the 8-14x GEMM win lands on
AWQ/RTN/QQQ.
moves where the guard is entered, so a future change to the primitive belongs
in
gptqmodel/utils/rocm.pyand does not need to touch this scoping.gptqmodel_ext) into the build cache. The content fingerprint changes once,so cached binaries rebuild once.
cpp.pybuild_root()restage race noted in the issueis unchanged.