Repository navigation
Remove FAISS IDs with IDSelectorBatch, not IDSelectorArray - #48
Merged
Merged
Conversation
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
# Conflicts: # CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14.
What was wrong
make_id_selector()insrc/litkit/index/ids.pyreturnedfaiss.IDSelectorArraywhenever the class existed, andIDSelectorBatchonly as a fallback.IDSelectorArraytests membership by scanning its whole ID list.IndexIDMap2.remove_ids()andIndexIVF.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.At 10M vectors that is about 100 s per batch on main.
The fix
make_id_selector()returns anIDSelectorBatch(a Bloom filter in front of a hash set) built from an int64 copy of the IDs. That is what FAISS's ownremove_ids(ndarray)uses. The docstring notes the one case where FAISS picksIDSelectorArrayinstead (an IVF index with a hashtable direct map, which litkit never sets).Evidence
tests/test_index.py:test_removing_many_ids_costs_about_as_much_as_removing_fewremoves 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.IDSelectorArrayfails the timing test. Passing the raw array toremove_idssurvives, because FAISS then builds the sameIDSelectorBatchitself.Review
safe_remove_idsreturns 0 before it uses the selector.Left alone
safe_remove_idsstill has a branch for an ndarray selector that no caller reaches. Make interrupted tar builds resumable #43 rewrites that function, so it is left for that PR.selector._litkit_idsfor its IVF removal path. When Make interrupted tar builds resumable #43 is updated from main it must keep setting that attribute on theIDSelectorBatch; without it, Make interrupted tar builds resumable #43 falls back to a per-ID Python loop over every stored vector (0.11 s per batch at 1M vectors, even when nothing is removed).