-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuvs_ann.cu
369 lines (317 loc) · 13.3 KB
/
cuvs_ann.cu
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (c) 2023-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 "CLI11.hpp"
#include "common.cuh"
#include <cuvs/neighbors/brute_force.hpp>
#include <cuvs/neighbors/cagra.hpp>
#include <cuvs/neighbors/ivf_flat.hpp>
#include <raft/core/device_mdarray.hpp>
#include <raft/core/device_resources.hpp>
#include <raft/core/resource/thrust_policy.hpp>
#include <raft/stats/neighborhood_recall.cuh>
#include <raft/util/cudart_utils.hpp>
#include <rmm/aligned.hpp>
#include <rmm/mr/device/cuda_async_memory_resource.hpp>
#include <rmm/mr/device/cuda_memory_resource.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>
#include <rmm/mr/device/managed_memory_resource.hpp>
#include <rmm/mr/device/owning_wrapper.hpp>
#include <rmm/mr/device/pool_memory_resource.hpp>
#include <rmm/mr/device/prefetch_resource_adaptor.hpp>
#include <rmm/mr/device/statistics_resource_adaptor.hpp>
#include <rmm/mr/device/system_memory_resource.hpp>
#include <thrust/copy.h>
#include <thrust/device_ptr.h>
#include <thrust/iterator/counting_iterator.h>
#include <chrono>
#include <cstdint>
#include <optional>
namespace {
/// MR factory functions
inline auto make_cuda() {
return std::make_shared<rmm::mr::cuda_memory_resource>();
}
inline auto make_async() {
return std::make_shared<rmm::mr::cuda_async_memory_resource>();
}
inline auto make_managed() {
return std::make_shared<rmm::mr::managed_memory_resource>();
}
inline auto make_prefetch() {
return rmm::mr::make_owning_wrapper<rmm::mr::prefetch_resource_adaptor>(
make_managed());
}
inline auto make_managed_pool() {
return rmm::mr::make_owning_wrapper<rmm::mr::pool_memory_resource>(
make_managed(), rmm::percent_of_free_device_memory(50));
}
inline auto make_prefetch_pool() {
return rmm::mr::make_owning_wrapper<rmm::mr::pool_memory_resource>(
make_prefetch(), rmm::percent_of_free_device_memory(50));
}
inline auto make_system() {
return std::make_shared<rmm::mr::system_memory_resource>();
}
inline auto make_system_pool() {
return rmm::mr::make_owning_wrapper<rmm::mr::pool_memory_resource>(
make_system(), rmm::percent_of_free_device_memory(50));
}
inline std::shared_ptr<rmm::mr::device_memory_resource>
create_memory_resource(std::string const &allocation_mode) {
if (allocation_mode == "cuda")
return make_cuda();
if (allocation_mode == "async")
return make_async();
if (allocation_mode == "managed")
return make_managed();
if (allocation_mode == "prefetch")
return make_prefetch();
if (allocation_mode == "managed_pool")
return make_managed_pool();
if (allocation_mode == "prefetch_pool")
return make_prefetch_pool();
if (allocation_mode == "system")
return make_system();
if (allocation_mode == "system_pool")
return make_system_pool();
return make_managed();
}
template <typename T>
auto calc_recall(const std::vector<T> &expected_idx,
const std::vector<T> &actual_idx, size_t rows, size_t cols) {
size_t match_count = 0;
size_t total_count = static_cast<size_t>(rows) * static_cast<size_t>(cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t k = 0; k < cols; ++k) {
size_t idx_k = i * cols + k; // row major assumption!
auto act_idx = actual_idx[idx_k];
for (size_t j = 0; j < cols; ++j) {
size_t idx = i * cols + j; // row major assumption!
auto exp_idx = expected_idx[idx];
if (act_idx == exp_idx) {
match_count++;
break;
}
}
}
}
return std::make_tuple(static_cast<double>(match_count) /
static_cast<double>(total_count),
match_count, total_count);
}
} // namespace
void ivf_search(raft::device_resources const &res,
raft::device_matrix_view<const float, int64_t> dataset,
raft::device_matrix_view<const float, int64_t> queries,
int64_t n_list, int64_t n_probe, int64_t top_k) {
using namespace cuvs::neighbors;
std::cout << "Performing IVF-FLAT search" << std::endl;
int64_t n_queries = queries.extent(0);
auto neighbors =
raft::make_device_matrix<int64_t, int64_t>(res, n_queries, top_k);
auto distances =
raft::make_device_matrix<float, int64_t>(res, n_queries, top_k);
{
// Build and search the IVF-FLAT index
ivf_flat::index_params index_params;
index_params.n_lists = n_list;
index_params.kmeans_trainset_fraction = 0.1;
index_params.kmeans_n_iters = 100;
index_params.metric = cuvs::distance::DistanceType::L2Expanded;
auto s = std::chrono::high_resolution_clock::now();
auto index = ivf_flat::build(res, index_params, dataset);
auto e = std::chrono::high_resolution_clock::now();
std::cout
<< "[TIME] Train and index: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(e - s).count()
<< " ms" << std::endl;
std::cout << "[INFO] Number of clusters " << index.n_lists()
<< ", number of vectors added to index " << index.size()
<< std::endl;
// Perform the search operation
ivf_flat::search_params search_params;
search_params.n_probes = n_probe;
s = std::chrono::high_resolution_clock::now();
ivf_flat::search(res, search_params, index, queries, neighbors.view(),
distances.view());
raft::resource::sync_stream(res);
e = std::chrono::high_resolution_clock::now();
std::cout
<< "[TIME] Search: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(e - s).count()
<< " ms" << std::endl;
}
{
// Brute force search for recall calculation
auto reference_neighbors =
raft::make_device_matrix<int64_t, int64_t>(res, n_queries, top_k);
auto reference_distances =
raft::make_device_matrix<float, int64_t>(res, n_queries, top_k);
auto brute_force_index = cuvs::neighbors::brute_force::build(res, dataset);
cuvs::neighbors::brute_force::search(res, brute_force_index, queries,
reference_neighbors.view(),
reference_distances.view());
raft::resource::sync_stream(res);
// Calculate recall
size_t size = n_queries * top_k;
std::vector<int64_t> neighbors_h(size);
std::vector<int64_t> neighbors_ref_h(size);
auto stream = raft::resource::get_cuda_stream(res);
raft::copy(neighbors_h.data(), neighbors.data_handle(), size, stream);
raft::copy(neighbors_ref_h.data(), reference_neighbors.data_handle(), size,
stream);
auto [recall, _, __] =
calc_recall<int64_t>(neighbors_ref_h, neighbors_h, n_queries, top_k);
std::cout << "[INFO] Recall@" << top_k << ": " << recall << std::endl;
}
}
void cagra_search(raft::device_resources const &res,
raft::device_matrix_view<const float, int64_t> dataset,
raft::device_matrix_view<const float, int64_t> queries,
int64_t itopk_size,
int64_t top_k) {
using namespace cuvs::neighbors;
std::cout << "Performing CAGRA search" << std::endl;
int64_t n_queries = queries.extent(0);
auto neighbors =
raft::make_device_matrix<uint32_t, int64_t>(res, n_queries, top_k);
auto distances =
raft::make_device_matrix<float, int64_t>(res, n_queries, top_k);
{
// Build and search the CAGRA index
cagra::index_params index_params;
index_params.metric = cuvs::distance::DistanceType::L2Expanded;
index_params.graph_build_params = cagra::graph_build_params::ivf_pq_params(
dataset.extents(),
index_params.metric
);
auto s = std::chrono::high_resolution_clock::now();
auto index = cagra::build(res, index_params, dataset);
raft::resource::sync_stream(res);
auto e = std::chrono::high_resolution_clock::now();
std::cout
<< "[TIME] Train and index: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(e - s).count()
<< " ms" << std::endl;
std::cout << "CAGRA index has " << index.size() << " vectors" << std::endl;
std::cout << "CAGRA graph has degree " << index.graph_degree()
<< ", graph size [" << index.graph().extent(0) << ", "
<< index.graph().extent(1) << "]" << std::endl;
// Perform the search operation
cagra::search_params search_params;
search_params.itopk_size = itopk_size;
s = std::chrono::high_resolution_clock::now();
cagra::search(res, search_params, index, queries, neighbors.view(),
distances.view());
raft::resource::sync_stream(res);
e = std::chrono::high_resolution_clock::now();
std::cout
<< "[TIME] Search: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(e - s).count()
<< " ms" << std::endl;
}
{
// Brute force search for recall calculation
auto reference_neighbors =
raft::make_device_matrix<int64_t, int64_t>(res, n_queries, top_k);
auto reference_distances =
raft::make_device_matrix<float, int64_t>(res, n_queries, top_k);
auto brute_force_index = cuvs::neighbors::brute_force::build(res, dataset);
cuvs::neighbors::brute_force::search(res, brute_force_index, queries,
reference_neighbors.view(),
reference_distances.view());
raft::resource::sync_stream(res);
// Calculate recall
size_t size = n_queries * top_k;
std::vector<uint32_t> neighbors_h(size);
std::vector<int64_t> neighbors_ref_h(size);
auto stream = raft::resource::get_cuda_stream(res);
raft::copy(neighbors_h.data(), neighbors.data_handle(), size, stream);
raft::copy(neighbors_ref_h.data(), reference_neighbors.data_handle(), size,
stream);
std::vector<int64_t> neighbors_h_64b(neighbors_h.begin(),
neighbors_h.end());
auto [recall, _, __] = calc_recall<int64_t>(
neighbors_ref_h, neighbors_h_64b, n_queries, top_k);
std::cout << "[INFO] Recall@" << top_k << ": " << recall << std::endl;
}
}
int main(int argc, char **argv) {
CLI::App app{"Run CUVS Benchmarks"};
argv = app.ensure_utf8(argv);
std::string dataset_dir;
app.add_option("-d,--dataset-dir", dataset_dir, "Path to the dataset");
std::string algo = "ivf";
app.add_option("--algo", algo, "Algorithm to run: cagra or ivf");
std::string mem_type = "cuda";
app.add_option("--mem-type", mem_type,
"Memory type: cuda / async / managed / prefetch / "
"managed_pool / prefetch_pool / system / system pool");
int64_t cuda_device = 0;
app.add_option("--cuda-device", cuda_device, "The CUDA device to use");
int64_t learn_limit = 10000;
app.add_option("--learn-limit", learn_limit,
"Limit the number of learn vectors");
int64_t search_limit = 10000;
app.add_option("--search-limit", search_limit,
"Limit the number of search vectors");
int64_t top_k = 10;
app.add_option("-k,--top-k", top_k, "Number of nearest neighbors");
int64_t itopk_size = 64;
app.add_option("--itop-k", itopk_size, "The value of itopk_size in CAGRA");
int64_t n_probe = 32;
app.add_option("--n-probe", n_probe, "Number of probes");
CLI11_PARSE(app, argc, argv);
if (dataset_dir.empty()) {
std::cerr << "[ERROR] Please provide a dataset directory" << std::endl;
return 1;
}
// Set the memory resources
raft::device_resources res;
auto mr = create_memory_resource(mem_type);
auto stats_mr =
rmm::mr::statistics_resource_adaptor<rmm::mr::device_memory_resource>(
mr.get());
rmm::mr::set_current_device_resource(&stats_mr);
// Read the dataset files
std::string dataset_path = dataset_dir + "/dataset.bin";
std::string query_path = dataset_dir + "/query.bin";
auto dataset_device =
read_bin_dataset<float, int64_t>(res, dataset_path.c_str(), learn_limit);
auto queries_device =
read_bin_dataset<float, int64_t>(res, query_path.c_str(), 10'000);
int64_t n_dataset = dataset_device.extent(0);
int64_t d_dataset = dataset_device.extent(1);
int64_t n_queries = queries_device.extent(0);
int64_t d_queries = queries_device.extent(1);
std::cout << "[INFO] Dataset shape: " << n_dataset << " x " << d_dataset
<< std::endl;
std::cout << "[INFO] Queries shape: " << n_queries << " x " << d_queries
<< std::endl;
// Set the index and search params
int64_t n_list = int64_t(4 * std::sqrt(n_dataset));
if (algo == "ivf") {
ivf_search(res, raft::make_const_mdspan(dataset_device.view()),
raft::make_const_mdspan(queries_device.view()), n_list, n_probe,
top_k);
} else if (algo == "cagra") {
cagra_search(res, raft::make_const_mdspan(dataset_device.view()),
raft::make_const_mdspan(queries_device.view()), itopk_size, top_k);
}
raft::resource::sync_stream(res);
std::cout << "[INFO] Peak memory usage: "
<< (stats_mr.get_bytes_counter().peak / 1048576.0) << " MiB\n";
}