Fix(util): ensure seed is < 2**23 for np.random.seed() #965
+8
−2
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.
Description
Fixes pytest-dev/pytest-randomly#689 and #960
np.random.seed() only works with seeds in range 0 to (2**32)-1. However, in our util function, we accept an arbitrary python int, which could be larger than this. Now, we modulo the given seed to the acceptable range for numpy.
torch says it needs a seed in the range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff], so I fixed that as well.
The other backends, such as cupy and cuda, mention no range limits in their docs, so I will leave them as-is for now.
I ran into this when using this library with pytest-randomly, which calls
fix_random_seed(an int possibly larger than 2**32-1)during test initialization using the 'pytest_randomly.random_seeder' entry point.Types of change
Fix
Checklist