Skip to content

Keep the ids array alive in add_preassigned - #5972

Open
burrows99 wants to merge 2 commits into
facebookresearch:mainfrom
burrows99:fix/add-preassigned-temporary-ids
Open

burrows99 wants to merge 2 commits into
facebookresearch:mainfrom
burrows99:fix/add-preassigned-temporary-ids

Conversation

@burrows99

Copy link
Copy Markdown

Fixes #5931

contrib.ivf_tools.add_preassigned rebound its ids parameter to faiss.swig_ptr(ids). The SWIG pointer does not hold a reference to the array, so when the caller passes a temporary (ids=np.arange(n)) the array is freed before add_core reads it, and the inverted lists store wrong ids. The pointer now goes into its own variable, so ids keeps the array alive for the call (the fix suggested in the issue).

Index.train(x, xq_train=...) in class_wrappers.py had the same pattern: the float32 copy made by np.ascontiguousarray was only referenced by the pointer passed to train_with_queries. It is now kept in xq_train.

flowchart TD
    A[add_preassigned with ids from np.arange] --> B{ids array}
    B -->|Before| C[Freed before add_core reads it so stored ids are garbage]
    B -->|After| D[Kept alive so stored ids are 1000 to 1999]
Loading

Narrated recording, before then after (sound on):

evidence.mp4

Commands shown (Python REPL, faiss-cpu 1.16.0 wheel with the contrib sources from main, then from this branch):

import numpy as np, faiss
from faiss.contrib.ivf_tools import add_preassigned, search_preassigned
x = np.random.rand(1000, 8).astype('float32')
index = faiss.index_factory(8, 'IVF4,Flat'); index.train(x); index.nprobe = 4
a = index.quantizer.search(x, 1)[1].ravel()
add_preassigned(index, x, a, ids=np.arange(1000, 2000))
index.search(x[:5], 1)[1].ravel()

main:

array([4683554687893991019,                1024,                1002,
                      1003,                1004])

this branch:

array([1000, 1001, 1002, 1003, 1004])

Edge cases and unchanged behaviour checked (in the REPL, shown in the video):

  • all 1000 vectors find their own id ((index.search(x, 1)[1].ravel() == np.arange(1000, 2000)).all() is True)
  • ids kept in a caller variable still work (5000 ... 5004)
  • without ids the vectors are numbered from 0, as before
  • search_preassigned on the same index returns 1000 ... 1004

The xq_train change could not be shown with a real index: only the SVS LeanVec indexes override train_with_queries, and SVS is not in the CPU wheel. I checked it by temporarily replacing IndexFlatL2.train_with_queries with a Python function that allocates an array and then reads the query pointer: with float64 xq_train it read freed memory on main and the original queries on this branch.

Tests (faiss-cpu 1.16.0 wheel, Python 3.12, macOS arm64, with this branch's Python files copied over the wheel's):

  • new TestPreassigned.test_temporary_ids in tests/test_contrib.py: fails on main (5 of 5 runs), passes on this branch
  • python -m pytest tests/test_contrib.py: 51 passed
  • python -m pytest tests/test_index.py tests/test_swig_wrapper.py tests/test_ivf_index.py: 107 passed, 2 failed; the same 2 (TestReconsException) fail with the wheel's unmodified files, because the 1.16.0 C++ library is older than the tests on main
  • The C++ library was not rebuilt from source; the change is Python-only.

I used AI assistance (Claude Code) to write this change and the description.

🤖 Generated with Claude Code

add_preassigned rebound its ids parameter to faiss.swig_ptr(ids). The
pointer does not hold a reference to the array, so when the caller passes
a temporary (ids=np.arange(n)) the array is freed before add_core reads it
and the inverted lists store garbage ids.

Keep the array under its own name. Index.train(xq_train=...) had the same
pattern: the float32 copy of xq_train was only referenced by the pointer.

Fixes facebookresearch#5931
@meta-cla meta-cla Bot added the CLA Signed label Nov 22, 2026

This branch has not been deployed

No deployments
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.

contrib.ivf_tools.add_preassigned stores wrong ids when ids is a temporary array

1 participant