Repository navigation
fix(amdsmi): load rocm-sdk-core's library from a copied Python module - #14510
Merged
dmitrii-galantsev merged 4 commits intoNov 6, 2026
Merged
Conversation
The rework in #9340 left the module only at _rocm_sdk_core/share/amd_smi in TheRock's rocm-sdk-core wheel. Copied into site-packages, it cannot find libamd_smi.so.27 there and loads only after PyTorch has loaded that file (ROCM-33336). - Without a system library, the loader now uses the _rocm_sdk_core package on sys.path, found without importing it so import guards cannot stop it. - TheRock sets _AMDSMI_ROCM_SDK_CORE in the copy it ships. That copy loads only rocm-sdk-core's library and never a system libamd_smi.so from another release. The rpm/deb copy keeps its order and gains only the fallback. - Each candidate is first opened with RTLD_NOLOAD. PyTorch 2.14's import hook redirects any libamd_smi.so* load to $ROCM_PATH/lib first; asked this way it can only return a copy already in the process. - SONAME 27 grew structs between 27.1 and 27.2, so a library newer than the module's _version.py is closed and skipped, and an older one warns. get_version_from_file now makes CMake reconfigure when amdsmi.h changes, so _version.py cannot lag the library in an incremental build. ROCM-33336 Signed-off-by: Galantsev, Dmitrii <dmitrii.galantsev@amd.com>
1 task done
✅ All Policy Checks Passed
📖 Need help? See the Policy FAQ for details on every check and how to fix failures. |
On Python before 3.14, Path.is_file() raises PermissionError for an entry under an unreadable directory, and Path() raises TypeError for a bytes entry, which escaped the loader's OSError handler and broke `import amdsmi`. The import system skips both, so the lookup does too. When _rocm_sdk_core was found but held no library, the flagged copy reported "_rocm_sdk_core not found on sys.path"; it now names the missing file. Tests now cover the version check on the SONAME path, the dlclose of a refused library, and both cases above. The CHANGELOG now says the fallback also follows a refused system library. Signed-off-by: Galantsev, Dmitrii <dmitrii.galantsev@amd.com>
dmitrii-galantsev
marked this pull request as ready for review
October 28, 2026 21:44
dmitrii-galantsev
requested review from
a team
and
a balanced review from Copilot
October 28, 2026 21:44
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Library-loading paths still let PyTorch hooks load a system AMD SMI copy instead of reusing or opening the intended SDK library, risking duplicate-library GPU-enumeration failures.
3 open issues
What changed in this PR
Updates AMD SMI’s Python loader so modules copied from TheRock’s rocm-sdk-core wheel can locate the SDK library outside share/amd_smi, while adding library-version checks.
Changes:
- Adds SDK package discovery and SDK-only loading behavior, mirrored in the wrapper generator.
- Adds reuse-before-load attempts, version compatibility checks, and loader regression tests.
- Triggers CMake reconfiguration when the version header changes to keep Python version metadata synchronized.
| File | Description |
|---|---|
projects/amdsmi/tools/generator.py |
Generates the updated SDK discovery and library-loading logic. |
projects/amdsmi/tests/python/test_abi_compat.py |
Tests SDK fallback, library reuse, version checks, and unusable search paths. |
projects/amdsmi/py-interface/amdsmi_wrapper.py |
Implements SDK discovery, loading controls, and version validation. |
projects/amdsmi/cmake_modules/utils.cmake |
Tracks the version header as a configure dependency. |
projects/amdsmi/CHANGELOG.md |
Documents copied-module support and compatibility safeguards. |
Review effort: Balanced
The packaging and install docs still said the rocm-sdk-core module only resolves its library relative to the wrapper. Describe the _AMDSMI_ROCM_SDK_CORE flag, the _rocm_sdk_core lookup that a module copied into site-packages relies on, RTLD_NOLOAD reuse of an already loaded library, and the refusal of a library newer than the module. Signed-off-by: Galantsev, Dmitrii <dmitrii.galantsev@amd.com>
… ctypes hook PyTorch 2.14's hook redirects any plain libamd_smi.so* CDLL to $ROCM_HOME's copy while torch is imported. With nothing loaded yet, the rocm-sdk-core candidate was redirected to a same-version system library, which passed the version check while _loaded_lib_path still named the SDK file. Open concrete candidates through ctypes.cdll, which the hook does not wrap, after the RTLD_NOLOAD probe. The SONAME lookup used a bare CDLL, so a copy already loaded from rocm-sdk-core was not reused and the hook loaded a second one. Route it through the same probe. An empty sys.path entry is the working directory; only non-str entries are skipped now, and relative entries are made absolute.
1 of 3 tasks
gabrpham
approved these changes
Nov 5, 2026
adam360x
approved these changes
Nov 6, 2026
dmitrii-galantsev
deleted the
users/dgalants/amdsmi-rocm-sdk-core-import
branch
November 6, 2026 00:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Motivation
TheRock's
rocm-sdk-corewheel carries theamdsmiPython module only at_rocm_sdk_core/share/amd_smi(since #9340). Copied into site-packages,import amdsmi; amdsmi.amdsmi_init()fails withlibamd_smi.so.27: cannot open shared object fileunlesstorchwas imported first, because only PyTorch's preload makes the library findable.Technical Details
_load_library()inamdsmi_wrapper.py(mirrored intools/generator.py):_rocm_sdk_corelookup. When no system library is found or the one found is refused, the loader takeslib/libamd_smi.so.27from the_rocm_sdk_corepackage (sys.modules, elsesys.path). It checks paths only and imports nothing, so import guards like rocprofiler-compute's cannot break it (test(amdsmi): cover loader resilience under a restrictive import guard #9879)._AMDSMI_ROCM_SDK_COREflag. TheRock sets it in the copy it ships inrocm-sdk-core(Mark the amdsmi Python module shipped in rocm-sdk-core TheRock#9578). That copy then uses the relocatable path and_rocm_sdk_coreonly, never a systemlibamd_smi.sofrom another release. The rpm/deb copy keeps its order and only gains the fallback.RTLD_NOLOAD. PyTorch 2.14's_amdsmi_cdll_hook(in the ROCm 10.1.0 wheels; narrowed upstream in [ROCm] Preserve amdsmi library selection during import pytorch/pytorch#197482) redirects anylibamd_smi.so*load to$ROCM_PATH/lib/libamd_smi.sofirst. WithROCM_PATHset and a system ROCm installed, the module loaded that second copy and bothtorch.cuda.device_count()and amdsmi reported 0 GPUs. Asked withRTLD_NOLOAD, the hook can only return a copy already in the process.amdsmi_fabric_info_tgrew from 320 to 552 bytes when its union gainedv2, and 10.2 nightlies built before revert(amdsmi): restore single-field driver version info #13926 have a 1792-byteamdsmi_driver_info_tthatamdsmi_get_gpu_driver_info()zeroes in full, against the 768 bytes a 27.1 module allocates. A library newer than the module's_version.pyis closed and skipped, so it cannot write past the module's buffers. An older one warns.AMDSMI_LIB_OVERRIDEstill loads anything.get_version_from_filemakes CMake reconfigure whenamdsmi.hchanges, so_version.pycannot lag the library in an incremental build.docs/packaging.md,docs/install/install.mdandpy-interface/README.mddescribe the new loader order, the flag,RTLD_NOLOADreuse and the version check.This does not make
share/amd_smipip-installable again. A module installed from there would not depend onrocm-sdk-coreand would go stale on the next ROCm upgrade.Issue Tracking
JIRA ID: ROCM-33336
Related: ROCM-31613, ROCm/TheRock#9226 (with #14409), ROCm/TheRock#3054
Test Plan
tests/python/test_abi_compat.py): flag on and off, the fallback,RTLD_NOLOADreuse, newer and older libraries, closing a refused library, unusablesys.pathentries. Each new check was disabled once to confirm its test fails.rocm-sdk-coremodule patched with this loader, on the shipped wheels (rocm-sdk-core10.1.0,torch 2.14.0+rocm10.1.0), 2× MI210, host with a 10.2 ROCm inld.so.cacheandROCM_PATHset:PYTHONPATH;/optandld.so.cachehidden;amd-smiCLI from an install tree and fromrocm-sdk-core's entry point.amdsmi.hfollowed byninja: reconfigures and updates_version.py.DESTDIRinstall: the site-packages copy matchesshare/amd_smi,_version.pyincluded.Test Result
libamd_smimapped, torch and amdsmi each report 2 GPUs.amd-smi versionandamd-smi list: work from both locations.Submission Checklist