1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
//! Brute Force KNN
use std::io::{stderr, Write};
use crate::distance_type::DistanceType;
use crate::dlpack::ManagedTensor;
use crate::error::{check_cuvs, Result};
use crate::resources::Resources;
/// Brute Force KNN Index
#[derive(Debug)]
pub struct Index(ffi::cuvsBruteForceIndex_t);
impl Index {
/// Builds a new Brute Force KNN Index from the dataset for efficient search.
///
/// # Arguments
///
/// * `res` - Resources to use
/// * `metric` - DistanceType to use for building the index
/// * `metric_arg` - Optional value of `p` for Minkowski distances
/// * `dataset` - A row-major matrix on either the host or device to index
pub fn build<T: Into<ManagedTensor>>(
res: &Resources,
metric: DistanceType,
metric_arg: Option<f32>,
dataset: T,
) -> Result<Index> {
let dataset: ManagedTensor = dataset.into();
let index = Index::new()?;
unsafe {
check_cuvs(ffi::cuvsBruteForceBuild(
res.0,
dataset.as_ptr(),
metric,
metric_arg.unwrap_or(2.0),
index.0,
))?;
}
Ok(index)
}
/// Creates a new empty index
pub fn new() -> Result<Index> {
unsafe {
let mut index = std::mem::MaybeUninit::<ffi::cuvsBruteForceIndex_t>::uninit();
check_cuvs(ffi::cuvsBruteForceIndexCreate(index.as_mut_ptr()))?;
Ok(Index(index.assume_init()))
}
}
/// Perform a Nearest Neighbors search on the Index
///
/// # Arguments
///
/// * `res` - Resources to use
/// * `queries` - A matrix in device memory to query for
/// * `neighbors` - Matrix in device memory that receives the indices of the nearest neighbors
/// * `distances` - Matrix in device memory that receives the distances of the nearest neighbors
pub fn search(
&self,
res: &Resources,
queries: &ManagedTensor,
neighbors: &ManagedTensor,
distances: &ManagedTensor,
) -> Result<()> {
unsafe {
let prefilter = ffi::cuvsFilter {
addr: 0,
type_: ffi::cuvsFilterType::NO_FILTER,
};
check_cuvs(ffi::cuvsBruteForceSearch(
res.0,
self.0,
queries.as_ptr(),
neighbors.as_ptr(),
distances.as_ptr(),
prefilter,
))
}
}
}
impl Drop for Index {
fn drop(&mut self) {
if let Err(e) = check_cuvs(unsafe { ffi::cuvsBruteForceIndexDestroy(self.0) }) {
write!(stderr(), "failed to call bruteForceIndexDestroy {:?}", e)
.expect("failed to write to stderr");
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use mark_flaky_tests::flaky;
use ndarray::s;
use ndarray_rand::rand_distr::Uniform;
use ndarray_rand::RandomExt;
fn test_bfknn(metric: DistanceType) {
let res = Resources::new().unwrap();
// Create a new random dataset to index
let n_datapoints = 16;
let n_features = 8;
let dataset_host =
ndarray::Array::<f32, _>::random((n_datapoints, n_features), Uniform::new(0., 1.0));
let dataset = ManagedTensor::from(&dataset_host).to_device(&res).unwrap();
println!("dataset {:#?}", dataset_host);
// build the brute force index
let index =
Index::build(&res, metric, None, dataset).expect("failed to create brute force index");
res.sync_stream().unwrap();
// use the first 4 points from the dataset as queries : will test that we get them back
// as their own nearest neighbor
let n_queries = 4;
let queries = dataset_host.slice(s![0..n_queries, ..]);
let k = 4;
println!("queries! {:#?}", queries);
let queries = ManagedTensor::from(&queries).to_device(&res).unwrap();
let mut neighbors_host = ndarray::Array::<i64, _>::zeros((n_queries, k));
let neighbors = ManagedTensor::from(&neighbors_host)
.to_device(&res)
.unwrap();
let mut distances_host = ndarray::Array::<f32, _>::zeros((n_queries, k));
let distances = ManagedTensor::from(&distances_host)
.to_device(&res)
.unwrap();
index
.search(&res, &queries, &neighbors, &distances)
.unwrap();
// Copy back to host memory
distances.to_host(&res, &mut distances_host).unwrap();
neighbors.to_host(&res, &mut neighbors_host).unwrap();
res.sync_stream().unwrap();
println!("distances {:#?}", distances_host);
println!("neighbors {:#?}", neighbors_host);
// nearest neighbors should be themselves, since queries are from the
// dataset
assert_eq!(neighbors_host[[0, 0]], 0);
assert_eq!(neighbors_host[[1, 0]], 1);
assert_eq!(neighbors_host[[2, 0]], 2);
assert_eq!(neighbors_host[[3, 0]], 3);
}
/*
#[test]
fn test_cosine() {
test_bfknn(DistanceType::CosineExpanded);
}
*/
#[flaky]
fn test_l2() {
test_bfknn(DistanceType::L2Expanded);
}
// NOTE: brute_force multiple-search test is omitted here because the C++
// brute_force::index stores a non-owning view into the dataset. Building
// from device data via `build()` drops the ManagedTensor after the call,
// leaving a dangling pointer. A follow-up PR will add dataset lifetime
// enforcement (DatasetOwnership<'a>) to make this safe.
// See: https://github.com/rapidsai/cuvs/issues/1838
}