Skip to content
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

Add profiling to dataloader next() #12124

Merged
merged 23 commits into from
May 6, 2022

Conversation

akashkw
Copy link
Contributor

@akashkw akashkw commented Feb 26, 2022

What does this PR do?

Adds profiling to next(dataloader_iter) in train, eval, and prediction loops.

Does your PR introduce any breaking changes? If yes, please list them.

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

Did you have fun?

Make sure you had fun coding 🙃

@akashkw akashkw changed the title Add profiling to dataloader Add profiling to dataloader next() Feb 26, 2022
@akashkw akashkw changed the title Add profiling to dataloader next() Add profiling to dataloader next() Feb 26, 2022
@akashkw akashkw marked this pull request as ready for review February 26, 2022 02:41
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

Hi @akashkw!

We cannot profile next() like this as this would not cover prefetched batches or inter-batch paralelism.

I recently had a call with @daniellepintz where I went over possible solutions. Here's some code pointers:

The concrete lines to profile:
https://github.com/PyTorchLightning/pytorch-lightning/blob/a0655611de460f1659b46150cda256d2e3fa974e/pytorch_lightning/utilities/fetching.py#L267

https://github.com/PyTorchLightning/pytorch-lightning/blob/a0655611de460f1659b46150cda256d2e3fa974e/pytorch_lightning/utilities/fetching.py#L336

Option 1:
Pass a profiler reference to the data fetchers, maybe also the stage and dataloader idx for more fine-grained profiling, then use these directly inside the fetching functions
Option 2:
Inject the profiler usage through callables that could be part of these hooks:
https://github.com/PyTorchLightning/pytorch-lightning/blob/a0655611de460f1659b46150cda256d2e3fa974e/pytorch_lightning/utilities/fetching.py#L61-L65
with a pattern similar to the one used in this PR to inject logic around the optimizer.step()
4cc05b2

@carmocca carmocca added this to the 1.7 milestone Feb 28, 2022
@carmocca carmocca self-assigned this Feb 28, 2022
@akashkw akashkw force-pushed the profile-dataloader branch from f878d68 to d1587a6 Compare March 1, 2022 01:30
@mergify mergify bot removed the has conflicts label Mar 1, 2022
@akashkw
Copy link
Contributor Author

akashkw commented Mar 2, 2022

Option 1:
Pass a profiler reference to the data fetchers, maybe also the stage and dataloader idx for more fine-grained profiling, then use these directly inside the fetching functions
Option 2:
Inject the profiler usage through callables that could be part of these hooks:

@carmocca do you have any preference between these two approaches?

@carmocca
Copy link
Contributor

carmocca commented Mar 2, 2022

I prefer option (2) so the fetching does not need to keep a reference to the trainer. (the profiler is owned by the trainer)

@akashkw akashkw requested review from Borda and SeanNaren as code owners March 3, 2022 23:05
@akashkw akashkw force-pushed the profile-dataloader branch from f6109d5 to 12a3ba5 Compare March 3, 2022 23:08
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

Good progress!

pytorch_lightning/loops/epoch/evaluation_epoch_loop.py Outdated Show resolved Hide resolved
pytorch_lightning/loops/epoch/prediction_epoch_loop.py Outdated Show resolved Hide resolved
pytorch_lightning/loops/epoch/evaluation_epoch_loop.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/fetching.py Outdated Show resolved Hide resolved
@@ -485,3 +486,88 @@ def validation_step(self, batch, batch_idx):
assert dm.count_called_on_before_batch_transfer == 4
assert dm.count_called_transfer_batch_to_device == 4
assert dm.count_called_on_after_batch_transfer == 4


@RunIf(skip_windows=True) # TODO: all durations are 0 on Windows
Copy link
Contributor

@carmocca carmocca May 3, 2022

Choose a reason for hiding this comment

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

I'm unsure if this is a known issue with the profiler on Windows or a bug...

Copy link
Contributor

Choose a reason for hiding this comment

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

we should open an issue. It just uses time module. Nothing fancy.

@carmocca carmocca enabled auto-merge (squash) May 3, 2022 17:20
@carmocca carmocca mentioned this pull request May 4, 2022
11 tasks
@mergify mergify bot added the ready PRs ready to be merged label May 4, 2022
pytorch_lightning/utilities/fetching.py Outdated Show resolved Hide resolved
pytorch_lightning/utilities/fetching.py Outdated Show resolved Hide resolved
Co-authored-by: Akihiro Nitta <nitta@akihironitta.com>
@carmocca carmocca requested a review from akihironitta May 5, 2022 14:48
@mergify mergify bot added has conflicts and removed ready PRs ready to be merged labels May 5, 2022
@mergify mergify bot added ready PRs ready to be merged and removed has conflicts ready PRs ready to be merged labels May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data handling Generic data-related topic feature Is an improvement or enhancement profiler ready PRs ready to be merged
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

7 participants