From 834c54324055e31ac2d94ff5ca4d0777db43038b Mon Sep 17 00:00:00 2001 From: Haijun Yu Date: Tue, 3 Oct 2023 02:30:47 -0700 Subject: [PATCH] Fix SingleBestResultHandler bug. if IndexFlatL2 empty data then search topk = 1 return label = 0 not -1. (#3075) Summary: Fix SingleBestResultHandler bug. if IndexFlatL2 empty data then search topk = 1 return label = 0 not -1. for example int d = 64; // dimension int nb = 100000; // database size int nq = 1; // nb of queries std::mt19937 rng; std::uniform_real_distribution<> distrib; float* xb = new float[d * nb]; float* xq = new float[d * nq]; for (int i = 0; i < nb; i++) { for (int j = 0; j < d; j++) xb[d * i + j] = distrib(rng); xb[d * i] += i / 1000.; } for (int i = 0; i < nq; i++) { for (int j = 0; j < d; j++) xq[d * i + j] = distrib(rng); xq[d * i] += i / 1000.; } faiss::IndexFlatL2 index(d); // call constructor printf("is_trained = %s\n", index.is_trained ? "true" : "false"); int k = 1; { // sanity check: search 1 first vectors of xb idx_t* I = new idx_t[k * nq]; float* D = new float[k * nq]; index.search(nq, xb, k, D, I); // *I = 0 not -1 } Pull Request resolved: https://github.com/facebookresearch/faiss/pull/3075 Reviewed By: pemazare Differential Revision: D49749983 Pulled By: mdouze fbshipit-source-id: 10e9784035118b9e33e109180cab425de28d4ded --- faiss/impl/ResultHandler.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faiss/impl/ResultHandler.h b/faiss/impl/ResultHandler.h index d096fbcfa3..53ed520826 100644 --- a/faiss/impl/ResultHandler.h +++ b/faiss/impl/ResultHandler.h @@ -446,7 +446,7 @@ struct SingleBestResultHandler { void begin(const size_t current_idx) { this->current_idx = current_idx; min_dis = HUGE_VALF; - min_idx = 0; + min_idx = -1; } /// add one result for query i