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  virtual ~GpuIndexIVFFlat();
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  virtual void reset();
73 
74  virtual void train(Index::idx_t n, const float* x);
75 
76  virtual void set_typename();
77 
78  protected:
79  /// Called from GpuIndex for add/add_with_ids
80  virtual void addImpl_(faiss::Index::idx_t n,
81  const float* x,
82  const faiss::Index::idx_t* ids);
83 
84  /// Called from GpuIndex for search
85  virtual void searchImpl_(faiss::Index::idx_t n,
86  const float* x,
88  float* distances,
89  faiss::Index::idx_t* labels) const;
90 
91  private:
92  /// Is float16 encoding enabled for our IVF data?
93  bool useFloat16IVFStorage_;
94 
95  /// Desired inverted list memory reservation
96  size_t reserveMemoryVecs_;
97 
98  /// Instance that we own; contains the inverted list
99  IVFFlat* index_;
100 };
101 
102 } } // namespace
void copyFrom(const faiss::IndexIVFFlat *index)
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
virtual void searchImpl_(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const
Called from GpuIndex for search.
virtual void reset()
removes all elements from the database.
virtual void train(Index::idx_t n, const float *x)
virtual void addImpl_(faiss::Index::idx_t n, const float *x, const faiss::Index::idx_t *ids)
Called from GpuIndex for add/add_with_ids.
GpuIndexIVFFlat(GpuResources *resources, int device, bool useFloat16CoarseQuantizer, bool useFloat16IVFStorage, int dims, int nlist, IndicesOptions indicesOptions, faiss::MetricType metric)
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
Definition: Index.h:44