Skip to content

fix(IndexFlat1D): keep sorted permutation valid across remove_ids and merge_from - #5654

Open
thlurte wants to merge 2 commits into
facebookresearch:mainfrom
thlurte:fix-indexflat1d-remove-ids
Open

thlurte wants to merge 2 commits into
facebookresearch:mainfrom
thlurte:fix-indexflat1d-remove-ids

Conversation

@thlurte

@thlurte thlurte commented Sep 18, 2026

Copy link
Copy Markdown

Fixes #5576.

Problem

IndexFlat1D maintains a sorted permutation array perm for binary search, which was previously only updated in add() and cleared in reset().

When remove_ids() or merge_from() was called, the inherited implementations from IndexFlatCodes compacted or appended elements and updated ntotal, leaving perm in a stale state with perm.size() != ntotal. The subsequent call to search() would throw a RuntimeError: '!(perm.size() == static_cast<size_t>(ntotal))' failed: Call update_permutation before search.

Solution

  1. remove_ids: Override in IndexFlat1D:
    • Call IndexFlatL2::remove_ids(sel).
    • If elements were removed (nremoved > 0):
      • If continuous_update == true, immediately call update_permutation().
      • If continuous_update == false, clear perm to invalidate it until update_permutation() is explicitly invoked.
  2. merge_from: Override in IndexFlat1D with identical permutation maintenance.
  3. Tests: Added regression tests in tests/test_index_accuracy.py (TestFlat1D.test_remove_ids and TestFlat1D.test_merge_from) covering:
    • continuous_update=True with search immediately following remove_ids.
    • continuous_update=False verifying permutation invalidation and re-sorting.
    • Deletion with no matched IDs (nremoved == 0).
    • Complete index clearance via remove_ids.
    • Merging two IndexFlat1D indexes.

… merge_from

Fixes facebookresearch#5576.

IndexFlat1D maintains a sorted permutation array `perm` for binary search,
which was only updated in `add()` and cleared in `reset()`. When `remove_ids()`
or `merge_from()` was called, the inherited implementations from `IndexFlatCodes`
compacted or appended elements and updated `ntotal`, leaving `perm` in a stale state
with `perm.size() != ntotal`. Subsequent `search()` calls would throw a RuntimeError.

This commit:
- Overrides `remove_ids()` in `IndexFlat1D` to rebuild `perm` when `continuous_update=true`
  or invalidate it (`perm.clear()`) when `continuous_update=false`.
- Overrides `merge_from()` in `IndexFlat1D` with matching permutation maintenance.
- Adds regression tests in `TestFlat1D` covering deletion, empty match, complete clearance,
  continuous/manual update lifecycles, and index merging.
@meta-cla

meta-cla Bot commented Sep 18, 2026

Copy link
Copy Markdown

Hi @thlurte!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexFlat1D.remove_ids leaves a stale sorted permutation; the next search fails

1 participant