-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
TST: Fix np.random thread test failures #29729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of minor things. Other than that, the changes look good.
AFAICT, all of the tests in test_random.py
could be converted to use explicit RandomState
instances; I don't think they test anything about the np.random.*
global method aliases separately. The only test in test_regression.py
that actually tests global state functionality specifically is test_call_within_randomstate
(it checks whether a specific RandomState
method call on a separate instance might affect the global state). The rest are safe to convert. I think many of the test_random.py
tests are duplicated by test_randomstate.py
, but I don't think we need a separate copy that tests the np.random.*
aliases explicitly, at least not in terms of their detailed functionality. IMO, at least.
@rkern Thank you for the feedback! I went ahead and committed the changes you suggested, as well as making changes to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read through everything and this is 100% a mechanical change that looks correct to me. Just one nitpick where I think we don't want to leave it as it was anymore because of how you've changed it.
This PR is related to #29552, specifically seeking to address
np.random
thread safe issues. Calls to legacynp.random
affects global state, and occasionally this results in test failures when running tests in parallel threads, especially when tests are expecting specific results for a specific seed (np.random.seed
is a good indicator of this). This can be fixed by making these global state calls into local state calls usingGenerator
orRandomState
instances.For now, most of these tests have been converted to use
np.random.RandomState
instances, since these test files are generally testing the legacy RandomState. Notablynumpy/random/test/test_random.py
andnumpy/random/test/test_regression.py
have not been touched since I figured they should continue to test the globalnp.random
calls and not be run in parallel. If folks think it's okay to also convert these files, I can definitely do so.