Skip to content

Reject non-finite cosine inputs - #689

Open
shoaib0657 wants to merge 1 commit into
nmslib:developfrom
shoaib0657:fix/cosine-nonfinite
Open

shoaib0657 wants to merge 1 commit into
nmslib:developfrom
shoaib0657:fix/cosine-nonfinite

Conversation

@shoaib0657

Copy link
Copy Markdown

Addresses #687.

Cosine indexes currently accept NaN and infinity, which can produce NaN distances. This checks the converted float32 batch before insertion or search and raises ValueError if any component is non-finite. A rejected insertion batch leaves existing items unchanged.

The check covers both Index and BFIndex, including finite float64 values that overflow during conversion. L2/IP behavior and normalization arithmetic are unchanged.

Regression tests cover single vectors and batches, rejected updates, automatic labels, filters, conversion overflow, strided inputs, zero vectors, and save/load.

Validation

  • make test: 28 tests passed.
  • make examples: all five examples passed.
  • C++ builds and ctest: 16/16 passed with exceptions ON and OFF.

Performance

In a local WSL2 test with 50,000 × 128 float32 vectors and one thread, median BFIndex insertion time increased from 20.36 ms to 22.75 ms. Timings varied, and query measurements were inconclusive.

@shoaib0657
shoaib0657 marked this pull request as ready for review October 2, 2026 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant