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
Sync numpy random generator with numba random state #3249
Comments
Thanks for the report. I think I see what you mean, that you are would like a call to a Numba implementation of e.g. Internally, Numba has entirely reimplemented both the CPython and NumPy RNG state mechanisms and this is entirely independent of NumPy's |
yes my solution is not thread safe.
do you see a way to have this mechanism made available directly by numba ?
|
I think this would constitute a behavioural change for Numba as the state of the NumPy RNG would be observed and influence the Numba There's a load of issues with thread safety and common uses of threaded RNG calls (like for Monte Carlo) that make this harder to implement in practice. Numba does something to try and be more practically useful for this (initialize random state per thread into TLS), but it does deviate from what would happen in NumPy/the case demonstrated above. Ping @seibert any thoughts? |
thanks @stuartarchibald for the good synthesis of the situation. Whatever can help people having better interaction between numpy random state and numba is I think good. It would facilitate the port of functions to numba as it would not require to change tests due to the use of numba. Both functions would give the same results, but one much faster... |
…d on Alex Gramfort's work <alexandre.gramfort@inria.fr> published on GitHub at numba/numba#3249
Feature request
I got a code for which I could not have deterministic test output due
to some np.random calls in a numba function. Reading the test_random.py
file I found maybe a way to address this issue using a decorator.
The idea is to get from a RandomState instance or the global np random
the state of numpy generator, set it to numba and then on exit taking
the numba state and set it back to numpy. The code looks like this.
what are you thoughts on this? could it inspire passing to jit of njit decorators a random_state parameter to keep np.random and numba states in sync. This would allow to get the same outputs if the njit / jit decorator is used or not.
this code solved my problem but maybe it can help others...
The text was updated successfully, but these errors were encountered: