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.useFloat16CoarseQuantizer,
33 "float16 unsupported; need CUDA SDK >= 7.5");
40 GpuIndexIVF::init_() {
44 if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
55 GpuIndexFlatConfig config = ivfConfig_.
flatConfig;
61 }
else if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
65 FAISS_ASSERT_MSG(
false,
"unknown metric type");
70 GpuIndexIVF::~GpuIndexIVF() {
86 FAISS_ASSERT(index->
nlist > 0);
87 FAISS_THROW_IF_NOT_FMT(index->
nlist <=
89 "GPU index only supports %zu inverted lists",
90 (
size_t) std::numeric_limits<int>::max());
107 }
else if (index->
metric_type == faiss::METRIC_INNER_PRODUCT) {
136 }
else if (index->
metric_type == faiss::METRIC_INNER_PRODUCT) {
171 }
else if (this->
metric_type == faiss::METRIC_INNER_PRODUCT) {
189 index->
cp = this->
cp;
193 index->direct_map.clear();
203 FAISS_THROW_IF_NOT_FMT(nprobe > 0 && nprobe <= 1024,
204 "nprobe must be from 1 to 1024; passed %d",
217 std::vector<Index::idx_t> ids(n);
219 ids[i] = this->
ntotal + i;
234 printf (
"IVF quantizer does not need training.\n");
241 printf (
"Training IVF quantizer on %ld vectors in %dD\n", n,
d);
249 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
bool quantizer_trains_alone
just pass over the trainset to quantizer
int device
GPU device on which the index is resident.
std::vector< std::vector< long > > ids
Inverted lists for indexes.
void add_with_ids(Index::idx_t n, const float *x, const Index::idx_t *ids) override
Index * quantizer
quantizer that maps vectors to inverted lists
GpuIndexFlatConfig flatConfig
Configuration for the coarse quantizer object.
GpuIndexFlat * getQuantizer()
Return the quantizer we're using.
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 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
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
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
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.