Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

bwhitt7
Copy link
Contributor

@bwhitt7 bwhitt7 commented Sep 9, 2025

This PR is related to #29552, specifically seeking to address np.random thread safe issues. Calls to legacy np.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 using Generator or RandomState 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. Notably numpy/random/test/test_random.py and numpy/random/test/test_regression.py have not been touched since I figured they should continue to test the global np.random calls and not be run in parallel. If folks think it's okay to also convert these files, I can definitely do so.

Copy link
Member

@rkern rkern left a 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.

@bwhitt7
Copy link
Contributor Author

bwhitt7 commented Sep 10, 2025

@rkern Thank you for the feedback! I went ahead and committed the changes you suggested, as well as making changes to test_random.py and test_regressions.py. For now I'm mainly converting the tests that caused thread safety issues, but I can go back to these tests at some point and convert the rest of of the global np.random.* calls to use RandomState instances instead if folks think that's best.

Copy link
Member

@ngoldbaum ngoldbaum left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants