Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuIndexIVFPQ.cu
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the CC-by-NC 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 "GpuIndexIVFPQ.h"
12 #include "../IndexFlat.h"
13 #include "../IndexIVFPQ.h"
14 #include "../ProductQuantizer.h"
15 #include "GpuIndexFlat.h"
16 #include "GpuResources.h"
17 #include "impl/IVFPQ.cuh"
18 #include "utils/CopyUtils.cuh"
19 #include "utils/DeviceUtils.h"
20 
21 #include <limits>
22 
23 namespace faiss { namespace gpu {
24 
26  const faiss::IndexIVFPQ* index,
27  GpuIndexIVFPQConfig config) :
28  GpuIndexIVF(resources,
29  index->d,
30  index->metric_type,
31  index->nlist,
32  config),
33  ivfpqConfig_(config),
34  subQuantizers_(0),
35  bitsPerCode_(0),
36  reserveMemoryVecs_(0),
37  index_(nullptr) {
38 #ifndef FAISS_USE_FLOAT16
39  FAISS_ASSERT(!ivfpqConfig_.useFloat16LookupTables);
40 #endif
41 
42  copyFrom(index);
43 }
44 
46  int dims,
47  int nlist,
48  int subQuantizers,
49  int bitsPerCode,
50  faiss::MetricType metric,
51  GpuIndexIVFPQConfig config) :
52  GpuIndexIVF(resources,
53  dims,
54  metric,
55  nlist,
56  config),
57  ivfpqConfig_(config),
58  subQuantizers_(subQuantizers),
59  bitsPerCode_(bitsPerCode),
60  reserveMemoryVecs_(0),
61  index_(nullptr) {
62 #ifndef FAISS_USE_FLOAT16
63  FAISS_ASSERT(!useFloat16LookupTables_);
64 #endif
65 
66  verifySettings_();
67 
68  // FIXME make IP work fully
69  FAISS_ASSERT(this->metric_type == faiss::METRIC_L2);
70 
71  // We haven't trained ourselves, so don't construct the PQ index yet
72  this->is_trained = false;
73 }
74 
75 GpuIndexIVFPQ::~GpuIndexIVFPQ() {
76  delete index_;
77 }
78 
79 void
81  DeviceScope scope(device_);
82 
83  // FIXME: support this
84  FAISS_THROW_IF_NOT_MSG(index->metric_type == faiss::METRIC_L2,
85  "inner product unsupported");
86  GpuIndexIVF::copyFrom(index);
87 
88  // Clear out our old data
89  delete index_;
90  index_ = nullptr;
91 
92  subQuantizers_ = index->pq.M;
93  bitsPerCode_ = index->pq.nbits;
94 
95  // We only support this
96  FAISS_ASSERT(index->pq.byte_per_idx == 1);
97  FAISS_ASSERT(index->by_residual);
98  FAISS_ASSERT(index->polysemous_ht == 0);
99  ivfpqConfig_.usePrecomputedTables = (bool) index->use_precomputed_table;
100 
101  verifySettings_();
102 
103  // The other index might not be trained
104  if (!index->is_trained) {
105  return;
106  }
107 
108  // Otherwise, we can populate ourselves from the other index
109  this->is_trained = true;
110 
111  // Copy our lists as well
112  // The product quantizer must have data in it
113  FAISS_ASSERT(index->pq.centroids.size() > 0);
114  index_ = new IVFPQ(resources_,
116  subQuantizers_,
117  bitsPerCode_,
118  (float*) index->pq.centroids.data(),
119  ivfpqConfig_.indicesOptions,
120  ivfpqConfig_.useFloat16LookupTables,
121  memorySpace_);
122  // Doesn't make sense to reserve memory here
123  index_->setPrecomputedCodes(ivfpqConfig_.usePrecomputedTables);
124 
125  // Copy database vectors, if any
126  for (size_t i = 0; i < index->codes.size(); ++i) {
127  auto& codes = index->codes[i];
128  auto& ids = index->ids[i];
129 
130  FAISS_ASSERT(ids.size() * subQuantizers_ == codes.size());
131 
132  // GPU index can only support max int entries per list
133  FAISS_THROW_IF_NOT_FMT(ids.size() <=
134  (size_t) std::numeric_limits<int>::max(),
135  "GPU inverted list can only support "
136  "%zu entries; %zu found",
137  (size_t) std::numeric_limits<int>::max(),
138  ids.size());
139 
140  index_->addCodeVectorsFromCpu(i, codes.data(), ids.data(), ids.size());
141  }
142 }
143 
144 void
146  DeviceScope scope(device_);
147 
148  // We must have the indices in order to copy to ourselves
149  FAISS_THROW_IF_NOT_MSG(ivfpqConfig_.indicesOptions != INDICES_IVF,
150  "Cannot copy to CPU as GPU index doesn't retain "
151  "indices (INDICES_IVF)");
152 
153  GpuIndexIVF::copyTo(index);
154 
155  //
156  // IndexIVFPQ information
157  //
158  index->by_residual = true;
159  index->use_precomputed_table = 0;
160  index->code_size = subQuantizers_;
161  index->pq = faiss::ProductQuantizer(this->d, subQuantizers_, bitsPerCode_);
162 
163  index->do_polysemous_training = false;
164  index->polysemous_training = nullptr;
165 
166  index->scan_table_threshold = 0;
167  index->max_codes = 0;
168  index->polysemous_ht = 0;
169  index->codes.clear();
170  index->codes.resize(nlist_);
171  index->precomputed_table.clear();
172 
173  if (index_) {
174  // Copy the inverted lists
175  for (int i = 0; i < nlist_; ++i) {
176  index->ids[i] = getListIndices(i);
177  index->codes[i] = getListCodes(i);
178  }
179 
180  // Copy PQ centroids
181  auto devPQCentroids = index_->getPQCentroids();
182  index->pq.centroids.resize(devPQCentroids.numElements());
183 
184  fromDevice<float, 3>(devPQCentroids,
185  index->pq.centroids.data(),
186  resources_->getDefaultStream(device_));
187 
188  if (ivfpqConfig_.usePrecomputedTables) {
189  index->precompute_table();
190  }
191  }
192 }
193 
194 void
196  reserveMemoryVecs_ = numVecs;
197  if (index_) {
198  DeviceScope scope(device_);
199  index_->reserveMemory(numVecs);
200  }
201 }
202 
203 void
205  ivfpqConfig_.usePrecomputedTables = enable;
206  if (index_) {
207  DeviceScope scope(device_);
208  index_->setPrecomputedCodes(enable);
209  }
210 
211  verifySettings_();
212 }
213 
214 bool
216  return ivfpqConfig_.usePrecomputedTables;
217 }
218 
219 int
221  return subQuantizers_;
222 }
223 
224 int
226  return bitsPerCode_;
227 }
228 
229 int
231  return utils::pow2(bitsPerCode_);
232 }
233 
234 size_t
236  if (index_) {
237  DeviceScope scope(device_);
238  return index_->reclaimMemory();
239  }
240 
241  return 0;
242 }
243 
244 void
246  if (index_) {
247  DeviceScope scope(device_);
248 
249  index_->reset();
250  this->ntotal = 0;
251  } else {
252  FAISS_ASSERT(this->ntotal == 0);
253  }
254 }
255 
256 void
257 GpuIndexIVFPQ::trainResidualQuantizer_(Index::idx_t n, const float* x) {
258  // Code largely copied from faiss::IndexIVFPQ
259  // FIXME: GPUize more of this
260  n = std::min(n, (Index::idx_t) (1 << bitsPerCode_) * 64);
261 
262  if (this->verbose) {
263  printf("computing residuals\n");
264  }
265 
266  std::vector<Index::idx_t> assign(n);
267  quantizer_->assign (n, x, assign.data());
268 
269  std::vector<float> residuals(n * d);
270 
271  for (idx_t i = 0; i < n; i++) {
272  quantizer_->compute_residual(x + i * d, &residuals[i * d], assign[i]);
273  }
274 
275  if (this->verbose) {
276  printf("training %d x %d product quantizer on %ld vectors in %dD\n",
277  subQuantizers_, getCentroidsPerSubQuantizer(), n, this->d);
278  }
279 
280  // Just use the CPU product quantizer to determine sub-centroids
281  faiss::ProductQuantizer pq(this->d, subQuantizers_, bitsPerCode_);
282  pq.verbose = this->verbose;
283  pq.train(n, residuals.data());
284 
285  index_ = new IVFPQ(resources_,
287  subQuantizers_,
288  bitsPerCode_,
289  pq.centroids.data(),
290  ivfpqConfig_.indicesOptions,
291  ivfpqConfig_.useFloat16LookupTables,
292  memorySpace_);
293  if (reserveMemoryVecs_) {
294  index_->reserveMemory(reserveMemoryVecs_);
295  }
296 
297  index_->setPrecomputedCodes(ivfpqConfig_.usePrecomputedTables);
298 }
299 
300 void
301 GpuIndexIVFPQ::train(Index::idx_t n, const float* x) {
302  DeviceScope scope(device_);
303 
304  if (this->is_trained) {
305  FAISS_ASSERT(quantizer_->is_trained);
306  FAISS_ASSERT(quantizer_->ntotal == nlist_);
307  FAISS_ASSERT(index_);
308  return;
309  }
310 
311  FAISS_ASSERT(!index_);
312 
313  trainQuantizer_(n, x);
314  trainResidualQuantizer_(n, x);
315 
316  this->is_trained = true;
317 }
318 
319 void
321  const float* x,
322  const Index::idx_t* xids) {
323  // Device is already set in GpuIndex::addInternal_
324  FAISS_ASSERT(index_);
325  FAISS_ASSERT(n > 0);
326 
327  auto stream = resources_->getDefaultStreamCurrentDevice();
328 
329  auto deviceVecs =
330  toDevice<float, 2>(resources_,
331  device_,
332  const_cast<float*>(x),
333  stream,
334  {(int) n, index_->getDim()});
335 
336  auto deviceIndices =
337  toDevice<Index::idx_t, 1>(resources_,
338  device_,
339  const_cast<Index::idx_t*>(xids),
340  stream,
341  {(int) n});
342 
343  // Not all vectors may be able to be added (some may contain NaNs
344  // etc)
345  ntotal += index_->classifyAndAddVectors(deviceVecs, deviceIndices);
346 }
347 
348 void
350  const float* x,
352  float* distances,
353  faiss::Index::idx_t* labels) const {
354  // Device is already set in GpuIndex::search
355 
356  FAISS_ASSERT(index_);
357  FAISS_ASSERT(n > 0);
358 
359  // Make sure arguments are on the device we desire; use temporary
360  // memory allocations to move it if necessary
361  auto devX =
362  toDevice<float, 2>(resources_,
363  device_,
364  const_cast<float*>(x),
365  resources_->getDefaultStream(device_),
366  {(int) n, index_->getDim()});
367  auto devDistances =
368  toDevice<float, 2>(resources_,
369  device_,
370  distances,
371  resources_->getDefaultStream(device_),
372  {(int) n, (int) k});
373  auto devLabels =
374  toDevice<faiss::Index::idx_t, 2>(resources_,
375  device_,
376  labels,
377  resources_->getDefaultStream(device_),
378  {(int) n, (int) k});
379 
380  index_->query(devX,
381  nprobe_,
382  (int) k,
383  devDistances,
384  devLabels);
385 
386  // Copy back if necessary
387  fromDevice<float, 2>(
388  devDistances, distances, resources_->getDefaultStream(device_));
389  fromDevice<faiss::Index::idx_t, 2>(
390  devLabels, labels, resources_->getDefaultStream(device_));
391 }
392 
393 int
394 GpuIndexIVFPQ::getListLength(int listId) const {
395  FAISS_ASSERT(index_);
396  return index_->getListLength(listId);
397 }
398 
399 std::vector<unsigned char>
400 GpuIndexIVFPQ::getListCodes(int listId) const {
401  FAISS_ASSERT(index_);
402  DeviceScope scope(device_);
403 
404  return index_->getListCodes(listId);
405 }
406 
407 std::vector<long>
408 GpuIndexIVFPQ::getListIndices(int listId) const {
409  FAISS_ASSERT(index_);
410  DeviceScope scope(device_);
411 
412  return index_->getListIndices(listId);
413 }
414 
415 void
416 GpuIndexIVFPQ::verifySettings_() const {
417  // Our implementation has these restrictions:
418 
419  // Must have some number of lists
420  FAISS_THROW_IF_NOT_MSG(nlist_ > 0, "nlist must be >0");
421 
422  // up to a single byte per code
423  FAISS_THROW_IF_NOT_FMT(bitsPerCode_ <= 8,
424  "Bits per code must be <= 8 (passed %d)", bitsPerCode_);
425 
426  // Sub-quantizers must evenly divide dimensions available
427  FAISS_THROW_IF_NOT_FMT(this->d % subQuantizers_ == 0,
428  "Number of sub-quantizers (%d) must be an "
429  "even divisor of the number of dimensions (%d)",
430  subQuantizers_, this->d);
431 
432  // The number of bytes per encoded vector must be one we support
433  FAISS_THROW_IF_NOT_FMT(IVFPQ::isSupportedPQCodeLength(subQuantizers_),
434  "Number of bytes per encoded vector / sub-quantizers (%d) "
435  "is not supported",
436  subQuantizers_);
437 
438  // We must have enough shared memory on the current device to store
439  // our lookup distances
440  int lookupTableSize = sizeof(float);
441 #ifdef FAISS_USE_FLOAT16
442  if (ivfpqConfig_.useFloat16LookupTables) {
443  lookupTableSize = sizeof(half);
444  }
445 #endif
446 
447  // 64 bytes per code is only supported with usage of float16, at 2^8
448  // codes per subquantizer
449  size_t requiredSmemSize =
450  lookupTableSize * subQuantizers_ * utils::pow2(bitsPerCode_);
451  size_t smemPerBlock = getMaxSharedMemPerBlock(device_);
452 
453  FAISS_THROW_IF_NOT_FMT(requiredSmemSize
454  <= getMaxSharedMemPerBlock(device_),
455  "Device %d has %zu bytes of shared memory, while "
456  "%d bits per code and %d sub-quantizers requires %zu "
457  "bytes. Consider useFloat16LookupTables and/or "
458  "reduce parameters",
459  device_, smemPerBlock, bitsPerCode_, subQuantizers_,
460  requiredSmemSize);
461 
462  // If precomputed codes are disabled, we have an extra limitation in
463  // terms of the number of dimensions per subquantizer
464  FAISS_THROW_IF_NOT_FMT(ivfpqConfig_.usePrecomputedTables ||
466  this->d / subQuantizers_),
467  "Number of dimensions per sub-quantizer (%d) "
468  "is unsupported with precomputed codes",
469  this->d / subQuantizers_);
470 
471  // TODO: fully implement METRIC_INNER_PRODUCT
472  FAISS_THROW_IF_NOT_MSG(this->metric_type == faiss::METRIC_L2,
473  "METRIC_INNER_PRODUCT is currently unsupported");
474 }
475 
476 } } // namespace
std::vector< long > getListIndices(int listId) const
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.
void precompute_table()
build precomputed table
Definition: IndexIVFPQ.cpp:391
size_t nbits
number of bits per quantization index
PolysemousTraining * polysemous_training
if NULL, use default
Definition: IndexIVFPQ.h:36
size_t byte_per_idx
nb bytes per code component (1 or 2)
GpuIndexIVFPQ(GpuResources *resources, const faiss::IndexIVFPQ *index, GpuIndexIVFPQConfig config=GpuIndexIVFPQConfig())
int getDim() const
Return the number of dimensions we are indexing.
Definition: IVFBase.cu:100
int getListLength(int listId) const
Definition: IVFBase.cu:200
FlatIndex * getGpuData()
For internal access.
Definition: GpuIndexFlat.h:120
void assign(idx_t n, const float *x, idx_t *labels, idx_t k=1)
Definition: Index.cpp:23
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
Definition: IVFBase.cu:45
int getListLength(int listId) const
bool do_polysemous_training
reorder PQ centroids after training?
Definition: IndexIVFPQ.h:35
size_t scan_table_threshold
use table computation or on-the-fly?
Definition: IndexIVFPQ.h:39
std::vector< float > precomputed_table
Definition: IndexIVFPQ.h:47
int polysemous_ht
Hamming thresh for polysemous filtering.
Definition: IndexIVFPQ.h:41
int getBitsPerCode() const
Return the number of bits per PQ code.
std::vector< std::vector< long > > ids
Inverted lists for indexes.
Definition: IndexIVF.h:55
int d
vector dimension
Definition: Index.h:64
void train(Index::idx_t n, const float *x) override
size_t max_codes
max nb of codes to visit to do a query
Definition: IndexIVFPQ.h:40
static bool isSupportedPQCodeLength(int size)
Returns true if we support PQ in this size.
Definition: IVFPQ.cu:72
int nprobe_
Number of inverted list probes per query.
Definition: GpuIndexIVF.h:86
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
int classifyAndAddVectors(Tensor< float, 2, true > &vecs, Tensor< long, 1, true > &indices)
Definition: IVFPQ.cu:120
void query(Tensor< float, 2, true > &queries, int nprobe, int k, Tensor< float, 2, true > &outDistances, Tensor< long, 2, true > &outIndices)
Definition: IVFPQ.cu:519
const int device_
The GPU device we are resident on.
Definition: GpuIndex.h:94
void copyFrom(const faiss::IndexIVFPQ *index)
Tensor< float, 3, true > getPQCentroids()
Definition: IVFPQ.cu:594
GpuResources * resources_
Manages streans, cuBLAS handles and scratch memory for devices.
Definition: GpuIndex.h:91
void copyTo(faiss::IndexIVF *index) const
Copy what we have to the CPU equivalent.
Definition: GpuIndexIVF.cu:148
long idx_t
all indices are this type
Definition: Index.h:62
int nlist_
Number of inverted lists that we manage.
Definition: GpuIndexIVF.h:83
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.
idx_t ntotal
total nb of indexed vectors
Definition: Index.h:65
bool verbose
verbosity level
Definition: Index.h:66
void setPrecomputedCodes(bool enable)
Enable or disable pre-computed codes.
Definition: IVFPQ.cu:102
std::vector< unsigned char > getListCodes(int listId) const
Return the list codes of a particular list back to the CPU.
Definition: IVFPQ.cu:586
void copyTo(faiss::IndexIVFPQ *index) const
const MemorySpace memorySpace_
The memory space of our primary storage on the GPU.
Definition: GpuIndex.h:97
bool by_residual
Encode residual or plain vector?
Definition: IndexIVFPQ.h:30
GpuIndexFlat * quantizer_
Quantizer for inverted lists.
Definition: GpuIndexIVF.h:92
MetricType metric_type
type of metric this index uses for search
Definition: Index.h:72
ProductQuantizer pq
produces the codes
Definition: IndexIVFPQ.h:33
size_t M
number of subquantizers
int getNumSubQuantizers() const
Return the number of sub-quantizers we are using.
std::vector< long > getListIndices(int listId) const
Return the list indices of a particular list back to the CPU.
Definition: IVFBase.cu:207
int getCentroidsPerSubQuantizer() const
Return the number of centroids per PQ code (2^bits per code)
size_t code_size
code size per vector in bytes
Definition: IndexIVFPQ.h:32
void setPrecomputedCodes(bool enable)
Enable or disable pre-computed codes.
void copyFrom(const faiss::IndexIVF *index)
Copy what we need from the CPU equivalent.
Definition: GpuIndexIVF.cu:80
bool is_trained
set if the Index does not require training, or if training is done already
Definition: Index.h:69
void compute_residual(const float *x, float *residual, idx_t key) const
Definition: Index.cpp:58
bool getPrecomputedCodes() const
Are pre-computed codes enabled?
IndicesOptions indicesOptions
Index storage options for the GPU.
Definition: GpuIndexIVF.h:31
size_t reclaimMemory()
Definition: IVFBase.cu:105
Implementing class for IVFPQ on the GPU.
Definition: IVFPQ.cuh:19
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
Definition: Index.h:43
int use_precomputed_table
if by_residual, build precompute tables
Definition: IndexIVFPQ.h:31
std::vector< unsigned char > getListCodes(int listId) const
std::vector< float > centroids
Centroid table, size M * ksub * dsub.
static bool isSupportedNoPrecomputedSubDimSize(int dims)
Definition: IVFPQ.cu:97