A common mistake when NumPy’s RNG with PyTorch
41–50 of 54 posts
Re: A common mistake when NumPy’s RNG with PyTorch
#42Re: A common mistake when NumPy’s RNG with PyTorch
#43Earlier quoted context omitted.
I believe the point is that the error will be more obvious if the state is passed around explicitly.
Possibly but this is the kind of boilerplate which people tend to ignore, especially when a program is non-trivial. It’s really easy to notice if you’re doing something like `seed_rng(); fork();` but once there’s distance and more than one thing being passed around I’d be surprised if you didn’t find the same pattern, perhaps a bit less common. Fundamentally, there two problems: fork() is a performance trick to try t…
Like, they think they're reading from /dev/random, but they're not: they're just running rand() (metaphorically speaking).
Re: A common mistake when NumPy’s RNG with PyTorch
#44Earlier quoted context omitted.
Possibly but this is the kind of boilerplate which people tend to ignore, especially when a program is non-trivial. It’s really easy to notice if you’re doing something like `seed_rng(); fork();` but once there’s distance and more than one thing being passed around I’d be surprised if you didn’t find the same pattern, perhaps a bit less common. Fundamentally, there two problems: fork() is a performance trick to try t…
Additionally, I think people make a hidden assumption that they don't even realize they're making: that when you ask for random numbers from numpy, they're more or less "true" random numbers, not seeded ones. Like, I think the intention of the programmers is just "give me a bunch of random numbers, I don't really care how as long as they're random", and assumes that that is what that numpy function does. But it doesn…
Re: A common mistake when NumPy’s RNG with PyTorch
#45I'm looking at some code that uses random.random() to randomly apply augmentations, I suspect that will have the same issue right?
Re: A common mistake when NumPy’s RNG with PyTorch
#46Re: A common mistake when NumPy’s RNG with PyTorch
#47Yeah, I'd run into this 2 years ago and ended up also reporting an issue on the Centernet repo [1] The solution I have in that issue adapts from the very helpful discussions in the original Pytorch issue [2] `worker_init_fn=lambda id: np.random.seed(torch.initial_seed() // 2*32 + id)` I will admit that this is *very* easy to mess up as evidenced by the fact that examples in the official tutorials for Pytorch and othe…
Re: A common mistake when NumPy’s RNG with PyTorch
#48Re: A common mistake when NumPy’s RNG with PyTorch
#49Yeah, I'd run into this 2 years ago and ended up also reporting an issue on the Centernet repo [1] The solution I have in that issue adapts from the very helpful discussions in the original Pytorch issue [2] `worker_init_fn=lambda id: np.random.seed(torch.initial_seed() // 2*32 + id)` I will admit that this is *very* easy to mess up as evidenced by the fact that examples in the official tutorials for Pytorch and othe…
torch.utils.data.get_worker_info().seed
So I guess something like the below (untested!) could work too: worker_init_fn=lambda id: np.random.seed(torch.utils.data.get_worker_info().seed)Re: A common mistake when NumPy’s RNG with PyTorch
#50Earlier 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.
IMHO cross-platform Python projects should call `multiprocessing.set_start_method('spawn')` to get the same behavior everywhere.