Skip to content

Commit c09992b

Browse files
mlomeli1facebook-github-bot
authored andcommitted
Back out "Better NaN handling" (facebookresearch#3006)
Summary: Pull Request resolved: facebookresearch#3006 Original commit changeset: 99e7786582e9 Original Phabricator Diff: D48031390 Reviewed By: algoriddle Differential Revision: D48353221 fbshipit-source-id: fd326f2a45d20f68507ca39a33a325528651b37d
1 parent e3deb71 commit c09992b

File tree

5 files changed

+108
-215
lines changed

5 files changed

+108
-215
lines changed

faiss/impl/HNSW.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <faiss/impl/HNSW.h>
1111

12-
#include <cmath>
1312
#include <string>
1413

1514
#include <faiss/impl/AuxIndexStructures.h>
@@ -543,11 +542,12 @@ int search_from_candidates(
543542
for (int i = 0; i < candidates.size(); i++) {
544543
idx_t v1 = candidates.ids[i];
545544
float d = candidates.dis[i];
546-
assert(v1 >= 0);
545+
FAISS_ASSERT(v1 >= 0);
547546
if (!sel || sel->is_member(v1)) {
548-
if (d < D[0]) {
549-
faiss::maxheap_replace_top(k, D, I, d, v1);
550-
nres++;
547+
if (nres < k) {
548+
faiss::maxheap_push(++nres, D, I, d, v1);
549+
} else if (d < D[0]) {
550+
faiss::maxheap_replace_top(nres, D, I, d, v1);
551551
}
552552
}
553553
vt.set(v1);
@@ -612,9 +612,10 @@ int search_from_candidates(
612612

613613
auto add_to_heap = [&](const size_t idx, const float dis) {
614614
if (!sel || sel->is_member(idx)) {
615-
if (dis < D[0]) {
616-
faiss::maxheap_replace_top(k, D, I, dis, idx);
617-
nres++;
615+
if (nres < k) {
616+
faiss::maxheap_push(++nres, D, I, dis, idx);
617+
} else if (dis < D[0]) {
618+
faiss::maxheap_replace_top(nres, D, I, dis, idx);
618619
}
619620
}
620621
candidates.push(idx, dis);
@@ -667,7 +668,7 @@ int search_from_candidates(
667668
stats.n3 += ndis;
668669
}
669670

670-
return std::min(nres, k);
671+
return nres;
671672
}
672673

673674
std::priority_queue<HNSW::Node> search_from_candidate_unbounded(
@@ -815,11 +816,6 @@ HNSWStats HNSW::search(
815816
// greedy search on upper levels
816817
storage_idx_t nearest = entry_point;
817818
float d_nearest = qdis(nearest);
818-
if (!std::isfinite(d_nearest)) {
819-
// means either the query or the entry point are NaN: in
820-
// both cases we can only return -1 as a result
821-
return stats;
822-
}
823819

824820
for (int level = max_level; level >= 1; level--) {
825821
greedy_update_nearest(*this, qdis, level, nearest, d_nearest);
@@ -830,6 +826,7 @@ HNSWStats HNSW::search(
830826
MinimaxHeap candidates(ef);
831827

832828
candidates.push(nearest, d_nearest);
829+
833830
search_from_candidates(
834831
*this, qdis, k, I, D, candidates, vt, stats, 0, 0, params);
835832
} else {

faiss/impl/ResultHandler.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ struct SingleBestResultHandler {
445445
/// begin results for query # i
446446
void begin(const size_t current_idx) {
447447
this->current_idx = current_idx;
448-
min_dis = C::neutral();
449-
min_idx = -1;
448+
min_dis = HUGE_VALF;
449+
min_idx = 0;
450450
}
451451

452452
/// add one result for query i
@@ -472,8 +472,7 @@ struct SingleBestResultHandler {
472472
this->i1 = i1;
473473

474474
for (size_t i = i0; i < i1; i++) {
475-
this->dis_tab[i] = C::neutral();
476-
this->ids_tab[i] = -1;
475+
this->dis_tab[i] = HUGE_VALF;
477476
}
478477
}
479478

faiss/impl/ScalarQuantizer.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -1075,11 +1075,6 @@ void ScalarQuantizer::set_derived_sizes() {
10751075
}
10761076

10771077
void ScalarQuantizer::train(size_t n, const float* x) {
1078-
for (size_t i = 0; i < n * d; i++) {
1079-
FAISS_THROW_IF_NOT_MSG(
1080-
std::isfinite(x[i]), "training data contains NaN or Inf");
1081-
}
1082-
10831078
int bit_per_dim = qtype == QT_4bit_uniform ? 4
10841079
: qtype == QT_4bit ? 4
10851080
: qtype == QT_6bit ? 6

tests/test_error_reporting.py

-182
This file was deleted.

0 commit comments

Comments
 (0)