|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, NVIDIA CORPORATION. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <cuvs/neighbors/cagra.hpp> |
| 18 | +#include <gtest/gtest.h> |
| 19 | +#include <raft/core/host_mdspan.hpp> |
| 20 | +#include <raft/core/resources.hpp> |
| 21 | + |
| 22 | +// This test targets public API exposure and basic invariants only (shapes, in-range indices). |
| 23 | +// Detailed optimization correctness is exercised by CAGRA build tests. |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +using IdxT = uint32_t; |
| 28 | + |
| 29 | +// Helper to create a simple synthetic KNN graph (ring-like neighbors) |
| 30 | +auto make_ring_knn_host(int64_t num_rows, int64_t kin) |
| 31 | +{ |
| 32 | + auto knn_graph = raft::make_host_matrix<IdxT, int64_t>(num_rows, kin); |
| 33 | + for (int64_t i = 0; i < num_rows; ++i) { |
| 34 | + for (int64_t j = 0; j < kin; ++j) { |
| 35 | + knn_graph(i, j) = static_cast<IdxT>((i + j + 1) % num_rows); |
| 36 | + } |
| 37 | + } |
| 38 | + return knn_graph; |
| 39 | +} |
| 40 | + |
| 41 | +TEST(CagraOptimize, HostToHostOptimizesGraph) |
| 42 | +{ |
| 43 | + raft::resources res; |
| 44 | + |
| 45 | + constexpr int64_t num_rows = 8; |
| 46 | + constexpr int64_t kin = 8; |
| 47 | + constexpr int64_t kout = 4; |
| 48 | + |
| 49 | + auto knn_graph = make_ring_knn_host(num_rows, kin); |
| 50 | + auto optimized_graph = raft::make_host_matrix<IdxT, int64_t>(num_rows, kout); |
| 51 | + |
| 52 | + // Test the optimize API |
| 53 | + cuvs::neighbors::cagra::helpers::optimize(res, knn_graph.view(), optimized_graph.view()); |
| 54 | + |
| 55 | + // Check basic invariants |
| 56 | + ASSERT_EQ(optimized_graph.extent(0), num_rows); |
| 57 | + ASSERT_EQ(optimized_graph.extent(1), kout); |
| 58 | + |
| 59 | + // Check that all neighbors are valid indices |
| 60 | + for (int64_t i = 0; i < num_rows; ++i) { |
| 61 | + for (int64_t j = 0; j < kout; ++j) { |
| 62 | + EXPECT_LT(optimized_graph(i, j), static_cast<IdxT>(num_rows)); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +} // namespace |
0 commit comments