Fix bBitMinHash NumPy pickling issue#248
Merged
ekzhu merged 2 commits intoekzhu:masterfrom Nov 3, 2025
Merged
Conversation
Contributor
Author
|
Update: edited Ubuntu repo in tests as in #246. The main reason I submitted a separate PR for this change is because this PR as a doc might be useful for posterity, since this is kind of a non-trivial change. |
Contributor
Author
|
cc @ekzhu |
ekzhu
approved these changes
Nov 3, 2025
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.
cc @ekzhu
Problem:
Recent PRs such as #246 failed tests in Python 3.11 due to inconsistencies in the way that NumPy handled type coercion in certain functions (such as left_shift
<<). This led to an issue where, in more recent versions of Python, when pickling hashvalues in bBitMinHash the hashvalues would overflow to zero (because NumPy, being strict, refused to coerce the type from np.uint32 to np.uint64), effectively removing the ability to pickle these objects - and worse, failing silently.This problem was not present in previous NumPy versions (e.g., 1.x) because they were more lenient with type coercion and would implicitly change the hashvalue type to np.uint64.
Solution:
We resolve this issue by instead performing the bitwise operations in bBitMinHash's
__getstate__routine in native Python BigInteger type. This type has theoretically unlimited precision which avoids overflows. Moreover, it is implicitly coerced to np.uint64 as we expect. This works across NumPy versions and therefore across Python versions as well.References
NEP 50 - Promotion rules for Python scalars
[DOC] Changes to NumPy data type promotion