diff --git a/backend/read_env.py b/backend/read_env.py index 575c1a57de..079211d4d7 100644 --- a/backend/read_env.py +++ b/backend/read_env.py @@ -57,8 +57,8 @@ def get_argument_from_env() -> Tuple[str, list, list, dict, str]: if rocm_root: cmake_args.append(f"-DCMAKE_HIP_COMPILER_ROCM_ROOT:STRING={rocm_root}") hipcc_flags = os.environ.get("HIP_HIPCC_FLAGS") - if hipcc_flags: - cmake_args.append(f"-DHIP_HIPCC_FLAGS:STRING={hipcc_flags}") + if hipcc_flags is not None: + os.environ["HIPFLAGS"] = os.environ.get("HIPFLAGS", "") + " " + hipcc_flags else: raise RuntimeError("Unsupported DP_VARIANT option: %s" % dp_variant) diff --git a/doc/install/install-from-source.md b/doc/install/install-from-source.md index 1447823c08..ef3f5d9c36 100644 --- a/doc/install/install-from-source.md +++ b/doc/install/install-from-source.md @@ -79,6 +79,7 @@ One may set the following environment variables before executing `pip`: | 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.| | 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. | | CMAKE_ARGS | str | - | Additional CMake arguments | +| <LANG>FLAGS (``=`CXX`, `CUDA` or `HIP`) | str | - | Default compilation flags to be used when compiling `` files. See [CMake documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html). | To test the installation, one should first jump out of the source directory ``` @@ -193,7 +194,8 @@ One may add the following arguments to `cmake`: | -DCMAKE_HIP_COMPILER_ROCM_ROOT=<value> | Path | Detected automatically | The path to the ROCM toolkit directory. | | -DLAMMPS_SOURCE_ROOT=<value> | 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. | | -DUSE_TF_PYTHON_LIBS=<value> | `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.| -| -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. | +| -DENABLE_NATIVE_OPTIMIZATION=<value> | `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. | +| -DCMAKE_<LANG>_FLAGS=<value> (``=`CXX`, `CUDA` or `HIP`) | str | - | Default compilation flags to be used when compiling `` files. See [CMake documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html). | If the CMake has been executed successfully, then run the following make commands to build the package: ```bash diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index c3b5fde054..8846779ae0 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -115,10 +115,14 @@ endif(USE_CUDA_TOOLKIT) # define USE_ROCM_TOOLKIT if(USE_ROCM_TOOLKIT) cmake_minimum_required(VERSION 3.21) - find_package(ROCM REQUIRED) + include(CMakeDetermineHIPCompiler) + list(APPEND CMAKE_PREFIX_PATH ${CMAKE_HIP_COMPILER_ROCM_ROOT}) + find_package(hip REQUIRED) + find_package(hipcub REQUIRED) add_definitions("-DTENSORFLOW_USE_ROCM") - add_compile_definitions(__HIP_PLATFORM_HCC__) - message(STATUS "Found ROCM in ${ROCM_ROOT}, build AMD GPU support") + message( + STATUS + "Found ROCM in ${CMAKE_HIP_COMPILER_ROCM_ROOT}, build AMD GPU support") set(DP_VARIANT "rocm") else() message(STATUS "Will not build AMD GPU support") diff --git a/source/cmake/FindROCM.cmake b/source/cmake/FindROCM.cmake deleted file mode 100644 index 6ef575ae39..0000000000 --- a/source/cmake/FindROCM.cmake +++ /dev/null @@ -1,84 +0,0 @@ -# Input: ROCM_ROOT -# -# Output: ROCM_FOUND ROCM_INCLUDE_DIRS ROCM_LIBRARIES - -# define the search path -cmake_minimum_required(VERSION 3.21) -include(CMakeDetermineHIPCompiler) -set(ROCM_PATH ${CMAKE_HIP_COMPILER_ROCM_ROOT}) -set(ROCM_search_PATHS ${CMAKE_HIP_COMPILER_ROCM_ROOT}) - -# includes -find_path( - ROCM_INCLUDE_DIRS - NAMES hip/hip_runtime.h rocprim/rocprim.hpp hipcub/hipcub.hpp - PATHS ${ROCM_search_PATHS} - PATH_SUFFIXES "include" - NO_DEFAULT_PATH) -if(NOT ROCM_INCLUDE_DIRS AND ROCM_FIND_REQUIRED) - message( - FATAL_ERROR - "Not found 'hip' or 'rocprim' or 'hipcub' directory in path '${ROCM_search_PATHS}' " - "You can manually set the ROCM install path by -DROCM_ROOT ") -endif() - -# FindHIP.cmake -find_path( - HIP_CMAKE - NAMES FindHIP.cmake - PATHS ${ROCM_search_PATHS} - PATH_SUFFIXES "hip/cmake" - NO_DEFAULT_PATH) - -if(NOT HIP_CMAKE AND ROCM_FIND_REQUIRED) - message( - FATAL_ERROR "Not found 'FindHIP.cmake' file in path '${ROCM_search_PATHS}' " - "You can manually set the ROCM install path by -DROCM_ROOT ") -endif() - -list(APPEND CMAKE_MODULE_PATH ${HIP_CMAKE}) -find_package(HIP) - -# define the libs to find -if(NOT ROCM_FIND_COMPONENTS) - if(HIP_VERSION VERSION_GREATER_EQUAL 3.5.1) - set(ROCM_FIND_COMPONENTS amd_comgr amdhip64) - else() - set(ROCM_FIND_COMPONENTS hip-hcc hiprtc) - endif() -endif() - -# libs -foreach(module ${ROCM_FIND_COMPONENTS}) - find_library( - ROCM_LIBRARIES_${module} - NAMES ${module} - PATHS ${ROCM_search_PATHS} - PATH_SUFFIXES "lib" - NO_DEFAULT_PATH) - if(ROCM_LIBRARIES_${module}) - list(APPEND ROCM_LIBRARIES ${ROCM_LIBRARIES_${module}}) - elseif(ROCM_FIND_REQUIRED) - message( - FATAL_ERROR "Not found lib/'${module}' in '${ROCM_search_PATHS}' " - "You can manually set the ROCM install path by -DROCM_ROOT ") - endif() -endforeach() - -# define the output variable -if(ROCM_INCLUDE_DIRS - AND ROCM_LIBRARIES - AND HIP_CMAKE) - set(ROCM_FOUND TRUE) -else() - set(ROCM_FOUND FALSE) -endif() - -# print message -if(NOT ROCM_FIND_QUIETLY) - message( - STATUS "Found ROCM: ${ROCM_INCLUDE_DIRS}, ${ROCM_LIBRARIES}, ${HIP_CMAKE}" - " in ${ROCM_search_PATHS}, build AMD GPU support") -endif() - -unset(ROCM_search_PATHS) diff --git a/source/lib/CMakeLists.txt b/source/lib/CMakeLists.txt index 323bf2d7c0..0f5bdb73fb 100644 --- a/source/lib/CMakeLists.txt +++ b/source/lib/CMakeLists.txt @@ -24,10 +24,12 @@ if(USE_ROCM_TOOLKIT) add_definitions("-DTENSORFLOW_USE_ROCM") add_subdirectory(src/gpu) set(EXTRA_LIBS ${EXTRA_LIBS} deepmd_op_rocm) - target_link_libraries(${libname} INTERFACE ${ROCM_LIBRARIES} ${EXTRA_LIBS}) + # to define __HIP_PLATFORM_AMD__ in hip_runtime.h + target_link_libraries(${libname} PUBLIC hip::host) + target_link_libraries(${libname} INTERFACE ${EXTRA_LIBS}) # gpu_rocm.h target_include_directories( - ${libname} PUBLIC $ + ${libname} PUBLIC $ $) endif() diff --git a/source/lib/src/gpu/CMakeLists.txt b/source/lib/src/gpu/CMakeLists.txt index 25223c82bf..4b491a312d 100644 --- a/source/lib/src/gpu/CMakeLists.txt +++ b/source/lib/src/gpu/CMakeLists.txt @@ -50,6 +50,7 @@ elseif(USE_ROCM_TOOLKIT) cmake_minimum_required(VERSION 3.21) # project name project(deepmd_op_rocm) + enable_language(HIP) set(GPU_LIB_NAME deepmd_op_rocm) set(CMAKE_LINK_WHAT_YOU_USE TRUE) @@ -57,19 +58,21 @@ elseif(USE_ROCM_TOOLKIT) set(CMAKE_CXX_STANDARD 14) set(CMAKE_HIP_STANDARD 14) add_definitions("-DCUB_IGNORE_DEPRECATED_CPP_DIALECT") - add_definitions("-DCUB_IGNORE_DEPRECATED_CPP_DIALECT") - message(STATUS "HIP major version is " ${HIP_VERSION_MAJOR}) + message(STATUS "HIP major version is " ${hip_VERSION_MAJOR}) - set(HIP_HIPCC_FLAGS -fno-gpu-rdc; -fPIC --std=c++14 ${HIP_HIPCC_FLAGS} - )# --amdgpu-target=gfx906 - if(HIP_VERSION VERSION_LESS 3.5.1) - set(HIP_HIPCC_FLAGS -hc; ${HIP_HIPCC_FLAGS}) + set(CMAKE_HIP_FLAGS -fno-gpu-rdc ${CMAKE_HIP_FLAGS}) # --amdgpu-target=gfx906 + if(hip_VERSION VERSION_LESS 3.5.1) + set(CMAKE_HIP_FLAGS -hc ${CMAKE_HIP_FLAGS}) endif() file(GLOB SOURCE_FILES "*.cu") - hip_add_library(${GPU_LIB_NAME} SHARED ${SOURCE_FILES}) + add_library(${GPU_LIB_NAME} SHARED ${SOURCE_FILES}) + set_source_files_properties(${SOURCE_FILES} PROPERTIES LANGUAGE HIP) + # -fpic + set_property(TARGET ${GPU_LIB_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON) + target_link_libraries(${GPU_LIB_NAME} PRIVATE hip::hipcub) endif()