11 #include "GpuIndexIVFFlat.h"
12 #include "../IndexFlat.h"
13 #include "../IndexIVF.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;
104 FAISS_ASSERT(index->
vecs.size() == index->
ids.size());
105 for (
size_t i = 0; i < index->
vecs.size(); ++i) {
106 auto& vecs = index->
vecs[i];
107 auto& ids = index->
ids[i];
109 FAISS_ASSERT(vecs.size() % this->
d == 0);
110 auto numVecs = vecs.size() / this->
d;
111 FAISS_ASSERT(numVecs == ids.size());
114 FAISS_THROW_IF_NOT_FMT(ids.size() <=
115 (size_t) std::numeric_limits<int>::max(),
116 "GPU inverted list can only support "
117 "%zu entries; %zu found",
118 (size_t) std::numeric_limits<int>::max(),
130 FAISS_THROW_IF_NOT_MSG(ivfFlatConfig_.
indicesOptions != INDICES_IVF,
131 "Cannot copy to CPU as GPU index doesn't retain "
132 "indices (INDICES_IVF)");
142 for (
int i = 0; i <
nlist_; ++i) {
168 FAISS_ASSERT(this->
ntotal == 0);
179 FAISS_ASSERT(index_);
183 FAISS_ASSERT(!index_);
185 trainQuantizer_(n, x);
195 if (reserveMemoryVecs_) {
207 FAISS_ASSERT(index_);
215 const_cast<float*
>(x),
217 {(int) n, index_->
getDim()});
219 static_assert(
sizeof(
long) ==
sizeof(
Index::idx_t),
"size mismatch");
223 const_cast<long*
>(xids),
239 FAISS_ASSERT(index_);
242 auto stream = resources_->getDefaultStream(device_);
249 const_cast<float*
>(x),
265 index_->
query(devX,
nprobe_, k, devDistances, devLabels);
268 fromDevice<float, 2>(devDistances, distances, stream);
269 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.
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
std::vector< std::vector< long > > ids
Inverted lists for indexes.
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
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.
MetricType
Some algorithms support both an inner product vetsion and a L2 search version.
std::vector< std::vector< float > > vecs