Skip to content

[BUG][ROCm] Default expandable_segments alloc conf silently corrupts GPU results on ROCm 10.1/gfx1201; HIP JIT loaders write hipify output into source tree; ParoQuantTritonLinear strided crash (#4047 incomplete); bnb Triton drops autograd at M>=16 #4072

Closed

Description

@Qubitium

Audit of main @ ad0525c on 4× AMD Radeon AI PRO R9700 (RDNA4, gfx1201), torch 2.16.0.dev+rocm10.1, free-threaded CPython 3.14.

1. import gptqmodel enables expandable_segments on ROCm, and on this stack that silently corrupts GPU results (high)

gptqmodel/_alloc_conf.py:26-30 sets DEFAULT_ALLOC_CONF = "expandable_segments:True,max_split_size_mb:256,garbage_collection_threshold:0.7", and gptqmodel/__init__.py applies it unconditionally through apply_default_alloc_conf(), with no ROCm check. On torch+rocm10.1 / gfx1201, expandable_segments:True combined with torch.cuda.empty_cache() on large tensors makes ordinary torch ops return wrong values, or sometimes raise hipErrorIllegalAddress. GPTQModel calls torch_empty_cache about 50 times during quantize and load, so every ROCm user runs into this by default.

Pure torch, no gptqmodel: a 2097152×16 matmul, recomputed on the GPU and compared, with empty_cache between 3 reps. The bad-row count should be 0:

PYTORCH_ALLOC_CONF=expandable_segments:False                                    bad rows 0
PYTORCH_ALLOC_CONF=expandable_segments:True                                     bad rows 4,522,900
the same script with only `import gptqmodel` added (its default alloc conf)    bad rows 655,360
  ... with PYTORCH_ALLOC_CONF=expandable_segments:False exported                bad rows 0
the same pattern without empty_cache                                            clean

Without empty_cache the pattern is clean, so the trigger is VMM segment release/remap. In GPTQModel this first showed up as "wrong rows" from the tt_blockfloat_hip linear (1e5–1e6 bad rows, fp32/fp16/bf16 alike), as NaN or 10× perplexity in end-to-end ROCm quantization, and as MEMORY_FAULT in dequant kernels. All of these went away with expandable_segments:False. The root cause is in the ROCm/torch platform, but GPTQModel is what turns it on. Fix: don't add expandable_segments when torch.version.hip is set, at least for ROCm ≥ 10 / gfx12 until the platform is fixed. Add a ROCm CI check that runs this pattern under the default config.

2. Five HIP JIT loaders write hipify output into the source tree (medium)

Torch hipifies sources next to the originals, and these loaders pass checkout paths directly. .gitignore:228-229 (gptqmodel_ext/**/*_hip.*, gptqmodel_ext/**/*.hip) hides the generated files from git status.

loader files written into gptqmodel_ext/
utils/allspark_hip.py:40 _sources allspark_hip/allspark_hip.hip, allspark_torch_hip_hip.cpp
utils/hadamard.py:31 _hadamard_sources hadamard/hadamard_hip.cpp, fast_hadamard_transform_hip.hip, fast_hadamard_transform_common_hip.h
quantization/gptq_cuda.py:17 gptq_block/gptq_block.hip
exllamav3/ext.py:75 23 files (bindings_hip.cpp, hgemm.hip, quant/*.hip, comp_units/*.hip, *_hip.cuh)
utils/paroquant.py:317 paroquant/rotation.hip

A read-only site-packages install therefore can't build these extensions, and a shared install gets modified at runtime. nf12_hip.py and tt_blockfloat_hip.py already do the right thing: they copy sources into <build_root>/_src first, and the nf12_hip.py:_sources docstring describes exactly this failure. Fix: move that staging helper into TorchOpsJitExtension and use it for every HIP build.

3. ParoQuantTritonLinear crashes on strided activations: #4047 (for #4042) missed this class (medium, not HIP-specific)

#4047 added .contiguous() in ParoLinear.forward (paroquant.py:~593), but ParoQuantTritonLinear overrides forward. At nn_modules/qlinear/paroquant_triton.py:221 it passes x.reshape(-1, K) (still a strided view) to _rotate_inputs, and the fused rotation op rejects it (gptqmodel_ext/paroquant/rotation.cu:1018). Batched generate() decode feeds hidden_states[:, -1:, :], so BACKEND.PAROQUANT_TRITON fails at bs>1:

ParoLinear            strided OK (max diff vs contiguous 0.0)
ParoQuantTritonLinear strided FAIL: RuntimeError: ParoQuant rotation tensors must be contiguous.

All 132 other Triton/Torch ParoQuant cases passed (2-D, 3-D, M 1-129, fp16/bf16). Fix: x_flat = x.reshape(-1, x.shape[-1]).contiguous(). Also parametrize test_paroquant_cuda_forward_packs_a_strided_activation (tests/kernels/test_paroquant.py:535) over ParoQuantTritonLinear.

4. bitsandbytes 4-bit on ROCm silently drops autograd for M >= 16 (low-medium)

nn_modules/qlinear/bitsandbytes.py:893-911: the Triton dispatch (triton_backend.should_use(...), on by default only on ROCm) has no requires_grad check, and bitsandbytes_triton.matmul_4bit is a raw Triton launch. The other paths in the same class (CPU packed :647, int8 dense :987) and the GPTQ fused gate do fall back when x.requires_grad:

rows=15  y.requires_grad=True  grad_fn=MatMul4BitBackward |dx|=51.5
rows=16  y.requires_grad=False grad_fn=None               |dx|=None

The class is SUPPORTS_TRAINING=False, so this affects grad-through-input use (adapters, attribution). GGUFTritonKernel behaves the same way while its parent GGUFTorchLinear declares SUPPORTS_TRAINING=True (not HIP-specific). Fix: add not (torch.is_grad_enabled() and x.requires_grad) to the Triton dispatch condition, in both places.

Checked, clean on gfx1201

  • Triton/Torch kernels vs an fp64 oracle (thousands of cases; M 1–1024, K/N 896–27648, bits 2/3/4/8, every group size, sym/asym, desc_act, bias, fp16/bf16, strided and 3-D inputs, torch.compile):
    • GPTQ TorchLinear, including the ROCm GEMV/fused GEMM paths;
    • AWQ Triton GEMM/GEMV and AWQ Torch;
    • GGUF Triton (Q1_0/Q4_K/Q5_K/Q6_K);
    • bitsandbytes nf4/fp4 and 8-bit;
    • ParoQuant Triton decode/prefill;
    • QQQ Triton dynamic_quant, which is bit-exact.
  • End-to-end with AUTO: ROCm PPL matches CUDA within 0.07 (Llama-1B GPTQ 28.90 vs 28.97, AWQ 20.85 vs 20.84).
  • Native HIP:
    • NF12 encode/decode is byte- and bit-exact, including inf/nan/denormal/±0. The NF12 linear is within bf16 rounding at M 0–1000, M×N > 2³¹ and on side streams.
    • AllSpark HIP is within 4e-3.
    • tt_blockfloat quant/dequant is bit-exact.
    • Hadamard is correct for widths 2–32768.
    • GSQ HIPRTC (wave32) matches eager to about 1e-6.
    • exllamav3 and paroquant build and load.
    • 788 pytest cases passed.
  • Gates: QQQ native, AWQ native GEMM/GEMV, VPTQ, ExLlamaV2, W4AFP8, W4A-NVFP4, Marlin, Machete and TritonV2 all fail fast on gfx1201 with a clear error. The ExLlamaV2 GPTQ message suggests BACKEND.TORCH, but the member is GPTQ_TORCH.
  • Generate: AUTO, static cache and torch.compile generation are correct for both GPTQ and AWQ.

Plausible, not verified (single visible GPU)

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 14, 2026
  2. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Addendum to item 1: the obvious workaround is overridden as well. alloc_conf_configured() only looks at ALLOC_CONF_ENV_VARS = ("PYTORCH_ALLOC_CONF", "PYTORCH_CUDA_ALLOC_CONF") (gptqmodel/_alloc_conf.py:35). ROCm torch also reads PYTORCH_HIP_ALLOC_CONF. If a user sets only the HIP variable, GPTQModel treats the allocator as unconfigured and writes PYTORCH_ALLOC_CONF, which torch reads first. The user's expandable_segments / max_split_size_mb / garbage_collection_threshold are then replaced without a warning. The backend: key still survives.

    PYTORCH_HIP_ALLOC_CONF=expandable_segments:False, allocate 1 GiB + 12345 B, print segment size:
      plain torch            -> 1075838976  (non-expandable: user setting honoured)
      after import gptqmodel -> 1090519040  (20 MiB-rounded expandable segment: setting overridden)
    

    So a ROCm user who applies PYTORCH_HIP_ALLOC_CONF=expandable_segments:False to avoid item 1 gets expandable segments re-enabled. Fix: add PYTORCH_HIP_ALLOC_CONF to ALLOC_CONF_ENV_VARS, and add a unit test that sets only that variable and asserts apply_default_alloc_conf() is a no-op. Until this is fixed, the workaround is to export PYTORCH_ALLOC_CONF=expandable_segments:False.

    Also seen with the default (:True) conf on gfx1201, likely the same corruption: same-seed GPTQ quantize differs in ~335/794 tensors from run to run (serial, GIL on); HSA_STATUS_ERROR_MEMORY_FAULT in dequant_kernel and garbage g_idx (min=max=558702850) over repeated load/forward/del cycles.

  3. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    More evidence for item 1, from end-to-end ROCm runs (R9700 gfx1201; each row compares the default alloc conf with expandable_segments:False):

    run default (gptqmodel's alloc conf) expandable_segments:False
    plain HF fp16 Qwen2.5-0.5B, no quantization, only import gptqmodel 18.549 ×4, then 207.7, 19.42, NaN, NaN (weights unchanged) 6/6 18.549
    Qwen2.5 gptq4 desc_act, reload in the same process PPL NaN 21.14
    same checkpoint, fresh-process eval 4× 21.245, 1× NaN, 1× 24.58 4/4 21.245
    Qwen2.5 foem4 PPL NaN 21.37 (CUDA 21.39)
    Qwen2.5 qqq4 crash: linalg.cholesky ... leading minor of order 1 not PD (qqq.py:817) 22.11
    Qwen2.5 paro4 hipErrorIllegalAddress, then the process hangs 20.54 (CUDA 20.30)

    Plain HF without import gptqmodel was 6/6 clean. With :False, every method matched CUDA PPL (rtn/gptaq/fp8/nf12/gguf/exl3 4-bit within 0.1). ROCm↔CPU and CUDA→ROCm checkpoints also load with matching outputs.

  4. Qubitium commented on Nov 15, 2026

    @Qubitium
    CollaboratorAuthor

    Resolved by #4079 (item 1: import gptqmodel no longer enables expandable_segments on ROCm, and installs ROCBLAS_USE_HIPBLASLT=0; both defer to a caller-provided value) and #4082 (items 2-4: HIP JIT sources staged out of the source tree, ParoQuantTritonLinear contiguous activations, bitsandbytes Triton autograd gate).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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