Skip to content

Commit 25a64df

Browse files
amd-sriramjeffdaily
authored andcommitted
[ROCm] add torch.version.rocm, distinct from torch.version.hip (pytorch#168097)
Historically, HIP and ROCm versions were interchangeable, but moving forward these versions are allowed to diverge. ROCm version represents the full ROCm software stack, while HIP is a component of the ROCm stack. Issue pytorch#166068 was fixed by [switching from using HIP_VERSION to ROCM_VERSION_DEV](pytorch#166336). However, this broke the build of ROCm apex because the hip version from `hipcc --version` no longer matched `torch.version.hip`. This highlights the need for both versions to be exposed. Bitsandbytes has also been impacted by the change in behavior of `torch.version.hip`: bitsandbytes-foundation/bitsandbytes#1799 (comment) The solution is to fix the `torch.version.hip` so that it uses the hipcc header values and removes the trailing hash code. In addition, `torch.version.rocm` variable is created to store the ROCm version. ## Technical Details ### Fix torch.version.hip HIP_VERSION variable is computed in https://github.com/ROCm/hip/blob/develop/cmake/FindHIP.cmake. This runs hipcc –version and extracts the output of HIP version line, e.g., ``` hipcc --version HIP version: 7.1.25421-32f9fa6ca5 ``` The HIP_VERSION variable may contain a hash code at the end. This trailing hashcode is removed from the HIP_VERSION variable so that the torch.version.hip can be parsed by packaging version parse method, e.g., ``` import torch from packaging import version print(version.parse(torch.version.hip)) ``` ### Add torch.version.rocm Code changes: - Add rocm variable to torch/version.py.tpl - Add code to write rocm variable in tools/generate_torch_version.py - Write rocm version in installation process - torch/CMakeLists.txt ## Testing Tested on a preview of ROCm 7.2. Successfully built pytorch and apex. Tested above parsing torch.version.hip code. ``` >>> import torch >>> torch.version.hip '7.1.25421' >>> torch.version.rocm '7.2.0' ``` Pull Request resolved: pytorch#168097 Approved by: https://github.com/jeffdaily Co-authored-by: Jeff Daily <[email protected]>
1 parent 6fa7791 commit 25a64df

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

cmake/public/LoadHIP.cmake

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,18 @@ find_package_and_print_version(HIP 1.0 MODULE)
8383
if(HIP_FOUND)
8484
set(PYTORCH_FOUND_HIP TRUE)
8585
find_package_and_print_version(hip REQUIRED CONFIG)
86+
if(HIP_VERSION)
87+
# Check if HIP_VERSION contains a dash (e.g., "7.1.25421-32f9fa6ca5")
88+
# and strip everything after it to get clean numeric version
89+
string(FIND "${HIP_VERSION}" "-" DASH_POS)
90+
if(NOT DASH_POS EQUAL -1)
91+
string(SUBSTRING "${HIP_VERSION}" 0 ${DASH_POS} HIP_VERSION_CLEAN)
92+
set(HIP_VERSION "${HIP_VERSION_CLEAN}")
93+
endif()
94+
message("HIP version: ${HIP_VERSION}")
95+
endif()
8696

87-
# The rocm-core package was only introduced in ROCm 6.4, so we make it optional.
97+
# The rocm-core package was only introduced in ROCm 6.4, so we make it optional.
8898
find_package(rocm-core CONFIG)
8999

90100
# Some old consumer HIP SDKs do not distribute rocm_version.h, so we allow

tools/generate_torch_version.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ def get_torch_version(sha: str | None = None) -> str:
119119
)
120120
parser.add_argument("--cuda-version", "--cuda_version", type=str)
121121
parser.add_argument("--hip-version", "--hip_version", type=str)
122+
parser.add_argument("--rocm-version", "--rocm_version", type=str)
122123
parser.add_argument("--xpu-version", "--xpu_version", type=str)
123124

124125
args = parser.parse_args()
125126

126127
assert args.is_debug is not None
127128
args.cuda_version = None if args.cuda_version == "" else args.cuda_version
128129
args.hip_version = None if args.hip_version == "" else args.hip_version
130+
args.rocm_version = None if args.rocm_version == "" else args.rocm_version
129131
args.xpu_version = None if args.xpu_version == "" else args.xpu_version
130132

131133
pytorch_root = Path(__file__).parent.parent
@@ -141,7 +143,7 @@ def get_torch_version(sha: str | None = None) -> str:
141143
with open(version_path, "w") as f:
142144
f.write("from typing import Optional\n\n")
143145
f.write(
144-
"__all__ = ['__version__', 'debug', 'cuda', 'git_version', 'hip', 'xpu']\n"
146+
"__all__ = ['__version__', 'debug', 'cuda', 'git_version', 'hip', 'rocm', 'xpu']\n"
145147
)
146148
f.write(f"__version__ = '{version}'\n")
147149
# NB: This is not 100% accurate, because you could have built the
@@ -151,4 +153,5 @@ def get_torch_version(sha: str | None = None) -> str:
151153
f.write(f"cuda: Optional[str] = {repr(args.cuda_version)}\n")
152154
f.write(f"git_version = {repr(sha)}\n")
153155
f.write(f"hip: Optional[str] = {repr(args.hip_version)}\n")
156+
f.write(f"rocm: Optional[str] = {repr(args.rocm_version)}\n")
154157
f.write(f"xpu: Optional[str] = {repr(args.xpu_version)}\n")

torch/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ add_custom_target(
490490
"${Python_EXECUTABLE}" "${TOOLS_PATH}/generate_torch_version.py"
491491
--is-debug=${TORCH_VERSION_DEBUG}
492492
--cuda-version=${CUDA_VERSION}
493-
--hip-version=${ROCM_VERSION_DEV}
493+
--hip-version=${HIP_VERSION_CLEAN}
494+
--rocm-version=${ROCM_VERSION_DEV}
494495
--xpu-version=${SYCL_COMPILER_VERSION}
495496
BYPRODUCTS ${TORCH_SRC_DIR}/version.py
496497
COMMENT "Regenerating version file..."

torch/version.py.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ cuda = '{{CUDA_VERSION}}'
44
# TODO: use workspace status to stamp the correct version
55
git_version = ""
66
hip = None
7+
rocm = None
78

89
# This is a gross monkey-patch hack that depends on the order of imports
910
# in torch/__init__.py

0 commit comments

Comments
 (0)