Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
GpuIndexIVFFlat.h
1 /**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8
9 #pragma once
10
11 #include "GpuIndexIVF.h"
12
13 namespace faiss { struct IndexIVFFlat; }
14
15 namespace faiss { namespace gpu {
16
17 class IVFFlat;
18 class GpuIndexFlat;
19
20 struct GpuIndexIVFFlatConfig : public GpuIndexIVFConfig {
21 inline GpuIndexIVFFlatConfig()
22 : useFloat16IVFStorage(false) {
23 }
24
25 /// Whether or not IVFFlat inverted list storage is in float16;
26 /// supported on all architectures
27 bool useFloat16IVFStorage;
28 };
29
30 /// Wrapper around the GPU implementation that looks like
31 /// faiss::IndexIVFFlat
32 class GpuIndexIVFFlat : public GpuIndexIVF {
33 public:
34 /// Construct from a pre-existing faiss::IndexIVFFlat instance, copying
35 /// data over to the given GPU, if the input index is trained.
36 GpuIndexIVFFlat(GpuResources* resources,
37 const faiss::IndexIVFFlat* index,
38 GpuIndexIVFFlatConfig config = GpuIndexIVFFlatConfig());
39
40 /// Constructs a new instance with an empty flat quantizer; the user
41 /// provides the number of lists desired.
42 GpuIndexIVFFlat(GpuResources* resources,
43 int dims,
44 int nlist,
45 faiss::MetricType metric,
46 GpuIndexIVFFlatConfig config = GpuIndexIVFFlatConfig());
47
48 ~GpuIndexIVFFlat() override;
49
50 /// Reserve GPU memory in our inverted lists for this number of vectors
51 void reserveMemory(size_t numVecs);
52
53 /// Initialize ourselves from the given CPU index; will overwrite
54 /// all data in ourselves
55 void copyFrom(const faiss::IndexIVFFlat* index);
56
57 /// Copy ourselves to the given CPU index; will overwrite all data
58 /// in the index instance
59 void copyTo(faiss::IndexIVFFlat* index) const;
60
61 /// After adding vectors, one can call this to reclaim device memory
62 /// to exactly the amount needed. Returns space reclaimed in bytes
63 size_t reclaimMemory();
64
65 void reset() override;
66
67 void train(Index::idx_t n, const float* x) override;
68
69 protected:
70 /// Called from GpuIndex for add/add_with_ids
71 void addImpl_(int n,
72 const float* x,
73 const Index::idx_t* ids) override;
74
75 /// Called from GpuIndex for search
76 void searchImpl_(int n,
77 const float* x,
78 int k,
79 float* distances,
80 Index::idx_t* labels) const override;
81
82 private:
83 GpuIndexIVFFlatConfig ivfFlatConfig_;
84
85 /// Desired inverted list memory reservation
86 size_t reserveMemoryVecs_;
87
88 /// Instance that we own; contains the inverted list
89 IVFFlat* index_;
90 };
91
92 } } // namespace
faiss::gpu::IVFFlat
Definition: IVFFlat.cuh:15
faiss::gpu::GpuIndexIVFFlat::GpuIndexIVFFlat
GpuIndexIVFFlat(GpuResources *resources, const faiss::IndexIVFFlat *index, GpuIndexIVFFlatConfig config=GpuIndexIVFFlatConfig())
Definition: GpuIndexIVFFlat.cu:23
faiss::gpu::GpuIndexIVFFlat::addImpl_
void addImpl_(int n, const float *x, const Index::idx_t *ids) override
Called from GpuIndex for add/add_with_ids.
Definition: GpuIndexIVFFlat.cu:202
faiss::gpu::GpuIndexIVFFlat::copyFrom
void copyFrom(const faiss::IndexIVFFlat *index)
Definition: GpuIndexIVFFlat.cu:77
faiss::gpu::GpuIndexIVFConfig
Definition: GpuIndexIVF.h:23
faiss::gpu::GpuIndexIVFFlat::reclaimMemory
size_t reclaimMemory()
Definition: GpuIndexIVFFlat.cu:149
faiss::gpu::GpuIndexIVFFlatConfig::useFloat16IVFStorage
bool useFloat16IVFStorage
Definition: GpuIndexIVFFlat.h:27
faiss::gpu::GpuResources
Definition: GpuResources.h:21
faiss::IndexIVFFlat
Definition: IndexIVFFlat.h:24
faiss::gpu::GpuIndexIVFFlat::train
void train(Index::idx_t n, const float *x) override
Definition: GpuIndexIVFFlat.cu:172
faiss::gpu::GpuIndexIVF
Definition: GpuIndexIVF.h:35
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::gpu::GpuIndexIVFFlat::copyTo
void copyTo(faiss::IndexIVFFlat *index) const
Definition: GpuIndexIVFFlat.cu:121
faiss::gpu::GpuIndexIVFFlat::reserveMemory
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
Definition: GpuIndexIVFFlat.cu:69
faiss::gpu::GpuIndexIVFFlat::searchImpl_
void searchImpl_(int n, const float *x, int k, float *distances, Index::idx_t *labels) const override
Called from GpuIndex for search.
Definition: GpuIndexIVFFlat.cu:224
faiss::gpu::GpuIndexIVFFlatConfig
Definition: GpuIndexIVFFlat.h:20
faiss::gpu::GpuIndexIVFFlat
Definition: GpuIndexIVFFlat.h:32
faiss::gpu::GpuIndexIVFFlat::reset
void reset() override
removes all elements from the database.
Definition: GpuIndexIVFFlat.cu:160
faiss::MetricType
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:44