Skip to content

Commit bd63ec4

Browse files
naromero77amdpytorchmergebot
authored andcommitted
[ROCm] LoadHIP CMake cleanup (pytorch#137112)
Should help mitigate issues reported here: pytorch#128313 While working on pytorch#136700, we realized that some of the ROCm CMake can be streamlined. This PR does not fix any bugs or provide any new functionality. Strictly clean-up. The remaining `${ROCM_ROCTX_LIB}` will be removed when we transition to the rocprofiler-sdk (to be done in a separate PR). Pull Request resolved: pytorch#137112 Approved by: https://github.com/jithunnair-amd, https://github.com/jeffdaily
1 parent 47c8aa8 commit bd63ec4

File tree

6 files changed

+51
-119
lines changed

6 files changed

+51
-119
lines changed

caffe2/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ if(USE_ROCM)
606606
# caffe2_nvrtc's stubs to driver APIs are useful for HIP.
607607
# See NOTE [ ATen NVRTC Stub and HIP ]
608608
add_library(caffe2_nvrtc SHARED ${ATen_NVRTC_STUB_SRCS})
609-
target_link_libraries(caffe2_nvrtc ${PYTORCH_HIP_LIBRARIES} ${ROCM_HIPRTC_LIB})
609+
target_link_libraries(caffe2_nvrtc hip::amdhip64 hiprtc::hiprtc)
610610
target_include_directories(caffe2_nvrtc PRIVATE ${CMAKE_BINARY_DIR})
611611
target_compile_definitions(caffe2_nvrtc PRIVATE USE_ROCM __HIP_PLATFORM_AMD__)
612612
install(TARGETS caffe2_nvrtc DESTINATION "${TORCH_INSTALL_LIB_DIR}")

cmake/External/rccl.cmake

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ if(NOT __NCCL_INCLUDED)
77
if(rccl_FOUND)
88
message(STATUS "RCCL Found!")
99
add_library(__caffe2_nccl INTERFACE)
10-
target_link_libraries(__caffe2_nccl INTERFACE ${PYTORCH_RCCL_LIBRARIES})
11-
target_include_directories(__caffe2_nccl INTERFACE ${RCCL_INCLUDE_DIRS})
10+
target_link_libraries(__caffe2_nccl INTERFACE roc::rccl)
1211
else()
1312
message(STATUS "RCCL NOT Found!")
1413
endif()

cmake/public/LoadHIP.cmake

+41-108
Original file line numberDiff line numberDiff line change
@@ -44,78 +44,60 @@ endif()
4444
message("Building PyTorch for GPU arch: ${PYTORCH_ROCM_ARCH}")
4545

4646
# Add HIP to the CMAKE Module Path
47+
# needed because the find_package call to this module uses the Module mode search
48+
# https://cmake.org/cmake/help/latest/command/find_package.html#search-modes
4749
set(CMAKE_MODULE_PATH ${ROCM_PATH}/lib/cmake/hip ${CMAKE_MODULE_PATH})
4850

51+
# Add ROCM_PATH to CMAKE_PREFIX_PATH, needed because the find_package
52+
# call to individual ROCM components uses the Config mode search
53+
list(APPEND CMAKE_PREFIX_PATH ${ROCM_PATH})
54+
4955
macro(find_package_and_print_version PACKAGE_NAME)
5056
find_package("${PACKAGE_NAME}" ${ARGN})
5157
message("${PACKAGE_NAME} VERSION: ${${PACKAGE_NAME}_VERSION}")
5258
endmacro()
5359

5460
# Find the HIP Package
55-
find_package_and_print_version(HIP 1.0)
61+
# MODULE argument is added for clarity that CMake is searching
62+
# for FindHIP.cmake in Module mode
63+
find_package_and_print_version(HIP 1.0 MODULE)
5664

5765
if(HIP_FOUND)
5866
set(PYTORCH_FOUND_HIP TRUE)
59-
set(FOUND_ROCM_VERSION_H FALSE)
60-
61-
set(PROJECT_RANDOM_BINARY_DIR "${PROJECT_BINARY_DIR}")
62-
set(file "${PROJECT_BINARY_DIR}/detect_rocm_version.cc")
6367

6468
# Find ROCM version for checks
65-
# ROCM 5.0 and later will have header api for version management
66-
if(EXISTS ${ROCM_INCLUDE_DIRS}/rocm_version.h)
67-
set(FOUND_ROCM_VERSION_H TRUE)
68-
file(WRITE ${file} ""
69-
"#include <rocm_version.h>\n"
70-
)
71-
elseif(EXISTS ${ROCM_INCLUDE_DIRS}/rocm-core/rocm_version.h)
72-
set(FOUND_ROCM_VERSION_H TRUE)
73-
file(WRITE ${file} ""
74-
"#include <rocm-core/rocm_version.h>\n"
75-
)
69+
if(EXISTS ${ROCM_INCLUDE_DIRS}/rocm-core/rocm_version.h)
70+
set(ROCM_HEADER_FILE ${ROCM_INCLUDE_DIRS}/rocm-core/rocm_version.h)
7671
else()
77-
message("********************* rocm_version.h couldnt be found ******************\n")
78-
endif()
79-
80-
if(FOUND_ROCM_VERSION_H)
81-
file(APPEND ${file} ""
82-
"#include <cstdio>\n"
83-
84-
"#ifndef ROCM_VERSION_PATCH\n"
85-
"#define ROCM_VERSION_PATCH 0\n"
86-
"#endif\n"
87-
"#define STRINGIFYHELPER(x) #x\n"
88-
"#define STRINGIFY(x) STRINGIFYHELPER(x)\n"
89-
"int main() {\n"
90-
" printf(\"%d.%d.%s\", ROCM_VERSION_MAJOR, ROCM_VERSION_MINOR, STRINGIFY(ROCM_VERSION_PATCH));\n"
91-
" return 0;\n"
92-
"}\n"
93-
)
94-
95-
try_run(run_result compile_result ${PROJECT_RANDOM_BINARY_DIR} ${file}
96-
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${ROCM_INCLUDE_DIRS}"
97-
RUN_OUTPUT_VARIABLE rocm_version_from_header
98-
COMPILE_OUTPUT_VARIABLE output_var
99-
)
100-
# We expect the compile to be successful if the include directory exists.
101-
if(NOT compile_result)
102-
message(FATAL_ERROR "Caffe2: Couldn't determine version from header: " ${output_var})
103-
endif()
104-
message(STATUS "Caffe2: Header version is: " ${rocm_version_from_header})
105-
set(ROCM_VERSION_DEV_RAW ${rocm_version_from_header})
106-
message("\n***** ROCm version from rocm_version.h ****\n")
107-
endif()
108-
109-
string(REGEX MATCH "^([0-9]+)\.([0-9]+)\.([0-9]+).*$" ROCM_VERSION_DEV_MATCH ${ROCM_VERSION_DEV_RAW})
110-
111-
if(ROCM_VERSION_DEV_MATCH)
112-
set(ROCM_VERSION_DEV_MAJOR ${CMAKE_MATCH_1})
113-
set(ROCM_VERSION_DEV_MINOR ${CMAKE_MATCH_2})
114-
set(ROCM_VERSION_DEV_PATCH ${CMAKE_MATCH_3})
115-
set(ROCM_VERSION_DEV "${ROCM_VERSION_DEV_MAJOR}.${ROCM_VERSION_DEV_MINOR}.${ROCM_VERSION_DEV_PATCH}")
116-
math(EXPR ROCM_VERSION_DEV_INT "(${ROCM_VERSION_DEV_MAJOR}*10000) + (${ROCM_VERSION_DEV_MINOR}*100) + ${ROCM_VERSION_DEV_PATCH}")
72+
message(FATAL_ERROR "********************* rocm_version.h could not be found ******************\n")
11773
endif()
11874

75+
# Read the ROCM headerfile into a variable
76+
file(READ ${ROCM_HEADER_FILE} ROCM_HEADER_CONTENT)
77+
78+
# Below we use a RegEx to find ROCM version numbers.
79+
# Note that CMake does not support \s for blank space. That is
80+
# why in the regular expressions below we have a blank space in
81+
# the square brackets.
82+
# There are three steps:
83+
# 1. Match regular expression
84+
# 2. Strip the non-numerical part of the string
85+
# 3. Strip leading and trailing spaces
86+
string(REGEX MATCH "ROCM_VERSION_MAJOR[ ]+[0-9]+" TEMP1 ${ROCM_HEADER_CONTENT})
87+
string(REPLACE "ROCM_VERSION_MAJOR" "" TEMP2 ${TEMP1})
88+
string(STRIP ${TEMP2} ROCM_VERSION_DEV_MAJOR)
89+
string(REGEX MATCH "ROCM_VERSION_MINOR[ ]+[0-9]+" TEMP1 ${ROCM_HEADER_CONTENT})
90+
string(REPLACE "ROCM_VERSION_MINOR" "" TEMP2 ${TEMP1})
91+
string(STRIP ${TEMP2} ROCM_VERSION_DEV_MINOR)
92+
string(REGEX MATCH "ROCM_VERSION_PATCH[ ]+[0-9]+" TEMP1 ${ROCM_HEADER_CONTENT})
93+
string(REPLACE "ROCM_VERSION_PATCH" "" TEMP2 ${TEMP1})
94+
string(STRIP ${TEMP2} ROCM_VERSION_DEV_PATCH)
95+
96+
# Create ROCM_VERSION_DEV_INT which is later used as a preprocessor macros
97+
set(ROCM_VERSION_DEV "${ROCM_VERSION_DEV_MAJOR}.${ROCM_VERSION_DEV_MINOR}.${ROCM_VERSION_DEV_PATCH}")
98+
math(EXPR ROCM_VERSION_DEV_INT "(${ROCM_VERSION_DEV_MAJOR}*10000) + (${ROCM_VERSION_DEV_MINOR}*100) + ${ROCM_VERSION_DEV_PATCH}")
99+
100+
message("\n***** ROCm version from rocm_version.h ****\n")
119101
message("ROCM_VERSION_DEV: ${ROCM_VERSION_DEV}")
120102
message("ROCM_VERSION_DEV_MAJOR: ${ROCM_VERSION_DEV_MAJOR}")
121103
message("ROCM_VERSION_DEV_MINOR: ${ROCM_VERSION_DEV_MINOR}")
@@ -127,42 +109,9 @@ if(HIP_FOUND)
127109
message("HIP_VERSION_MINOR: ${HIP_VERSION_MINOR}")
128110
message("TORCH_HIP_VERSION: ${TORCH_HIP_VERSION}")
129111

130-
message("\n***** Library versions from dpkg *****\n")
131-
execute_process(COMMAND dpkg -l COMMAND grep rocm-dev COMMAND awk "{print $2 \" VERSION: \" $3}")
132-
execute_process(COMMAND dpkg -l COMMAND grep rocm-libs COMMAND awk "{print $2 \" VERSION: \" $3}")
133-
execute_process(COMMAND dpkg -l COMMAND grep hsakmt-roct COMMAND awk "{print $2 \" VERSION: \" $3}")
134-
execute_process(COMMAND dpkg -l COMMAND grep rocr-dev COMMAND awk "{print $2 \" VERSION: \" $3}")
135-
execute_process(COMMAND dpkg -l COMMAND grep -w hcc COMMAND awk "{print $2 \" VERSION: \" $3}")
136-
execute_process(COMMAND dpkg -l COMMAND grep hip-base COMMAND awk "{print $2 \" VERSION: \" $3}")
137-
execute_process(COMMAND dpkg -l COMMAND grep hip_hcc COMMAND awk "{print $2 \" VERSION: \" $3}")
138-
112+
# Find ROCM components using Config mode
113+
# These components will be searced for recursively in ${ROCM_PATH}
139114
message("\n***** Library versions from cmake find_package *****\n")
140-
141-
set(CMAKE_HIP_CLANG_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
142-
set(CMAKE_HIP_CLANG_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
143-
### Remove setting of Flags when FindHIP.CMake PR #558 is accepted.###
144-
145-
set(hip_DIR ${ROCM_PATH}/lib/cmake/hip)
146-
set(hsa-runtime64_DIR ${ROCM_PATH}/lib/cmake/hsa-runtime64)
147-
set(AMDDeviceLibs_DIR ${ROCM_PATH}/lib/cmake/AMDDeviceLibs)
148-
set(amd_comgr_DIR ${ROCM_PATH}/lib/cmake/amd_comgr)
149-
set(rocrand_DIR ${ROCM_PATH}/lib/cmake/rocrand)
150-
set(hiprand_DIR ${ROCM_PATH}/lib/cmake/hiprand)
151-
set(rocblas_DIR ${ROCM_PATH}/lib/cmake/rocblas)
152-
set(hipblas_DIR ${ROCM_PATH}/lib/cmake/hipblas)
153-
set(hipblaslt_DIR ${ROCM_PATH}/lib/cmake/hipblaslt)
154-
set(miopen_DIR ${ROCM_PATH}/lib/cmake/miopen)
155-
set(rocfft_DIR ${ROCM_PATH}/lib/cmake/rocfft)
156-
set(hipfft_DIR ${ROCM_PATH}/lib/cmake/hipfft)
157-
set(hipsparse_DIR ${ROCM_PATH}/lib/cmake/hipsparse)
158-
set(rccl_DIR ${ROCM_PATH}/lib/cmake/rccl)
159-
set(rocprim_DIR ${ROCM_PATH}/lib/cmake/rocprim)
160-
set(hipcub_DIR ${ROCM_PATH}/lib/cmake/hipcub)
161-
set(rocthrust_DIR ${ROCM_PATH}/lib/cmake/rocthrust)
162-
set(hipsolver_DIR ${ROCM_PATH}/lib/cmake/hipsolver)
163-
set(hiprtc_DIR ${ROCM_PATH}/lib/cmake/hiprtc)
164-
165-
166115
find_package_and_print_version(hip REQUIRED)
167116
find_package_and_print_version(hsa-runtime64 REQUIRED)
168117
find_package_and_print_version(amd_comgr REQUIRED)
@@ -181,27 +130,11 @@ if(HIP_FOUND)
181130
find_package_and_print_version(hipsolver REQUIRED)
182131
find_package_and_print_version(hiprtc REQUIRED)
183132

184-
185-
find_library(PYTORCH_HIP_LIBRARIES amdhip64 HINTS ${ROCM_PATH}/lib)
186-
# TODO: miopen_LIBRARIES should return fullpath to the library file,
187-
# however currently it's just the lib name
188-
if(TARGET ${miopen_LIBRARIES})
189-
set(PYTORCH_MIOPEN_LIBRARIES ${miopen_LIBRARIES})
190-
else()
191-
find_library(PYTORCH_MIOPEN_LIBRARIES ${miopen_LIBRARIES} HINTS ${ROCM_PATH}/lib)
192-
endif()
193-
# TODO: rccl_LIBRARIES should return fullpath to the library file,
194-
# however currently it's just the lib name
195-
if(TARGET ${rccl_LIBRARIES})
196-
set(PYTORCH_RCCL_LIBRARIES ${rccl_LIBRARIES})
197-
else()
198-
find_library(PYTORCH_RCCL_LIBRARIES ${rccl_LIBRARIES} HINTS ${ROCM_PATH}/lib)
199-
endif()
200-
find_library(ROCM_HIPRTC_LIB hiprtc HINTS ${ROCM_PATH}/lib)
201133
# roctx is part of roctracer
202134
find_library(ROCM_ROCTX_LIB roctx64 HINTS ${ROCM_PATH}/lib)
203135

204136
# check whether HIP declares new types
137+
set(PROJECT_RANDOM_BINARY_DIR "${PROJECT_BINARY_DIR}")
205138
set(file "${PROJECT_BINARY_DIR}/hip_new_types.cc")
206139
file(WRITE ${file} ""
207140
"#include <hip/library_types.h>\n"

test/cpp/jit/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ if(USE_CUDA)
143143
endif()
144144
elseif(USE_ROCM)
145145
target_link_libraries(test_jit PRIVATE
146-
${ROCM_HIPRTC_LIB}
147-
${PYTORCH_HIP_LIBRARIES}
146+
hiprtc::hiprtc
147+
hip::amdhip64
148148
${TORCH_CUDA_LIBRARIES})
149149

150150
target_compile_definitions(test_jit PRIVATE USE_ROCM)

test/cpp/lazy/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ if(USE_CUDA)
3636
target_compile_definitions(test_lazy PRIVATE USE_CUDA)
3737
elseif(USE_ROCM)
3838
target_link_libraries(test_lazy PRIVATE
39-
${ROCM_HIPRTC_LIB}
40-
${PYTORCH_HIP_LIBRARIES}
39+
hiprtc::hiprtc
40+
hip::amdhip64
4141
${TORCH_CUDA_LIBRARIES})
4242

4343
target_compile_definitions(test_lazy PRIVATE USE_ROCM)

test/cpp/tensorexpr/CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ if(USE_CUDA)
5858
target_compile_definitions(tutorial_tensorexpr PRIVATE USE_CUDA)
5959
elseif(USE_ROCM)
6060
target_link_libraries(test_tensorexpr PRIVATE
61-
${ROCM_HIPRTC_LIB}
62-
${PYTORCH_HIP_LIBRARIES}
61+
hiprtc::hiprtc
62+
hip::amdhip64
6363
${TORCH_CUDA_LIBRARIES})
6464
target_compile_definitions(test_tensorexpr PRIVATE USE_ROCM)
6565

6666
target_link_libraries(tutorial_tensorexpr PRIVATE
67-
${ROCM_HIPRTC_LIB}
68-
${PYTORCH_HIP_LIBRARIES}
67+
hiprtc::hiprtc
68+
hip::amdhip64
6969
${TORCH_CUDA_LIBRARIES})
7070
target_compile_definitions(tutorial_tensorexpr PRIVATE USE_ROCM)
7171
endif()

0 commit comments

Comments
 (0)