Skip to content

[BUG][ROCm] HIP JIT extension cache key ignores the gfx target (PYTORCH_ROCM_ARCH / gcnArchName): a binary built for another arch is silently reused -> 'invalid device function' in every later process #4815

Closed

Description

@Qubitium

Found on main @ dba142d, Radeon AI PRO R9700 (gfx1201), ROCm 10.1, torch 2.16.0.dev20261025+rocm10.1.

What is wrong

TorchOpsJitExtension._cache_fingerprint() (gptqmodel/utils/cpp.py:1704) decides which cached JIT binary a process may load. Its device part, _cuda_cache_fingerprint_payload() (gptqmodel/utils/cpp.py:1741-1775), only covers CUDA inputs:

  • TORCH_CUDA_ARCH_LIST, which hipcc ignores;
  • torch.cuda.get_device_capability(), which on ROCm is a coarse (major, minor) pair. gfx1201 and gfx1200 both report (12, 0), and gfx90a and gfx908 both report (9, 0);
  • resolved_cuda_arch_flags(), which is [] on ROCm.

On this card the whole device payload is ['cuda_ext=1', 'visible_caps=12.0'].

PYTORCH_ROCM_ARCH and the device's gcnArchName are what actually choose the code objects hipcc emits, and neither is in the key. A HIP binary built for one gfx target is therefore reused by a later process on a different target. Every kernel launch in that process then fails with invalid device function, until someone deletes the cache by hand.

This affects every HIP JIT extension built through TorchOpsJitExtension: nf12_hip, exllamav3, hadamard, paro, gptq_block, allspark_hip and tt_blockfloat_hip. On CUDA the equivalent knob (TORCH_CUDA_ARCH_LIST) is part of the key, so this gap is ROCm-only.

How a user gets here

  • The docs (docs/rocm-rdna4.md, "Family-wide HIP builds compile") tell users to set PYTORCH_ROCM_ARCH. A build made with a list that does not contain the running device's target poisons the cache for every later default process.
  • A cache shared between machines, for example the default ~/.cache/gptqmodel/torch_extensions on an NFS home: a gfx1200 (RX 9060) box and a gfx1201 (R9700) box, or an MI210 (gfx90a) node and an MI100 (gfx908) node, get the same key.
  • A mixed host whose GPUs report the same capability pair.

Repro (one gfx1201 card, fresh cache dir)

# archkey.py <ext_dir>
import os, sys, torch
os.environ["GPTQMODEL_TORCH_EXTENSIONS_DIR"] = sys.argv[1]
from gptqmodel.utils import nf12_hip
print(os.environ.get("PYTORCH_ROCM_ARCH"), torch.cuda.get_device_properties(0).gcnArchName,
      nf12_hip._EXTENSION.build_root().name)
try:
    nf12_hip.prewarm_nf12_hip_extension()
    w = torch.randn(256, 256, dtype=torch.bfloat16, device="cuda")
    print("roundtrip ok:", torch.equal(nf12_hip.decode(*nf12_hip.encode(w)), w))
except Exception as e:
    print(type(e).__name__, e)
$ PYTORCH_ROCM_ARCH=gfx1100 python archkey.py /tmp/ext   # e.g. built on / for another GPU
gfx1100 gfx1201 7d7806fbcd53c8e9
RuntimeError nf12_hip.selftest failed: invalid device function
$ python archkey.py /tmp/ext                             # plain process on the gfx1201 card
None gfx1201 7d7806fbcd53c8e9                            # <- same key, reuses the gfx1100-only binary
RuntimeError nf12_hip.selftest failed: invalid device function
$ python archkey.py /tmp/ext_fresh                       # same process, empty cache
None gfx1201 58fde202a3f33893
roundtrip ok: True

The second process never rebuilds: the key matches, so _try_load_prebuilt_library loads the wrong-target .so. NF12 is used automatically during bf16 quantization on RDNA4 (auto_dtype_cast=AUTO_NF12), and EXL3 and Paro inference use their HIP extensions, so those paths then fail on that machine with an error that does not mention the cache.

Suggested fix

In _cuda_cache_fingerprint_payload(), when torch.version.hip is set, add:

  • PYTORCH_ROCM_ARCH, normalized;
  • otherwise the sorted gcnArchName of the visible devices, stripped of the :sramecc+/:xnack- suffixes (utils/arch.normalize_arch). That is the list torch's _get_rocm_arch_flags compiles for when the variable is unset;
  • torch.version.hip.

Drop get_device_capability() on ROCm, as the arch docs already require ("never used to classify AMD hardware").

Test that should have caught it

tests/test_hip_jit_source_staging.py and the cpp.py fingerprint unit tests cover sources and CUDA arch lists, but not ROCm targets. Add a CPU-only unit test that monkeypatches torch.version.hip, PYTORCH_ROCM_ARCH and gcnArchName, and asserts the fingerprint changes.

Severity

Medium. It needs a wrong-target binary in the cache first. Once that happens, every later process silently picks it up and the HIP kernels fail with invalid device function until the cache is cleared by hand.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4826

    Fixing: add ROCm arch to the JIT cache fingerprint, unit-test the key, keep CUDA payload unchanged.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Claiming. Will add the ROCm target (PYTORCH_ROCM_ARCH, else the visible devices' normalized gcnArchName) plus torch.version.hip to the HIP JIT cache fingerprint, drop the coarse capability pair on ROCm, and add a CPU-only unit test that monkeypatches torch.version.hip/PYTORCH_ROCM_ARCH/gcnArchName.

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4815-hipcache is working on this issue now.

    Started 2026-11-17T16:08:32Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    claiming: put the ROCm gfx target in the JIT cache fingerprint, add the CPU-only regression test

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-4815-hipcache is releasing this claim — already fixed on main by #4826 (6dffaf230). I re-derived the key and verified it on the actual hardware rather than duplicating the change.

    Real-hardware verification (Radeon AI PRO R9700, gfx1201, ROCm 10.1, torch 2.16.0.dev+rocm10.1)

    nf12_hip._EXTENSION._cuda_cache_fingerprint_payload() and build_root(), one process per env:

    PYTORCH_ROCM_ARCH payload cache dir
    unset ['cuda_ext=1', 'hip=7.16.26385', 'rocm_arch_list=gfx1201'] 4dca2849eaa60c8c
    gfx1100 ['cuda_ext=1', 'hip=7.16.26385', 'rocm_arch_list=gfx1100'] c15c7abddbeea6dd
    gfx1201 ['cuda_ext=1', 'hip=7.16.26385', 'rocm_arch_list=gfx1201'] 4dca2849eaa60c8c

    All three behaviours the issue asked for hold on the card:

    • the visible-device fallback resolves gcnArchName to gfx1201 when the variable is unset (the rocm_arch_list=none case is gone);
    • a wrong-target build gets a different key, so the gfx1100 binary can no longer be reused on this card;
    • an explicit gfx1201 correctly shares the default key, so the fix does not needlessly fork the cache.

    Also confirmed the coarse capability pair is no longer an input: visible_caps= is absent from the ROCm payload entirely, and hip= pins the toolchain.

    I drafted the same change independently before noticing #4826 and dropped it; the merged helper (_hip_cache_fingerprint_payload / _normalize_rocm_arch_list / _visible_rocm_arch_list) splits PYTORCH_ROCM_ARCH on ; and spaces and preserves :xnack/:sramecc suffixes, which matches torch's own _get_rocm_arch_flags, so the comma-separated spelling is intentionally not accepted.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions