From 66c4d1fb271ca137e3688849f9b9b47cbf52bead Mon Sep 17 00:00:00 2001 From: rraminen Date: Wed, 13 May 2026 20:46:12 +0000 Subject: [PATCH 1/2] Use TORCH_HIP_VERSION --- src/libtorchaudio/utils.cpp | 5 ++++- src/torchaudio/_extension/utils.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/libtorchaudio/utils.cpp b/src/libtorchaudio/utils.cpp index 3fb53264d3..3b3c99cc1c 100644 --- a/src/libtorchaudio/utils.cpp +++ b/src/libtorchaudio/utils.cpp @@ -15,7 +15,10 @@ bool is_align_available() { } std::optional cuda_version() { -#ifdef USE_CUDA +#if defined(TORCH_HIP_VERSION) + // TORCH_HIP_VERSION = {ROCM_VERSION[0] * 100 + ROCM_VERSION[1]} + return static_cast(TORCH_HIP_VERSION); +#elif defined(USE_CUDA) return CUDA_VERSION; #else return {}; diff --git a/src/torchaudio/_extension/utils.py b/src/torchaudio/_extension/utils.py index 526b8e704b..c3df0690b1 100644 --- a/src/torchaudio/_extension/utils.py +++ b/src/torchaudio/_extension/utils.py @@ -126,4 +126,16 @@ def _check_cuda_version(): f"PyTorch has CUDA version {t_version} whereas TorchAudio has CUDA version {ta_version}. " "Please install the TorchAudio version that matches your PyTorch version." ) + elif version is not None and torch.version.hip is not None: + ta_major = int(version) // 100 + ta_minor = int(version) % 100 + ta_version = f"{ta_major}.{ta_minor}" + hip_parts = torch.version.hip.split(".") + t_version = f"{hip_parts[0]}.{hip_parts[1]}" + if ta_version != t_version: + raise RuntimeError( + "Detected that PyTorch and TorchAudio were compiled with different ROCm HIP versions. " + f"PyTorch has HIP version {t_version} whereas TorchAudio has HIP version {ta_version}. " + "Please install the TorchAudio version that matches your PyTorch version." + ) return version From 41c4084b02cd961b8b2bc7f0097688b504e24502 Mon Sep 17 00:00:00 2001 From: rraminen Date: Wed, 13 May 2026 21:30:04 +0000 Subject: [PATCH 2/2] Remove rocm version logic in extensions.py --- tools/setup_helpers/extension.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tools/setup_helpers/extension.py b/tools/setup_helpers/extension.py index a8d0689cb5..8c97b11c65 100644 --- a/tools/setup_helpers/extension.py +++ b/tools/setup_helpers/extension.py @@ -3,7 +3,12 @@ from pathlib import Path import torch -from torch.utils.cpp_extension import BuildExtension, CppExtension, CUDAExtension +from torch.utils.cpp_extension import ( + TORCH_HIP_VERSION, + BuildExtension, + CppExtension, + CUDAExtension, +) __all__ = [ "get_ext_modules", @@ -74,14 +79,10 @@ def get_ext_modules(): if _USE_ROCM: extension = CUDAExtension extra_compile_args["nvcc"] = ["-O3"] - # TORCH_HIP_VERSION is used by hipified C++ (e.g. utils_hip.cpp); PyTorch only defines it when building PyTorch. - if torch.version.hip: - parts = torch.version.hip.split(".") - major = int(parts[0]) if len(parts) > 0 else 0 - minor = int(parts[1]) if len(parts) > 1 else 0 - torch_hip_version = major * 100 + minor # e.g. 7.1.x -> 701 - extra_compile_args["cxx"].append("-DTORCH_HIP_VERSION=" + str(torch_hip_version)) - extra_compile_args["nvcc"].append("-DTORCH_HIP_VERSION=" + str(torch_hip_version)) + if TORCH_HIP_VERSION is not None: + flag = f"-DTORCH_HIP_VERSION={int(TORCH_HIP_VERSION)}" + extra_compile_args["cxx"].append(flag) + extra_compile_args["nvcc"].append(flag) sources = [ "utils.cpp",