|
1 | | -/* |
2 | | - * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. |
3 | | - * SPDX-License-Identifier: Apache-2.0 |
4 | | - */ |
5 | | - |
6 | | -#include <gtest/gtest.h> |
7 | | - |
8 | | -#include <cuvs/neighbors/cagra.hpp> |
9 | | - |
10 | | -#include <raft/core/device_mdarray.hpp> |
11 | | -#include <raft/core/device_resources.hpp> |
12 | | -#include <raft/random/rng.cuh> |
13 | | - |
14 | | -#include <cstdint> |
15 | | -#include <type_traits> |
16 | | - |
17 | | -namespace cuvs::neighbors::cagra { |
18 | | - |
19 | | -template <typename DataT> |
20 | | -class CagraIterativeBuildBugTest : public ::testing::Test { |
21 | | - public: |
22 | | - using data_type = DataT; |
23 | | - |
24 | | - protected: |
25 | | - void run() |
26 | | - { |
27 | | - // Set up iterative CAGRA graph building |
28 | | - cagra::index_params index_params; |
29 | | - // The bug manifests when graph_degree is equal to intermediate_graph_degree |
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <gtest/gtest.h> |
| 7 | + |
| 8 | +#include <cuvs/neighbors/cagra.hpp> |
| 9 | + |
| 10 | +#include <raft/core/device_mdarray.hpp> |
| 11 | +#include <raft/core/device_resources.hpp> |
| 12 | +#include <raft/random/rng.cuh> |
| 13 | + |
| 14 | +#include <cstdint> |
| 15 | +#include <type_traits> |
| 16 | + |
| 17 | +namespace cuvs::neighbors::cagra { |
| 18 | + |
| 19 | +template <typename DataT> |
| 20 | +class CagraIterativeBuildBugTest : public ::testing::Test { |
| 21 | + public: |
| 22 | + using data_type = DataT; |
| 23 | + |
| 24 | + protected: |
| 25 | + void run() |
| 26 | + { |
| 27 | + // Set up iterative CAGRA graph building |
| 28 | + cagra::index_params index_params; |
| 29 | + // The bug manifests when graph_degree is equal to intermediate_graph_degree |
30 | 30 | // see issue https://github.com/nvidia/cuvs/issues/1818 |
31 | | - index_params.graph_degree = 16; |
32 | | - index_params.intermediate_graph_degree = 16; |
33 | | - |
34 | | - // Use iterative CAGRA search for graph building |
35 | | - index_params.graph_build_params = graph_build_params::iterative_search_params(); |
36 | | - |
37 | | - // Build the index |
38 | | - auto cagra_index = cagra::build(res, index_params, raft::make_const_mdspan(dataset->view())); |
39 | | - raft::resource::sync_stream(res); |
40 | | - |
41 | | - // Verify the index was built successfully |
42 | | - ASSERT_GT(cagra_index.size(), 0); |
43 | | - ASSERT_EQ(cagra_index.dim(), n_dim); |
44 | | - } |
45 | | - |
46 | | - void SetUp() override |
47 | | - { |
48 | | - dataset.emplace(raft::make_device_matrix<data_type, int64_t>(res, n_samples, n_dim)); |
49 | | - raft::random::RngState r(1234ULL); |
50 | | - |
51 | | - // Generate random data based on type |
52 | | - if constexpr (std::is_same_v<data_type, float>) { |
53 | | - raft::random::normal( |
54 | | - res, r, dataset->data_handle(), n_samples * n_dim, data_type(0), data_type(1)); |
55 | | - } else if constexpr (std::is_same_v<data_type, int8_t>) { |
56 | | - raft::random::uniformInt( |
57 | | - res, r, dataset->data_handle(), n_samples * n_dim, int8_t(-128), int8_t(127)); |
58 | | - } else if constexpr (std::is_same_v<data_type, uint8_t>) { |
59 | | - raft::random::uniformInt( |
60 | | - res, r, dataset->data_handle(), n_samples * n_dim, uint8_t(0), uint8_t(255)); |
61 | | - } |
62 | | - raft::resource::sync_stream(res); |
63 | | - } |
64 | | - |
65 | | - void TearDown() override |
66 | | - { |
67 | | - dataset.reset(); |
68 | | - raft::resource::sync_stream(res); |
69 | | - } |
70 | | - |
71 | | - private: |
72 | | - raft::resources res; |
73 | | - std::optional<raft::device_matrix<data_type, int64_t>> dataset = std::nullopt; |
74 | | - |
75 | | - constexpr static int64_t n_samples = 10000; |
76 | | - constexpr static int64_t n_dim = 1024; |
77 | | -}; |
78 | | - |
79 | | -// Instantiate test for different data types |
80 | | -using TestTypes = ::testing::Types<float, int8_t, uint8_t>; |
81 | | -TYPED_TEST_SUITE(CagraIterativeBuildBugTest, TestTypes); |
82 | | - |
83 | | -TYPED_TEST(CagraIterativeBuildBugTest, IterativeBuildTest) { this->run(); } |
84 | | - |
85 | | -} // namespace cuvs::neighbors::cagra |
| 31 | + index_params.graph_degree = 16; |
| 32 | + index_params.intermediate_graph_degree = 16; |
| 33 | + |
| 34 | + // Use iterative CAGRA search for graph building |
| 35 | + index_params.graph_build_params = graph_build_params::iterative_search_params(); |
| 36 | + |
| 37 | + // Build the index |
| 38 | + auto cagra_index = cagra::build(res, index_params, raft::make_const_mdspan(dataset->view())); |
| 39 | + raft::resource::sync_stream(res); |
| 40 | + |
| 41 | + // Verify the index was built successfully |
| 42 | + ASSERT_GT(cagra_index.size(), 0); |
| 43 | + ASSERT_EQ(cagra_index.dim(), n_dim); |
| 44 | + } |
| 45 | + |
| 46 | + void SetUp() override |
| 47 | + { |
| 48 | + dataset.emplace(raft::make_device_matrix<data_type, int64_t>(res, n_samples, n_dim)); |
| 49 | + raft::random::RngState r(1234ULL); |
| 50 | + |
| 51 | + // Generate random data based on type |
| 52 | + if constexpr (std::is_same_v<data_type, float>) { |
| 53 | + raft::random::normal( |
| 54 | + res, r, dataset->data_handle(), n_samples * n_dim, data_type(0), data_type(1)); |
| 55 | + } else if constexpr (std::is_same_v<data_type, int8_t>) { |
| 56 | + raft::random::uniformInt( |
| 57 | + res, r, dataset->data_handle(), n_samples * n_dim, int8_t(-128), int8_t(127)); |
| 58 | + } else if constexpr (std::is_same_v<data_type, uint8_t>) { |
| 59 | + raft::random::uniformInt( |
| 60 | + res, r, dataset->data_handle(), n_samples * n_dim, uint8_t(0), uint8_t(255)); |
| 61 | + } |
| 62 | + raft::resource::sync_stream(res); |
| 63 | + } |
| 64 | + |
| 65 | + void TearDown() override |
| 66 | + { |
| 67 | + dataset.reset(); |
| 68 | + raft::resource::sync_stream(res); |
| 69 | + } |
| 70 | + |
| 71 | + private: |
| 72 | + raft::resources res; |
| 73 | + std::optional<raft::device_matrix<data_type, int64_t>> dataset = std::nullopt; |
| 74 | + |
| 75 | + constexpr static int64_t n_samples = 10000; |
| 76 | + constexpr static int64_t n_dim = 1024; |
| 77 | +}; |
| 78 | + |
| 79 | +// Instantiate test for different data types |
| 80 | +using TestTypes = ::testing::Types<float, int8_t, uint8_t>; |
| 81 | +TYPED_TEST_SUITE(CagraIterativeBuildBugTest, TestTypes); |
| 82 | + |
| 83 | +TYPED_TEST(CagraIterativeBuildBugTest, IterativeBuildTest) { this->run(); } |
| 84 | + |
| 85 | +} // namespace cuvs::neighbors::cagra |
0 commit comments