Skip to content
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

[WIP] Expose extend() in C API #276

Open
wants to merge 94 commits into
base: branch-25.02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
7961798
wip
ajit283 Jun 14, 2024
764dc1e
cleanup
ajit283 Jun 14, 2024
b0fd0b1
Added support for different data types and device allocation in NewMa…
ajit283 Jun 19, 2024
b47cf5b
bruteforce works
ajit283 Jun 19, 2024
1ec0c8c
refactor
ajit283 Jun 19, 2024
d5c0dc3
improve resources
ajit283 Jun 19, 2024
cc6319e
add ivf_flat, modules not working yet
ajit283 Jun 25, 2024
01af7c9
sync
ajit283 Jun 29, 2024
ed9bf47
cleanup
ajit283 Jul 2, 2024
4e79d8e
Merge branch 'rapidsai:branch-24.08' into go
ajit283 Jul 2, 2024
d278abc
rename
ajit283 Jul 2, 2024
b91721d
sync
ajit283 Jul 2, 2024
6b90861
.
ajit283 Jul 2, 2024
773fd94
Merge branch 'rapidsai:branch-24.08' into go
ajit283 Jul 3, 2024
7cbc1f9
sync
ajit283 Jul 10, 2024
46ec2f7
ivf_pq + cagra
ajit283 Jul 10, 2024
9dcffbd
Merge branch 'branch-24.08' into go
ajit283 Jul 10, 2024
f82216c
CI/CD attempt
ajit283 Jul 10, 2024
486d40a
add distance
ajit283 Jul 10, 2024
6f5c5a6
replace string arguments
ajit283 Jul 21, 2024
bfdf3be
make library easier to use
ajit283 Jul 21, 2024
de3cea0
graph degrees
ajit283 Jul 23, 2024
57e8dc0
fix top-level tests
ajit283 Jul 24, 2024
ab173dc
Merge branch 'branch-24.08' into go
cjnolet Jul 24, 2024
2d5fb95
renaming
ajit283 Aug 1, 2024
505d8dc
Merge branch 'go' of https://github.com/ajit283/cuvs into go
ajit283 Aug 1, 2024
c3360ee
change package name
ajit283 Aug 5, 2024
e261c8a
package name change (rapidsai)
ajit283 Aug 5, 2024
daaebdf
first try
ajit283 Aug 6, 2024
a4890ed
dlpack add expand
ajit283 Aug 7, 2024
e5a3dcc
fix comments
ajit283 Aug 8, 2024
f2bac2d
cagra: expose some types
ajit283 Aug 11, 2024
a684b01
Merge branch 'branch-24.08' into go
ajit283 Aug 11, 2024
34837b6
Merge branch 'branch-24.10' into extend-c-api
cjnolet Aug 12, 2024
ccb5d86
fix test
ajit283 Aug 21, 2024
c78e86f
add return tensor
ajit283 Aug 22, 2024
7d45e24
extend
ajit283 Aug 22, 2024
a7084c2
add extend with return (wip, test missing)
ajit283 Aug 22, 2024
3af1b73
Merge branch 'branch-24.10' into go
cjnolet Aug 22, 2024
44d9e58
fix brute_force, add pool
ajit283 Aug 26, 2024
862ecc5
Merge branch 'branch-24.10' into extend-c-api
ajit283 Sep 27, 2024
49379a0
Merge branch 'branch-24.10' into extend-c-api
cjnolet Sep 27, 2024
3db15ff
Merge branch 'branch-24.12' into extend-c-api
ajit283 Oct 9, 2024
9447f63
update, add search_width
ajit283 Oct 14, 2024
04b6532
add memory resource test
ajit283 Oct 16, 2024
a082f67
thread_local fix
ajit283 Oct 17, 2024
a84e764
change pool memory
ajit283 Oct 28, 2024
b0d4686
Merge branch 'branch-24.12' into extend-c-api
cjnolet Nov 4, 2024
853d538
Merge branch 'branch-24.10' into go
cjnolet Nov 4, 2024
4021229
Merge branch 'branch-24.12' into go
cjnolet Nov 4, 2024
6dd2044
simplify memory_resource
ajit283 Nov 5, 2024
3e33692
Merge branch 'go' of https://github.com/ajit283/cuvs into go
ajit283 Nov 5, 2024
59bb8ca
.
ajit283 Nov 5, 2024
2b10a23
fix documentation
ajit283 Nov 5, 2024
8be2d05
proper test
ajit283 Nov 8, 2024
f37c58e
fix test
ajit283 Nov 8, 2024
4fd78ca
cleanup
ajit283 Nov 8, 2024
11428ea
Merge branch 'branch-24.12' into extend-c-api
ajit283 Nov 8, 2024
b1a0476
Merge branch 'branch-24.12' into go
cjnolet Nov 15, 2024
e36d556
Merge branch 'branch-24.12' into c-expose-filtering
ajit283 Nov 21, 2024
13dba54
fixes, add issue
ajit283 Nov 21, 2024
fd270b0
Merge branch 'branch-24.12' into extend-c-api
ajit283 Nov 21, 2024
a2c5033
add TODO comment
ajit283 Nov 21, 2024
dddb165
Merge branch 'branch-24.12' into go
ajit283 Nov 21, 2024
e05782a
cleanup top-level packages
ajit283 Nov 21, 2024
a315632
cleanup neighbors
ajit283 Nov 22, 2024
3c97864
.
ajit283 Nov 22, 2024
bd2dd76
build
ajit283 Nov 22, 2024
bd76cf8
ci
ajit283 Nov 22, 2024
1e5a756
ci
ajit283 Nov 22, 2024
927f2be
fix pointer pinning issues
ajit283 Nov 26, 2024
fad44c2
address comments
ajit283 Nov 27, 2024
0d0184a
.
ajit283 Nov 27, 2024
e3d33a0
Update cpp/src/neighbors/cagra_c.cpp
ajit283 Nov 29, 2024
d0dc961
Merge branch 'branch-24.12' into c-expose-filtering
benfred Nov 29, 2024
102aee5
Merge branch 'branch-24.12' into extend-c-api
ajit283 Nov 29, 2024
3b17895
fix filter repr.
ajit283 Dec 3, 2024
f7fac35
add docstrings
ajit283 Dec 4, 2024
d814e37
Merge branch 'branch-24.12' into go
ajit283 Dec 4, 2024
4e584cc
Merge branch 'branch-24.12' into c-expose-filtering
lowener Dec 4, 2024
7999cd6
Merge branch 'branch-24.12' into extend-c-api
cjnolet Dec 5, 2024
21924ad
Merge branch 'branch-24.12' into extend-c-api
ajit283 Dec 7, 2024
1d61a90
Merge branch 'branch-25.02' into extend-c-api
ajit283 Dec 7, 2024
f30b57c
adjust
ajit283 Dec 7, 2024
b1fdcb6
adjust 2
ajit283 Dec 7, 2024
4a340bc
adjust 3
ajit283 Dec 7, 2024
a245fd1
adjust 4
ajit283 Dec 7, 2024
2708bc0
Merge branch 'branch-25.02' into go
ajit283 Dec 10, 2024
4e2202e
Merge branch 'branch-25.02' into c-expose-filtering
ajit283 Dec 10, 2024
05bf3c8
Merge pull request #7 from ajit283/c-expose-filtering
ajit283 Dec 10, 2024
efeaf39
Merge pull request #6 from ajit283/go
ajit283 Dec 10, 2024
6edcf93
Merge branch 'integration-branch' into extend-c-api
ajit283 Dec 10, 2024
311dd82
Revert "Merge branch 'integration-branch' into extend-c-api"
ajit283 Dec 10, 2024
feaef9b
Merge branch 'branch-25.02' into extend-c-api
cjnolet Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions cpp/include/cuvs/neighbors/cagra.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,42 @@ cuvsError_t cuvsCagraCompressionParamsCreate(cuvsCagraCompressionParams_t* param
*/
cuvsError_t cuvsCagraCompressionParamsDestroy(cuvsCagraCompressionParams_t params);

/**
* @}
*/

/**
* @defgroup cagra_c_extend_params C API for CUDA ANN Graph-based nearest neighbor search
* @{
*/
/**
* @brief Supplemental parameters to extend CAGRA Index
*
*/
struct cuvsCagraExtendParams {
/** Degree of input graph for pruning. */
uint32_t max_chunk_size;
/** Degree of output graph. */
};

typedef struct cuvsCagraExtendParams* cuvsCagraExtendParams_t;

/**
* @brief Allocate CAGRA Extend params, and populate with default values
*
* @param[in] params cuvsCagraExtendParams_t to allocate
* @return cuvsError_t
*/
cuvsError_t cuvsCagraExtendParamsCreate(cuvsCagraExtendParams_t* params);

/**
* @brief De-allocate CAGRA Extend params
*
* @param[in] params
* @return cuvsError_t
*/
cuvsError_t cuvsCagraExtendParamsDestroy(cuvsCagraExtendParams_t params);

/**
* @}
*/
Expand Down Expand Up @@ -336,6 +372,36 @@ cuvsError_t cuvsCagraBuild(cuvsResources_t res,
* @}
*/

/**
* @defgroup cagra_c_extend_params C API for CUDA ANN Graph-based nearest neighbor search
* @{
*/

/**
* @brief Extend a CAGRA 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`
*
* @param[in] res cuvsResources_t opaque C handle
* @param[in] params cuvsCagraExtendParams_t used to extend CAGRA index
* @param[in] additional_dataset DLManagedTensor* additional dataset
* @param[out] return_dataset DLManagedTensor* extended dataset
* @param[out] index cuvsCagraIndex_t extended CAGRA index
* @return cuvsError_t
*/
cuvsError_t cuvsCagraExtend(cuvsResources_t res,
cuvsCagraExtendParams_t params,
DLManagedTensor* additional_dataset,
DLManagedTensor* return_dataset,
cuvsCagraIndex_t index);

/**
* @}
*/

/**
* @defgroup cagra_c_index_search C API for CUDA ANN Graph-based nearest neighbor search
* @{
Expand Down
72 changes: 72 additions & 0 deletions cpp/src/neighbors/cagra_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,43 @@ void* _build(cuvsResources_t res, cuvsCagraIndexParams params, DLManagedTensor*
return index;
}

template <typename T>
void _extend(cuvsResources_t res,
cuvsCagraExtendParams params,
cuvsCagraIndex index,
DLManagedTensor* additional_dataset_tensor,
DLManagedTensor* return_tensor)
{
auto dataset = additional_dataset_tensor->dl_tensor;
auto return_dl_tensor = return_tensor->dl_tensor;
auto index_ptr = reinterpret_cast<cuvs::neighbors::cagra::index<T, uint32_t>*>(index.addr);
auto res_ptr = reinterpret_cast<raft::resources*>(res);

// TODO: use C struct here (see issue #487)
auto extend_params = cuvs::neighbors::cagra::extend_params();
extend_params.max_chunk_size = params.max_chunk_size;
Comment on lines +101 to +102
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be changed in this PR (especially since the rest of cagra/ivf-* doesn't follow this right now) - but I think we should start re-using the C-api parameter structs in the C++ api , like we are doing in the distances here

using DistanceType = cuvsDistanceType;
. This means we don't have to have two different identical structs in the C and C++ api, and copy between them you are doing here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to this. It increases the maintenance burden when we have parallel objects like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajit283 as Ben mentioned, it doesn't have to be changed in this PR, but can you create a Github issue for this and reference the issue number in a TODO here in the code? This way we won't lose sight of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem that arises when we do this is that we have to include the C header file in the C++ header file. While it works for distances enum, here the issue is that we also have dlpack headers which we don't link to the C++ target.

So we'll have to pull out all the structs into another file to make it work.

@cjnolet @benfred


if (cuvs::core::is_dlpack_device_compatible(dataset) &&
cuvs::core::is_dlpack_device_compatible(return_dl_tensor)) {
using mdspan_type = raft::device_matrix_view<T const, int64_t, raft::row_major>;
using mdspan_return_type = raft::device_matrix_view<T, int64_t, raft::row_major>;
auto mds = cuvs::core::from_dlpack<mdspan_type>(additional_dataset_tensor);
auto return_mds = cuvs::core::from_dlpack<mdspan_return_type>(return_tensor);
cuvs::neighbors::cagra::extend(*res_ptr, extend_params, mds, *index_ptr, return_mds);
} else if (cuvs::core::is_dlpack_host_compatible(dataset) &&
cuvs::core::is_dlpack_host_compatible(return_dl_tensor)) {
using mdspan_type = raft::host_matrix_view<T const, int64_t, raft::row_major>;
using mdspan_return_type = raft::device_matrix_view<T, int64_t, raft::row_major>;
auto mds = cuvs::core::from_dlpack<mdspan_type>(additional_dataset_tensor);
auto return_mds = cuvs::core::from_dlpack<mdspan_return_type>(return_tensor);
cuvs::neighbors::cagra::extend(*res_ptr, extend_params, mds, *index_ptr, return_mds);
} else {
RAFT_FAIL("Unsupported dataset DLtensor dtype: %d and bits: %d",
dataset.dtype.code,
dataset.dtype.bits);
}
}

template <typename T>
void _search(cuvsResources_t res,
cuvsCagraSearchParams params,
Expand Down Expand Up @@ -208,6 +245,30 @@ extern "C" cuvsError_t cuvsCagraBuild(cuvsResources_t res,
});
}

extern "C" cuvsError_t cuvsCagraExtend(cuvsResources_t res,
cuvsCagraExtendParams_t params,
DLManagedTensor* additional_dataset_tensor,
DLManagedTensor* return_dataset_tensor,
cuvsCagraIndex_t index_c_ptr)
{
return cuvs::core::translate_exceptions([=] {
auto dataset = additional_dataset_tensor->dl_tensor;
auto index = *index_c_ptr;

if ((dataset.dtype.code == kDLFloat) && (dataset.dtype.bits == 32)) {
_extend<float>(res, *params, index, additional_dataset_tensor, return_dataset_tensor);
} else if (dataset.dtype.code == kDLInt && dataset.dtype.bits == 8) {
_extend<int8_t>(res, *params, index, additional_dataset_tensor, return_dataset_tensor);
} else if (dataset.dtype.code == kDLUInt && dataset.dtype.bits == 8) {
_extend<uint8_t>(res, *params, index, additional_dataset_tensor, return_dataset_tensor);
} else {
RAFT_FAIL("Unsupported dataset DLtensor dtype: %d and bits: %d",
dataset.dtype.code,
dataset.dtype.bits);
}
});
}

extern "C" cuvsError_t cuvsCagraSearch(cuvsResources_t res,
cuvsCagraSearchParams_t params,
cuvsCagraIndex_t index_c_ptr,
Expand Down Expand Up @@ -283,6 +344,17 @@ extern "C" cuvsError_t cuvsCagraCompressionParamsDestroy(cuvsCagraCompressionPar
return cuvs::core::translate_exceptions([=] { delete params; });
}

extern "C" cuvsError_t cuvsCagraExtendParamsCreate(cuvsCagraExtendParams_t* params)
{
return cuvs::core::translate_exceptions(
[=] { *params = new cuvsCagraExtendParams{.max_chunk_size = 0}; });
}

extern "C" cuvsError_t cuvsCagraExtendParamsDestroy(cuvsCagraExtendParams_t params)
{
return cuvs::core::translate_exceptions([=] { delete params; });
}

extern "C" cuvsError_t cuvsCagraSearchParamsCreate(cuvsCagraSearchParams_t* params)
{
return cuvs::core::translate_exceptions([=] {
Expand Down
Loading
Loading