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
/**
* 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 "IVFBase.cuh"
#include "../GpuResources.h"
#include "FlatIndex.cuh"
#include "InvertedListAppend.cuh"
#include "RemapIndices.h"
#include "../utils/DeviceDefs.cuh"
#include "../utils/DeviceUtils.h"
#include "../utils/HostTensor.cuh"
#include <limits>
#include <thrust/host_vector.h>
#include <unordered_map>
namespace faiss { namespace gpu {
IVFBase::IVFBase(GpuResources* resources,
FlatIndex* quantizer,
int bytesPerVector,
IndicesOptions indicesOptions,
MemorySpace space) :
resources_(resources),
quantizer_(quantizer),
bytesPerVector_(bytesPerVector),
indicesOptions_(indicesOptions),
space_(space),
dim_(quantizer->getDim()),
numLists_(quantizer->getSize()),
maxListLength_(0) {
reset();
}
IVFBase::~IVFBase() {
}
void
IVFBase::reserveMemory(size_t numVecs) {
size_t vecsPerList = numVecs / deviceListData_.size();
if (vecsPerList < 1) {
return;
}
auto stream = resources_->getDefaultStreamCurrentDevice();
size_t bytesPerDataList = vecsPerList * bytesPerVector_;
for (auto& list : deviceListData_) {
list->reserve(bytesPerDataList, stream);
}
if ((indicesOptions_ == INDICES_32_BIT) ||
(indicesOptions_ == INDICES_64_BIT)) {
// Reserve for index lists as well
size_t bytesPerIndexList = vecsPerList *
(indicesOptions_ == INDICES_32_BIT ? sizeof(int) : sizeof(long));
for (auto& list : deviceListIndices_) {
list->reserve(bytesPerIndexList, stream);
}
}
// Update device info for all lists, since the base pointers may
// have changed
updateDeviceListInfo_(stream);
}
void
IVFBase::reset() {
deviceListData_.clear();
deviceListIndices_.clear();
deviceListDataPointers_.clear();
deviceListIndexPointers_.clear();
deviceListLengths_.clear();
listOffsetToUserIndex_.clear();
for (size_t i = 0; i < numLists_; ++i) {
deviceListData_.emplace_back(
std::unique_ptr<DeviceVector<unsigned char>>(
new DeviceVector<unsigned char>(space_)));
deviceListIndices_.emplace_back(
std::unique_ptr<DeviceVector<unsigned char>>(
new DeviceVector<unsigned char>(space_)));
listOffsetToUserIndex_.emplace_back(std::vector<long>());
}
deviceListDataPointers_.resize(numLists_, nullptr);
deviceListIndexPointers_.resize(numLists_, nullptr);
deviceListLengths_.resize(numLists_, 0);
maxListLength_ = 0;
}
int
IVFBase::getDim() const {
return dim_;
}
size_t
IVFBase::reclaimMemory() {
// Reclaim all unused memory exactly
return reclaimMemory_(true);
}
size_t
IVFBase::reclaimMemory_(bool exact) {
auto stream = resources_->getDefaultStreamCurrentDevice();
size_t totalReclaimed = 0;
for (int i = 0; i < deviceListData_.size(); ++i) {
auto& data = deviceListData_[i];
totalReclaimed += data->reclaim(exact, stream);
deviceListDataPointers_[i] = data->data();
}
for (int i = 0; i < deviceListIndices_.size(); ++i) {
auto& indices = deviceListIndices_[i];
totalReclaimed += indices->reclaim(exact, stream);
deviceListIndexPointers_[i] = indices->data();
}
// Update device info for all lists, since the base pointers may
// have changed
updateDeviceListInfo_(stream);
return totalReclaimed;
}
void
IVFBase::updateDeviceListInfo_(cudaStream_t stream) {
std::vector<int> listIds(deviceListData_.size());
for (int i = 0; i < deviceListData_.size(); ++i) {
listIds[i] = i;
}
updateDeviceListInfo_(listIds, stream);
}
void
IVFBase::updateDeviceListInfo_(const std::vector<int>& listIds,
cudaStream_t stream) {
auto& mem = resources_->getMemoryManagerCurrentDevice();
HostTensor<int, 1, true>
hostListsToUpdate({(int) listIds.size()});
HostTensor<int, 1, true>
hostNewListLength({(int) listIds.size()});
HostTensor<void*, 1, true>
hostNewDataPointers({(int) listIds.size()});
HostTensor<void*, 1, true>
hostNewIndexPointers({(int) listIds.size()});
for (int i = 0; i < listIds.size(); ++i) {
auto listId = listIds[i];
auto& data = deviceListData_[listId];
auto& indices = deviceListIndices_[listId];
hostListsToUpdate[i] = listId;
hostNewListLength[i] = data->size() / bytesPerVector_;
hostNewDataPointers[i] = data->data();
hostNewIndexPointers[i] = indices->data();
}
// Copy the above update sets to the GPU
DeviceTensor<int, 1, true> listsToUpdate(
mem, hostListsToUpdate, stream);
DeviceTensor<int, 1, true> newListLength(
mem, hostNewListLength, stream);
DeviceTensor<void*, 1, true> newDataPointers(
mem, hostNewDataPointers, stream);
DeviceTensor<void*, 1, true> newIndexPointers(
mem, hostNewIndexPointers, stream);
// Update all pointers to the lists on the device that may have
// changed
runUpdateListPointers(listsToUpdate,
newListLength,
newDataPointers,
newIndexPointers,
deviceListLengths_,
deviceListDataPointers_,
deviceListIndexPointers_,
stream);
}
size_t
IVFBase::getNumLists() const {
return numLists_;
}
int
IVFBase::getListLength(int listId) const {
FAISS_ASSERT(listId < deviceListLengths_.size());
return deviceListLengths_[listId];
}
std::vector<long>
IVFBase::getListIndices(int listId) const {
FAISS_ASSERT(listId < numLists_);
if (indicesOptions_ == INDICES_32_BIT) {
FAISS_ASSERT(listId < deviceListIndices_.size());
auto intInd = deviceListIndices_[listId]->copyToHost<int>(
resources_->getDefaultStreamCurrentDevice());
std::vector<long> out(intInd.size());
for (size_t i = 0; i < intInd.size(); ++i) {
out[i] = (long) intInd[i];
}
return out;
} else if (indicesOptions_ == INDICES_64_BIT) {
FAISS_ASSERT(listId < deviceListIndices_.size());
return deviceListIndices_[listId]->copyToHost<long>(
resources_->getDefaultStreamCurrentDevice());
} else if (indicesOptions_ == INDICES_CPU) {
FAISS_ASSERT(listId < deviceListData_.size());
FAISS_ASSERT(listId < listOffsetToUserIndex_.size());
auto& userIds = listOffsetToUserIndex_[listId];
FAISS_ASSERT(userIds.size() ==
deviceListData_[listId]->size() / bytesPerVector_);
// this will return a copy
return userIds;
} else {
// unhandled indices type (includes INDICES_IVF)
FAISS_ASSERT(false);
return std::vector<long>();
}
}
void
IVFBase::addIndicesFromCpu_(int listId,
const long* indices,
size_t numVecs) {
auto stream = resources_->getDefaultStreamCurrentDevice();
auto& listIndices = deviceListIndices_[listId];
auto prevIndicesData = listIndices->data();
if (indicesOptions_ == INDICES_32_BIT) {
// Make sure that all indices are in bounds
std::vector<int> indices32(numVecs);
for (size_t i = 0; i < numVecs; ++i) {
auto ind = indices[i];
FAISS_ASSERT(ind <= (long) std::numeric_limits<int>::max());
indices32[i] = (int) ind;
}
listIndices->append((unsigned char*) indices32.data(),
numVecs * sizeof(int),
stream,
true /* exact reserved size */);
} else if (indicesOptions_ == INDICES_64_BIT) {
listIndices->append((unsigned char*) indices,
numVecs * sizeof(long),
stream,
true /* exact reserved size */);
} else if (indicesOptions_ == INDICES_CPU) {
// indices are stored on the CPU
FAISS_ASSERT(listId < listOffsetToUserIndex_.size());
auto& userIndices = listOffsetToUserIndex_[listId];
userIndices.insert(userIndices.begin(), indices, indices + numVecs);
} else {
// indices are not stored
FAISS_ASSERT(indicesOptions_ == INDICES_IVF);
}
if (prevIndicesData != listIndices->data()) {
deviceListIndexPointers_[listId] = listIndices->data();
}
}
} } // namespace