Skip to content

Commit 9766d64

Browse files
Michael Norrisfacebook-github-bot
Michael Norris
authored andcommitted
Add index binary to telemetry (facebookresearch#4001)
Summary: Pull Request resolved: facebookresearch#4001 It was mentioned in S461104 chat to cover all index types. This adds Binary for telemetry as well as the reverse factory string for binary indexes, which we did not support before. Unit test covers 4 ways of reading a binary index. The reverse factory string util is not complete. The remaining binary index types could get added later. Reviewed By: asadoughi Differential Revision: D65102643 fbshipit-source-id: 52f1053bda59e427a081369ada80265b67e55bd4
1 parent ecb4b80 commit 9766d64

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

faiss/cppcontrib/factory_tools.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ const std::map<faiss::ScalarQuantizer::QuantizerType, std::string> sq_types = {
2525
};
2626

2727
int get_hnsw_M(const faiss::IndexHNSW* index) {
28-
if (index->hnsw.cum_nneighbor_per_level.size() >= 1) {
28+
if (index->hnsw.cum_nneighbor_per_level.size() > 1) {
29+
return index->hnsw.cum_nneighbor_per_level[1] / 2;
30+
}
31+
// Avoid runtime error, just return 0.
32+
return 0;
33+
}
34+
35+
int get_hnsw_M(const faiss::IndexBinaryHNSW* index) {
36+
if (index->hnsw.cum_nneighbor_per_level.size() > 1) {
2937
return index->hnsw.cum_nneighbor_per_level[1] / 2;
3038
}
3139
// Avoid runtime error, just return 0.
@@ -153,4 +161,32 @@ std::string reverse_index_factory(const faiss::Index* index) {
153161
return "";
154162
}
155163

164+
std::string reverse_index_factory(const faiss::IndexBinary* index) {
165+
std::string prefix;
166+
if (dynamic_cast<const faiss::IndexBinaryFlat*>(index)) {
167+
return "BFlat";
168+
} else if (
169+
const faiss::IndexBinaryIVF* ivf_index =
170+
dynamic_cast<const faiss::IndexBinaryIVF*>(index)) {
171+
const faiss::IndexBinary* quantizer = ivf_index->quantizer;
172+
173+
if (dynamic_cast<const faiss::IndexBinaryFlat*>(quantizer)) {
174+
return "BIVF" + std::to_string(ivf_index->nlist);
175+
} else if (
176+
const faiss::IndexBinaryHNSW* hnsw_index =
177+
dynamic_cast<const faiss::IndexBinaryHNSW*>(
178+
quantizer)) {
179+
return "BIVF" + std::to_string(ivf_index->nlist) + "_HNSW" +
180+
std::to_string(get_hnsw_M(hnsw_index));
181+
}
182+
// Add further cases for BinaryIVF here.
183+
} else if (
184+
const faiss::IndexBinaryHNSW* hnsw_index =
185+
dynamic_cast<const faiss::IndexBinaryHNSW*>(index)) {
186+
return "BHNSW" + std::to_string(get_hnsw_M(hnsw_index));
187+
}
188+
// Avoid runtime error, just return empty string for logging.
189+
return "";
190+
}
191+
156192
} // namespace faiss

faiss/cppcontrib/factory_tools.h

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#pragma once
1111

12+
#include <faiss/IndexBinaryFlat.h>
13+
#include <faiss/IndexBinaryHNSW.h>
14+
#include <faiss/IndexBinaryIVF.h>
1215
#include <faiss/IndexHNSW.h>
1316
#include <faiss/IndexIDMap.h>
1417
#include <faiss/IndexIVFFlat.h>
@@ -21,5 +24,6 @@
2124
namespace faiss {
2225

2326
std::string reverse_index_factory(const faiss::Index* index);
27+
std::string reverse_index_factory(const faiss::IndexBinary* index);
2428

2529
} // namespace faiss

0 commit comments

Comments
 (0)