/* * SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. * SPDX-License-Identifier: Apache-2.0 */ #include #include #include namespace cuvs::neighbors::cagra { graph_build_params_t index_params::graph_build_heuristic(raft::matrix_extent dataset, size_t intermediate_graph_degree, cuvs::distance::DistanceType metric, size_t build_quality) { if (dataset.extent(0) < int64_t(1e6)) { // Use NN descent for smaller datasets auto nn_descent_params = graph_build_params::nn_descent_params(intermediate_graph_degree, metric); nn_descent_params.max_iterations = 5 + build_quality; return nn_descent_params; } else { // Otherwise, use IVF-PQ auto ivf_pq_params = cuvs::neighbors::graph_build_params::ivf_pq_params(dataset, metric); ivf_pq_params.search_params.n_probes = std::round(2 + std::sqrt(ivf_pq_params.build_params.n_lists) / 20 + build_quality); return ivf_pq_params; } } cagra::index_params index_params::from_dataset(raft::matrix_extent dataset, size_t graph_degree, cuvs::distance::DistanceType metric, size_t build_quality) { cagra::index_params params; params.metric = metric; params.graph_degree = graph_degree; params.intermediate_graph_degree = graph_degree * 3 / 2; params.graph_build_params = graph_build_heuristic(dataset, params.intermediate_graph_degree, metric, build_quality); return params; } cagra::index_params index_params::from_hnsw_params(raft::matrix_extent dataset, int M, int ef_construction, hnsw_heuristic_type heuristic, cuvs::distance::DistanceType metric) { cagra::index_params params; switch (heuristic) { case hnsw_heuristic_type::SAME_GRAPH_FOOTPRINT: params.graph_degree = M * 2; params.intermediate_graph_degree = M * 3; params.variable_graph_degree_fraction = 0.35; break; case hnsw_heuristic_type::SIMILAR_SEARCH_PERFORMANCE: default: params.graph_degree = M; params.intermediate_graph_degree = M + M * ef_construction / 256; params.variable_graph_degree_fraction = 1.0; break; } params.graph_build_params = graph_build_heuristic(dataset, params.intermediate_graph_degree, metric, ef_construction / 16); return params; } } // namespace cuvs::neighbors::cagra