Skip to content

Commit

Permalink
Back out "Better NaN handling" (#3006)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #3006

Original commit changeset: 99e7786582e9

Original Phabricator Diff: D48031390

Reviewed By: algoriddle

Differential Revision: D48353221

fbshipit-source-id: fd326f2a45d20f68507ca39a33a325528651b37d
  • Loading branch information
mlomeli1 authored and facebook-github-bot committed Aug 15, 2023
1 parent e3deb71 commit c09992b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 215 deletions.
25 changes: 11 additions & 14 deletions faiss/impl/HNSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include <faiss/impl/HNSW.h>

#include <cmath>
#include <string>

#include <faiss/impl/AuxIndexStructures.h>
Expand Down Expand Up @@ -543,11 +542,12 @@ int search_from_candidates(
for (int i = 0; i < candidates.size(); i++) {
idx_t v1 = candidates.ids[i];
float d = candidates.dis[i];
assert(v1 >= 0);
FAISS_ASSERT(v1 >= 0);
if (!sel || sel->is_member(v1)) {
if (d < D[0]) {
faiss::maxheap_replace_top(k, D, I, d, v1);
nres++;
if (nres < k) {
faiss::maxheap_push(++nres, D, I, d, v1);
} else if (d < D[0]) {
faiss::maxheap_replace_top(nres, D, I, d, v1);
}
}
vt.set(v1);
Expand Down Expand Up @@ -612,9 +612,10 @@ int search_from_candidates(

auto add_to_heap = [&](const size_t idx, const float dis) {
if (!sel || sel->is_member(idx)) {
if (dis < D[0]) {
faiss::maxheap_replace_top(k, D, I, dis, idx);
nres++;
if (nres < k) {
faiss::maxheap_push(++nres, D, I, dis, idx);
} else if (dis < D[0]) {
faiss::maxheap_replace_top(nres, D, I, dis, idx);
}
}
candidates.push(idx, dis);
Expand Down Expand Up @@ -667,7 +668,7 @@ int search_from_candidates(
stats.n3 += ndis;
}

return std::min(nres, k);
return nres;
}

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

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

candidates.push(nearest, d_nearest);

search_from_candidates(
*this, qdis, k, I, D, candidates, vt, stats, 0, 0, params);
} else {
Expand Down
7 changes: 3 additions & 4 deletions faiss/impl/ResultHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ struct SingleBestResultHandler {
/// begin results for query # i
void begin(const size_t current_idx) {
this->current_idx = current_idx;
min_dis = C::neutral();
min_idx = -1;
min_dis = HUGE_VALF;
min_idx = 0;
}

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

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

Expand Down
5 changes: 0 additions & 5 deletions faiss/impl/ScalarQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,6 @@ void ScalarQuantizer::set_derived_sizes() {
}

void ScalarQuantizer::train(size_t n, const float* x) {
for (size_t i = 0; i < n * d; i++) {
FAISS_THROW_IF_NOT_MSG(
std::isfinite(x[i]), "training data contains NaN or Inf");
}

int bit_per_dim = qtype == QT_4bit_uniform ? 4
: qtype == QT_4bit ? 4
: qtype == QT_6bit ? 6
Expand Down
182 changes: 0 additions & 182 deletions tests/test_error_reporting.py

This file was deleted.

0 comments on commit c09992b

Please sign in to comment.