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

KNN bruteforce and IVF methods C/C++ API #33

Merged
merged 30 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
20658f8
Adding IVF-Flat and IVF-PQ
viclafargue Feb 7, 2024
6976acc
Adding bruteforce KNN
viclafargue Feb 7, 2024
9baadc8
Expose IVF methods
viclafargue Feb 7, 2024
9944bae
Adding bruteforce test
viclafargue Feb 9, 2024
b4fdee0
IVF-Flat and IVF-PQ tests
viclafargue Feb 16, 2024
85e0fba
Update IVF-Flat test
viclafargue Feb 19, 2024
1bae6b9
Update bruteforce KNN API
viclafargue Mar 1, 2024
2619b13
Update IVF-PQ testing
viclafargue Mar 1, 2024
540be0d
IVF-FLAT C API
viclafargue Mar 4, 2024
fe60dcc
Merge branch 'branch-24.04' into add-ivf_flat-and-ivf_pq
cjnolet Mar 4, 2024
e38d530
IVF-PQ C API
viclafargue Mar 4, 2024
089f67b
Merge branch 'branch-24.04' into add-ivf_flat-and-ivf_pq
cjnolet Mar 4, 2024
493165e
BRUTEFORCE C API
viclafargue Mar 5, 2024
33ffa18
REVERT cmake-format changes
viclafargue Mar 5, 2024
8738810
Fixing style/doc/comments issues
viclafargue Mar 8, 2024
1e1eb58
Solidify bruteforce index
viclafargue Mar 18, 2024
3e81649
IVF-FLAT compiled index
viclafargue Mar 18, 2024
0ac4820
IVF-PQ compiled index
viclafargue Mar 18, 2024
58dc152
Removing macros from headers
viclafargue Mar 18, 2024
f5aeffc
Merge remote-tracking branch 'rapidsai/branch-24.04' into add-ivf_fla…
viclafargue Mar 18, 2024
6aff838
IVF-Flat documentation
viclafargue Mar 18, 2024
5359afb
IVF-PQ documentation
viclafargue Mar 19, 2024
ab35e10
Bruteforce documentation
viclafargue Mar 19, 2024
675bf2a
C doc update
viclafargue Mar 19, 2024
00f67c2
C++ IVF-Flat doc examples
viclafargue Mar 19, 2024
ef5e3a4
C++ IVF-Flat and IVF-PQ doc examples
viclafargue Mar 19, 2024
53f6265
C++ Bruteforce doc examples
viclafargue Mar 19, 2024
82c003a
Add documentation for distance types
viclafargue Mar 19, 2024
d84d3cc
Python code generators
viclafargue Mar 19, 2024
7111bfb
Restore CMakeLists.txt style
viclafargue Mar 20, 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
26 changes: 26 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ include(cmake/thirdparty/get_cutlass.cmake)

add_library(
cuvs SHARED
src/neighbors/brute_force.cpp

src/neighbors/cagra_build_float.cpp
src/neighbors/cagra_build_int8.cpp
src/neighbors/cagra_build_uint8.cpp
Expand All @@ -198,6 +200,30 @@ add_library(
src/neighbors/cagra_serialize_float.cpp
src/neighbors/cagra_serialize_int8.cpp
src/neighbors/cagra_serialize_uint8.cpp

src/neighbors/ivf_flat_build_float.cpp
src/neighbors/ivf_flat_build_int8.cpp
src/neighbors/ivf_flat_build_uint8.cpp
src/neighbors/ivf_flat_extend_float.cpp
src/neighbors/ivf_flat_extend_int8.cpp
src/neighbors/ivf_flat_extend_uint8.cpp
src/neighbors/ivf_flat_search_float.cpp
src/neighbors/ivf_flat_search_int8.cpp
src/neighbors/ivf_flat_search_uint8.cpp
src/neighbors/ivf_flat_serialize_float.cpp
src/neighbors/ivf_flat_serialize_int8.cpp
src/neighbors/ivf_flat_serialize_uint8.cpp

src/neighbors/ivf_pq_build_float.cpp
src/neighbors/ivf_pq_build_int8.cpp
src/neighbors/ivf_pq_build_uint8.cpp
src/neighbors/ivf_pq_extend_float.cpp
src/neighbors/ivf_pq_extend_int8.cpp
src/neighbors/ivf_pq_extend_uint8.cpp
src/neighbors/ivf_pq_search_float.cpp
src/neighbors/ivf_pq_search_int8.cpp
src/neighbors/ivf_pq_search_uint8.cpp
src/neighbors/ivf_pq_serialize.cpp
)

target_compile_options(
Expand Down
39 changes: 39 additions & 0 deletions cpp/include/cuvs/neighbors/brute_force.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 <raft/distance/distance_types.hpp>
#include <raft_runtime/neighbors/brute_force.hpp>


namespace cuvs::neighbors::brute_force {

#define RAFT_INST_BFKNN(IDX_T, DATA_T, MATRIX_IDX_T, INDEX_LAYOUT, SEARCH_LAYOUT) \
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
void knn(raft::resources const& handle, \
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
raft::device_matrix_view<const DATA_T, MATRIX_IDX_T, INDEX_LAYOUT> index, \
raft::device_matrix_view<const DATA_T, MATRIX_IDX_T, SEARCH_LAYOUT> search, \
raft::device_matrix_view<IDX_T, MATRIX_IDX_T, raft::row_major> indices, \
raft::device_matrix_view<DATA_T, MATRIX_IDX_T, raft::row_major> distances, \
raft::distance::DistanceType metric = raft::distance::DistanceType::L2Unexpanded, \
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
std::optional<float> metric_arg = std::make_optional<float>(2.0f), \
std::optional<IDX_T> global_id_offset = std::nullopt);

RAFT_INST_BFKNN(int64_t, float, int64_t, raft::row_major, raft::row_major);

#undef RAFT_INST_BFKNN

} // namespace cuvs::neighbors::brute_force
79 changes: 79 additions & 0 deletions cpp/include/cuvs/neighbors/ivf_flat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 <raft_runtime/neighbors/ivf_flat.hpp>
viclafargue marked this conversation as resolved.
Show resolved Hide resolved


namespace cuvs::neighbors::ivf_flat {

using index_params = raft::neighbors::ivf_flat::index_params;
using search_params = raft::neighbors::ivf_flat::search_params;

template <typename T, typename IdxT>
using index = raft::neighbors::ivf_flat::index<T, IdxT>;

#define CUVS_IVF_FLAT(T, IdxT) \
auto build(raft::resources const& handle, \
const cuvs::neighbors::ivf_flat::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset); \
\
void build(raft::resources const& handle, \
const cuvs::neighbors::ivf_flat::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset, \
cuvs::neighbors::ivf_flat::index<T, IdxT>& idx); \
\
auto extend(raft::resources const& handle, \
raft::device_matrix_view<const T, IdxT, raft::row_major> new_vectors, \
std::optional<raft::device_vector_view<const IdxT, IdxT>> new_indices, \
const cuvs::neighbors::ivf_flat::index<T, IdxT>& orig_index); \
\
void extend(raft::resources const& handle, \
raft::device_matrix_view<const T, IdxT, raft::row_major> new_vectors, \
std::optional<raft::device_vector_view<const IdxT, IdxT>> new_indices, \
cuvs::neighbors::ivf_flat::index<T, IdxT>* idx); \
\
void search(raft::resources const& handle, \
const cuvs::neighbors::ivf_flat::search_params& params, \
cuvs::neighbors::ivf_flat::index<T, IdxT>& index, \
raft::device_matrix_view<const T, IdxT, raft::row_major> queries, \
raft::device_matrix_view<IdxT, IdxT, raft::row_major> neighbors, \
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @viclafargue for this PR! I think it is important to make a distinction between index type that is used to return neighbor indices (IdxT) and the index type used to calculate offsets in mdspans (lets call it MatIdxT). I think it would be good to have MatIdxT consistently int64_t, while IdxT could remain a template parameter. that we specialize for int64_t.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! Makes sense. Can totally do the change, however it won't work when IdxT != MatIdxT. An update of the RAFT runtime would be necessary for this to work.

raft::device_matrix_view<float, IdxT, raft::row_major> distances); \
\
void serialize_file(raft::resources const& handle, \
const std::string& filename, \
const cuvs::neighbors::ivf_flat::index<T, IdxT>& index); \
\
void deserialize_file(raft::resources const& handle, \
const std::string& filename, \
cuvs::neighbors::ivf_flat::index<T, IdxT>* index); \
\
void serialize(raft::resources const& handle, \
std::string& str, \
const cuvs::neighbors::ivf_flat::index<T, IdxT>& index); \
\
void deserialize(raft::resources const& handle, \
const std::string& str, \
cuvs::neighbors::ivf_flat::index<T, IdxT>* index);

CUVS_IVF_FLAT(float, uint64_t);
CUVS_IVF_FLAT(int8_t, uint64_t);
CUVS_IVF_FLAT(uint8_t, uint64_t);

#undef CUVS_IVF_FLAT

} // namespace cuvs::neighbors::ivf_flat
71 changes: 71 additions & 0 deletions cpp/include/cuvs/neighbors/ivf_pq.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 <raft_runtime/neighbors/ivf_pq.hpp>
viclafargue marked this conversation as resolved.
Show resolved Hide resolved


namespace cuvs::neighbors::ivf_pq {

using index_params = raft::neighbors::ivf_pq::index_params;
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
using search_params = raft::neighbors::ivf_pq::search_params;

template <typename IdxT>
using index = raft::neighbors::ivf_pq::index<IdxT>;

#define CUVS_IVF_PQ(T, IdxT) \
auto build(raft::resources const& handle, \
const cuvs::neighbors::ivf_pq::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset); \
\
void build(raft::resources const& handle, \
const cuvs::neighbors::ivf_pq::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset, \
cuvs::neighbors::ivf_pq::index<IdxT>* idx); \
\
auto extend(raft::resources const& handle, \
raft::device_matrix_view<const T, IdxT, raft::row_major> new_vectors, \
std::optional<raft::device_vector_view<const IdxT, IdxT>> new_indices, \
const cuvs::neighbors::ivf_pq::index<IdxT>& orig_index); \
\
void extend(raft::resources const& handle, \
raft::device_matrix_view<const T, IdxT, raft::row_major> new_vectors, \
std::optional<raft::device_vector_view<const IdxT, IdxT>> new_indices, \
cuvs::neighbors::ivf_pq::index<IdxT>* idx); \
\
void search(raft::resources const& handle, \
const cuvs::neighbors::ivf_pq::search_params& params, \
cuvs::neighbors::ivf_pq::index<IdxT>& index, \
raft::device_matrix_view<const T, IdxT, raft::row_major> queries, \
raft::device_matrix_view<IdxT, IdxT, raft::row_major> neighbors, \
raft::device_matrix_view<float, IdxT, raft::row_major> distances); \
\
void serialize(raft::resources const& handle, \
std::string& filename, \
const cuvs::neighbors::ivf_pq::index<IdxT>& index); \
\
void deserialize(raft::resources const& handle, \
const std::string& filename, \
cuvs::neighbors::ivf_pq::index<IdxT>* index);

CUVS_IVF_PQ(float, uint64_t);
CUVS_IVF_PQ(int8_t, uint64_t);
CUVS_IVF_PQ(uint8_t, uint64_t);

#undef CUVS_IVF_PQ

} // namespace cuvs::neighbors::ivf_pq
39 changes: 39 additions & 0 deletions cpp/src/neighbors/brute_force.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*/

#include <cuvs/neighbors/brute_force.hpp>

namespace cuvs::neighbors::brute_force {

#define RAFT_INST_BFKNN(IDX_T, DATA_T, MATRIX_IDX_T, INDEX_LAYOUT, SEARCH_LAYOUT) \
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
void knn(raft::resources const& handle, \
raft::device_matrix_view<const DATA_T, MATRIX_IDX_T, INDEX_LAYOUT> index, \
raft::device_matrix_view<const DATA_T, MATRIX_IDX_T, SEARCH_LAYOUT> search, \
raft::device_matrix_view<IDX_T, MATRIX_IDX_T, raft::row_major> indices, \
raft::device_matrix_view<DATA_T, MATRIX_IDX_T, raft::row_major> distances, \
raft::distance::DistanceType metric, \
std::optional<float> metric_arg, \
std::optional<IDX_T> global_id_offset) \
{ \
raft::runtime::neighbors::brute_force::knn(handle, index, search, indices, distances, \
metric, metric_arg, global_id_offset); \
}

RAFT_INST_BFKNN(int64_t, float, int64_t, raft::row_major, raft::row_major);

#undef RAFT_INST_BFKNN

} // namespace cuvs::neighbors::brute_force
41 changes: 41 additions & 0 deletions cpp/src/neighbors/ivf_flat_build_float.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
viclafargue marked this conversation as resolved.
Show resolved Hide resolved
* 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.
*/

#include <cuvs/neighbors/ivf_flat.hpp>

namespace cuvs::neighbors::ivf_flat {

#define CUVS_INST_IVF_FLAT_BUILD(T, IdxT) \
auto build(raft::resources const& handle, \
const cuvs::neighbors::ivf_flat::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset) \
{ \
return raft::runtime::neighbors::ivf_flat::build(handle, params, dataset); \
} \
\
void build(raft::resources const& handle, \
const cuvs::neighbors::ivf_flat::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset, \
cuvs::neighbors::ivf_flat::index<T, IdxT>& idx) \
{ \
raft::runtime::neighbors::ivf_flat::build(handle, params, dataset, idx); \
}

CUVS_INST_IVF_FLAT_BUILD(float, int64_t);

#undef CUVS_INST_IVF_FLAT_BUILD

} // namespace cuvs::neighbors::ivf_flat
41 changes: 41 additions & 0 deletions cpp/src/neighbors/ivf_flat_build_int8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.
*/

#include <cuvs/neighbors/ivf_flat.hpp>

namespace cuvs::neighbors::ivf_flat {

#define CUVS_INST_IVF_FLAT_BUILD(T, IdxT) \
auto build(raft::resources const& handle, \
const cuvs::neighbors::ivf_flat::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset) \
{ \
return raft::runtime::neighbors::ivf_flat::build(handle, params, dataset); \
} \
\
void build(raft::resources const& handle, \
const cuvs::neighbors::ivf_flat::index_params& params, \
raft::device_matrix_view<const T, IdxT, raft::row_major> dataset, \
cuvs::neighbors::ivf_flat::index<T, IdxT>& idx) \
{ \
raft::runtime::neighbors::ivf_flat::build(handle, params, dataset, idx); \
}

CUVS_INST_IVF_FLAT_BUILD(int8_t, int64_t);

#undef CUVS_INST_IVF_FLAT_BUILD

} // namespace cuvs::neighbors::ivf_flat
Loading
Loading