Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Test that balanced k-means and IVF builds are reproducible
  • Loading branch information
bdice committed Oct 11, 2026
commit 9e8ca0d28820e0b9863c05a04ee0c33b6b2a2e69
88 changes: 88 additions & 0 deletions cpp/tests/cluster/kmeans_balanced.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include <raft/core/handle.hpp>
#include <raft/core/operators.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/linalg/map.cuh>
#include <raft/linalg/unary_op.cuh>
#include <raft/random/make_blobs.cuh>
#include <raft/random/rng.cuh>
#include <raft/stats/adjusted_rand_index.cuh>
#include <raft/util/cuda_utils.cuh>
#include <raft/util/cudart_utils.hpp>
Expand All @@ -22,6 +24,8 @@

#include <gtest/gtest.h>

#include <cmath>
#include <cstring>
#include <optional>
#include <vector>

Expand Down Expand Up @@ -301,4 +305,88 @@ KB_TEST((KmeansBalancedTest<int8_t, float, int, int, i2f_scaler<int8_t, float>,
KmeansBalancedTestFI8I32I32_SEP,
inputsf_i32);

/*
* Identical fits must give bitwise-identical centers. With `skewed`, 95% of the rows are packed in
* a tiny region and n_lists divides n_rows, so most clusters start (nearly) empty and the balancing
* step must find a donor for many receivers.
*/

template <typename DataT>
void check_balanced_fit_reproducible(
cuvs::cluster::kmeans::balanced_donor_selection donor_selection, bool skewed = false)
{
raft::resources handle;
auto stream = raft::resource::get_cuda_stream(handle);
const int64_t n_rows = 20000;
const int64_t n_cols = 24;
const int64_t n_lists = skewed ? 1000 : 200;
auto data_f = raft::make_device_matrix<float, int64_t>(handle, n_rows, n_cols);
auto data = raft::make_device_matrix<DataT, int64_t>(handle, n_rows, n_cols);
raft::random::RngState rng(1234ULL);
raft::random::uniform(handle, rng, data_f.data_handle(), n_rows * n_cols, -100.0f, 100.0f);
if (skewed) {
auto packed = raft::make_device_matrix_view<float, int64_t>(
data_f.data_handle(), n_rows * 95 / 100, n_cols);
raft::linalg::map(
handle, packed, raft::mul_const_op<float>{1e-3f}, raft::make_const_mdspan(packed));
}
raft::linalg::map(
handle, data.view(), raft::cast_op<DataT>{}, raft::make_const_mdspan(data_f.view()));

cuvs::cluster::kmeans::balanced_params params;
params.n_iters = 20;
params.donor_selection = donor_selection;
if (donor_selection == cuvs::cluster::kmeans::balanced_donor_selection::Random) {
// The IVF-PQ codebook settings
params.balance_lower_tolerance = 0.25f;
params.balance_upper_tolerance = 4.0f;
params.centroid_offset = 0.05f;
}

std::vector<std::vector<float>> results;
for (int run = 0; run < 2; run++) {
auto centers = raft::make_device_matrix<float, int64_t>(handle, n_lists, n_cols);
ASSERT_NO_THROW(cuvs::cluster::kmeans::fit(
handle, params, raft::make_const_mdspan(data.view()), centers.view()));
std::vector<float> h(centers.size());
raft::update_host(h.data(), centers.data_handle(), h.size(), stream);
raft::resource::sync_stream(handle);
for (auto x : h) {
ASSERT_TRUE(std::isfinite(x));
}
results.push_back(std::move(h));
}
ASSERT_EQ(0, std::memcmp(results[0].data(), results[1].data(), results[0].size() * sizeof(float)))
<< "balanced k-means centers differ between identical fits";
}

TEST(KmeansBalancedReproducible, FloatSizeSorted)
{
check_balanced_fit_reproducible<float>(
cuvs::cluster::kmeans::balanced_donor_selection::SizeSorted);
}

TEST(KmeansBalancedReproducible, FloatRandomDonor)
{
check_balanced_fit_reproducible<float>(cuvs::cluster::kmeans::balanced_donor_selection::Random);
}

TEST(KmeansBalancedReproducible, Int8SizeSorted)
{
check_balanced_fit_reproducible<int8_t>(
cuvs::cluster::kmeans::balanced_donor_selection::SizeSorted);
}

TEST(KmeansBalancedReproducible, SkewedSizeSorted)
{
check_balanced_fit_reproducible<float>(
cuvs::cluster::kmeans::balanced_donor_selection::SizeSorted, true);
}

TEST(KmeansBalancedReproducible, SkewedRandomDonor)
{
check_balanced_fit_reproducible<float>(cuvs::cluster::kmeans::balanced_donor_selection::Random,
true);
}

} // namespace cuvs
26 changes: 26 additions & 0 deletions cpp/tests/neighbors/ann_ivf_flat/test_float_int64_t.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/linalg/init.cuh>
#include <raft/matrix/init.cuh>
#include <raft/random/rng.cuh>

#include <cstring>
#include <numeric>
#include <optional>
#include <sstream>
Expand All @@ -27,6 +29,30 @@ TEST_P(AnnIVFFlatTestF_float, AnnIVFFlat)

INSTANTIATE_TEST_CASE_P(AnnIVFFlatTest, AnnIVFFlatTestF_float, ::testing::ValuesIn(inputs));

TEST(IVFFlatBuild, Reproducible)
{
raft::resources handle;
auto stream = raft::resource::get_cuda_stream(handle);
const int64_t n_rows = 100000;
const int64_t dim = 32;
auto dataset = raft::make_device_matrix<float, int64_t>(handle, n_rows, dim);
raft::random::RngState rng(42ULL);
raft::random::uniform(handle, rng, dataset.data_handle(), n_rows * dim, -1.0f, 1.0f);

index_params params;
params.n_lists = 500;
std::vector<std::vector<float>> centers;
for (int run = 0; run < 2; run++) {
const auto index = build(handle, params, raft::make_const_mdspan(dataset.view()));
std::vector<float> h(index.centers().size());
raft::update_host(h.data(), index.centers().data_handle(), h.size(), stream);
raft::resource::sync_stream(handle);
centers.push_back(std::move(h));
}
ASSERT_EQ(0, std::memcmp(centers[0].data(), centers[1].data(), centers[0].size() * sizeof(float)))
<< "IVF-Flat centers differ between identical builds";
}

TEST(IVFFlatSerialization, StreamRoundTrip)
{
raft::resources handle;
Expand Down
55 changes: 54 additions & 1 deletion cpp/tests/neighbors/ann_ivf_pq/test_float_int64_t.cu
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "../ann_ivf_pq.cuh"

#include <raft/random/rng.cuh>

#include <cstring>
#include <type_traits>
#include <vector>

namespace cuvs::neighbors::ivf_pq {

using f32_f32_i64 = ivf_pq_test<float, float, int64_t>;
Expand All @@ -28,4 +34,51 @@ INSTANTIATE(f32_f32_i64_filter,
defaults() + small_dims() + big_dims_moderate_lut() + enum_variety_l2() +
enum_variety_l2sqrt() + enum_variety_ip() + enum_variety_cosine());

template <typename MdspanT>
auto copy_to_host(raft::resources const& handle, MdspanT view)
{
std::vector<std::remove_cv_t<typename MdspanT::element_type>> h(view.size());
raft::update_host(
h.data(), view.data_handle(), h.size(), raft::resource::get_cuda_stream(handle));
raft::resource::sync_stream(handle);
return h;
}

void check_ivf_pq_build_reproducible(codebook_gen codebook_kind)
{
raft::resources handle;
const int64_t n_rows = 100000;
const int64_t dim = 32;
auto dataset = raft::make_device_matrix<float, int64_t>(handle, n_rows, dim);
raft::random::RngState rng(42ULL);
raft::random::uniform(handle, rng, dataset.data_handle(), n_rows * dim, -1.0f, 1.0f);

index_params params;
params.n_lists = 500;
params.pq_dim = 16;
params.codebook_kind = codebook_kind;
std::vector<std::vector<float>> centers, pq_centers;
for (int run = 0; run < 2; run++) {
const auto index = build(handle, params, raft::make_const_mdspan(dataset.view()));
centers.push_back(copy_to_host(handle, index.centers()));
pq_centers.push_back(copy_to_host(handle, index.pq_centers()));
}
ASSERT_EQ(0, std::memcmp(centers[0].data(), centers[1].data(), centers[0].size() * sizeof(float)))
<< "IVF-PQ cluster centers differ between identical builds";
ASSERT_EQ(
0,
std::memcmp(pq_centers[0].data(), pq_centers[1].data(), pq_centers[0].size() * sizeof(float)))
<< "IVF-PQ codebooks differ between identical builds";
}

TEST(IvfPqBuild, ReproduciblePerSubspace)
{
check_ivf_pq_build_reproducible(codebook_gen::PER_SUBSPACE);
}

TEST(IvfPqBuild, ReproduciblePerCluster)
{
check_ivf_pq_build_reproducible(codebook_gen::PER_CLUSTER);
}

} // namespace cuvs::neighbors::ivf_pq
Loading