Add index binary to telemetry (#4001)

Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/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
This commit is contained in:
Michael Norris
2024-10-29 09:58:43 -07:00
committed by Facebook GitHub Bot
parent ecb4b802e9
commit 9766d64dde
2 changed files with 41 additions and 1 deletions
+37 -1
View File
@@ -25,7 +25,15 @@ const std::map<faiss::ScalarQuantizer::QuantizerType, std::string> sq_types = {
};
int get_hnsw_M(const faiss::IndexHNSW* index) {
if (index->hnsw.cum_nneighbor_per_level.size() >= 1) {
if (index->hnsw.cum_nneighbor_per_level.size() > 1) {
return index->hnsw.cum_nneighbor_per_level[1] / 2;
}
// Avoid runtime error, just return 0.
return 0;
}
int get_hnsw_M(const faiss::IndexBinaryHNSW* index) {
if (index->hnsw.cum_nneighbor_per_level.size() > 1) {
return index->hnsw.cum_nneighbor_per_level[1] / 2;
}
// Avoid runtime error, just return 0.
@@ -153,4 +161,32 @@ std::string reverse_index_factory(const faiss::Index* index) {
return "";
}
std::string reverse_index_factory(const faiss::IndexBinary* index) {
std::string prefix;
if (dynamic_cast<const faiss::IndexBinaryFlat*>(index)) {
return "BFlat";
} else if (
const faiss::IndexBinaryIVF* ivf_index =
dynamic_cast<const faiss::IndexBinaryIVF*>(index)) {
const faiss::IndexBinary* quantizer = ivf_index->quantizer;
if (dynamic_cast<const faiss::IndexBinaryFlat*>(quantizer)) {
return "BIVF" + std::to_string(ivf_index->nlist);
} else if (
const faiss::IndexBinaryHNSW* hnsw_index =
dynamic_cast<const faiss::IndexBinaryHNSW*>(
quantizer)) {
return "BIVF" + std::to_string(ivf_index->nlist) + "_HNSW" +
std::to_string(get_hnsw_M(hnsw_index));
}
// Add further cases for BinaryIVF here.
} else if (
const faiss::IndexBinaryHNSW* hnsw_index =
dynamic_cast<const faiss::IndexBinaryHNSW*>(index)) {
return "BHNSW" + std::to_string(get_hnsw_M(hnsw_index));
}
// Avoid runtime error, just return empty string for logging.
return "";
}
} // namespace faiss
+4
View File
@@ -9,6 +9,9 @@
#pragma once
#include <faiss/IndexBinaryFlat.h>
#include <faiss/IndexBinaryHNSW.h>
#include <faiss/IndexBinaryIVF.h>
#include <faiss/IndexHNSW.h>
#include <faiss/IndexIDMap.h>
#include <faiss/IndexIVFFlat.h>
@@ -21,5 +24,6 @@
namespace faiss {
std::string reverse_index_factory(const faiss::Index* index);
std::string reverse_index_factory(const faiss::IndexBinary* index);
} // namespace faiss