Repository navigation
Linux: resolve the ROCm version from every source, not the first that answers - #8414
Conversation
…wers install.sh took the ROCm version from the first source that responded, in the order amd-smi, /opt/rocm/.info/version, hipconfig, dpkg rocm-core, rpm rocm-core. Debian 13 (and Linux Mint on top of it) packages hipconfig at 5.7.31921-0 next to a 6.1.x rocminfo/HSA runtime, so detection settled on rocm5.7, the "PyTorch ROCm wheels require ROCm 6.0+" gate fired, and a working RX 7900 XTX got torch 2.10.0+cpu. The same install log then reported "AMD ROCm (gfx1100) ROCm: /opt/rocm", so the installer held evidence against its own conclusion. Detection now reads every source and takes the highest reading. A source reporting lower than another on the same host is stale packaging, not a downgrade, so the highest is the one that describes the runtime the GPU uses. The inline || chain is split into one helper per source plus a highest-wins resolver; each helper returns 0 unconditionally, so the case where every source is missing still reaches the actionable no-version warning instead of dying under set -e. The sub-6.0 warning also names a way forward. It now says the reported version was the highest any source gave, and names UNSLOTH_TORCH_INDEX_FAMILY and UNSLOTH_TORCH_INDEX_URL, both of which return from get_torch_index_url before any GPU probing and are forwarded through the WSL reroute, so they are a real override for a host with split ROCm packaging. Tests: tests/sh/test_rocm_version_source_disagreement.sh covers the reported Debian 13 shape, a stale reading in each source position, a genuine ROCm 5.x host still falling back to CPU, every source missing not killing the installer under set -e, and the unchanged tag normalisation (patch levels, 6.5+ clip, 7.3+ cap). test_rocm_support.py's set -e guard is rewritten against the new shape and gains a per-position highest-wins check.
Resolving the ROCm version by highest reading fixed the undershoot in #8402 and opened the symmetric question: a source reading HIGHER than the host now picks the wheel family instead of being shadowed by its position in the chain. Most of that is fine and was already bounded. PyTorch's ROCm wheels vendor their own ROCm userspace into torch/lib, so what they need from the host is the amdgpu/KFD driver, and AMD documents driver/userspace compatibility as +/- 2 releases, widening to about a year's span from 6.4 on. The normalisation below can only emit an index leaf PyTorch actually publishes. A host that reads high because it genuinely carries a newer ROCm tree is therefore usually right, or at worst harmless. What is not bounded is a reading taken off something that is not installed at all, and dpkg could produce one. `dpkg-query -W` lists every package in the status database except purged ones, so a rocm-core taken out with `apt remove` and never purged sits in state "deinstall ok config-files" and still reports the version it had. A host that ran ROCm 7.0, went back to 6.1 and never purged therefore offered a 7.0 reading off a package that is gone. Fourth in the old first-answer chain it was usually shadowed; under highest-wins it beat every live source. Detection now asks for ${Status} alongside ${Version} and takes the version only when the status word is "installed". ${Status} is a documented showformat field with no dpkg version floor, and dpkg-query renders an unrecognised field as empty rather than failing, so on a dpkg that lacked it this source would go silent rather than over-report. The other four were audited for the same class of state and none has one. rpm has no equivalent of config-files: `rpm -e` drops the header record, and `rpm -q` then exits 1, which the helper already treats as no answer. amd-smi resolves librocm-core.so and hipconfig reads its own tree's version file, so both report a ROCm that is installed. /opt/rocm/.info/version describes whatever tree /opt/rocm resolves to. What those three cannot do, on a host carrying several ROCm trees, is say which runtime a process will actually load; none of them knows, and neither does this installer. So disagreement is now on the record. When the sources do not agree, resolution names every reading and which one won. That is the cheap part of the answer to the multi-version case: when a too-high pick does bite it surfaces as an HSA error at the first HIP call, or as a GPU silently missing from torch, and neither points back at wheel selection without the readings in the install log. Agreeing sources stay silent. The call site stops discarding stderr, which only ever carried this line: each source's own noise is suppressed inside the resolver. Tests: the shell suite gains the config-files 7.0 entry on a 6.1 host, its installed twin at the same versions so the assertion cannot pass on the version alone, the other non-installed dpkg states, and a stale-high reading in each of the five source positions. Its dpkg-query mock now renders the showformat string it is handed, so the tests cover how install.sh asks for the version and not only how it parses the answer; test_rocm_support.py gains the same mock and the config-files/installed pair. Both new cases fail against the previous commit and pass here.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 23efc93041
ℹ️ 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".
| ver="$(rpm -q --qf '%{VERSION}\n' rocm-core 2>/dev/null)" && \ | ||
| [ -n "$ver" ] && \ | ||
| printf '%s\n' "$ver" | awk -F'[.-]' '{print "rocm"$1"."$2; exit}'; }) 2>/dev/null || _rocm_tag="" | ||
| _rocm_tag=$(_detect_rocm_version_tag) || _rocm_tag="" |
There was a problem hiding this comment.
Feed the resolved version into Radeon wheel selection
On the split-package Radeon host this change targets—no usable amd-smi or version file, hipconfig reporting 5.7, and installed rocm-core reporting 6.1—this resolver correctly chooses rocm6.1, but a fresh install subsequently enters the Radeon branch at lines 4350-4352 and calls get_radeon_wheel_url(), whose independent first-answer chain at lines 3442-3451 still chooses hipconfig's 5.7. If that Radeon listing contains a complete wheel trio, lines 4501-4515 install those explicit 5.7 wheels instead of using TORCH_INDEX_URL, and the override file then preserves them during the Unsloth install. Reuse the resolved version when constructing the Radeon URL so the corrected decision actually controls the installed PyTorch build.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Checked this against the live repo rather than the code alone, and the stale URL cannot reach a wrong install. get_radeon_wheel_url can only emit a version its own three sources agree on, and the two outcomes are a 404 or a listing without a complete wheel set. rocm-rel-5.7.31921/ 404s, and the sed shortener only rewrites two-component tags so the retry is skipped. rocm-rel-5.7/ does exist (200), but it ships torch and torchvision only, no torchaudio, and the caller requires all three plus a version match. Both branches land on _install_torch_default_index, which uses the TORCH_INDEX_URL the new resolver produced. So the degradation is a 404 the caller already handles, and the fallback target is the corrected value. Two answers to the same question in one file is a consistency argument rather than a defect, and the function is untouched by this diff.
| _rt_ver=$(dpkg-query -W -f='${Status} ${Version}\n' rocm-core 2>/dev/null \ | ||
| | awk '$3 == "installed" && $4 != "" { print $4; exit }') || return 0 |
There was a problem hiding this comment.
Reject dpkg entries marked for reinstallation
When rocm-core is in dpkg's broken reinstreq state, ${Status} is rendered as install reinstreq installed, so testing only $3 == "installed" accepts its version even though dpkg describes such packages in dpkg --force-help as packages that “require installation.” After a failed upgrade or downgrade, that database version can be higher than every usable ROCm source and highest-wins will select incompatible wheels from it, defeating the dead-entry protection added here; require the error-status field ($2) to be ok as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The field ordering is right: ${Status} renders as want, eflag, status, so $3 is the status word and a hypothetical 'install reinstreq installed' would pass the filter. The state is unreachable, though. In dpkg's src/main/configure.c, deferred_configure refuses a package that is already PKG_STAT_INSTALLED, accepts only PKG_STAT_UNPACKED or PKG_STAT_HALFCONFIGURED as input, and bails on PKG_EFLAG_REINSTREQ with 'package is in a very bad inconsistent state'. On the path that does reach installed it calls pkg_reset_eflags before post_postinst_tasks(pkg, PKG_STAT_INSTALLED), so the flag is cleared on the way in. reinstreq pairs with half-installed, half-configured or unpacked in practice, all of which $3 == "installed" already excludes. It is also a different shape from the config-files case: there the files are gone and the version describes nothing, whereas a reinstreq package is unpacked at exactly the version it reports, so even a forced instance would not be a lying reading.
amd-smi prints one pipe-delimited line, so the ROCm field is followed on the same line by the amdgpu driver version. Stripping every non-digit from the field glued the two together, and when amd-smi cannot detect a ROCm userspace it prints "ROCm version: N/A", which left only the driver version behind: ... | ROCm version: N/A | amdgpu version: 6.10.10 | ... -> rocm6.10 While detection took the first source that answered this was mostly hidden, since a later real reading could not be reached anyway. Under highest-wins a fabricated reading competes, and this one wins: on a host with a genuine ROCm 6.1 the N/A line resolved rocm6.4 instead of rocm6.1. The field is now cut at the separator and required to start with digits, so an N/A or empty field contributes nothing instead of a wrong number.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5e2a83ae81
ℹ️ 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".
| _rt_readings=$({ | ||
| _rocm_tag_from_amd_smi | ||
| _rocm_tag_from_version_file | ||
| _rocm_tag_from_hipconfig |
There was a problem hiding this comment.
Bound the newly unconditional ROCm probes
When an AMD host already has a valid reading from amd-smi or /opt/rocm/.info/version but a lower-priority hipconfig, dpkg-query, or rpm probe hangs, this new all-source sequence blocks the installer indefinitely; the previous first-answer chain never invoked those later probes after a successful reading. Run each external probe through the existing _run_bounded helper so a damaged secondary ROCm installation or package database is treated like an unavailable source rather than preventing installation.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct for one of the five, and bounded in 9c1af36. Worked through each source on reachability rather than on whether it could theoretically be slow.
| source | newly unconditional | can block | verdict |
|---|---|---|---|
| amd-smi version | no, it was already first in the chain | yes, the documented /dev/shm mutex deadlock after --gpureset on ROCm 6.1 and earlier | pre-existing, not this change |
| /opt/rocm/.info/version | yes | no, a plain file read with no subprocess | fine |
| hipconfig --version | only on ROCm 5.x hosts | no | fine |
| dpkg-query -W | yes | no | fine |
| rpm -q | yes | yes, unbounded | bounded now |
hipconfig is already invoked unconditionally further down the file, and was before this change, so every host that resolves to ROCm ran it either way. It is also safe by construction: --version reads $HIP_PATH/share/hip/version and the binary is not linked against libamdhip64 or HSA. The stall people hit belongs to the rocm_agent_enumerator path used by bare hipconfig and --full.
dpkg-query opens the database msdbrw_readonly, which is an explicit no-lock branch in dbmodify.c, and writers use F_SETLK rather than F_SETLKW. strace confirms it: no lock file opened and no F_SETLK at all. There is an open wishlist bug asking dpkg to add waiting, which is the opposite of the concern.
rpm is the real one. On the BerkeleyDB backend, which is rpm below 4.16 and therefore RHEL 8 and SLES 15, both supported ROCm platforms, a leftover /var/lib/rpm/__db.00* from any killed rpm or yum leaves plain queries wedged in futex until the files are deleted (rhbz#485780, rhbz#73097), and rpm 6.0.x reintroduced a read lock that deadlocks rpm --query against a running dnf transaction (rhbz#2463435). Previously rpm was last in a first-answer-wins chain, so on any normal ROCm install the version file answered at position two and rpm was never reached.
Ordering is not a mitigation, since highest-wins needs every answer and short-circuiting is unavailable; reordering only changes which probe hangs first. The fix routes rpm through _run_bounded, the file's existing convention already used for nvidia-smi at three sites, which no-ops where timeout is absent so no new dependency is introduced. Both documented rpm hangs are SIGTERM-interruptible, so a plain bound is enough. A timed-out probe is simply a source that declined to answer.
Tests: a wedged-rpm mock plus a bound assertion, 58 passed. Reverting the one-line change fails both. Whole tests/sh 45/45, test_rocm_support 486 passed, bash -n and dash -n clean, shellcheck multiset identical to main.
The test builds a shell script out of get_torch_index_url plus the two gfx helpers, but the version chain now lives in its own helpers, so the call to _detect_rocm_version_tag hit a command that does not exist. The guarded assignment swallowed the 127 and the no-version endpoint was reached for the wrong reason, which made the test pass whatever the resolver did. Mutation confirms it: returning a real tag from _detect_rocm_version_tag left the test green before this change and fails it after. The /opt/rocm/.info/version rewrite moves onto the helper text for the same reason. The read moved out of get_torch_index_url, so rewriting the function body alone had quietly become a no-op.
Resolving the ROCm version from every source instead of the first that answers moved `rpm -q rocm-core` from last in a first-answer-wins chain to a probe that runs on every AMD host. On RHEL/SLES with a normal ROCm install /opt/rocm/.info/version answered at position two, so rpm was never invoked; now it always is. rpm -q is not a lock-free read. On the BerkeleyDB backend (rpm < 4.16, so RHEL 8 and SLES 15, both supported ROCm platforms) a leftover /var/lib/rpm/__db.00* from any killed rpm or yum leaves plain queries stuck in futex until the files are removed, and rpm 6.0.x reintroduced a read lock that deadlocks rpm --query against a running dnf transaction. Route it through the existing _run_bounded helper so a wedged rpmdb makes the source decline to answer instead of hanging the installer. _run_bounded no-ops where timeout is absent, so this adds no new dependency. The other four sources are left alone: amd-smi ran first in the old chain so it is not newly reachable, the version file is a plain read, hipconfig --version reads a version file and is not linked against HIP or HSA, and dpkg-query takes no dpkg lock at all.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Fixes #8402
The report
A Linux Mint 22.2 host with an RX 7900 XTX (gfx1100) and a working ROCm install got
torch 2.10.0+cpu. The install log carried the evidence against its own conclusion:install.shtook the ROCm version from the first source that answered, in the orderamd-smi,/opt/rocm/.info/version,hipconfig,dpkg rocm-core,rpm rocm-core. Debian 13, and Linux Mint on top of it, packageshipconfigat5.7.31921-0next to a 6.1.xrocminfo/HSA runtime.hipconfiganswers third,dpkgfourth, so detection settled onrocm5.7, the 6.0+ gate fired, and a card that ROCm supports fine trained on CPU.The change
Detection reads every source and takes the highest, rather than the first that answers. On a single host a source reading lower than another is stale packaging, not a downgrade, so the highest reading is the one that describes the runtime the GPU uses.
The inline
||chain becomes one helper per source plus a highest-wins resolver. Each helper returns 0 unconditionally, so the case it has to survive - a real AMD GPU with no ROCm userspace at all, wheredpkg-query/rpmexit 1 - still reaches the actionable "no ROCm/HIP install was found" warning instead of dying underset -e.The sub-6.0 warning also names a way forward: it says the version reported was the highest any source gave, and names
UNSLOTH_TORCH_INDEX_FAMILYandUNSLOTH_TORCH_INDEX_URL. Both return fromget_torch_index_urlbefore any GPU probing and are forwarded through the WSL reroute, so they are a real override for a host with split packaging.The obvious objection: does highest-wins overshoot?
Swapping first-answer-wins for highest-wins fixes the undershoot and raises the symmetric risk: a source reading higher than the host now picks the wheel family instead of being shadowed by its position. Three things bound it.
1. A dead package can no longer vote. This is the one real hole and the second commit closes it.
dpkg-query -Wlists every package in the status database except purged ones (man dpkg-query,--list: packages are listed "regardless of their status", excluding onlynot-installed). Arocm-coretaken out withapt removeand never purged sits in statedeinstall ok config-filesand still reports the version it had. A host that ran ROCm 7.0, went back to 6.1 and never purged therefore offered a 7.0 reading off a package that is gone. Fourth in the old chain it was usually shadowed; under highest-wins it beat every live source.Detection now asks for
${Status}alongside${Version}and uses the version only when the status word isinstalled.${Status}is a documented showformat field with no dpkg version floor, anddpkg-queryrenders an unrecognised field as empty rather than failing, so on a dpkg that somehow lacked it this source would go silent rather than over-report.2. The other four sources have no equivalent state. Each was checked for the same class of problem:
rpm -q rocm-corerpm -edrops the header record, andrpm -qthen exits 1, which the helper already treats as no answer./opt/rocm/.info/version/opt/rocmresolves to.amd-smi versionlibrocm-core.so, so it reports a ROCm that is installed, not the driver.hipconfig --versionWhat the last three cannot do, on a host carrying several ROCm trees, is say which runtime a process will actually load. None of them knows, and neither does this installer.
3. Even a genuinely high reading is mostly harmless. PyTorch's ROCm wheels vendor their own ROCm userspace into
torch/lib, so what they need from the host is the amdgpu/KFD driver, and AMD documents driver/userspace compatibility as +/- 2 releases, widening to about a year's span from 6.4 on. On top of that the existing normalisation can only emit an index leaf PyTorch publishes (6.5+ clips torocm6.4, 7.3+ caps torocm7.2), so no reading can produce a 404 index.The residue is a multi-version host where the pick is too new for the driver. That does not fail loudly: it surfaces as an HSA error at the first HIP call, or as a GPU silently missing from torch, neither of which points back at wheel selection. So when the sources disagree, resolution now names every reading and which one won:
Agreeing sources stay silent. The call site stops discarding stderr, which only ever carried this line; each source's own noise is suppressed inside the resolver.
Tests
tests/sh/test_rocm_version_source_disagreement.sh(51 assertions):config-filesrocm-coreat 7.0 on a 6.1 host, plus itsinstalledtwin at the same versions, so the assertion cannot pass on the version alonehalf-installed,unpacked,half-configured)set -eThe dpkg-query mock renders the showformat string it is handed rather than printing a bare version, so the tests cover how
install.shasks for the version and not only how it parses the answer.tests/studio/install/test_rocm_support.pygains the same mock and the config-files/installed pair.Both new dpkg cases were confirmed to fail against the first commit and pass after the second, so they are not vacuous.
Full run on this branch:
tests/sh45/45 scripts pass,test_get_torch_index_url.sh64 assertions,test_rocm_support.py486 passed 1 skipped,test_ci_shell_suite_coverage.py+test_cross_platform_parity.py105 passed.bash -n,dash -nandshellcheck -e SC1090,SC2034all clean, with the shellcheck finding multiset unchanged frommain.Not in this change
get_radeon_wheel_urland the Radeon repo path are untouched.studio/are untouched; this isinstall.sh's detection only./sys/module/amdgpu/versionand the driver compatibility matrix rather than any of these five userspace sources, which is a larger change than this bug needs.