Skip to content

Commit f3c05bd

Browse files
pankajsingh88facebook-github-bot
authored andcommitted
add reconstruct support to additive quantizers (#3752)
Summary: Pull Request resolved: #3752 add reconstruct support to additive quantizers. fixes github issue: 2422 Reviewed By: asadoughi Differential Revision: D61255049 fbshipit-source-id: 09a0edae7fc24295a686d332e2d052e37372d2c0
1 parent 6e6685b commit f3c05bd

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

faiss/IndexIVFAdditiveQuantizer.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ void IndexIVFAdditiveQuantizer::sa_decode(
116116
}
117117
}
118118

119+
void IndexIVFAdditiveQuantizer::reconstruct_from_offset(
120+
int64_t list_no,
121+
int64_t offset,
122+
float* recons) const {
123+
const uint8_t* code = invlists->get_single_code(list_no, offset);
124+
aq->decode(code, recons, 1);
125+
if (by_residual) {
126+
std::vector<float> centroid(d);
127+
quantizer->reconstruct(list_no, centroid.data());
128+
for (int i = 0; i < d; ++i) {
129+
recons[i] += centroid[i];
130+
}
131+
}
132+
}
133+
119134
IndexIVFAdditiveQuantizer::~IndexIVFAdditiveQuantizer() = default;
120135

121136
/*********************************************

faiss/IndexIVFAdditiveQuantizer.h

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ struct IndexIVFAdditiveQuantizer : IndexIVF {
5656

5757
void sa_decode(idx_t n, const uint8_t* codes, float* x) const override;
5858

59+
void reconstruct_from_offset(int64_t list_no, int64_t offset, float* recons)
60+
const override;
61+
5962
~IndexIVFAdditiveQuantizer() override;
6063
};
6164

tests/test_index.py

+17
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,23 @@ def test_IndexIVFPQ(self):
493493

494494
self.run_search_and_reconstruct(index, xb, xq, eps=1.0)
495495

496+
def test_IndexIVFRQ(self):
497+
d = 32
498+
nb = 1000
499+
nt = 1500
500+
nq = 200
501+
502+
(xt, xb, xq) = get_dataset(d, nb, nt, nq)
503+
504+
quantizer = faiss.IndexFlatL2(d)
505+
index = faiss.IndexIVFResidualQuantizer(quantizer, d, 32, 8, 8)
506+
index.cp.min_points_per_centroid = 5 # quiet warning
507+
index.nprobe = 4
508+
index.train(xt)
509+
index.add(xb)
510+
511+
self.run_search_and_reconstruct(index, xb, xq, eps=1.0)
512+
496513
def test_MultiIndex(self):
497514
d = 32
498515
nb = 1000

0 commit comments

Comments
 (0)