Live data from Hacker News

A common mistake when NumPy’s RNG with PyTorch

tanelp.github.io

21–30 of 54 posts

Re: A common mistake when NumPy’s RNG with PyTorch

#21
post #19
post #12

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!”

Certainly if you use it without understanding how to use a random number generator... but that is not really a fault in numpy OR TensorFlow...

Re: A common mistake when NumPy’s RNG with PyTorch

#22
post #5

Forgetting 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…

Indeed. Almost every time (like now) when you think you need a random number, you actually need a low discrepancy sequence.

Re: A common mistake when NumPy’s RNG with PyTorch

#24
Iirc the bug Karpathy mentioned in his tweet was actually due to the seed being the same across multigpu data parallel workers! You need to account for this too. So the author hasnt solved it.

I 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

#25
post #14

This 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

#26

This 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…

"I downloaded over a hundred thousand repositories from GitHub that import PyTorch... Out of these, over 95% of the repositories are plagued by this problem."

Title seems pretty accurate to me!

Re: A common mistake when NumPy’s RNG with PyTorch

#27
post #14

This 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.

I believe the point is that the error will be more obvious if the state is passed around explicitly.

Re: A common mistake when NumPy’s RNG with PyTorch

#28
.NET has a similar pitfall, but not due to forking but rather that the Random() default seed is based on the system clock. So starting several threads constructing new Random objects with the hope that they are unique might in fact give you same RNG sequences.

Re: A common mistake when NumPy’s RNG with PyTorch

#29
post #18

Earlier 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.

I get the desire to be pedantic, but does anyone at all train DL models on Windows? (barring toy projects for fun and perhaps debugging) The same can be said about num_workers > 0. You _have to_ fork worker threads unless you train something super tiny like MNIST and you load the whole dataset on GPU.
Post reply on HN