-
Notifications
You must be signed in to change notification settings - Fork 184
All neighbors C and Python bindings #1282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rapids-bot
merged 20 commits into
rapidsai:branch-25.10
from
viclafargue:all-neighbors-python-bindings
Sep 18, 2025
Merged
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9256f97
All neighbors Python bindings
viclafargue a86a809
Allowing indices in-place write and optionally returning core_distances
viclafargue 6c8aecc
additional checks
viclafargue 7ee9f45
adressing review
viclafargue 71971e2
Making single GPU the default
viclafargue ff968f5
document multi-gpu
viclafargue e4f00ad
additional doc
viclafargue 312519e
Merge branch 'branch-25.10' into all-neighbors-python-bindings
viclafargue 2864baa
Merge branch 'branch-25.10' into all-neighbors-python-bindings
viclafargue a027467
Answering review
viclafargue b24f757
Merge branch 'branch-25.10' into all-neighbors-python-bindings
viclafargue 5986273
Fix doc for single GPU CI
viclafargue 64c454e
Merge branch 'branch-25.10' into all-neighbors-python-bindings
cjnolet 87ecff6
Answered reviews
viclafargue b361d33
recall threshold update
viclafargue 0eb4186
Merge branch 'branch-25.10' into all-neighbors-python-bindings
viclafargue a967350
Adding All Neighbors C API
viclafargue d917af8
Updating pytest
viclafargue 2c22e14
Merge branch 'branch-25.10' into all-neighbors-python-bindings
viclafargue 9842b10
update doc
viclafargue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * Copyright (c) 2025, NVIDIA CORPORATION. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cuvs/core/c_api.h> | ||
| #include <cuvs/distance/distance.h> | ||
| #include <cuvs/neighbors/ivf_pq.h> | ||
| #include <cuvs/neighbors/nn_descent.h> | ||
| #include <dlpack/dlpack.h> | ||
| #include <stdint.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * @defgroup all_neighbors_c_build All-neighbors C-API build (SNMG only) | ||
| * @{ | ||
| * | ||
| * All-neighbors constructs an approximate k-NN graph for all vectors in a dataset. | ||
| * This SNMG C API can be used with a multi-GPU resources handle (`cuvsResources_t`) | ||
| * created from `raft::device_resources_snmg` to distribute work across GPUs. | ||
|
viclafargue marked this conversation as resolved.
Outdated
|
||
| * | ||
| * Notes: | ||
| * - Outputs (indices, distances, core_distances) are expected to be on device memory. | ||
| * - Host variant accepts host-resident dataset; device variant accepts device-resident dataset. | ||
| * - For batching, `overlap_factor < n_clusters` must hold. | ||
| * - When `core_distances` is provided, mutual-reachability distances are produced (see alpha). | ||
| */ | ||
|
|
||
| /** | ||
| * @brief Graph build algorithm selection. | ||
| */ | ||
| typedef enum { | ||
| CUVS_ALL_NEIGHBORS_ALGO_BRUTE_FORCE = 0, ///< Use Brute Force for local kNN subgraphs | ||
| CUVS_ALL_NEIGHBORS_ALGO_IVF_PQ = 1, ///< Use IVF-PQ for local kNN subgraphs (host dataset only) | ||
| CUVS_ALL_NEIGHBORS_ALGO_NN_DESCENT = 2 ///< Use NN-Descent for local kNN subgraphs | ||
| } cuvsAllNeighborsAlgo; | ||
|
|
||
| /** | ||
| * @brief Parameters controlling SNMG all-neighbors build. | ||
| */ | ||
| struct cuvsAllNeighborsIndexParams { | ||
| cuvsAllNeighborsAlgo algo; ///< Local kNN graph build algorithm | ||
| size_t overlap_factor; ///< Number of clusters each point is assigned to (must be < n_clusters) | ||
| size_t | ||
| n_clusters; ///< Number of clusters/batches to partition the dataset into (> overlap_factor) | ||
| cuvsDistanceType metric; ///< Distance metric used for graph construction | ||
|
|
||
| // Algorithm-specific parameters (only one should be set based on algo) | ||
| cuvsIvfPqIndexParams_t ivf_pq_params; ///< Parameters for IVF-PQ algorithm (when algo == | ||
| ///< CUVS_ALL_NEIGHBORS_ALGO_IVF_PQ) | ||
| cuvsNNDescentIndexParams_t nn_descent_params; ///< Parameters for NN-Descent algorithm (when algo | ||
| ///< == CUVS_ALL_NEIGHBORS_ALGO_NN_DESCENT) | ||
| }; | ||
|
|
||
| typedef struct cuvsAllNeighborsIndexParams* cuvsAllNeighborsIndexParams_t; | ||
|
|
||
| /** | ||
| * @brief Build an all-neighbors k-NN graph automatically detecting host vs device dataset. | ||
| * | ||
| * @param[in] res Can be a SNMG multi-GPU resources (`cuvsResources_t`) or single-GPU | ||
| * resources | ||
| * @param[in] params Build parameters (see cuvsAllNeighborsIndexParams) | ||
| * @param[in] dataset 2D tensor [num_rows x dim] on host or device (auto-detected) | ||
| * @param[out] indices 2D tensor [num_rows x k] on device (int64) | ||
| * @param[out] distances Optional 2D tensor [num_rows x k] on device (float32); can be NULL | ||
| * @param[out] core_distances Optional 1D tensor [num_rows] on device (float32); can be NULL | ||
| * @param[in] alpha Mutual-reachability scaling; used only when core_distances is provided | ||
| * | ||
| * The function automatically detects whether the dataset is host-resident or device-resident | ||
| * and calls the appropriate implementation. For host datasets, it partitions data into | ||
| * `n_clusters` clusters and assigns each row to `overlap_factor` nearest clusters. For device | ||
| * datasets, `n_clusters` must be 1 (no batching); `overlap_factor` is ignored. | ||
| * Outputs always reside in device memory. | ||
| */ | ||
| cuvsError_t cuvsAllNeighborsBuild(cuvsResources_t res, | ||
| cuvsAllNeighborsIndexParams_t params, | ||
| DLManagedTensor* dataset, | ||
| DLManagedTensor* indices, | ||
| DLManagedTensor* distances, | ||
| DLManagedTensor* core_distances, | ||
| float alpha); | ||
|
|
||
| /** @} */ | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.