From a86383ca11e3a4fd28e127f6d509ed857fd5a7f0 Mon Sep 17 00:00:00 2001 From: Yuanyuan Chen Date: Fri, 23 Oct 2026 05:15:29 +0000 Subject: [PATCH 1/2] Take the ROCm SDK version for _rocm_init.py from the build environment tools/metadata/dependencies.py pins rocm[libraries] to PYTORCH_ROCM_SDK_VERSION, or to the rocm package installed in the build environment when the variable is unset, but torch/CMakeLists.txt only generated _rocm_init.py from the variable. A build that installs the SDK's rocm package instead of exporting the version got the dependency but no preload. Fall back to the installed rocm package in CMake too, so both read the same version. Test Plan: ``` cmake -P t.cmake # check_version='' PYTORCH_ROCM_SDK_VERSION=9.9.9 cmake -P t.cmake # check_version='9.9.9' ``` Authored with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 --- torch/CMakeLists.txt | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/torch/CMakeLists.txt b/torch/CMakeLists.txt index 953d7ce7e3ccd..10535d743ff3b 100644 --- a/torch/CMakeLists.txt +++ b/torch/CMakeLists.txt @@ -466,10 +466,22 @@ endif() # _rocm_init.py is gitignored (injected by ROCm build scripts to bootstrap # torch/__init__.py). See https://github.com/ROCm/TheRock/blob/main/docs/packaging/python_packaging.md -# When building against the ROCm SDK wheels, PYTORCH_ROCM_SDK_VERSION names -# their version: generate the file so `import torch` preloads the SDK's -# libraries from those wheels and checks that the installed SDK matches. -if(USE_ROCM AND NOT "$ENV{PYTORCH_ROCM_SDK_VERSION}" STREQUAL "") +# Generate it for the ROCm SDK wheels this build targets, so `import torch` +# preloads their libraries and checks the installed SDK matches. The version is +# PYTORCH_ROCM_SDK_VERSION, else the rocm package in the build environment, +# matching tools/metadata/dependencies.py. +set(_rocm_sdk_version "$ENV{PYTORCH_ROCM_SDK_VERSION}") +if(USE_ROCM AND _rocm_sdk_version STREQUAL "") + execute_process( + COMMAND "${Python_EXECUTABLE}" -c "from importlib.metadata import version; print(version('rocm'), end='')" + OUTPUT_VARIABLE _rocm_sdk_version + RESULT_VARIABLE _rocm_sdk_result + ERROR_QUIET) + if(NOT _rocm_sdk_result EQUAL 0) + set(_rocm_sdk_version "") + endif() +endif() +if(USE_ROCM AND NOT _rocm_sdk_version STREQUAL "") set(_rocm_preloads amd_comgr amd_smi amdhip64 hiprtc hipblas hipfft hiprand hipsparse hipsparselt hipsolver hipblaslt miopen rocm_sysdeps_liblzma rocm-openblas @@ -482,7 +494,7 @@ if(USE_ROCM AND NOT "$ENV{PYTORCH_ROCM_SDK_VERSION}" STREQUAL "") " import rocm_sdk\n" " rocm_sdk.initialize_process(\n" " preload_shortnames=[${_rocm_preloads}],\n" - " check_version='$ENV{PYTORCH_ROCM_SDK_VERSION}')\n") + " check_version='${_rocm_sdk_version}')\n") endif() # scikit-build-core won't include it via Python package collection. Install it # explicitly via cmake instead. From c545515a00a1524dd52d7828b4c380fecf576a63 Mon Sep 17 00:00:00 2001 From: Yuanyuan Chen Date: Fri, 23 Oct 2026 05:18:25 +0000 Subject: [PATCH 2/2] Depend on the ROCm SDK device wheel too rocm[libraries] leaves out rocm-sdk-device-gfx1201, which carries the GPU code objects for rocBLAS, MIOpen and the other kpack-split libraries; without it their kernels fail with hipErrorInvalidKernelFile. Authored with an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 --- tools/metadata/dependencies.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/metadata/dependencies.py b/tools/metadata/dependencies.py index 5696acfde992a..8450934a4bb6a 100644 --- a/tools/metadata/dependencies.py +++ b/tools/metadata/dependencies.py @@ -62,7 +62,8 @@ def dynamic_metadata( if extra: deps.extend(r.strip() for r in extra.split("|") if r.strip()) elif _is_truthy(os.environ.get("USE_ROCM")): - # Pin the ROCm SDK wheels the build used: PYTORCH_ROCM_SDK_VERSION when + # Pin the ROCm SDK wheels the build used, including the device wheel with + # the GPU code objects: PYTORCH_ROCM_SDK_VERSION when # building against an unpacked SDK, else the installed rocm package. rocm_version = os.environ.get("PYTORCH_ROCM_SDK_VERSION") if not rocm_version: @@ -71,7 +72,7 @@ def dynamic_metadata( except PackageNotFoundError: rocm_version = None if rocm_version: - deps.append(f"rocm[libraries]=={rocm_version}") + deps.append(f"rocm[libraries,device]=={rocm_version}") # cupti-python has no free-threaded wheels; PEP 508 cannot express this ABI constraint. if sysconfig.get_config_var("Py_GIL_DISABLED"):