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 all 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
67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,73 @@ auto metric = raft::distance::DistanceType::L2SqrtExpanded;
raft::distance::pairwise_distance(handle, input.view(), input.view(), output.view(), metric);
```

It's also possible to create `raft::device_mdspan` views to invoke the same API with raw pointers and shape information:

```c++
#include <raft/core/handle.hpp>
#include <raft/core/device_mdspan.hpp>
#include <raft/random/make_blobs.cuh>
#include <raft/distance/distance.cuh>

raft::handle_t handle;

int n_samples = 5000;
int n_features = 50;

float *input;
int *labels;
float *output;

...
// Allocate input, labels, and output pointers
...

auto input_view = raft::make_device_matrix_view(input, n_samples, n_features);
auto labels_view = raft::make_device_vector_view(labels, n_samples);
auto output_view = raft::make_device_matrix_view(output, n_samples, n_samples);

raft::random::make_blobs(handle, input_view, labels_view);

auto metric = raft::distance::DistanceType::L2SqrtExpanded;
raft::distance::pairwise_distance(handle, input_view, input_view, output_view, metric);
```


### Python Example

The `pylibraft` package contains a Python API for RAFT algorithms and primitives. `pylibraft` integrates nicely into other libraries by being very lightweight with minimal dependencies and accepting any object that supports the `__cuda_array_interface__`, such as [CuPy's ndarray](https://docs.cupy.dev/en/stable/user_guide/interoperability.html#rmm). The number of RAFT algorithms exposed in this package is continuing to grow from release to release.

The example below demonstrates computing the pairwise Euclidean distances between CuPy arrays. `pylibraft` is a low-level API that prioritizes efficiency and simplicity over being pythonic, which is shown here by pre-allocating the output memory before invoking the `pairwise_distance` function. Note that CuPy is not a required dependency for `pylibraft`.
The example below demonstrates computing the pairwise Euclidean distances between CuPy arrays. Note that CuPy is not a required dependency for `pylibraft`.

```python
import cupy as cp

from pylibraft.distance import pairwise_distance

n_samples = 5000
n_features = 50

in1 = cp.random.random_sample((n_samples, n_features), dtype=cp.float32)
in2 = cp.random.random_sample((n_samples, n_features), dtype=cp.float32)

output = pairwise_distance(in1, in2, metric="euclidean")
```

The `output` array supports [__cuda_array_interface__](https://numba.pydata.org/numba-doc/dev/cuda/cuda_array_interface.html#cuda-array-interface-version-2) so it is interoperable with other libraries like CuPy, Numba, and PyTorch that also support it.

Below is an example of converting the output `pylibraft.device_ndarray` to a CuPy array:
```python
cupy_array = cp.asarray(output)
```

And converting to a PyTorch tensor:
```python
import torch

torch_tensor = torch.as_tensor(output, device='cuda')
```

`pylibraft` also supports writing to a pre-allocated output array so any `__cuda_array_interface__` supported array can be written to in-place:

```python
import cupy as cp
Expand All @@ -95,9 +157,10 @@ in1 = cp.random.random_sample((n_samples, n_features), dtype=cp.float32)
in2 = cp.random.random_sample((n_samples, n_features), dtype=cp.float32)
output = cp.empty((n_samples, n_samples), dtype=cp.float32)

pairwise_distance(in1, in2, output, metric="euclidean")
pairwise_distance(in1, in2, out=output, metric="euclidean")
```


## Installing

RAFT itself can be installed through conda, [Cmake Package Manager (CPM)](https://github.com/cpm-cmake/CPM.cmake), or by building the repository from source. Please refer to the [build instructions](docs/source/build.md) for more a comprehensive guide on building RAFT and using it in downstream projects.
Expand Down
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
2 changes: 2 additions & 0 deletions python/pylibraft/pylibraft/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
# limitations under the License.
#


from .cuda import Stream
from .device_ndarray import device_ndarray
from .handle import Handle
Loading