Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuIndexIVFFlat.cu
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // Copyright 2004-present Facebook. All Rights Reserved.
10
11 #include "GpuIndexIVFFlat.h"
12 #include "../IndexFlat.h"
13 #include "../IndexIVFFlat.h"
14 #include "GpuIndexFlat.h"
15 #include "GpuResources.h"
16 #include "impl/IVFFlat.cuh"
17 #include "utils/CopyUtils.cuh"
18 #include "utils/DeviceUtils.h"
19 #include "utils/Float16.cuh"
20
21 #include <limits>
22
23 namespace faiss { namespace gpu {
24
25 GpuIndexIVFFlat::GpuIndexIVFFlat(GpuResources* resources,
26 const faiss::IndexIVFFlat* index,
27 GpuIndexIVFFlatConfig config) :
28 GpuIndexIVF(resources,
29 index->d,
30 index->metric_type,
31 index->nlist,
32 config),
33 ivfFlatConfig_(config),
34 reserveMemoryVecs_(0),
35 index_(nullptr) {
36 #ifndef FAISS_USE_FLOAT16
37 FAISS_THROW_IF_NOT_MSG(!ivfFlatConfig_.useFloat16IVFStorage,
38 "float16 unsupported; need CUDA SDK >= 7.5");
39 #endif
40
41 copyFrom(index);
42 }
43
44 GpuIndexIVFFlat::GpuIndexIVFFlat(GpuResources* resources,
45 int dims,
46 int nlist,
47 faiss::MetricType metric,
48 GpuIndexIVFFlatConfig config) :
49 GpuIndexIVF(resources, dims, metric, nlist, config),
50 ivfFlatConfig_(config),
51 reserveMemoryVecs_(0),
52 index_(nullptr) {
53
54 // faiss::Index params
55 this->is_trained = false;
56
57 #ifndef FAISS_USE_FLOAT16
58 FAISS_THROW_IF_NOT_MSG(!ivfFlatConfig_.useFloat16IVFStorage,
59 "float16 unsupported; need CUDA SDK >= 7.5");
60 #endif
61
62 // We haven't trained ourselves, so don't construct the IVFFlat
63 // index yet
64 }
65
66 GpuIndexIVFFlat::~GpuIndexIVFFlat() {
67 delete index_;
68 }
69
70 void
71 GpuIndexIVFFlat::reserveMemory(size_t numVecs) {
72 reserveMemoryVecs_ = numVecs;
73 if (index_) {
74 index_->reserveMemory(numVecs);
75 }
76 }
77
78 void
79 GpuIndexIVFFlat::copyFrom(const faiss::IndexIVFFlat* index) {
80 DeviceScope scope(device_);
81
82 GpuIndexIVF::copyFrom(index);
83
84 // Clear out our old data
85 delete index_;
86 index_ = nullptr;
87
88 // The other index might not be trained
89 if (!index->is_trained) {
90 return;
91 }
92
93 // Otherwise, we can populate ourselves from the other index
94 this->is_trained = true;
95
96 // Copy our lists as well
97 index_ = new IVFFlat(resources_,
98 quantizer_->getGpuData(),
99 index->metric_type == faiss::METRIC_L2,
100 ivfFlatConfig_.useFloat16IVFStorage,
101 ivfFlatConfig_.indicesOptions,
102 memorySpace_);
103 InvertedLists *ivf = index->invlists;
104
105 for (size_t i = 0; i < ivf->nlist; ++i) {
106 auto numVecs = ivf->list_size(i);
107
108 // GPU index can only support max int entries per list
109 FAISS_THROW_IF_NOT_FMT(numVecs <=
110 (size_t) std::numeric_limits<int>::max(),
111 "GPU inverted list can only support "
112 "%zu entries; %zu found",
113 (size_t) std::numeric_limits<int>::max(),
114 numVecs);
115
116 index_->addCodeVectorsFromCpu(
117 i, (const float*)(ivf->get_codes(i)),
118 ivf->get_ids(i), numVecs);
119 }
120 }
121
122 void
123 GpuIndexIVFFlat::copyTo(faiss::IndexIVFFlat* index) const {
124 DeviceScope scope(device_);
125
126 // We must have the indices in order to copy to ourselves
127 FAISS_THROW_IF_NOT_MSG(ivfFlatConfig_.indicesOptions != INDICES_IVF,
128 "Cannot copy to CPU as GPU index doesn't retain "
129 "indices (INDICES_IVF)");
130
131 GpuIndexIVF::copyTo(index);
132 index->code_size = this->d * sizeof(float);
133
134 InvertedLists *ivf = new ArrayInvertedLists(
135 nlist_, index->code_size);
136
137 index->replace_invlists(ivf, true);
138
139 // Copy the inverted lists
140 if (index_) {
141 for (int i = 0; i < nlist_; ++i) {
142 ivf->add_entries (
143 i, index_->getListIndices(i).size(),
144 index_->getListIndices(i).data(),
145 (const uint8_t*)index_->getListVectors(i).data());
146 }
147 }
148 }
149
150 size_t
151 GpuIndexIVFFlat::reclaimMemory() {
152 if (index_) {
153 DeviceScope scope(device_);
154
155 return index_->reclaimMemory();
156 }
157
158 return 0;
159 }
160
161 void
162 GpuIndexIVFFlat::reset() {
163 if (index_) {
164 DeviceScope scope(device_);
165
166 index_->reset();
167 this->ntotal = 0;
168 } else {
169 FAISS_ASSERT(this->ntotal == 0);
170 }
171 }
172
173 void
174 GpuIndexIVFFlat::train(Index::idx_t n, const float* x) {
175 DeviceScope scope(device_);
176
177 if (this->is_trained) {
178 FAISS_ASSERT(quantizer_->is_trained);
179 FAISS_ASSERT(quantizer_->ntotal == nlist_);
180 FAISS_ASSERT(index_);
181 return;
182 }
183
184 FAISS_ASSERT(!index_);
185
186 trainQuantizer_(n, x);
187
188 // The quantizer is now trained; construct the IVF index
189 index_ = new IVFFlat(resources_,
190 quantizer_->getGpuData(),
191 this->metric_type == faiss::METRIC_L2,
192 ivfFlatConfig_.useFloat16IVFStorage,
193 ivfFlatConfig_.indicesOptions,
194 memorySpace_);
195
196 if (reserveMemoryVecs_) {
197 index_->reserveMemory(reserveMemoryVecs_);
198 }
199
200 this->is_trained = true;
201 }
202
203 void
204 GpuIndexIVFFlat::addImpl_(Index::idx_t n,
205 const float* x,
206 const Index::idx_t* xids) {
207 // Device is already set in GpuIndex::addInternal_
208 FAISS_ASSERT(index_);
209 FAISS_ASSERT(n > 0);
210
211 auto stream = resources_->getDefaultStreamCurrentDevice();
212
213 auto deviceVecs =
214 toDevice<float, 2>(resources_,
215 device_,
216 const_cast<float*>(x),
217 stream,
218 {(int) n, index_->getDim()});
219
220 static_assert(sizeof(long) == sizeof(Index::idx_t), "size mismatch");
221 auto deviceIds =
222 toDevice<long, 1>(resources_,
223 device_,
224 const_cast<long*>(xids),
225 stream,
226 {(int) n});
227
228 // Not all vectors may be able to be added (some may contain NaNs
229 // etc)
230 ntotal += index_->classifyAndAddVectors(deviceVecs, deviceIds);
231 }
232
233 void
234 GpuIndexIVFFlat::searchImpl_(faiss::Index::idx_t n,
235 const float* x,
236 faiss::Index::idx_t k,
237 float* distances,
238 faiss::Index::idx_t* labels) const {
239 // Device is already set in GpuIndex::search
240 FAISS_ASSERT(index_);
241 FAISS_ASSERT(n > 0);
242
243 auto stream = resources_->getDefaultStream(device_);
244
245 // Make sure arguments are on the device we desire; use temporary
246 // memory allocations to move it if necessary
247 auto devX =
248 toDevice<float, 2>(resources_,
249 device_,
250 const_cast<float*>(x),
251 stream,
252 {(int) n, this->d});
253 auto devDistances =
254 toDevice<float, 2>(resources_,
255 device_,
256 distances,
257 stream,
258 {(int) n, (int) k});
259 auto devLabels =
260 toDevice<faiss::Index::idx_t, 2>(resources_,
261 device_,
262 labels,
263 stream,
264 {(int) n, (int) k});
265
266 index_->query(devX, nprobe_, k, devDistances, devLabels);
267
268 // Copy back if necessary
269 fromDevice<float, 2>(devDistances, distances, stream);
270 fromDevice<faiss::Index::idx_t, 2>(devLabels, labels, stream);
271 }
272
273
274 } } // namespace
faiss::gpu::IVFFlat
Definition: IVFFlat.cuh:17
faiss::gpu::GpuIndexIVFFlat::GpuIndexIVFFlat
GpuIndexIVFFlat(GpuResources *resources, const faiss::IndexIVFFlat *index, GpuIndexIVFFlatConfig config=GpuIndexIVFFlatConfig())
Definition: GpuIndexIVFFlat.cu:25
faiss::gpu::GpuResources::getDefaultStreamCurrentDevice
cudaStream_t getDefaultStreamCurrentDevice()
Calls getDefaultStream with the current device.
Definition: GpuResources.cpp:25
faiss::ArrayInvertedLists
Definition: IndexIVF.h:123
faiss::gpu::GpuIndexIVFFlat::searchImpl_
void searchImpl_(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const override
Called from GpuIndex for search.
Definition: GpuIndexIVFFlat.cu:234
faiss::gpu::IVFBase::getDim
int getDim() const
Return the number of dimensions we are indexing.
Definition: IVFBase.cu:100
faiss::gpu::GpuIndexFlat::getGpuData
FlatIndex * getGpuData()
For internal access.
Definition: GpuIndexFlat.h:120
faiss::InvertedLists::list_size
virtual size_t list_size(size_t list_no) const =0
get the size of a list
faiss::gpu::GpuIndexIVFFlat::copyFrom
void copyFrom(const faiss::IndexIVFFlat *index)
Definition: GpuIndexIVFFlat.cu:79
faiss::gpu::IVFBase::reserveMemory
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
Definition: IVFBase.cu:45
faiss::gpu::GpuIndexIVFFlat::reclaimMemory
size_t reclaimMemory()
Definition: GpuIndexIVFFlat.cu:151
faiss::gpu::GpuIndexIVFFlatConfig::useFloat16IVFStorage
bool useFloat16IVFStorage
Definition: GpuIndexIVFFlat.h:29
faiss::gpu::GpuResources
Definition: GpuResources.h:23
faiss::IndexIVFFlat
Definition: IndexIVFFlat.h:25
faiss::gpu::GpuIndexIVFFlat::addImpl_
void addImpl_(faiss::Index::idx_t n, const float *x, const faiss::Index::idx_t *ids) override
Called from GpuIndex for add/add_with_ids.
Definition: GpuIndexIVFFlat.cu:204
faiss::gpu::GpuIndexIVFFlat::train
void train(Index::idx_t n, const float *x) override
Definition: GpuIndexIVFFlat.cu:174
faiss::InvertedLists
Definition: IndexIVF.h:67
faiss::gpu::GpuIndexIVF
Definition: GpuIndexIVF.h:37
faiss::Index::d
int d
vector dimension
Definition: Index.h:64
faiss::gpu::IVFFlat::classifyAndAddVectors
int classifyAndAddVectors(Tensor< float, 2, true > &vecs, Tensor< long, 1, true > &indices)
Definition: IVFFlat.cu:129
faiss::gpu::GpuIndexIVFFlat::copyTo
void copyTo(faiss::IndexIVFFlat *index) const
Definition: GpuIndexIVFFlat.cu:123
faiss::gpu::GpuIndexIVF::nprobe_
int nprobe_
Number of inverted list probes per query.
Definition: GpuIndexIVF.h:91
faiss::gpu::GpuIndexIVFFlat::reserveMemory
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
Definition: GpuIndexIVFFlat.cu:71
faiss::gpu::GpuIndex::device_
const int device_
The GPU device we are resident on.
Definition: GpuIndex.h:94
faiss::gpu::GpuIndex::resources_
GpuResources * resources_
Manages streans, cuBLAS handles and scratch memory for devices.
Definition: GpuIndex.h:91
faiss::gpu::GpuIndexIVF::copyTo
void copyTo(faiss::IndexIVF *index) const
Copy what we have to the CPU equivalent.
Definition: GpuIndexIVF.cu:149
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::gpu::GpuIndexIVF::nlist_
int nlist_
Number of inverted lists that we manage.
Definition: GpuIndexIVF.h:88
faiss::Index::ntotal
idx_t ntotal
total nb of indexed vectors
Definition: Index.h:65
faiss::gpu::IVFBase::reset
void reset()
Definition: IVFBase.cu:75
faiss::gpu::IVFFlat::addCodeVectorsFromCpu
void addCodeVectorsFromCpu(int listId, const float *vecs, const long *indices, size_t numVecs)
Definition: IVFFlat.cu:54
faiss::gpu::DeviceScope
Definition: DeviceUtils.h:65
faiss::gpu::GpuIndex::memorySpace_
const MemorySpace memorySpace_
The memory space of our primary storage on the GPU.
Definition: GpuIndex.h:97
faiss::gpu::GpuIndexIVF::quantizer_
GpuIndexFlat * quantizer_
Quantizer for inverted lists.
Definition: GpuIndexIVF.h:94
faiss::gpu::IVFFlat::query
void query(Tensor< float, 2, true > &queries, int nprobe, int k, Tensor< float, 2, true > &outDistances, Tensor< long, 2, true > &outIndices)
Definition: IVFFlat.cu:287
faiss::gpu::IVFFlat::getListVectors
std::vector< float > getListVectors(int listId) const
Return the vectors of a particular list back to the CPU.
Definition: IVFFlat.cu:353
faiss::Index::metric_type
MetricType metric_type
type of metric this index uses for search
Definition: Index.h:72
faiss::IndexIVF::invlists
InvertedLists * invlists
Acess to the actual data.
Definition: IndexIVF.h:168
faiss::gpu::IVFBase::getListIndices
std::vector< long > getListIndices(int listId) const
Return the list indices of a particular list back to the CPU.
Definition: IVFBase.cu:207
faiss::gpu::GpuIndexIVF::copyFrom
void copyFrom(const faiss::IndexIVF *index)
Copy what we need from the CPU equivalent.
Definition: GpuIndexIVF.cu:81
faiss::Index::is_trained
bool is_trained
set if the Index does not require training, or if training is done already
Definition: Index.h:69
faiss::gpu::GpuIndexIVFFlatConfig
Definition: GpuIndexIVFFlat.h:22
faiss::gpu::GpuIndexIVFConfig::indicesOptions
IndicesOptions indicesOptions
Index storage options for the GPU.
Definition: GpuIndexIVF.h:31
faiss::gpu::IVFBase::reclaimMemory
size_t reclaimMemory()
Definition: IVFBase.cu:105
faiss::gpu::GpuIndexIVFFlat::reset
void reset() override
removes all elements from the database.
Definition: GpuIndexIVFFlat.cu:162
faiss::IndexIVF::code_size
size_t code_size
code size per vector in bytes
Definition: IndexIVF.h:171
faiss::MetricType
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:43