Skip to content

Commit

Permalink
Add index binary to telemetry (#4001)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
Michael Norris authored and facebook-github-bot committed Oct 29, 2024
1 parent ecb4b80 commit 9766d64
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
38 changes: 37 additions & 1 deletion faiss/cppcontrib/factory_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 changes: 4 additions & 0 deletions faiss/cppcontrib/factory_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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

0 comments on commit 9766d64

Please sign in to comment.