Fix: treat NumPy uint8 vectors as u8, not bit-packed binary - #780
Open
chakshu-dhannawat wants to merge 1 commit into
Open
Fix: treat NumPy uint8 vectors as u8, not bit-packed binary#780chakshu-dhannawat wants to merge 1 commit into
chakshu-dhannawat wants to merge 1 commit into
Conversation
A NumPy uint8 buffer is ambiguous: the same bytes back both bit-packed binary vectors (b1x8) and unsigned byte vectors (u8). numpy_string_to_kind maps uint8 to b1x8 for backwards compatibility, so when no explicit dtype was passed to add/search, a u8 index reinterpreted its data as packed bits and the vectors silently collapsed to (near-)zero. int8 worked because it maps to i8 unambiguously. Disambiguate against the index's own scalar kind: when the caller gave no dtype and the buffer resolves to b1x8 but the index is u8, use u8. Binary indexes are unaffected, so bit-packed uint8 vectors keep working. Applied at the add, search, and cluster dispatch sites through a shared helper, and added a scalar_kind() accessor to the multi-shard wrapper so it compiles for both index types. Adds a regression test that stores sparse uint8 counts, checks the round trip and search ranking, and confirms the binary path is untouched. The test fails on the current code (vector stored as all zeros) and passes with the fix. Closes unum-cloud#595
3 tasks
Contributor
Author
|
Heads up on the one red check (Ubuntu GCC 14): it's the C++ core test failing at
The 22 other checks are green, including the Python suite that covers the actual fix. A rerun of just the GCC 14 job should clear it, but I don't have permissions to trigger one. |
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.
Closes #595.
The bug
A
u8index silently corrupts its vectors. Addingnp.uint8data zeros outalmost everything, so search results are meaningless.
int8works fine.Root cause
A NumPy
uint8buffer is ambiguous: the same bytes back both bit-packed binaryvectors (
b1x8) and unsigned byte vectors (u8).numpy_string_to_kindmapsuint8tob1x8for backwards compatibility. When no explicitdtypeis passedto
add/search, the dispatch used that detected kind directly, so au8indexreinterpreted its data as packed bits and the values collapsed to (near-)zero.
int8avoids this because it maps toi8unambiguously.The index already knows it is a
u8index, so that is enough to disambiguate.The fix
When the caller gave no
dtypeand the buffer resolves tob1x8but the index'sown scalar kind is
u8, useu8. Binary indexes are untouched, so bit-packeduint8vectors keep working exactly as before. This is wired through a smallshared helper at the add, search, and cluster dispatch sites. The multi-shard
wrapper gains a
scalar_kind()accessor (mirroring its existingscalar_words())so the helper compiles for both index types.
Tests
Added
test_u8_vectors_not_misread_as_binary: it stores sparseuint8counts,checks the round trip and search ranking, and confirms a binary index is
unaffected. It fails on the current code (vector stored as all zeros) and passes
with the fix. Full
test_index.pysuite is green (609 passed, 6 pre-existing skips).