Skip to content

Conversation

@NikolasWolke
Copy link
Contributor

@NikolasWolke NikolasWolke commented Jul 21, 2023

Lightning modules may return None in the training_step, in order to skip the current batch when using automatic optimization. However, the learning rate finder failed in that case.

What does this PR do?

This fix

  • modifies the _LRCallback.on_train_batch_end method to support None from the training step by also handling an empty dict (since _AutomaticOptimization.run turns None STEP_OUTPUT into an empty dict)
  • makes sure, that the list of losses and lrs have the same length as otherwise the selected learning rate would not be correct. If the lightning module returns None in the training step, nan is added to list of losses. because they are ignored when computing the suggestion.

Fixes #17992

Before submitting
  • Was this discussed/agreed 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 minor internal changes/refactors)

PR review

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

Reviewer checklist
  • 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

@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Jul 21, 2023
@NikolasWolke NikolasWolke marked this pull request as ready for review July 21, 2023 12:47
@awaelchli awaelchli added tuner community This PR is from the community labels Jul 21, 2023
@awaelchli awaelchli added this to the 2.0.x milestone Jul 21, 2023
@awaelchli awaelchli added the bug Something isn't working label Jul 21, 2023
@awaelchli awaelchli self-assigned this Jul 21, 2023
Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

Looks great! only minor comments :)

@NikolasWolke NikolasWolke force-pushed the bugfix/17992_support_none_from_training_step_in_lr_finder branch from cb0b3e2 to f7e76a9 Compare July 21, 2023 14:05
Lightning modules may return None in the training_step, in order to skip the current batch when using automatic optimization. However, the learning rate finder failed in that case.

This fix
* modifies the _LRCallback on train batch end method to support None from the training step by also handling an empty dict (since _AutomaticOptimization.run turns None STEP_OUTPUT into an empty dict)
* makes sure, that the list of losses and lrs have the same length as otherwise the selected learning rate would not be correct. If the lightning module returns None in the training step, nan is added to list of losses. because they are ignored when computing the suggestion.
@NikolasWolke
Copy link
Contributor Author

In the last build pl-cpu (macOS-11, lightning, 3.10, 2.0), pl-cpu (ubuntu-20.04, lightning, 3.10, 2.0) and probot failed all simply with Error: . Not sure what to make of that 🤔

Copy link
Contributor

@awaelchli awaelchli left a comment

Choose a reason for hiding this comment

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

Thanks <3

@mergify mergify bot added the ready PRs ready to be merged label Aug 2, 2023
@awaelchli awaelchli requested a review from Borda as a code owner August 2, 2023 10:26
@mergify mergify bot added has conflicts and removed ready PRs ready to be merged labels Aug 3, 2023
awaelchli and others added 2 commits August 3, 2023 15:22
@mergify mergify bot added ready PRs ready to be merged and removed has conflicts labels Aug 3, 2023
@mergify mergify bot added ready PRs ready to be merged and removed ready PRs ready to be merged labels Aug 3, 2023
@awaelchli awaelchli merged commit 28c401c into Lightning-AI:master Aug 3, 2023
Borda pushed a commit that referenced this pull request Aug 14, 2023
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

(cherry picked from commit 28c401c)
lexierule pushed a commit that referenced this pull request Aug 14, 2023
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

(cherry picked from commit 28c401c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working community This PR is from the community pl Generic label for PyTorch Lightning package ready PRs ready to be merged tuner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LR Finder does not support Models that return None in training_step

5 participants