From 2bfe42d7e86a645e54c81f46f2afc561539fc381 Mon Sep 17 00:00:00 2001 From: Mickael Ide Date: Wed, 24 Jan 2024 16:55:25 +0100 Subject: [PATCH] Fix cmake export and template project --- cpp/CMakeLists.txt | 4 +- cpp/cmake/thirdparty/get_raft.cmake | 4 +- cpp/template/CMakeLists.txt | 7 +- cpp/template/README.md | 4 +- cpp/template/cmake/thirdparty/get_cuvs.cmake | 11 +- cpp/template/src/cagra_example.cu | 5 +- cpp/template/src/ivf_flat_example.cu | 160 ------------------- 7 files changed, 13 insertions(+), 182 deletions(-) delete mode 100644 cpp/template/src/ivf_flat_example.cu diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 2239a7e15..718ca917e 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -245,7 +245,7 @@ SECTIONS if(CUVS_NVTX) # This enables NVTX within the project with no option to disable it downstream. - target_link_libraries(cuvs PUBLIC CUDA::nvToolsExt) + target_link_libraries(cuvs PUBLIC CUDA::nvtx3) target_compile_definitions(cuvs PUBLIC NVTX_ENABLED) else() # Allow enable NVTX downstream if not set here. This creates a new option at build/install time, @@ -263,7 +263,7 @@ else() "\" OFF)" [=[ -target_link_libraries(cuvs::cuvs INTERFACE $<$:CUDA::nvToolsExt>) +target_link_libraries(cuvs::cuvs INTERFACE $<$:CUDA::nvtx3>) target_compile_definitions(cuvs::cuvs INTERFACE $<$:NVTX_ENABLED>) ]=] diff --git a/cpp/cmake/thirdparty/get_raft.cmake b/cpp/cmake/thirdparty/get_raft.cmake index 6a8feb2c6..d57d27312 100644 --- a/cpp/cmake/thirdparty/get_raft.cmake +++ b/cpp/cmake/thirdparty/get_raft.cmake @@ -35,8 +35,8 @@ function(find_and_configure_raft) #----------------------------------------------------- rapids_cpm_find(raft ${PKG_VERSION} GLOBAL_TARGETS raft::raft - BUILD_EXPORT_SET cuvs-template-exports - INSTALL_EXPORT_SET cuvs-template-exports + BUILD_EXPORT_SET cuvs-exports + INSTALL_EXPORT_SET cuvs-exports COMPONENTS ${RAFT_COMPONENTS} CPM_ARGS GIT_REPOSITORY https://github.com/${PKG_FORK}/raft.git diff --git a/cpp/template/CMakeLists.txt b/cpp/template/CMakeLists.txt index b3e0c8b23..535a73d4e 100644 --- a/cpp/template/CMakeLists.txt +++ b/cpp/template/CMakeLists.txt @@ -1,5 +1,5 @@ # ============================================================================= -# Copyright (c) 2023, NVIDIA CORPORATION. +# Copyright (c) 2023-2024, NVIDIA CORPORATION. # # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except # in compliance with the License. You may obtain a copy of the License at @@ -35,7 +35,4 @@ include(cmake/thirdparty/get_cuvs.cmake) # -------------- compile tasks ----------------- # add_executable(CAGRA_EXAMPLE src/cagra_example.cu) -target_link_libraries(CAGRA_EXAMPLE PRIVATE cuvs::cuvs cuvs::compiled) - -add_executable(IVF_FLAT_EXAMPLE src/ivf_flat_example.cu) -target_link_libraries(IVF_FLAT_EXAMPLE PRIVATE cuvs::cuvs cuvs::compiled) +target_link_libraries(CAGRA_EXAMPLE PRIVATE cuvs::cuvs) diff --git a/cpp/template/README.md b/cpp/template/README.md index 31b17d446..5393c0229 100644 --- a/cpp/template/README.md +++ b/cpp/template/README.md @@ -10,9 +10,9 @@ This directory (`CUVS_SOURCE/cpp/template`) can be copied directly in order to b CUVS can be integrated into an existing CMake project by copying the contents in the `configure rapids-cmake` and `configure cuvs` sections of the provided `CMakeLists.txt` into your project, along with `cmake/thirdparty/get_cuvs.cmake`. -Make sure to link against the appropriate Cmake targets. Use `cuvs::cuvs`to add make the headers available and `cuvs::compiled` when utilizing the shared library. +Make sure to link against the appropriate Cmake targets. Use `cuvs::cuvs` to utilize the shared library. ```cmake -target_link_libraries(your_app_target PRIVATE cuvs::cuvs cuvs::compiled) +target_link_libraries(your_app_target PRIVATE cuvs::cuvs) ``` diff --git a/cpp/template/cmake/thirdparty/get_cuvs.cmake b/cpp/template/cmake/thirdparty/get_cuvs.cmake index 0c60ef978..c77674be0 100644 --- a/cpp/template/cmake/thirdparty/get_cuvs.cmake +++ b/cpp/template/cmake/thirdparty/get_cuvs.cmake @@ -1,5 +1,5 @@ # ============================================================================= -# Copyright (c) 2023, NVIDIA CORPORATION. +# Copyright (c) 2023-2024, NVIDIA CORPORATION. # # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except # in compliance with the License. You may obtain a copy of the License at @@ -17,7 +17,7 @@ set(CUVS_FORK "rapidsai") set(CUVS_PINNED_TAG "branch-${RAPIDS_VERSION}") function(find_and_configure_cuvs) - set(oneValueArgs VERSION FORK PINNED_TAG COMPILE_LIBRARY ENABLE_NVTX ENABLE_MNMG_DEPENDENCIES) + set(oneValueArgs VERSION FORK PINNED_TAG COMPILE_LIBRARY ENABLE_NVTX) cmake_parse_arguments(PKG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) @@ -26,10 +26,6 @@ function(find_and_configure_cuvs) string(APPEND CUVS_COMPONENTS " compiled") endif() - if(PKG_ENABLE_MNMG_DEPENDENCIES) - string(APPEND CUVS_COMPONENTS " distributed") - endif() - #----------------------------------------------------- # Invoke CPM find_package() #----------------------------------------------------- @@ -46,7 +42,7 @@ function(find_and_configure_cuvs) "BUILD_TESTS OFF" "BUILD_PRIMS_BENCH OFF" "BUILD_ANN_BENCH OFF" - "CUVS_NVTX ${ENABLE_NVTX}" + "CUVS_NVTX ${PKG_ENABLE_NVTX}" "CUVS_COMPILE_LIBRARY ${PKG_COMPILE_LIBRARY}" ) endfunction() @@ -58,6 +54,5 @@ find_and_configure_cuvs(VERSION ${CUVS_VERSION}.00 FORK ${CUVS_FORK} PINNED_TAG ${CUVS_PINNED_TAG} COMPILE_LIBRARY ON - ENABLE_MNMG_DEPENDENCIES OFF ENABLE_NVTX OFF ) diff --git a/cpp/template/src/cagra_example.cu b/cpp/template/src/cagra_example.cu index da58a9259..6fab4291b 100644 --- a/cpp/template/src/cagra_example.cu +++ b/cpp/template/src/cagra_example.cu @@ -43,7 +43,7 @@ void cagra_build_search_simple(raft::device_resources const& dev_resources, cagra::index_params index_params; std::cout << "Building CAGRA index (search graph)" << std::endl; - auto index = cagra::build(dev_resources, index_params, dataset); + auto index = cagra::build(dev_resources, index_params, dataset); std::cout << "CAGRA index has " << index.size() << " vectors" << std::endl; std::cout << "CAGRA graph has degree " << index.graph_degree() << ", graph size [" @@ -52,8 +52,7 @@ void cagra_build_search_simple(raft::device_resources const& dev_resources, // use default search parameters cagra::search_params search_params; // search K nearest neighbors - cagra::search( - dev_resources, search_params, index, queries, neighbors.view(), distances.view()); + cagra::search(dev_resources, search_params, index, queries, neighbors.view(), distances.view()); // The call to ivf_flat::search is asynchronous. Before accessing the data, sync by calling // raft::resource::sync_stream(dev_resources); diff --git a/cpp/template/src/ivf_flat_example.cu b/cpp/template/src/ivf_flat_example.cu deleted file mode 100644 index c7ec425bd..000000000 --- a/cpp/template/src/ivf_flat_example.cu +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright (c) 2023, NVIDIA CORPORATION. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -#include "common.cuh" - -void ivf_flat_build_search_simple(raft::device_resources const& dev_resources, - raft::device_matrix_view dataset, - raft::device_matrix_view queries) -{ - using namespace cuvs::neighbors; - - ivf_flat::index_params index_params; - index_params.n_lists = 1024; - index_params.kmeans_trainset_fraction = 0.1; - index_params.metric = cuvs::distance::DistanceType::L2Expanded; - - std::cout << "Building IVF-Flat index" << std::endl; - auto index = ivf_flat::build(dev_resources, index_params, dataset); - - std::cout << "Number of clusters " << index.n_lists() << ", number of vectors added to index " - << index.size() << std::endl; - - // Create output arrays. - int64_t topk = 10; - int64_t n_queries = queries.extent(0); - auto neighbors = raft::make_device_matrix(dev_resources, n_queries, topk); - auto distances = raft::make_device_matrix(dev_resources, n_queries, topk); - - // Set search parameters. - ivf_flat::search_params search_params; - search_params.n_probes = 50; - - // Search K nearest neighbors for each of the queries. - ivf_flat::search( - dev_resources, search_params, index, queries, neighbors.view(), distances.view()); - - // The call to ivf_flat::search is asynchronous. Before accessing the data, sync by calling - // raft::resource::sync_stream(dev_resources); - - print_results(dev_resources, neighbors.view(), distances.view()); -} - -void ivf_flat_build_extend_search(raft::device_resources const& dev_resources, - raft::device_matrix_view dataset, - raft::device_matrix_view queries) -{ - using namespace cuvs::neighbors; - - // Define dataset indices. - auto data_indices = raft::make_device_vector(dev_resources, dataset.extent(0)); - thrust::counting_iterator first(0); - thrust::device_ptr ptr(data_indices.data_handle()); - thrust::copy( - raft::resource::get_thrust_policy(dev_resources), first, first + dataset.extent(0), ptr); - - // Sub-sample the dataset to create a training set. - auto trainset = - subsample(dev_resources, dataset, raft::make_const_mdspan(data_indices.view()), 0.1); - - ivf_flat::index_params index_params; - index_params.n_lists = 100; - index_params.metric = cuvs::distance::DistanceType::L2Expanded; - index_params.add_data_on_build = false; - - std::cout << "\nRun k-means clustering using the training set" << std::endl; - auto index = - ivf_flat::build(dev_resources, index_params, raft::make_const_mdspan(trainset.view())); - - std::cout << "Number of clusters " << index.n_lists() << ", number of vectors added to index " - << index.size() << std::endl; - - std::cout << "Filling index with the dataset vectors" << std::endl; - index = ivf_flat::extend(dev_resources, - dataset, - std::make_optional(raft::make_const_mdspan(data_indices.view())), - index); - - std::cout << "Index size after addin dataset vectors " << index.size() << std::endl; - - // Set search parameters. - ivf_flat::search_params search_params; - search_params.n_probes = 10; - - // Create output arrays. - int64_t topk = 10; - int64_t n_queries = queries.extent(0); - auto neighbors = raft::make_device_matrix(dev_resources, n_queries, topk); - auto distances = raft::make_device_matrix(dev_resources, n_queries, topk); - - // Search K nearest neighbors for each queries. - ivf_flat::search( - dev_resources, search_params, index, queries, neighbors.view(), distances.view()); - - // The call to ivf_flat::search is asynchronous. Before accessing the data, sync using: - // raft::resource::sync_stream(dev_resources); - - print_results(dev_resources, neighbors.view(), distances.view()); -} - -int main() -{ - raft::device_resources dev_resources; - - // Set pool memory resource with 1 GiB initial pool size. All allocations use the same pool. - rmm::mr::pool_memory_resource pool_mr( - rmm::mr::get_current_device_resource(), 1024 * 1024 * 1024ull); - rmm::mr::set_current_device_resource(&pool_mr); - - // Alternatively, one could define a pool allocator for temporary arrays (used within RAFT - // algorithms). In that case only the internal arrays would use the pool, any other allocation - // uses the default RMM memory resource. Here is how to change the workspace memory resource to - // a pool with 2 GiB upper limit. - // raft::resource::set_workspace_to_pool_resource(dev_resources, 2 * 1024 * 1024 * 1024ull); - - // Create input arrays. - int64_t n_samples = 10000; - int64_t n_dim = 3; - int64_t n_queries = 10; - auto dataset = raft::make_device_matrix(dev_resources, n_samples, n_dim); - auto queries = raft::make_device_matrix(dev_resources, n_queries, n_dim); - generate_dataset(dev_resources, dataset.view(), queries.view()); - - // Simple build and search example. - ivf_flat_build_search_simple(dev_resources, - raft::make_const_mdspan(dataset.view()), - raft::make_const_mdspan(queries.view())); - - // Build and extend example. - ivf_flat_build_extend_search(dev_resources, - raft::make_const_mdspan(dataset.view()), - raft::make_const_mdspan(queries.view())); -}