/
GpuDistance.cu
565 lines (522 loc) · 21 KB
/
GpuDistance.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
* Copyright (c) 2023, 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 <faiss/gpu/GpuDistance.h>
#include <faiss/gpu/GpuResources.h>
#include <faiss/gpu/utils/DeviceUtils.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/utils/Heap.h>
#include <faiss/gpu/impl/Distance.cuh>
#include <faiss/gpu/utils/ConversionOperators.cuh>
#include <faiss/gpu/utils/CopyUtils.cuh>
#include <faiss/gpu/utils/DeviceTensor.cuh>
#if defined USE_NVIDIA_RAFT
#include <faiss/gpu/utils/RaftUtils.h>
#include <raft/core/device_mdspan.hpp>
#include <raft/core/device_resources.hpp>
#include <raft/core/error.hpp>
#include <raft/core/mdspan_types.hpp>
#include <raft/core/operators.hpp>
#include <raft/core/temporary_device_buffer.hpp>
#include <raft/linalg/unary_op.cuh>
#include <raft/neighbors/brute_force.cuh>
#define RAFT_NAME "raft"
#endif
namespace faiss {
namespace gpu {
#if defined USE_NVIDIA_RAFT
using namespace raft::distance;
using namespace raft::neighbors;
#endif
bool should_use_raft(GpuDistanceParams args) {
cudaDeviceProp prop;
int dev = args.device >= 0 ? args.device : getCurrentDevice();
cudaGetDeviceProperties(&prop, dev);
if (prop.major < 7)
return false;
return args.use_raft;
}
template <typename T>
void bfKnnConvert(GpuResourcesProvider* prov, const GpuDistanceParams& args) {
// Validate the input data
FAISS_THROW_IF_NOT_MSG(
args.k > 0 || args.k == -1,
"bfKnn: k must be > 0 for top-k reduction, "
"or -1 for all pairwise distances");
FAISS_THROW_IF_NOT_MSG(args.dims > 0, "bfKnn: dims must be > 0");
FAISS_THROW_IF_NOT_MSG(
args.numVectors > 0, "bfKnn: numVectors must be > 0");
FAISS_THROW_IF_NOT_MSG(
args.vectors, "bfKnn: vectors must be provided (passed null)");
FAISS_THROW_IF_NOT_MSG(
args.numQueries > 0, "bfKnn: numQueries must be > 0");
FAISS_THROW_IF_NOT_MSG(
args.queries, "bfKnn: queries must be provided (passed null)");
FAISS_THROW_IF_NOT_MSG(
args.outDistances,
"bfKnn: outDistances must be provided (passed null)");
FAISS_THROW_IF_NOT_MSG(
args.outIndices || args.k == -1,
"bfKnn: outIndices must be provided (passed null)");
// If the user specified a device, then ensure that it is currently set
int device = -1;
if (args.device == -1) {
// Original behavior if no device is specified, use the current CUDA
// thread local device
device = getCurrentDevice();
} else {
// Otherwise, use the device specified in `args`
device = args.device;
FAISS_THROW_IF_NOT_FMT(
device >= 0 && device < getNumDevices(),
"bfKnn: device specified must be -1 (current CUDA thread local device) "
"or within the range [0, %d)",
getNumDevices());
}
DeviceScope scope(device);
// Don't let the resources go out of scope
auto resImpl = prov->getResources();
auto res = resImpl.get();
auto stream = res->getDefaultStreamCurrentDevice();
auto tVectors = toDeviceTemporary<T, 2>(
res,
device,
const_cast<T*>(reinterpret_cast<const T*>(args.vectors)),
stream,
{args.vectorsRowMajor ? args.numVectors : args.dims,
args.vectorsRowMajor ? args.dims : args.numVectors});
auto tQueries = toDeviceTemporary<T, 2>(
res,
device,
const_cast<T*>(reinterpret_cast<const T*>(args.queries)),
stream,
{args.queriesRowMajor ? args.numQueries : args.dims,
args.queriesRowMajor ? args.dims : args.numQueries});
DeviceTensor<float, 1, true> tVectorNorms;
if (args.vectorNorms) {
tVectorNorms = toDeviceTemporary<float, 1>(
res,
device,
const_cast<float*>(args.vectorNorms),
stream,
{args.numVectors});
}
auto tOutDistances = toDeviceTemporary<float, 2>(
res,
device,
args.outDistances,
stream,
{args.numQueries, args.k == -1 ? args.numVectors : args.k});
if (args.k == -1) {
// Reporting all pairwise distances
allPairwiseDistanceOnDevice<T>(
res,
device,
stream,
tVectors,
args.vectorsRowMajor,
args.vectorNorms ? &tVectorNorms : nullptr,
tQueries,
args.queriesRowMajor,
args.metric,
args.metricArg,
tOutDistances);
} else if (args.outIndicesType == IndicesDataType::I64) {
auto tOutIndices = toDeviceTemporary<idx_t, 2>(
res,
device,
(idx_t*)args.outIndices,
stream,
{args.numQueries, args.k});
// Since we've guaranteed that all arguments are on device, call the
// implementation
bfKnnOnDevice<T>(
res,
device,
stream,
tVectors,
args.vectorsRowMajor,
args.vectorNorms ? &tVectorNorms : nullptr,
tQueries,
args.queriesRowMajor,
args.k,
args.metric,
args.metricArg,
tOutDistances,
tOutIndices,
args.ignoreOutDistances);
fromDevice<idx_t, 2>(tOutIndices, (idx_t*)args.outIndices, stream);
} else if (args.outIndicesType == IndicesDataType::I32) {
// The brute-force API supports i64 indices, but our output buffer is
// i32 so we need to temporarily allocate and then convert back to i32
// FIXME: convert to int32_t everywhere?
static_assert(sizeof(int) == 4, "");
DeviceTensor<idx_t, 2, true> tIntIndices(
res,
makeTempAlloc(AllocType::Other, stream),
{args.numQueries, args.k});
// Since we've guaranteed that all arguments are on device, call the
// implementation
bfKnnOnDevice<T>(
res,
device,
stream,
tVectors,
args.vectorsRowMajor,
args.vectorNorms ? &tVectorNorms : nullptr,
tQueries,
args.queriesRowMajor,
args.k,
args.metric,
args.metricArg,
tOutDistances,
tIntIndices,
args.ignoreOutDistances);
// Convert and copy int indices out
auto tOutIntIndices = toDeviceTemporary<int, 2>(
res,
device,
(int*)args.outIndices,
stream,
{args.numQueries, args.k});
convertTensor<idx_t, int, 2>(stream, tIntIndices, tOutIntIndices);
// Copy back if necessary
fromDevice<int, 2>(tOutIntIndices, (int*)args.outIndices, stream);
} else {
FAISS_THROW_MSG("unknown outIndicesType");
}
// Copy distances back if necessary
fromDevice<float, 2>(tOutDistances, args.outDistances, stream);
}
void bfKnn(GpuResourcesProvider* prov, const GpuDistanceParams& args) {
// For now, both vectors and queries must be of the same data type
FAISS_THROW_IF_NOT_MSG(
args.vectorType == args.queryType,
"limitation: both vectorType and queryType must currently "
"be the same (F32 or F16");
#if defined USE_NVIDIA_RAFT
// Note: For now, RAFT bfknn requires queries and vectors to be same layout
if (should_use_raft(args) && args.queriesRowMajor == args.vectorsRowMajor) {
DistanceType distance = metricFaissToRaft(args.metric, false);
auto resImpl = prov->getResources();
auto res = resImpl.get();
raft::device_resources& handle = res->getRaftHandleCurrentDevice();
auto stream = res->getDefaultStreamCurrentDevice();
int64_t dims = args.dims;
int64_t num_vectors = args.numVectors;
int64_t num_queries = args.numQueries;
int k = args.k;
float metric_arg = args.metricArg;
auto inds =
raft::make_writeback_temporary_device_buffer<idx_t, int64_t>(
handle,
reinterpret_cast<idx_t*>(args.outIndices),
raft::matrix_extent<int64_t>(num_queries, (int64_t)k));
auto dists =
raft::make_writeback_temporary_device_buffer<float, int64_t>(
handle,
reinterpret_cast<float*>(args.outDistances),
raft::matrix_extent<int64_t>(num_queries, (int64_t)k));
if (args.queriesRowMajor) {
auto index = raft::make_readonly_temporary_device_buffer<
const float,
int64_t,
raft::row_major>(
handle,
const_cast<float*>(
reinterpret_cast<const float*>(args.vectors)),
raft::matrix_extent<int64_t>(num_vectors, dims));
auto search = raft::make_readonly_temporary_device_buffer<
const float,
int64_t,
raft::row_major>(
handle,
const_cast<float*>(
reinterpret_cast<const float*>(args.queries)),
raft::matrix_extent<int64_t>(num_queries, dims));
// get device_vector_view to the precalculate norms if available
std::optional<raft::temporary_device_buffer<
const float,
raft::vector_extent<int64_t>>>
norms;
std::optional<raft::device_vector_view<const float, int64_t>>
norms_view;
if (args.vectorNorms) {
norms = raft::make_readonly_temporary_device_buffer<
const float,
int64_t>(
handle,
args.vectorNorms,
raft::vector_extent<int64_t>(num_queries));
norms_view = norms->view();
}
raft::neighbors::brute_force::index idx(
handle, index.view(), norms_view, distance, metric_arg);
raft::neighbors::brute_force::search<float, idx_t>(
handle, idx, search.view(), inds.view(), dists.view());
} else {
auto index = raft::make_readonly_temporary_device_buffer<
const float,
int64_t,
raft::col_major>(
handle,
const_cast<float*>(
reinterpret_cast<const float*>(args.vectors)),
raft::matrix_extent<int64_t>(num_vectors, dims));
auto search = raft::make_readonly_temporary_device_buffer<
const float,
int64_t,
raft::col_major>(
handle,
const_cast<float*>(
reinterpret_cast<const float*>(args.queries)),
raft::matrix_extent<int64_t>(num_queries, dims));
std::vector<raft::device_matrix_view<
const float,
int64_t,
raft::col_major>>
index_vec = {index.view()};
brute_force::knn(
handle,
index_vec,
search.view(),
inds.view(),
dists.view(),
distance,
metric_arg);
}
if (args.metric == MetricType::METRIC_Lp) {
raft::linalg::unary_op(
handle,
raft::make_const_mdspan(dists.view()),
dists.view(),
[metric_arg] __device__(const float& a) {
return powf(a, metric_arg);
});
} else if (args.metric == MetricType::METRIC_JensenShannon) {
raft::linalg::unary_op(
handle,
raft::make_const_mdspan(dists.view()),
dists.view(),
[] __device__(const float& a) { return powf(a, 2); });
}
handle.sync_stream();
} else
#else
if (should_use_raft(args)) {
FAISS_THROW_IF_NOT_MSG(
!should_use_raft(args),
"RAFT has not been compiled into the current version so it cannot be used.");
} else
#endif
if (args.vectorType == DistanceDataType::F32) {
bfKnnConvert<float>(prov, args);
} else if (args.vectorType == DistanceDataType::F16) {
bfKnnConvert<half>(prov, args);
} else {
FAISS_THROW_MSG("unknown vectorType");
}
}
template <class C>
void bfKnn_shard_database(
GpuResourcesProvider* prov,
const GpuDistanceParams& args,
size_t shard_size,
size_t distance_size) {
std::vector<typename C::T> heaps_distances;
if (args.ignoreOutDistances) {
heaps_distances.resize(args.numQueries * args.k, 0);
}
HeapArray<C> heaps = {
(size_t)args.numQueries,
(size_t)args.k,
(typename C::TI*)args.outIndices,
args.ignoreOutDistances ? heaps_distances.data()
: args.outDistances};
heaps.heapify();
std::vector<typename C::TI> labels(args.numQueries * args.k);
std::vector<typename C::T> distances(args.numQueries * args.k);
GpuDistanceParams args_batch = args;
args_batch.outDistances = distances.data();
args_batch.ignoreOutDistances = false;
args_batch.outIndices = labels.data();
for (idx_t i = 0; i < args.numVectors; i += shard_size) {
args_batch.numVectors = min(shard_size, args.numVectors - i);
args_batch.vectors =
(char*)args.vectors + distance_size * args.dims * i;
args_batch.vectorNorms =
args.vectorNorms ? args.vectorNorms + i : nullptr;
bfKnn(prov, args_batch);
for (auto& label : labels) {
label += i;
}
heaps.addn_with_ids(args.k, distances.data(), labels.data(), args.k);
}
heaps.reorder();
}
void bfKnn_single_query_shard(
GpuResourcesProvider* prov,
const GpuDistanceParams& args,
size_t vectorsMemoryLimit) {
if (vectorsMemoryLimit == 0) {
bfKnn(prov, args);
return;
}
FAISS_THROW_IF_NOT_MSG(
args.numVectors > 0, "bfKnn_tiling: numVectors must be > 0");
FAISS_THROW_IF_NOT_MSG(
args.vectors,
"bfKnn_tiling: vectors must be provided (passed null)");
FAISS_THROW_IF_NOT_MSG(
getDeviceForAddress(args.vectors) == -1,
"bfKnn_tiling: vectors should be in CPU memory when vectorsMemoryLimit > 0");
FAISS_THROW_IF_NOT_MSG(
args.vectorsRowMajor,
"bfKnn_tiling: tiling vectors is only supported in row major mode");
FAISS_THROW_IF_NOT_MSG(
args.k > 0,
"bfKnn_tiling: tiling vectors is only supported for k > 0");
size_t distance_size = args.vectorType == DistanceDataType::F32 ? 4
: args.vectorType == DistanceDataType::F16 ? 2
: 0;
FAISS_THROW_IF_NOT_MSG(
distance_size > 0, "bfKnn_tiling: unknown vectorType");
size_t shard_size = vectorsMemoryLimit / (args.dims * distance_size);
FAISS_THROW_IF_NOT_MSG(
shard_size > 0, "bfKnn_tiling: vectorsMemoryLimit is too low");
if (args.numVectors <= shard_size) {
bfKnn(prov, args);
return;
}
if (is_similarity_metric(args.metric)) {
if (args.outIndicesType == IndicesDataType::I64) {
bfKnn_shard_database<CMin<float, int64_t>>(
prov, args, shard_size, distance_size);
} else if (args.outIndicesType == IndicesDataType::I32) {
bfKnn_shard_database<CMin<float, int32_t>>(
prov, args, shard_size, distance_size);
} else {
FAISS_THROW_MSG("bfKnn_tiling: unknown outIndicesType");
}
} else {
if (args.outIndicesType == IndicesDataType::I64) {
bfKnn_shard_database<CMax<float, int64_t>>(
prov, args, shard_size, distance_size);
} else if (args.outIndicesType == IndicesDataType::I32) {
bfKnn_shard_database<CMax<float, int32_t>>(
prov, args, shard_size, distance_size);
} else {
FAISS_THROW_MSG("bfKnn_tiling: unknown outIndicesType");
}
}
}
void bfKnn_tiling(
GpuResourcesProvider* prov,
const GpuDistanceParams& args,
size_t vectorsMemoryLimit,
size_t queriesMemoryLimit) {
if (queriesMemoryLimit == 0) {
bfKnn_single_query_shard(prov, args, vectorsMemoryLimit);
return;
}
FAISS_THROW_IF_NOT_MSG(
args.numQueries > 0, "bfKnn_tiling: numQueries must be > 0");
FAISS_THROW_IF_NOT_MSG(
args.queries,
"bfKnn_tiling: queries must be provided (passed null)");
FAISS_THROW_IF_NOT_MSG(
getDeviceForAddress(args.queries) == -1,
"bfKnn_tiling: queries should be in CPU memory when queriesMemoryLimit > 0");
FAISS_THROW_IF_NOT_MSG(
args.queriesRowMajor,
"bfKnn_tiling: tiling queries is only supported in row major mode");
FAISS_THROW_IF_NOT_MSG(
args.k > 0,
"bfKnn_tiling: tiling queries is only supported for k > 0");
size_t distance_size = args.queryType == DistanceDataType::F32 ? 4
: args.queryType == DistanceDataType::F16 ? 2
: 0;
FAISS_THROW_IF_NOT_MSG(
distance_size > 0, "bfKnn_tiling: unknown queryType");
size_t label_size = args.outIndicesType == IndicesDataType::I64 ? 8
: args.outIndicesType == IndicesDataType::I32 ? 4
: 0;
FAISS_THROW_IF_NOT_MSG(
distance_size > 0, "bfKnn_tiling: unknown outIndicesType");
size_t shard_size = queriesMemoryLimit /
(args.k * (distance_size + label_size) + args.dims * distance_size);
FAISS_THROW_IF_NOT_MSG(
shard_size > 0, "bfKnn_tiling: queriesMemoryLimit is too low");
FAISS_THROW_IF_NOT_MSG(
args.outIndices,
"bfKnn: outIndices must be provided (passed null)");
for (idx_t i = 0; i < args.numQueries; i += shard_size) {
GpuDistanceParams args_batch = args;
args_batch.numQueries = min(shard_size, args.numQueries - i);
args_batch.queries =
(char*)args.queries + distance_size * args.dims * i;
if (!args_batch.ignoreOutDistances) {
args_batch.outDistances = args.outDistances + args.k * i;
}
args_batch.outIndices =
(char*)args.outIndices + args.k * label_size * i;
bfKnn_single_query_shard(prov, args_batch, vectorsMemoryLimit);
}
}
// legacy version
void bruteForceKnn(
GpuResourcesProvider* res,
faiss::MetricType metric,
// A region of memory size numVectors x dims, with dims
// innermost
const float* vectors,
bool vectorsRowMajor,
idx_t numVectors,
// A region of memory size numQueries x dims, with dims
// innermost
const float* queries,
bool queriesRowMajor,
idx_t numQueries,
int dims,
int k,
// A region of memory size numQueries x k, with k
// innermost
float* outDistances,
// A region of memory size numQueries x k, with k
// innermost
idx_t* outIndices) {
std::cerr << "bruteForceKnn is deprecated; call bfKnn instead" << std::endl;
GpuDistanceParams args;
args.metric = metric;
args.k = k;
args.dims = dims;
args.vectors = vectors;
args.vectorsRowMajor = vectorsRowMajor;
args.numVectors = numVectors;
args.queries = queries;
args.queriesRowMajor = queriesRowMajor;
args.numQueries = numQueries;
args.outDistances = outDistances;
args.outIndices = outIndices;
bfKnn(res, args);
}
} // namespace gpu
} // namespace faiss