Skip to content

Conversation

@michael-diggin
Copy link
Contributor

@michael-diggin michael-diggin commented Dec 8, 2024

Fixes #105203 and is a follow up PR to #141833

When in_order is True (the default), tasks are given out to workers in a round robin fashion. When in_order is False this is no longer needed, as we give up guarantees of reproducibility, and instead tasks should be given to workers that are able to perform work.
In this PR I've added tracking of the number of outstanding tasks for each worker (updated when tasks are added to their queue, and when data is returned to the main thread). When finding the next queue to add a task to, if in_order is False it will only add the task to the workers queue if it has fewer than _prefetch_factor tasks outstanding.
The current default behaviour is left as is.

Tests are also updated to assert on the worker IDs for each sample of data returned.
I've run the following to confirm they aren't flaky

for i in {1..20}; do python test/test_dataloader.py TestOutOfOrderDataLoader; done

cc @andrewkho @divyanshk @ssnl @VitalyFedyunin @dzhulgakov

@pytorch-bot
Copy link

pytorch-bot bot commented Dec 8, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/142324

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit a20b7fa with merge base 3f80632 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot bot added the release notes: dataloader release notes category label Dec 8, 2024
@janeyx99 janeyx99 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Dec 9, 2024
@michael-diggin
Copy link
Contributor Author

Hi @andrewkho, @divyanshk, would it be possible to get a review of this follow up PR when you get a chance? Thanks!

@andrewkho andrewkho added the module: dataloader Related to torch.utils.data.DataLoader and Sampler label Dec 17, 2024
@andrewkho
Copy link
Contributor

Sorry for the delay @michael-diggin this slipped off my radar, having a look now

Copy link
Contributor

@andrewkho andrewkho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice diff! One question to think through for IterableDatasets, and probably want to add context in one of the comments for future readers

@michael-diggin
Copy link
Contributor Author

Really nice diff! One question to think through for IterableDatasets, and probably want to add context in one of the comments for future readers

Thanks for the quick review @andrewkho! I've responded to the comments, let me know what you think about the IterableDataset case, or if you've got a different idea in mind that may work better.

Copy link
Contributor

@andrewkho andrewkho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and thank you for adding this! Just a question about potential deadlocks, which I think this is safe from, but not sure if we can have stronger guarantees somehow


def _process_data(self, data):
def _process_data(self, data, worker_idx):
self._workers_num_tasks[worker_idx] -= 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that the way this is set up, we'll never deadlock due to not decrementing this, but is there somewhere we can give stronger guarantees on this?

Copy link
Contributor Author

@michael-diggin michael-diggin Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't deadlock the way it is set up.
_workers_num_tasks is only incremented in try_put_index() which gets called in two places (other than at the very beginning of an epoch to start off a bunch of tasks):

  1. _process_data, where it is decremented first, so that will be safe, this is also the only way _next_data can exit that isn't shutdown/end of epoch
  2. within _next_data when a worker for an IterableDataset is finished, also safe as that's just distributing work

I think the key thing is that _process_data, and hence the decrementing, is pretty much always called by _next_data when it returns, and that decrementing happens before any incrementing.
I can't think of anything off the top of my head that would give stronger guarantees, but maybe a small bit of refactoring could make this more clear (eg incrementing it within _process_data after the call to _try_put_index)? I'll also try to add a new test case that gives more confidence that it won't deadlock too.

@michael-diggin
Copy link
Contributor Author

michael-diggin commented Dec 27, 2024

One of the test failures looked a bit strange, but also unrelated. I've merged in a later commit from main which may help (passes on my local branch now). @andrewkho would you be able to rerun the CI when you get a chance? Thanks!

@michael-diggin
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jan 3, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged module: dataloader Related to torch.utils.data.DataLoader and Sampler open source release notes: dataloader release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pytorch dataloader not loading first-available data with multiple workers

5 participants