11 #include "GpuIndexIVFFlat.h"
12 #include "../IndexFlat.h"
13 #include "../IndexIVFFlat.h"
14 #include "GpuIndexFlat.h"
15 #include "GpuResources.h"
16 #include "impl/IVFFlat.cuh"
17 #include "utils/CopyUtils.cuh"
18 #include "utils/DeviceUtils.h"
19 #include "utils/Float16.cuh"
23 namespace faiss {
namespace gpu {
33 ivfFlatConfig_(config),
34 reserveMemoryVecs_(0),
36 #ifndef FAISS_USE_FLOAT16
38 "float16 unsupported; need CUDA SDK >= 7.5");
49 GpuIndexIVF(resources, dims, metric, nlist, config),
50 ivfFlatConfig_(config),
51 reserveMemoryVecs_(0),
57 #ifndef FAISS_USE_FLOAT16
59 "float16 unsupported; need CUDA SDK >= 7.5");
66 GpuIndexIVFFlat::~GpuIndexIVFFlat() {
72 reserveMemoryVecs_ = numVecs;
105 for (
size_t i = 0; i < ivf->nlist; ++i) {
109 FAISS_THROW_IF_NOT_FMT(numVecs <=
110 (
size_t) std::numeric_limits<int>::max(),
111 "GPU inverted list can only support "
112 "%zu entries; %zu found",
113 (
size_t) std::numeric_limits<int>::max(),
117 i, (
const float*)(ivf->get_codes(i)),
118 ivf->get_ids(i), numVecs);
127 FAISS_THROW_IF_NOT_MSG(ivfFlatConfig_.
indicesOptions != INDICES_IVF,
128 "Cannot copy to CPU as GPU index doesn't retain "
129 "indices (INDICES_IVF)");
137 index->replace_invlists(ivf,
true);
141 for (
int i = 0; i <
nlist_; ++i) {
169 FAISS_ASSERT(this->
ntotal == 0);
180 FAISS_ASSERT(index_);
184 FAISS_ASSERT(!index_);
186 trainQuantizer_(n, x);
196 if (reserveMemoryVecs_) {
208 FAISS_ASSERT(index_);
216 const_cast<float*
>(x),
218 {(int) n, index_->
getDim()});
220 static_assert(
sizeof(
long) ==
sizeof(
Index::idx_t),
"size mismatch");
224 const_cast<long*
>(xids),
240 FAISS_ASSERT(index_);
243 auto stream = resources_->getDefaultStream(device_);
250 const_cast<float*
>(x),
266 index_->
query(devX,
nprobe_, k, devDistances, devLabels);
269 fromDevice<float, 2>(devDistances, distances, stream);
270 fromDevice<faiss::Index::idx_t, 2>(devLabels, labels, stream);
GpuIndexIVFFlat(GpuResources *resources, const faiss::IndexIVFFlat *index, GpuIndexIVFFlatConfig config=GpuIndexIVFFlatConfig())
cudaStream_t getDefaultStreamCurrentDevice()
Calls getDefaultStream with the current device.
void searchImpl_(faiss::Index::idx_t n, const float *x, faiss::Index::idx_t k, float *distances, faiss::Index::idx_t *labels) const override
Called from GpuIndex for search.
int getDim() const
Return the number of dimensions we are indexing.
FlatIndex * getGpuData()
For internal access.
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 addImpl_(faiss::Index::idx_t n, const float *x, const faiss::Index::idx_t *ids) override
Called from GpuIndex for add/add_with_ids.
void train(Index::idx_t n, const float *x) override
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 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
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
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.