Skip to content

fix(rocm): scope the deterministic BLAS pin and stage HIP sources (#4092) - #4105

Merged
Qubitium merged 5 commits into
mainfrom
fix/issue-4092-rocm
Nov 15, 2026
Merged

Qubitium merged 5 commits into
mainfrom
fix/issue-4092-rocm

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

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 PR
already 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, torch
2.16.0.dev20261025+rocm10.1), HIP_VISIBLE_DEVICES=0. CUDA cross-check on an
RTX 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:#c62828
Loading
Item Root cause (before)
1 stage_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).
2 legacy_plan (gptqmodel/quantization/awq/modules/triton/scheduler.py:61-67) returned split_k_iters=8 for every shape outside the 8 exact RDNA4 rules; the fp32 reduction uses tl.atomic_add (gemm.py:252-255), so repeats differ.
3 with quant_blas_backend(): wrapped the whole calibrated run (gptqmodel/models/base.py:2128); rocBLAS has no fast fp16/bf16 kernels on gfx1201.
4 The GPTQMODEL_ROCM_QUANT_BLAS=hipblaslt / ROCBLAS_USE_HIPBLASLT=1 opt-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:#2e7d32
Loading

Why 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 X GEMM regressed Qwen2.5-0.5B-Instruct to
332/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 as
defense-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_PREVIOUS in
gptqmodel/utils/rocm.py. This branch had added a parallel
_BLAS_SCOPE_LOCK/_BLAS_SCOPE_DEPTH/_BLAS_SCOPE_RESTORE. Resolution:

Symbol Action
_BLAS_SCOPE_LOCK (this branch) deleted
_BLAS_SCOPE_DEPTH (this branch) deleted
_BLAS_SCOPE_RESTORE (this branch) deleted
_QUANT_BLAS_GUARD_LOCK (threading.RLock, main) kept verbatim
_QUANT_BLAS_GUARD_DEPTH, _QUANT_BLAS_GUARD_PREVIOUS (main) kept verbatim
quant_blas_backend() body now byte-identical to main (docstring only differs)
_needs_deterministic_hessian + method-scoped with (models/base.py) kept — the item-3 substance
_warn_if_blas_is_not_reproducible + quantize_tuning hook kept — item 4
_REPRODUCIBILITY_WARNED unchanged

threading.RLock is reentrant, which is strictly better here: the nested
per-call contexts in quantization/gptq.py become free while the method-scoped
override is active. Main's concurrency tests
(tests/test_concurrency_guards.py) pass unchanged.

3. Per-item detail

Item 1 - HIP source staging

  • stage_hip_sources now defaults to True (gptqmodel/utils/cpp.py:1166).
  • _stage_hip_sources (gptqmodel/utils/cpp.py:1236) stages the tracked source
    tree, stages include roots inside the checkout under _src/_checkout, leaves
    already-writable out-of-checkout sources alone, and refuses (RuntimeError) to
    hipify a checkout path when a caller explicitly opts out.
  • tt_blockfloat_cuda.prewarm() returns False and _ops() raises on HIP
    (gptqmodel/utils/tt_blockfloat_cuda.py:134,170).

Fail-before (tt_blockfloat_cuda.prewarm() on ROCm):

 M gptqmodel_ext/tt_blockfloat_hip/tt_blockfloat_hip.hip
 M gptqmodel_ext/tt_blockfloat_hip/tt_blockfloat_hip_torch.cpp
 2 files changed, 1185 insertions(+), 1580 deletions(-)

Pass-after:

HIP_VISIBLE_DEVICES=0 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -c "
from gptqmodel.utils import tt_blockfloat_cuda as t
print('supported', t.supported()); print('prewarm', t.prewarm()); print('direct load', t._EXTENSION.load())"
# supported False
# prewarm False
# direct load False
git status --porcelain gptqmodel_ext | wc -l   # 0
ls ~/.cache/gptqmodel/torch_extensions/tt_blockfloat_cuda/_src
# README.md tt_blockfloat_cuda.cu tt_blockfloat_cuda.h tt_blockfloat_cuda_torch.cpp
# tt_blockfloat_hip.hip tt_blockfloat_hip_torch.cpp

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 0
include 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=1 when _single_split_required (scheduler.py:73) sees an
RDNA4 gcnArchName or torch.are_deterministic_algorithms_enabled(). The
exact-shape RDNA4 rules were already split-K=1. mode="legacy" and training
keep the historical split-K=8 contract, and the plan cache key now includes the
deterministic flag.

Fail-before, 20 repeats, FP16, deterministic on:

N=2048 K=2048 M=64 split=8 distinct=19/20 median_us=229.6
N=4096 K=4096 M=32 split=8 distinct=19/20 median_us=249.3
N=8192 K=2048 M=16 split=8 distinct=19/20 median_us=224.6

Pass-after:

N=2048 K=2048 M=64 split=1 distinct=0/20 median_us=106.6
N=4096 K=4096 M=32 split=1 distinct=0/20 median_us=152.4
N=8192 K=2048 M=16 split=1 distinct=0/20 median_us=104.7

One distinct result over 20 repeats, and 1.6-2.2x faster.

Item 3 - BLAS scope

Re-measured after merging main d7a41a1dd (the #4094 merge), on gfx1201.
F.linear TFLOPS, median of 50 iterations after 10 warm-ups:

Shape dtype rocBLAS (before) hipBLASLt (after, no-Hessian methods) speedup
4096x4096x14336 bf16 9.5 139.0 14.6x
512x2048x8192 fp16 10.2 119.8 11.7x
16x896x4864 bf16 0.5 7.3 13.4x
8192x4096 X^T X fp32 20.5 16.3 0.8x (rocBLAS wins)

Cross-process determinism, two separate processes, every saved tensor hashed
(128 samples, 4-bit, group_size 128):

Model Method Differing tensors (after) Time (s)
Llama-3.2-1B-Instruct GPTQ 0 / 483 58.1 / 61.4
Llama-3.2-1B-Instruct AWQ 0 / 371 50.7 / 45.5
Qwen2.5-0.5B-Instruct GPTQ 0 / 795 (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 once
when GPTQMODEL_ROCM_QUANT_BLAS is hipblaslt/cublaslt or
ROCBLAS_USE_HIPBLASLT is truthy. Re-measured on the reconciled tree,
Llama-3.2-1B, 128 samples:

Opt-in Runs Differing tensors
none (default) 2 0 / 483
ROCBLAS_USE_HIPBLASLT=1 2 224 / 483 (qweight and scales, from layer 0)
GPTQMODEL_ROCM_QUANT_BLAS=hipblaslt 4 222 / 483 between the two kernel-selection groups; 0 within a group

The 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

Situation Before After
CUDA-only prewarm()/_ops() on HIP hipified the CUDA kernel into the checkout False / actionable RuntimeError; staging also keeps the tree clean
Caller opts out of staging with a checkout source mutated the checkout RuntimeError: refusing to hipify ... before any write
Build root inside the source tree RuntimeError unchanged
AWQ shape outside the exact RDNA4 rules split-K=8, nondeterministic split-K=1 on RDNA4 / deterministic algorithms
AWQ shape on CDNA/other ROCm split-K=8 split-K=8 (unchanged)
AWQ/RTN/QQQ quantization forced rocBLAS hipBLASLt
GPTQ-family quantization forced rocBLAS forced rocBLAS (activations feed the Hessian)
GPTQMODEL_ROCM_QUANT_BLAS=hipblaslt or ROCBLAS_USE_HIPBLASLT=1 silent one warning: checkpoints may differ across processes
Concurrent quantize() calls first exit restored hipBLASLt mid-run (#4094) #4094's reentrant refcount; unchanged by this PR
Non-ROCm host no-op no-op

5. Change inventory

Area File Purpose
HIP staging gptqmodel/utils/cpp.py default staging, checkout include roots, opt-out refusal
HIP guard gptqmodel/utils/tt_blockfloat_cuda.py supported() gate on prewarm()/_ops()
AWQ scheduler gptqmodel/quantization/awq/modules/triton/scheduler.py split-K=1 generic plan on RDNA4/deterministic
BLAS scope gptqmodel/models/base.py _needs_deterministic_hessian, method-scoped override
BLAS scope gptqmodel/quantization/gptq.py per-call override on Hessian GEMM/inversion
BLAS scope gptqmodel/utils/rocm.py opt-in warning + scoping docstring (refcount is #4094's)
Docs docs/rocm-default-environment.md, docs/rocm-rdna4.md, README.md flows, measured numbers, opt-in contract
Tests tests/test_hip_jit_source_staging.py, tests/test_awq_triton_scheduler.py, tests/test_rocm_quant_blas.py, tests/test_rocm_quant_tuning.py regression coverage

6. Tests / guarantees

Test Invariant
test_cuda_only_build_on_rocm_never_writes_into_the_checkout a stubbed hipify writes only under the build cache; the checkout is byte-identical
test_opted_out_staging_refuses_to_mutate_the_checkout opt-out with a checkout path raises before writing
test_include_roots_inside_the_checkout_are_staged Machete-style include roots are staged
test_generic_fused_plan_drops_split_k_on_rdna4 RDNA4 generic plan is split-K=1
test_deterministic_algorithms_force_a_single_split deterministic mode is split-K=1; cache key includes the flag
test_gptq_hessian_accumulation_enters_the_override compute_hessian_xtx enters the override
test_only_hessian_methods_enter_the_override AWQ/RTN/QQQ do not
test_nested_scopes_switch_once_and_restore_once nested scopes share one switch and restore once
test_hipblaslt_opt_in_warns_once_about_reproducibility one warning per process
tests/test_concurrency_guards.py (main, #4094) the refcount primitive still restores only after the last worker
tests/test_rocm_default_env.py::test_quantization_is_reproducible_across_processes Qwen2.5-0.5B GPTQ 0/795 across two processes

7. Validation

# Lint (required gate)
ruff check gptqmodel docs/eora tests setup.py
# All checks passed!

# ROCm venv (HIP_VISIBLE_DEVICES=0), required set + main's concurrency test
HIP_VISIBLE_DEVICES=0 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest -q \
  tests/test_rocm_quant_blas.py tests/test_rocm_quant_tuning.py \
  tests/test_rocm_default_env.py tests/test_concurrency_guards.py
# 57 passed

# CPU / CUDA venv, changed + related test files
PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest -q \
  tests/test_rocm_quant_blas.py tests/test_rocm_quant_tuning.py \
  tests/test_concurrency_guards.py tests/test_awq_triton_scheduler.py \
  tests/test_hip_jit_source_staging.py tests/test_rocm_backend_gating.py \
  tests/test_extension_load_api.py
# 115 passed, 1 skipped

# CUDA cross-check (RTX 4090): override is a no-op, generic plan stays split-K=8,
# deterministic algorithms switch it to split-K=1.
CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae PYTHONPATH=$PWD \
  /opt/venvs/cu130t/bin/python /tmp/n4092/cuda_smoke.py
# hip None; override no-op on CUDA: Cublas; cuda generic plan: 8; cuda deterministic plan: 1

The branch is merged with origin/main (ffa0502a3) before this revision; the
BLAS measurements above were taken after the earlier d7a41a1dd merge, and the
later merge touched no BLAS or quantization code (AWQ replay and module-tree
changes only).

8. Reviewer notes

…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.
Comment thread gptqmodel/utils/rocm.py Resolved
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.
@Qubitium
Qubitium merged commit 5a36fee into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4092-rocm branch November 15, 2026 09:33
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant