From 8b5895ff79896f5a644ed55f87d6f31a4a821012 Mon Sep 17 00:00:00 2001 From: Bhavik Sheth Date: Mon, 22 Jul 2024 16:01:35 -0700 Subject: [PATCH] 1720 - expose FAISS version field to c_api (#3635) Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/3635 Add a util function to return the version in the c api. Reviewed By: ramilbakhshyiev, fxdawnn Differential Revision: D59817407 fbshipit-source-id: ca805f8e04f554d0294ba9da8ec6dc7c31e91fe3 --- faiss/Index.h | 25 ++++++++++++++++++++----- faiss/utils/utils.cpp | 5 +++++ faiss/utils/utils.h | 3 +++ tests/CMakeLists.txt | 1 + tests/test_utils.cpp | 19 +++++++++++++++++++ 5 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tests/test_utils.cpp diff --git a/faiss/Index.h b/faiss/Index.h index 3d1bdb996..f57140ec4 100644 --- a/faiss/Index.h +++ b/faiss/Index.h @@ -20,6 +20,18 @@ #define FAISS_VERSION_MINOR 8 #define FAISS_VERSION_PATCH 0 +// Macro to combine the version components into a single string +#ifndef FAISS_STRINGIFY +#define FAISS_STRINGIFY(ARG) #ARG +#endif +#ifndef FAISS_TOSTRING +#define FAISS_TOSTRING(ARG) FAISS_STRINGIFY(ARG) +#endif +#define VERSION_STRING \ + FAISS_TOSTRING(FAISS_VERSION_MAJOR) \ + "." FAISS_TOSTRING(FAISS_VERSION_MINOR) "." FAISS_TOSTRING( \ + FAISS_VERSION_PATCH) + /** * @namespace faiss * @@ -38,8 +50,8 @@ namespace faiss { -/// Forward declarations see impl/AuxIndexStructures.h, impl/IDSelector.h and -/// impl/DistanceComputer.h +/// Forward declarations see impl/AuxIndexStructures.h, impl/IDSelector.h +/// and impl/DistanceComputer.h struct IDSelector; struct RangeSearchResult; struct DistanceComputer; @@ -56,7 +68,8 @@ struct SearchParameters { virtual ~SearchParameters() {} }; -/** Abstract structure for an index, supports adding vectors and searching them. +/** Abstract structure for an index, supports adding vectors and searching + * them. * * All vectors provided at add or search time are 32-bit float arrays, * although the internal representation may vary. @@ -154,7 +167,8 @@ struct Index { /** return the indexes of the k vectors closest to the query x. * - * This function is identical as search but only return labels of neighbors. + * This function is identical as search but only return labels of + * neighbors. * @param n number of vectors * @param x input vectors to search, size n * d * @param labels output labels of the NNs, size n*k @@ -179,7 +193,8 @@ struct Index { */ virtual void reconstruct(idx_t key, float* recons) const; - /** Reconstruct several stored vectors (or an approximation if lossy coding) + /** Reconstruct several stored vectors (or an approximation if lossy + * coding) * * this function may not be defined for some indexes * @param n number of vectors to reconstruct diff --git a/faiss/utils/utils.cpp b/faiss/utils/utils.cpp index dc6faddaf..bcb3c280b 100644 --- a/faiss/utils/utils.cpp +++ b/faiss/utils/utils.cpp @@ -7,6 +7,7 @@ // -*- c++ -*- +#include #include #include @@ -129,6 +130,10 @@ std::string get_compile_options() { return options; } +std::string get_version() { + return VERSION_STRING; +} + #ifdef _MSC_VER double getmillisecs() { LARGE_INTEGER ts; diff --git a/faiss/utils/utils.h b/faiss/utils/utils.h index e75207338..4d10a8c0f 100644 --- a/faiss/utils/utils.h +++ b/faiss/utils/utils.h @@ -37,6 +37,9 @@ std::string get_compile_options(); * Get some stats about the system **************************************************/ +// Expose FAISS version as a string +std::string get_version(); + /// ms elapsed since some arbitrary epoch double getmillisecs(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3980d7dd7..23d076a55 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -35,6 +35,7 @@ set(FAISS_TEST_SRC test_disable_pq_sdc_tables.cpp test_common_ivf_empty_index.cpp test_callback.cpp + test_utils.cpp ) add_executable(faiss_test ${FAISS_TEST_SRC}) diff --git a/tests/test_utils.cpp b/tests/test_utils.cpp new file mode 100644 index 000000000..793e29071 --- /dev/null +++ b/tests/test_utils.cpp @@ -0,0 +1,19 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include + +#include +#include + +TEST(TestUtils, get_version) { + std::string version = std::to_string(FAISS_VERSION_MAJOR) + "." + + std::to_string(FAISS_VERSION_MINOR) + "." + + std::to_string(FAISS_VERSION_PATCH); + + EXPECT_EQ(version, faiss::get_version()); +}