Skip to content

Commit

Permalink
Merge branch 'branch-24.04' into fea-cache-ivf-pq-params
Browse files Browse the repository at this point in the history
  • Loading branch information
achirkin authored Feb 5, 2024
2 parents 336f599 + 6328be7 commit 272f87e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
5 changes: 3 additions & 2 deletions cpp/bench/ann/src/raft/raft_cagra_wrapper.h
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -159,7 +159,8 @@ void RaftCagra<T, IdxT>::build(const T* dataset, size_t nrow, cudaStream_t strea
index_params_.nn_descent_params,
index_params_.ivf_pq_refine_rate,
index_params_.ivf_pq_build_params,
index_params_.ivf_pq_search_params)));
index_params_.ivf_pq_search_params,
false)));

handle_.stream_wait(stream); // RAFT stream -> bench stream
}
Expand Down
24 changes: 17 additions & 7 deletions cpp/cmake/thirdparty/get_hnswlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,30 @@ function(find_and_configure_hnswlib)
install(TARGETS hnswlib EXPORT hnswlib-exports)
install(DIRECTORY "${hnswlib_SOURCE_DIR}/hnswlib/" DESTINATION include/hnswlib)

# write export rules
rapids_export(
BUILD hnswlib
VERSION ${PKG_VERSION}
EXPORT_SET hnswlib-exports
GLOBAL_TARGETS hnswlib
NAMESPACE hnswlib::)
# write install export rules
rapids_export(
INSTALL hnswlib
VERSION ${PKG_VERSION}
EXPORT_SET hnswlib-exports
GLOBAL_TARGETS hnswlib
NAMESPACE hnswlib::)
endif()

# write build export rules
rapids_export(
BUILD hnswlib
VERSION ${PKG_VERSION}
EXPORT_SET hnswlib-exports
GLOBAL_TARGETS hnswlib
NAMESPACE hnswlib::)

include("${rapids-cmake-dir}/export/find_package_root.cmake")

# When using RAFT from the build dir, ensure hnswlib is also found in RAFT's build dir. This
# line adds `set(hnswlib_ROOT "${CMAKE_CURRENT_LIST_DIR}")` to build/raft-dependencies.cmake
rapids_export_find_package_root(
BUILD hnswlib [=[${CMAKE_CURRENT_LIST_DIR}]=] EXPORT_SET raft-exports
)
endif()
endfunction()

Expand Down
4 changes: 2 additions & 2 deletions cpp/include/raft/neighbors/cagra_types.hpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -145,7 +145,7 @@ struct index : ann::index {
/** Total length of the index (number of vectors). */
[[nodiscard]] constexpr inline auto size() const noexcept -> IdxT
{
return dataset_view_.extent(0);
return dataset_view_.extent(0) ? dataset_view_.extent(0) : graph_view_.extent(0);
}

/** Dimensionality of the data. */
Expand Down
17 changes: 14 additions & 3 deletions cpp/include/raft/neighbors/detail/cagra/cagra_build.cuh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -296,7 +296,8 @@ index<T, IdxT> build(
std::optional<experimental::nn_descent::index_params> nn_descent_params = std::nullopt,
std::optional<float> refine_rate = std::nullopt,
std::optional<ivf_pq::index_params> pq_build_params = std::nullopt,
std::optional<ivf_pq::search_params> search_params = std::nullopt)
std::optional<ivf_pq::search_params> search_params = std::nullopt,
bool construct_index_with_dataset = true)
{
size_t intermediate_degree = params.intermediate_graph_degree;
size_t graph_degree = params.graph_degree;
Expand Down Expand Up @@ -334,12 +335,22 @@ index<T, IdxT> build(

auto cagra_graph = raft::make_host_matrix<IdxT, int64_t>(dataset.extent(0), graph_degree);

RAFT_LOG_INFO("optimizing graph");
optimize<IdxT>(res, knn_graph->view(), cagra_graph.view());

// free intermediate graph before trying to create the index
knn_graph.reset();

RAFT_LOG_INFO("Graph optimized, creating index");
// Construct an index from dataset and optimized knn graph.
return index<T, IdxT>(res, params.metric, dataset, raft::make_const_mdspan(cagra_graph.view()));
if (construct_index_with_dataset) {
return index<T, IdxT>(res, params.metric, dataset, raft::make_const_mdspan(cagra_graph.view()));
} else {
// We just add the graph. User is expected to update dataset separately. This branch is used
// if user needs special control of memory allocations for the dataset.
index<T, IdxT> idx(res, params.metric);
idx.update_graph(res, raft::make_const_mdspan(cagra_graph.view()));
return idx;
}
}
} // namespace raft::neighbors::cagra::detail
5 changes: 5 additions & 0 deletions cpp/include/raft/neighbors/detail/cagra/cagra_serialize.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ void serialize(raft::resources const& res,
serialize_scalar(res, os, index_.metric());
serialize_mdspan(res, os, index_.graph());

include_dataset &= (index_.dataset().extent(0) > 0);

serialize_scalar(res, os, include_dataset);
if (include_dataset) {
RAFT_LOG_INFO("Saving CAGRA index with dataset");
auto dataset = index_.dataset();
// Remove padding before saving the dataset
auto host_dataset = make_host_matrix<T, int64_t>(dataset.extent(0), dataset.extent(1));
Expand All @@ -80,6 +83,8 @@ void serialize(raft::resources const& res,
resource::get_cuda_stream(res)));
resource::sync_stream(res);
serialize_mdspan(res, os, host_dataset.view());
} else {
RAFT_LOG_INFO("Saving CAGRA index WITHOUT dataset");
}
}

Expand Down

0 comments on commit 272f87e

Please sign in to comment.