Skip to content

Commit

Permalink
Making cuco, thrust, and mdspan optional dependencies. (#585)
Browse files Browse the repository at this point in the history
In addition to the cuco dependency, the following changes are included:
1. ability to turn off thrust and mdspan dependencies (rmm is still required)
2. compiling libraries now defaults to the same setting of `BUILD_TESTS` (tests are still enabled)
3. cuco dependency is disabled by default (unless distance component is enabled)
4. the headers which are safe to expose in public APIs are moved over to `core/` directory.

Authors:
  - Corey J. Nolet (https://github.com/cjnolet)

Approvers:
  - Dante Gama Dessavre (https://github.com/dantegd)
  - Robert Maynard (https://github.com/robertmaynard)
  - Mark Sadang (https://github.com/msadang)

URL: #585
  • Loading branch information
cjnolet authored Apr 19, 2022
1 parent 2f6d247 commit d0601af
Show file tree
Hide file tree
Showing 43 changed files with 3,767 additions and 3,642 deletions.
45 changes: 32 additions & 13 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
In addition to the libraries included with cudatoolkit 11.0+, there are some other dependencies below for building RAFT from source. Many of the dependencies are optional and depend only on the primitives being used. All of these can be installed with cmake or [rapids-cpm](https://github.com/rapidsai/rapids-cmake#cpm) and many of them can be installed with [conda](https://anaconda.org).

#### Required
- [Thrust](https://github.com/NVIDIA/thrust) v1.15 / [CUB](https://github.com/NVIDIA/cub)
- [RMM](https://github.com/rapidsai/rmm) corresponding to RAFT version.
- [mdspan](https://github.com/rapidsai/mdspan)

#### Optional
- [cuCollections](https://github.com/NVIDIA/cuCollections) - Used in `raft::sparse::distance` API
- [mdspan](https://github.com/rapidsai/mdspan) - On by default but can be disabled.
- [Thrust](https://github.com/NVIDIA/thrust) v1.15 / [CUB](https://github.com/NVIDIA/cub) - On by default but can be disabled.
- [cuCollections](https://github.com/NVIDIA/cuCollections) - Used in `raft::sparse::distance` API.
- [Libcu++](https://github.com/NVIDIA/libcudacxx) v1.7.0
- [FAISS](https://github.com/facebookresearch/faiss) v1.7.0 - Used in `raft::spatial::knn` API and needed to build tests.
- [NCCL](https://github.com/NVIDIA/nccl) - Used in `raft::comms` API and needed to build `Pyraft`
Expand All @@ -53,6 +53,11 @@ The following example will download the needed dependencies and install the RAFT
./build.sh libraft --install
```

The `--minimal-deps` flag can be used to install the headers with minimal dependencies:
```bash
./build.sh libraft --install --minimal-deps
```

### <a id="shared_cpp_libs"></a>C++ Shared Libraries (optional)

For larger projects which make heavy use of the pairwise distances or nearest neighbors APIs, shared libraries can be built to speed up compile times. These shared libraries can also significantly improve re-compile times both while developing RAFT and developing against the APIs. Build all of the available shared libraries by passing `--compile-libs` flag to `build.sh`:
Expand All @@ -69,7 +74,14 @@ Add the `--install` flag to the above example to also install the shared librari

### <a id="gtests"></a>Tests

Compile the tests using the `tests` target in `build.sh`. By default, the shared libraries are assumed to be already built and on the library path. Add `--compile-libs` to also compile them.
Compile the tests using the `tests` target in `build.sh`.

```bash
./build.sh libraft tests
```

Test compile times can be improved significantly by using the optional shared libraries. If installed, they will be used automatically when building the tests but `--compile-libs` can be used to add additional compilation units and compile them with the tests.

```bash
./build.sh libraft tests --compile-libs
```
Expand Down Expand Up @@ -110,11 +122,13 @@ RAFT's cmake has the following configurable flags available:.
| --- | --- | --- | --- |
| BUILD_TESTS | ON, OFF | ON | Compile Googletests |
| BUILD_BENCH | ON, OFF | ON | Compile benchmarks |
| raft_FIND_COMPONENTS | nn distance | | Configures the optional components as a space-separated list |
| RAFT_COMPILE_LIBRARIES | ON, OFF | OFF | Compiles all `libraft` shared libraries (these are required for Googletests) |
| RAFT_COMPILE_NN_LIBRARY | ON, OFF | ON | Compiles the `libraft-nn` shared library |
| RAFT_COMPILE_DIST_LIBRARY | ON, OFF | ON | Compiles the `libraft-distance` shared library |
| RAFT_COMPILE_NN_LIBRARY | ON, OFF | OFF | Compiles the `libraft-nn` shared library |
| RAFT_COMPILE_DIST_LIBRARY | ON, OFF | OFF | Compiles the `libraft-distance` shared library |
| RAFT_ENABLE_NN_DEPENDENCIES | ON, OFF | OFF | Searches for dependencies of nearest neighbors API, such as FAISS, and compiles them if not found. Needed for `raft::spatial::knn` |
| RAFT_ENABLE_cuco_DEPENDENCY | ON, OFF | ON | Enables the cuCollections dependency used by `raft::sparse::distance` |
| RAFT_ENABLE_thrust_DEPENDENCY | ON, OFF | ON | Enables the Thrust dependency. This can be disabled when using many simple utilities or to override with a different Thrust version. |
| RAFT_ENABLE_mdspan_DEPENDENCY | ON, OFF | ON | Enables the std::mdspan dependency. This can be disabled when using many simple utilities. |
| RAFT_ENABLE_nccl_DEPENDENCY | ON, OFF | OFF | Enables NCCL dependency used by `raft::comms` and needed to build `pyraft` |
| RAFT_ENABLE_ucx_DEPENDENCY | ON, OFF | OFF | Enables UCX dependency used by `raft::comms` and needed to build `pyraft` |
| RAFT_USE_FAISS_STATIC | ON, OFF | OFF | Statically link FAISS into `libraft-nn` |
Expand Down Expand Up @@ -212,7 +226,8 @@ set(RAFT_PINNED_TAG "branch-${RAFT_VERSION}")
function(find_and_configure_raft)
set(oneValueArgs VERSION FORK PINNED_TAG USE_FAISS_STATIC
COMPILE_LIBRARIES ENABLE_NN_DEPENDENCIES CLONE_ON_PIN
USE_NN_LIBRARY USE_DISTANCE_LIBRARY)
USE_NN_LIBRARY USE_DISTANCE_LIBRARY
ENABLE_thrust_DEPENDENCY ENABLE_mdspan_DEPENDENCY)
cmake_parse_arguments(PKG "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${ARGN} )
Expand Down Expand Up @@ -256,6 +271,8 @@ function(find_and_configure_raft)
"RAFT_ENABLE_NN_DEPENDENCIES ${PKG_ENABLE_NN_DEPENDENCIES}"
"RAFT_USE_FAISS_STATIC ${PKG_USE_FAISS_STATIC}"
"RAFT_COMPILE_LIBRARIES ${PKG_COMPILE_LIBRARIES}"
"RAFT_ENABLE_thrust_DEPENDENCY ${PKG_ENABLE_thrust_DEPENDENCY}"
"RAFT_ENABLE_mdspan_DEPENDENCY ${PKG_ENABLE_mdspan_DEPENDENCY}"
)
endfunction()
Expand All @@ -272,11 +289,13 @@ find_and_configure_raft(VERSION ${RAFT_VERSION}.00
# even if it's already installed.
CLONE_ON_PIN ON
COMPILE_LIBRARIES NO
USE_NN_LIBRARY NO
USE_DISTANCE_LIBRARY NO
ENABLE_NN_DEPENDENCIES NO # This builds FAISS if not installed
USE_FAISS_STATIC NO
COMPILE_LIBRARIES NO
USE_NN_LIBRARY NO
USE_DISTANCE_LIBRARY NO
ENABLE_NN_DEPENDENCIES NO # This builds FAISS if not installed
USE_FAISS_STATIC NO
ENABLE_thrust_DEPENDENCY YES
ENABLE_mdspan_DEPENDENCY YES
)
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# <div align="left"><img src="https://rapids.ai/assets/images/rapids_logo.png" width="90px"/>&nbsp;RAFT: Reusable Accelerated Functions and Tools</div>

RAFT contains fundamental widely-used algorithms and primitives for data science, graph and machine learning. The algorithms are CUDA-accelerated and form building-blocks for rapidly composing analytics.
RAFT contains fundamental widely-used algorithms and primitives for data science and machine learning. The algorithms are CUDA-accelerated and form building-blocks for rapidly composing analytics.

By taking a primitives-based approach to algorithm development, RAFT
- accelerates algorithm construction time
Expand Down
15 changes: 13 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ARGS=$*
# script, and that this script resides in the repo dir!
REPODIR=$(cd $(dirname $0); pwd)

VALIDARGS="clean libraft pyraft pylibraft docs tests bench clean -v -g --install --compile-libs --compile-nn --compile-dist --allgpuarch --nvtx --show_depr_warn -h --buildfaiss"
VALIDARGS="clean libraft pyraft pylibraft docs tests bench clean -v -g --install --compile-libs --compile-nn --compile-dist --allgpuarch --nvtx --show_depr_warn -h --buildfaiss --minimal-deps"
HELP="$0 [<target> ...] [<flag> ...]
where <target> is:
clean - remove all existing build artifacts and configuration (start over)
Expand All @@ -36,6 +36,8 @@ HELP="$0 [<target> ...] [<flag> ...]
--compile-libs - compile shared libraries for all components
--compile-nn - compile shared library for nn component
--compile-dist - compile shared library for distance component
--minimal-deps - disables dependencies like thrust so they can be overridden.
can be useful for a pure header-only install
--allgpuarch - build for all supported GPU architectures
--buildfaiss - build faiss statically into raft
--install - install cmake targets
Expand All @@ -62,6 +64,9 @@ COMPILE_LIBRARIES=OFF
COMPILE_NN_LIBRARY=OFF
COMPILE_DIST_LIBRARY=OFF
ENABLE_NN_DEPENDENCIES=OFF

ENABLE_thrust_DEPENDENCY=ON

ENABLE_ucx_DEPENDENCY=OFF
ENABLE_nccl_DEPENDENCY=OFF

Expand Down Expand Up @@ -105,6 +110,11 @@ fi
if hasArg --install; then
INSTALL_TARGET="install"
fi

if hasArg --minimal-deps; then
ENABLE_thrust_DEPENDENCY=OFF
fi

if hasArg -v; then
VERBOSE_FLAG="-v"
CMAKE_LOG_LEVEL="VERBOSE"
Expand Down Expand Up @@ -218,7 +228,8 @@ if (( ${NUMARGS} == 0 )) || hasArg libraft || hasArg docs || hasArg tests || has
-DRAFT_COMPILE_DIST_LIBRARY=${COMPILE_DIST_LIBRARY} \
-DRAFT_USE_FAISS_STATIC=${BUILD_STATIC_FAISS} \
-DRAFT_ENABLE_nccl_DEPENDENCY=${ENABLE_nccl_DEPENDENCY} \
-DRAFT_ENABLE_ucx_DEPENDENCY=${ENABLE_ucx_DEPENDENCY}
-DRAFT_ENABLE_ucx_DEPENDENCY=${ENABLE_ucx_DEPENDENCY} \
-DRAFT_ENABLE_thrust_DEPENDENCY=${ENABLE_thrust_DEPENDENCY}

if [[ ${CMAKE_TARGET} != "" ]]; then
echo "-- Compiling targets: ${CMAKE_TARGET}, verbose=${VERBOSE_FLAG}"
Expand Down
1 change: 0 additions & 1 deletion conda/recipes/libraft_distance/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ requirements:
- ucx-py {{ ucx_py_version }}
- ucx-proc=*=gpu
- gtest=1.10.0
- gmock
- librmm {{ minor_version }}
run:
- libraft-headers {{ version }}
Expand Down
2 changes: 1 addition & 1 deletion conda/recipes/libraft_headers/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
# Copyright (c) 2022, NVIDIA CORPORATION.

./build.sh libraft --install -v --allgpuarch
./build.sh libraft --install -v --allgpuarch
1 change: 0 additions & 1 deletion conda/recipes/libraft_headers/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ requirements:
- ucx-py {{ ucx_py_version }}
- ucx-proc=*=gpu
- gtest=1.10.0
- gmock
- librmm {{ minor_version}}
- libcusolver>=11.2.1
run:
Expand Down
1 change: 0 additions & 1 deletion conda/recipes/libraft_nn/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ requirements:
- faiss-proc=*=cuda
- libfaiss 1.7.0 *_cuda
- gtest=1.10.0
- gmock
- librmm {{ minor_version }}
run:
- {{ pin_compatible('cudatoolkit', max_pin='x', min_pin='x') }}
Expand Down
55 changes: 40 additions & 15 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
set(RAPIDS_VERSION "22.06")
set(RAFT_VERSION "${RAPIDS_VERSION}.00")

cmake_minimum_required(VERSION 3.20.1 FATAL_ERROR)
file(DOWNLOAD https://raw.githubusercontent.com/rapidsai/rapids-cmake/branch-22.06/RAPIDS.cmake
Expand All @@ -26,7 +28,7 @@ include(rapids-find)

rapids_cuda_init_architectures(RAFT)

project(RAFT VERSION 22.06.00 LANGUAGES CXX CUDA)
project(RAFT VERSION ${RAFT_VERSION} LANGUAGES CXX CUDA)

# Needed because GoogleBenchmark changes the state of FindThreads.cmake, causing subsequent runs to
# have different values for the `Threads::Threads` target. Setting this flag ensures
Expand Down Expand Up @@ -55,16 +57,22 @@ option(CUDA_ENABLE_KERNELINFO "Enable kernel resource usage info" OFF)
option(CUDA_ENABLE_LINEINFO "Enable the -lineinfo option for nvcc (useful for cuda-memcheck / profiler)" OFF)
option(CUDA_STATIC_RUNTIME "Statically link the CUDA runtime" OFF)
option(DETECT_CONDA_ENV "Enable detection of conda environment for dependencies" ON)
option(DISABLE_DEPRECATION_WARNINGS "Disable depreaction warnings " ON)
option(DISABLE_DEPRECATION_WARNINGS "Disable deprecaction warnings " ON)
option(DISABLE_OPENMP "Disable OpenMP" OFF)
option(NVTX "Enable nvtx markers" OFF)

option(RAFT_COMPILE_LIBRARIES "Enable building raft shared library instantiations" ON)
option(RAFT_COMPILE_LIBRARIES "Enable building raft shared library instantiations" ${BUILD_TESTS})
option(RAFT_COMPILE_NN_LIBRARY "Enable building raft nearest neighbors shared library instantiations" OFF)
option(RAFT_COMPILE_DIST_LIBRARY "Enable building raft distant shared library instantiations" OFF)
option(RAFT_ENABLE_NN_DEPENDENCIES "Search for raft::nn dependencies like faiss" ${RAFT_COMPILE_LIBRARIES})

option(RAFT_ENABLE_cuco_DEPENDENCY "Enable cuCollections dependency" ON)
option(RAFT_ENABLE_mdspan_DEPENDENCY "Enable mdspan dependency" ON)
option(RAFT_ENABLE_thrust_DEPENDENCY "Enable Thrust dependency" ON)

if(BUILD_TESTS AND NOT RAFT_ENABLE_thrust_DEPENDENCY)
message(VERBOSE "RAFT: BUILD_TESTS is enabled, overriding RAFT_ENABLE_thrust_DEPENDENCY")
set(RAFT_ENABLE_thrust_DEPENDENCY ON)
endif()

# Currently, UCX and NCCL are only needed to build Pyraft and so a simple find_package() is sufficient
option(RAFT_ENABLE_nccl_DEPENDENCY "Enable NCCL dependency" OFF)
Expand All @@ -75,6 +83,7 @@ option(RAFT_EXCLUDE_FAISS_FROM_ALL "Exclude FAISS targets from RAFT's 'all' targ
include(CMakeDependentOption)
cmake_dependent_option(RAFT_USE_FAISS_STATIC "Build and statically link the FAISS library for nearest neighbors search on GPU" ON RAFT_COMPILE_LIBRARIES OFF)

message(VERBOSE "RAFT: Building optional components: ${raft_FIND_COMPONENTS}")
message(VERBOSE "RAFT: Build RAFT unit-tests: ${BUILD_TESTS}")
message(VERBOSE "RAFT: Building raft C++ benchmarks: ${BUILD_BENCH}")
message(VERBOSE "RAFT: Enable detection of conda environment for dependencies: ${DETECT_CONDA_ENV}")
Expand Down Expand Up @@ -123,6 +132,10 @@ include(cmake/modules/ConfigureCUDA.cmake)
##############################################################################
# - Requirements -------------------------------------------------------------

if(distance IN_LIST raft_FIND_COMPONENTS OR RAFT_COMPILE_LIBRARIES OR RAFT_COMPILE_DIST_LIBRARY)
set(RAFT_ENABLE_cuco_DEPENDENCY ON)
endif()

# add third party dependencies using CPM
rapids_cpm_init()

Expand Down Expand Up @@ -151,17 +164,20 @@ target_include_directories(raft INTERFACE
"$<BUILD_INTERFACE:${RAFT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

# Keep RAFT as lightweight as possible.
# Only CUDA libs, rmm, and mdspan should
# be used in global target.
target_link_libraries(raft INTERFACE
raft::Thrust
$<$<BOOL:${NVTX}>:CUDA::nvToolsExt>
CUDA::cublas
CUDA::curand
CUDA::cusolver
CUDA::cudart
CUDA::cusparse
rmm::rmm
$<$<BOOL:${RAFT_ENABLE_cuco_DEPENDENCY}>:cuco::cuco>
std::mdspan)
$<$<BOOL:${RAFT_ENABLE_thrust_DEPENDENCY}>:raft::Thrust>
$<$<BOOL:${RAFT_ENABLE_mdspan_DEPENDENCY}>:std::mdspan>
)

target_compile_definitions(raft INTERFACE $<$<BOOL:${NVTX}>:NVTX_ENABLED>)
target_compile_features(raft INTERFACE cxx_std_17 $<BUILD_INTERFACE:cuda_std_17>)
Expand Down Expand Up @@ -248,6 +264,7 @@ endif()

target_link_libraries(raft_distance INTERFACE
raft::raft
$<$<BOOL:${RAFT_ENABLE_cuco_DEPENDENCY}>:cuco::cuco>
$<TARGET_NAME_IF_EXISTS:raft_distance_lib>
$<TARGET_NAME_IF_EXISTS:raft::raft_distance_lib>
)
Expand Down Expand Up @@ -301,6 +318,7 @@ endif()

target_link_libraries(raft_nn INTERFACE
raft::raft
$<$<BOOL:${RAFT_ENABLE_NN_DEPENDENCIES}>:faiss::faiss>
$<TARGET_NAME_IF_EXISTS:raft_nn_lib>
$<TARGET_NAME_IF_EXISTS:raft::raft_nn_lib>)

Expand Down Expand Up @@ -341,14 +359,17 @@ install(DIRECTORY include/raft
install(FILES include/raft.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/raft)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/raft/version_config.hpp
DESTINATION include/raft)

##############################################################################
# - install export -----------------------------------------------------------
set(doc_string
[=[
Provide targets for the RAFT: Reusable Accelerated Functions and Tools

RAFT contains fundamental widely-used algorithms and primitives
for data science, graph, and ml.
for data science and machine learning.

Optional Components:
- nn
Expand All @@ -361,13 +382,18 @@ Imported Targets:

]=])

set(code_string
[=[

if(NOT TARGET raft::Thrust)
thrust_create_target(raft::Thrust FROM_OPTIONS)
set(code_string )
if(RAFT_ENABLE_thrust_DEPENDENCY)
string(APPEND code_string
[=[
if(NOT TARGET raft::Thrust)
thrust_create_target(raft::Thrust FROM_OPTIONS)
endif()
]=])
endif()

string(APPEND code_string
[=[
if(distance IN_LIST raft_FIND_COMPONENTS)
enable_language(CUDA)
endif()
Expand All @@ -381,8 +407,7 @@ if(nn IN_LIST raft_FIND_COMPONENTS)
add_library(faiss ALIAS faiss::faiss)
endif()
endif()
]=]
)
]=])

# Use `rapids_export` for 22.04 as it will have COMPONENT support
include(cmake/modules/raft_export.cmake)
Expand Down
31 changes: 15 additions & 16 deletions cpp/cmake/thirdparty/get_cuco.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@

function(find_and_configure_cuco VERSION)

if(RAFT_ENABLE_cuco_DEPENDENCY)
rapids_cpm_find(cuco ${VERSION}
GLOBAL_TARGETS cuco::cuco
BUILD_EXPORT_SET raft-exports
INSTALL_EXPORT_SET raft-exports
CPM_ARGS
GIT_REPOSITORY https://github.com/NVIDIA/cuCollections.git
GIT_TAG fb58a38701f1c24ecfe07d8f1f208bbe80930da5
OPTIONS "BUILD_TESTS OFF"
"BUILD_BENCHMARKS OFF"
"BUILD_EXAMPLES OFF"
)
endif()

rapids_cpm_find(cuco ${VERSION}
GLOBAL_TARGETS cuco::cuco
BUILD_EXPORT_SET raft-distance-exports
INSTALL_EXPORT_SET raft-distance-exports
CPM_ARGS
GIT_REPOSITORY https://github.com/NVIDIA/cuCollections.git
GIT_TAG 6ec8b6dcdeceea07ab4456d32461a05c18864411
OPTIONS "BUILD_TESTS OFF"
"BUILD_BENCHMARKS OFF"
"BUILD_EXAMPLES OFF"
)
endfunction()

# cuCollections doesn't have a version yet
find_and_configure_cuco(0.0.1)
if(RAFT_ENABLE_cuco_DEPENDENCY)
# cuCollections doesn't have a version yet
find_and_configure_cuco(0.0.1)
endif()
6 changes: 4 additions & 2 deletions cpp/cmake/thirdparty/get_libcudacxx.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

# This function finds libcudacxx and sets any additional necessary environment variables.
function(find_and_configure_libcudacxx)

include(${rapids-cmake-dir}/cpm/libcudacxx.cmake)

rapids_cpm_libcudacxx(BUILD_EXPORT_SET raft-exports
INSTALL_EXPORT_SET raft-exports)

endfunction()

find_and_configure_libcudacxx()
if(RAFT_ENABLE_cuco_DEPENDENCY)
find_and_configure_libcudacxx()
endif()
Loading

0 comments on commit d0601af

Please sign in to comment.