Repository navigation
Conversation
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
This branch has not been deployed
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 #5931
contrib.ivf_tools.add_preassignedrebound itsidsparameter tofaiss.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 beforeadd_corereads it, and the inverted lists store wrong ids. The pointer now goes into its own variable, soidskeeps the array alive for the call (the fix suggested in the issue).Index.train(x, xq_train=...)inclass_wrappers.pyhad the same pattern: the float32 copy made bynp.ascontiguousarraywas only referenced by the pointer passed totrain_with_queries. It is now kept inxq_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]Narrated recording, before then after (sound on):
evidence.mp4
Commands shown (Python REPL, faiss-cpu 1.16.0 wheel with the
contribsources from main, then from this branch):main:
this branch:
Edge cases and unchanged behaviour checked (in the REPL, shown in the video):
(index.search(x, 1)[1].ravel() == np.arange(1000, 2000)).all()isTrue)5000 ... 5004)idsthe vectors are numbered from 0, as beforesearch_preassignedon the same index returns1000 ... 1004The
xq_trainchange could not be shown with a real index: only the SVS LeanVec indexes overridetrain_with_queries, and SVS is not in the CPU wheel. I checked it by temporarily replacingIndexFlatL2.train_with_querieswith a Python function that allocates an array and then reads the query pointer: with float64xq_trainit 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):
TestPreassigned.test_temporary_idsintests/test_contrib.py: fails on main (5 of 5 runs), passes on this branchpython -m pytest tests/test_contrib.py: 51 passedpython -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 mainI used AI assistance (Claude Code) to write this change and the description.
🤖 Generated with Claude Code