-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add default PyTorch seeding and worker_init_fn to DataLoader #4018
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks good, but we should be careful with passing functions around. lambdas aren't picklable
torch/utils/data/dataloader.py
Outdated
| _set_worker_signal_handlers() | ||
|
|
||
| torch.set_num_threads(1) | ||
| torch.manual_seed(torch.initial_seed() + id + 1) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/utils/data/dataloader.py
Outdated
|
|
||
|
|
||
| def _worker_loop(dataset, index_queue, data_queue, collate_fn): | ||
| def _worker_loop(dataset, index_queue, data_queue, collate_fn, init_fn, id): |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/utils/data/dataloader.py
Outdated
| args=(self.dataset, self.index_queue, self.worker_result_queue, self.collate_fn)) | ||
| for _ in range(self.num_workers)] | ||
| args=(self.dataset, self.index_queue, self.worker_result_queue, self.collate_fn, | ||
| self.worker_init_fn, i)) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
Does this fix #3880 completely? As I understand it, at each epoch the worker processes are created fresh, so if the main process never calls random functions, |
torch/utils/data/dataloader.py
Outdated
| this value in :attr:`worker_init_fn`, which can be used to set other seeds | ||
| (e.g. NumPy) before data loading. | ||
| .. warning:: If ``spawn'' start method is used, :attr:`worker_init_fn` cannot be a lambda |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but would be nice to have one more test.
torch/utils/data/dataloader.py
Outdated
| self.done_event.set() | ||
| # if worker_manager_thread is waiting to put | ||
| while not self.data_queue.empty(): | ||
| while self.data_queue is not None and not self.data_queue.empty(): |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/utils/data/dataloader.py
Outdated
| this value in :attr:`worker_init_fn`, which can be used to set other seeds | ||
| (e.g. NumPy) before data loading. | ||
| .. warning:: If ``spawn'' start method is used, :attr:`worker_init_fn` cannot be a lambda |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| worker_init_fn=init_fn) | ||
| for batch in dataloader: | ||
| self.assertEqual(12345, batch[0]) | ||
| self.assertEqual(12345, batch[1]) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
test/test_dataloader.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/utils/data/dataloader.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
torch/utils/data/dataloader.py
Outdated
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
* Add default PyTorch seeding and worker_init_fn to DataLoader * generate seed using current RNG each time * worker_seed <- main_proc_RNG_generated_seed + worker_id
Fixes #3880 .