Repository navigation
Conversation
|
|
|
Warning: Unknown label
Please add the new label to .github/pytorch-probot.yml |
… /dev/shm
Three environment/config fixes that make genuinely-failing tests pass on the
2.14 ROCm wheels. Each is a harness/CI-environment gap, not a test or PyTorch
bug; verified locally on MI300X (gfx942) via run_test.py. No skip-list changes
here — validate via a debug (skipped-tests-only) run.
1. TORCH_SERIALIZATION_DEBUG=1 in setup_env: upstream .ci/pytorch/test.sh exports
this for all CI; TheRock did not, so test_serialization
TestSerialization/TestOldSerialization::test_debug_set_in_ci failed (they assert
the flag is set under IS_CI). Fixes both.
2. Register TheRock's wheel-shipped ROCm lib dirs (_rocm_sdk_core/lib +
rocm_sysdeps/lib, _rocm_sdk_devel/lib) with ldconfig in setup_env. Those dirs
aren't on the default loader/link search path, so:
- test_multiprocessing test_fs*: torch_shm_manager subprocess exits 127
(librocprofiler-sdk.so.1 / libamdhip64.so.7 not found)
- test_utils TestStandaloneCPPJIT::test_load_standalone: ld undefined
reference to hipModuleLoadDataEx@hip_4.2 (libaotriton link)
LIMITATION: the proper fix bakes $ORIGIN-relative RPATH into the torch .so's at
wheel-build time (upstream pytorch/pytorch#188454 does exactly this via
patch_rocm_sysdeps_rpath + ldconfig). We can't patch an already-built wheel from
the test harness, so we register with ldconfig here as the test-side equivalent;
drop once #188454 / the RPATH bake lands. Best-effort (no-ops if non-root).
3. Bump the test container --shm-size 10g -> 64g: the 8-rank FSDP HSDP 3D-mesh
test (TestFullyShardHSDP3DTraining::test_3d_mlp_with_nd_mesh) exhausts a 10g
/dev/shm with RCCL shared-memory segments ("No space left on device"); passes at
64g. Matches upstream ROCm CI shm size.
Co-Authored-By: Claude <[email protected]>
…m_deps`)
Add `_preload_rocm_deps()` in torch/__init__.py, the ROCm analogue of
`_preload_cuda_deps`. At import time (before `import torch._C`), it locates the
`rocm` pip package's `_rocm_sdk_core` via importlib.util.find_spec and
RTLD_GLOBAL-preloads the vendored sysdeps (librocm_sysdeps_*) and the core ROCm
runtime libs from `_rocm_sdk_core/lib{,/rocm_sysdeps/lib}`, wired into
`_load_global_deps()`.
Because it lives in torch source it travels with any build, so a bare
`setup.py` / `python -m build` in a TheRock env yields an importable,
self-contained wheel without relying on LD_LIBRARY_PATH. Guarded to no-op on
non-Linux, on non-ROCm builds (torch.version.hip is None -> CUDA/CPU), and on
OS-managed ROCm where `_rocm_sdk_core` is absent (apt/opt-rocm path).
Made with Cursor
e112afd to
5e6d1e2
Compare
_preload_rocm_deps)
On TheRock ROCm wheels, `import torch` aborts with
`KeyError: 'libamd_smi.so'`: the amdsmi python package installs the native
library as a versioned soname under `_rocm_sdk_core/lib` (e.g.
`libamd_smi.so.26`), which amdsmi's own `find_smi_library()` fails to locate
when `$ROCM_HOME`/`$ROCM_PATH` and the loader path are unset. Its wrapper then
does `_libraries['libamd_smi.so']`, raising `KeyError`, which escapes the
existing guard (only `ModuleNotFoundError` was tolerated) and kills
`import torch`.
Two-part torch-side hardening (mirrors the `_preload_rocm_deps()` philosophy):
1. Make `_amdsmi_cdll_hook` TheRock-aware: locate `_rocm_sdk_core` via
`importlib.util.find_spec` and add its `lib/libamd_smi.so*` (glob, incl.
versioned sonames) as last-resort candidate paths; match names whose
basename starts with `libamd_smi.so` so amdsmi's bare
`CDLL("libamd_smi.so")` is redirected to the real versioned file. Existing
`ROCM_HOME`/`ROCM_PATH` behavior is preserved; guarded to Linux.
2. Make a non-discoverable amdsmi non-fatal: a half-installed amdsmi (python
package present, `.so` undiscoverable) now degrades to `_HAS_AMDSMI=False`
instead of aborting `import torch`, matching the existing missing-module
path.
Downstream follow-up to `_preload_rocm_deps()` (pytorch#188454) for the TheRock
wheels world.
Made with Cursor
|
|
f88ae7f to
68fd788
Compare
51d3aca to
a3e4b78
Compare
On TheRock ROCm wheels, `import torch` aborts with
`KeyError: 'libamd_smi.so'`: the amdsmi python package installs the native
library as a versioned soname under `_rocm_sdk_core/lib` (e.g.
`libamd_smi.so.26`), which amdsmi's own `find_smi_library()` fails to locate
when `$ROCM_HOME`/`$ROCM_PATH` and the loader path are unset. Its wrapper then
does `_libraries['libamd_smi.so']`, raising `KeyError`, which escapes the
existing guard (only `ModuleNotFoundError` was tolerated) and kills
`import torch`.
Two-part torch-side hardening (mirrors the `_preload_rocm_deps()` philosophy):
1. Make `_amdsmi_cdll_hook` TheRock-aware: locate `_rocm_sdk_core` via
`importlib.util.find_spec` and add its `lib/libamd_smi.so*` (glob, incl.
versioned sonames) as last-resort candidate paths; match names whose
basename starts with `libamd_smi.so` so amdsmi's bare
`CDLL("libamd_smi.so")` is redirected to the real versioned file. Existing
`ROCM_HOME`/`ROCM_PATH` behavior is preserved; guarded to Linux.
2. Make a non-discoverable amdsmi non-fatal: a half-installed amdsmi (python
package present, `.so` undiscoverable) now degrades to `_HAS_AMDSMI=False`
instead of aborting `import torch`, matching the existing missing-module
path.
Downstream follow-up to `_preload_rocm_deps()` (pytorch#188454) for the TheRock
wheels world.
Made with Cursor
On TheRock ROCm wheels, `import torch` aborts with
`KeyError: 'libamd_smi.so'`: the amdsmi python package installs the native
library as a versioned soname under `_rocm_sdk_core/lib` (e.g.
`libamd_smi.so.26`), which amdsmi's own `find_smi_library()` fails to locate
when `$ROCM_HOME`/`$ROCM_PATH` and the loader path are unset. Its wrapper then
does `_libraries['libamd_smi.so']`, raising `KeyError`, which escapes the
existing guard (only `ModuleNotFoundError` was tolerated) and kills
`import torch`.
Two-part torch-side hardening (mirrors the `_preload_rocm_deps()` philosophy):
1. Make `_amdsmi_cdll_hook` TheRock-aware: locate `_rocm_sdk_core` via
`importlib.util.find_spec` and add its `lib/libamd_smi.so*` (glob, incl.
versioned sonames) as last-resort candidate paths; match names whose
basename starts with `libamd_smi.so` so amdsmi's bare
`CDLL("libamd_smi.so")` is redirected to the real versioned file. Existing
`ROCM_HOME`/`ROCM_PATH` behavior is preserved; guarded to Linux.
2. Make a non-discoverable amdsmi non-fatal: a half-installed amdsmi (python
package present, `.so` undiscoverable) now degrades to `_HAS_AMDSMI=False`
instead of aborting `import torch`, matching the existing missing-module
path.
Downstream follow-up to `_preload_rocm_deps()` (pytorch#188454) for the TheRock
wheels world.
Made with Cursor
|
@pytorchbot merge -f "Lint clean. PR is only updating ROCm-specific logic in the files. rocm-nightly failures are understood as being unrelated" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
@amdfaa, are you going to backport this to 2.12 and 2.13? |
ScottTodd
left a comment
There was a problem hiding this comment.
This PR is misguided, please revert then file issues in TheRock for rocm python package bugs and involve myself and/or @marbre in such discussions.
Documentation for how to work with the ROCm python packages is here: https://github.com/ROCm/TheRock/blob/main/docs/packaging/python_packaging.md#using-packages-from-frameworks, and this PR does the opposite.
The description here has a lot of text but is missing a description of what the actual problems are that this is trying to solve. It jumps to root cause analysis that appears wrong.
| except (KeyError, OSError) as err: | ||
| # The amdsmi python package is installed but its native library | ||
| # (libamd_smi.so) could not be discovered/loaded -- e.g. TheRock | ||
| # ROCm wheels lay amdsmi out so that its own find_smi_library() | ||
| # misses the versioned libamd_smi.so.* and raises KeyError. Treat | ||
| # this like a missing optional dependency (degrade to | ||
| # _HAS_AMDSMI=False) instead of aborting `import torch`. | ||
| _AMDSMI_ERR = err | ||
| raise ModuleNotFoundError( | ||
| "amdsmi is installed but libamd_smi.so could not be loaded" | ||
| ) from err |
There was a problem hiding this comment.
"TheRock ROCm wheels lay amdsmi out so that its own find_smi_library() misses the versioned libamd_smi.so.* and raises KeyError." is exactly the kind of issue that needs to be resolved in the ROCm packages themselves by TheRock and/or amdsmi, not worked around by pytorch. Please work with us directly on such issues instead of add this sort of code to individual frameworks.
| # ROCm runtime libs a TheRock-built libtorch DT_NEEDEDs, mirroring the set | ||
| # bundled by .ci/manywheel/repair_wheel.py::ROCM_SO_FILES. Ordered leaf-first so | ||
| # that RTLD_GLOBAL preloading satisfies inter-lib NEEDED entries as we go. | ||
| _rocm_core_libs: list[str] = [ | ||
| "libamd_comgr.so", | ||
| "libhsa-runtime64.so", | ||
| "libamdhip64.so", | ||
| "libhiprtc.so", | ||
| "librocm-core.so", | ||
| "librocm_smi64.so", | ||
| "libroctx64.so", | ||
| "libroctracer64.so", | ||
| "librocblas.so", | ||
| "libhipblas.so", | ||
| "libhipblaslt.so", | ||
| "librocfft.so", | ||
| "libhipfft.so", | ||
| "librocrand.so", | ||
| "libhiprand.so", | ||
| "librocsolver.so", | ||
| "libhipsolver.so", | ||
| "librocsparse.so", | ||
| "libhipsparse.so", | ||
| "libhipsparselt.so", | ||
| "libMIOpen.so", | ||
| "librccl.so", | ||
| ] |
There was a problem hiding this comment.
This list of rocm libraries is dependent on the rocm version (which can change from commit to commit as libraries are added, removed, or merged). Because of this, the list belongs in rocm itself, and we already built the dedicated _rocm_init.initialize() code path for this, see the documentation at https://github.com/ROCm/TheRock/blob/main/docs/packaging/python_packaging.md#performing-initialization.
What you could do would be to generate the _rocm_init.py file as part of some build script in torch, modeled after what we do in TheRock's build scripts: https://github.com/ROCm/TheRock/blob/802f1c98a315a25215e641aa1e5ea42b057a3561/external-builds/pytorch/build_prod_wheels.py#L356-L371, as I had prototyped back on #160776 with tools/amd_build/write_rocm_init.py. Take special note of the comments in that file:
The expectation is that the https://github.com/pytorch/pytorch repo will build
"PyTorch HEAD on ROCm stable", while https://github.com/ROCm/TheRock downstream
will build "PyTorch HEAD on ROCm HEAD". However, while the ROCm Python packages
continue to mature, the "stable" version pinned in this file will be
periodically updated. Eventually a pattern like `7.1.*` may be preffered here,
allowing for changes in patch versions but warning about changes to major or
minor versions.
Available versions can be seen on the index pages at
https://github.com/ROCm/TheRock/blob/main/RELEASES.md#installing-releases-using-pip.
Currently (August 2025), only 7.0.0rcYYYYMMDD versions are available, but in the
future there may be non-rc versions, possibly pushed to PyPI.
The lists of library preloads available are likely to change from ROCm version
to ROCm version, while the lists of preloads _used directly by PyTorch_ should
be dependent on code in PyTorch itself. Indirect dependencies like a ROCm
library taking a dependency on another ROCm library, are possible too and would
require changes here during a version bump, even with no other PyTorch changes.
There was a problem hiding this comment.
Additionally, these libraries exist on Windows as .dll files, so there shouldn't be just a Linux-specific list and only Linux-specific preload/init code.
| def _preload_rocm_deps() -> None: | ||
| """Preload TheRock ROCm runtime libs, the ROCm analogue of _preload_cuda_deps. | ||
|
|
||
| TheRock ships the ROCm runtime as the ``rocm`` pip package, unpacked to | ||
| ``<site-packages>/_rocm_sdk_core`` (a sibling of ``torch/``; the same layout | ||
| torch.utils.cpp_extension._find_rocm_home() keys off). Its runtime libs live | ||
| in ``_rocm_sdk_core/lib`` and the vendored OS-side "sysdeps" (libdrm, liblzma, | ||
| ... renamed to ``librocm_sysdeps_*.so``) live in | ||
| ``_rocm_sdk_core/lib/rocm_sysdeps/lib``. Neither dir is on the default loader | ||
| search path, so a wheel built against TheRock -- whose torch .so files carry | ||
| NEEDED entries such as ``librocm_sysdeps_liblzma.so.5`` and | ||
| ``libamdhip64.so`` -- is only importable when those dirs are reachable | ||
| (e.g. via LD_LIBRARY_PATH from /etc/rocm_env.sh). Preloading the libs here | ||
| (RTLD_GLOBAL, before ``import torch._C``) makes the wheel self-resolving | ||
| regardless of the environment, the same way _preload_cuda_deps handles the | ||
| nvidia-*-cu12 wheels. Because this lives in torch source it travels with any | ||
| build (bare ``setup.py`` / ``python -m build``), not just the CI script. |
There was a problem hiding this comment.
Same as above, rocm sysdep loading generally belongs in the rocm packages, not torch. See https://github.com/ROCm/TheRock/blob/main/docs/packaging/python_packaging.md#performing-initialization
… /dev/shm
Three environment/config fixes that make genuinely-failing tests pass on the
2.14 ROCm wheels. Each is a harness/CI-environment gap, not a test or PyTorch
bug; verified locally on MI300X (gfx942) via run_test.py. No skip-list changes
here — validate via a debug (skipped-tests-only) run.
1. TORCH_SERIALIZATION_DEBUG=1 in setup_env: upstream .ci/pytorch/test.sh exports
this for all CI; TheRock did not, so test_serialization
TestSerialization/TestOldSerialization::test_debug_set_in_ci failed (they assert
the flag is set under IS_CI). Fixes both.
2. Register TheRock's wheel-shipped ROCm lib dirs (_rocm_sdk_core/lib +
rocm_sysdeps/lib, _rocm_sdk_devel/lib) with ldconfig in setup_env. Those dirs
aren't on the default loader/link search path, so:
- test_multiprocessing test_fs*: torch_shm_manager subprocess exits 127
(librocprofiler-sdk.so.1 / libamdhip64.so.7 not found)
- test_utils TestStandaloneCPPJIT::test_load_standalone: ld undefined
reference to hipModuleLoadDataEx@hip_4.2 (libaotriton link)
LIMITATION: the proper fix bakes $ORIGIN-relative RPATH into the torch .so's at
wheel-build time (upstream pytorch/pytorch#188454 does exactly this via
patch_rocm_sysdeps_rpath + ldconfig). We can't patch an already-built wheel from
the test harness, so we register with ldconfig here as the test-side equivalent;
drop once #188454 / the RPATH bake lands. Best-effort (no-ops if non-root).
3. Bump the test container --shm-size 10g -> 64g: the 8-rank FSDP HSDP 3D-mesh
test (TestFullyShardHSDP3DTraining::test_3d_mlp_with_nd_mesh) exhausts a 10g
/dev/shm with RCCL shared-memory segments ("No space left on device"); passes at
64g. Matches upstream ROCm CI shm size.
Co-Authored-By: Claude <[email protected]>
…oad_rocm_deps`) (pytorch#188454) ## Summary This PR makes PyTorch ROCm/TheRock wheels **self-contained**: it fixes the two `import torch` failures that occur on the wheel (`_rocm_sdk_*` site-packages) layout, so `import torch` works without `LD_LIBRARY_PATH` or a sourced ROCm env (e.g. the ROCm distributed preflight `python .ci/pytorch/rocm_preflight.py`, which never sources `/etc/rocm_env.sh`). Both fixes live in torch **source**, so they travel with any build (including a bare `python -m build` in a TheRock env), not just CI-produced wheels. This is the **minimal, consolidated PR** carrying **both** import-discovery fixes — a **two-file** diff (`torch/__init__.py` + `torch/cuda/__init__.py`). Both changes are **no-ops on the traditional tarball `/opt/rocm` install**: each early-returns when the wheel package's `_rocm_sdk_core` is absent, so non-wheel ROCm CI (apt/`/opt/rocm`, whose libs are already on the loader path), CUDA, and CPU builds are unaffected. ## Fixes ### 1. `torch/__init__.py` — `_preload_rocm_deps()` Fixes `ImportError: librocm_sysdeps_liblzma.so.5: cannot open shared object file` at `from torch._C import *` - Adds `_preload_rocm_deps()`, the ROCm analogue of `_preload_cuda_deps()` (which handles the `nvidia-*-cu12` wheels). At import time, **before** `import torch._C`, it locates the `rocm` pip package's `_rocm_sdk_core` via `importlib.util.find_spec` (the same anchor `torch.utils.cpp_extension._find_rocm_home()` uses) and `ctypes.CDLL`-preloads (`RTLD_GLOBAL`) the vendored sysdeps (`librocm_sysdeps_*`) and core ROCm runtime libs from `_rocm_sdk_core/lib` and `_rocm_sdk_core/lib/rocm_sysdeps/lib`. Wired into `_load_global_deps()`. - Guarded to no-op on non-Linux, on non-ROCm builds (`torch.version.hip is None` → CUDA/CPU), and on OS-managed ROCm where `_rocm_sdk_core` is absent (the apt/`/opt/rocm` path). ### 2. `torch/cuda/__init__.py` — amdsmi / `libamd_smi.so` discovery hook + non-fatal guard Fixes the *next* failure that surfaces once `_preload_rocm_deps()` gets `import torch` past the `librocm_sysdeps` error: `KeyError: 'libamd_smi.so'` raised from the `amdsmi` Python binding at `import amdsmi` (inside `_C._initExtension`) On TheRock wheels the amdsmi package installs the native library as a versioned soname under `_rocm_sdk_core/lib` (e.g. `libamd_smi.so.26`), which amdsmi's own `find_smi_library()` fails to locate when `$ROCM_HOME`/`$ROCM_PATH` and the loader path are unset; its wrapper then does `_libraries['libamd_smi.so']` → `KeyError`, which escaped the existing guard (only `ModuleNotFoundError` was tolerated) and killed `import torch`. Two-part torch-side hardening: - Make `_amdsmi_cdll_hook` **TheRock-aware**: locate `_rocm_sdk_core` via `importlib.util.find_spec` and add its `lib/libamd_smi.so*` (glob, incl. versioned sonames) as last-resort candidate paths; match names whose basename **starts with** `libamd_smi.so` so amdsmi's bare `CDLL("libamd_smi.so")` is redirected to the real versioned file. Existing `ROCM_HOME`/`ROCM_PATH` behavior preserved; guarded to Linux. - Make a non-discoverable amdsmi **non-fatal**: broaden the guard so `KeyError`/`OSError` degrade to `_HAS_AMDSMI=False` instead of aborting `import torch`, matching the existing missing-module path. ## Consolidation note The amdsmi discovery fix (#2 above) originally shipped as standalone PR pytorch#188724. That PR has been **closed** and folded into this PR so **both** import-discovery fixes travel together in a single minimal two-file change. Scope note: the TheRock tarball→wheels migration and the earlier CI-only RPATH/ldconfig workarounds are **not** part of this PR. The wheels migration is tracked separately by pytorch#188429, which rebases on top of this change. ## Validation (TheRock wheel path) — PASSED Validated end-to-end by **temporarily** stacking Ethan's wheels-migration PR pytorch#188429 (which adds the `rocm-preview` wheel CI) on top of the two source fixes and running `ciflow/rocm-preview` (run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341)) on the stacked head. The temporary stack was then **removed**, so this PR remains the minimal two-file source diff. - **`build-osdc` (TheRock wheel-path build): SUCCESS** — [job 84742887851](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84742887851). - **`import torch` works on the `_rocm_sdk_*` wheel layout** — proven by the passing shard [`test (default, 5/6)` job 84772673709](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673709), which imports torch and prints the HIP 7.14 / MIOpen 3.5.2 build config. - **No** `ImportError: librocm_sysdeps*` (incl. `librocm_sysdeps_liblzma`) and **no** `KeyError: 'libamd_smi.so'` anywhere in the run → **both** fixes are validated on the **real TheRock wheel path**. - Remaining rocm-preview shard failures were **unrelated** to this PR — test-content failures (`test_CTCLoss_cudnn_cuda`, `test_cxx_flags` `RuntimeError map::at`, `test_hip_device_count`, `test_lazy_imports_are_lazy`) and infra (`ROCm pre-flight: no GPUs visible to the container` on the distributed shards). - Run: https://github.com/pytorch/pytorch/actions/runs/28581558341 ### rocm-preview distributed shards (register fat binary failed) Reference/evidence for the `register fat binary failed` (`code_object.cpp:1038`) investigation — the rocm-preview (TheRock wheel-path) distributed shards from run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341) (`linux-noble-rocm-preview-py3.12-gfx942`), all FAILED: - distributed 1/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673683 (`test (distributed, 1, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 2/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673678 (`test (distributed, 2, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 3/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673727 (`test (distributed, 3, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) <details> <summary>Failure log the amdsmi fix addresses (pre-fix, run 28524549808)</summary> ``` /var/lib/jenkins/workspace/libamd_smi.so: cannot open shared object file: No such file or directory Unable to find libamd_smi.so library try installing amd-smi-lib from your package manager Traceback (most recent call last): File "/var/lib/jenkins/workspace/.ci/pytorch/rocm_preflight.py", line 21, in <module> import torch File ".../site-packages/torch/cuda/__init__.py", line 114, in <module> import amdsmi # type: ignore[import] File ".../site-packages/amdsmi/amdsmi_wrapper.py", line 218, in <module> amdsmi_free_name_value_pairs = _libraries['libamd_smi.so'].amdsmi_free_name_value_pairs KeyError: 'libamd_smi.so' ``` </details> ## Notes: amdsmi discovery + fat-binary logs - **Bare `CDLL("libamd_smi.so")`** comes from amdsmi's own `find_smi_library()` (unversioned candidate), not torch; torch's `_amdsmi_cdll_hook` intercepts it and retries the versioned `_rocm_sdk_core/lib/libamd_smi.so*`. - **Not just preload amdsmi via `_rocm_core_libs`:** amdsmi resolves by exact filename, so an RTLD_GLOBAL versioned `libamd_smi.so.26` won't satisfy it, and eager-preloading a non-`DT_NEEDED` lib reintroduces the ODR double-load the hook prevents — the CDLL-redirect hook is the correct fix. - **`E0702 … code_object.cpp:1038 register fat binary failed` is not caused by this PR:** it's emitted by the HIP/clr C++ runtime, appears ambiently (~4,774×/run across rocm-preview gfx942 jobs incl. the distributed shards above), and is non-fatal — a pre-existing TheRock rocm-preview build/toolchain matter, orthogonal to these two Python-only changes. - **Fix-plan pointer (fat-binary):** prove pre-existing on a PR-free `main` rocm-preview baseline → localize via `AMD_LOG_LEVEL=3/4` → scoped clr-log suppress/demote (escalate to clr/comgr pin or `PYTORCH_ROCM_ARCH` only on a real running-arch failure) → validate via `ciflow/rocm-preview`. Belongs in the build/CI layer, not torch source. Tracked in ROCm/frameworks-internal pytorch#17160. ## Test plan - [x] Validate on the TheRock wheel path via `ciflow/rocm-preview` (see **Validation** above). - [x] `ast.parse` both changed files (`torch/__init__.py`, `torch/cuda/__init__.py`). - [x] Confirm CUDA and CPU builds are unaffected (both fixes no-op when `torch.version.hip is None` / `_rocm_sdk_core` absent). Fixes pytorch#188561 Made with Cursor Pull Request resolved: pytorch#188454 Approved by: https://github.com/jeffdaily
…oad_rocm_deps`) (pytorch#188454) ## Summary This PR makes PyTorch ROCm/TheRock wheels **self-contained**: it fixes the two `import torch` failures that occur on the wheel (`_rocm_sdk_*` site-packages) layout, so `import torch` works without `LD_LIBRARY_PATH` or a sourced ROCm env (e.g. the ROCm distributed preflight `python .ci/pytorch/rocm_preflight.py`, which never sources `/etc/rocm_env.sh`). Both fixes live in torch **source**, so they travel with any build (including a bare `python -m build` in a TheRock env), not just CI-produced wheels. This is the **minimal, consolidated PR** carrying **both** import-discovery fixes — a **two-file** diff (`torch/__init__.py` + `torch/cuda/__init__.py`). Both changes are **no-ops on the traditional tarball `/opt/rocm` install**: each early-returns when the wheel package's `_rocm_sdk_core` is absent, so non-wheel ROCm CI (apt/`/opt/rocm`, whose libs are already on the loader path), CUDA, and CPU builds are unaffected. ## Fixes ### 1. `torch/__init__.py` — `_preload_rocm_deps()` Fixes `ImportError: librocm_sysdeps_liblzma.so.5: cannot open shared object file` at `from torch._C import *` - Adds `_preload_rocm_deps()`, the ROCm analogue of `_preload_cuda_deps()` (which handles the `nvidia-*-cu12` wheels). At import time, **before** `import torch._C`, it locates the `rocm` pip package's `_rocm_sdk_core` via `importlib.util.find_spec` (the same anchor `torch.utils.cpp_extension._find_rocm_home()` uses) and `ctypes.CDLL`-preloads (`RTLD_GLOBAL`) the vendored sysdeps (`librocm_sysdeps_*`) and core ROCm runtime libs from `_rocm_sdk_core/lib` and `_rocm_sdk_core/lib/rocm_sysdeps/lib`. Wired into `_load_global_deps()`. - Guarded to no-op on non-Linux, on non-ROCm builds (`torch.version.hip is None` → CUDA/CPU), and on OS-managed ROCm where `_rocm_sdk_core` is absent (the apt/`/opt/rocm` path). ### 2. `torch/cuda/__init__.py` — amdsmi / `libamd_smi.so` discovery hook + non-fatal guard Fixes the *next* failure that surfaces once `_preload_rocm_deps()` gets `import torch` past the `librocm_sysdeps` error: `KeyError: 'libamd_smi.so'` raised from the `amdsmi` Python binding at `import amdsmi` (inside `_C._initExtension`) On TheRock wheels the amdsmi package installs the native library as a versioned soname under `_rocm_sdk_core/lib` (e.g. `libamd_smi.so.26`), which amdsmi's own `find_smi_library()` fails to locate when `$ROCM_HOME`/`$ROCM_PATH` and the loader path are unset; its wrapper then does `_libraries['libamd_smi.so']` → `KeyError`, which escaped the existing guard (only `ModuleNotFoundError` was tolerated) and killed `import torch`. Two-part torch-side hardening: - Make `_amdsmi_cdll_hook` **TheRock-aware**: locate `_rocm_sdk_core` via `importlib.util.find_spec` and add its `lib/libamd_smi.so*` (glob, incl. versioned sonames) as last-resort candidate paths; match names whose basename **starts with** `libamd_smi.so` so amdsmi's bare `CDLL("libamd_smi.so")` is redirected to the real versioned file. Existing `ROCM_HOME`/`ROCM_PATH` behavior preserved; guarded to Linux. - Make a non-discoverable amdsmi **non-fatal**: broaden the guard so `KeyError`/`OSError` degrade to `_HAS_AMDSMI=False` instead of aborting `import torch`, matching the existing missing-module path. ## Consolidation note The amdsmi discovery fix (pytorch#2 above) originally shipped as standalone PR pytorch#188724. That PR has been **closed** and folded into this PR so **both** import-discovery fixes travel together in a single minimal two-file change. Scope note: the TheRock tarball→wheels migration and the earlier CI-only RPATH/ldconfig workarounds are **not** part of this PR. The wheels migration is tracked separately by pytorch#188429, which rebases on top of this change. ## Validation (TheRock wheel path) — PASSED Validated end-to-end by **temporarily** stacking Ethan's wheels-migration PR pytorch#188429 (which adds the `rocm-preview` wheel CI) on top of the two source fixes and running `ciflow/rocm-preview` (run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341)) on the stacked head. The temporary stack was then **removed**, so this PR remains the minimal two-file source diff. - **`build-osdc` (TheRock wheel-path build): SUCCESS** — [job 84742887851](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84742887851). - **`import torch` works on the `_rocm_sdk_*` wheel layout** — proven by the passing shard [`test (default, 5/6)` job 84772673709](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673709), which imports torch and prints the HIP 7.14 / MIOpen 3.5.2 build config. - **No** `ImportError: librocm_sysdeps*` (incl. `librocm_sysdeps_liblzma`) and **no** `KeyError: 'libamd_smi.so'` anywhere in the run → **both** fixes are validated on the **real TheRock wheel path**. - Remaining rocm-preview shard failures were **unrelated** to this PR — test-content failures (`test_CTCLoss_cudnn_cuda`, `test_cxx_flags` `RuntimeError map::at`, `test_hip_device_count`, `test_lazy_imports_are_lazy`) and infra (`ROCm pre-flight: no GPUs visible to the container` on the distributed shards). - Run: https://github.com/pytorch/pytorch/actions/runs/28581558341 ### rocm-preview distributed shards (register fat binary failed) Reference/evidence for the `register fat binary failed` (`code_object.cpp:1038`) investigation — the rocm-preview (TheRock wheel-path) distributed shards from run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341) (`linux-noble-rocm-preview-py3.12-gfx942`), all FAILED: - distributed 1/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673683 (`test (distributed, 1, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 2/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673678 (`test (distributed, 2, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 3/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673727 (`test (distributed, 3, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) <details> <summary>Failure log the amdsmi fix addresses (pre-fix, run 28524549808)</summary> ``` /var/lib/jenkins/workspace/libamd_smi.so: cannot open shared object file: No such file or directory Unable to find libamd_smi.so library try installing amd-smi-lib from your package manager Traceback (most recent call last): File "/var/lib/jenkins/workspace/.ci/pytorch/rocm_preflight.py", line 21, in <module> import torch File ".../site-packages/torch/cuda/__init__.py", line 114, in <module> import amdsmi # type: ignore[import] File ".../site-packages/amdsmi/amdsmi_wrapper.py", line 218, in <module> amdsmi_free_name_value_pairs = _libraries['libamd_smi.so'].amdsmi_free_name_value_pairs KeyError: 'libamd_smi.so' ``` </details> ## Notes: amdsmi discovery + fat-binary logs - **Bare `CDLL("libamd_smi.so")`** comes from amdsmi's own `find_smi_library()` (unversioned candidate), not torch; torch's `_amdsmi_cdll_hook` intercepts it and retries the versioned `_rocm_sdk_core/lib/libamd_smi.so*`. - **Not just preload amdsmi via `_rocm_core_libs`:** amdsmi resolves by exact filename, so an RTLD_GLOBAL versioned `libamd_smi.so.26` won't satisfy it, and eager-preloading a non-`DT_NEEDED` lib reintroduces the ODR double-load the hook prevents — the CDLL-redirect hook is the correct fix. - **`E0702 … code_object.cpp:1038 register fat binary failed` is not caused by this PR:** it's emitted by the HIP/clr C++ runtime, appears ambiently (~4,774×/run across rocm-preview gfx942 jobs incl. the distributed shards above), and is non-fatal — a pre-existing TheRock rocm-preview build/toolchain matter, orthogonal to these two Python-only changes. - **Fix-plan pointer (fat-binary):** prove pre-existing on a PR-free `main` rocm-preview baseline → localize via `AMD_LOG_LEVEL=3/4` → scoped clr-log suppress/demote (escalate to clr/comgr pin or `PYTORCH_ROCM_ARCH` only on a real running-arch failure) → validate via `ciflow/rocm-preview`. Belongs in the build/CI layer, not torch source. Tracked in ROCm/frameworks-internal pytorch#17160. ## Test plan - [x] Validate on the TheRock wheel path via `ciflow/rocm-preview` (see **Validation** above). - [x] `ast.parse` both changed files (`torch/__init__.py`, `torch/cuda/__init__.py`). - [x] Confirm CUDA and CPU builds are unaffected (both fixes no-op when `torch.version.hip is None` / `_rocm_sdk_core` absent). Fixes pytorch#188561 Made with Cursor Pull Request resolved: pytorch#188454 Approved by: https://github.com/jeffdaily
…oad_rocm_deps`) (pytorch#188454) ## Summary This PR makes PyTorch ROCm/TheRock wheels **self-contained**: it fixes the two `import torch` failures that occur on the wheel (`_rocm_sdk_*` site-packages) layout, so `import torch` works without `LD_LIBRARY_PATH` or a sourced ROCm env (e.g. the ROCm distributed preflight `python .ci/pytorch/rocm_preflight.py`, which never sources `/etc/rocm_env.sh`). Both fixes live in torch **source**, so they travel with any build (including a bare `python -m build` in a TheRock env), not just CI-produced wheels. This is the **minimal, consolidated PR** carrying **both** import-discovery fixes — a **two-file** diff (`torch/__init__.py` + `torch/cuda/__init__.py`). Both changes are **no-ops on the traditional tarball `/opt/rocm` install**: each early-returns when the wheel package's `_rocm_sdk_core` is absent, so non-wheel ROCm CI (apt/`/opt/rocm`, whose libs are already on the loader path), CUDA, and CPU builds are unaffected. ## Fixes ### 1. `torch/__init__.py` — `_preload_rocm_deps()` Fixes `ImportError: librocm_sysdeps_liblzma.so.5: cannot open shared object file` at `from torch._C import *` - Adds `_preload_rocm_deps()`, the ROCm analogue of `_preload_cuda_deps()` (which handles the `nvidia-*-cu12` wheels). At import time, **before** `import torch._C`, it locates the `rocm` pip package's `_rocm_sdk_core` via `importlib.util.find_spec` (the same anchor `torch.utils.cpp_extension._find_rocm_home()` uses) and `ctypes.CDLL`-preloads (`RTLD_GLOBAL`) the vendored sysdeps (`librocm_sysdeps_*`) and core ROCm runtime libs from `_rocm_sdk_core/lib` and `_rocm_sdk_core/lib/rocm_sysdeps/lib`. Wired into `_load_global_deps()`. - Guarded to no-op on non-Linux, on non-ROCm builds (`torch.version.hip is None` → CUDA/CPU), and on OS-managed ROCm where `_rocm_sdk_core` is absent (the apt/`/opt/rocm` path). ### 2. `torch/cuda/__init__.py` — amdsmi / `libamd_smi.so` discovery hook + non-fatal guard Fixes the *next* failure that surfaces once `_preload_rocm_deps()` gets `import torch` past the `librocm_sysdeps` error: `KeyError: 'libamd_smi.so'` raised from the `amdsmi` Python binding at `import amdsmi` (inside `_C._initExtension`) On TheRock wheels the amdsmi package installs the native library as a versioned soname under `_rocm_sdk_core/lib` (e.g. `libamd_smi.so.26`), which amdsmi's own `find_smi_library()` fails to locate when `$ROCM_HOME`/`$ROCM_PATH` and the loader path are unset; its wrapper then does `_libraries['libamd_smi.so']` → `KeyError`, which escaped the existing guard (only `ModuleNotFoundError` was tolerated) and killed `import torch`. Two-part torch-side hardening: - Make `_amdsmi_cdll_hook` **TheRock-aware**: locate `_rocm_sdk_core` via `importlib.util.find_spec` and add its `lib/libamd_smi.so*` (glob, incl. versioned sonames) as last-resort candidate paths; match names whose basename **starts with** `libamd_smi.so` so amdsmi's bare `CDLL("libamd_smi.so")` is redirected to the real versioned file. Existing `ROCM_HOME`/`ROCM_PATH` behavior preserved; guarded to Linux. - Make a non-discoverable amdsmi **non-fatal**: broaden the guard so `KeyError`/`OSError` degrade to `_HAS_AMDSMI=False` instead of aborting `import torch`, matching the existing missing-module path. ## Consolidation note The amdsmi discovery fix (pytorch#2 above) originally shipped as standalone PR pytorch#188724. That PR has been **closed** and folded into this PR so **both** import-discovery fixes travel together in a single minimal two-file change. Scope note: the TheRock tarball→wheels migration and the earlier CI-only RPATH/ldconfig workarounds are **not** part of this PR. The wheels migration is tracked separately by pytorch#188429, which rebases on top of this change. ## Validation (TheRock wheel path) — PASSED Validated end-to-end by **temporarily** stacking Ethan's wheels-migration PR pytorch#188429 (which adds the `rocm-preview` wheel CI) on top of the two source fixes and running `ciflow/rocm-preview` (run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341)) on the stacked head. The temporary stack was then **removed**, so this PR remains the minimal two-file source diff. - **`build-osdc` (TheRock wheel-path build): SUCCESS** — [job 84742887851](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84742887851). - **`import torch` works on the `_rocm_sdk_*` wheel layout** — proven by the passing shard [`test (default, 5/6)` job 84772673709](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673709), which imports torch and prints the HIP 7.14 / MIOpen 3.5.2 build config. - **No** `ImportError: librocm_sysdeps*` (incl. `librocm_sysdeps_liblzma`) and **no** `KeyError: 'libamd_smi.so'` anywhere in the run → **both** fixes are validated on the **real TheRock wheel path**. - Remaining rocm-preview shard failures were **unrelated** to this PR — test-content failures (`test_CTCLoss_cudnn_cuda`, `test_cxx_flags` `RuntimeError map::at`, `test_hip_device_count`, `test_lazy_imports_are_lazy`) and infra (`ROCm pre-flight: no GPUs visible to the container` on the distributed shards). - Run: https://github.com/pytorch/pytorch/actions/runs/28581558341 ### rocm-preview distributed shards (register fat binary failed) Reference/evidence for the `register fat binary failed` (`code_object.cpp:1038`) investigation — the rocm-preview (TheRock wheel-path) distributed shards from run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341) (`linux-noble-rocm-preview-py3.12-gfx942`), all FAILED: - distributed 1/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673683 (`test (distributed, 1, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 2/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673678 (`test (distributed, 2, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 3/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673727 (`test (distributed, 3, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) <details> <summary>Failure log the amdsmi fix addresses (pre-fix, run 28524549808)</summary> ``` /var/lib/jenkins/workspace/libamd_smi.so: cannot open shared object file: No such file or directory Unable to find libamd_smi.so library try installing amd-smi-lib from your package manager Traceback (most recent call last): File "/var/lib/jenkins/workspace/.ci/pytorch/rocm_preflight.py", line 21, in <module> import torch File ".../site-packages/torch/cuda/__init__.py", line 114, in <module> import amdsmi # type: ignore[import] File ".../site-packages/amdsmi/amdsmi_wrapper.py", line 218, in <module> amdsmi_free_name_value_pairs = _libraries['libamd_smi.so'].amdsmi_free_name_value_pairs KeyError: 'libamd_smi.so' ``` </details> ## Notes: amdsmi discovery + fat-binary logs - **Bare `CDLL("libamd_smi.so")`** comes from amdsmi's own `find_smi_library()` (unversioned candidate), not torch; torch's `_amdsmi_cdll_hook` intercepts it and retries the versioned `_rocm_sdk_core/lib/libamd_smi.so*`. - **Not just preload amdsmi via `_rocm_core_libs`:** amdsmi resolves by exact filename, so an RTLD_GLOBAL versioned `libamd_smi.so.26` won't satisfy it, and eager-preloading a non-`DT_NEEDED` lib reintroduces the ODR double-load the hook prevents — the CDLL-redirect hook is the correct fix. - **`E0702 … code_object.cpp:1038 register fat binary failed` is not caused by this PR:** it's emitted by the HIP/clr C++ runtime, appears ambiently (~4,774×/run across rocm-preview gfx942 jobs incl. the distributed shards above), and is non-fatal — a pre-existing TheRock rocm-preview build/toolchain matter, orthogonal to these two Python-only changes. - **Fix-plan pointer (fat-binary):** prove pre-existing on a PR-free `main` rocm-preview baseline → localize via `AMD_LOG_LEVEL=3/4` → scoped clr-log suppress/demote (escalate to clr/comgr pin or `PYTORCH_ROCM_ARCH` only on a real running-arch failure) → validate via `ciflow/rocm-preview`. Belongs in the build/CI layer, not torch source. Tracked in ROCm/frameworks-internal pytorch#17160. ## Test plan - [x] Validate on the TheRock wheel path via `ciflow/rocm-preview` (see **Validation** above). - [x] `ast.parse` both changed files (`torch/__init__.py`, `torch/cuda/__init__.py`). - [x] Confirm CUDA and CPU builds are unaffected (both fixes no-op when `torch.version.hip is None` / `_rocm_sdk_core` absent). Fixes pytorch#188561 Made with Cursor Pull Request resolved: pytorch#188454 Approved by: https://github.com/jeffdaily
…oad_rocm_deps`) (pytorch#188454) ## Summary This PR makes PyTorch ROCm/TheRock wheels **self-contained**: it fixes the two `import torch` failures that occur on the wheel (`_rocm_sdk_*` site-packages) layout, so `import torch` works without `LD_LIBRARY_PATH` or a sourced ROCm env (e.g. the ROCm distributed preflight `python .ci/pytorch/rocm_preflight.py`, which never sources `/etc/rocm_env.sh`). Both fixes live in torch **source**, so they travel with any build (including a bare `python -m build` in a TheRock env), not just CI-produced wheels. This is the **minimal, consolidated PR** carrying **both** import-discovery fixes — a **two-file** diff (`torch/__init__.py` + `torch/cuda/__init__.py`). Both changes are **no-ops on the traditional tarball `/opt/rocm` install**: each early-returns when the wheel package's `_rocm_sdk_core` is absent, so non-wheel ROCm CI (apt/`/opt/rocm`, whose libs are already on the loader path), CUDA, and CPU builds are unaffected. ## Fixes ### 1. `torch/__init__.py` — `_preload_rocm_deps()` Fixes `ImportError: librocm_sysdeps_liblzma.so.5: cannot open shared object file` at `from torch._C import *` - Adds `_preload_rocm_deps()`, the ROCm analogue of `_preload_cuda_deps()` (which handles the `nvidia-*-cu12` wheels). At import time, **before** `import torch._C`, it locates the `rocm` pip package's `_rocm_sdk_core` via `importlib.util.find_spec` (the same anchor `torch.utils.cpp_extension._find_rocm_home()` uses) and `ctypes.CDLL`-preloads (`RTLD_GLOBAL`) the vendored sysdeps (`librocm_sysdeps_*`) and core ROCm runtime libs from `_rocm_sdk_core/lib` and `_rocm_sdk_core/lib/rocm_sysdeps/lib`. Wired into `_load_global_deps()`. - Guarded to no-op on non-Linux, on non-ROCm builds (`torch.version.hip is None` → CUDA/CPU), and on OS-managed ROCm where `_rocm_sdk_core` is absent (the apt/`/opt/rocm` path). ### 2. `torch/cuda/__init__.py` — amdsmi / `libamd_smi.so` discovery hook + non-fatal guard Fixes the *next* failure that surfaces once `_preload_rocm_deps()` gets `import torch` past the `librocm_sysdeps` error: `KeyError: 'libamd_smi.so'` raised from the `amdsmi` Python binding at `import amdsmi` (inside `_C._initExtension`) On TheRock wheels the amdsmi package installs the native library as a versioned soname under `_rocm_sdk_core/lib` (e.g. `libamd_smi.so.26`), which amdsmi's own `find_smi_library()` fails to locate when `$ROCM_HOME`/`$ROCM_PATH` and the loader path are unset; its wrapper then does `_libraries['libamd_smi.so']` → `KeyError`, which escaped the existing guard (only `ModuleNotFoundError` was tolerated) and killed `import torch`. Two-part torch-side hardening: - Make `_amdsmi_cdll_hook` **TheRock-aware**: locate `_rocm_sdk_core` via `importlib.util.find_spec` and add its `lib/libamd_smi.so*` (glob, incl. versioned sonames) as last-resort candidate paths; match names whose basename **starts with** `libamd_smi.so` so amdsmi's bare `CDLL("libamd_smi.so")` is redirected to the real versioned file. Existing `ROCM_HOME`/`ROCM_PATH` behavior preserved; guarded to Linux. - Make a non-discoverable amdsmi **non-fatal**: broaden the guard so `KeyError`/`OSError` degrade to `_HAS_AMDSMI=False` instead of aborting `import torch`, matching the existing missing-module path. ## Consolidation note The amdsmi discovery fix (#2 above) originally shipped as standalone PR pytorch#188724. That PR has been **closed** and folded into this PR so **both** import-discovery fixes travel together in a single minimal two-file change. Scope note: the TheRock tarball→wheels migration and the earlier CI-only RPATH/ldconfig workarounds are **not** part of this PR. The wheels migration is tracked separately by pytorch#188429, which rebases on top of this change. ## Validation (TheRock wheel path) — PASSED Validated end-to-end by **temporarily** stacking Ethan's wheels-migration PR pytorch#188429 (which adds the `rocm-preview` wheel CI) on top of the two source fixes and running `ciflow/rocm-preview` (run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341)) on the stacked head. The temporary stack was then **removed**, so this PR remains the minimal two-file source diff. - **`build-osdc` (TheRock wheel-path build): SUCCESS** — [job 84742887851](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84742887851). - **`import torch` works on the `_rocm_sdk_*` wheel layout** — proven by the passing shard [`test (default, 5/6)` job 84772673709](https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673709), which imports torch and prints the HIP 7.14 / MIOpen 3.5.2 build config. - **No** `ImportError: librocm_sysdeps*` (incl. `librocm_sysdeps_liblzma`) and **no** `KeyError: 'libamd_smi.so'` anywhere in the run → **both** fixes are validated on the **real TheRock wheel path**. - Remaining rocm-preview shard failures were **unrelated** to this PR — test-content failures (`test_CTCLoss_cudnn_cuda`, `test_cxx_flags` `RuntimeError map::at`, `test_hip_device_count`, `test_lazy_imports_are_lazy`) and infra (`ROCm pre-flight: no GPUs visible to the container` on the distributed shards). - Run: https://github.com/pytorch/pytorch/actions/runs/28581558341 ### rocm-preview distributed shards (register fat binary failed) Reference/evidence for the `register fat binary failed` (`code_object.cpp:1038`) investigation — the rocm-preview (TheRock wheel-path) distributed shards from run [28581558341](https://github.com/pytorch/pytorch/actions/runs/28581558341) (`linux-noble-rocm-preview-py3.12-gfx942`), all FAILED: - distributed 1/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673683 (`test (distributed, 1, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 2/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673678 (`test (distributed, 2, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) - distributed 3/3: https://github.com/pytorch/pytorch/actions/runs/28581558341/job/84772673727 (`test (distributed, 3, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed)`) <details> <summary>Failure log the amdsmi fix addresses (pre-fix, run 28524549808)</summary> ``` /var/lib/jenkins/workspace/libamd_smi.so: cannot open shared object file: No such file or directory Unable to find libamd_smi.so library try installing amd-smi-lib from your package manager Traceback (most recent call last): File "/var/lib/jenkins/workspace/.ci/pytorch/rocm_preflight.py", line 21, in <module> import torch File ".../site-packages/torch/cuda/__init__.py", line 114, in <module> import amdsmi # type: ignore[import] File ".../site-packages/amdsmi/amdsmi_wrapper.py", line 218, in <module> amdsmi_free_name_value_pairs = _libraries['libamd_smi.so'].amdsmi_free_name_value_pairs KeyError: 'libamd_smi.so' ``` </details> ## Notes: amdsmi discovery + fat-binary logs - **Bare `CDLL("libamd_smi.so")`** comes from amdsmi's own `find_smi_library()` (unversioned candidate), not torch; torch's `_amdsmi_cdll_hook` intercepts it and retries the versioned `_rocm_sdk_core/lib/libamd_smi.so*`. - **Not just preload amdsmi via `_rocm_core_libs`:** amdsmi resolves by exact filename, so an RTLD_GLOBAL versioned `libamd_smi.so.26` won't satisfy it, and eager-preloading a non-`DT_NEEDED` lib reintroduces the ODR double-load the hook prevents — the CDLL-redirect hook is the correct fix. - **`E0702 … code_object.cpp:1038 register fat binary failed` is not caused by this PR:** it's emitted by the HIP/clr C++ runtime, appears ambiently (~4,774×/run across rocm-preview gfx942 jobs incl. the distributed shards above), and is non-fatal — a pre-existing TheRock rocm-preview build/toolchain matter, orthogonal to these two Python-only changes. - **Fix-plan pointer (fat-binary):** prove pre-existing on a PR-free `main` rocm-preview baseline → localize via `AMD_LOG_LEVEL=3/4` → scoped clr-log suppress/demote (escalate to clr/comgr pin or `PYTORCH_ROCM_ARCH` only on a real running-arch failure) → validate via `ciflow/rocm-preview`. Belongs in the build/CI layer, not torch source. Tracked in ROCm/frameworks-internal pytorch#17160. ## Test plan - [x] Validate on the TheRock wheel path via `ciflow/rocm-preview` (see **Validation** above). - [x] `ast.parse` both changed files (`torch/__init__.py`, `torch/cuda/__init__.py`). - [x] Confirm CUDA and CPU builds are unaffected (both fixes no-op when `torch.version.hip is None` / `_rocm_sdk_core` absent). Fixes pytorch#188561 Made with Cursor Pull Request resolved: pytorch#188454 Approved by: https://github.com/jeffdaily
|
This PR continues to cause issues in ROCm, for the reasons I stated: ROCm/TheRock#6965. Specifically:
|
Drop the TheRock ROCm runtime preloading added in pytorch#188454 from torch/__init__.py while keeping the amdsmi / libamd_smi.so discovery changes in torch/cuda/__init__.py from that PR. Test Plan: - Verified diff only touches torch/__init__.py (torch/cuda/__init__.py unchanged) Authored with assistance from Cursor Co-authored-by: Cursor <[email protected]>
Drop the TheRock ROCm runtime preloading added in pytorch#188454 from torch/__init__.py while keeping the amdsmi / libamd_smi.so discovery changes in torch/cuda/__init__.py from that PR. Test Plan: - Verified diff only touches torch/__init__.py (torch/cuda/__init__.py unchanged) Authored with assistance from Cursor Co-authored-by: Cursor <[email protected]>
…RPATH (#190276) ## Summary Adds the ROCm 7.14 nightly binary-wheel build, installing ROCm from the multi-arch TheRock wheel index (`https://repo.amd.com/rocm/whl-multi-arch/`). Matrix: `ROCM_ARCHES = ["7.2", "7.14"]`. Re-combined into one PR (docker image + wheels) now that #190749 (content-addressed builder images) and #190985 (ROCm `manywheel-build` consumes the ECR image via `get-docker-tag`) let a single PR build its own `rocm7.14` `.ci/docker` image (pushed to ECR on `ciflow/docker`) and build/consume the wheel in the same `ciflow/binaries` run. ## Changes - `.ci/docker/common/install_rocm.sh`: `install_almalinux()` (mirrors `install_ubuntu()`) installs ROCm from the wheel index (`rocm[libraries,devel,device-all]==${ROCM_VERSION}.*`), discovers the root via `rocm-sdk path`, writes `/etc/rocm_env.sh` (build-time env; no `/opt/rocm` symlink); dispatched on `ID=almalinux`. - `.ci/docker/manywheel/Dockerfile_2_28`: OS-package stage renamed to `rocm_final_legacy`; new wheel-based `rocm_final` stage. `.ci/docker/manywheel/build.sh`: folded rocm7.14 into the generic `rocm*` case (7.14 -> `rocm_final` + almalinux base via `ROCM_VERSION`; earlier -> `rocm_final_legacy` + `rocm/dev-almalinux-8`). `build-manywheel-images.yml`: add the `rocm7.14` tag. - **`.ci/manywheel/repair_wheel.py`: RPATH method.** Instead of hand-maintaining `ROCM_SO_FILES` and bundling, add `rocm_rpaths()` (`$ORIGIN/../../_rocm_sdk_core/lib` + `rocm_sysdeps/lib`, mirroring `cuda_rpaths()`). For the TheRock wheel layout (`ROCM_HOME` under `_rocm_sdk*`) set RPATH and skip bundling; keep `rocm_bundle`/`ROCM_SO_FILES` only for the legacy OS layout (`/opt/rocm`, rocm7.2). This gets us out of maintaining a fragile list of ROCm libraries. - `.github/scripts/generate_binary_build_matrix.py`: `ROCM_ARCHES=["7.2","7.14"]` + regenerated workflow. `.ci/manywheel/build_env_setup.py`: `discover_rocm_home()`. ## Notes - RPATH is currently used only for the wheel-based rocm7.14 layout; rocm7.2 (OS `/opt/rocm`) keeps the self-contained bundle. The RPATH resolves the ROCm libs from the `rocm` pip package (`_rocm_sdk_core`, sibling of torch), the same way CUDA/XPU resolve from their pip packages, combined with `_preload_rocm_deps` (#188454). - RPATH-only for now: relies on `_rocm_sdk_core` being co-installed with torch in the CI build/test image; declaring `rocm` as an end-user `PYTORCH_EXTRA_INSTALL_REQUIREMENTS` is a follow-up. - Per #190749, the ROCm/XPU GPU **test** path stays on the Docker Hub image. ## Validation Built `manylinux2_28-builder:rocm7.14` locally: `install_almalinux` installs 7.14 from `repo.amd.com`; `hipcc --version` reports HIP 7.14.60850; `$ROCM_HOME/lib` has runtime `.so`s + `rocm_sysdeps/lib`. ruff/bash/py checks pass. Pull Request resolved: #190276 Approved by: https://github.com/jithunnair-amd, https://github.com/atalman Co-authored-by: Jithun Nair <[email protected]>
Summary
This PR makes PyTorch ROCm/TheRock wheels self-contained: it fixes the two
import torchfailures that occur on the wheel (_rocm_sdk_*site-packages) layout, soimport torchworks withoutLD_LIBRARY_PATHor a sourced ROCm env (e.g. the ROCm distributed preflightpython .ci/pytorch/rocm_preflight.py, which never sources/etc/rocm_env.sh).Both fixes live in torch source, so they travel with any build (including a bare
python -m buildin a TheRock env), not just CI-produced wheels. This is the minimal, consolidated PR carrying both import-discovery fixes — a two-file diff (torch/__init__.py+torch/cuda/__init__.py).Both changes are no-ops on the traditional tarball
/opt/rocminstall: each early-returns when the wheel package's_rocm_sdk_coreis absent, so non-wheel ROCm CI (apt//opt/rocm, whose libs are already on the loader path), CUDA, and CPU builds are unaffected.Fixes
1.
torch/__init__.py—_preload_rocm_deps()Fixes
ImportError: librocm_sysdeps_liblzma.so.5: cannot open shared object fileatfrom torch._C import *_preload_rocm_deps(), the ROCm analogue of_preload_cuda_deps()(which handles thenvidia-*-cu12wheels). At import time, beforeimport torch._C, it locates therocmpip package's_rocm_sdk_coreviaimportlib.util.find_spec(the same anchortorch.utils.cpp_extension._find_rocm_home()uses) andctypes.CDLL-preloads (RTLD_GLOBAL) the vendored sysdeps (librocm_sysdeps_*) and core ROCm runtime libs from_rocm_sdk_core/liband_rocm_sdk_core/lib/rocm_sysdeps/lib. Wired into_load_global_deps().torch.version.hip is None→ CUDA/CPU), and on OS-managed ROCm where_rocm_sdk_coreis absent (the apt//opt/rocmpath).2.
torch/cuda/__init__.py— amdsmi /libamd_smi.sodiscovery hook + non-fatal guardFixes the next failure that surfaces once
_preload_rocm_deps()getsimport torchpast thelibrocm_sysdepserror:KeyError: 'libamd_smi.so'raised from theamdsmiPython binding atimport amdsmi(inside_C._initExtension)On TheRock wheels the amdsmi package installs the native library as a versioned soname under
_rocm_sdk_core/lib(e.g.libamd_smi.so.26), which amdsmi's ownfind_smi_library()fails to locate when$ROCM_HOME/$ROCM_PATHand the loader path are unset; its wrapper then does_libraries['libamd_smi.so']→KeyError, which escaped the existing guard (onlyModuleNotFoundErrorwas tolerated) and killedimport torch. Two-part torch-side hardening:_amdsmi_cdll_hookTheRock-aware: locate_rocm_sdk_coreviaimportlib.util.find_specand add itslib/libamd_smi.so*(glob, incl. versioned sonames) as last-resort candidate paths; match names whose basename starts withlibamd_smi.soso amdsmi's bareCDLL("libamd_smi.so")is redirected to the real versioned file. ExistingROCM_HOME/ROCM_PATHbehavior preserved; guarded to Linux.KeyError/OSErrordegrade to_HAS_AMDSMI=Falseinstead of abortingimport torch, matching the existing missing-module path.Consolidation note
The amdsmi discovery fix (#2 above) originally shipped as standalone PR #188724. That PR has been closed and folded into this PR so both import-discovery fixes travel together in a single minimal two-file change.
Scope note: the TheRock tarball→wheels migration and the earlier CI-only RPATH/ldconfig workarounds are not part of this PR. The wheels migration is tracked separately by #188429, which rebases on top of this change.
Validation (TheRock wheel path) — PASSED
Validated end-to-end by temporarily stacking Ethan's wheels-migration PR #188429 (which adds the
rocm-previewwheel CI) on top of the two source fixes and runningciflow/rocm-preview(run 28581558341) on the stacked head. The temporary stack was then removed, so this PR remains the minimal two-file source diff.build-osdc(TheRock wheel-path build): SUCCESS — job 84742887851.import torchworks on the_rocm_sdk_*wheel layout — proven by the passing shardtest (default, 5/6)job 84772673709, which imports torch and prints the HIP 7.14 / MIOpen 3.5.2 build config.ImportError: librocm_sysdeps*(incl.librocm_sysdeps_liblzma) and noKeyError: 'libamd_smi.so'anywhere in the run → both fixes are validated on the real TheRock wheel path.test_CTCLoss_cudnn_cuda,test_cxx_flagsRuntimeError map::at,test_hip_device_count,test_lazy_imports_are_lazy) and infra (ROCm pre-flight: no GPUs visible to the containeron the distributed shards).rocm-preview distributed shards (register fat binary failed)
Reference/evidence for the
register fat binary failed(code_object.cpp:1038) investigation — the rocm-preview (TheRock wheel-path) distributed shards from run 28581558341 (linux-noble-rocm-preview-py3.12-gfx942), all FAILED:test (distributed, 1, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed))test (distributed, 2, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed))test (distributed, 3, 3, linux.rocm.gpu.gfx942.4, module:rocm, oncall:distributed))Failure log the amdsmi fix addresses (pre-fix, run 28524549808)
Notes: amdsmi discovery + fat-binary logs
CDLL("libamd_smi.so")comes from amdsmi's ownfind_smi_library()(unversioned candidate), not torch; torch's_amdsmi_cdll_hookintercepts it and retries the versioned_rocm_sdk_core/lib/libamd_smi.so*._rocm_core_libs: amdsmi resolves by exact filename, so an RTLD_GLOBAL versionedlibamd_smi.so.26won't satisfy it, and eager-preloading a non-DT_NEEDEDlib reintroduces the ODR double-load the hook prevents — the CDLL-redirect hook is the correct fix.E0702 … code_object.cpp:1038 register fat binary failedis not caused by this PR: it's emitted by the HIP/clr C++ runtime, appears ambiently (~4,774×/run across rocm-preview gfx942 jobs incl. the distributed shards above), and is non-fatal — a pre-existing TheRock rocm-preview build/toolchain matter, orthogonal to these two Python-only changes.mainrocm-preview baseline → localize viaAMD_LOG_LEVEL=3/4→ scoped clr-log suppress/demote (escalate to clr/comgr pin orPYTORCH_ROCM_ARCHonly on a real running-arch failure) → validate viaciflow/rocm-preview. Belongs in the build/CI layer, not torch source. Tracked in ROCm/frameworks-internal Feature request: torch.broadcast_to #17160.Test plan
ciflow/rocm-preview(see Validation above).ast.parseboth changed files (torch/__init__.py,torch/cuda/__init__.py).torch.version.hip is None/_rocm_sdk_coreabsent).Fixes #188561
cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang
Made with Cursor