11 #include "GpuIndexIVFPQ.h"
12 #include "../IndexFlat.h"
13 #include "../IndexIVFPQ.h"
14 #include "../ProductQuantizer.h"
15 #include "GpuIndexFlat.h"
16 #include "GpuResources.h"
17 #include "impl/IVFPQ.cuh"
18 #include "utils/CopyUtils.cuh"
19 #include "utils/DeviceUtils.h"
23 namespace faiss {
namespace gpu {
36 reserveMemoryVecs_(0),
38 #ifndef FAISS_USE_FLOAT16
58 subQuantizers_(subQuantizers),
59 bitsPerCode_(bitsPerCode),
60 reserveMemoryVecs_(0),
62 #ifndef FAISS_USE_FLOAT16
69 FAISS_ASSERT(this->
metric_type == faiss::METRIC_L2);
75 GpuIndexIVFPQ::~GpuIndexIVFPQ() {
84 FAISS_THROW_IF_NOT_MSG(index->
metric_type == faiss::METRIC_L2,
85 "inner product unsupported");
92 subQuantizers_ = index->
pq.
M;
126 size_t nlist = ivf ? ivf->
nlist : 0;
127 for (
size_t i = 0; i < nlist; ++i) {
131 FAISS_THROW_IF_NOT_FMT(list_size <=
132 (
size_t) std::numeric_limits<int>::max(),
133 "GPU inverted list can only support "
134 "%zu entries; %zu found",
135 (
size_t) std::numeric_limits<int>::max(),
138 index_->addCodeVectorsFromCpu(
148 FAISS_THROW_IF_NOT_MSG(ivfpqConfig_.
indicesOptions != INDICES_IVF,
149 "Cannot copy to CPU as GPU index doesn't retain "
150 "indices (INDICES_IVF)");
173 index->replace_invlists(ivf,
true);
177 for (
int i = 0; i <
nlist_; ++i) {
180 index->
invlists->add_entries (i, ids.size(), ids.data(), codes.data());
185 index->
pq.
centroids.resize(devPQCentroids.numElements());
187 fromDevice<float, 3>(devPQCentroids,
199 reserveMemoryVecs_ = numVecs;
224 return subQuantizers_;
234 return utils::pow2(bitsPerCode_);
255 FAISS_ASSERT(this->
ntotal == 0);
260 GpuIndexIVFPQ::trainResidualQuantizer_(
Index::idx_t n,
const float* x) {
263 n = std::min(n, (
Index::idx_t) (1 << bitsPerCode_) * 64);
266 printf(
"computing residuals\n");
269 std::vector<Index::idx_t>
assign(n);
272 std::vector<float> residuals(n *
d);
274 for (idx_t i = 0; i < n; i++) {
279 printf(
"training %d x %d product quantizer on %ld vectors in %dD\n",
286 pq.train(n, residuals.data());
296 if (reserveMemoryVecs_) {
310 FAISS_ASSERT(index_);
314 FAISS_ASSERT(!index_);
316 trainQuantizer_(n, x);
317 trainResidualQuantizer_(n, x);
327 FAISS_ASSERT(index_);
335 const_cast<float*
>(x),
337 {(int) n, index_->
getDim()});
359 FAISS_ASSERT(index_);
367 const_cast<float*
>(x),
368 resources_->getDefaultStream(device_),
369 {(int) n, index_->
getDim()});
374 resources_->getDefaultStream(device_),
380 resources_->getDefaultStream(device_),
390 fromDevice<float, 2>(
391 devDistances, distances, resources_->getDefaultStream(device_));
392 fromDevice<faiss::Index::idx_t, 2>(
393 devLabels, labels, resources_->getDefaultStream(device_));
398 FAISS_ASSERT(index_);
402 std::vector<unsigned char>
404 FAISS_ASSERT(index_);
412 FAISS_ASSERT(index_);
419 GpuIndexIVFPQ::verifySettings_()
const {
423 FAISS_THROW_IF_NOT_MSG(
nlist_ > 0,
"nlist must be >0");
426 FAISS_THROW_IF_NOT_FMT(bitsPerCode_ <= 8,
427 "Bits per code must be <= 8 (passed %d)", bitsPerCode_);
430 FAISS_THROW_IF_NOT_FMT(this->
d % subQuantizers_ == 0,
431 "Number of sub-quantizers (%d) must be an "
432 "even divisor of the number of dimensions (%d)",
433 subQuantizers_, this->
d);
437 "Number of bytes per encoded vector / sub-quantizers (%d) "
443 int lookupTableSize =
sizeof(float);
444 #ifdef FAISS_USE_FLOAT16
446 lookupTableSize =
sizeof(half);
452 size_t requiredSmemSize =
453 lookupTableSize * subQuantizers_ * utils::pow2(bitsPerCode_);
454 size_t smemPerBlock = getMaxSharedMemPerBlock(device_);
456 FAISS_THROW_IF_NOT_FMT(requiredSmemSize
457 <= getMaxSharedMemPerBlock(device_),
458 "Device %d has %zu bytes of shared memory, while "
459 "%d bits per code and %d sub-quantizers requires %zu "
460 "bytes. Consider useFloat16LookupTables and/or "
462 device_, smemPerBlock, bitsPerCode_, subQuantizers_,
469 this->d / subQuantizers_),
470 "Number of dimensions per sub-quantizer (%d) "
471 "is not currently supported without precomputed codes. "
472 "Only 1, 2, 3, 4, 6, 8, 10, 12, 16, 20, 24, 28, 32 dims "
473 "per sub-quantizer are currently supported with no "
474 "precomputed codes. "
475 "Precomputed codes supports any number of dimensions, but "
476 "will involve memory overheads.",
477 this->d / subQuantizers_);
480 FAISS_THROW_IF_NOT_MSG(this->
metric_type == faiss::METRIC_L2,
481 "METRIC_INNER_PRODUCT is currently unsupported");
std::vector< long > getListIndices(int listId) const
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.
void precompute_table()
build precomputed table
size_t nbits
number of bits per quantization index
cudaStream_t getDefaultStreamCurrentDevice()
Calls getDefaultStream with the current device.
bool usePrecomputedTables
PolysemousTraining * polysemous_training
if NULL, use default
size_t byte_per_idx
nb bytes per code component (1 or 2)
virtual const idx_t * get_ids(size_t list_no) const =0
GpuIndexIVFPQ(GpuResources *resources, const faiss::IndexIVFPQ *index, GpuIndexIVFPQConfig config=GpuIndexIVFPQConfig())
int getDim() const
Return the number of dimensions we are indexing.
int getListLength(int listId) const
FlatIndex * getGpuData()
For internal access.
void assign(idx_t n, const float *x, idx_t *labels, idx_t k=1)
virtual size_t list_size(size_t list_no) const =0
get the size of a list
void reserveMemory(size_t numVecs)
Reserve GPU memory in our inverted lists for this number of vectors.
int getListLength(int listId) const
bool do_polysemous_training
reorder PQ centroids after training?
size_t scan_table_threshold
use table computation or on-the-fly?
std::vector< float > precomputed_table
int polysemous_ht
Hamming thresh for polysemous filtering.
int getBitsPerCode() const
Return the number of bits per PQ code.
bool useFloat16LookupTables
virtual cudaStream_t getDefaultStream(int device)=0
void train(Index::idx_t n, const float *x) override
static bool isSupportedPQCodeLength(int size)
Returns true if we support PQ in this size.
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.
int classifyAndAddVectors(Tensor< float, 2, true > &vecs, Tensor< long, 1, true > &indices)
void query(Tensor< float, 2, true > &queries, int nprobe, int k, Tensor< float, 2, true > &outDistances, Tensor< long, 2, true > &outIndices)
const int device_
The GPU device we are resident on.
void copyFrom(const faiss::IndexIVFPQ *index)
Tensor< float, 3, true > getPQCentroids()
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.
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.
idx_t ntotal
total nb of indexed vectors
bool verbose
verbosity level
void setPrecomputedCodes(bool enable)
Enable or disable pre-computed codes.
std::vector< unsigned char > getListCodes(int listId) const
Return the list codes of a particular list back to the CPU.
void copyTo(faiss::IndexIVFPQ *index) const
size_t nlist
number of possible key values
const MemorySpace memorySpace_
The memory space of our primary storage on the GPU.
bool by_residual
Encode residual or plain vector?
GpuIndexFlat * quantizer_
Quantizer for inverted lists.
MetricType metric_type
type of metric this index uses for search
ProductQuantizer pq
produces the codes
InvertedLists * invlists
Acess to the actual data.
size_t M
number of subquantizers
int getNumSubQuantizers() const
Return the number of sub-quantizers we are using.
std::vector< long > getListIndices(int listId) const
Return the list indices of a particular list back to the CPU.
int getCentroidsPerSubQuantizer() const
Return the number of centroids per PQ code (2^bits per code)
void setPrecomputedCodes(bool enable)
Enable or disable pre-computed codes.
virtual const uint8_t * get_codes(size_t list_no) const =0
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 compute_residual(const float *x, float *residual, idx_t key) const
size_t max_codes
max nb of codes to visit to do a query
bool getPrecomputedCodes() const
Are pre-computed codes enabled?
IndicesOptions indicesOptions
Index storage options for the GPU.
Implementing class for IVFPQ on the GPU.
size_t code_size
code size per vector in bytes
MetricType
Some algorithms support both an inner product version and a L2 search version.
int use_precomputed_table
if by_residual, build precompute tables
std::vector< unsigned char > getListCodes(int listId) const
std::vector< float > centroids
Centroid table, size M * ksub * dsub.
static bool isSupportedNoPrecomputedSubDimSize(int dims)