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

IVF-PQ Python wrappers #970

Merged
merged 38 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
753a058
renaming to device_ndarray
cjnolet Nov 10, 2022
2bd251a
Using pre-commit
cjnolet Nov 10, 2022
ac561ec
Making pairwise distance output optional
cjnolet Nov 10, 2022
f5c4a00
Updating docs for device_ndarray
cjnolet Nov 10, 2022
3d0114b
Updating style
cjnolet Nov 10, 2022
5e9162d
Add Python wrapper for IVF-PQ
tfeher Oct 30, 2022
571fb59
Added test, works so far<
tfeher Nov 3, 2022
00da77a
Move build params to __init__
tfeher Nov 3, 2022
bc4c66c
Extend method wrapped
tfeher Nov 3, 2022
52feeac
Remove preferred_thread_block_size search param, since it will be dep…
tfeher Nov 7, 2022
b259e08
Improve checks on input arrays, and test it
tfeher Nov 7, 2022
c9f38cd
Forward codebook_kind param and test build params
tfeher Nov 7, 2022
1aa9b26
Handle search type params, add tests
tfeher Nov 7, 2022
eb43663
Separate C interface into pxd file
tfeher Nov 8, 2022
a7b8cdc
wip
tfeher Nov 8, 2022
7994a62
Refactored IVF-PQ Python API as a thin wrapper around the cpp objects
tfeher Nov 9, 2022
84c7271
Forward exceptions from build and extend to Python
tfeher Nov 9, 2022
21391c0
Move is_c_cond into utils, remove per_ prefix from coodebook kind str…
tfeher Nov 11, 2022
1b5ff59
Handle memory resource arg, add examples
tfeher Nov 11, 2022
d4a11c2
Fix extend specialization
tfeher Nov 11, 2022
f76b438
Define string representation for the index
tfeher Nov 11, 2022
a6c9d35
Adding device_ndarray.empty() staticmethod
cjnolet Nov 14, 2022
340300f
Using pylibraft.device_ndarray in pairwise_distances
cjnolet Nov 14, 2022
9d8d2b9
Removing acciddentally checked-in file
cjnolet Nov 14, 2022
45e5212
Update python/pylibraft/pylibraft/common/device_ndarray.py
cjnolet Nov 14, 2022
3f8dcc4
Updating usage examples in readme
cjnolet Nov 14, 2022
c4f9c7f
Adding example of converting device_ndarray to torch tensor
cjnolet Nov 14, 2022
ec3cc6e
Adding to pairwise distance pydocs
cjnolet Nov 14, 2022
f3d59f3
Making in-place output optional for fused_l2_nn_argmin
cjnolet Nov 14, 2022
ec93403
Adding link to cuda array interface on README
cjnolet Nov 14, 2022
8014eb5
Merge branch 'fea-2212-device_buffer' into fea-ivf-pq-python-wrapper-…
cjnolet Nov 14, 2022
c8fba7a
Tests are passing. Committing.
cjnolet Nov 14, 2022
09485bc
Fixing style
cjnolet Nov 14, 2022
2f40799
Merge branch 'branch-22.12' into fea_ivf_pq_python_wrapper
cjnolet Nov 14, 2022
f0b3fee
Fixing typo for codespell
cjnolet Nov 14, 2022
0fd422e
Use np dtypes, add string repr. for SearchParams
tfeher Nov 14, 2022
14c3718
Use cuda_interruptible context
tfeher Nov 14, 2022
d45fd8f
Fix style
tfeher Nov 14, 2022
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
17 changes: 17 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,21 @@ if(RAFT_COMPILE_DIST_LIBRARY)
src/distance/specializations/fused_l2_nn_double_int64.cu
src/distance/specializations/fused_l2_nn_float_int.cu
src/distance/specializations/fused_l2_nn_float_int64.cu
src/nn/specializations/detail/ivfpq_build.cu
src/nn/specializations/detail/ivfpq_compute_similarity_float_fast.cu
src/nn/specializations/detail/ivfpq_compute_similarity_float_no_basediff.cu
src/nn/specializations/detail/ivfpq_compute_similarity_float_no_smem_lut.cu
src/nn/specializations/detail/ivfpq_compute_similarity_fp8s_fast.cu
src/nn/specializations/detail/ivfpq_compute_similarity_fp8s_no_basediff.cu
src/nn/specializations/detail/ivfpq_compute_similarity_fp8s_no_smem_lut.cu
src/nn/specializations/detail/ivfpq_compute_similarity_fp8u_fast.cu
src/nn/specializations/detail/ivfpq_compute_similarity_fp8u_no_basediff.cu
src/nn/specializations/detail/ivfpq_compute_similarity_fp8u_no_smem_lut.cu
src/nn/specializations/detail/ivfpq_compute_similarity_half_fast.cu
src/nn/specializations/detail/ivfpq_compute_similarity_half_no_basediff.cu
src/nn/specializations/detail/ivfpq_compute_similarity_half_no_smem_lut.cu
src/nn/specializations/detail/ivfpq_search.cu
src/nn/specializations/detail/ivfpq_search_float_uint64_t.cu
src/random/specializations/rmat_rectangular_generator_int_double.cu
src/random/specializations/rmat_rectangular_generator_int64_double.cu
src/random/specializations/rmat_rectangular_generator_int_float.cu
Expand Down Expand Up @@ -400,6 +415,8 @@ if(RAFT_COMPILE_NN_LIBRARY)
src/nn/specializations/detail/ivfpq_compute_similarity_half_fast.cu
src/nn/specializations/detail/ivfpq_compute_similarity_half_no_basediff.cu
src/nn/specializations/detail/ivfpq_compute_similarity_half_no_smem_lut.cu
src/nn/specializations/detail/ivfpq_build.cu
src/nn/specializations/detail/ivfpq_search.cu
src/nn/specializations/detail/ivfpq_search_float_int64_t.cu
src/nn/specializations/detail/ivfpq_search_float_uint32_t.cu
src/nn/specializations/detail/ivfpq_search_float_uint64_t.cu
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2022, 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/ivf_pq_types.hpp>

namespace raft::neighbors ::ivf_pq {

#define RAFT_INST_SEARCH(T, IdxT) \
void search(const handle_t&, \
const search_params&, \
const index<IdxT>&, \
const T*, \
uint32_t, \
uint32_t, \
IdxT*, \
float*, \
rmm::mr::device_memory_resource*);

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

#undef RAFT_INST_SEARCH

// We define overloads for build and extend with void return type. This is used in the Cython
// wrappers, where exception handling is not compatible with return type that has nontrivial
// constructor.
#define RAFT_INST_BUILD_EXTEND(T, IdxT) \
auto build(const handle_t& handle, \
const index_params& params, \
const T* dataset, \
IdxT n_rows, \
uint32_t dim) \
->index<IdxT>; \
\
auto extend(const handle_t& handle, \
const index<IdxT>& orig_index, \
const T* new_vectors, \
const IdxT* new_indices, \
IdxT n_rows) \
->index<IdxT>; \
\
void build(const handle_t& handle, \
const index_params& params, \
const T* dataset, \
IdxT n_rows, \
uint32_t dim, \
index<IdxT>* idx); \
\
void extend(const handle_t& handle, \
index<IdxT>* idx, \
const T* new_vectors, \
const IdxT* new_indices, \
IdxT n_rows);

RAFT_INST_BUILD_EXTEND(float, uint64_t)
RAFT_INST_BUILD_EXTEND(int8_t, uint64_t)
RAFT_INST_BUILD_EXTEND(uint8_t, uint64_t)

#undef RAFT_INST_BUILD_EXTEND

} // namespace raft::neighbors::ivf_pq
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#include <raft/core/handle.hpp>
#include <raft/distance/distance_types.hpp>
#include <raft/spatial/knn/faiss_mr.hpp>

namespace raft {
namespace spatial {
Expand Down
66 changes: 66 additions & 0 deletions cpp/src/nn/specializations/detail/ivfpq_build.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2022, 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/ivf_pq.cuh>
#include <raft/neighbors/specializations/ivf_pq_specialization.hpp>

namespace raft::neighbors::ivf_pq {

#define RAFT_INST_BUILD_EXTEND(T, IdxT) \
auto build(const handle_t& handle, \
const index_params& params, \
const T* dataset, \
IdxT n_rows, \
uint32_t dim) \
->index<IdxT> \
{ \
return build<T, IdxT>(handle, params, dataset, n_rows, dim); \
} \
auto extend(const handle_t& handle, \
const index<IdxT>& orig_index, \
const T* new_vectors, \
const IdxT* new_indices, \
IdxT n_rows) \
->index<IdxT> \
{ \
return extend<T, IdxT>(handle, orig_index, new_vectors, new_indices, n_rows); \
} \
\
void build(const handle_t& handle, \
const index_params& params, \
const T* dataset, \
IdxT n_rows, \
uint32_t dim, \
index<IdxT>* idx) \
{ \
*idx = build<T, IdxT>(handle, params, dataset, n_rows, dim); \
} \
void extend(const handle_t& handle, \
index<IdxT>* idx, \
const T* new_vectors, \
const IdxT* new_indices, \
IdxT n_rows) \
{ \
extend<T, IdxT>(handle, *idx, new_vectors, new_indices, n_rows); \
}

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

#undef RAFT_INST_BUILD_EXTEND

} // namespace raft::neighbors::ivf_pq
43 changes: 43 additions & 0 deletions cpp/src/nn/specializations/detail/ivfpq_search.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2022, 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/ivf_pq.cuh>
#include <raft/neighbors/specializations/detail/ivf_pq_search.cuh>
#include <raft/neighbors/specializations/ivf_pq_specialization.hpp>

namespace raft::neighbors::ivf_pq {

#define RAFT_SEARCH_INST(T, IdxT) \
void search(const handle_t& handle, \
const search_params& params, \
const index<IdxT>& idx, \
const T* queries, \
uint32_t n_queries, \
uint32_t k, \
IdxT* neighbors, \
float* distances, \
rmm::mr::device_memory_resource* mr) \
{ \
search<T, IdxT>(handle, params, idx, queries, n_queries, k, neighbors, distances, mr); \
}

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

#undef RAFT_INST_SEARCH

} // namespace raft::neighbors::ivf_pq
1 change: 1 addition & 0 deletions python/pylibraft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ rapids_cython_init()

add_subdirectory(pylibraft/common)
add_subdirectory(pylibraft/distance)
add_subdirectory(pylibraft/neighbors)
add_subdirectory(pylibraft/random)
add_subdirectory(pylibraft/cluster)

Expand Down
28 changes: 28 additions & 0 deletions python/pylibraft/pylibraft/neighbors/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# =============================================================================
# Copyright (c) 2022, 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.
# =============================================================================

# Set the list of Cython files to build
set(cython_sources ivf_pq.pyx)
set(linked_libraries raft::raft raft::distance)

# Build all of the Cython targets
rapids_cython_create_modules(
CXX
SOURCE_FILES "${cython_sources}"
LINKED_LIBRARIES "${linked_libraries}"
MODULE_PREFIX nn_)

foreach(cython_module IN LISTS RAPIDS_CYTHON_CREATED_TARGETS)
set_target_properties(${cython_module} PROPERTIES INSTALL_RPATH "\$ORIGIN;\$ORIGIN/../library")
endforeach()
14 changes: 14 additions & 0 deletions python/pylibraft/pylibraft/neighbors/__init__.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2022, 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.
#
14 changes: 14 additions & 0 deletions python/pylibraft/pylibraft/neighbors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2022, 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.
#
Loading