-
Notifications
You must be signed in to change notification settings - Fork 109
KNN bruteforce and IVF methods C/C++ API #33
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 30 commits into
rapidsai:branch-24.04
from
viclafargue:add-ivf_flat-and-ivf_pq
Mar 20, 2024
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
20658f8
Adding IVF-Flat and IVF-PQ
viclafargue 6976acc
Adding bruteforce KNN
viclafargue 9baadc8
Expose IVF methods
viclafargue 9944bae
Adding bruteforce test
viclafargue b4fdee0
IVF-Flat and IVF-PQ tests
viclafargue 85e0fba
Update IVF-Flat test
viclafargue 1bae6b9
Update bruteforce KNN API
viclafargue 2619b13
Update IVF-PQ testing
viclafargue 540be0d
IVF-FLAT C API
viclafargue fe60dcc
Merge branch 'branch-24.04' into add-ivf_flat-and-ivf_pq
cjnolet e38d530
IVF-PQ C API
viclafargue 089f67b
Merge branch 'branch-24.04' into add-ivf_flat-and-ivf_pq
cjnolet 493165e
BRUTEFORCE C API
viclafargue 33ffa18
REVERT cmake-format changes
viclafargue 8738810
Fixing style/doc/comments issues
viclafargue 1e1eb58
Solidify bruteforce index
viclafargue 3e81649
IVF-FLAT compiled index
viclafargue 0ac4820
IVF-PQ compiled index
viclafargue 58dc152
Removing macros from headers
viclafargue f5aeffc
Merge remote-tracking branch 'rapidsai/branch-24.04' into add-ivf_fla…
viclafargue 6aff838
IVF-Flat documentation
8000
viclafargue 5359afb
IVF-PQ documentation
viclafargue ab35e10
Bruteforce documentation
viclafargue 675bf2a
C doc update
viclafargue 00f67c2
C++ IVF-Flat doc examples
viclafargue ef5e3a4
C++ IVF-Flat and IVF-PQ doc examples
viclafargue 53f6265
C++ Bruteforce doc examples
viclafargue 82c003a
Add documentation for distance types
viclafargue d84d3cc
Python code generators
viclafargue 7111bfb
Restore CMakeLists.txt style
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
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,70 @@ | ||
/* | ||
* Copyright (c) 2024, 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. | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** enum to tell how to compute distance */ | ||
enum DistanceType { | ||
|
||
/** evaluate as dist_ij = sum(x_ik^2) + sum(y_ij)^2 - 2*sum(x_ik * y_jk) */ | ||
L2Expanded = 0, | ||
/** same as above, but inside the epilogue, perform square root operation */ | ||
L2SqrtExpanded = 1, | ||
/** cosine distance */ | ||
CosineExpanded = 2, | ||
/** L1 distance */ | ||
L1 = 3, | ||
/** evaluate as dist_ij += (x_ik - y-jk)^2 */ | ||
L2Unexpanded = 4, | ||
/** same as above, but inside the epilogue, perform square root operation */ | ||
L2SqrtUnexpanded = 5, | ||
/** basic inner product **/ | ||
InnerProduct = 6, | ||
/** Chebyshev (Linf) distance **/ | ||
Linf = 7, | ||
/** Canberra distance **/ | ||
Canberra = 8, | ||
/** Generalized Minkowski distance **/ | ||
LpUnexpanded = 9, | ||
/** Correlation distance **/ | ||
CorrelationExpanded = 10, | ||
/** Jaccard distance **/ | ||
JaccardExpanded = 11, | ||
/** Hellinger distance **/ | ||
HellingerExpanded = 12, | ||
/** Haversine distance **/ | ||
Haversine = 13, | ||
/** Bray-Curtis distance **/ | ||
BrayCurtis = 14, | ||
/** Jensen-Shannon distance**/ | ||
JensenShannon = 15, | ||
/** Hamming distance **/ | ||
HammingUnexpanded = 16, | ||
/** KLDivergence **/ | ||
KLDivergence = 17, | ||
/** RusselRao **/ | ||
RusselRaoExpanded = 18, | ||
/** Dice-Sorensen distance **/ | ||
DiceExpanded = 19, | ||
/** Precomputed (special value) **/ | ||
Precomputed = 100 | ||
}; | ||
|
||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Copyright (c) 2024, 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_types.h> | ||
#include <dlpack/dlpack.h> | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @defgroup bruteforce_c_index Bruteforce index | ||
* @{ | ||
*/ | ||
/** | ||
* @brief Struct to hold address of cuvs::neighbors::brute_force::index and its active trained dtype | ||
* | ||
*/ | ||
typedef struct { | ||
uintptr_t addr; | ||
DLDataType dtype; | ||
} bruteForceIndex; | ||
|
||
typedef bruteForceIndex* cuvsBruteForceIndex_t; | ||
|
||
/** | ||
* @brief Allocate BRUTEFORCE index | ||
* | ||
* @param[in] index cuvsBruteForceIndex_t to allocate | ||
* @return cuvsError_t | ||
*/ | ||
cuvsError_t bruteForceIndexCreate(cuvsBruteForceIndex_t* index); | ||
|
||
/** | ||
* @brief De-allocate BRUTEFORCE index | ||
* | ||
* @param[in] index cuvsBruteForceIndex_t to de-allocate | ||
*/ | ||
cuvsError_t bruteForceIndexDestroy(cuvsBruteForceIndex_t index); | ||
/** | ||
* @} | ||
*/ | ||
|
||
/** | ||
* @defgroup bruteforce_c_index_build Bruteforce index build | ||
* @{ | ||
*/ | ||
/** | ||
* @brief Build a BRUTEFORCE index with a `DLManagedTensor` which has underlying | ||
* `DLDeviceType` equal to `kDLCUDA`, `kDLCUDAHost`, `kDLCUDAManaged`, | ||
* or `kDLCPU`. Also, acceptable underlying types are: | ||
* 1. `kDLDataType.code == kDLFloat` and `kDLDataType.bits = 32` | ||
* 2. `kDLDataType.code == kDLInt` and `kDLDataType.bits = 8` | ||
* 3. `kDLDataType.code == kDLUInt` and `kDLDataType.bits = 8` | ||
* | ||
* @code {.c} | ||
* #include <cuvs/core/c_api.h> | ||
* #include <cuvs/neighbors/brute_force.h> | ||
* | ||
* // Create cuvsResources_t | ||
* cuvsResources_t res; | ||
* cuvsError_t res_create_status = cuvsResourcesCreate(&res); | ||
* | ||
* // Assume a populated `DLManagedTensor` type here | ||
* DLManagedTensor dataset; | ||
* | ||
* // Create BRUTEFORCE index | ||
* cuvsBruteForceIndex_t index; | ||
* cuvsError_t index_create_status = bruteForceIndexCreate(&index); | ||
* | ||
* // Build the BRUTEFORCE Index | ||
* cuvsError_t build_status = bruteForceBuild(res, &dataset_tensor, L2Expanded, 0.f, index); | ||
* | ||
* // de-allocate `index` and `res` | ||
* cuvsError_t index_destroy_status = bruteForceIndexDestroy(index); | ||
* cuvsError_t res_destroy_status = cuvsResourcesDestroy(res); | ||
* @endcode | ||
* | ||
* @param[in] res cuvsResources_t opaque C handle | ||
* @param[in] dataset DLManagedTensor* training dataset | ||
* @param[in] metric metric | ||
* @param[in] metric_arg metric_arg | ||
* @param[out] index cuvsBruteForceIndex_t Newly built BRUTEFORCE index | ||
* @return cuvsError_t | ||
*/ | ||
cuvsError_t bruteForceBuild(cuvsResources_t res, | ||
DLManagedTensor* dataset, | ||
enum DistanceType metric, | ||
float metric_arg, | ||
cuvsBruteForceIndex_t index); | ||
/** | ||
* @} | ||
*/ | ||
|
||
/** | ||
* @defgroup bruteforce_c_index_search Bruteforce index search | ||
* @{ | ||
*/ | ||
/** | ||
* @brief Search a BRUTEFORCE index with a `DLManagedTensor` which has underlying | ||
* `DLDeviceType` equal to `kDLCUDA`, `kDLCUDAHost`, `kDLCUDAManaged`. | ||
* It is also important to note that the BRUTEFORCE index must have been built | ||
* with the same type of `queries`, such that `index.dtype.code == | ||
* queries.dl_tensor.dtype.code` Types for input are: | ||
* 1. `queries`: `kDLDataType.code == kDLFloat` and `kDLDataType.bits = 32` | ||
* 2. `neighbors`: `kDLDataType.code == kDLUInt` and `kDLDataType.bits = 32` | ||
* 3. `distances`: `kDLDataType.code == kDLFloat` and `kDLDataType.bits = 32` | ||
* | ||
* @code {.c} | ||
* #include <cuvs/core/c_api.h> | ||
* #include <cuvs/neighbors/brute_force.h> | ||
* | ||
* // Create cuvsResources_t | ||
* cuvsResources_t res; | ||
* cuvsError_t res_create_status = cuvsResourcesCreate(&res); | ||
* | ||
* // Assume a populated `DLManagedTensor` type here | ||
* DLManagedTensor dataset; | ||
* DLManagedTensor queries; | ||
* DLManagedTensor neighbors; | ||
* | ||
* // Search the `index` built using `bruteForceBuild` | ||
* cuvsError_t search_status = bruteForceSearch(res, index, &queries, &neighbors, &distances); | ||
* | ||
* // de-allocate `res` | ||
* cuvsError_t res_destroy_status = cuvsResourcesDestroy(res); | ||
* @endcode | ||
* | ||
* @param[in] res cuvsResources_t opaque C handle | ||
* @param[in] index bruteForceIndex which has been returned by `bruteForceBuild` | ||
* @param[in] queries DLManagedTensor* queries dataset to search | ||
* @param[out] neighbors DLManagedTensor* output `k` neighbors for queries | ||
* @param[out] distances DLManagedTensor* output `k` distances for queries | ||
*/ | ||
cuvsError_t bruteForceSearch(cuvsResources_t res, | ||
cuvsBruteForceIndex_t index, | ||
DLManagedTensor* queries, | ||
DLManagedTensor* neighbors, | ||
DLManagedTensor* distances); | ||
/** | ||
* @} | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
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.