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