Repository navigation
fix(python): encode accelerated IVF_PQ dot indexes without residuals - #11199
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The accelerated dot path now matches Rust’s PQ contract: L2 codebooks over raw vectors. The recall regression fails with the base encoding functions and passes with this fix; the neighboring accelerator tests also pass.
This PQ correction can stand independently of the pre-existing IVF convergence issue tracked in #11173.
Xuanwo
approved these changes
Nov 19, 2026
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.
create_index(..., index_type="IVF_PQ", metric="dot", accelerator=...)builds an index with much lower recall than the CPU build. The torch path trains the PQ codebook and computes the PQ codes in Python, and the Rust builder uses them unchanged. For dot the two sides disagreed, the same way they did for cosine in #9755:PQBuildParams::use_residual), and always trains the PQ codebook with L2. For dot it encodes the raw vectors and searches them without subtracting the IVF centroid.Changes
All in
python/python/lance/vector.py, dot only:compute_partitions(residual sample for PQ training) andone_pass_assign_ivf_pq_on_accelerator(final encoding): do not subtract the IVF centroid when the metric is dot.train_pq_codebook_on_accelerator: train the per-sub-vector k-means withl2for dot, as for cosine.l2andcosineare unchanged.Not changed here: at
nprobes=1the accelerated build still trails the CPU build, because the dot IVF k-means inlance.torch.kmeansstops before its first update for vectors with norm above 1. That is fixed separately in #11173. This PR does not depend on it: the test below probes every partition, so it compares only the PQ encoding. With both changes the accelerated build matches the CPU build at everynprobes(last row of the table).Both changes are needed: with only the residual change the accelerated recall@10 at nprobes 4 is about 0.25, with only the L2 codebook about 0.41, with both 0.81.
Tests
Added
test_torch_dot_ivf_pq_recalltopython/python/tests/test_vector_index.py, next to the cosine one. It builds a dot IVF_PQ index on 4096 x 32-d clustered vectors, once without accelerator and once withaccelerator=torch.device("cpu"), and checks that the recall@10 (nprobes=4) of the accelerated build is within 0.1 of the CPU build. It needs only torch (pytest.importorskip("torch")) and runs in about 5 s.recall@10 against exact dot top-10, 20000 x 32-d clustered data, 8 partitions, 16 sub-vectors:
accelerator=cpu, beforeaccelerator=cpu, afteraccelerator=mps, after, plus #11173Single runs; the CPU build and the torch sampling are not seeded, so the numbers vary by a few points between runs.
Checks run on an Apple-silicon Mac using the pylance 15.0.0-beta.14 wheel with this branch's
lance/vector.pyoverlaid (vector.pyis identical to main's apart from this change; the Rust extension is the wheel's):-k "torch or accelerator"intest_vector_index.py: 8 passed.ruff checkandruff format --checkpass on the touched files (ruff 0.11.2, the version CI pins).uv run make lintwas not run: it needsmake install, which builds the Rust extension.