11 #include "GpuIndexIVF.h"
12 #include "../FaissAssert.h"
13 #include "../IndexFlat.h"
14 #include "../IndexIVF.h"
15 #include "GpuIndexFlat.h"
16 #include "utils/DeviceUtils.h"
17 #include "utils/Float16.cuh"
19 namespace faiss {
namespace gpu {
21 GpuIndexIVF::GpuIndexIVF(GpuResources* resources,
25 GpuIndexIVFConfig config) :
26 GpuIndex(resources, dims, metric, config),
27 ivfConfig_(std::move(config)),
31 #ifndef FAISS_USE_FLOAT16
32 FAISS_THROW_IF_NOT_MSG(!ivfConfig_.flatConfig.useFloat16 &&
33 !ivfConfig_.flatConfig.useFloat16Accumulator,
34 "float16 unsupported; need CUDA SDK >= 7.5");
41 GpuIndexIVF::init_() {
45 if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
56 GpuIndexFlatConfig config = ivfConfig_.
flatConfig;
62 }
else if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
66 FAISS_ASSERT_MSG(
false,
"unknown metric type");
71 GpuIndexIVF::~GpuIndexIVF() {
87 FAISS_ASSERT(index->
nlist > 0);
88 FAISS_THROW_IF_NOT_FMT(index->
nlist <=
90 "GPU index only supports %zu inverted lists",
91 (
size_t) std::numeric_limits<int>::max());
108 }
else if (index->
metric_type == faiss::METRIC_INNER_PRODUCT) {
137 }
else if (index->
metric_type == faiss::METRIC_INNER_PRODUCT) {
172 }
else if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
190 index->
cp = this->
cp;
192 index->direct_map.clear();
202 FAISS_THROW_IF_NOT_FMT(nprobe > 0 && nprobe <= 1024,
203 "nprobe must be from 1 to 1024; passed %d",
216 std::vector<Index::idx_t> ids(n);
218 ids[i] = this->
ntotal + i;
233 printf (
"IVF quantizer does not need training.\n");
240 printf (
"Training IVF quantizer on %ld vectors in %dD\n", n,
d);
248 Clustering clus(this->
d,
nlist_, this->
cp);
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
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
int device
GPU device on which the index is resident.
void add_with_ids(Index::idx_t n, const float *x, const Index::idx_t *ids) override
GpuIndexFlatConfig flatConfig
Configuration for the coarse quantizer object.
GpuIndexFlat * getQuantizer()
Return the quantizer we're using.
int nprobe_
Number of inverted list probes per query.
char quantizer_trains_alone
const int device_
The GPU device we are resident on.
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
ClusteringParameters cp
to override default clustering params
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
void reset() override
Clears all vectors from this index.
Index * quantizer
quantizer that maps vectors to inverted lists
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
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?
bool own_fields
whether object owns the quantizer
size_t nlist
number of possible key values
MetricType
Some algorithms support both an inner product version and a L2 search version.