diff --git a/faiss/IndexHNSW.cpp b/faiss/IndexHNSW.cpp index c0bb81c052..e18e203208 100644 --- a/faiss/IndexHNSW.cpp +++ b/faiss/IndexHNSW.cpp @@ -35,26 +35,6 @@ #include #include -extern "C" { - -/* declare BLAS functions, see http://www.netlib.org/clapack/cblas/ */ - -int sgemm_( - const char* transa, - const char* transb, - FINTEGER* m, - FINTEGER* n, - FINTEGER* k, - const float* alpha, - const float* a, - FINTEGER* lda, - const float* b, - FINTEGER* ldb, - float* beta, - float* c, - FINTEGER* ldc); -} - namespace faiss { using MinimaxHeap = HNSW::MinimaxHeap; @@ -340,7 +320,7 @@ void IndexHNSW::range_search( RangeSearchResult* result, const SearchParameters* params) const { using RH = RangeSearchBlockResultHandler; - RH bres(result, radius); + RH bres(result, is_similarity_metric(metric_type) ? -radius : radius); hnsw_search(this, n, x, bres, params); diff --git a/tests/test_graph_based.py b/tests/test_graph_based.py index d5797186da..a5dcd4c03a 100644 --- a/tests/test_graph_based.py +++ b/tests/test_graph_based.py @@ -184,6 +184,34 @@ def test_abs_inner_product(self): # 4769 vs. 500*10 self.assertGreater(inter, Iref.size * 0.9) + +class Issue3684(unittest.TestCase): + + def test_issue3684(self): + np.random.seed(1234) # For reproducibility + d = 256 # Example dimension + nb = 10 # Number of database vectors + nq = 2 # Number of query vectors + xb = np.random.random((nb, d)).astype('float32') + xq = np.random.random((nq, d)).astype('float32') + + faiss.normalize_L2(xb) # Normalize both query and database vectors + faiss.normalize_L2(xq) + + hnsw_index_ip = faiss.IndexHNSWFlat(256, 16, faiss.METRIC_INNER_PRODUCT) + hnsw_index_ip.hnsw.efConstruction = 512 + hnsw_index_ip.hnsw.efSearch = 512 + hnsw_index_ip.add(xb) + + # test knn + D, I = hnsw_index_ip.search(xq, 10) + self.assertTrue(np.all(D[:, :-1] >= D[:, 1:])) + + # test range search + radius = 0.74 # Cosine similarity threshold + lims, D, I = hnsw_index_ip.range_search(xq, radius) + self.assertTrue(np.all(D >= radius)) + class TestNSG(unittest.TestCase):