Skip to content

Commit

Permalink
Add sa_decode() to IndexIVFAdditiveQuantizer (facebookresearch#2362)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: facebookresearch#2362

Reviewed By: mdouze

Differential Revision: D37280450

fbshipit-source-id: 610b553e8219df9a9f52442ffc3942036f47284a
  • Loading branch information
alexanderguzhva authored and facebook-github-bot committed Jun 20, 2022
1 parent 3986ebf commit fb8193d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions faiss/IndexIVFAdditiveQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,32 @@ void IndexIVFAdditiveQuantizer::encode_vectors(
}
}

void IndexIVFAdditiveQuantizer::sa_decode(
idx_t n,
const uint8_t* codes,
float* x) const {
const size_t coarse_size = coarse_code_size();

#pragma omp parallel if (n > 1000)
{
std::vector<float> residual(d);

#pragma omp for
for (idx_t i = 0; i < n; i++) {
const uint8_t* code = codes + i * (code_size + coarse_size);
int64_t list_no = decode_listno(code);
float* xi = x + i * d;
aq->decode(code + coarse_size, xi, 1);
if (by_residual) {
quantizer->reconstruct(list_no, residual.data());
for (size_t j = 0; j < d; j++) {
xi[j] += residual[j];
}
}
}
}
}

IndexIVFAdditiveQuantizer::~IndexIVFAdditiveQuantizer() {}

/*********************************************
Expand Down
2 changes: 2 additions & 0 deletions faiss/IndexIVFAdditiveQuantizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct IndexIVFAdditiveQuantizer : IndexIVF {
InvertedListScanner* get_InvertedListScanner(
bool store_pairs) const override;

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

~IndexIVFAdditiveQuantizer() override;
};

Expand Down
3 changes: 3 additions & 0 deletions tests/test_standalone_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def test_IVFPQ6x8np(self):
def test_LSH(self):
self.do_encode_twice('LSHrt')

def test_RQ6x8(self):
self.do_encode_twice('RQ6x8')


class TestIndexEquiv(unittest.TestCase):

Expand Down

0 comments on commit fb8193d

Please sign in to comment.