Skip to content

Remove FAISS IDs with IDSelectorBatch, not IDSelectorArray - #48

Merged
wshlavacek merged 2 commits into
mainfrom
fix/14-faiss-id-selector
Dec 2, 2026
Merged

wshlavacek merged 2 commits into
mainfrom
fix/14-faiss-id-selector

Conversation

@wshlavacek

Copy link
Copy Markdown
Collaborator

Fixes #14.

What was wrong

make_id_selector() in src/litkit/index/ids.py returned faiss.IDSelectorArray whenever the class existed, and IDSelectorBatch only as a fallback. IDSelectorArray tests membership by scanning its whole ID list. IndexIDMap2.remove_ids() and IndexIVF.remove_ids() run that test once per stored vector, so removing k IDs from N vectors cost O(N × k). Every dedup removal pays this: --update, backfill and segment ingestion.

Stored vectors IDs removed main (IDSelectorArray) this PR (IDSelectorBatch)
100,000 100 0.009 s 0.003 s
100,000 2,000 0.106 s 0.003 s
100,000 20,000 0.933 s 0.004 s
1,000,000 20,000 10.3 s 0.033 s

At 10M vectors that is about 100 s per batch on main.

The fix

make_id_selector() returns an IDSelectorBatch (a Bloom filter in front of a hash set) built from an int64 copy of the IDs. That is what FAISS's own remove_ids(ndarray) uses. The docstring notes the one case where FAISS picks IDSelectorArray instead (an IVF index with a hashtable direct map, which litkit never sets).

Evidence

  • New tests in tests/test_index.py:
    • test_removing_many_ids_costs_about_as_much_as_removing_few removes 20 and then 20,000 of 100,000 flat vectors and requires the second to take less than 10× the first plus 10 ms. It fails on main; 30 repeated runs on the fix used at most 8.5% of the limit.
    • test_make_id_selector_removes_exactly_the_given_ids[flat|ivfpq × 5] are controls: empty, repeated, absent and above-32-bit IDs, and a strided list. They pass on main and on the fix.
  • Mutation check: going back to IDSelectorArray fails the timing test. Passing the raw array to remove_ids survives, because FAISS then builds the same IDSelectorBatch itself.
  • At litkit's real shapes (d=768), removing 20k of 1M vectors: IVF-PQ (the default chunk index) 9.75 s → 0.025 s; flat 9.60 s → 0.115 s.
  • Full suite: 372 passed, 1 skipped, 8 xfailed (Builds give a paper a second vector when it reappears #7 and Databases created before doc_id can't be opened #10).

Review

  • Round 1: no bugs. The docstring now names the hashtable direct-map case, and the CHANGELOG makes no claim about IVF-PQ removal being correct (it isn't on main; Make interrupted tar builds resumable #43 fixes that).
  • Round 2: clean. 1,440 randomized trials against main (flat, IVF-PQ, HNSW; in memory and after save and reload; uint64, strided and Fortran-order inputs; extreme int64 values) gave 0 mismatches. HNSW is unaffected: safe_remove_ids returns 0 before it uses the selector.

Left alone

make_id_selector() used faiss.IDSelectorArray whenever it existed, and
IDSelectorBatch only as a fallback ("try newer name first"; both classes
exist and behave differently). IDSelectorArray tests membership by
scanning its whole ID array, and IndexIDMap2.remove_ids() and
IndexIVF.remove_ids() run that test once per stored vector, so removing
k IDs from N vectors cost O(N * k). Removing 20,000 IDs from 1M flat
vectors took 10.3 s; at 10M vectors that is about 100 s per batch, paid
by --update, backfill and segment ingestion on every dedup.

make_id_selector() now returns an IDSelectorBatch, a hash set behind a
Bloom filter, as FAISS's own remove_ids(ndarray) does. The same removal
takes 0.033 s.

Fixes #14
@wshlavacek
wshlavacek merged commit ed15145 into main Dec 2, 2026
4 checks passed
@wshlavacek
wshlavacek deleted the fix/14-faiss-id-selector branch December 2, 2026 06:26
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.

Removing IDs from FAISS scans the ID list once per stored vector

1 participant