Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions projects/amdsmi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ Full documentation for amd_smi_lib is available at [https://rocm.docs.amd.com/pr
- **Fixed GPU metrics returning no data on integrated GPUs**.
- `amdsmi_get_gpu_metrics_info()` and `amdsmi_get_gpu_activity()` failed on APUs such as gfx1103, leaving PLX temperature, GPU utilization and `amd-smi metric` empty. Those devices now report the values they provide with the rest marked N/A, and any GPU without a PLX sensor now reports N/A there instead of a placeholder reading.

- **Fixed the `amdsmi` Python module from TheRock's `rocm-sdk-core` wheel failing outside `share/amd_smi`**.
- Copied into site-packages, the module could not load `libamd_smi.so.27` unless `torch` was imported first. It now loads the library from the `_rocm_sdk_core` package on `sys.path` when no usable system library is found, and reuses the copy PyTorch already loaded instead of mapping a second one.
- The copy TheRock ships in `rocm-sdk-core` loads only that wheel's library, never a system `libamd_smi.so` from another ROCm release.
- The module refuses a library newer than itself, whose structures can be larger than the module allocates, and warns about an older one. `AMDSMI_LIB_OVERRIDE` still loads any library.

- **Fixed `amd-smi` printing a Python traceback when an unknown NIC or switch is selected**.
- `amd-smi static --nic 999` and `--switch 999` failed while building the "device not found" error, so the command exited `1` with a traceback and no readable message. `--json` and `--csv` produced no parseable output.
- Both now report `Can not find a device: NIC '999'` (or `SWITCH`) and exit `196`, matching `--gpu`, `--cpu`, and `--core`.
Expand Down
3 changes: 3 additions & 0 deletions projects/amdsmi/cmake_modules/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function(get_version_from_file REL_FILE_PATH ITEM)
set(OUTPUT_ITEM "0")

if(EXISTS "${FILE_PATH}")
# Reconfigure when the version changes, so the version configured into
# files such as the Python module's _version.py matches the library.
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${FILE_PATH}")
file(READ ${FILE_PATH} file_contents)
string(REGEX MATCHALL "AMDSMI_LIB_VERSION_${ITEM} *[0-9]+" OUTPUT_STR "${file_contents}")
list(LENGTH OUTPUT_STR OUTPUT_STR_LENGTH)
Expand Down
169 changes: 158 additions & 11 deletions projects/amdsmi/py-interface/amdsmi_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def char_pointer_cast(string, encoding='utf-8'):
# ---------------------------------------------------------------------------
# Dynamic library loading
# ---------------------------------------------------------------------------
# This wrapper supports three self-contained install paths:
# This wrapper supports four self-contained install paths:
#
# 1. pip wheel
# The wheel ships ``libamd_smi_python.so`` next to this file.
Expand All @@ -168,6 +168,10 @@ def char_pointer_cast(string, encoding='utf-8'):
# Wrapper at ``<root>/share/amd_smi/amdsmi/``, library at
# ``<root>/lib/<SONAME>`` -- resolved by fixed relative path.
#
# 4. the copy TheRock ships in its rocm-sdk-core wheel, also once copied
# into site-packages: library at ``<_rocm_sdk_core>/lib/<SONAME>``.
# TheRock marks that copy by setting ``_AMDSMI_ROCM_SDK_CORE``.
#
# A user installs ONE of these packages. We never combine paths
# from them -- no ROCM_HOME / ROCM_PATH ladders, no walking up to a
# ROCm root, no LD_LIBRARY_PATH probing. ``AMDSMI_LIB_OVERRIDE`` stays
Expand All @@ -190,6 +194,112 @@ def char_pointer_cast(string, encoding='utf-8'):
# system library (which would risk symbol conflicts inside PyTorch / JAX).
_AMDSMI_ALLOW_SYSTEM_FALLBACK = True

# TheRock flips this to True in the copy it ships in the rocm-sdk-core wheel
# (build_tools/build_python_packages.py). That copy then loads only
# rocm-sdk-core's library, never a system libamd_smi.so found by SONAME, so a
# module from one ROCm release is not paired with another release's library.
_AMDSMI_ROCM_SDK_CORE = False


def _module_version():
"""(major, minor) of the build this module came from, read from the
_version.py that is configured beside it, or None when it is absent."""
import re

try:
text = (Path(__file__).resolve().parent / "_version.py").read_text()
except OSError:
return None
m = re.search(r'__version__\s*=\s*["\'](\d+)\.(\d+)', text)
return (int(m.group(1)), int(m.group(2))) if m else None


def _check_library_version(lib, path):
"""Reject a library newer than this module: SONAME 27 has grown structs
between minor releases, and an older module's buffers are too small for
them. An older library only lacks newer functions, so it is a warning."""
expected = _module_version()
if expected is None:
return
try:
get_lib_version = lib.amdsmi_get_lib_version
except AttributeError:
return
# amdsmi_version_t starts with uint32 major, minor; the buffer is larger
# than the struct so no future layout can overflow it.
version = (ctypes.c_uint32 * 64)()
if get_lib_version(version) != 0:
return
found = (version[0], version[1])
if found[0] != expected[0] or found[1] > expected[1]:
raise OSError(
"loading %s returned AMD SMI library %d.%d, which this %d.%d Python module cannot "
"use; set AMDSMI_LIB_OVERRIDE to a library path to load it anyway"
% ((path,) + found + expected)
)
if found[1] < expected[1]:
import warnings

warnings.warn(
"loading %s returned AMD SMI library %d.%d, older than this %d.%d Python module; "
"newer functions are unavailable" % ((path,) + found + expected),
stacklevel=3,
)


def _unload(lib):
"""Drop the reference ctypes.CDLL took on a library the version check
rejected, so a later load by SONAME, or one PyTorch's hook redirects,
cannot return it."""
import _ctypes

handle = getattr(lib, "_handle", None)
if handle is not None and hasattr(_ctypes, "dlclose"):
_ctypes.dlclose(handle)


def _rocm_sdk_core_dir():
"""The _rocm_sdk_core package directory, found the way an import would
find it but without importing it, so import guards that reject third-party
packages cannot break the load."""
import sys

module = sys.modules.get("_rocm_sdk_core")
if getattr(module, "__file__", None):
return Path(module.__file__).parent
for entry in sys.path:
if not entry or not isinstance(entry, str):
Comment thread
dmitrii-galantsev marked this conversation as resolved.
Resolved
continue
candidate = Path(entry) / "_rocm_sdk_core"
try:
if (candidate / "__init__.py").is_file():
return candidate
except OSError:
continue
return None


def _open_library(path, mode, errors):
"""Open path, reusing it when it is already loaded. PyTorch's import hook
can redirect a plain load to another libamd_smi; with RTLD_NOLOAD the
redirect can only return a copy the process has already loaded."""
noload = getattr(os, "RTLD_NOLOAD", 0)
for attempt in ([mode | noload] if noload else []) + [mode]:
try:
lib = ctypes.CDLL(str(path), mode=attempt)
Comment thread
dmitrii-galantsev marked this conversation as resolved.
Resolved
except OSError as err:
if attempt == mode:
errors.append(str(err))
continue
try:
_check_library_version(lib, path)
except OSError as err:
errors.append(str(err))
_unload(lib)
return None
return lib
return None


def _load_library():
"""Load the AMD SMI shared library.
Expand All @@ -198,8 +308,10 @@ def _load_library():
1. ``AMDSMI_LIB_OVERRIDE`` env var (ABI-test escape hatch).
2. ``libamd_smi_python.so`` next to this file (pip wheel).
3. ``<root>/lib/<SONAME>`` relative to this file (relocatable ROCm tree).
4. SONAME via the dynamic linker (system rpm / deb); skipped when
_AMDSMI_ALLOW_SYSTEM_FALLBACK is False (pip wheel).
4. In rocm-sdk-core's copy: ``_rocm_sdk_core/lib/<SONAME>``, then stop.
5. SONAME via the dynamic linker (system rpm / deb).
6. ``_rocm_sdk_core/lib/<SONAME>``, for a copy without TheRock's flag.
3-6 skip a library newer than this module.
"""
mode = getattr(ctypes, "RTLD_LOCAL", 0)

Expand All @@ -222,16 +334,51 @@ def _load_library():
# amdsmi_interface.py resolves librocm-core.so the same way. A
# present-but-unloadable file (missing deps) must not shadow the system
# linker lookup, so fall through on OSError.
candidates = []
here = Path(__file__).resolve()
if len(here.parents) > 3:
relocatable = here.parents[3] / "lib" / _AMDSMI_LIB_SONAME
if relocatable.exists():
try:
return ctypes.CDLL(str(relocatable), mode=mode), str(relocatable)
except OSError:
pass

return ctypes.CDLL(_AMDSMI_LIB_SONAME, mode=mode), _AMDSMI_LIB_SONAME
candidates.append(here.parents[3] / "lib" / _AMDSMI_LIB_SONAME)
if _AMDSMI_ROCM_SDK_CORE:
sdk_core = _rocm_sdk_core_dir()
if sdk_core is not None:
candidates.append(sdk_core / "lib" / _AMDSMI_LIB_SONAME)

errors = []
for path in candidates:
if path.exists():
lib = _open_library(path, mode, errors)
if lib is not None:
return lib, str(path)

if _AMDSMI_ROCM_SDK_CORE:
if sdk_core is None:
errors.append("_rocm_sdk_core not found on sys.path")
raise OSError(
"no usable %s from rocm-sdk-core: %s"
% (_AMDSMI_LIB_SONAME, "; ".join(errors) or "no file at " + ", ".join(map(str, candidates)))
)
try:
lib = ctypes.CDLL(_AMDSMI_LIB_SONAME, mode=mode)
except OSError as err:
errors.append(str(err))
else:
try:
_check_library_version(lib, _AMDSMI_LIB_SONAME)
return lib, _AMDSMI_LIB_SONAME
except OSError as err:
errors.append(str(err))
_unload(lib)
Comment thread
dmitrii-galantsev marked this conversation as resolved.
Resolved

# No system library: a module copied beside TheRock's rocm-sdk-core wheel
# by hand, without TheRock's flag, still finds that wheel's library.
sdk_core = _rocm_sdk_core_dir()
if sdk_core is not None:
path = sdk_core / "lib" / _AMDSMI_LIB_SONAME
if path.exists():
lib = _open_library(path, mode, errors)
if lib is not None:
return lib, str(path)
raise OSError("; ".join(errors))


class _MissingLibrary:
Expand Down
Loading
Loading