forked from facebookresearch/faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGpuIndexFlat.cu
388 lines (317 loc) · 11.7 KB
/
GpuIndexFlat.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
/**
* 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.
*/
#include <faiss/gpu/GpuIndexFlat.h>
#include <faiss/IndexFlat.h>
#include <faiss/gpu/GpuResources.h>
#include <faiss/gpu/impl/FlatIndex.cuh>
#include <faiss/gpu/utils/ConversionOperators.cuh>
#include <faiss/gpu/utils/CopyUtils.cuh>
#include <faiss/gpu/utils/DeviceUtils.h>
#include <faiss/gpu/utils/Float16.cuh>
#include <faiss/gpu/utils/StaticUtils.h>
#include <limits>
namespace faiss { namespace gpu {
GpuIndexFlat::GpuIndexFlat(GpuResources* resources,
const faiss::IndexFlat* index,
GpuIndexFlatConfig config) :
GpuIndex(resources, index->d, index->metric_type, config),
config_(std::move(config)),
data_(nullptr) {
verifySettings_();
// Flat index doesn't need training
this->is_trained = true;
copyFrom(index);
}
GpuIndexFlat::GpuIndexFlat(GpuResources* resources,
int dims,
faiss::MetricType metric,
GpuIndexFlatConfig config) :
GpuIndex(resources, dims, metric, config),
config_(std::move(config)),
data_(nullptr) {
verifySettings_();
// Flat index doesn't need training
this->is_trained = true;
// Construct index
DeviceScope scope(device_);
data_ = new FlatIndex(resources,
dims,
metric == faiss::METRIC_L2,
config_.useFloat16,
config_.useFloat16Accumulator,
config_.storeTransposed,
memorySpace_);
}
GpuIndexFlat::~GpuIndexFlat() {
delete data_;
}
void
GpuIndexFlat::copyFrom(const faiss::IndexFlat* index) {
DeviceScope scope(device_);
this->d = index->d;
this->metric_type = index->metric_type;
// GPU code has 32 bit indices
FAISS_THROW_IF_NOT_FMT(index->ntotal <=
(faiss::Index::idx_t) std::numeric_limits<int>::max(),
"GPU index only supports up to %zu indices; "
"attempting to copy CPU index with %zu parameters",
(size_t) std::numeric_limits<int>::max(),
(size_t) index->ntotal);
this->ntotal = index->ntotal;
delete data_;
data_ = new FlatIndex(resources_,
this->d,
index->metric_type == faiss::METRIC_L2,
config_.useFloat16,
config_.useFloat16Accumulator,
config_.storeTransposed,
memorySpace_);
// The index could be empty
if (index->ntotal > 0) {
data_->add(index->xb.data(),
index->ntotal,
resources_->getDefaultStream(device_));
}
}
void
GpuIndexFlat::copyTo(faiss::IndexFlat* index) const {
DeviceScope scope(device_);
index->d = this->d;
index->ntotal = this->ntotal;
index->metric_type = this->metric_type;
FAISS_ASSERT(data_);
FAISS_ASSERT(data_->getSize() == this->ntotal);
index->xb.resize(this->ntotal * this->d);
auto stream = resources_->getDefaultStream(device_);
if (this->ntotal > 0) {
if (config_.useFloat16) {
auto vecFloat32 = data_->getVectorsFloat32Copy(stream);
fromDevice(vecFloat32, index->xb.data(), stream);
} else {
fromDevice(data_->getVectorsFloat32Ref(), index->xb.data(), stream);
}
}
}
size_t
GpuIndexFlat::getNumVecs() const {
return this->ntotal;
}
void
GpuIndexFlat::reset() {
DeviceScope scope(device_);
// Free the underlying memory
data_->reset();
this->ntotal = 0;
}
void
GpuIndexFlat::train(Index::idx_t n, const float* x) {
// nothing to do
}
void
GpuIndexFlat::add(Index::idx_t n, const float* x) {
FAISS_THROW_IF_NOT_MSG(this->is_trained, "Index not trained");
// For now, only support <= max int results
FAISS_THROW_IF_NOT_FMT(n <= (Index::idx_t) std::numeric_limits<int>::max(),
"GPU index only supports up to %d indices",
std::numeric_limits<int>::max());
if (n == 0) {
// nothing to add
return;
}
DeviceScope scope(device_);
// To avoid multiple re-allocations, ensure we have enough storage
// available
data_->reserve(n, resources_->getDefaultStream(device_));
// If we're not operating in float16 mode, we don't need the input
// data to be resident on our device; we can add directly.
if (!config_.useFloat16) {
addImpl_(n, x, nullptr);
} else {
// Otherwise, perform the paging
GpuIndex::add(n, x);
}
}
bool
GpuIndexFlat::addImplRequiresIDs_() const {
return false;
}
void
GpuIndexFlat::addImpl_(int n,
const float* x,
const Index::idx_t* ids) {
FAISS_ASSERT(data_);
FAISS_ASSERT(n > 0);
// We do not support add_with_ids
FAISS_THROW_IF_NOT_MSG(!ids, "add_with_ids not supported");
// Due to GPU indexing in int32, we can't store more than this
// number of vectors on a GPU
FAISS_THROW_IF_NOT_FMT(this->ntotal + n <=
(faiss::Index::idx_t) std::numeric_limits<int>::max(),
"GPU index only supports up to %zu indices",
(size_t) std::numeric_limits<int>::max());
data_->add(x, n, resources_->getDefaultStream(device_));
this->ntotal += n;
}
void
GpuIndexFlat::searchImpl_(int n,
const float* x,
int k,
float* distances,
Index::idx_t* labels) const {
auto stream = resources_->getDefaultStream(device_);
// Input and output data are already resident on the GPU
Tensor<float, 2, true> queries(const_cast<float*>(x), {n, (int) this->d});
Tensor<float, 2, true> outDistances(distances, {n, k});
Tensor<Index::idx_t, 2, true> outLabels(labels, {n, k});
// FlatIndex only supports int indices
DeviceTensor<int, 2, true> outIntLabels(
resources_->getMemoryManagerCurrentDevice(), {n, k}, stream);
data_->query(queries, k, outDistances, outIntLabels, true);
// Convert int to idx_t
convertTensor<int, faiss::Index::idx_t, 2>(stream,
outIntLabels,
outLabels);
}
void
GpuIndexFlat::reconstruct(faiss::Index::idx_t key,
float* out) const {
DeviceScope scope(device_);
FAISS_THROW_IF_NOT_MSG(key < this->ntotal, "index out of bounds");
auto stream = resources_->getDefaultStream(device_);
if (config_.useFloat16) {
// FIXME jhj: kernel for copy
auto vec = data_->getVectorsFloat32Copy(key, 1, stream);
fromDevice(vec.data(), out, this->d, stream);
} else {
auto vec = data_->getVectorsFloat32Ref()[key];
fromDevice(vec.data(), out, this->d, stream);
}
}
void
GpuIndexFlat::reconstruct_n(faiss::Index::idx_t i0,
faiss::Index::idx_t num,
float* out) const {
DeviceScope scope(device_);
FAISS_THROW_IF_NOT_MSG(i0 < this->ntotal, "index out of bounds");
FAISS_THROW_IF_NOT_MSG(i0 + num - 1 < this->ntotal, "num out of bounds");
auto stream = resources_->getDefaultStream(device_);
if (config_.useFloat16) {
// FIXME jhj: kernel for copy
auto vec = data_->getVectorsFloat32Copy(i0, num, stream);
fromDevice(vec.data(), out, num * this->d, stream);
} else {
auto vec = data_->getVectorsFloat32Ref()[i0];
fromDevice(vec.data(), out, this->d * num, stream);
}
}
void
GpuIndexFlat::compute_residual(const float* x,
float* residual,
faiss::Index::idx_t key) const {
compute_residual_n(1, x, residual, &key);
}
void
GpuIndexFlat::compute_residual_n(faiss::Index::idx_t n,
const float* xs,
float* residuals,
const faiss::Index::idx_t* keys) const {
FAISS_THROW_IF_NOT_FMT(n <=
(faiss::Index::idx_t) std::numeric_limits<int>::max(),
"GPU index only supports up to %zu indices",
(size_t) std::numeric_limits<int>::max());
auto stream = resources_->getDefaultStream(device_);
DeviceScope scope(device_);
auto vecsDevice =
toDevice<float, 2>(resources_, device_,
const_cast<float*>(xs), stream,
{(int) n, (int) this->d});
auto idsDevice =
toDevice<faiss::Index::idx_t, 1>(resources_, device_,
const_cast<faiss::Index::idx_t*>(keys),
stream,
{(int) n});
auto residualDevice =
toDevice<float, 2>(resources_, device_, residuals, stream,
{(int) n, (int) this->d});
// Convert idx_t to int
auto keysInt =
convertTensor<faiss::Index::idx_t, int, 1>(resources_, stream, idsDevice);
FAISS_ASSERT(data_);
data_->computeResidual(vecsDevice,
keysInt,
residualDevice);
fromDevice<float, 2>(residualDevice, residuals, stream);
}
void
GpuIndexFlat::verifySettings_() const {
// If we want Hgemm, ensure that it is supported on this device
if (config_.useFloat16Accumulator) {
FAISS_THROW_IF_NOT_MSG(config_.useFloat16,
"useFloat16Accumulator can only be enabled "
"with useFloat16");
FAISS_THROW_IF_NOT_FMT(getDeviceSupportsFloat16Math(config_.device),
"Device %d does not support Hgemm "
"(useFloat16Accumulator)",
config_.device);
}
}
//
// GpuIndexFlatL2
//
GpuIndexFlatL2::GpuIndexFlatL2(GpuResources* resources,
faiss::IndexFlatL2* index,
GpuIndexFlatConfig config) :
GpuIndexFlat(resources, index, config) {
}
GpuIndexFlatL2::GpuIndexFlatL2(GpuResources* resources,
int dims,
GpuIndexFlatConfig config) :
GpuIndexFlat(resources, dims, faiss::METRIC_L2, config) {
}
void
GpuIndexFlatL2::copyFrom(faiss::IndexFlat* index) {
FAISS_THROW_IF_NOT_MSG(index->metric_type == metric_type,
"Cannot copy a GpuIndexFlatL2 from an index of "
"different metric_type");
GpuIndexFlat::copyFrom(index);
}
void
GpuIndexFlatL2::copyTo(faiss::IndexFlat* index) {
FAISS_THROW_IF_NOT_MSG(index->metric_type == metric_type,
"Cannot copy a GpuIndexFlatL2 to an index of "
"different metric_type");
GpuIndexFlat::copyTo(index);
}
//
// GpuIndexFlatIP
//
GpuIndexFlatIP::GpuIndexFlatIP(GpuResources* resources,
faiss::IndexFlatIP* index,
GpuIndexFlatConfig config) :
GpuIndexFlat(resources, index, config) {
}
GpuIndexFlatIP::GpuIndexFlatIP(GpuResources* resources,
int dims,
GpuIndexFlatConfig config) :
GpuIndexFlat(resources, dims, faiss::METRIC_INNER_PRODUCT, config) {
}
void
GpuIndexFlatIP::copyFrom(faiss::IndexFlat* index) {
FAISS_THROW_IF_NOT_MSG(index->metric_type == metric_type,
"Cannot copy a GpuIndexFlatIP from an index of "
"different metric_type");
GpuIndexFlat::copyFrom(index);
}
void
GpuIndexFlatIP::copyTo(faiss::IndexFlat* index) {
// The passed in index must be IP
FAISS_THROW_IF_NOT_MSG(index->metric_type == metric_type,
"Cannot copy a GpuIndexFlatIP to an index of "
"different metric_type");
GpuIndexFlat::copyTo(index);
}
} } // namespace