fix: respect PQ num_bits for numpy codebooks#7586
Open
ddupg wants to merge 2 commits into
Open
Conversation
wjones127
approved these changes
Jul 2, 2026
| validate_vector_index(dataset, "vector", refine_factor=10, pass_threshold=0.99) | ||
|
|
||
|
|
||
| def test_index_with_pq_codebook_rejects_wrong_num_bits_shape(tmp_path): |
Contributor
There was a problem hiding this comment.
praise: thanks for adding a test for the error message.
| "index_kwargs,num_centroids,pass_threshold", | ||
| [ | ||
| pytest.param({}, 256, 0.99, id="default_num_bits"), | ||
| pytest.param({"num_bits": 4}, 16, 0.5, id="four_bit"), |
Contributor
There was a problem hiding this comment.
question: why the 50% recall threshold here? Seems rather low.
Contributor
Author
There was a problem hiding this comment.
Good point. 50% wasn't a meaningful threshold here since the codebook is random. I split out the 4-bit case to check nbits == 4 and that queries run. This also matches the Rust 4-bit PQ tests, which don't assert recall today.
1d620b4 to
5ae8390
Compare
5ae8390 to
55f8c59
Compare
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.
Summary
2 ** num_bitsinstead of always requiring 256 centroidspq_codebookdocstring to describe thenum_bits-dependent shapeReason
Dataset.create_index(..., index_type="IVF_PQ", num_bits=4, pq_codebook=<numpy array>)should accept a codebook with 16 centroids per sub-vector. The Python wrapper already exposesnum_bits, and the Rust PQ implementation supports 4-bit and 8-bit PQ, but the numpy validation path was still hard-coded to the 8-bit shape with 256 centroids. This rejected a valid 4-bit user-supplied codebook before it reached the Rust layer.Validation
uv run --frozen --python 3.12 pytest python/tests/test_vector_index.py::test_index_with_pq_codebook python/tests/test_vector_index.py::test_index_with_pq_codebook_rejects_wrong_num_bits_shapeuv run --frozen --python 3.12 ruff format --check --diff python/lance/dataset.py python/tests/test_vector_index.pyuv run --frozen --python 3.12 ruff check python/lance/dataset.py python/tests/test_vector_index.pygit diff --checkLocal lint note
uv run --frozen --python 3.12 make lintwas attempted. Ruff passed, but pyright failed in this local environment because optional imports such astensorflowandtorchare not installed.