-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First verstion of a CAGRA API in pylibraft. Todos: - [x] C++ raft_runtime instantiations and void overloads - [x] Cython API - [x] Solve issue of `cagra_types.hpp` including `#include <raft/util/pow2_utils.cuh>` that makes it need nvcc, blocking a clean C++ only cython build - [x] Check in pytests - [x] Add examples to docstrings - [x] Accommodate for parameter rename of #1676 - [x] Accomodate changes of #1664 - [x] Move out of experimental namespace Authors: - Dante Gama Dessavre (https://github.com/dantegd) - Corey J. Nolet (https://github.com/cjnolet) Approvers: - Corey J. Nolet (https://github.com/cjnolet)
- Loading branch information
Showing
27 changed files
with
1,832 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,91 @@ | ||
/* | ||
* Copyright (c) 2023, 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 <raft/neighbors/cagra_types.hpp> | ||
#include <raft/neighbors/ivf_pq_types.hpp> | ||
#include <string> | ||
|
||
#include <raft/core/device_mdspan.hpp> | ||
#include <raft/core/host_device_accessor.hpp> | ||
#include <raft/core/mdspan.hpp> | ||
|
||
namespace raft::runtime::neighbors::cagra { | ||
|
||
// Using device and host_matrix_view avoids needing to typedef mutltiple mdspans based on accessors | ||
#define RAFT_INST_CAGRA_FUNCS(T, IdxT) \ | ||
auto build(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::device_matrix_view<const T, int64_t, row_major> dataset) \ | ||
->raft::neighbors::cagra::index<T, IdxT>; \ | ||
\ | ||
auto build(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::host_matrix_view<const T, int64_t, row_major> dataset) \ | ||
->raft::neighbors::cagra::index<T, IdxT>; \ | ||
\ | ||
void build_device(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::device_matrix_view<const T, int64_t, row_major> dataset, \ | ||
raft::neighbors::cagra::index<T, IdxT>& idx); \ | ||
\ | ||
void build_host(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::host_matrix_view<const T, int64_t, row_major> dataset, \ | ||
raft::neighbors::cagra::index<T, IdxT>& idx); \ | ||
\ | ||
void search(raft::resources const& handle, \ | ||
raft::neighbors::cagra::search_params const& params, \ | ||
const raft::neighbors::cagra::index<T, IdxT>& index, \ | ||
raft::device_matrix_view<const T, int64_t, row_major> queries, \ | ||
raft::device_matrix_view<IdxT, int64_t, row_major> neighbors, \ | ||
raft::device_matrix_view<float, int64_t, row_major> distances); \ | ||
void serialize_file(raft::resources const& handle, \ | ||
const std::string& filename, \ | ||
const raft::neighbors::cagra::index<T, IdxT>& index); \ | ||
\ | ||
void deserialize_file(raft::resources const& handle, \ | ||
const std::string& filename, \ | ||
raft::neighbors::cagra::index<T, IdxT>* index); \ | ||
void serialize(raft::resources const& handle, \ | ||
std::string& str, \ | ||
const raft::neighbors::cagra::index<T, IdxT>& index); \ | ||
\ | ||
void deserialize(raft::resources const& handle, \ | ||
const std::string& str, \ | ||
raft::neighbors::cagra::index<T, IdxT>* index); | ||
|
||
RAFT_INST_CAGRA_FUNCS(float, uint32_t); | ||
RAFT_INST_CAGRA_FUNCS(int8_t, uint32_t); | ||
RAFT_INST_CAGRA_FUNCS(uint8_t, uint32_t); | ||
|
||
#undef RAFT_INST_CAGRA_FUNCS | ||
|
||
#define RAFT_INST_CAGRA_OPTIMIZE(IdxT) \ | ||
void optimize_device(raft::resources const& res, \ | ||
raft::device_matrix_view<IdxT, int64_t, row_major> knn_graph, \ | ||
raft::host_matrix_view<IdxT, int64_t, row_major> new_graph); \ | ||
\ | ||
void optimize_host(raft::resources const& res, \ | ||
raft::host_matrix_view<IdxT, int64_t, row_major> knn_graph, \ | ||
raft::host_matrix_view<IdxT, int64_t, row_major> new_graph); | ||
|
||
RAFT_INST_CAGRA_OPTIMIZE(uint32_t); | ||
|
||
#undef RAFT_INST_CAGRA_OPTIMIZE | ||
|
||
} // namespace raft::runtime::neighbors::cagra |
This file contains 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,81 @@ | ||
/* | ||
* Copyright (c) 2023, 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. | ||
*/ | ||
|
||
#include <raft/neighbors/cagra.cuh> | ||
#include <raft/neighbors/ivf_pq.cuh> | ||
#include <raft/neighbors/ivf_pq_types.hpp> | ||
#include <raft_runtime/neighbors/cagra.hpp> | ||
|
||
namespace raft::runtime::neighbors::cagra { | ||
|
||
#define RAFT_INST_CAGRA_BUILD(T, IdxT) \ | ||
auto build(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::device_matrix_view<const T, int64_t, row_major> dataset) \ | ||
->raft::neighbors::cagra::index<T, IdxT> \ | ||
{ \ | ||
return raft::neighbors::cagra::build<T, IdxT>(handle, params, dataset); \ | ||
} \ | ||
\ | ||
auto build(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::host_matrix_view<const T, int64_t, row_major> dataset) \ | ||
->raft::neighbors::cagra::index<T, IdxT> \ | ||
{ \ | ||
return raft::neighbors::cagra::build<T, IdxT>(handle, params, dataset); \ | ||
} \ | ||
\ | ||
void build_device(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::device_matrix_view<const T, int64_t, row_major> dataset, \ | ||
raft::neighbors::cagra::index<T, IdxT>& idx) \ | ||
{ \ | ||
idx = build(handle, params, dataset); \ | ||
} \ | ||
\ | ||
void build_host(raft::resources const& handle, \ | ||
const raft::neighbors::cagra::index_params& params, \ | ||
raft::host_matrix_view<const T, int64_t, row_major> dataset, \ | ||
raft::neighbors::cagra::index<T, IdxT>& idx) \ | ||
{ \ | ||
idx = build(handle, params, dataset); \ | ||
} | ||
|
||
RAFT_INST_CAGRA_BUILD(float, uint32_t); | ||
RAFT_INST_CAGRA_BUILD(int8_t, uint32_t); | ||
RAFT_INST_CAGRA_BUILD(uint8_t, uint32_t); | ||
|
||
#undef RAFT_INST_CAGRA_BUILD | ||
|
||
#define RAFT_INST_CAGRA_OPTIMIZE(IdxT) \ | ||
void optimize_device(raft::resources const& handle, \ | ||
raft::device_matrix_view<IdxT, int64_t, row_major> knn_graph, \ | ||
raft::host_matrix_view<IdxT, int64_t, row_major> new_graph) \ | ||
{ \ | ||
raft::neighbors::cagra::optimize(handle, knn_graph, new_graph); \ | ||
} \ | ||
void optimize_host(raft::resources const& handle, \ | ||
raft::host_matrix_view<IdxT, int64_t, row_major> knn_graph, \ | ||
raft::host_matrix_view<IdxT, int64_t, row_major> new_graph) \ | ||
{ \ | ||
raft::neighbors::cagra::optimize(handle, knn_graph, new_graph); \ | ||
} | ||
|
||
RAFT_INST_CAGRA_OPTIMIZE(uint32_t); | ||
|
||
#undef RAFT_INST_CAGRA_OPTIMIZE | ||
|
||
} // namespace raft::runtime::neighbors::cagra |
This file contains 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,39 @@ | ||
/* | ||
* Copyright (c) 2023, 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. | ||
*/ | ||
|
||
#include <raft/neighbors/cagra.cuh> | ||
#include <raft_runtime/neighbors/cagra.hpp> | ||
|
||
namespace raft::runtime::neighbors::cagra { | ||
|
||
#define RAFT_INST_CAGRA_SEARCH(T, IdxT) \ | ||
void search(raft::resources const& handle, \ | ||
raft::neighbors::cagra::search_params const& params, \ | ||
const raft::neighbors::cagra::index<T, IdxT>& index, \ | ||
raft::device_matrix_view<const T, int64_t, row_major> queries, \ | ||
raft::device_matrix_view<IdxT, int64_t, row_major> neighbors, \ | ||
raft::device_matrix_view<float, int64_t, row_major> distances) \ | ||
{ \ | ||
raft::neighbors::cagra::search<T, IdxT>(handle, params, index, queries, neighbors, distances); \ | ||
} | ||
|
||
RAFT_INST_CAGRA_SEARCH(float, uint32_t); | ||
RAFT_INST_CAGRA_SEARCH(int8_t, uint32_t); | ||
RAFT_INST_CAGRA_SEARCH(uint8_t, uint32_t); | ||
|
||
#undef RAFT_INST_CAGRA_SEARCH | ||
|
||
} // namespace raft::runtime::neighbors::cagra |
This file contains 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,65 @@ | ||
/* | ||
* Copyright (c) 2023, 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. | ||
*/ | ||
|
||
#include <sstream> | ||
#include <string> | ||
|
||
#include <raft/core/device_resources.hpp> | ||
#include <raft/neighbors/cagra_serialize.cuh> | ||
#include <raft/neighbors/cagra_types.hpp> | ||
#include <raft_runtime/neighbors/cagra.hpp> | ||
|
||
namespace raft::runtime::neighbors::cagra { | ||
|
||
#define RAFT_INST_CAGRA_SERIALIZE(DTYPE) \ | ||
void serialize_file(raft::resources const& handle, \ | ||
const std::string& filename, \ | ||
const raft::neighbors::cagra::index<DTYPE, uint32_t>& index) \ | ||
{ \ | ||
raft::neighbors::cagra::serialize(handle, filename, index); \ | ||
}; \ | ||
\ | ||
void deserialize_file(raft::resources const& handle, \ | ||
const std::string& filename, \ | ||
raft::neighbors::cagra::index<DTYPE, uint32_t>* index) \ | ||
{ \ | ||
if (!index) { RAFT_FAIL("Invalid index pointer"); } \ | ||
*index = raft::neighbors::cagra::deserialize<DTYPE, uint32_t>(handle, filename); \ | ||
}; \ | ||
void serialize(raft::resources const& handle, \ | ||
std::string& str, \ | ||
const raft::neighbors::cagra::index<DTYPE, uint32_t>& index) \ | ||
{ \ | ||
std::stringstream os; \ | ||
raft::neighbors::cagra::serialize(handle, os, index); \ | ||
str = os.str(); \ | ||
} \ | ||
\ | ||
void deserialize(raft::resources const& handle, \ | ||
const std::string& str, \ | ||
raft::neighbors::cagra::index<DTYPE, uint32_t>* index) \ | ||
{ \ | ||
std::istringstream is(str); \ | ||
if (!index) { RAFT_FAIL("Invalid index pointer"); } \ | ||
*index = raft::neighbors::cagra::deserialize<DTYPE, uint32_t>(handle, is); \ | ||
} | ||
|
||
RAFT_INST_CAGRA_SERIALIZE(float); | ||
RAFT_INST_CAGRA_SERIALIZE(int8_t); | ||
RAFT_INST_CAGRA_SERIALIZE(uint8_t); | ||
|
||
#undef RAFT_INST_CAGRA_SERIALIZE | ||
} // namespace raft::runtime::neighbors::cagra |
This file contains 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 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 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.