Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuIndexIVFFlat.h
1 
2 /**
3  * Copyright (c) 2015-present, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under the CC-by-NC license found in the
7  * LICENSE file in the root directory of this source tree.
8  */
9 
10 // Copyright 2004-present Facebook. All Rights Reserved.
11 
12 #pragma once
13 
14 #include "GpuIndexIVF.h"
15 
16 namespace faiss { struct IndexIVFFlat; }
17 
18 namespace faiss { namespace gpu {
19 
20 class IVFFlat;
21 class GpuIndexFlat;
22 
23 /// Wrapper around the GPU implementation that looks like
24 /// faiss::IndexIVFFlat
25 class GpuIndexIVFFlat : public GpuIndexIVF {
26  public:
27  /// Constructs a new instance with an empty flat quantizer; the user
28  /// provides the number of lists desired.
29  GpuIndexIVFFlat(GpuResources* resources,
30  int device,
31  // Does the coarse quantizer use float16?
32  bool useFloat16CoarseQuantizer,
33  // Is our IVF storage of vectors in float16?
34  bool useFloat16IVFStorage,
35  int dims,
36  int nlist,
37  IndicesOptions indicesOptions,
38  faiss::MetricType metric);
39 
40  /// Call to initialize ourselves from a GpuIndexFlat instance. The
41  /// quantizer must match the dimension parameters specified; if
42  /// populated, it must also match the number of list elements
43  /// available.
44  /// The index must also be present on the same device as ourselves.
45  /// We do not own this quantizer instance.
46  GpuIndexIVFFlat(GpuResources* resources,
47  int device,
48  GpuIndexFlat* quantizer,
49  bool useFloat16,
50  int dims,
51  int nlist,
52  IndicesOptions indicesOptions,
53  faiss::MetricType metric);
54 
55  ~GpuIndexIVFFlat() override;
56 
57  /// Reserve GPU memory in our inverted lists for this number of vectors
58  void reserveMemory(size_t numVecs);
59 
60  /// Initialize ourselves from the given CPU index; will overwrite
61  /// all data in ourselves
62  void copyFrom(const faiss::IndexIVFFlat* index);
63 
64  /// Copy ourselves to the given CPU index; will overwrite all data
65  /// in the index instance
66  void copyTo(faiss::IndexIVFFlat* index) const;
67 
68  /// After adding vectors, one can call this to reclaim device memory
69  /// to exactly the amount needed. Returns space reclaimed in bytes
70  size_t reclaimMemory();
71 
72  void reset() override;
73 
74  void train(Index::idx_t n, const float* x) override;
75 
76  /// `x` and `xids` can be resident on the CPU or any GPU; the proper
77  /// copies are performed
79  const float* x,
80  const Index::idx_t* xids) override;
81 
82  /// `x`, `distances` and `labels` can be resident on the CPU or any
83  /// GPU; copies are performed as needed
85  const float* x,
87  float* distances,
88  faiss::Index::idx_t* labels) const override;
89 
90  void set_typename() override;
91 
92  private:
93  /// Is float16 encoding enabled for our IVF data?
94  bool useFloat16IVFStorage_;
95 
96  /// Desired inverted list memory reservation
97  size_t reserveMemoryVecs_;
98 
99  /// Instance that we own; contains the inverted list
100  IVFFlat* index_;
101 };
102 
103 } } // namespace
void copyFrom(const faiss::IndexIVFFlat *index)
void train(Index::idx_t n, const float *x) override
void copyTo(faiss::IndexIVFFlat *index) const
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
long idx_t
all indices are this type
Definition: Index.h:64
void add_with_ids(Index::idx_t n, const float *x, const Index::idx_t *xids) override
GpuIndexIVFFlat(GpuResources *resources, int device, bool useFloat16CoarseQuantizer, bool useFloat16IVFStorage, int dims, int nlist, IndicesOptions indicesOptions, faiss::MetricType metric)
void reset() override
removes all elements from the database.
void search(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const override
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
Definition: Index.h:44