Skip to content

fix(rocm): install deterministic ROCm env defaults (#4072, #4075) - #4079

Merged
Qubitium merged 1 commit into
mainfrom
fix/rocm-default-env
Nov 15, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/rocm-default-env

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4075
Part of #4072 (item 1)

TL;DR

import gptqmodel now installs two ROCm-only process defaults that both defer to a caller-provided value: PYTORCH_ALLOC_CONF without expandable_segments, and ROCBLAS_USE_HIPBLASLT=0. The first stops a VMM bug from silently corrupting Torch results after empty_cache(); the second makes GPTQ quantization reproducible across processes. CUDA behaviour is unchanged. Net: 3 code files (1 new), 2 test files (1 new), 2 docs pages (1 new), plus README.md.

1. The problem

Old flow (main @ 39b62765e):

flowchart TD
    A["import gptqmodel"] --> B["apply_default_alloc_conf"]
    B --> C{"caller set PYTORCH_ALLOC_CONF or PYTORCH_CUDA_ALLOC_CONF"}
    C -- "no" --> D["install expandable_segments:True on every build"]
    C -- "yes" --> E["preserve caller value"]
    D --> F["first BLAS call"]
    E --> F
    F --> G["rocBLAS forwards GEMMs to hipBLASLt"]
    G --> H["hipBLASLt picks a kernel per process"]

    classDef fail fill:#ffe0e0,stroke:#c62828,color:#000
    class D,H fail
Loading
Mechanism Old location Problem
Allocator default gptqmodel/_alloc_conf.py:26-30, applied unconditionally at gptqmodel/__init__.py:29 expandable_segments:True + torch.cuda.empty_cache() releases/remaps VMM segments on ROCm 10.1/gfx1201 and subsequent Torch ops read wrong data or raise hipErrorIllegalAddress
rocBLAS selection no GPT-QModel default rocBLAS forwards GEMMs to hipBLASLt, which chooses MT64x64x16 vs MT256x128x8 (or split-K) per process; the first divergence is torch.cholesky_inverse(L, out=L) at quantization/gptq.py:1828, and GPTQ rounding amplifies it into different checkpoints

2. The new flow

flowchart TD
    A["import gptqmodel"] --> B["_alloc_conf.apply_default_alloc_conf"]
    B --> C{"running_on_rocm"}
    C -- "no" --> D["CUDA default with expandable_segments"]
    C -- "yes" --> E{"caller set PYTORCH_ALLOC_CONF, PYTORCH_CUDA_ALLOC_CONF or PYTORCH_HIP_ALLOC_CONF"}
    D --> F["_rocm_env.apply_default_rocm_blas_env"]
    E -- "yes" --> G["preserve caller value"]
    E -- "no" --> H["ROCm default without expandable_segments"]
    G --> F
    H --> F
    F --> I{"running_on_rocm and caller did not set ROCBLAS_USE_HIPBLASLT"}
    I -- "yes" --> J["set ROCBLAS_USE_HIPBLASLT=0"]
    I -- "no" --> K["leave it untouched"]

    classDef fail fill:#ffe0e0,stroke:#c62828,color:#000
    class G,K fail
Loading
  • gptqmodel/_rocm_env.py (new): running_on_rocm() and apply_default_rocm_blas_env(). Presence of ROCBLAS_USE_HIPBLASLT is honoured verbatim (including 1 and "").
  • gptqmodel/_alloc_conf.py: default_alloc_conf() returns ROCM_DEFAULT_ALLOC_CONF (max_split_size_mb:256,garbage_collection_threshold:0.7) on HIP and the historical DEFAULT_ALLOC_CONF otherwise; alloc_conf_env_vars() also treats PYTORCH_HIP_ALLOC_CONF as caller configuration on ROCm.
  • gptqmodel/__init__.py: applies both before gptqmodel.utils, i.e. before the allocator or any BLAS call exists.
  • docs/rocm-default-environment.md (new) keeps the diagrams and contracts; docs/rocm-rdna4.md no longer claims rocBLAS is selected when it actually forwarded to hipBLASLt. Both are linked from README.md.

3. Failure / ambiguity contract

Situation Behaviour
Caller sets any allocator variable (including PYTORCH_HIP_ALLOC_CONF) nothing written; caller value used
Caller sets ROCBLAS_USE_HIPBLASLT to 1, 0 or "" nothing written; caller value used
Non-ROCm host CUDA default installed; ROCBLAS_USE_HIPBLASLT never written
BLAS call before import gptqmodel rocBLAS has already fixed its choice; cannot be corrected (documented)

4. Before / after

Hardware: AMD Radeon AI PRO R9700 (gfx1201, HIP 7.16.26385, torch 2.16.0.dev+rocm10.1).

Check Before After
Issue #4072 pure-Torch repro: 2097152x16 matmul, empty_cache() between 3 reps 294912 / 0 / 196608 bad rows in 3 runs on main 0 bad rows (repeated runs)
Issue #4075: Qwen2.5-0.5B-Instruct GPTQ 4-bit g128, 32 samples, two separate processes, 795 materialized tensors 334/795 tensors differ (main worktree, expandable_segments:False to isolate #4075) 0/795 differ
Same quantize with explicit ROCBLAS_USE_HIPBLASLT=1 on this branch 334/795 differ (explicit override, expected)
import gptqmodel on CUDA expandable_segments:True,..., no BLAS var identical

The allocator repro is intermittent by nature; the deterministic regression guard is that the installed default contains no expandable_segments and that the child process observes it.

5. Tests

New tests/test_rocm_default_env.py:

  • allocator: CUDA default keeps expandable_segments; ROCm default drops it; caller values (all three names) preserved;
  • rocBLAS: default installed, explicit 1/0/"" preserved, non-ROCm untouched;
  • real ROCm subprocess: import gptqmodel installs both defaults, the issue's matmul repro returns 0 bad rows;
  • cross-process: quantizes twice in separate interpreters (local HF snapshot, HF_HUB_OFFLINE=1), asserts each child observed ROCBLAS_USE_HIPBLASLT=0 and all 795 tensor hashes match.

Commands and results:

# ROCm (HIP_VISIBLE_DEVICES=0)
pytest -q tests/test_rocm_default_env.py tests/test_alloc_conf.py \
         tests/test_rocm_quant_blas.py tests/test_rocm_quant_tuning.py
-> 54 passed, 1 skipped in 73.27s

# CUDA (CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae)
pytest -q tests/test_alloc_conf.py tests/test_rocm_default_env.py \
         tests/test_rocm_quant_blas.py tests/test_rocm_quant_tuning.py
-> 52 passed, 3 skipped in 13.52s

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

Branch was merged with origin/main (39b62765e) before the final run; it was already up to date.

6. Reviewer notes / limitations

  • The ROCm default applies to every HIP build (as the issue requested), although the corruption was measured on gfx1201. Revisit if a ROCm release fixes the VMM path.
  • A BLAS call performed before import gptqmodel cannot be corrected; this is the same constraint the allocator default already has and is documented.
  • No GPTQMODEL_* switch is added; these are platform variables with caller precedence, like the existing allocator default.

…producible

ROCm 10.1/gfx1201 corrupts ordinary Torch results when
expandable_segments:True is combined with torch.cuda.empty_cache(), which
GPT-QModel calls throughout quantization. Install a ROCm allocator default
without expandable_segments and keep the CUDA default unchanged.

rocBLAS also forwards its GEMMs to hipBLASLt, which selects a different
kernel per process for the same shape; identical GPTQ runs then save
different checkpoints (334/795 tensors on Qwen2.5-0.5B). Export
ROCBLAS_USE_HIPBLASLT=0 at import so rocBLAS stays deterministic.

Both defaults defer to any caller-provided value, including the HIP-only
PYTORCH_HIP_ALLOC_CONF alias.

Fixes #4075
Part of #4072 (item 1)
@Qubitium
Qubitium merged commit 99f3b4b into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/rocm-default-env branch November 15, 2026 00:50
Qubitium added a commit that referenced this pull request Nov 15, 2026
) (#4105)

* fix(rocm): stage HIP sources, make AWQ split-K deterministic, scope the 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.

* docs(rocm): record the post-#4094-merge measurements (#4092)

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.
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