So click baity. A proper title would be, be careful when using random numbers and multi processing...
My initial reaction was, “it can’t possibly be as big as the mistake you’re making if you’re using TensorFlow!”
A common mistake when NumPy’s RNG with PyTorch
21–30 of 54 posts
Re: A common mistake when NumPy’s RNG with PyTorch
#22Forgetting to seed your RNG is a really classic bug. IMHO RNGs should auto seed unless explicitly set not to, but since the opposite behaviour was baked into C so many years ago it's kind of the default. The worst part is how easy a bug this is to miss unless you're explicitly printing out the first set of random numbers for some strange reason.
I’m of the opposite opinion and would get away from all auto RNG seeding: 1) this will help reproducibility a great deal, which is a pain so often. 2) forcing users to actually understand the seeding of RNGs from the point that they are novice programmers could help allay bugs of the sort seen in this post, which I believe stems from having too much faith that RNGs will simply work out of the box as substitutions for…
Re: A common mistake when NumPy’s RNG with PyTorch
#23Re: A common mistake when NumPy’s RNG with PyTorch
#24I know this bc I fixed the bug. And probably caused it. Hehe.
Also you dont just want to set ur numpy seed but also the native python one and the torch one.
Re: A common mistake when NumPy’s RNG with PyTorch
#25This post is yet another example of why you should never use APIs for random number generation that rely upon and mutate hidden global state, like the functions in numpy.random. Instead, use APIs that explicitly deal with RNG state, e.g., by calling methods on an explicitly created numpy.random.Generator object. JAX takes this one step further: there are no mutable RNG objects at all, and the users has to explicitly…
The post just stresses that one should be careful when using random states and multiprocessing, so you should either reseed after forking or using multiprocess/multithread-aware RNG API.
Re: A common mistake when NumPy’s RNG with PyTorch
#26This is probably because I never read these kinds of blogposts but this is one of the most flagrantly clickbait titles I've ever seen. Like the article doesn't even suggest ditching numpy in favor of jax or some kind of other hot take (which would at least warrant such a bombastic title) it literally just presents one instance in which you might be making a mistake when using numpy's rng (not even something more uniq…
Title seems pretty accurate to me!
Re: A common mistake when NumPy’s RNG with PyTorch
#27This post is yet another example of why you should never use APIs for random number generation that rely upon and mutate hidden global state, like the functions in numpy.random. Instead, use APIs that explicitly deal with RNG state, e.g., by calling methods on an explicitly created numpy.random.Generator object. JAX takes this one step further: there are no mutable RNG objects at all, and the users has to explicitly…
The solution you suggest is irrelevant to the issue mentioned in the article. Even if you use np.random.RandomState, or any other "explicit RNG state", that state will still be copied in the fork() call. The post just stresses that one should be careful when using random states and multiprocessing, so you should either reseed after forking or using multiprocess/multithread-aware RNG API.
Re: A common mistake when NumPy’s RNG with PyTorch
#28Re: A common mistake when NumPy’s RNG with PyTorch
#29Earlier quoted context omitted.
NumPy does auto-seed the RNG if you don't pass a seed yourself, using platform-specific code to pull some entropy from the OS. So that common case is handled reasonably well, unlike with C. In fact if you want exactly reproducible results (e.g. in testcases), you have to seed with a known seed, to avoid that default behavior. The issue here is a little more subtle: if you fork 10 copies of your Python process, all 10…
It’s even slightly more subtle than that. Python multiprocessing doesn’t use fork on Windows. It starts a new process and so shouldn’t be affected by this. So to trigger this you need to have num_processes != 0 on your DataLoader and be running on a non-Windows platform.