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

remove redundant iterator call to data fetcher in loops #9117

Merged
merged 4 commits into from
Aug 26, 2021
Merged

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Aug 25, 2021

What does this PR do?

#9052 moved the iterator management from the fit loop to the training epoch loop.
the dataloader gets called with iter() but later on also with enumerate() on top of it. this will not play nicely in #8950 where it's important to track the state of an iterator.

This PR removes the additional iter() call.
A full integration test for iterator state is present in #8950 .

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 update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)
    NOTNEEDED since the change linked above was only introduced in master recently.

PR review

Anyone in the community is free to review the PR once the tests have passed.
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?

I made sure I had fun coding 🙃

@tchaton
Copy link
Contributor

tchaton commented Aug 25, 2021

Hey @awaelchli. Thanks, definitely a good catch !

@tchaton
Copy link
Contributor

tchaton commented Aug 25, 2021

Can we add a test to prevent regression ?

Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

LGMT !

@awaelchli awaelchli changed the title fix redundant iterator call in training epoch loop fix redundant iterator call in fit loop Aug 25, 2021
@tchaton
Copy link
Contributor

tchaton commented Aug 26, 2021

Hey @awaelchli, added the same fix for evaluation loop. Thanks for finding this out.

@awaelchli awaelchli added the bug Something isn't working label Aug 26, 2021
@awaelchli awaelchli added this to the v1.5 milestone Aug 26, 2021
@awaelchli awaelchli added feature Is an improvement or enhancement and removed bug Something isn't working labels Aug 26, 2021
@awaelchli awaelchli marked this pull request as ready for review August 26, 2021 08:06
@mergify mergify bot added the ready PRs ready to be merged label Aug 26, 2021
@mergify mergify bot requested a review from a team August 26, 2021 08:06
@awaelchli awaelchli changed the title fix redundant iterator call in fit loop remove redundant iterator call to data fetcher in loops Aug 26, 2021
@awaelchli
Copy link
Contributor Author

@tchaton I don't exactly know what to test here. This change is needed for #8950 and I'm breaking it out in a separate PR to ensure it passes on its own and to reduce confusion for reviewers #8950.

#8950 adds a full test for both the changes here and there.

@codecov
Copy link

codecov bot commented Aug 26, 2021

Codecov Report

Merging #9117 (f5f1b40) into master (12d076f) will decrease coverage by 0%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #9117    +/-   ##
=======================================
- Coverage      92%     92%    -0%     
=======================================
  Files         176     176            
  Lines       14666   14865   +199     
=======================================
+ Hits        13500   13677   +177     
- Misses       1166    1188    +22     

@awaelchli awaelchli merged commit 02612e1 into master Aug 26, 2021
@awaelchli awaelchli deleted the bugfix/iter branch August 26, 2021 09:36
@tchaton
Copy link
Contributor

tchaton commented Aug 26, 2021

@tchaton I don't exactly know what to test here. This change is needed for #8950 and I'm breaking it out in a separate PR to ensure it passes on its own and to reduce confusion for reviewers #8950.

#8950 adds a full test for both the changes here and there.

Hey @awaelchli. Same here. Hopefully, your test can used to prevent regression.

Copy link

@aazzolini aazzolini left a comment

Choose a reason for hiding this comment

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

Making evaluation_loop depend on fetcher doesn't seem needed to me -- do we need anything other than an iterator proper?

if not isinstance(dataloader_iter, DataLoaderIterDataFetcher):
dataloader_iter = enumerate(dataloader_iter, batch_idx)
# restore iteration
if not isinstance(data_fetcher, DataLoaderIterDataFetcher):

Choose a reason for hiding this comment

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

why not simply move this into the contract of this iterator? basically make the contract such that these data fetchers will always return the batch_idx as part of it.

Copy link
Contributor Author

@awaelchli awaelchli Aug 28, 2021

Choose a reason for hiding this comment

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

yes, I suppose could be done like that :)

@@ -105,9 +105,11 @@ def _process_training_step_output(
return results, hiddens


def _prepare_dataloader_iter(dataloader_iter: Iterator, batch_idx: int) -> Iterator:
def _prepare_dataloader_iter(data_fetcher: AbstractDataFetcher, batch_idx: int) -> Iterator:

Choose a reason for hiding this comment

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

How much do we actually need to expose the "AbstractDataFetcher" here versus just an iterator? It seems to me that we're really not adding much value by exposing a new interface here.

The custom logic below really should be part of the iterator implementation itself as i comment below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, the iterator type annotation would have sufficed here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants