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

extract optimizer loop #9191

Merged
merged 45 commits into from
Sep 2, 2021
Merged

extract optimizer loop #9191

merged 45 commits into from
Sep 2, 2021

Conversation

awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Aug 29, 2021

What does this PR do?

Part of #7938

Extract an OptimizerLoop from the existing TrainingBatchLoop. This cleanly separates the code path for automatic and manual optimization. Automatic optimization now completely lives inside the OptimizerLoop.

Previously:

(details left out)

if lightning_module.automatic_optimization:
    for opt_idx, opt in enumerate(trainer.optimizers):
        self._run_optimization(batch, opt_idx, opt)
else:
     self._run_optimization(batch, 0, None)

The run_optimization had to handle both code paths for automatic and manual optimization, making it hard to follow, debug, and extend.

Now:

(details left out)

if lightning_module.automatic_optimization:
    self.optimizer.run(batch, optimizers)
else:
     self._run_manual_optimization(batch)

The code path branches here cleanly. The new OptimizerLoop handles automatic optimization completely and the manual optimization path is kept isolated. This greatly improves readability, debugging, and extensibility for loop customization.

A follow-up PR will remove some remaining traces of mixed interfaces, we are holding it out from this PR to make it more manageable to review.

Blocked by:

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)

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 🙃

@awaelchli awaelchli force-pushed the refactor/optimizer-loop branch from f5586c7 to 69a29d9 Compare August 29, 2021 23:08
@awaelchli awaelchli force-pushed the refactor/optimizer-loop branch from fc46e02 to 9226271 Compare August 29, 2021 23:42
@awaelchli awaelchli added this to the v1.5 milestone Aug 30, 2021
@codecov
Copy link

codecov bot commented Aug 31, 2021

Codecov Report

Merging #9191 (8475526) into master (35876bb) will increase coverage by 0%.
The diff coverage is 99%.

@@          Coverage Diff           @@
##           master   #9191   +/-   ##
======================================
  Coverage      92%     92%           
======================================
  Files         176     178    +2     
  Lines       14801   14871   +70     
======================================
+ Hits        13653   13723   +70     
  Misses       1148    1148           

Copy link
Contributor

@SeanNaren SeanNaren left a comment

Choose a reason for hiding this comment

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

LGTM, great work!

@mergify mergify bot added ready PRs ready to be merged and removed has conflicts labels Sep 1, 2021
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.

Great work !

@tchaton tchaton merged commit 7535093 into master Sep 2, 2021
@tchaton tchaton deleted the refactor/optimizer-loop branch September 2, 2021 11:40
@awaelchli awaelchli mentioned this pull request Sep 14, 2021
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready PRs ready to be merged refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants