9 #include "GpuIndexIVFFlat.h"
10 #include "../IndexFlat.h"
11 #include "../IndexIVFFlat.h"
12 #include "GpuIndexFlat.h"
13 #include "GpuResources.h"
14 #include "impl/IVFFlat.cuh"
15 #include "utils/CopyUtils.cuh"
16 #include "utils/DeviceUtils.h"
17 #include "utils/Float16.cuh"
21 namespace faiss {
namespace gpu {
31 ivfFlatConfig_(config),
32 reserveMemoryVecs_(0),
34 #ifndef FAISS_USE_FLOAT16
36 "float16 unsupported; need CUDA SDK >= 7.5");
47 GpuIndexIVF(resources, dims, metric, nlist, config),
48 ivfFlatConfig_(config),
49 reserveMemoryVecs_(0),
55 #ifndef FAISS_USE_FLOAT16
57 "float16 unsupported; need CUDA SDK >= 7.5");
64 GpuIndexIVFFlat::~GpuIndexIVFFlat() {
70 reserveMemoryVecs_ = numVecs;
103 for (
size_t i = 0; i < ivf->nlist; ++i) {
107 FAISS_THROW_IF_NOT_FMT(numVecs <=
108 (
size_t) std::numeric_limits<int>::max(),
109 "GPU inverted list can only support "
110 "%zu entries; %zu found",
111 (
size_t) std::numeric_limits<int>::max(),
115 i, (
const float*)(ivf->get_codes(i)),
116 ivf->get_ids(i), numVecs);
125 FAISS_THROW_IF_NOT_MSG(ivfFlatConfig_.
indicesOptions != INDICES_IVF,
126 "Cannot copy to CPU as GPU index doesn't retain "
127 "indices (INDICES_IVF)");
139 for (
int i = 0; i <
nlist_; ++i) {
167 FAISS_ASSERT(this->
ntotal == 0);
178 FAISS_ASSERT(index_);
182 FAISS_ASSERT(!index_);
184 trainQuantizer_(n, x);
194 if (reserveMemoryVecs_) {
206 FAISS_ASSERT(index_);
212 static_assert(
sizeof(
long) ==
sizeof(
Index::idx_t),
"size mismatch");
230 FAISS_ASSERT(index_);
237 static_assert(
sizeof(
long) ==
sizeof(
Index::idx_t),
"size mismatch");
240 index_->
query(queries,
nprobe_, k, outDistances, outLabels);
GpuIndexIVFFlat(GpuResources *resources, const faiss::IndexIVFFlat *index, GpuIndexIVFFlatConfig config=GpuIndexIVFFlatConfig())
simple (default) implementation as an array of inverted lists
FlatIndex * getGpuData()
For internal access.
void addImpl_(int n, const float *x, const Index::idx_t *ids) override
Called from GpuIndex for add/add_with_ids.
virtual size_t list_size(size_t list_no) const =0
get the size of a list
void copyFrom(const faiss::IndexIVFFlat *index)
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
bool useFloat16IVFStorage
void train(Index::idx_t n, const float *x) override
long idx_t
all indices are this type
int classifyAndAddVectors(Tensor< float, 2, true > &vecs, Tensor< long, 1, true > &indices)
void copyTo(faiss::IndexIVFFlat *index) const
int nprobe_
Number of inverted list probes per query.
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
const int device_
The GPU device we are resident on.
GpuResources * resources_
Manages streams, cuBLAS handles and scratch memory for devices.
void copyTo(faiss::IndexIVF *index) const
Copy what we have to the CPU equivalent.
void replace_invlists(InvertedLists *il, bool own=false)
replace the inverted lists, old one is deallocated if own_invlists
int nlist_
Number of inverted lists that we manage.
idx_t ntotal
total nb of indexed vectors
void addCodeVectorsFromCpu(int listId, const float *vecs, const long *indices, size_t numVecs)
const MemorySpace memorySpace_
The memory space of our primary storage on the GPU.
GpuIndexFlat * quantizer_
Quantizer for inverted lists.
void query(Tensor< float, 2, true > &queries, int nprobe, int k, Tensor< float, 2, true > &outDistances, Tensor< long, 2, true > &outIndices)
std::vector< float > getListVectors(int listId) const
Return the vectors of a particular list back to the CPU.
MetricType metric_type
type of metric this index uses for search
void searchImpl_(int n, const float *x, int k, float *distances, Index::idx_t *labels) const override
Called from GpuIndex for search.
InvertedLists * invlists
Acess to the actual data.
std::vector< long > getListIndices(int listId) const
Return the list indices of a particular list back to the CPU.
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
IndicesOptions indicesOptions
Index storage options for the GPU.
void reset() override
removes all elements from the database.
size_t code_size
code size per vector in bytes
MetricType
Some algorithms support both an inner product version and a L2 search version.