Skip to content

Linux: fix ROCm detection on split Debian stacks - #8886

Merged
danielhanchen merged 38 commits into
unslothai:mainfrom
TaskPuppyNatani:fix/rocm-old-host-version-fallback
Sep 1, 2026
Merged

danielhanchen merged 38 commits into
unslothai:mainfrom
TaskPuppyNatani:fix/rocm-old-host-version-fallback

Conversation

@TaskPuppyNatani

@TaskPuppyNatani TaskPuppyNatani commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #8402 and #8414.

Problem

Debian 13 / LMDE can expose an older hipconfig (5.7.x) alongside a newer installed HSA runtime (6.1.x), while rocm-core, amd-smi and /opt/rocm/.info/version are all absent. In that layout the resolver still sees only ROCm 5.7 and falls back to CPU wheels on a working card.

Scope, after main moved

Since this PR was opened, main has gained the rocm-core dpkg and rpm version sources and the disagreement warning. What remains unique here is the layout those do not reach: Debian ships no rocm-core at all, so the fallback this PR adds fires exactly where nothing else votes.

  • adds installed libhsa-runtime64-1 as a Debian ROCm version source, in install.sh and mirrored in studio/install_python_stack.py
  • treats rocm-core as authoritative among dpkg sources, with the HSA runtime read only in its absence
  • makes the Python detector collect all valid readings and choose the highest
  • keeps removed-but-not-purged dpkg entries from voting
  • hands get_radeon_wheel_url the already-resolved leaf, so the Radeon branch cannot re-probe and rediscover the stale reading
  • memoises detection so the disagreement warning prints once, as install.sh does
  • preserves the existing subprocess timeouts
  • bounds every ROCm version source with _run_bounded, not just rpm

Version sources only. No GPU-model special cases, no wheel-table changes, and no assumption that every ROCm 5.x host should use a newer wheel. The per-device gfx indexing, the gfx1102/gfx1200/gfx1201 rocm6.4 wheel floor and the migrated installed-wheel repair moved to #9672, which is stacked on this PR and should land after it.

Why every source is now bounded

Highest-wins made all five sources run on every AMD host, where the old first-answer-wins
chain stopped at the first that answered. rpm was bounded when that landed; dpkg-query,
hipconfig and amd-smi were not. Driving each one against a probe that never returns,
_detect_rocm_version_tag hangs forever on the first three and returns normally on rpm.
dpkg-query is the source this PR makes decisive on Debian, so it is the one that must not
hang. The two probes inside get_radeon_wheel_url are bounded for the same reason. A
timed-out probe is just a source that declined to answer, and _run_bounded no-ops where
timeout is absent, so this adds no dependency.

Why rocm-core outranks the HSA package

They are not peers. rocm-core comes from AMD's repo and marks the ROCm release. libhsa-runtime64-1 comes from the distro archive and tracks that archive. AMD's own HSA package is hsa-rocr, under a different name and versioned 1.x, so apt-cache madison libhsa-runtime64-1 on Ubuntu lists only the Ubuntu archive.

On Ubuntu 24.04 with AMD's ROCm 7.2.1 repo, the host carries rocm-core 7.2.1 beside Ubuntu's libhsa-runtime64-1 5.7.1-2build1. Voting them as peers resolves correctly but prints [WARN] ROCm version sources disagree (rocm5.7 rocm7.2) on every install. With the precedence rule it resolves rocm7.2 silently.

Validation

Rebuilt the reported layout on Debian 13 trixie with the real packages: hipconfig 5.7.31921-0, libhsa-runtime64-1 6.1.2-3 from deb.debian.org/debian trixie/main, no rocm-core, no version file. Those are the versions trixie actually ships, and they match the 6.1.x rocminfo/HSA beside a 5.7 hipconfig described in #8402.

resolved
main rocm5.7, falls back to CPU
this branch rocm6.1, one warning naming both readings
  • tests/studio/install/: 3292 passed, 3 skipped
  • tests/sh/test_rocm_version_source_disagreement.sh: 72 passed, 0 failed
  • tests/sh/test_get_torch_index_url.sh: 64 passed, 0 failed
  • dash, bash and busybox sh: 37 simulated host layouts (Debian-split, healthy Ubuntu with
    AMD's repo, RHEL, NVIDIA and CPU-only hosts carrying stale ROCm packages, rc and
    half-configured dpkg entries, aarch64, macOS, mixed gfx1151+gfx1100, gfx1200+gfx1201, gfx906,
    and every probe hanging / empty / garbled / exiting nonzero / absent) resolve to the same
    index under all three shells
  • against main, the only resolutions that change are hosts whose sole dpkg source is
    libhsa-runtime64-1, which move from cpu to a ROCm leaf. Nothing moves between ROCm leaves,
    and nothing moves toward or away from a CUDA leaf
  • bash -n / sh -n / dash -n / busybox sh -n install.sh, ruff check, git diff --check: clean
  • Self-hosted gfx1151 (Strix Halo) / Linux / ROCm 7.2.1, base vs head: CONFIRMED. On the real ROCm host base and head resolve the same tag and the same wheel index, so nothing regresses; under a stubbed Debian-split layout base resolves rocm5.7 and head resolves rocm6.1.

Not covered. No Debian 13 box with an AMD GPU. The Debian 13 and Ubuntu 24.04 hosts above are WSL2 with real package installs and no /dev/kfd, which is sufficient here because every path this changes reads the dpkg database and the version-source probes, not the device. The affected gfx1100 machine in #8402 belongs to the original reporter and has not been re-run against this branch. Under busybox sh the bounding is weaker than under dash/bash: busybox's timeout applet signals only the direct child, so a probe that leaves a grandchild holding stdout is not actually cut off. GNU timeout signals the process group and does cut it off. Nothing changed here makes that worse, and no distro that ships dpkg-query or hipconfig uses busybox as /bin/sh.

Merge order

#9152 rewrites the "no readable ROCm version" warning this PR edits, and now also
_rocm_tag_from_hipconfig, where it adds a $ROCM_PATH/bin lookup on the line this PR wraps in
_run_bounded. Both conflicts are text-only: the warning arms still fall through to the same
echo "$_base/cpu"; return, and the bounded runner composes with the new lookup as
_run_bounded "$_rt_hipconfig" --version.

Resolved both orders in a throwaway tree and they produce a byte-identical install.sh. On it,
tests/sh/test_rocm_version_source_disagreement.sh 72 passed,
tests/sh/test_get_torch_index_url.sh 64 passed,
tests/sh/test_rocm_no_version_arch_route.sh 42 passed,
tests/sh/test_rocm_no_version_arch_route_e2e.sh 21 passed, and
tests/studio/install/ 3293 passed, 3 skipped. Either order is safe; #9152 first leaves the
smaller residual conflict.

@LeoBorcherding LeoBorcherding self-assigned this Aug 15, 2026
@TaskPuppyNatani
TaskPuppyNatani marked this pull request as ready for review August 15, 2026 01:53

@mahiatlinux mahiatlinux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed this against fd6ab9aca. The premise checks out and the fix is right for the host in the issue, but there are two things I would not merge as-is, plus a set of smaller items.

The premise is correct

I verified the Debian side against the trixie archive index rather than taking it on trust:

package trixie amd64
hipcc (ships hipconfig) 5.7.1-6+deb13u1
libhsa-runtime64-1 6.1.2-3
rocminfo 6.1.2-2
rocm-core does not exist in any Debian suite

grep '^opt/rocm' Contents-amd64 over the whole trixie archive returns zero files, so /opt/rocm/.info/version genuinely cannot exist on a pure Debian install. The split is real and libhsa-runtime64-1 is the right package to read: its version field tracks the ROCm release (bookworm 5.2.3, trixie 6.1.2, forky 6.4.3+dfsg, experimental 7.2.4+dfsg), the -1 is just the soname, and the 64-bit time_t transition never renamed it. rocminfo depends on it, so on any host that reaches the ROCm branch at all the package is guaranteed present. The alternatives are worse: rocminfo's "ROCk module version 6.10.5" line is the amdgpu driver version, which is the exact trap #8414 had to fix for amd-smi, and the HSA soname on disk is 1.13.0.

The shell parsing is solid. I ran the new _rocm_tag_from_hsa_runtime_dpkg against 6.1.2-3, 1:6.1.2-3, 6.4.3+dfsg-4, 7.2.4+dfsg-1~exp1 and every non-installed dpkg state (config-files, not-installed, half-configured, unpacked, unknown package) under both bash and POSIX sh -e. Field indices, epoch strip and the $3 == "installed" gate all behave.

1. gfx1102 and RDNA4 hosts on Debian 13 regress from CPU to broken ROCm

This is the one that worries me. I scanned the actual wheel contents:

wheel gfx arches shipped
torch-2.6.0+rocm6.1-cp312 (newest on the rocm6.1 leaf) gfx1030, gfx1100, gfx1101
torch-2.9.1+rocm6.4-cp312 gfx1030, gfx1100, gfx1101, gfx1102, gfx1200, gfx1201
How I checked
$ curl -sL -r $((SZ-15000000))-$((SZ-1)) \
    ".../rocm6.1/torch-2.6.0%2Brocm6.1-cp312-cp312-manylinux_2_28_x86_64.whl" -o t61.bin
$ strings -a t61.bin | grep -o 'gfx1[0-9][0-9][0-9]' | sort | uniq -c | sort -rn
  96 gfx1101
  96 gfx1100
  90 gfx1030

$ curl -sL -r $((SZ-15000000))-$((SZ-1)) \
    ".../rocm6.4/torch-2.9.1%2Brocm6.4-cp312-cp312-manylinux_2_28_x86_64.whl" -o t64.bin
$ strings -a t64.bin | grep -o 'gfx1[0-9][0-9][0-9]' | sort | uniq -c | sort -rn
 355 gfx1201
 341 gfx1200
 207 gfx1101
 191 gfx1100
  96 gfx1102
  90 gfx1030

The gfx1100 RX 7900 XTX in the issue is genuinely fixed by this PR. But a Debian 13 host with a gfx1102 card (RX 7600, RX 7700S, PRO W7600, mapped at install.sh:4740) now resolves rocm6.1 and installs a torch that has no kernels for its arch. It fails at the first BLAS call, the same failure class already special-cased for gfx906 at install.sh:4647. Before this PR those hosts got CPU torch plus an explicit warning naming the UNSLOTH_TORCH_INDEX_FAMILY override, which is a worse install but an honest one.

The AMD per-arch reroute cannot catch them either. install.sh:4328 only fires on a */cpu index, which is no longer true once detection succeeds, and the arch list at install.sh:4355 is gfx1201|gfx1200|gfx1151|gfx1150|gfx1152 with no gfx1102.

Since ROCm wheels vendor their own userspace (I confirmed torch/lib/libhsa-runtime64.so, libamdhip64.so and librocblas.so are inside the wheel, so the host HSA version is only a proxy for the KFD vintage), flooring the resolved leaf up to one that actually ships the arch's kernels is defensible and fits the boundedness argument already written at install.sh:3565-3575. A gfx1102/gfx120x guard mirroring _gfx906_needs_legacy_index would also do.

2. Both tests/sh suites now assemble a resolver that is missing the new source

tests/sh/test_get_torch_index_url.sh:58-66 and tests/sh/test_rocm_version_source_disagreement.sh:56-64 extract the install.sh helpers by name with sed, and both carry a comment saying the list must stay in sync. _rocm_tag_from_hsa_runtime_dpkg was added to neither, nor to the guard loop at test_rocm_version_source_disagreement.sh:78-80 (that loop catches renames, not additions).

I reproduced it with each harness's own extraction list, a dpkg-query mock answering libhsa-runtime64-1 at 6.1.2-2, and hipconfig at 5.7.31921-0:

harness list (helper missing)  -> _detect_rocm_version_tag prints rocm5.7
same list plus the helper      -> rocm6.1

So the two suites that exist for #8402 currently reproduce the bug this PR fixes and pass anyway. The 127 from the undefined function is swallowed by the 2>/dev/null at install.sh:3584, and because the call is not the last command in the brace group the group's exit status is unaffected, so CI stays green. Under a POSIX sh it is worse: errexit does propagate into $(...) there, the group aborts, || _rt_readings="" wipes every reading, and detection returns empty.

There is a third hardcoded list at tests/studio/install/test_rocm_support.py:2872-2883 that was also missed. The PR updated the one at :2252-2259 but not this one, and that block's own comment describes exactly this failure mode.

3. get_radeon_wheel_url still has its own first-answer-wins chain

install.sh:4015-4041 probes amd-smi, then /opt/rocm/.info/version, then hipconfig, with no dpkg and no rpm, and takes the first answer. On the host in the issue it reads 5.7.31921 and builds https://repo.radeon.com/rocm/manylinux/rocm-rel-5.7.31921/, then retries rocm-rel-5.7/, while get_torch_index_url has just correctly resolved rocm6.1.

This branch is newly reachable because of this PR: _amd_gpu_radeon only arms when the index matches */rocm* (install.sh:4461-4468), and before the fix that host resolved */cpu. It happens to self-correct today only because rocm-rel-5.7/ has no cp312 wheels and no torchaudio, so _radeon_versions_match stays false and it falls back to the rocm6.1 index with a warning that misattributes the cause. That is a thin margin, and it is the same bug class #8414 removed everywhere else. Feeding it the already-resolved tag, or at least flooring its reading at that tag, would close it.

Test gaps worth closing in this PR

These are all mutations I ran that the suite does not catch:

  • The stderr disagreement warning has no Python coverage at all. Changing if len(distinct) > 1: (studio/install_python_stack.py:788) to if False and ..., or to >= 1 so it warns even when every source agrees, both survive all 496 tests. The shell side pins this at test_rocm_version_source_disagreement.sh:309.
  • No test covers rocm-core and libhsa-runtime64-1 both installed at different versions, which is the normal state on a host using AMD's own apt repo. Swapping the two tuple entries at :721-722 survives. _write_dpkg_query_stub holds a single package/version/status triple, so it cannot express that host without a change.
  • test_debian_split_runtime_uses_installed_hsa_runtime (tests/studio/install/test_rocm_support.py:608-640) patches subprocess.run wholesale and hand-builds the output regardless of the requested --showformat. Changing production to -f=${Version}\n, which silently kills the dpkg source on every real host, still passes. The install.sh sibling at :2367 uses the real stub and does catch it, so the stronger pattern is already in the file. Also the raise AssertionError(f"unexpected probe: {cmd}") at :632 never fires: production wraps the probe in except Exception, which swallows it.
  • Dropping $3 == "installed" from install.sh:3526 survives the whole suite. There is no HSA counterpart to test_removed_dpkg_rocm_core_cannot_pick_the_wheels, whose docstring explains why the paired config-files/installed shape matters.
  • The HSA-specific epoch strip at install.sh:3529 is untested: the new test uses 6.1.2-2, which has no epoch. Using 1:6.1.2-2 would cover it.

Smaller items

  • install.sh:3706 and install.sh:3755 still enumerate "amd-smi, /opt/rocm/.info/version, hipconfig or rocm-core". The second one now tells Debian users to install rocm-core, which is the package this PR exists because Debian does not ship.
  • _ensure_rocm_torch runs twice per install (studio/install_python_stack.py:4578 and :4792), and the detector no longer short-circuits, so all five probes run twice and the disagreement warning prints twice. install.sh emits it once. Memoising the result fixes both.
  • The warning at :788-795 iterates readings rather than distinct, so sources that agree get listed twice, and the label for the file readings is the full $ROCM_PATH, which is user-controlled and can sit under $HOME. install.sh prints sort -u tags with no paths. The WARNING: prefix is also the only one in the file; the neighbouring ROCm messages use [WARN].
  • :649-659 now records .info/version and lib/rocm_version as two independent sources where install.sh has one, so the two implementations can disagree. That works against the "studio update == fresh install" claim in the comment just below at :713. A break after the first successful parse keeps parity.
  • The comment at :710-714 was spliced mid-sentence ("...so probe dpkg (Debian/Ubuntu) can report rocm-core or the packaged HSA runtime"), and "fallbacks" is no longer accurate now that these sources always run.
  • The Python dpkg parse does result.stdout.strip().split() then indexes fields[2]/fields[3], which flattens multi-line output, where install.sh's awk scans for the first line whose $3 is installed. It can only lose a reading, never fabricate one, and it is close to unreachable for this package, but per-line iteration is a one-line change and removes the divergence.
  • studio/install_llama_prebuilt.py:3320 is a third copy of this detector, still first-answer-wins with no status filter, and its comment at :3373 claiming it mirrors install.sh and _detect_rocm_version() is no longer true. Its Linux ROCm branch looks unreachable in production, so a follow-up is fine, but the comment should not keep asserting parity that does not hold.

One suggestion that collapses several of these

install.sh:3522-3531 is byte-identical to :3505-3520 apart from the package name. Rather than a second function, query both packages in one call inside the existing _rocm_tag_from_dpkg:

dpkg-query -W -f='${Status} ${Version}\n' rocm-core libhsa-runtime64-1

with the epoch strip and split moved into the same awk so it can emit zero, one or two lines. _highest_rocm_tag already consumes a multi-line stream and the sort -u at :3590 already collapses agreement, so nothing downstream changes. That is one dpkg fork instead of two, and because _rocm_tag_from_dpkg is already present in all four enumeration sites, it needs no test-harness sync edits, which removes item 2 entirely.

For reference, the 23 pytest failures in tests/studio/install/test_rocm_support.py and the failures in both tests/sh suites on my machine are pre-existing macOS artefacts. Output is byte-identical at fd6ab9aca and at this PR's head, so nothing here is a regression from the change.

@TaskPuppyNatani

Copy link
Copy Markdown
Contributor Author

I’ve already prepared a local revision addressing the review feedback, and the targeted tests are passing. I saw that Leo was asked to apply fixes, so I wanted to check before pushing anything: should I hold off and let Leo handle the changes, or would you like me to push my revision?

@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e893eba9d1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/install_python_stack.py Resolved
Comment thread install.sh Resolved
Comment thread install.sh Resolved
Three fixes on top of the split-Debian ROCm detection work.

studio/install_python_stack.py gated the kernel-less-wheel repair on
_gfx_needs_rocm64_generic_index(ver), so a gfx1200 host reading ROCm 6.4 or
newer skipped it entirely and studio update left an installed +rocm6.1 build
in place. Split the runtime-target test from the host-version test: the index
reroute stays version-gated, the repair now runs on the target alone.

install.sh resolved _runtime_gfx from HIP_VISIBLE_DEVICES and
ROCR_VISIBLE_DEVICES only. AMD documents CUDA_VISIBLE_DEVICES as a HIP
visibility variable and recommends it for cross-vendor portability, so a mixed
gfx1100 + gfx1200 host masked with CUDA_VISIBLE_DEVICES=1 selected gfx1200 at
runtime while the installer routed for gfx1100. Resolve the three variables
first-set-wins, matching _pick_visible_index.

install.sh also took UNSLOTH_ROCM_GFX_ARCH verbatim, so a value copied from a
HIP gcnArchName (gfx1200:xnack-) matched none of the bare architecture
patterns. Strip the suffix and surrounding whitespace the way _gfx906_env and
_runtime_target_needs_rocm64_generic_index already do.

Regressions cover all three, and each fails without its fix.
@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 22e94eaf3e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread install.sh Resolved
get_torch_index_url clips its leaf to a family PyTorch publishes: ROCm 6.5
becomes rocm6.4, 7.3 and newer become rocm7.2, and no leaf ever carries a
patch level. repo.radeon.com does publish rocm-rel-6.5 and rocm-rel-7.2.1,
so passing that leaf into get_radeon_wheel_url pointed a Radeon host at an
older directory, or at none when only the patch-level directory exists. The
caller's X.Y.Z to X.Y retry cannot recover it because a leaf is already X.Y.

Treat the resolved leaf as a floor instead. The host probe runs as before and
wins whenever it reads no older than the leaf, so 6.5.0 and 7.2.1 reach their
own directories. The leaf takes over when the probe is missing or older than
the resolver's answer, which is the split Debian host whose hipconfig reports
5.7, the case the argument was added for.

tests/sh/test_rocm_version_source_disagreement.sh covers both directions and
extracts the new comparison helper alongside the functions it tests.
@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0df43b9355

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread install.sh Resolved
Comment thread studio/install_python_stack.py Resolved
HIP_VISIBLE_DEVICES, ROCR_VISIBLE_DEVICES and CUDA_VISIBLE_DEVICES carry device
ordinals, but install.sh built its gfx list with a flat grep and then
de-duplicated it before indexing. On a host enumerating gfx1100, gfx1100,
gfx1200 an ordinal of 2 ran past the end of the two-entry list and silently
fell back to the first gfx1100, so the selected RDNA 4 card never reached the
rocm6.4 floor and received wheels with no kernels for it.

The de-duplication was not gratuitous: rocminfo prints a device's arch on both
its Name and ISA lines, so counting raw tokens is wrong too. _gfx_targets_per_device
splits on agent headers and takes one token per device, exactly as
_detect_amd_gfx_codes(dedup=False) does in studio/install_python_stack.py, and
falls back to first-seen order for flat output such as amd-smi's. With the list
now one entry per device, the indexing step no longer de-duplicates.

Also refreshes the gfx906 comment that justified its policy by the old
de-duplication, and extracts the new helper in the two Strix probe tests that
run the detection block standalone.
@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

1 similar comment
@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3134043f5f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread install.sh Resolved
ROCR_VISIBLE_DEVICES is an ROCr-level mask, so rocminfo returns only the
selected agents, already in the mask's order. Indexing that filtered list again
by the mask's first ordinal double-applies it: on physical gfx1100,gfx1200 with
ROCR_VISIBLE_DEVICES=1,0 rocminfo reports gfx1200,gfx1100 and ordinal 1 picked
gfx1100, leaving the runtime-selected gfx1200 on kernel-less wheels.

Track which probe answered and skip the ordinal when rocminfo answered and
ROCR_VISIBLE_DEVICES is the first-set mask, matching the _LAST_AMD_GFX_PROBE
check in _runtime_target_needs_rocm64_generic_index. HIP_VISIBLE_DEVICES and
CUDA_VISIBLE_DEVICES are applied by the HIP runtime rather than by ROCr, and
amd-smi ignores all three, so both keep ordinal indexing.

The existing ROCR row in the visible-mask test modelled an impossible host,
rocminfo listing both GPUs while ROCR_VISIBLE_DEVICES selected one, and is
replaced by cases that report what a masked rocminfo actually returns.
@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d9ff197d86

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread install.sh Resolved
Comment thread install.sh Resolved
Two gaps in the gfx1102/RDNA 4 rocm6.4 floor.

The per-device gfx parser recognised rocminfo's "Agent N" and hipinfo's
"Device #N" but not amd-smi's "GPU: N" and "GPU[N]", which install.sh already
parses at line 3104 for its GPU-presence check. Without rocminfo, a host with
gfx1100, gfx1100, gfx1200 therefore collapsed to two entries and an ordinal of 2
fell out of range, so the selected RDNA 4 card missed the floor.
_detect_amd_gfx_codes had the same omission and is fixed alongside. A header
sharing its line with the token, as "GPU[0] : gfx1100" does, now contributes
that token instead of dropping it.

The migrated-environment repair only reinstalled torch when torch.version.hip
was empty, so a legacy venv already carrying a kernel-less +rocm6.1 wheel
finished migration with it after printing the reroute warning. It now also
reinstalls when the architecture floor selected rocm6.4 and the installed wheel
reports an older family, matching _installed_rocm_wheel_is_below on the Python
side. _venv_torch_rocm_below reads the family from the venv's torch, and an
unidentifiable family counts as older, as it does in Python.
@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

staged ci failures unrelated to this pr, syncing and restaging
image

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

staged ci failures are unrelated to this pr
image

The branch was 7 commits behind, and its Repo tests (CPU) and Python 3.13 red
was staleness rather than content: the failures named playwright_tool_activity,
test_source_read_encoding and test_gpu_init_crash_message arity, none of which
this PR touches. Merging main so the checks mean something. No conflicts.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-01T03:46:18.020552Z 9ae178f New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 41e3dd8f65

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread install.sh Outdated
Comment on lines 4229 to 4233
# detect a host x.y/x.y.z once, in the historical source order
_host_ver=$({ command -v amd-smi >/dev/null 2>&1 && \
amd-smi version 2>/dev/null | awk -F'ROCm version: ' \
'NF>1{if(match($2,/[0-9]+\.[0-9]+(\.[0-9]+)?/)){print substr($2,RSTART,RLENGTH); ok=1; exit}} END{exit !ok}'; } || \
{ [ -r /opt/rocm/.info/version ] && \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the highest host version for Radeon repositories

On a Radeon host where version sources disagree and the newer reading comes after the first successful probe—for example, /opt/rocm/.info/version reports 6.4 while hipconfig reports the actual 7.3 runtime—the generic resolver correctly chooses the highest source and caps its leaf to rocm7.2, but this first-answer chain records 6.4 and then selects the leaf-derived rocm-rel-7.2 instead of rocm-rel-7.3. Fresh evidence beyond the earlier capped-leaf report is this commit's new highest-wins disagreement behavior: the Radeon probe still uses the historical first-answer order, so it should retain the same winning host reading rather than independently re-probing.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data flow is described accurately, but the consequence is inverted, so declining this one.

The resolved value is max(first-answer host reading, resolved leaf), so it can never fall below the leaf and no lower version wins over what this PR guarantees. The only gap is when the true maximum exceeds the PyTorch cap, and above the cap there is nothing to fetch:

https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2/    200
https://repo.radeon.com/rocm/manylinux/rocm-rel-7.2.4/  200
https://repo.radeon.com/rocm/manylinux/rocm-rel-7.3/    404
https://repo.radeon.com/rocm/manylinux/rocm-rel-7.13/   404

The live listing ends at rocm-rel-7.2.4. There is no 7.3 directory and no preview-stream directory at all; the technology-preview wheels live under repo.amd.com/rocm/whl/gfx/, which install.sh routes separately and which clears _amd_gpu_radeon, so such a host never reaches this branch.

Traced through the caller: returning .../rocm-rel-7.3/ makes _radeon_fetch_listing fail, the short-path sed is a no-op because 7.3 has no patch component, _radeon_listing_ok stays false, and control reaches the 'Radeon repo unavailable' fallback. The user loses AMD's validated wheel trio for the pytorch.org ROCm index. Today they get rocm-rel-7.2, AMD's newest published set, which is what a 7.3-or-later host actually wants.

Separately, the loss would need a host above the newest PyTorch ROCm family, sources disagreeing, and the highest source not being first in the amd-smi to version-file to hipconfig order. On the mainstream case where they agree, _radeon_host_ver_not_older 7.3 7.2 is true and the host reading already wins. And the split-packaging direction this PR exists for has hipconfig lower and last in the chain, so an earlier higher source already wins there.

Comment-only pass over the lines this PR adds. Deletes restatements of the
code, section narration and measured numbers that belong in the description,
and keeps the ordering rationale for rocm-core over libhsa-runtime64-1, the
set -e contract on the version-source helpers, and the reason dpkg stdout is
parsed independently of the exit status.

Shell edits are whole comment-line deletions only. Python: 87 added comment
lines to 50. Shell: 58 added comment-only lines to 28.
Second comment-only pass, now that docstrings and shell rewording are in scope.
Collapses the multi-paragraph test docstrings to their first line, reduces the
rocm-core over libhsa-runtime64-1 ordering rationale in install.sh to two lines,
and reflows the set -e header and the removed-but-not-purged note.

Python: 87 added comment lines to 29. Shell: 58 added comment-only lines to 14.
They are body of a generated stub, so they are fixture data rather than this
PR's own comments, and the shell gate treats heredoc bodies as code.
@danielhanchen

Copy link
Copy Markdown
Member

Consolidating my earlier comments into one summary and deleting the rest.

Validated on real AMD hardware (gfx1151 Strix Halo)

CONFIRMED. Debian-split layout presented to a real ROCm 7.2.1 host, base versus head:

host shape base resolves head resolves
split stack: hipconfig 5.7 beside HSA runtime 6.1, no rocm-core rocm5.7 then cpu wheels rocm6.1
every control host (healthy Ubuntu, NVIDIA with stale ROCm packages, CPU-only with stale ROCm packages) unchanged unchanged

Identical under dash and bash, which matters because install.sh is #!/bin/sh and verified bashism-free, so dash parity is a real contract rather than a nicety.

Honest limit: the probes are stubbed on PATH. No actual Debian 13 machine was involved, so this shows the fix works given the layout from #8402, not that it encounters it the way a user does.

The scope of this PR has shrunk since I first reviewed it

Main now queries rocm-core through both dpkg-query and rpm -q independently of this PR. What remains uniquely here is the libhsa-runtime64-1 fallback, which is the actual Debian 13 case: rocm-core absent, distro-archive HSA runtime present. That is still unaddressed on main (zero occurrences) and is worth having.

The ordering rationale is the part to review: rocm-core wins outright, and libhsa-runtime64-1 is consulted only in its absence, because the HSA package comes from the distro archive and can be older than the ROCm release beside it (Ubuntu 5.7.1 next to rocm-core 7.2.1).

Merge order

This conflicts with #9152 in exactly one place: the "no readable ROCm version" warning text in get_torch_index_url. Both sides fall through to the same echo "$_base/cpu"; return, so it is a text merge, not a logic reconciliation. Taking #9152 first leaves this PR a smaller rebase, since #9152 deletes the lines this one edits.

I merged both locally and the resolved tree passes bash -n, sh -n, and the shell suites 64 / 72 / 42.

The comment pass above is comment-only, verified by comment_tools check, a shell code-residue gate, and independently at AST level.

danielhanchen and others added 2 commits September 1, 2026 01:50
Highest-wins resolution made all five sources run on every AMD host, so a wedged
dpkg-query, hipconfig or amd-smi hangs the installer where the old first-answer-wins
chain stopped before reaching it. rpm was already bounded; the other three were not,
and dpkg-query is the deciding source on Debian, where no rocm-core exists and the
installed libhsa-runtime64-1 is what the resolver reads.

Also bounds the amd-smi and hipconfig probes inside get_radeon_wheel_url, which run
on the same hosts for the same reason.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 2454d486fe

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…e change

Collapses the multi-line paragraphs added by the _run_bounded and dpkg
precedence work down to the facts a reader cannot get from the code:
the unconditional return under set -e, why every probe is bounded, the
rpmdb lock citations, the "installed" status word, the load-bearing
|| true, and the rocm-core over libhsa-runtime64-1 precedence.
@danielhanchen
danielhanchen merged commit 3703a66 into unslothai:main Sep 1, 2026
1 check was pending
danielhanchen added a commit that referenced this pull request Sep 15, 2026
…NA4 to rocm6.4 (#9672)

* Fix ROCm detection on split Debian stacks

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Harden ROCm detection on split Debian stacks

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* close the gaps in the gfx1102/RDNA4 rocm6.4 floor

Three fixes on top of the split-Debian ROCm detection work.

studio/install_python_stack.py gated the kernel-less-wheel repair on
_gfx_needs_rocm64_generic_index(ver), so a gfx1200 host reading ROCm 6.4 or
newer skipped it entirely and studio update left an installed +rocm6.1 build
in place. Split the runtime-target test from the host-version test: the index
reroute stays version-gated, the repair now runs on the target alone.

install.sh resolved _runtime_gfx from HIP_VISIBLE_DEVICES and
ROCR_VISIBLE_DEVICES only. AMD documents CUDA_VISIBLE_DEVICES as a HIP
visibility variable and recommends it for cross-vendor portability, so a mixed
gfx1100 + gfx1200 host masked with CUDA_VISIBLE_DEVICES=1 selected gfx1200 at
runtime while the installer routed for gfx1100. Resolve the three variables
first-set-wins, matching _pick_visible_index.

install.sh also took UNSLOTH_ROCM_GFX_ARCH verbatim, so a value copied from a
HIP gcnArchName (gfx1200:xnack-) matched none of the bare architecture
patterns. Strip the suffix and surrounding whitespace the way _gfx906_env and
_runtime_target_needs_rocm64_generic_index already do.

Regressions cover all three, and each fails without its fix.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* keep the real host ROCm release for repo.radeon.com

get_torch_index_url clips its leaf to a family PyTorch publishes: ROCm 6.5
becomes rocm6.4, 7.3 and newer become rocm7.2, and no leaf ever carries a
patch level. repo.radeon.com does publish rocm-rel-6.5 and rocm-rel-7.2.1,
so passing that leaf into get_radeon_wheel_url pointed a Radeon host at an
older directory, or at none when only the patch-level directory exists. The
caller's X.Y.Z to X.Y retry cannot recover it because a leaf is already X.Y.

Treat the resolved leaf as a floor instead. The host probe runs as before and
wins whenever it reads no older than the leaf, so 6.5.0 and 7.2.1 reach their
own directories. The leaf takes over when the probe is missing or older than
the resolver's answer, which is the split Debian host whose hipconfig reports
5.7, the case the argument was added for.

tests/sh/test_rocm_version_source_disagreement.sh covers both directions and
extracts the new comparison helper alongside the functions it tests.

* index visible-device masks by device, not by arch

HIP_VISIBLE_DEVICES, ROCR_VISIBLE_DEVICES and CUDA_VISIBLE_DEVICES carry device
ordinals, but install.sh built its gfx list with a flat grep and then
de-duplicated it before indexing. On a host enumerating gfx1100, gfx1100,
gfx1200 an ordinal of 2 ran past the end of the two-entry list and silently
fell back to the first gfx1100, so the selected RDNA 4 card never reached the
rocm6.4 floor and received wheels with no kernels for it.

The de-duplication was not gratuitous: rocminfo prints a device's arch on both
its Name and ISA lines, so counting raw tokens is wrong too. _gfx_targets_per_device
splits on agent headers and takes one token per device, exactly as
_detect_amd_gfx_codes(dedup=False) does in studio/install_python_stack.py, and
falls back to first-seen order for flat output such as amd-smi's. With the list
now one entry per device, the indexing step no longer de-duplicates.

Also refreshes the gfx906 comment that justified its policy by the old
de-duplication, and extracts the new helper in the two Strix probe tests that
run the detection block standalone.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* do not re-index an rocr mask rocminfo already applied

ROCR_VISIBLE_DEVICES is an ROCr-level mask, so rocminfo returns only the
selected agents, already in the mask's order. Indexing that filtered list again
by the mask's first ordinal double-applies it: on physical gfx1100,gfx1200 with
ROCR_VISIBLE_DEVICES=1,0 rocminfo reports gfx1200,gfx1100 and ordinal 1 picked
gfx1100, leaving the runtime-selected gfx1200 on kernel-less wheels.

Track which probe answered and skip the ordinal when rocminfo answered and
ROCR_VISIBLE_DEVICES is the first-set mask, matching the _LAST_AMD_GFX_PROBE
check in _runtime_target_needs_rocm64_generic_index. HIP_VISIBLE_DEVICES and
CUDA_VISIBLE_DEVICES are applied by the HIP runtime rather than by ROCr, and
amd-smi ignores all three, so both keep ordinal indexing.

The existing ROCR row in the visible-mask test modelled an impossible host,
rocminfo listing both GPUs while ROCR_VISIBLE_DEVICES selected one, and is
replaced by cases that report what a masked rocminfo actually returns.

* split amd-smi device blocks and repair floored wheels on migration

Two gaps in the gfx1102/RDNA 4 rocm6.4 floor.

The per-device gfx parser recognised rocminfo's "Agent N" and hipinfo's
"Device #N" but not amd-smi's "GPU: N" and "GPU[N]", which install.sh already
parses at line 3104 for its GPU-presence check. Without rocminfo, a host with
gfx1100, gfx1100, gfx1200 therefore collapsed to two entries and an ordinal of 2
fell out of range, so the selected RDNA 4 card missed the floor.
_detect_amd_gfx_codes had the same omission and is fixed alongside. A header
sharing its line with the token, as "GPU[0] : gfx1100" does, now contributes
that token instead of dropping it.

The migrated-environment repair only reinstalled torch when torch.version.hip
was empty, so a legacy venv already carrying a kernel-less +rocm6.1 wheel
finished migration with it after printing the reroute warning. It now also
reinstalls when the architecture floor selected rocm6.4 and the installed wheel
reports an older family, matching _installed_rocm_wheel_is_below on the Python
side. _venv_torch_rocm_below reads the family from the venv's torch, and an
unidentifiable family counts as older, as it does in Python.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* key the migrated repair on the gpu, not on whether the leaf moved

The flag the migrated repair reads was set inside the reroute, so it only
covered hosts detected at ROCm 6.0 to 6.3. A gfx1102/gfx1200/gfx1201 host
already reading 6.4 or newer selects a good index without rerouting, so the
flag stayed false and a legacy venv carrying +rocm6.1 kept it, which is the
same shape as the earlier install_python_stack.py fix that separated
_gfx_rocm64_target from _gfx_rocm64_floor.

_gfx_rocm64_target now records that the runtime GPU needs the floor at all,
independent of the leaf, and the reroute stays gated on the detected version.
An explicit index pin still skips the whole block, so it never triggers the
extra reinstall.

The routing harness in the tests returns the flag alongside the leaf, and
test_install_sh_marks_the_floor_target_independently_of_the_leaf pins it for
gfx1200 on rocm6.1, rocm6.4 and rocm7.2, with gfx1100 as the negative.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Keep floored gfx1102/gfx1200/gfx1201 off the Radeon repo; memoise ROCm detection

The rocm6.4 floor rewrote TORCH_INDEX_URL but left _amd_gpu_radeon set, so on a
Radeon-branded card the install took the Radeon branch and the floored index was
only its fallback. repo.radeon.com's newest pairing trio under rocm-rel-6.4/ is
torch 2.6.0+rocm6.4.0, whose rocBLAS Tensile libraries carry gfx1030/1100/1101/
1200/1201/908/942 and no gfx1102 at all, and rocm-rel-7.2/ is narrower still
(gfx120X-all, gfx90a, gfx942, gfx950). The arch the floor exists for therefore
still received a wheel with no kernels for it. Clear the flag whenever one of the
three arches is the runtime target, mirroring the gfx906 branch below, which
clears it even when its own reroute is a no-op.

Memoise _detect_rocm_version on the _TORCH_RUNTIME_PROBE pattern already in the
file. _ensure_rocm_torch() runs twice on Linux and the detector no longer
short-circuits, so all five sources were probed twice and the source-disagreement
warning printed twice where install.sh prints it once. The host ROCm stack cannot
change mid-run, so unlike the torch probe nothing invalidates it after a pip
operation; the reset exists for the autouse test fixtures.

Correct install_llama_prebuilt.py's claim that its detector mirrors install.sh
and _detect_rocm_version(). It no longer does: no status gate, no HSA runtime,
still first-answer-wins. The detector itself is left to a follow-up since it
moves llama.cpp asset selection rather than the torch family.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Read rocm-core over the distro HSA package, and stop tests reading the host

rocm-core comes from AMD's repo and marks the ROCm release; libhsa-runtime64-1
comes from the distro archive and tracks the archive. Ubuntu 24.04 with AMD's
ROCm 7.2 repo really does carry rocm-core 7.2.1 beside Ubuntu's own
libhsa-runtime64-1 5.7.1-2build1, so voting them as peers made every healthy
host print a disagreement warning naming a 5.7 reading that means nothing.
AMD's own HSA package is hsa-rocr, under a different name and versioned 1.x.
Debian ships no rocm-core, so the fallback still fires exactly where it was
added and the reported host still resolves rocm6.1.

Also stop three TestDetectRocmVersion cases reading the developer's machine:
the detector no longer returns at the first answer, so an unpatched test picks
up the host's real hipconfig or dpkg packages and CI never sees it.

And terminate records in the dpkg stub. $(...) strips trailing newlines, so a
real "\n" showformat concatenated every package onto one line and the
multi-package assertion only ever tested the first record.

* Split the gfx floor and installed-wheel repair out to a follow-up

danielhanchen asked for this PR to stay the ROCm version-source change, so the
per-device gfx indexing, the gfx1102/gfx1200/gfx1201 rocm6.4 floor and the
migrated installed-wheel repair all move out. They are one dependency chain
rather than three separable pieces: the floor needs per-device indexing to read
the right runtime arch, and the repair keys off the floor target.

Kept here: get_radeon_wheel_url taking the already-resolved leaf. Without it a
split Debian host resolves rocm6.1 and then the Radeon branch re-probes from
scratch, rediscovers hipconfig 5.7 and asks for rocm-rel-5.7/, so it closes a
hole this change opens rather than adding scope.

* Index AMD visible-device masks by device, and floor gfx1102/RDNA4 to rocm6.4

Split out of #8886 so that PR stays the ROCm version-source change. Stacked on
it: the floor consumes the leaf #8886 resolves.

One dependency chain. _gfx_targets_per_device emits one gfx token per device, so
a visible-device mask selects a card rather than an arch. The floor then reads
the right runtime arch and routes gfx1102/gfx1200/gfx1201 to the generic rocm6.4
wheels, which are the oldest family shipping their kernels. The migrated repair
keys off that floor target so an upgraded venv stops keeping a kernel-less wheel.

* Collapse the per-device gfx probe onto the existing GPU record helpers

_gfx_targets_per_device parsed a second time what _rocminfo_gpu_records (#9498)
and _amd_smi_gpu_records already parse for the GPU summary, so delete it and read
the gfx half of those records instead. Both move up to the probe-parser block so
the arch routing can call them; their bodies are unchanged, so the studio/setup.sh
mirrors and their tests stay as they are.

One parser per tool now, shared by the routing block and the display block: a mask
ordinal indexes cards in both, and a duplicate arch cannot collapse two devices
into one entry in either.

The amd-smi shim in test_gfx_probe_survives_no_match_under_set_e answered every
subcommand with "GPU: gfx1151", which is not a shape amd-smi emits; it now answers
`list` with BDF only and `static --asic` with TARGET_GRAPHICS_VERSION, so the test
proves the fallthrough it describes. Same for the flat-output amd-smi stub in
test_install_sh_still_indexes_amd_smi_output_under_a_rocr_mask.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* amd-smi: take the bracketed GPU[N] header too, arch and all

The record parser only opened a device on `GPU: N`, so amd-smi's other header
shape, `GPU[0]  : gfx1100`, produced no records at all. That shape is not
hypothetical: it carries the arch on the header line itself with no
TARGET_GRAPHICS_VERSION following, and the rest of the tree already reads it.
_has_amd_rocm_gpu matches `gpu[ ]*[:[][ ]*[0-9]` at install.sh:3208, the Python
detector matches the same at install_python_stack.py:1660, and the per-device
split at :1814 carries a comment saying install.sh parses the same shapes. It
did not.

Consequence on an amd-smi-only host (no rocminfo): _gfx_all came back empty, so
_runtime_gfx was empty and every arch policy this PR adds was skipped without a
word. A gfx1200 box resolving ROCm 6.1 keeps the kernel-less rocm6.1 wheels
instead of being floored to rocm6.4, which is the case the PR exists for.

Header shapes now both open a record, and the header's own gfx token is taken
when it has one. Applied to studio/setup.sh identically; the parity assertion in
test_rocminfo_gpu_name_7307.sh caught the two copies drifting on a comment while
this was being written.

Three cases added to that suite, which now sources the amd-smi parser rather than
only diffing the two copies of it: bracketed multi-device keeps one record per
device in order, the keyed form still pairs arch with name, and a header with no
arch stays empty instead of borrowing the next device's.

Verified: 26 passed, 0 failed on Git Bash. test_install_gpu_summary_probe_sources
and test_setup_gpu_summary_probe_sources report identical results with and
without this commit on this box (0/28 and 3/37); they need a POSIX symlink the
environment cannot make, and pass on the Linux runner.

* Translate amd-smi order before indexing it, and keep unreadable adapters

Two ways the arch routing could read the wrong adapter, both from treating a
position in a list as if it were a device id.

Order. amd-smi enumerates in discovery order, which is not the order HIP numbers
devices, and the routing applied a HIP/ROCR/CUDA ordinal to it directly. The
translation already existed: _amd_smi_hip_order builds a HIP_ID map from
`amd-smi list -e`, and the GPU summary has used it all along. Routing now runs
the same translation, and when no map comes back and the adapters are not alike
it declines with a warning rather than guess, which is the call the summary
already makes on the same evidence.

    discovery [gfx90a MI210, gfx1200 RX 9070], HIP order reversed
    HIP_VISIBLE_DEVICES=0   before: gfx90a    after: gfx1200
    HIP_VISIBLE_DEVICES=1   before: gfx1200   after: gfx90a
    same, no HIP_ID map     before: gfx90a    after: declines

Blank slots. _amd_smi_gpu_records emits an empty gfx field on purpose so an
adapter whose arch it cannot read keeps its ordinal, and the index step then
filtered those lines out with NF, collapsing the list. Command substitution ate
a trailing one too.

    [blank, gfx1151], HIP_VISIBLE_DEVICES=0
    before: gfx1151, so the Strix reroute fired on device 0's behalf
    after:  unknown, which matches no policy

_gfx_arch_slots does that preservation, and prints nothing when NO adapter has an
arch so the probe chain still falls through to the next source.

Two places deliberately keep the old behaviour. rocminfo is an ROCr client and
already enumerates in the order HIP numbers from, which is the premise the mask
handling was written on, so it is marked hip and skips the check. Identical
adapters skip it too: every ordinal gives the same answer, so there is nothing
to get wrong.

Seven cases added to test_rocminfo_gpu_name_7307.sh, which now sources
_gfx_arch_slots and _amd_smi_hip_order. 33 passed, 0 failed on Git Bash.
sh -n and bash -n clean. test_install_gpu_summary_probe_sources and
test_setup_gpu_summary_probe_sources report 0/28 and 3/37 with and without this
commit alike; they need a POSIX symlink this box cannot make.

The decline path itself is three lines of shell inside the routing block and is
not covered end to end here: that needs the block harness, which is one of the
two suites the environment cannot run. Its inputs are covered.

* Define _amd_smi_hip_order before the routing calls it, and inject it into the tests

Both are the same mistake in two places: a shell helper that is not in scope
where it is used, hidden by an `|| true`.

install.sh is sequential and the GPU probing runs at top level, inside the case
opened at 4806. The translation call added last push sits at 4847; the helper was
defined at 5108, 261 lines further down. So on every amd-smi host the shell said
command not found, `|| true` swallowed it, _gfx_smi_out came back empty, and the
next two lines erased records that were fine. Reproduced in three lines:

    out=$(printf 'x\n' | _fn 'recs' || true); echo "[$(printf '%s\n' "$out" | head -1)]"
    _fn() { echo hip; }
    -> _fn: command not found
    -> []

Moved next to _gfx_arch_slots at 3590, ahead of both call sites. Body unchanged,
so the install.sh/setup.sh parity assertion still holds.

Same shape in tests/studio/install/test_rocm_support.py. Its probe blocks are
extracted from install.sh and run with only the helpers the harness injects, and
that list was still the two record parsers. The routing block calls
_gfx_arch_slots now, so bash printed `_gfx_arch_slots: command not found` four
times and then a syntax error. Added both new helpers to _gpu_record_helpers,
with a note on why a missing one is worse than a loud failure: the block's
`|| true` turns it into an empty arch list and the assertion passes for the
wrong reason.

Four cases added to test_rocminfo_gpu_name_7307.sh asserting each helper is
defined above its first call, by order rather than by line number so a later edit
does not have to update them. Checked against b8fbc16 to confirm the guard
actually fires: FAIL _amd_smi_hip_order def=5108 firstuse=4847 there, PASS here.

37 passed, 0 failed. sh -n, bash -n and python -m ast all clean.

Not re-run: tests/studio/install/test_rocm_support.py. It downloads models and
filled this disk mid-run, taking a baseline comparison with it. The
command-not-found lines it printed are gone, which is what the harness change was
for, but I am not quoting a pass count I did not watch finish.

* Answer `amd-smi list -e` in the routing stubs so the ordinal tests test an ordinal

The amd-smi stubs ignored argv and returned the ASIC listing for every call,
`amd-smi list -e` included. So the routing block saw no HIP_ID map, and with
unlike adapters known only in discovery order it correctly declined to apply an
ordinal and left the caller's leaf alone. The leaf it left alone was rocm6.1,
the default these tests pass in.

That is a real miss, not a cosmetic one, and it was hiding in the pass column:

  splits_amd_smi_gpu_headers[HIP_VISIBLE_DEVICES=2]  expected rocm6.4 got rocm6.1
  splits_amd_smi_gpu_headers[HIP_VISIBLE_DEVICES=1]  expected rocm6.1 got rocm6.1
  still_indexes_amd_smi_output_under_a_rocr_mask     expected rocm6.1 got rocm6.1

Only the first failed. The other two agree with the decline by coincidence, so
they were asserting nothing about the header splitting and the ROCR indexing
they are named for.

_amd_smi_stub answers `list -e` with a HIP_ID map, identity by default, which is
what a caller indexing the listing directly already assumes. Both call sites use
it. Added a third test that withholds the map on the same adapters and asserts
the decline, so the behaviour that was silently absorbing these three now has
one test that means to land there.

4 passed, 0 failed, and the old stubs reproduce the table above against the same
install.sh.

Verified by running the harness's own assembled script from a file rather than
through pytest. Git Bash on Windows truncates a `bash -c` payload at 8188 bytes
and these scripts are ~23.5k, so every test in this class dies there with a
syntax error from the cut, whatever the branch. That is this machine, not the
diff; the Linux runner executes them normally.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Compose the ROCr and HIP visibility layers instead of shadowing one with the other

rocminfo is an ROCr client, so its output is already filtered by
ROCR_VISIBLE_DEVICES. The routing then picked the first variable that was SET
across all three layers, so an ROCr mask that had already done its work shadowed
CUDA_VISIBLE_DEVICES, the HIP-layer alias, which should select among the
survivors. With ROCR_VISIBLE_DEVICES=2,1 and CUDA_VISIBLE_DEVICES=1 the target
is the second survivor; the old chain named the first and floored the index for
the wrong card.

Consult only the HIP layer once rocminfo has applied the ROCr one, mirroring
_HIP_LAYER_MASKS in studio/install_python_stack.py, which already did this.

* Make the generic-wheel floor tests independent of the host's own torch

TestGfx1102Rocm64Floor._ensure_for_gfx patched the detectors but left
_installed_rocm_wheel_family and _torch_requires_rocm_sdk reading the RUNNING
interpreter. The scenarios it sets up install generic pytorch.org wheels, which
do not pull rocm[libraries], so the per-arch family is None by construction.

On a gfx1151 Strix Halo runner whose venv holds 2.11.0+rocm7.13.0 they answered
gfx1151 instead. The per-arch repick arm then saw _have != _want, reinstalled,
and test_installed_64_wheel_left_alone_on_newer_host and
test_unaffected_arch_not_repaired_on_newer_host failed on AMD hardware while
passing on every CUDA or CPU box, this one included.

Pin both to the scenario. A test about the generic-wheel floor must not depend
on what the machine running it happens to have installed.

* Tighten the comments this change adds

Comments only. Every load-bearing fact stays: the issue numbers, the POSIX awk
constraint on -v newlines, the two amd-smi header shapes, why an unreadable arch
keeps its slot, why the ROCr and HIP layers compose rather than shadow, and the
keep-in-sync pointers to studio/setup.sh and install_python_stack.py.

Applied to studio/setup.sh as well: test_rocminfo_gpu_name_7307.sh asserts the
two copies of the amd-smi parser and HIP reorder are byte identical, so editing
one alone breaks parity. The same test also sed-matches the first-set-wins
comment to lift a block, so that anchor line is preserved verbatim.

* Scrub CUDA_VISIBLE_DEVICES in the generic-wheel floor fixture

_visible_masks_select_no_gpu reads CUDA_VISIBLE_DEVICES as the HIP alias when
HIP_VISIBLE_DEVICES is unset, so a host running the suite with
CUDA_VISIBLE_DEVICES="" (a CPU CI shard, or a developer hiding a CUDA card)
made _runtime_gfx_target answer no GPU and the three debian-split floor tests
took the generic index instead of the per-arch one. The fixture already
scrubbed the HIP and ROCr masks; scrub the CUDA one too, as the other
fixtures in this file do. 625 pass with the variable empty, set and unset.

* Tighten the comments this change adds for PR #9672

* Fall through to amd-smi static --asic when list carries no arch for PR #9672

* Layer ROCR_VISIBLE_DEVICES under the HIP index on the amd-smi path for PR #9672

* Decline arch routing on a ROCr UUID mask over unlike amd-smi adapters for PR #9672

---------

Co-authored-by: Natani L. Mayday <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maheswar Kumar <[email protected]>
Co-authored-by: danielhanchen <[email protected]>
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…NA4 to rocm6.4 (unslothai#9672)

* Fix ROCm detection on split Debian stacks

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Harden ROCm detection on split Debian stacks

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* close the gaps in the gfx1102/RDNA4 rocm6.4 floor

Three fixes on top of the split-Debian ROCm detection work.

studio/install_python_stack.py gated the kernel-less-wheel repair on
_gfx_needs_rocm64_generic_index(ver), so a gfx1200 host reading ROCm 6.4 or
newer skipped it entirely and studio update left an installed +rocm6.1 build
in place. Split the runtime-target test from the host-version test: the index
reroute stays version-gated, the repair now runs on the target alone.

install.sh resolved _runtime_gfx from HIP_VISIBLE_DEVICES and
ROCR_VISIBLE_DEVICES only. AMD documents CUDA_VISIBLE_DEVICES as a HIP
visibility variable and recommends it for cross-vendor portability, so a mixed
gfx1100 + gfx1200 host masked with CUDA_VISIBLE_DEVICES=1 selected gfx1200 at
runtime while the installer routed for gfx1100. Resolve the three variables
first-set-wins, matching _pick_visible_index.

install.sh also took UNSLOTH_ROCM_GFX_ARCH verbatim, so a value copied from a
HIP gcnArchName (gfx1200:xnack-) matched none of the bare architecture
patterns. Strip the suffix and surrounding whitespace the way _gfx906_env and
_runtime_target_needs_rocm64_generic_index already do.

Regressions cover all three, and each fails without its fix.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* keep the real host ROCm release for repo.radeon.com

get_torch_index_url clips its leaf to a family PyTorch publishes: ROCm 6.5
becomes rocm6.4, 7.3 and newer become rocm7.2, and no leaf ever carries a
patch level. repo.radeon.com does publish rocm-rel-6.5 and rocm-rel-7.2.1,
so passing that leaf into get_radeon_wheel_url pointed a Radeon host at an
older directory, or at none when only the patch-level directory exists. The
caller's X.Y.Z to X.Y retry cannot recover it because a leaf is already X.Y.

Treat the resolved leaf as a floor instead. The host probe runs as before and
wins whenever it reads no older than the leaf, so 6.5.0 and 7.2.1 reach their
own directories. The leaf takes over when the probe is missing or older than
the resolver's answer, which is the split Debian host whose hipconfig reports
5.7, the case the argument was added for.

tests/sh/test_rocm_version_source_disagreement.sh covers both directions and
extracts the new comparison helper alongside the functions it tests.

* index visible-device masks by device, not by arch

HIP_VISIBLE_DEVICES, ROCR_VISIBLE_DEVICES and CUDA_VISIBLE_DEVICES carry device
ordinals, but install.sh built its gfx list with a flat grep and then
de-duplicated it before indexing. On a host enumerating gfx1100, gfx1100,
gfx1200 an ordinal of 2 ran past the end of the two-entry list and silently
fell back to the first gfx1100, so the selected RDNA 4 card never reached the
rocm6.4 floor and received wheels with no kernels for it.

The de-duplication was not gratuitous: rocminfo prints a device's arch on both
its Name and ISA lines, so counting raw tokens is wrong too. _gfx_targets_per_device
splits on agent headers and takes one token per device, exactly as
_detect_amd_gfx_codes(dedup=False) does in studio/install_python_stack.py, and
falls back to first-seen order for flat output such as amd-smi's. With the list
now one entry per device, the indexing step no longer de-duplicates.

Also refreshes the gfx906 comment that justified its policy by the old
de-duplication, and extracts the new helper in the two Strix probe tests that
run the detection block standalone.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* do not re-index an rocr mask rocminfo already applied

ROCR_VISIBLE_DEVICES is an ROCr-level mask, so rocminfo returns only the
selected agents, already in the mask's order. Indexing that filtered list again
by the mask's first ordinal double-applies it: on physical gfx1100,gfx1200 with
ROCR_VISIBLE_DEVICES=1,0 rocminfo reports gfx1200,gfx1100 and ordinal 1 picked
gfx1100, leaving the runtime-selected gfx1200 on kernel-less wheels.

Track which probe answered and skip the ordinal when rocminfo answered and
ROCR_VISIBLE_DEVICES is the first-set mask, matching the _LAST_AMD_GFX_PROBE
check in _runtime_target_needs_rocm64_generic_index. HIP_VISIBLE_DEVICES and
CUDA_VISIBLE_DEVICES are applied by the HIP runtime rather than by ROCr, and
amd-smi ignores all three, so both keep ordinal indexing.

The existing ROCR row in the visible-mask test modelled an impossible host,
rocminfo listing both GPUs while ROCR_VISIBLE_DEVICES selected one, and is
replaced by cases that report what a masked rocminfo actually returns.

* split amd-smi device blocks and repair floored wheels on migration

Two gaps in the gfx1102/RDNA 4 rocm6.4 floor.

The per-device gfx parser recognised rocminfo's "Agent N" and hipinfo's
"Device #N" but not amd-smi's "GPU: N" and "GPU[N]", which install.sh already
parses at line 3104 for its GPU-presence check. Without rocminfo, a host with
gfx1100, gfx1100, gfx1200 therefore collapsed to two entries and an ordinal of 2
fell out of range, so the selected RDNA 4 card missed the floor.
_detect_amd_gfx_codes had the same omission and is fixed alongside. A header
sharing its line with the token, as "GPU[0] : gfx1100" does, now contributes
that token instead of dropping it.

The migrated-environment repair only reinstalled torch when torch.version.hip
was empty, so a legacy venv already carrying a kernel-less +rocm6.1 wheel
finished migration with it after printing the reroute warning. It now also
reinstalls when the architecture floor selected rocm6.4 and the installed wheel
reports an older family, matching _installed_rocm_wheel_is_below on the Python
side. _venv_torch_rocm_below reads the family from the venv's torch, and an
unidentifiable family counts as older, as it does in Python.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* key the migrated repair on the gpu, not on whether the leaf moved

The flag the migrated repair reads was set inside the reroute, so it only
covered hosts detected at ROCm 6.0 to 6.3. A gfx1102/gfx1200/gfx1201 host
already reading 6.4 or newer selects a good index without rerouting, so the
flag stayed false and a legacy venv carrying +rocm6.1 kept it, which is the
same shape as the earlier install_python_stack.py fix that separated
_gfx_rocm64_target from _gfx_rocm64_floor.

_gfx_rocm64_target now records that the runtime GPU needs the floor at all,
independent of the leaf, and the reroute stays gated on the detected version.
An explicit index pin still skips the whole block, so it never triggers the
extra reinstall.

The routing harness in the tests returns the flag alongside the leaf, and
test_install_sh_marks_the_floor_target_independently_of_the_leaf pins it for
gfx1200 on rocm6.1, rocm6.4 and rocm7.2, with gfx1100 as the negative.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Keep floored gfx1102/gfx1200/gfx1201 off the Radeon repo; memoise ROCm detection

The rocm6.4 floor rewrote TORCH_INDEX_URL but left _amd_gpu_radeon set, so on a
Radeon-branded card the install took the Radeon branch and the floored index was
only its fallback. repo.radeon.com's newest pairing trio under rocm-rel-6.4/ is
torch 2.6.0+rocm6.4.0, whose rocBLAS Tensile libraries carry gfx1030/1100/1101/
1200/1201/908/942 and no gfx1102 at all, and rocm-rel-7.2/ is narrower still
(gfx120X-all, gfx90a, gfx942, gfx950). The arch the floor exists for therefore
still received a wheel with no kernels for it. Clear the flag whenever one of the
three arches is the runtime target, mirroring the gfx906 branch below, which
clears it even when its own reroute is a no-op.

Memoise _detect_rocm_version on the _TORCH_RUNTIME_PROBE pattern already in the
file. _ensure_rocm_torch() runs twice on Linux and the detector no longer
short-circuits, so all five sources were probed twice and the source-disagreement
warning printed twice where install.sh prints it once. The host ROCm stack cannot
change mid-run, so unlike the torch probe nothing invalidates it after a pip
operation; the reset exists for the autouse test fixtures.

Correct install_llama_prebuilt.py's claim that its detector mirrors install.sh
and _detect_rocm_version(). It no longer does: no status gate, no HSA runtime,
still first-answer-wins. The detector itself is left to a follow-up since it
moves llama.cpp asset selection rather than the torch family.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Read rocm-core over the distro HSA package, and stop tests reading the host

rocm-core comes from AMD's repo and marks the ROCm release; libhsa-runtime64-1
comes from the distro archive and tracks the archive. Ubuntu 24.04 with AMD's
ROCm 7.2 repo really does carry rocm-core 7.2.1 beside Ubuntu's own
libhsa-runtime64-1 5.7.1-2build1, so voting them as peers made every healthy
host print a disagreement warning naming a 5.7 reading that means nothing.
AMD's own HSA package is hsa-rocr, under a different name and versioned 1.x.
Debian ships no rocm-core, so the fallback still fires exactly where it was
added and the reported host still resolves rocm6.1.

Also stop three TestDetectRocmVersion cases reading the developer's machine:
the detector no longer returns at the first answer, so an unpatched test picks
up the host's real hipconfig or dpkg packages and CI never sees it.

And terminate records in the dpkg stub. $(...) strips trailing newlines, so a
real "\n" showformat concatenated every package onto one line and the
multi-package assertion only ever tested the first record.

* Split the gfx floor and installed-wheel repair out to a follow-up

danielhanchen asked for this PR to stay the ROCm version-source change, so the
per-device gfx indexing, the gfx1102/gfx1200/gfx1201 rocm6.4 floor and the
migrated installed-wheel repair all move out. They are one dependency chain
rather than three separable pieces: the floor needs per-device indexing to read
the right runtime arch, and the repair keys off the floor target.

Kept here: get_radeon_wheel_url taking the already-resolved leaf. Without it a
split Debian host resolves rocm6.1 and then the Radeon branch re-probes from
scratch, rediscovers hipconfig 5.7 and asks for rocm-rel-5.7/, so it closes a
hole this change opens rather than adding scope.

* Index AMD visible-device masks by device, and floor gfx1102/RDNA4 to rocm6.4

Split out of unslothai#8886 so that PR stays the ROCm version-source change. Stacked on
it: the floor consumes the leaf unslothai#8886 resolves.

One dependency chain. _gfx_targets_per_device emits one gfx token per device, so
a visible-device mask selects a card rather than an arch. The floor then reads
the right runtime arch and routes gfx1102/gfx1200/gfx1201 to the generic rocm6.4
wheels, which are the oldest family shipping their kernels. The migrated repair
keys off that floor target so an upgraded venv stops keeping a kernel-less wheel.

* Collapse the per-device gfx probe onto the existing GPU record helpers

_gfx_targets_per_device parsed a second time what _rocminfo_gpu_records (unslothai#9498)
and _amd_smi_gpu_records already parse for the GPU summary, so delete it and read
the gfx half of those records instead. Both move up to the probe-parser block so
the arch routing can call them; their bodies are unchanged, so the studio/setup.sh
mirrors and their tests stay as they are.

One parser per tool now, shared by the routing block and the display block: a mask
ordinal indexes cards in both, and a duplicate arch cannot collapse two devices
into one entry in either.

The amd-smi shim in test_gfx_probe_survives_no_match_under_set_e answered every
subcommand with "GPU: gfx1151", which is not a shape amd-smi emits; it now answers
`list` with BDF only and `static --asic` with TARGET_GRAPHICS_VERSION, so the test
proves the fallthrough it describes. Same for the flat-output amd-smi stub in
test_install_sh_still_indexes_amd_smi_output_under_a_rocr_mask.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* amd-smi: take the bracketed GPU[N] header too, arch and all

The record parser only opened a device on `GPU: N`, so amd-smi's other header
shape, `GPU[0]  : gfx1100`, produced no records at all. That shape is not
hypothetical: it carries the arch on the header line itself with no
TARGET_GRAPHICS_VERSION following, and the rest of the tree already reads it.
_has_amd_rocm_gpu matches `gpu[ ]*[:[][ ]*[0-9]` at install.sh:3208, the Python
detector matches the same at install_python_stack.py:1660, and the per-device
split at :1814 carries a comment saying install.sh parses the same shapes. It
did not.

Consequence on an amd-smi-only host (no rocminfo): _gfx_all came back empty, so
_runtime_gfx was empty and every arch policy this PR adds was skipped without a
word. A gfx1200 box resolving ROCm 6.1 keeps the kernel-less rocm6.1 wheels
instead of being floored to rocm6.4, which is the case the PR exists for.

Header shapes now both open a record, and the header's own gfx token is taken
when it has one. Applied to studio/setup.sh identically; the parity assertion in
test_rocminfo_gpu_name_7307.sh caught the two copies drifting on a comment while
this was being written.

Three cases added to that suite, which now sources the amd-smi parser rather than
only diffing the two copies of it: bracketed multi-device keeps one record per
device in order, the keyed form still pairs arch with name, and a header with no
arch stays empty instead of borrowing the next device's.

Verified: 26 passed, 0 failed on Git Bash. test_install_gpu_summary_probe_sources
and test_setup_gpu_summary_probe_sources report identical results with and
without this commit on this box (0/28 and 3/37); they need a POSIX symlink the
environment cannot make, and pass on the Linux runner.

* Translate amd-smi order before indexing it, and keep unreadable adapters

Two ways the arch routing could read the wrong adapter, both from treating a
position in a list as if it were a device id.

Order. amd-smi enumerates in discovery order, which is not the order HIP numbers
devices, and the routing applied a HIP/ROCR/CUDA ordinal to it directly. The
translation already existed: _amd_smi_hip_order builds a HIP_ID map from
`amd-smi list -e`, and the GPU summary has used it all along. Routing now runs
the same translation, and when no map comes back and the adapters are not alike
it declines with a warning rather than guess, which is the call the summary
already makes on the same evidence.

    discovery [gfx90a MI210, gfx1200 RX 9070], HIP order reversed
    HIP_VISIBLE_DEVICES=0   before: gfx90a    after: gfx1200
    HIP_VISIBLE_DEVICES=1   before: gfx1200   after: gfx90a
    same, no HIP_ID map     before: gfx90a    after: declines

Blank slots. _amd_smi_gpu_records emits an empty gfx field on purpose so an
adapter whose arch it cannot read keeps its ordinal, and the index step then
filtered those lines out with NF, collapsing the list. Command substitution ate
a trailing one too.

    [blank, gfx1151], HIP_VISIBLE_DEVICES=0
    before: gfx1151, so the Strix reroute fired on device 0's behalf
    after:  unknown, which matches no policy

_gfx_arch_slots does that preservation, and prints nothing when NO adapter has an
arch so the probe chain still falls through to the next source.

Two places deliberately keep the old behaviour. rocminfo is an ROCr client and
already enumerates in the order HIP numbers from, which is the premise the mask
handling was written on, so it is marked hip and skips the check. Identical
adapters skip it too: every ordinal gives the same answer, so there is nothing
to get wrong.

Seven cases added to test_rocminfo_gpu_name_7307.sh, which now sources
_gfx_arch_slots and _amd_smi_hip_order. 33 passed, 0 failed on Git Bash.
sh -n and bash -n clean. test_install_gpu_summary_probe_sources and
test_setup_gpu_summary_probe_sources report 0/28 and 3/37 with and without this
commit alike; they need a POSIX symlink this box cannot make.

The decline path itself is three lines of shell inside the routing block and is
not covered end to end here: that needs the block harness, which is one of the
two suites the environment cannot run. Its inputs are covered.

* Define _amd_smi_hip_order before the routing calls it, and inject it into the tests

Both are the same mistake in two places: a shell helper that is not in scope
where it is used, hidden by an `|| true`.

install.sh is sequential and the GPU probing runs at top level, inside the case
opened at 4806. The translation call added last push sits at 4847; the helper was
defined at 5108, 261 lines further down. So on every amd-smi host the shell said
command not found, `|| true` swallowed it, _gfx_smi_out came back empty, and the
next two lines erased records that were fine. Reproduced in three lines:

    out=$(printf 'x\n' | _fn 'recs' || true); echo "[$(printf '%s\n' "$out" | head -1)]"
    _fn() { echo hip; }
    -> _fn: command not found
    -> []

Moved next to _gfx_arch_slots at 3590, ahead of both call sites. Body unchanged,
so the install.sh/setup.sh parity assertion still holds.

Same shape in tests/studio/install/test_rocm_support.py. Its probe blocks are
extracted from install.sh and run with only the helpers the harness injects, and
that list was still the two record parsers. The routing block calls
_gfx_arch_slots now, so bash printed `_gfx_arch_slots: command not found` four
times and then a syntax error. Added both new helpers to _gpu_record_helpers,
with a note on why a missing one is worse than a loud failure: the block's
`|| true` turns it into an empty arch list and the assertion passes for the
wrong reason.

Four cases added to test_rocminfo_gpu_name_7307.sh asserting each helper is
defined above its first call, by order rather than by line number so a later edit
does not have to update them. Checked against b8fbc16 to confirm the guard
actually fires: FAIL _amd_smi_hip_order def=5108 firstuse=4847 there, PASS here.

37 passed, 0 failed. sh -n, bash -n and python -m ast all clean.

Not re-run: tests/studio/install/test_rocm_support.py. It downloads models and
filled this disk mid-run, taking a baseline comparison with it. The
command-not-found lines it printed are gone, which is what the harness change was
for, but I am not quoting a pass count I did not watch finish.

* Answer `amd-smi list -e` in the routing stubs so the ordinal tests test an ordinal

The amd-smi stubs ignored argv and returned the ASIC listing for every call,
`amd-smi list -e` included. So the routing block saw no HIP_ID map, and with
unlike adapters known only in discovery order it correctly declined to apply an
ordinal and left the caller's leaf alone. The leaf it left alone was rocm6.1,
the default these tests pass in.

That is a real miss, not a cosmetic one, and it was hiding in the pass column:

  splits_amd_smi_gpu_headers[HIP_VISIBLE_DEVICES=2]  expected rocm6.4 got rocm6.1
  splits_amd_smi_gpu_headers[HIP_VISIBLE_DEVICES=1]  expected rocm6.1 got rocm6.1
  still_indexes_amd_smi_output_under_a_rocr_mask     expected rocm6.1 got rocm6.1

Only the first failed. The other two agree with the decline by coincidence, so
they were asserting nothing about the header splitting and the ROCR indexing
they are named for.

_amd_smi_stub answers `list -e` with a HIP_ID map, identity by default, which is
what a caller indexing the listing directly already assumes. Both call sites use
it. Added a third test that withholds the map on the same adapters and asserts
the decline, so the behaviour that was silently absorbing these three now has
one test that means to land there.

4 passed, 0 failed, and the old stubs reproduce the table above against the same
install.sh.

Verified by running the harness's own assembled script from a file rather than
through pytest. Git Bash on Windows truncates a `bash -c` payload at 8188 bytes
and these scripts are ~23.5k, so every test in this class dies there with a
syntax error from the cut, whatever the branch. That is this machine, not the
diff; the Linux runner executes them normally.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Compose the ROCr and HIP visibility layers instead of shadowing one with the other

rocminfo is an ROCr client, so its output is already filtered by
ROCR_VISIBLE_DEVICES. The routing then picked the first variable that was SET
across all three layers, so an ROCr mask that had already done its work shadowed
CUDA_VISIBLE_DEVICES, the HIP-layer alias, which should select among the
survivors. With ROCR_VISIBLE_DEVICES=2,1 and CUDA_VISIBLE_DEVICES=1 the target
is the second survivor; the old chain named the first and floored the index for
the wrong card.

Consult only the HIP layer once rocminfo has applied the ROCr one, mirroring
_HIP_LAYER_MASKS in studio/install_python_stack.py, which already did this.

* Make the generic-wheel floor tests independent of the host's own torch

TestGfx1102Rocm64Floor._ensure_for_gfx patched the detectors but left
_installed_rocm_wheel_family and _torch_requires_rocm_sdk reading the RUNNING
interpreter. The scenarios it sets up install generic pytorch.org wheels, which
do not pull rocm[libraries], so the per-arch family is None by construction.

On a gfx1151 Strix Halo runner whose venv holds 2.11.0+rocm7.13.0 they answered
gfx1151 instead. The per-arch repick arm then saw _have != _want, reinstalled,
and test_installed_64_wheel_left_alone_on_newer_host and
test_unaffected_arch_not_repaired_on_newer_host failed on AMD hardware while
passing on every CUDA or CPU box, this one included.

Pin both to the scenario. A test about the generic-wheel floor must not depend
on what the machine running it happens to have installed.

* Tighten the comments this change adds

Comments only. Every load-bearing fact stays: the issue numbers, the POSIX awk
constraint on -v newlines, the two amd-smi header shapes, why an unreadable arch
keeps its slot, why the ROCr and HIP layers compose rather than shadow, and the
keep-in-sync pointers to studio/setup.sh and install_python_stack.py.

Applied to studio/setup.sh as well: test_rocminfo_gpu_name_7307.sh asserts the
two copies of the amd-smi parser and HIP reorder are byte identical, so editing
one alone breaks parity. The same test also sed-matches the first-set-wins
comment to lift a block, so that anchor line is preserved verbatim.

* Scrub CUDA_VISIBLE_DEVICES in the generic-wheel floor fixture

_visible_masks_select_no_gpu reads CUDA_VISIBLE_DEVICES as the HIP alias when
HIP_VISIBLE_DEVICES is unset, so a host running the suite with
CUDA_VISIBLE_DEVICES="" (a CPU CI shard, or a developer hiding a CUDA card)
made _runtime_gfx_target answer no GPU and the three debian-split floor tests
took the generic index instead of the per-arch one. The fixture already
scrubbed the HIP and ROCr masks; scrub the CUDA one too, as the other
fixtures in this file do. 625 pass with the variable empty, set and unset.

* Tighten the comments this change adds for PR unslothai#9672

* Fall through to amd-smi static --asic when list carries no arch for PR unslothai#9672

* Layer ROCR_VISIBLE_DEVICES under the HIP index on the amd-smi path for PR unslothai#9672

* Decline arch routing on a ROCr UUID mask over unlike amd-smi adapters for PR unslothai#9672

---------

Co-authored-by: Natani L. Mayday <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maheswar Kumar <[email protected]>
Co-authored-by: danielhanchen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants