Skip to content

Commit

Permalink
Add CUDA plugin code (#2814)
Browse files Browse the repository at this point in the history
* Add CUDA plugin code

* Remove test file

* Use CGBN submodule and moved shared codes out

---------

Co-authored-by: Chester Chen <[email protected]>
  • Loading branch information
YuanTingHsieh and chesterxgchen authored Aug 21, 2024
1 parent e218b6c commit 048df7e
Show file tree
Hide file tree
Showing 36 changed files with 1,729 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "integration/xgboost/encryption_plugins/cuda_plugin/CGBN"]
path = integration/xgboost/encryption_plugins/cuda_plugin/CGBN
url = https://github.com/NVlabs/CGBN.git
55 changes: 23 additions & 32 deletions integration/xgboost/encryption_plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
cmake_minimum_required(VERSION 3.19)
project(xgb_nvflare LANGUAGES CXX C VERSION 1.0)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
cmake_policy(VERSION ${CMAKE_VERSION})
message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message(STATUS "CMAKE_VERSION: ${CMAKE_VERSION}")

option(GOOGLE_TEST "Build google tests" OFF)

file(GLOB_RECURSE LIB_SRC "src/*.cc")

add_library(nvflare SHARED ${LIB_SRC})
set_target_properties(nvflare PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
POSITION_INDEPENDENT_CODE ON
ENABLE_EXPORTS ON
)
target_include_directories(nvflare PRIVATE ${xgb_nvflare_SOURCE_DIR}/src/include)

if (APPLE)
add_link_options("LINKER:-object_path_lto,$<TARGET_PROPERTY:NAME>_lto.o")
add_link_options("LINKER:-cache_path_lto,${CMAKE_BINARY_DIR}/LTOCache")
endif ()

#-- Unit Tests
if(GOOGLE_TEST)
find_package(GTest REQUIRED)
enable_testing()
add_executable(nvflare_test)
target_link_libraries(nvflare_test PRIVATE nvflare)


target_include_directories(nvflare_test PRIVATE ${xgb_nvflare_SOURCE_DIR}/src/include)
# this has to be set before project()
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES 70)
endif()

add_subdirectory(${xgb_nvflare_SOURCE_DIR}/tests)
project(xgb_plugins LANGUAGES CUDA CXX VERSION 1.0)
option(BUILD_CUDA_PLUGIN "Build CUDA plugin" ON)
option(BUILD_NVFLARE_PLUGIN "Build NVFlare plugin" ON)

add_test(
NAME TestNvflarePlugins
COMMAND nvflare_test
WORKING_DIRECTORY ${xgb_nvflare_BINARY_DIR})
if (BUILD_CUDA_PLUGIN)
add_subdirectory(cuda_plugin)
else()
message(STATUS "Skipping CUDA plugin")
endif()

if (BUILD_NVFLARE_PLUGIN)
add_subdirectory(nvflare_plugin)
else()
message(STATUS "Skipping NVFLARE plugin")
endif()

20 changes: 17 additions & 3 deletions integration/xgboost/encryption_plugins/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# Build Instruction
# XGBoost plugins

cd NVFlare/integration/xgboost/encryption_plugins

## Install required dependencies for CUDA plugin
If you want to build CUDA plugin, you need to install the following libraries:
Require `libgmp-dev`, CUDA runtime >= 12.1, CUDA driver >= 12.1, NVIDIA GPU Driver >= 535
Compute Compatibility >= 7.0

## Build instructions

```
mkdir build
cd build
cmake ..
make
```

## Disable build of CUDA plugin
You can pass option to cmake to disable the build of CUDA plugin if you don't have the environment:
```
cmake -DBUILD_CUDA_PLUGIN=OFF ..
```

The library is libxgb_nvflare.so
1 change: 1 addition & 0 deletions integration/xgboost/encryption_plugins/cuda_plugin/CGBN
Submodule CGBN added at e8b9d2
52 changes: 52 additions & 0 deletions integration/xgboost/encryption_plugins/cuda_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(gmp REQUIRED)

# Set NVCC compiler
find_program(NVCC nvcc)
if(NOT NVCC)
message(FATAL_ERROR "NVCC not found! Please make sure CUDA is installed.")
endif()

file(GLOB_RECURSE LIB_SRC
${CMAKE_SOURCE_DIR}/shared/dam/*.cc
${CMAKE_SOURCE_DIR}/shared/plugins/*.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc
)
message(STATUS "LIB_SRC files: ${LIB_SRC}")

file(GLOB_RECURSE CUDA_SRC
${CMAKE_CURRENT_SOURCE_DIR}/CGBN/include/cgbn/cgbn.h
${CMAKE_CURRENT_SOURCE_DIR}/src/*.h
)
message(STATUS "CUDA_SRC files: ${CUDA_SRC}")

set(TARGET_NAME cuda_paillier)

set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/src/delegated_plugin.cc
${CMAKE_CURRENT_SOURCE_DIR}/src/plugin_main.cc
${CUDA_SRC}
PROPERTIES LANGUAGE CUDA
)

add_library(${TARGET_NAME} SHARED ${LIB_SRC})
set_target_properties(${TARGET_NAME}
PROPERTIES
CUDA_RUNTIME_LIBRARY Shared
)
set_target_properties(${TARGET_NAME}
PROPERTIES
LINKER_LANGUAGE CUDA
)

target_include_directories(${TARGET_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/shared/include
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/CGBN/include/cgbn
)

target_compile_features(${TARGET_NAME} PRIVATE cuda_std_17)
target_link_libraries(${TARGET_NAME} PRIVATE gmp::gmpc)
target_link_libraries(${TARGET_NAME} PRIVATE gmp::gmpxx)
target_link_libraries(${TARGET_NAME} PRIVATE gmp::gmp)

87 changes: 87 additions & 0 deletions integration/xgboost/encryption_plugins/cuda_plugin/Findgmp.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
####################################################
# Looking for main header file in standard locations
find_path(gmpc_INCLUDE_DIR gmp.h)
find_path(gmpxx_INCLUDE_DIR gmpxx.h)

############################################
# Looking for binaries in standard locations
find_library(gmpc_LIBRARY NAMES gmp libgmp)
find_library(gmpxx_LIBRARY NAMES gmpxx libgmpxx)

######################################################################################
# QUESTION: IS ALL THAT NECESSARY OR find_package_handle_standard_args DOES THE JOB? #
######################################################################################
IF (gmpc_INCLUDE_DIR STREQUAL "gmpc_INCLUDE_DIR-NOTFOUND")
MESSAGE(WARNING "GMP c headers not found")
SET(GMP_DETECTION_ERROR TRUE)
ELSEIF(gmpxx_INCLUDE_DIR STREQUAL "gmpxx_INCLUDE_DIR-NOTFOUND")
MESSAGE(WARNING "GMP c++ headers not found")
SET(GMP_DETECTION_ERROR TRUE)
ELSEIF(gmpc_LIBRARY STREQUAL "gmpc_LIBRARY-NOTFOUND")
MESSAGE(WARNING "GMP c library not found")
SET(GMP_DETECTION_ERROR TRUE)
ELSEIF(gmpxx_LIBRARY STREQUAL "gmpxx_LIBRARY-NOTFOUND")
MESSAGE(WARNING "GMP c++ library not found")
SET(GMP_DETECTION_ERROR TRUE)
ENDIF()

IF (NOT GMP_DETECTION_ERROR)

mark_as_advanced(gmpc_INCLUDE_DIR gmpc_LIBRARY gmpxx_INCLUDE_DIR gmpxx_LIBRARY)

#############################
# Setting find_package output
# gmp_FOUND
# Cache variables
# gmp_INCLUDE_DIR
# gmp_LIBRARY
# CMakeLists variables
# gmp_INCLUDE_DIRS
# gmp_LIBRARIES
include( FindPackageHandleStandardArgs )
find_package_handle_standard_args(gmp REQUIRED_VARS
gmpc_LIBRARY
gmpxx_LIBRARY
gmpc_INCLUDE_DIR
gmpxx_INCLUDE_DIR
)

IF (gmp_FOUND)

SET(gmpc_LIBRARIES ${gmpc_LIBRARY})
SET(gmpc_INCLUDE_DIRS ${gmpc_INCLUDE_DIR})
SET(gmpxx_LIBRARIES ${gmpxx_LIBRARY})
SET(gmpxx_INCLUDE_DIRS ${gmpxx_INCLUDE_DIR})

##################################
# Setting gmp::gmp
IF (NOT TARGET gmp::gmpc)
add_library(gmp::gmpc UNKNOWN IMPORTED)
set_target_properties(gmp::gmpc PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
IMPORTED_LOCATION "${gmpc_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${gmpc_INCLUDE_DIR}"
)
ENDIF()
#SET(GMPC_TARGET "gmp::gmpc")
IF (NOT TARGET gmp::gmpxx)
add_library(gmp::gmpxx UNKNOWN IMPORTED)
set_target_properties(gmp::gmpxx PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
IMPORTED_LOCATION "${gmpxx_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${gmpxx_INCLUDE_DIR}"
)
ENDIF()
#SET(GMPXX_TARGET "gmp::gmpxx")
IF (NOT TARGET gmp::gmp)
add_library(gmp::gmp INTERFACE IMPORTED)
#SET(GMP_TARGET "${GMPC_TARGET};${GMPXX_TARGET}")
set_target_properties(gmp::gmp PROPERTIES
LINK_INTERFACE_LIBRARIES "gmp::gmpc;gmp::gmpxx"
IMPORTED_LOCATION "${gmpc_LIBRARY};${gmpxx_LIBRARY}")
ENDIF()

ENDIF()

ENDIF()

4 changes: 4 additions & 0 deletions integration/xgboost/encryption_plugins/cuda_plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CUDA plugin

Use CUDA to do paillier encryption and addition.

Loading

0 comments on commit 048df7e

Please sign in to comment.