IndexFlatL2 silently drops rows containing non-finite components from search results (-1 slots) without any error (1.15.0)
Current Behavior
IndexFlatL2::add accepts vectors containing NaN / +Inf without error, and search then silently OMITS those rows from results: with k=3 over a 3-row index, only 1 result is returned and the remaining slots are padded with -1.
rows: 0 = [NaN, 1], 1 = [Inf, 1], 2 = [0.5, 1]; query = [0.5, 1]
search(q, k=3) -> I = [2, -1, -1], D = [0.0, FLT_MAX, FLT_MAX]The true nearest row is correct; the two non-finite rows are neither returned with an inf/NaN distance nor flagged in any way. A caller cannot distinguish "fewer than k valid rows exist" from "rows were discarded", and the effective k silently shrinks depending on data values.
Expected Behavior
At minimum, one of:
- reject non-finite components at
addtime with a clear error (ecosystem precedent: pgvector rejects with "NaN not allowed in vector"; Qdrant treats API-boundary rejection as the correct policy in #9378), or - return the rows with inf/NaN distances and let the caller filter.
Silently shrinking the result set is the most dangerous of the options because it corrupts exact-search contracts (min(k, n) results) without any signal.
Steps to Reproduce
reproduce.py --output observed.json (Python 3 + faiss + numpy only). Adds the three rows, searches k=3, prints ids/distances, and exits non-zero unless 3 rows come back with no -1 slot. Deterministic; observed.json is from an actual run, executed twice with identical results.
import numpy as np, faiss
idx = faiss.IndexFlatL2(2)
idx.add(np.array([[float('nan'), 1], [float('inf'), 1], [0.5, 1]], dtype=np.float32))
D, I = idx.search(np.array([[0.5, 1]], dtype=np.float32), 3)
print(I) # [[ 2 -1 -1 ]]Environment
- faiss-cpu 1.15.0 (conda-forge wheel), Python 3.12, Windows 11 x64
IndexFlatL2, 3 rows, k=3
The issue was found while checking that adding rows never shrinks another row's result set. Related: #297 (negative distances, closed) and the wiki FAQ on floating-point accuracy; this report is about missing input validation combined with silent result truncation, which the FAQ's accuracy note does not cover.
Source: facebookresearch/faiss