|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <faiss/IndexIVFIndependentQuantizer.h> |
| 9 | +#include <faiss/IndexIVFPQ.h> |
| 10 | +#include <faiss/impl/FaissAssert.h> |
| 11 | +#include <faiss/utils/utils.h> |
| 12 | + |
| 13 | +namespace faiss { |
| 14 | + |
| 15 | +IndexIVFIndependentQuantizer::IndexIVFIndependentQuantizer( |
| 16 | + Index* quantizer, |
| 17 | + IndexIVF* index_ivf, |
| 18 | + VectorTransform* vt) |
| 19 | + : Index(quantizer->d, index_ivf->metric_type), |
| 20 | + quantizer(quantizer), |
| 21 | + vt(vt), |
| 22 | + index_ivf(index_ivf) { |
| 23 | + if (vt) { |
| 24 | + FAISS_THROW_IF_NOT_MSG( |
| 25 | + vt->d_in == d && vt->d_out == index_ivf->d, |
| 26 | + "invalid vector dimensions"); |
| 27 | + } else { |
| 28 | + FAISS_THROW_IF_NOT_MSG(index_ivf->d == d, "invalid vector dimensions"); |
| 29 | + } |
| 30 | + |
| 31 | + if (quantizer->is_trained && quantizer->ntotal != 0) { |
| 32 | + FAISS_THROW_IF_NOT(quantizer->ntotal == index_ivf->nlist); |
| 33 | + } |
| 34 | + if (index_ivf->is_trained && vt) { |
| 35 | + FAISS_THROW_IF_NOT(vt->is_trained); |
| 36 | + } |
| 37 | + ntotal = index_ivf->ntotal; |
| 38 | + is_trained = |
| 39 | + (quantizer->is_trained && quantizer->ntotal == index_ivf->nlist && |
| 40 | + (!vt || vt->is_trained) && index_ivf->is_trained); |
| 41 | + |
| 42 | + // disable precomputed tables because they use the distances that are |
| 43 | + // provided by the coarse quantizer (that are out of sync with the IVFPQ) |
| 44 | + if (auto index_ivfpq = dynamic_cast<IndexIVFPQ*>(index_ivf)) { |
| 45 | + index_ivfpq->use_precomputed_table = -1; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +IndexIVFIndependentQuantizer::~IndexIVFIndependentQuantizer() { |
| 50 | + if (own_fields) { |
| 51 | + delete quantizer; |
| 52 | + delete index_ivf; |
| 53 | + delete vt; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +namespace { |
| 58 | + |
| 59 | +struct VTransformedVectors : TransformedVectors { |
| 60 | + VTransformedVectors(const VectorTransform* vt, idx_t n, const float* x) |
| 61 | + : TransformedVectors(x, vt ? vt->apply(n, x) : x) {} |
| 62 | +}; |
| 63 | + |
| 64 | +struct SubsampledVectors : TransformedVectors { |
| 65 | + SubsampledVectors(int d, idx_t* n, idx_t max_n, const float* x) |
| 66 | + : TransformedVectors( |
| 67 | + x, |
| 68 | + fvecs_maybe_subsample(d, (size_t*)n, max_n, x, true)) {} |
| 69 | +}; |
| 70 | + |
| 71 | +} // anonymous namespace |
| 72 | + |
| 73 | +void IndexIVFIndependentQuantizer::add(idx_t n, const float* x) { |
| 74 | + std::vector<float> D(n); |
| 75 | + std::vector<idx_t> I(n); |
| 76 | + quantizer->search(n, x, 1, D.data(), I.data()); |
| 77 | + |
| 78 | + VTransformedVectors tv(vt, n, x); |
| 79 | + |
| 80 | + index_ivf->add_core(n, tv.x, nullptr, I.data()); |
| 81 | +} |
| 82 | + |
| 83 | +void IndexIVFIndependentQuantizer::search( |
| 84 | + idx_t n, |
| 85 | + const float* x, |
| 86 | + idx_t k, |
| 87 | + float* distances, |
| 88 | + idx_t* labels, |
| 89 | + const SearchParameters* params) const { |
| 90 | + FAISS_THROW_IF_NOT_MSG(!params, "search parameters not supported"); |
| 91 | + int nprobe = index_ivf->nprobe; |
| 92 | + std::vector<float> D(n * nprobe); |
| 93 | + std::vector<idx_t> I(n * nprobe); |
| 94 | + quantizer->search(n, x, nprobe, D.data(), I.data()); |
| 95 | + |
| 96 | + VTransformedVectors tv(vt, n, x); |
| 97 | + |
| 98 | + index_ivf->search_preassigned( |
| 99 | + n, tv.x, k, I.data(), D.data(), distances, labels, false); |
| 100 | +} |
| 101 | + |
| 102 | +void IndexIVFIndependentQuantizer::reset() { |
| 103 | + index_ivf->reset(); |
| 104 | + ntotal = 0; |
| 105 | +} |
| 106 | + |
| 107 | +void IndexIVFIndependentQuantizer::train(idx_t n, const float* x) { |
| 108 | + // quantizer training |
| 109 | + size_t nlist = index_ivf->nlist; |
| 110 | + Level1Quantizer l1(quantizer, nlist); |
| 111 | + l1.train_q1(n, x, verbose, metric_type); |
| 112 | + |
| 113 | + // train the VectorTransform |
| 114 | + if (vt && !vt->is_trained) { |
| 115 | + if (verbose) { |
| 116 | + printf("IndexIVFIndependentQuantizer: train the VectorTransform\n"); |
| 117 | + } |
| 118 | + vt->train(n, x); |
| 119 | + } |
| 120 | + |
| 121 | + // get the centroids from the quantizer, transform them and |
| 122 | + // add them to the index_ivf's quantizer |
| 123 | + if (verbose) { |
| 124 | + printf("IndexIVFIndependentQuantizer: extract the main quantizer centroids\n"); |
| 125 | + } |
| 126 | + std::vector<float> centroids(nlist * d); |
| 127 | + quantizer->reconstruct_n(0, nlist, centroids.data()); |
| 128 | + VTransformedVectors tcent(vt, nlist, centroids.data()); |
| 129 | + |
| 130 | + if (verbose) { |
| 131 | + printf("IndexIVFIndependentQuantizer: add centroids to the secondary quantizer\n"); |
| 132 | + } |
| 133 | + if (!index_ivf->quantizer->is_trained) { |
| 134 | + index_ivf->quantizer->train(nlist, tcent.x); |
| 135 | + } |
| 136 | + index_ivf->quantizer->add(nlist, tcent.x); |
| 137 | + |
| 138 | + // train the payload |
| 139 | + |
| 140 | + // optional subsampling |
| 141 | + idx_t max_nt = index_ivf->train_encoder_num_vectors(); |
| 142 | + if (max_nt <= 0) { |
| 143 | + max_nt = (size_t)1 << 35; |
| 144 | + } |
| 145 | + SubsampledVectors sv(index_ivf->d, &n, max_nt, x); |
| 146 | + |
| 147 | + // transform subsampled vectors |
| 148 | + VTransformedVectors tv(vt, n, sv.x); |
| 149 | + |
| 150 | + if (verbose) { |
| 151 | + printf("IndexIVFIndependentQuantizer: train encoder\n"); |
| 152 | + } |
| 153 | + |
| 154 | + if (index_ivf->by_residual) { |
| 155 | + // assign with quantizer |
| 156 | + std::vector<idx_t> assign(n); |
| 157 | + quantizer->assign(n, sv.x, assign.data()); |
| 158 | + |
| 159 | + // compute residual with IVF quantizer |
| 160 | + std::vector<float> residuals(n * index_ivf->d); |
| 161 | + index_ivf->quantizer->compute_residual_n( |
| 162 | + n, tv.x, residuals.data(), assign.data()); |
| 163 | + |
| 164 | + index_ivf->train_encoder(n, residuals.data(), assign.data()); |
| 165 | + } else { |
| 166 | + index_ivf->train_encoder(n, tv.x, nullptr); |
| 167 | + } |
| 168 | + index_ivf->is_trained = true; |
| 169 | + is_trained = true; |
| 170 | +} |
| 171 | + |
| 172 | +} // namespace faiss |
0 commit comments