-
Notifications
You must be signed in to change notification settings - Fork 198
feat(rust): add serialize/deserialize to brute force index #2231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamie8johnson
wants to merge
3
commits into
NVIDIA:main
Choose a base branch
from
jamie8johnson:rust-brute-force-serialize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Add serialize/deserialize to the Rust brute force index | ||
|
|
||
| ## What | ||
|
|
||
| Adds `serialize` and `deserialize` to the Rust `brute_force::Index`, wrapping the | ||
| existing C entry points `cuvsBruteForceSerialize` / `cuvsBruteForceDeserialize`. | ||
| This brings the brute force binding to parity with the CAGRA binding, which already | ||
| exposes serialize/deserialize. | ||
|
|
||
| - `Index::serialize(&self, res, filename)` writes the index to disk. | ||
| - `Index::deserialize(res, filename) -> Result<Index>` loads an index from disk. | ||
|
|
||
| Both methods mirror the CAGRA implementation: | ||
|
|
||
| - A private `path_to_cstring` helper converts the filesystem path to a `CString`, | ||
| returning `Error::InvalidArgument` (instead of panicking) for paths that are not | ||
| valid UTF-8 or that contain an interior NUL byte. The path is validated before any | ||
| FFI call is made. | ||
| - Every FFI call is wrapped in `check_cuvs`. | ||
| - `deserialize` constructs the `Index` handle first (via `Index::new()`), so that if | ||
| the underlying `cuvsBruteForceDeserialize` call fails, the handle's `Drop` still | ||
| runs and releases the C-side index allocation (RAII-safe error path). | ||
|
|
||
| The doc comments note that the serialization format may change between cuVS versions, | ||
| matching the wording in the C header. | ||
|
|
||
| ## Notes for reviewers | ||
|
|
||
| - **No new bindings were generated.** `cuvsBruteForceSerialize` and | ||
| `cuvsBruteForceDeserialize` are already present in `rust/cuvs-sys/src/bindings.rs` | ||
| (brute_force is pulled in through `core/all.h`), so this change is purely additive | ||
| on the safe Rust wrapper side and touches no generated code. | ||
| - **Test helper lifetime detail.** The brute force `Index` keeps a non-owning device | ||
| view of its dataset (`_dataset`). The serialize round-trip test deliberately keeps | ||
| the host `ndarray` array in the same scope as the index for the duration of the | ||
| test, because the device tensor's `shape` pointer borrows that array's dimension | ||
| storage. Moving the host array while the index is alive would dangle that pointer | ||
| (this is a property of the existing `ManagedTensor` view, not of these new methods). | ||
| - **Conflicts with sibling in-flight Rust PRs** (e.g. the IVF-SQ bindings PR #2229 | ||
| and other Rust binding PRs): if conflicts arise, resolve by merging `main` into this | ||
| branch rather than rebasing, per the project's no-rebase contribution guideline. | ||
|
|
||
| ## Testing | ||
|
|
||
| Two new unit tests were added alongside the existing `test_l2`, mirroring the CAGRA | ||
| serialize tests: | ||
|
|
||
| - `test_brute_force_serialize_deserialize` — builds an index, serializes it, asserts | ||
| the output file exists and is non-empty, deserializes it back, and re-verifies that | ||
| a self-neighbor search on the **loaded** index returns each query as its own nearest | ||
| neighbor. | ||
| - `test_brute_force_serialize_rejects_interior_nul` — confirms that a path containing | ||
| an interior NUL byte surfaces as `Error::InvalidArgument` rather than panicking. | ||
|
|
||
| All brute force tests pass (run single-threaded on a single GPU): | ||
|
|
||
| ``` | ||
| cargo test -p cuvs brute_force -- --test-threads=1 | ||
| test brute_force::tests::test_brute_force_serialize_deserialize ... ok | ||
| test brute_force::tests::test_brute_force_serialize_rejects_interior_nul ... ok | ||
| test brute_force::tests::test_l2 ... ok | ||
| test result: ok. 3 passed; 0 failed; 0 ignored | ||
| ``` | ||
|
|
||
| `cargo fmt` and `cargo clippy` are clean for the changed file. | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.