Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ROCM to the DGL build #3

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ __pycache__/

# Distribution / packaging
.Python
build/
build
build-*/
out/
CMakeUserPresets.json
dataset/
datasets/
develop-eggs/
Expand Down Expand Up @@ -159,6 +162,7 @@ cscope.*
# vscode
.clangd
.vscode
*.code-workspace

# asv
.asv
Expand Down
94 changes: 62 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(cmake/util/Util.cmake)
include(cmake/util/MshadowUtil.cmake)
include(cmake/util/FindCUDA.cmake)
include(cmake/util/FindROCm.cmake)

# Options for building DGL.
# NOTE: Please avoid editing this file to change build type. Instead, using
Expand All @@ -20,6 +21,7 @@ dgl_option(BUILD_TYPE "Type of the build: dev, dogfood or release" "dev")
message(STATUS "Build for ${BUILD_TYPE}")

dgl_option(USE_CUDA "Build with CUDA" OFF)
dgl_option(USE_ROCM "Build with ROCM" OFF)
dgl_option(TORCH_PYTHON_INTERPS "Python interpreter for building sub-components" python3)

# Conda build related options.
Expand Down Expand Up @@ -109,7 +111,7 @@ dgl_feature_option(
if (EXTERNAL_DLPACK_PATH OR EXTERNAL_DMLC_PATH OR EXTERNAL_NANOFLANN_PATH OR EXTERNAL_NANOFLANN_PATH OR EXTERNAL_METIS_PATH OR EXTERNAL_GKLIB_PATH)
message(STATUS "Using at least one external library")
set(USE_EXTERNAL_LIBS ON)

if (BUILD_CPP_TEST)
message(FATAL_ERROR "Cannot build cpp unittests with external libraries")
endif(BUILD_CPP_TEST)
Expand Down Expand Up @@ -146,6 +148,12 @@ if(USE_CUDA)
cuda_include_directories(BEFORE "${CMAKE_SOURCE_DIR}/third_party/cccl/libcudacxx/include")
endif(USE_CUDA)

if(USE_ROCM)
message(STATUS "Build with ROCm support")
project(dgl C CXX)
include(cmake/modules/ROCm.cmake)
endif(USE_ROCM)

# initial variables
if(NOT MSVC)
set(DGL_LINKER_LIBS "dl")
Expand Down Expand Up @@ -224,28 +232,35 @@ if(USE_OPENMP)
message(STATUS "Build with OpenMP.")
endif(USE_OPENMP)

file(GLOB_RECURSE DGL_CUDA_SRC
src/array/cuda/*.cc
src/array/cuda/*.cu
src/array/cuda/uvm/*.cc
src/array/cuda/uvm/*.cu
src/kernel/cuda/*.cc
src/kernel/cuda/*.cu
src/partition/cuda/*.cu
src/runtime/cuda/*.cc
src/runtime/cuda/*.cu
src/geometry/cuda/*.cu
src/graph/transform/cuda/*.cu
src/graph/sampling/randomwalks/*.cu
)

# Configure cuda
if(USE_CUDA)
file(GLOB_RECURSE DGL_CUDA_SRC
src/array/cuda/*.cc
src/array/cuda/*.cu
src/array/cuda/uvm/*.cc
src/array/cuda/uvm/*.cu
src/kernel/cuda/*.cc
src/kernel/cuda/*.cu
src/partition/cuda/*.cu
src/runtime/cuda/*.cc
src/runtime/cuda/*.cu
src/geometry/cuda/*.cu
src/graph/transform/cuda/*.cu
src/graph/sampling/randomwalks/*.cu
)
list(APPEND DGL_SRC ${DGL_CUDA_SRC})
dgl_config_cuda(DGL_LINKER_LIBS)
cuda_add_library(dgl SHARED ${DGL_SRC})
else(USE_CUDA)
elseif(USE_ROCM)
set(DGL_HIP_SRC "${DGL_CUDA_SRC}")
set_source_files_properties(${DGL_HIP_SRC} PROPERTIES LANGUAGE HIP)
list(APPEND DGL_SRC ${DGL_HIP_SRC})
dgl_config_rocm(DGL_LINKER_LIBS)
add_library(dgl SHARED ${DGL_SRC})
endif(USE_CUDA)
else()
add_library(dgl SHARED ${DGL_SRC})
endif()

if ((NOT MSVC) AND USE_EPOLL)
INCLUDE(CheckIncludeFile)
Expand Down Expand Up @@ -293,7 +308,7 @@ if(EXTERNAL_DMLC_PATH)
endif()
message(STATUS "looking for dmlc headers in ${EXTERNAL_DMLC_PATH}")
include_directories(SYSTEM ${EXTERNAL_DMLC_PATH})

if (NOT EXTERNAL_DMLC_LIB_PATH)
message(FATAL_ERROR "EXTERNAL_DMLC_LIB_PATH must be set if EXTERNAL_DMLC_PATH is set")
endif()
Expand Down Expand Up @@ -371,21 +386,26 @@ endif(EXTERNAL_METIS_PATH)

# Avoid exposing third-party symbols when using DGL as a library.
if((NOT MSVC) AND (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin"))
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--exclude-libs,ALL")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--exclude-libs,ALL")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs,ALL")
endif()

# Compile gpu_cache
file(GLOB gpu_cache_src
third_party/HugeCTR/gpu_cache/src/nv_gpu_cache.cu
)
# Manually build gpu_cache because CMake always builds it as shared
if(USE_CUDA)
# Manually build gpu_cache because CMake always builds it as shared
file(GLOB gpu_cache_src
third_party/HugeCTR/gpu_cache/src/nv_gpu_cache.cu
)
cuda_add_library(gpu_cache STATIC ${gpu_cache_src})
target_include_directories(gpu_cache PRIVATE "third_party/HugeCTR/gpu_cache/include")
target_include_directories(dgl PRIVATE "third_party/HugeCTR/gpu_cache/include")
list(APPEND DGL_LINKER_LIBS gpu_cache)
message(STATUS "Build with HugeCTR GPU embedding cache.")
elseif(USE_ROCM)
set_source_files_properties(${gpu_cache_src} PROPERTIES LANGUAGE HIP)
add_library(gpu_cache STATIC ${gpu_cache_src})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm aware we have a hip_add_library but I'm not sure if or when it is preferred over just add_library.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, the HIP docs just use add_library: https://rocm.docs.amd.com/en/latest/conceptual/cmake-packages.html#using-hip-in-cmake. I can look into hip_add_library, although this appears to work.

target_include_directories(gpu_cache PRIVATE "third_party/HugeCTR/gpu_cache/include")
target_include_directories(dgl PRIVATE "third_party/HugeCTR/gpu_cache/include")
list(APPEND DGL_LINKER_LIBS gpu_cache)
message(STATUS "Build with HugeCTR GPU embedding cache.")
endif(USE_CUDA)

# support PARALLEL_ALGORITHMS
Expand All @@ -407,7 +427,9 @@ endif(MSVC)
# of the wrong version when I build everything in the same CMake process. As
# a result, I (BarclayII) am launching an individual CMake build for every PyTorch version.
if(BUILD_TORCH)
message(STATUS "Configuring Torch build")
file(TO_NATIVE_PATH ${CMAKE_CURRENT_BINARY_DIR} BINDIR)
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR} SRCDIR)
file(TO_NATIVE_PATH ${CMAKE_COMMAND} CMAKE_CMD)
if(MSVC)
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/tensoradapter/pytorch/build.bat BUILD_SCRIPT)
Expand All @@ -428,13 +450,15 @@ if(BUILD_TORCH)
tensoradapter_pytorch
${CMAKE_COMMAND} -E env
CMAKE_COMMAND=${CMAKE_CMD}
GENERATOR=${CMAKE_GENERATOR}
CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
CUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR}
USE_CUDA=${USE_CUDA}
EXTERNAL_DMLC_LIB_PATH=${EXTERNAL_DMLC_LIB_PATH}
BINDIR=${CMAKE_CURRENT_BINARY_DIR}
USE_ROCM=${USE_ROCM}
BINDIR=${BINDIR}
SRCDIR=${SRCDIR}
bash ${BUILD_SCRIPT} ${TORCH_PYTHON_INTERPS}
DEPENDS ${BUILD_SCRIPT}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tensoradapter/pytorch)
DEPENDS ${BUILD_SCRIPT})
endif(MSVC)
add_dependencies(dgl tensoradapter_pytorch)
endif(BUILD_TORCH)
Expand Down Expand Up @@ -464,6 +488,7 @@ endif(BUILD_CPP_TEST)
if(BUILD_SPARSE)
message(STATUS "Configuring DGL sparse library")
file(TO_NATIVE_PATH ${CMAKE_CURRENT_BINARY_DIR} BINDIR)
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR} SRCDIR)
file(TO_NATIVE_PATH ${CMAKE_COMMAND} CMAKE_CMD)
get_target_property(DGL_INCLUDE_DIRS dgl INCLUDE_DIRECTORIES)
message(STATUS "DGL include directories: ${DGL_INCLUDE_DIRS}")
Expand Down Expand Up @@ -492,16 +517,20 @@ if(BUILD_SPARSE)
ALL
${CMAKE_COMMAND} -E env
CMAKE_COMMAND=${CMAKE_CMD}
GENERATOR=${CMAKE_GENERATOR}
CMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
CUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR}
USE_CUDA=${USE_CUDA}
BINDIR=${CMAKE_CURRENT_BINARY_DIR}
USE_ROCM=${USE_ROCM}
BINDIR=${BINDIR}
SRCDIR=${SRCDIR}
INCLUDEDIR="${DGL_INCLUDE_DIRS}"
EXTERNAL_DMLC_LIB_PATH=${EXTERNAL_DMLC_LIB_PATH}
CFLAGS=${CMAKE_C_FLAGS}
CXXFLAGS=${CMAKE_CXX_FLAGS}
LDFLAGS=${CMAKE_SHARED_LINKER_FLAGS}
bash ${BUILD_SCRIPT} ${TORCH_PYTHON_INTERPS}
DEPENDS ${BUILD_SCRIPT}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dgl_sparse)
DEPENDS ${BUILD_SCRIPT})
endif(MSVC)
add_dependencies(dgl_sparse dgl)
endif(BUILD_SPARSE)
Expand Down Expand Up @@ -537,6 +566,7 @@ if(BUILD_GRAPHBOLT)
CMAKE_COMMAND=${CMAKE_CMD}
CUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR}
USE_CUDA=${USE_CUDA}
USE_ROCM=${USE_ROCM}
USE_LIBURING=${USE_LIBURING}
BINDIR=${CMAKE_CURRENT_BINARY_DIR}
CFLAGS=${CMAKE_C_FLAGS}
Expand Down
33 changes: 33 additions & 0 deletions cmake/modules/ROCm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ROCm Module
if(USE_ROCM)
find_rocm(${USE_ROCM} REQUIRED)
else(USE_ROCM)
return()
endif()

###### Borrowed from MSHADOW project

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-std=c++17" SUPPORT_CXX17)

################################################################################################
# Config rocm compilation and append ROCm libraries to linker_libs
# Usage:
# dgl_config_rocm(linker_libs)
macro(dgl_config_rocm linker_libs)
if(NOT ROCM_FOUND)
message(FATAL_ERROR "Cannot find ROCm.")
endif()

enable_language(HIP)

add_definitions(-DDGL_USE_ROCM)
# We need the newest stuff that isn't turned on by default yet
add_definitions(-DHIP_ENABLE_WARP_SYNC_BUILTINS)

list(APPEND ${linker_libs}
hip::host
roc::hipblas
roc::hipsparse
hip::hiprand)
endmacro()
22 changes: 22 additions & 0 deletions cmake/util/FindROCm.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#######################################################
# Usage:
# find_rocm(${USE_ROCM})
#
# - When USE_ROCM=ON, use auto search
#
# Please use the CMAKE variable ROCM_HOME to set ROCm directory
#
# Provide variables:
#
# - ROCM_FOUND
#

macro(find_rocm use_rocm)
# ROCM prints out a nonsense version here from
# ROCmCMakeBuildToolsConfigVersion.cmake that is just going to confuse people
find_package(ROCM REQUIRED)
find_package_and_print_version(HIP REQUIRED)
find_package_and_print_version(hipBLAS REQUIRED)
find_package_and_print_version(hipRAND REQUIRED)
find_package_and_print_version(hipSPARSE REQUIRED)
endmacro(find_rocm)
5 changes: 5 additions & 0 deletions cmake/util/Util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ macro(dgl_option variable description value)
unset(${variable} CACHE)
endif()
endmacro()

macro(find_package_and_print_version PACKAGE_NAME)
find_package("${PACKAGE_NAME}" ${ARGN})
message("${PACKAGE_NAME} VERSION: ${${PACKAGE_NAME}_VERSION}")
endmacro()
3 changes: 3 additions & 0 deletions dgl_sparse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ set(SPARSE_LINKER_LIBS "")
if(USE_CUDA)
add_definitions(-DDGL_USE_CUDA)
enable_language(CUDA)
elseif(USE_ROCM)
add_definitions(-DDGL_USE_ROCM)
enable_language(HIP)
endif()

# For windows, define NOMINMAX to avoid conflict with std::min/max
Expand Down
57 changes: 35 additions & 22 deletions dgl_sparse/build.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
#!/bin/bash
# Helper script to build dgl sparse libraries for PyTorch
set -e
set -euo pipefail

mkdir -p build
mkdir -p $BINDIR/dgl_sparse
cd build
SPARSE_BINDIR="${BINDIR}/dgl_sparse"
SPARSE_SRCDIR="${SRCDIR}/dgl_sparse"
mkdir -p "${SPARSE_BINDIR}/build"
cd "${SPARSE_BINDIR}/build"

if [ $(uname) = 'Darwin' ]; then
CPSOURCE=*.dylib
CPSOURCE=*.dylib
else
CPSOURCE=*.so
CPSOURCE=*.so
fi

CMAKE_FLAGS="-DCUDA_TOOLKIT_ROOT_DIR=$CUDA_TOOLKIT_ROOT_DIR -DTORCH_CUDA_ARCH_LIST=$TORCH_CUDA_ARCH_LIST -DUSE_CUDA=$USE_CUDA -DEXTERNAL_DMLC_LIB_PATH=$EXTERNAL_DMLC_LIB_PATH"
# CMake passes in the list of directories separated by spaces. Here we replace them with semicolons.
CMAKE_FLAGS="$CMAKE_FLAGS -DDGL_INCLUDE_DIRS=${INCLUDEDIR// /;} -DDGL_BUILD_DIR=$BINDIR"
echo $CMAKE_FLAGS
declare -a CMAKE_FLAGS=(
"-G${GENERATOR}"
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
"-DCUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR}"
"-DTORCH_CUDA_ARCH_LIST=${TORCH_CUDA_ARCH_LIST:-}"
"-DUSE_CUDA=${USE_CUDA}"
"-DUSE_ROCM=${USE_ROCM}"
"-DEXTERNAL_DMLC_LIB_PATH=${EXTERNAL_DMLC_LIB_PATH:-}"
# CMake passes in the list of directories separated by spaces. Here we
# replace them with semicolons.
"-DDGL_INCLUDE_DIRS=${INCLUDEDIR// /;}"
"-DDGL_BUILD_DIR=${BINDIR}"
)
echo "DGL Sparse CMAKE_FLAGS: ${CMAKE_FLAGS[@]}"

if [ $# -eq 0 ]; then
$CMAKE_COMMAND $CMAKE_FLAGS ..
make -j
cp -v $CPSOURCE $BINDIR/dgl_sparse
"${CMAKE_COMMAND}" "${CMAKE_FLAGS[@]}" "${SPARSE_SRCDIR}"
cmake --build .
# CPSOURCE deliberately unquoted to expand wildcard
cp -v ${CPSOURCE} "${SPARSE_BINDIR}"
else
for PYTHON_INTERP in $@; do
TORCH_VER=$($PYTHON_INTERP -c 'import torch; print(torch.__version__.split("+")[0])')
mkdir -p $TORCH_VER
cd $TORCH_VER
$CMAKE_COMMAND $CMAKE_FLAGS -DPYTHON_INTERP=$PYTHON_INTERP ../..
make -j
cp -v $CPSOURCE $BINDIR/dgl_sparse
cd ..
done
for PYTHON_INTERP in "$@"; do
TORCH_VER="$("${PYTHON_INTERP}" -c 'import torch; print(torch.__version__.split("+")[0])')"
mkdir -p "${TORCH_VER}"
cd "${TORCH_VER}"
"${CMAKE_COMMAND}" "${CMAKE_FLAGS[@]}" -DPYTHON_INTERP="${PYTHON_INTERP}" "${SPARSE_SRCDIR}"
cmake --build .
# CPSOURCE deliberately unquoted to expand wildcard
cp -v ${CPSOURCE} "${SPARSE_BINDIR}"
cd ..
done
fi
Loading