diff --git a/tools/metadata/dependencies.py b/tools/metadata/dependencies.py index 0b1be2acf01ea..5696acfde992a 100644 --- a/tools/metadata/dependencies.py +++ b/tools/metadata/dependencies.py @@ -61,14 +61,16 @@ def dynamic_metadata( extra = os.environ.get("PYTORCH_EXTRA_INSTALL_REQUIREMENTS") if extra: deps.extend(r.strip() for r in extra.split("|") if r.strip()) - elif _is_truthy(os.environ.get("USE_ROCM")) and not _is_truthy( - os.environ.get("PYTORCH_ROCM_EXTERNAL_SDK") - ): - try: - rocm_version = version("rocm") - except PackageNotFoundError: - pass - else: + elif _is_truthy(os.environ.get("USE_ROCM")): + # Pin the ROCm SDK wheels the build used: 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: + try: + rocm_version = version("rocm") + except PackageNotFoundError: + rocm_version = None + if rocm_version: deps.append(f"rocm[libraries]=={rocm_version}") # cupti-python has no free-threaded wheels; PEP 508 cannot express this ABI constraint. diff --git a/torch/CMakeLists.txt b/torch/CMakeLists.txt index e2479ab4f98e0..953d7ce7e3ccd 100644 --- a/torch/CMakeLists.txt +++ b/torch/CMakeLists.txt @@ -466,6 +466,24 @@ 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 "") + set(_rocm_preloads + amd_comgr amd_smi amdhip64 hiprtc hipblas hipfft hiprand hipsparse + hipsparselt hipsolver hipblaslt miopen rocm_sysdeps_liblzma rocm-openblas + rocprofiler-sdk rocprofiler-sdk-roctx roctracer64 roctx64 rccl hipfile) + list(TRANSFORM _rocm_preloads PREPEND "'") + list(TRANSFORM _rocm_preloads APPEND "'") + list(JOIN _rocm_preloads ", " _rocm_preloads) + file(WRITE "${TORCH_SRC_DIR}/_rocm_init.py" + "def initialize():\n" + " import rocm_sdk\n" + " rocm_sdk.initialize_process(\n" + " preload_shortnames=[${_rocm_preloads}],\n" + " check_version='$ENV{PYTORCH_ROCM_SDK_VERSION}')\n") +endif() # scikit-build-core won't include it via Python package collection. Install it # explicitly via cmake instead. if(SKBUILD AND USE_ROCM AND EXISTS "${TORCH_SRC_DIR}/_rocm_init.py")