Skip to content

Commit ffe10f9

Browse files
authored
cmake: use modern HIP language (#2857)
Use modern `HIP` language instead of `hip_add_library`. Use modern `find_package` to find `hip` and `hipcub` instead of `FindROCM.cmake` (which is removed in this PR). --------- Signed-off-by: Jinzhe Zeng <[email protected]>
1 parent aa3b58e commit ffe10f9

File tree

6 files changed

+26
-99
lines changed

6 files changed

+26
-99
lines changed

backend/read_env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def get_argument_from_env() -> Tuple[str, list, list, dict, str]:
5757
if rocm_root:
5858
cmake_args.append(f"-DCMAKE_HIP_COMPILER_ROCM_ROOT:STRING={rocm_root}")
5959
hipcc_flags = os.environ.get("HIP_HIPCC_FLAGS")
60-
if hipcc_flags:
61-
cmake_args.append(f"-DHIP_HIPCC_FLAGS:STRING={hipcc_flags}")
60+
if hipcc_flags is not None:
61+
os.environ["HIPFLAGS"] = os.environ.get("HIPFLAGS", "") + " " + hipcc_flags
6262
else:
6363
raise RuntimeError("Unsupported DP_VARIANT option: %s" % dp_variant)
6464

doc/install/install-from-source.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ One may set the following environment variables before executing `pip`:
7979
| TENSORFLOW_ROOT | Path | Detected automatically | The path to TensorFlow Python library. By default the installer only finds TensorFlow under user site-package directory (`site.getusersitepackages()`) or system site-package directory (`sysconfig.get_path("purelib")`) due to limitation of [PEP-517](https://peps.python.org/pep-0517/). If not found, the latest TensorFlow (or the environment variable `TENSORFLOW_VERSION` if given) from PyPI will be built against.|
8080
| DP_ENABLE_NATIVE_OPTIMIZATION | 0, 1 | 0 | Enable compilation optimization for the native machine's CPU type. Do not enable it if generated code will run on different CPUs. |
8181
| CMAKE_ARGS | str | - | Additional CMake arguments |
82+
| &lt;LANG&gt;FLAGS (`<LANG>`=`CXX`, `CUDA` or `HIP`) | str | - | Default compilation flags to be used when compiling `<LANG>` files. See [CMake documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html). |
8283

8384
To test the installation, one should first jump out of the source directory
8485
```
@@ -193,7 +194,8 @@ One may add the following arguments to `cmake`:
193194
| -DCMAKE_HIP_COMPILER_ROCM_ROOT=&lt;value&gt; | Path | Detected automatically | The path to the ROCM toolkit directory. |
194195
| -DLAMMPS_SOURCE_ROOT=&lt;value&gt; | Path | - | Only neccessary for LAMMPS plugin mode. The path to the [LAMMPS source code](install-lammps.md). LAMMPS 8Apr2021 or later is supported. If not assigned, the plugin mode will not be enabled. |
195196
| -DUSE_TF_PYTHON_LIBS=&lt;value&gt; | `TRUE` or `FALSE` | `FALSE` | If `TRUE`, Build C++ interface with TensorFlow's Python libraries(TensorFlow's Python Interface is required). And there's no need for building TensorFlow's C++ interface.|
196-
| -DENABLE_NATIVE_OPTIMIZATION | `TRUE` or `FALSE` | `FALSE` | Enable compilation optimization for the native machine's CPU type. Do not enable it if generated code will run on different CPUs. |
197+
| -DENABLE_NATIVE_OPTIMIZATION=&lt;value&gt; | `TRUE` or `FALSE` | `FALSE` | Enable compilation optimization for the native machine's CPU type. Do not enable it if generated code will run on different CPUs. |
198+
| -DCMAKE_&lt;LANG&gt;_FLAGS=&lt;value&gt; (`<LANG>`=`CXX`, `CUDA` or `HIP`) | str | - | Default compilation flags to be used when compiling `<LANG>` files. See [CMake documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html). |
197199

198200
If the CMake has been executed successfully, then run the following make commands to build the package:
199201
```bash

source/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@ endif(USE_CUDA_TOOLKIT)
111111
# define USE_ROCM_TOOLKIT
112112
if(USE_ROCM_TOOLKIT)
113113
cmake_minimum_required(VERSION 3.21)
114-
find_package(ROCM REQUIRED)
114+
include(CMakeDetermineHIPCompiler)
115+
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_HIP_COMPILER_ROCM_ROOT})
116+
find_package(hip REQUIRED)
117+
find_package(hipcub REQUIRED)
115118
add_definitions("-DTENSORFLOW_USE_ROCM")
116-
add_compile_definitions(__HIP_PLATFORM_HCC__)
117-
message(STATUS "Found ROCM in ${ROCM_ROOT}, build AMD GPU support")
119+
message(
120+
STATUS
121+
"Found ROCM in ${CMAKE_HIP_COMPILER_ROCM_ROOT}, build AMD GPU support")
118122
set(DP_VARIANT "rocm")
119123
else()
120124
message(STATUS "Will not build AMD GPU support")

source/cmake/FindROCM.cmake

Lines changed: 0 additions & 84 deletions
This file was deleted.

source/lib/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ if(USE_ROCM_TOOLKIT)
2424
add_definitions("-DTENSORFLOW_USE_ROCM")
2525
add_subdirectory(src/gpu)
2626
set(EXTRA_LIBS ${EXTRA_LIBS} deepmd_op_rocm)
27-
target_link_libraries(${libname} INTERFACE ${ROCM_LIBRARIES} ${EXTRA_LIBS})
27+
# to define __HIP_PLATFORM_AMD__ in hip_runtime.h
28+
target_link_libraries(${libname} PUBLIC hip::host)
29+
target_link_libraries(${libname} INTERFACE ${EXTRA_LIBS})
2830
# gpu_rocm.h
2931
target_include_directories(
30-
${libname} PUBLIC $<BUILD_INTERFACE:${ROCM_INCLUDE_DIRS}>
32+
${libname} PUBLIC $<BUILD_INTERFACE:${HIP_INCLUDE_DIRS}>
3133
$<INSTALL_INTERFACE:include>)
3234
endif()
3335

source/lib/src/gpu/CMakeLists.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,29 @@ elseif(USE_ROCM_TOOLKIT)
5050
cmake_minimum_required(VERSION 3.21)
5151
# project name
5252
project(deepmd_op_rocm)
53+
enable_language(HIP)
5354
set(GPU_LIB_NAME deepmd_op_rocm)
5455
set(CMAKE_LINK_WHAT_YOU_USE TRUE)
5556

5657
# set c++ version c++11
5758
set(CMAKE_CXX_STANDARD 14)
5859
set(CMAKE_HIP_STANDARD 14)
5960
add_definitions("-DCUB_IGNORE_DEPRECATED_CPP_DIALECT")
60-
add_definitions("-DCUB_IGNORE_DEPRECATED_CPP_DIALECT")
6161

62-
message(STATUS "HIP major version is " ${HIP_VERSION_MAJOR})
62+
message(STATUS "HIP major version is " ${hip_VERSION_MAJOR})
6363

64-
set(HIP_HIPCC_FLAGS -fno-gpu-rdc; -fPIC --std=c++14 ${HIP_HIPCC_FLAGS}
65-
)# --amdgpu-target=gfx906
66-
if(HIP_VERSION VERSION_LESS 3.5.1)
67-
set(HIP_HIPCC_FLAGS -hc; ${HIP_HIPCC_FLAGS})
64+
set(CMAKE_HIP_FLAGS -fno-gpu-rdc ${CMAKE_HIP_FLAGS}) # --amdgpu-target=gfx906
65+
if(hip_VERSION VERSION_LESS 3.5.1)
66+
set(CMAKE_HIP_FLAGS -hc ${CMAKE_HIP_FLAGS})
6867
endif()
6968

7069
file(GLOB SOURCE_FILES "*.cu")
7170

72-
hip_add_library(${GPU_LIB_NAME} SHARED ${SOURCE_FILES})
71+
add_library(${GPU_LIB_NAME} SHARED ${SOURCE_FILES})
72+
set_source_files_properties(${SOURCE_FILES} PROPERTIES LANGUAGE HIP)
73+
# -fpic
74+
set_property(TARGET ${GPU_LIB_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
75+
target_link_libraries(${GPU_LIB_NAME} PRIVATE hip::hipcub)
7376

7477
endif()
7578

0 commit comments

Comments
 (0)