Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9fd71ef
add CAGRA ANN to replace brute force kmeans for prediction + test cas…
HowardHuang1 Mar 18, 2026
80070c4
fix test cases + fix threshold
HowardHuang1 Mar 18, 2026
fa6fd70
add test cases for entire IVF-PQ pipeline
HowardHuang1 Mar 18, 2026
d8543a6
rename variable to use_ann_for_extend since previous changes are only…
HowardHuang1 Mar 19, 2026
d26e916
Replace brute force with CAGRA ANN in build()
HowardHuang1 Mar 20, 2026
ad054af
add test cases
HowardHuang1 Mar 24, 2026
0f14da1
Merge remote-tracking branch 'upstream' into HH-ANNs-for-clustering-a…
HowardHuang1 Jun 25, 2026
e69259f
rename use_cagra and use_ann variables to use_cagra_for_cluster_assig…
HowardHuang1 Jun 25, 2026
1d76516
fix null list or empty trained indices error for IVF-PQ serialize cau…
HowardHuang1 Jun 25, 2026
8e62682
rename benchmark names so that Build_NearestCentroidLookup_Speedup mi…
HowardHuang1 Jun 25, 2026
167f0c4
refactor out common code shared by BM_IVFPQ_Build_NearestCentroidLook…
HowardHuang1 Jun 26, 2026
64586ad
add ann for cluster assignment to postfit. Added use_ann_for_build_po…
HowardHuang1 Jun 26, 2026
de80d26
Merge remote-tracking branch 'upstream' into HH-ANNs-for-clustering-a…
HowardHuang1 Jun 27, 2026
7daf9cb
add tuning script for ann_rebuild_interval balancing recall and speedup
HowardHuang1 Jun 27, 2026
76ebb73
Merge remote-tracking branch 'upstream' into HH-ANNs-for-clustering-a…
HowardHuang1 Jun 27, 2026
525cd4c
Merge remote-tracking branch 'upstream/main' into HH-ANNs-for-cluster…
HowardHuang1 Sep 5, 2026
1673e39
fix merge conflict with build now taking only dataset views and not d…
HowardHuang1 Sep 7, 2026
3951248
Replace hand-picked cluster-assignment bench args with controlled K s…
HowardHuang1 Sep 7, 2026
1daabbe
Add controlled fixed-K/vary-N, grid, search-only, dim, and rebuild-in…
HowardHuang1 Sep 8, 2026
52ddd3d
Merge remote-tracking branch 'upstream/main' into HH-ANNs-for-cluster…
HowardHuang1 Oct 27, 2026
3de0823
Fix benchmark plots and make fit-tuning quality metrics meaningful
HowardHuang1 Oct 28, 2026
73825bd
Read benchmark datasets from .fbin files and redesign plot panel 4
HowardHuang1 Nov 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test cases for entire IVF-PQ pipeline
  • Loading branch information
HowardHuang1 committed Mar 18, 2026
commit fa6fd704dc82437dbc4c09548592795f3ebc25db
21 changes: 21 additions & 0 deletions cpp/bench/ann/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,27 @@ if(CUVS_ANN_BENCH_USE_CUVS_CAGRA)
)
install(TARGETS CUVS_CLUSTER_ASSIGNMENT_BENCH COMPONENT ann_bench DESTINATION bin/ann)
add_dependencies(CUVS_ANN_BENCH_ALL CUVS_CLUSTER_ASSIGNMENT_BENCH)

# Full IVF-PQ build benchmark: validates extend() uses CAGRA when n_lists >= 200k
add_executable(CUVS_IVFPQ_BUILD_BENCH src/cuvs/cuvs_ivf_pq_build_bench.cu)
target_link_libraries(
CUVS_IVFPQ_BUILD_BENCH
PRIVATE cuvs benchmark::benchmark
)
target_include_directories(
CUVS_IVFPQ_BUILD_BENCH
PUBLIC "$<BUILD_INTERFACE:${CUVS_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CUVS_SOURCE_DIR}/../c/include>"
)
set_target_properties(
CUVS_IVFPQ_BUILD_BENCH
PROPERTIES CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CUDA_STANDARD 17
CUDA_STANDARD_REQUIRED ON
)
install(TARGETS CUVS_IVFPQ_BUILD_BENCH COMPONENT ann_bench DESTINATION bin/ann)
add_dependencies(CUVS_ANN_BENCH_ALL CUVS_IVFPQ_BUILD_BENCH)
endif()

if(CUVS_ANN_BENCH_USE_CUVS_CAGRA)
Expand Down
149 changes: 149 additions & 0 deletions cpp/bench/ann/src/cuvs/cuvs_ivf_pq_build_bench.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*
* Benchmark: full IVF-PQ build path and compare brute-force vs CAGRA cluster assignment.
* For each scenario we run one benchmark that does both a brute and a CAGRA full build,
* reports brute_ms and cagra_ms, and speedup = brute_ms / cagra_ms (>1 means CAGRA faster).
*
* n_lists = number of cluster centroids. We use at least 5 vectors per cluster
* (n_vectors >= 5 * n_lists). "Time" = wall time for one iteration (brute + CAGRA build).
*
* Full build includes kmeans, PQ codebook training, and assignment. Threshold 200K in
* ivf_pq_build.cuh was chosen from assignment-only benchmarks.
*/
#include <benchmark/benchmark.h>

#include <cuvs/neighbors/ivf_pq.hpp>

#include <chrono>
#include <memory>
#include <raft/core/device_mdarray.hpp>
#include <raft/core/device_resources.hpp>
#include <raft/core/resources.hpp>
#include <raft/core/resource/cuda_stream_pool.hpp>
#include <raft/random/rng.cuh>
#include <raft/random/rng_state.hpp>
#include <raft/util/cudart_utils.hpp>

#include <rmm/cuda_stream_pool.hpp>
#include <rmm/device_uvector.hpp>

namespace {

void init_random_dataset(raft::resources const& handle,
float* data,
int64_t n_rows,
int64_t dim)
{
raft::random::RngState rng(12345ULL);
raft::random::uniform(handle, rng, data, n_rows * dim, float(-1), float(1));
raft::resource::sync_stream(handle);
}

} // namespace

static void BM_IVFPQ_Build_Speedup(benchmark::State& state)
{
int64_t n_rows = static_cast<int64_t>(state.range(0));
uint32_t n_lists = static_cast<uint32_t>(state.range(1));
int64_t dim = static_cast<int64_t>(state.range(2));

raft::device_resources handle;
rmm::device_uvector<float> dataset(static_cast<size_t>(n_rows) * static_cast<size_t>(dim),
raft::resource::get_cuda_stream(handle));
init_random_dataset(handle, dataset.data(), n_rows, dim);

cuvs::neighbors::ivf_pq::index_params params_brute;
params_brute.n_lists = n_lists;
params_brute.kmeans_n_iters = 3;
params_brute.kmeans_trainset_fraction = 0.2;
params_brute.add_data_on_build = true;
params_brute.metric = cuvs::distance::DistanceType::L2Expanded;
params_brute.use_ann_for_cluster_assignment = false;

cuvs::neighbors::ivf_pq::index_params params_cagra = params_brute;
params_cagra.use_ann_for_cluster_assignment = true;

raft::resource::set_cuda_stream_pool(handle, std::make_shared<rmm::cuda_stream_pool>(1));

auto dataset_view = raft::make_device_matrix_view<const float, int64_t, raft::row_major>(
dataset.data(), n_rows, dim);

double total_brute_ms = 0.0, total_cagra_ms = 0.0;
int64_t iterations = 0;

for (auto _ : state) {
auto start = std::chrono::steady_clock::now();
auto idx_brute = cuvs::neighbors::ivf_pq::build(handle, params_brute, dataset_view);
benchmark::DoNotOptimize(idx_brute.size());
raft::resource::sync_stream(handle);
auto end = std::chrono::steady_clock::now();
total_brute_ms += 1e-6 * std::chrono::duration<double, std::nano>(end - start).count();

start = std::chrono::steady_clock::now();
auto idx_cagra = cuvs::neighbors::ivf_pq::build(handle, params_cagra, dataset_view);
benchmark::DoNotOptimize(idx_cagra.size());
raft::resource::sync_stream(handle);
end = std::chrono::steady_clock::now();
total_cagra_ms += 1e-6 * std::chrono::duration<double, std::nano>(end - start).count();

++iterations;
}

if (total_cagra_ms > 0) {
state.counters["speedup"] = total_brute_ms / total_cagra_ms;
}
state.counters["brute_ms"] = benchmark::Counter(total_brute_ms,
benchmark::Counter::kAvgIterations);
state.counters["cagra_ms"] = benchmark::Counter(total_cagra_ms,
benchmark::Counter::kAvgIterations);
}

constexpr int64_t kDim = 128;

// At least 5 vectors per cluster (n_vectors = 5 * n_lists). One row per config: brute_ms, cagra_ms, speedup.

// 1. 64K centroids, 5 vecs/cluster
BENCHMARK(BM_IVFPQ_Build_Speedup)
->Args({327680, 65536, kDim})
->Unit(benchmark::kMillisecond)
->UseRealTime()
->ArgNames({"n_vectors", "n_lists", "dim"});

// 2. 200K centroids, 5 vecs/cluster
BENCHMARK(BM_IVFPQ_Build_Speedup)
->Args({1000000, 200000, kDim})
->Unit(benchmark::kMillisecond)
->UseRealTime()
->ArgNames({"n_vectors", "n_lists", "dim"});

// 3. 400K centroids, 5 vecs/cluster
BENCHMARK(BM_IVFPQ_Build_Speedup)
->Args({2000000, 400000, kDim})
->Unit(benchmark::kMillisecond)
->UseRealTime()
->ArgNames({"n_vectors", "n_lists", "dim"});

// 4. 600K centroids, 5 vecs/cluster
BENCHMARK(BM_IVFPQ_Build_Speedup)
->Args({3000000, 600000, kDim})
->Unit(benchmark::kMillisecond)
->UseRealTime()
->ArgNames({"n_vectors", "n_lists", "dim"});

// 5. 800K centroids, 5 vecs/cluster
BENCHMARK(BM_IVFPQ_Build_Speedup)
->Args({4000000, 800000, kDim})
->Unit(benchmark::kMillisecond)
->UseRealTime()
->ArgNames({"n_vectors", "n_lists", "dim"});

// 6. 1M centroids, 5 vecs/cluster
BENCHMARK(BM_IVFPQ_Build_Speedup)
->Args({5000000, 1000000, kDim})
->Unit(benchmark::kMillisecond)
->UseRealTime()
->ArgNames({"n_vectors", "n_lists", "dim"});

BENCHMARK_MAIN();
13 changes: 13 additions & 0 deletions cpp/include/cuvs/neighbors/ivf_pq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ struct index_params : cuvs::neighbors::index_params {
*/
uint32_t max_train_points_per_pq_code = 256;

/**
* Override for cluster assignment during extend (and add_data_on_build).
* - std::nullopt (default): use heuristic (CAGRA when n_lists >= 200k, else brute force).
* - true: use CAGRA for assignment regardless of n_lists (for benchmarking).
* - false: use brute-force assignment regardless of n_lists (for benchmarking).
*/
std::optional<bool> use_ann_for_cluster_assignment;

/**
* Creates index_params based on shape of the input dataset.
* Usage example:
Expand Down Expand Up @@ -418,6 +426,9 @@ class index_iface {
const raft::resources& res) const = 0;
virtual raft::device_matrix_view<const half, uint32_t, raft::row_major> centers_half(
const raft::resources& res) const = 0;

/** When set, overrides heuristic for using CAGRA vs brute force in extend (cluster assignment). */
virtual std::optional<bool> use_ann_for_cluster_assignment() const = 0;
};

/**
Expand Down Expand Up @@ -653,6 +664,8 @@ class index : public index_iface<IdxT>, cuvs::neighbors::index {
*/
uint32_t get_list_size_in_bytes(uint32_t label) const override;

std::optional<bool> use_ann_for_cluster_assignment() const noexcept override;

/**
* @brief Construct index from implementation pointer.
*
Expand Down
13 changes: 9 additions & 4 deletions cpp/src/neighbors/ivf_pq/ivf_pq_build.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,8 @@ auto clone(const raft::resources& res, const index<IdxT>& source) -> index<IdxT>
source.pq_bits(),
source.pq_dim(),
source.conservative_memory_allocation(),
source.codes_layout());
source.codes_layout(),
source.use_ann_for_cluster_assignment());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updates to IVF-PQ are not needed for this PR. Lets just stick to Lloyd's algorithm (regular kmeans). Balanced kmeans can be looked into later.

// Copy the independent parts using mutable accessors
raft::copy(res, impl->list_sizes(), source.list_sizes());
Expand Down Expand Up @@ -1120,7 +1121,9 @@ void extend(raft::resources const& handle,
cuvs::cluster::kmeans::balanced_params kmeans_params;
kmeans_params.metric = index->metric();

if (n_clusters >= kUseAnnForClusterAssignmentMinClusters) {
bool use_cagra = index->use_ann_for_cluster_assignment().value_or(
n_clusters >= kUseAnnForClusterAssignmentMinClusters);
if (use_cagra) {
// Use CAGRA for cluster assignment when K is large (build once, search per batch).
auto centers_view = raft::make_device_matrix_view<const float, int64_t, raft::row_major>(
cluster_centers.data(), n_clusters, index->dim());
Expand Down Expand Up @@ -1300,7 +1303,8 @@ auto build(raft::resources const& handle,
params.pq_bits,
params.pq_dim == 0 ? index<IdxT>::calculate_pq_dim(dim) : params.pq_dim,
params.conservative_memory_allocation,
params.codes_layout);
params.codes_layout,
params.use_ann_for_cluster_assignment);

auto stream = raft::resource::get_cuda_stream(handle);
utils::memzero(
Expand Down Expand Up @@ -1608,7 +1612,8 @@ auto build(
index_params.pq_bits,
pq_dim,
index_params.conservative_memory_allocation,
index_params.codes_layout);
index_params.codes_layout,
index_params.use_ann_for_cluster_assignment);

utils::memzero(
impl->accum_sorted_sizes().data_handle(), impl->accum_sorted_sizes().size(), stream);
Expand Down
11 changes: 8 additions & 3 deletions cpp/src/neighbors/ivf_pq_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class index_impl : public index_iface<IdxT> {
uint32_t pq_bits,
uint32_t pq_dim,
bool conservative_memory_allocation,
list_layout codes_layout = list_layout::INTERLEAVED);
list_layout codes_layout = list_layout::INTERLEAVED,
std::optional<bool> use_ann_for_cluster_assignment = std::nullopt);

~index_impl() = default;
index_impl(index_impl&&) = default;
Expand Down Expand Up @@ -71,9 +72,11 @@ class index_impl : public index_iface<IdxT> {
const raft::resources& res) const override;

uint32_t get_list_size_in_bytes(uint32_t label) const override;
std::optional<bool> use_ann_for_cluster_assignment() const noexcept override;

protected:
cuvs::distance::DistanceType metric_;
std::optional<bool> use_ann_for_cluster_assignment_;
codebook_gen codebook_kind_;
list_layout codes_layout_;
uint32_t dim_;
Expand Down Expand Up @@ -113,7 +116,8 @@ class owning_impl : public index_impl<IdxT> {
uint32_t pq_bits,
uint32_t pq_dim,
bool conservative_memory_allocation,
list_layout codes_layout = list_layout::INTERLEAVED);
list_layout codes_layout = list_layout::INTERLEAVED,
std::optional<bool> use_ann_for_cluster_assignment = std::nullopt);

~owning_impl() = default;
owning_impl(owning_impl&&) = default;
Expand Down Expand Up @@ -159,7 +163,8 @@ class view_impl : public index_impl<IdxT> {
raft::device_matrix_view<const float, uint32_t, raft::row_major> centers_view,
raft::device_matrix_view<const float, uint32_t, raft::row_major> centers_rot_view,
raft::device_matrix_view<const float, uint32_t, raft::row_major> rotation_matrix_view,
list_layout codes_layout = list_layout::INTERLEAVED);
list_layout codes_layout = list_layout::INTERLEAVED,
std::optional<bool> use_ann_for_cluster_assignment = std::nullopt);

~view_impl() = default;
view_impl(view_impl&&) = default;
Expand Down
28 changes: 23 additions & 5 deletions cpp/src/neighbors/ivf_pq_index.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ index_impl<IdxT>::index_impl(raft::resources const& handle,
uint32_t pq_bits,
uint32_t pq_dim,
bool conservative_memory_allocation,
list_layout codes_layout)
list_layout codes_layout,
std::optional<bool> use_ann_for_cluster_assignment)
: metric_(metric),
codebook_kind_(codebook_kind),
codes_layout_(codes_layout),
dim_(dim),
pq_bits_(pq_bits),
pq_dim_(pq_dim == 0 ? index<IdxT>::calculate_pq_dim(dim) : pq_dim),
conservative_memory_allocation_(conservative_memory_allocation),
use_ann_for_cluster_assignment_(use_ann_for_cluster_assignment),
lists_(n_lists),
list_sizes_{raft::make_device_vector<uint32_t, uint32_t>(handle, n_lists)},
data_ptrs_{raft::make_device_vector<uint8_t*, uint32_t>(handle, n_lists)},
Expand Down Expand Up @@ -121,6 +123,12 @@ bool index_impl<IdxT>::conservative_memory_allocation() const noexcept
return conservative_memory_allocation_;
}

template <typename IdxT>
std::optional<bool> index_impl<IdxT>::use_ann_for_cluster_assignment() const noexcept
{
return use_ann_for_cluster_assignment_;
}

template <typename IdxT>
std::vector<std::shared_ptr<list_data_base<IdxT>>>& index_impl<IdxT>::lists() noexcept
{
Expand Down Expand Up @@ -197,7 +205,8 @@ owning_impl<IdxT>::owning_impl(raft::resources const& handle,
uint32_t pq_bits,
uint32_t pq_dim,
bool conservative_memory_allocation,
list_layout codes_layout)
list_layout codes_layout,
std::optional<bool> use_ann_for_cluster_assignment)
: index_impl<IdxT>(handle,
metric,
codebook_kind,
Expand All @@ -206,7 +215,8 @@ owning_impl<IdxT>::owning_impl(raft::resources const& handle,
pq_bits,
pq_dim,
conservative_memory_allocation,
codes_layout),
codes_layout,
use_ann_for_cluster_assignment),
pq_centers_{raft::make_device_mdarray<float>(
handle, index<IdxT>::make_pq_centers_extents(dim, pq_dim, pq_bits, codebook_kind, n_lists))},
centers_{
Expand Down Expand Up @@ -247,7 +257,8 @@ view_impl<IdxT>::view_impl(
raft::device_matrix_view<const float, uint32_t, raft::row_major> centers_view,
raft::device_matrix_view<const float, uint32_t, raft::row_major> centers_rot_view,
raft::device_matrix_view<const float, uint32_t, raft::row_major> rotation_matrix_view,
list_layout codes_layout)
list_layout codes_layout,
std::optional<bool> use_ann_for_cluster_assignment)
: index_impl<IdxT>(handle,
metric,
codebook_kind,
Expand All @@ -256,7 +267,8 @@ view_impl<IdxT>::view_impl(
pq_bits,
pq_dim,
conservative_memory_allocation,
codes_layout),
codes_layout,
use_ann_for_cluster_assignment),
pq_centers_view_(pq_centers_view),
centers_view_(centers_view),
centers_rot_view_(centers_rot_view),
Expand Down Expand Up @@ -594,6 +606,12 @@ uint32_t index<IdxT>::get_list_size_in_bytes(uint32_t label) const
return impl_->get_list_size_in_bytes(label);
}

template <typename IdxT>
std::optional<bool> index<IdxT>::use_ann_for_cluster_assignment() const noexcept
{
return impl_->use_ann_for_cluster_assignment();
}

template <typename IdxT>
void index_impl<IdxT>::check_consistency()
{
Expand Down