Skip to content

Commit

Permalink
[Sparse] Change CMakeFiles to build DGL Sparse for multiple PyTorch v…
Browse files Browse the repository at this point in the history
…ersions (dmlc#5106)

* initial commit

* Update build.sh
  • Loading branch information
BarclayII authored Jan 6, 2023
1 parent 6069f34 commit 9a40d20
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 17 deletions.
45 changes: 42 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,46 @@ if(BUILD_CPP_TEST)
endif(BUILD_CPP_TEST)

if(BUILD_SPARSE)
set(DGL_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/include")
list(APPEND DGL_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/src")
add_subdirectory(dgl_sparse)
message(STATUS "Configuring DGL sparse library")
file(TO_NATIVE_PATH ${CMAKE_CURRENT_BINARY_DIR} BINDIR)
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}")
message(STATUS "DGL link directories: ${DGL_INCLUDE_DIRS}")
if(MSVC)
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/dgl_sparse/build.bat BUILD_SCRIPT)
add_custom_target(
dgl_sparse
ALL
${CMAKE_COMMAND} -E env
CMAKE_COMMAND=${CMAKE_CMD}
CUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR}
USE_CUDA=${USE_CUDA}
BINDIR=${BINDIR}
INCLUDEDIR="${DGL_INCLUDE_DIRS}"
CFLAGS=${CMAKE_C_FLAGS}
CXXFLAGS=${CMAKE_CXX_FLAGS}
LDFLAGS=${CMAKE_SHARED_LINKER_FLAGS}
cmd /e:on /c ${BUILD_SCRIPT} ${TORCH_PYTHON_INTERPS}
DEPENDS ${BUILD_SCRIPT}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dgl_sparse)
else(MSVC)
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/dgl_sparse/build.sh BUILD_SCRIPT)
add_custom_target(
dgl_sparse
ALL
${CMAKE_COMMAND} -E env
CMAKE_COMMAND=${CMAKE_CMD}
CUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR}
USE_CUDA=${USE_CUDA}
BINDIR=${CMAKE_CURRENT_BINARY_DIR}
INCLUDEDIR="${DGL_INCLUDE_DIRS}"
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)
endif(MSVC)
add_dependencies(dgl_sparse dgl)
endif(BUILD_SPARSE)
23 changes: 13 additions & 10 deletions dgl_sparse/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
cmake_minimum_required(VERSION 3.5)
project(dgl_sparse C CXX)

# Find PyTorch cmake files and PyTorch versions with the python interpreter $TORCH_PYTHON_INTERPS
# Find PyTorch cmake files and PyTorch versions with the python interpreter $PYTHON_INTERP
# ("python3" or "python" if empty)
if(NOT TORCH_PYTHON_INTERPS)
find_program(TORCH_PYTHON_INTERPS NAMES python3 python)
if(NOT PYTHON_INTERP)
find_program(PYTHON_INTERP NAMES python3 python)
endif()
message(STATUS "Using Python interpreter: ${TORCH_PYTHON_INTERPS}")
message(STATUS "Using Python interpreter: ${PYTHON_INTERP}")
file(TO_NATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/find_cmake.py FIND_CMAKE_PY)
execute_process(
COMMAND ${TORCH_PYTHON_INTERPS} ${FIND_CMAKE_PY}
COMMAND ${PYTHON_INTERP} ${FIND_CMAKE_PY}
OUTPUT_VARIABLE TORCH_PREFIX_VER
OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "find_cmake.py output: ${TORCH_PREFIX_VER}")
Expand All @@ -26,6 +27,9 @@ endif()
set(Torch_DIR "${TORCH_PREFIX}/Torch")
message(STATUS "Setting directory to ${Torch_DIR}")
find_package(Torch REQUIRED)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TORCH_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g3 -ggdb")

set(LIB_DGL_SPARSE_NAME "dgl_sparse_pytorch_${TORCH_VER}")

Expand All @@ -43,9 +47,8 @@ target_compile_definitions(${LIB_DGL_SPARSE_NAME} PRIVATE TORCH_VERSION_MAJOR=${
target_compile_definitions(${LIB_DGL_SPARSE_NAME} PRIVATE TORCH_VERSION_MINOR=${TORCH_VERSION_MINOR})

target_include_directories(${LIB_DGL_SPARSE_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/third_party/dmlc-core/include")
target_link_libraries(${LIB_DGL_SPARSE_NAME} dmlc)
message(STATUS "DGL include directories: ${DGL_INCLUDE_DIRS}")
target_include_directories(${LIB_DGL_SPARSE_NAME} PRIVATE ${DGL_INCLUDE_DIRS})
target_link_directories(${LIB_DGL_SPARSE_NAME} PRIVATE ${DGL_BUILD_DIR} "${DGL_BUILD_DIR}/third_party/dmlc-core")
target_link_libraries(${LIB_DGL_SPARSE_NAME} dmlc dgl)
set(GOOGLE_TEST 0) # Turn off dmlc-core test


target_include_directories(${LIB_DGL_SPARSE_NAME} PRIVATE ${DGL_INCLUDE})
target_link_libraries(${LIB_DGL_SPARSE_NAME} dgl)
31 changes: 31 additions & 0 deletions dgl_sparse/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
REM Helper script to build tensor adapter libraries for PyTorch
@ECHO OFF
SETLOCAL EnableDelayedExpansion

MD "%BINDIR%\tensoradapter\pytorch"
DEL /S /Q build
MD build
PUSHD build

IF x%1x == xx GOTO single

FOR %%X IN (%*) DO (
DEL /S /Q *
"%CMAKE_COMMAND%" -DCMAKE_CONFIGURATION_TYPES=Release -DCUDA_TOOLKIT_ROOT_DIR="%CUDA_TOOLKIT_ROOT_DIR%" -DTORCH_CUDA_ARCH_LIST=%TORCH_CUDA_ARCH_LIST% -DDGL_INCLUDE_DIRS=%INCLUDEDIR: =;% -DUSE_CUDA=%USE_CUDA% -DPYTHON_INTERP=%%X .. -G "Visual Studio 16 2019" || EXIT /B 1
msbuild tensoradapter_pytorch.sln /m /nr:false || EXIT /B 1
COPY /Y Release\*.dll "%BINDIR%\tensoradapter\pytorch" || EXIT /B 1
)

GOTO end

:single

DEL /S /Q *
"%CMAKE_COMMAND%" -DCMAKE_CONFIGURATION_TYPES=Release -DCUDA_TOOLKIT_ROOT_DIR="%CUDA_TOOLKIT_ROOT_DIR%" -DTORCH_CUDA_ARCH_LIST=%TORCH_CUDA_ARCH_LIST% -DUSE_CUDA=%USE_CUDA% -DDGL_INCLUDE_DIRS=%INCLUDEDIR: =;% .. -G "Visual Studio 16 2019" || EXIT /B 1
msbuild tensoradapter_pytorch.sln /m /nr:false || EXIT /B 1
COPY /Y Release\*.dll "%BINDIR%\tensoradapter\pytorch" || EXIT /B 1

:end
POPD

ENDLOCAL
5 changes: 4 additions & 1 deletion dgl_sparse/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Helper script to build dgl sparse libraries
# Helper script to build dgl sparse libraries for PyTorch
set -e

rm -rf build
Expand All @@ -14,6 +14,9 @@ else
fi

CMAKE_FLAGS="-DCUDA_TOOLKIT_ROOT_DIR=$CUDA_TOOLKIT_ROOT_DIR -DTORCH_CUDA_ARCH_LIST=$TORCH_CUDA_ARCH_LIST -DUSE_CUDA=$USE_CUDA"
# 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

if [ $# -eq 0 ]; then
$CMAKE_COMMAND $CMAKE_FLAGS ..
Expand Down
6 changes: 3 additions & 3 deletions dgl_sparse/src/sparse_matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,20 @@ std::shared_ptr<CSR> SparseMatrix::CSCPtr() {
std::tuple<torch::Tensor, torch::Tensor> SparseMatrix::COOTensors() {
auto coo = COOPtr();
auto val = value();
return {coo->row, coo->col};
return std::make_tuple(coo->row, coo->col);
}

std::tuple<torch::Tensor, torch::Tensor, torch::optional<torch::Tensor>>
SparseMatrix::CSRTensors() {
auto csr = CSRPtr();
auto val = value();
return {csr->indptr, csr->indices, csr->value_indices};
return std::make_tuple(csr->indptr, csr->indices, csr->value_indices);
}

std::tuple<torch::Tensor, torch::Tensor, torch::optional<torch::Tensor>>
SparseMatrix::CSCTensors() {
auto csc = CSCPtr();
return {csc->indptr, csc->indices, csc->value_indices};
return std::make_tuple(csc->indptr, csc->indices, csc->value_indices);
}

c10::intrusive_ptr<SparseMatrix> SparseMatrix::Transpose() const {
Expand Down

0 comments on commit 9a40d20

Please sign in to comment.