12 #include "GpuIndexIVF.h"
13 #include "../FaissAssert.h"
14 #include "../IndexFlat.h"
15 #include "../IndexIVF.h"
16 #include "GpuIndexFlat.h"
17 #include "utils/DeviceUtils.h"
18 #include "utils/Float16.cuh"
20 namespace faiss {
namespace gpu {
22 GpuIndexIVF::GpuIndexIVF(GpuResources* resources,
24 IndicesOptions indicesOptions,
25 bool useFloat16CoarseQuantizer,
29 GpuIndex(resources, device, dims, metric),
30 indicesOptions_(indicesOptions),
31 useFloat16CoarseQuantizer_(useFloat16CoarseQuantizer),
35 ownsQuantizer_(true) {
36 #ifndef FAISS_USE_FLOAT16
37 FAISS_ASSERT(!useFloat16CoarseQuantizer_,
38 "float16 unsupported; need CUDA SDK >= 7.5");
44 GpuIndexIVF::GpuIndexIVF(GpuResources* resources,
46 IndicesOptions indicesOptions,
50 GpuIndexFlat* quantizer) :
51 GpuIndex(resources, device, dims, metric),
52 indicesOptions_(indicesOptions),
53 useFloat16CoarseQuantizer_(quantizer->getUseFloat16()),
56 quantizer_(quantizer),
57 ownsQuantizer_(false) {
58 #ifndef FAISS_USE_FLOAT16
59 FAISS_ASSERT(!useFloat16CoarseQuantizer_,
60 "float16 unsupported; need CUDA SDK >= 7.5");
63 FAISS_ASSERT(quantizer_->d == this->d);
64 FAISS_ASSERT(quantizer_->metric_type == this->metric_type);
70 GpuIndexIVF::init_() {
74 if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
89 }
else if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
100 GpuIndexIVF::~GpuIndexIVF() {
123 FAISS_ASSERT(index->
nlist > 0);
124 FAISS_ASSERT(index->
nlist <=
138 }
else if (index->
metric_type == faiss::METRIC_INNER_PRODUCT) {
157 FAISS_ASSERT(index->
ntotal <=
169 }
else if (index->
metric_type == faiss::METRIC_INNER_PRODUCT) {
204 }
else if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
226 index->direct_map.clear();
236 FAISS_ASSERT(nprobe > 0);
249 std::vector<Index::idx_t> ids(n);
251 ids[i] = this->
ntotal + i;
266 printf (
"IVF quantizer does not need training.\n");
273 printf (
"Training IVF quantizer on %ld vectors in %dD\n", n,
d);
IndicesOptions getIndicesOptions() const
What indices storage options are we using?
int getNumProbes() const
Returns our current number of list probes per query.
void setNumProbes(int nprobe)
Sets the number of list probes per query.
int niter
clustering iterations
const bool useFloat16CoarseQuantizer_
int getNumLists() const
Returns the number of inverted lists we're managing.
void copyTo(faiss::IndexFlat *index) const
size_t nprobe
number of probes at query time
bool getUseFloat16CoarseQuantizer() const
Is our coarse quantizer storing and performing math in float16?
bool quantizer_trains_alone
just pass over the trainset to quantizer
virtual void add_with_ids(idx_t n, const float *x, const long *xids)
int device_
The GPU device we are resident on.
std::vector< std::vector< long > > ids
Inverted lists for indexes.
Index * quantizer
quantizer that maps vectors to inverted lists
ClusteringParameters cp
to override default clustering params
int nprobe_
Number of inverted list probes per query.
bool own_fields
whether object owns the quantizer
const IndicesOptions indicesOptions_
How should indices be stored on the GPU?
GpuResources * resources_
Manages streans, cuBLAS handles and scratch memory for devices.
void copyTo(faiss::IndexIVF *index) const
Copy what we have to the CPU equivalent.
long idx_t
all indices are this type
int nlist_
Number of inverted lists that we manage.
idx_t ntotal
total nb of indexed vectors
bool verbose
verbosity level
void copyFrom(const faiss::IndexFlat *index)
GpuIndexFlat * quantizer_
Quantizer for inverted lists.
MetricType metric_type
type of metric this index uses for search
size_t nlist
number of possible key values
ClusteringParameters cp_
Ability to override default clustering parameters.
void reset() override
Clears all vectors from this index.
void copyFrom(const faiss::IndexIVF *index)
Copy what we need from the CPU equivalent.
bool is_trained
set if the Index does not require training, or if training is done already
bool ownsQuantizer_
Do we own the above quantizer instance?
void add(Index::idx_t n, const float *x) override
bool maintain_direct_map
map for direct access to the elements. Enables reconstruct().
bool spherical
do we want normalized centroids?
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.