From 6a1dd4e52605bb54569315fc4cd4f7979994ae63 Mon Sep 17 00:00:00 2001 From: rohildshah Date: Thu, 13 Mar 2025 17:34:47 -0700 Subject: [PATCH 1/3] add hnsw+metricLp test and metric_arg fix --- faiss/IndexHNSW.cpp | 1 + tests/test_hnsw.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/faiss/IndexHNSW.cpp b/faiss/IndexHNSW.cpp index ed7824ebbb..f0f78efbaf 100644 --- a/faiss/IndexHNSW.cpp +++ b/faiss/IndexHNSW.cpp @@ -332,6 +332,7 @@ void IndexHNSW::add(idx_t n, const float* x) { storage, "Please use IndexHNSWFlat (or variants) instead of IndexHNSW directly"); FAISS_THROW_IF_NOT(is_trained); + storage->metric_arg = metric_arg; int n0 = ntotal; storage->add(n, x); ntotal = storage->ntotal; diff --git a/tests/test_hnsw.cpp b/tests/test_hnsw.cpp index 9424bd3499..4fab55c1a0 100644 --- a/tests/test_hnsw.cpp +++ b/tests/test_hnsw.cpp @@ -193,6 +193,26 @@ TEST(HNSW, Test_popmin_infinite_distances) { } } +TEST(HNSW, Test_IndexHNSWFlat_METRIC_Lp) { + // Create an HNSW index with METRIC_Lp and metric_arg = 3 + faiss::IndexHNSWFlat index(1, 32, faiss::METRIC_Lp); + index.metric_arg = 3; + + // Add a single data point + float data[1] = {0.0}; + index.add(1, data); + + // Prepare a query + float query[1] = {2.0}; + float distance; + faiss::idx_t label; + + index.search(1, query, 1, &distance, &label); + + EXPECT_NEAR(distance, 8.0, 1e-5); // Distance should be 8.0 (2^3) + EXPECT_EQ(label, 0); // Label should be 0 +} + class HNSWTest : public testing::Test { protected: HNSWTest() { From f28224a34ecebf7039ea9abd5ff9dd68a84fa5cd Mon Sep 17 00:00:00 2001 From: rohildshah Date: Fri, 14 Mar 2025 14:39:30 -0700 Subject: [PATCH 2/3] fix formatting --- tests/test_hnsw.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_hnsw.cpp b/tests/test_hnsw.cpp index 4fab55c1a0..30919331ec 100644 --- a/tests/test_hnsw.cpp +++ b/tests/test_hnsw.cpp @@ -210,7 +210,7 @@ TEST(HNSW, Test_IndexHNSWFlat_METRIC_Lp) { index.search(1, query, 1, &distance, &label); EXPECT_NEAR(distance, 8.0, 1e-5); // Distance should be 8.0 (2^3) - EXPECT_EQ(label, 0); // Label should be 0 + EXPECT_EQ(label, 0); // Label should be 0 } class HNSWTest : public testing::Test { From 00dcd2db231ba51d2522732e239bc0677e4769d2 Mon Sep 17 00:00:00 2001 From: rohildshah Date: Mon, 17 Mar 2025 20:31:24 -0700 Subject: [PATCH 3/3] Update IndexHNSW metric_arg initialization location --- faiss/IndexHNSW.cpp | 5 +++-- tests/test_hnsw.cpp | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/faiss/IndexHNSW.cpp b/faiss/IndexHNSW.cpp index f0f78efbaf..5983e9d831 100644 --- a/faiss/IndexHNSW.cpp +++ b/faiss/IndexHNSW.cpp @@ -210,7 +210,9 @@ IndexHNSW::IndexHNSW(int d, int M, MetricType metric) : Index(d, metric), hnsw(M) {} IndexHNSW::IndexHNSW(Index* storage, int M) - : Index(storage->d, storage->metric_type), hnsw(M), storage(storage) {} + : Index(storage->d, storage->metric_type), hnsw(M), storage(storage) { + metric_arg = storage->metric_arg; +} IndexHNSW::~IndexHNSW() { if (own_fields) { @@ -332,7 +334,6 @@ void IndexHNSW::add(idx_t n, const float* x) { storage, "Please use IndexHNSWFlat (or variants) instead of IndexHNSW directly"); FAISS_THROW_IF_NOT(is_trained); - storage->metric_arg = metric_arg; int n0 = ntotal; storage->add(n, x); ntotal = storage->ntotal; diff --git a/tests/test_hnsw.cpp b/tests/test_hnsw.cpp index 30919331ec..9c33c08a9e 100644 --- a/tests/test_hnsw.cpp +++ b/tests/test_hnsw.cpp @@ -193,10 +193,11 @@ TEST(HNSW, Test_popmin_infinite_distances) { } } -TEST(HNSW, Test_IndexHNSWFlat_METRIC_Lp) { +TEST(HNSW, Test_IndexHNSW_METRIC_Lp) { // Create an HNSW index with METRIC_Lp and metric_arg = 3 - faiss::IndexHNSWFlat index(1, 32, faiss::METRIC_Lp); - index.metric_arg = 3; + faiss::IndexFlat storage_index(1, faiss::METRIC_Lp); + storage_index.metric_arg = 3; + faiss::IndexHNSW index(&storage_index, 32); // Add a single data point float data[1] = {0.0};