[ROCM] Add ROCM version api within cmake (#69481)

Summary:
In ROCm 5.0 and later the version of the ROCm platform can be obtained via
an api call vs reading from a flat file.

If the header file /opt/rocm/include/rocm_version.h exists,
LoadHIP.cmake compiles source referencing the api and prints out the
ROCM Versions.

If the file does not exist, LoadHIP.cmake will revert to the previous
approach of looking for the version-dev file.

Fixes #{issue number}

cc jeffdaily sunway513 jithunnair-amd ROCmSupport KyleCZH

Pull Request resolved: https://github.com/pytorch/pytorch/pull/69481

Reviewed By: seemethere, janeyx99

Differential Revision: D34153435

Pulled By: malfet

fbshipit-source-id: f8c0650d27666d2a3cf47d812807798c47210b37
This commit is contained in:
Douglas Lehr 2022-02-10 16:09:41 -08:00 committed by Facebook GitHub Bot
parent ffe8a0e41b
commit 6cbb4f7a0c

View File

@ -5,7 +5,11 @@ if(NOT DEFINED ENV{ROCM_PATH})
else()
set(ROCM_PATH $ENV{ROCM_PATH})
endif()
if(NOT DEFINED ENV{ROCM_INCLUDE_DIRS})
set(ROCM_INCLUDE_DIRS ${ROCM_PATH}/include)
else()
set(ROCM_INCLUDE_DIRS $ENV{ROCM_INCLUDE_DIRS})
endif()
# HIP_PATH
if(NOT DEFINED ENV{HIP_PATH})
set(HIP_PATH ${ROCM_PATH}/hip)
@ -151,8 +155,47 @@ if(HIP_FOUND)
set(PYTORCH_FOUND_HIP TRUE)
# Find ROCM version for checks
file(READ "${ROCM_PATH}/.info/version-dev" ROCM_VERSION_DEV_RAW)
string(REGEX MATCH "^([0-9]+)\.([0-9]+)\.([0-9]+)-.*$" ROCM_VERSION_DEV_MATCH ${ROCM_VERSION_DEV_RAW})
# ROCM 5.0 and later will have header api for version management
if(EXISTS ${ROCM_INCLUDE_DIRS}/rocm_version.h)
set(PROJECT_RANDOM_BINARY_DIR "${PROJECT_BINARY_DIR}")
set(file "${PROJECT_BINARY_DIR}/detect_rocm_version.cc")
file(WRITE ${file} ""
"#include <rocm_version.h>\n"
"#include <cstdio>\n"
"#ifndef ROCM_VERSION_PATCH\n"
"#define ROCM_VERSION_PATCH 0\n"
"#endif\n"
"#define STRINGIFYHELPER(x) #x\n"
"#define STRINGIFY(x) STRINGIFYHELPER(x)\n"
"int main() {\n"
" printf(\"%d.%d.%s\", ROCM_VERSION_MAJOR, ROCM_VERSION_MINOR, STRINGIFY(ROCM_VERSION_PATCH));\n"
" return 0;\n"
"}\n"
)
try_run(run_result compile_result ${PROJECT_RANDOM_BINARY_DIR} ${file}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${ROCM_INCLUDE_DIRS}"
RUN_OUTPUT_VARIABLE rocm_version_from_header
COMPILE_OUTPUT_VARIABLE output_var
)
# We expect the compile to be successful if the include directory exists.
if(NOT compile_result)
message(FATAL_ERROR "Caffe2: Couldn't determine version from header: " ${output_var})
endif()
message(STATUS "Caffe2: Header version is: " ${rocm_version_from_header})
set(ROCM_VERSION_DEV_RAW ${rocm_version_from_header})
message("\n***** ROCm version from rocm_version.h ****\n")
# ROCM < 4.5, we don't have the header api file, use flat file
else()
file(READ "${ROCM_PATH}/.info/version-dev" ROCM_VERSION_DEV_RAW)
message("\n***** ROCm version from ${ROCM_PATH}/.info/version-dev ****\n")
endif()
string(REGEX MATCH "^([0-9]+)\.([0-9]+)\.([0-9]+).*$" ROCM_VERSION_DEV_MATCH ${ROCM_VERSION_DEV_RAW})
if(ROCM_VERSION_DEV_MATCH)
set(ROCM_VERSION_DEV_MAJOR ${CMAKE_MATCH_1})
set(ROCM_VERSION_DEV_MINOR ${CMAKE_MATCH_2})
@ -160,7 +203,7 @@ if(HIP_FOUND)
set(ROCM_VERSION_DEV "${ROCM_VERSION_DEV_MAJOR}.${ROCM_VERSION_DEV_MINOR}.${ROCM_VERSION_DEV_PATCH}")
math(EXPR ROCM_VERSION_DEV_INT "(${ROCM_VERSION_DEV_MAJOR}*10000) + (${ROCM_VERSION_DEV_MINOR}*100) + ${ROCM_VERSION_DEV_PATCH}")
endif()
message("\n***** ROCm version from ${ROCM_PATH}/.info/version-dev ****\n")
message("ROCM_VERSION_DEV: ${ROCM_VERSION_DEV}")
message("ROCM_VERSION_DEV_MAJOR: ${ROCM_VERSION_DEV_MAJOR}")
message("ROCM_VERSION_DEV_MINOR: ${ROCM_VERSION_DEV_MINOR}")