Skip to content

Conversation

@awaelchli
Copy link
Contributor

@awaelchli awaelchli commented Nov 6, 2022

What does this PR do?

Fixes #15501

PyTorch 1.13 introduced Adam(fused=True|False). It fuses several operations in the step() method of the optimizer to speed up execution (reduced number of kernel launches). However, since certain operations get fused together, we can no longer unscale the gradients independently in case we want to do gradient clipping. With a fused optimizer like Adam, the unscaling gets handled directly in the step method. Furthermore, a call to scaler.unscale_(optimizer) in our precision plugin currently leads to gradients being unscaled twice in the case of fused Adam, which is undesired. We address both issues in this PR.

A more detailed explanation is here.

In summary:

  • If you are using precision=16 AND fused Adam, this PR fixes optimization parity with non-fused Adam
  • If you are using precision=16 AND fused Adam AND also gradient clipping, there will be an error saying it is not supported.

Follow up work:

  • Currently, the added parity test is skipped in GPU CI. We need to upgrade CI to 1.13.

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

None intended.

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 🙃

cc @Borda @carmocca @justusschock @awaelchli @akihironitta

@awaelchli awaelchli added the feature Is an improvement or enhancement label Nov 6, 2022
@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Nov 6, 2022
@awaelchli awaelchli added precision: amp Automatic Mixed Precision optimizer and removed pl Generic label for PyTorch Lightning package labels Nov 6, 2022
@awaelchli awaelchli changed the title Support fused Adam Support fused Adam with mixed precision Nov 6, 2022
@awaelchli awaelchli added this to the v1.9 milestone Nov 6, 2022
@awaelchli awaelchli self-assigned this Nov 6, 2022
@awaelchli awaelchli added the community This PR is from the community label Nov 6, 2022
@awaelchli awaelchli modified the milestones: v1.9, v1.8.x Nov 6, 2022
@github-actions github-actions bot added the pl Generic label for PyTorch Lightning package label Nov 8, 2022
@awaelchli awaelchli marked this pull request as ready for review November 8, 2022 00:58
@awaelchli awaelchli added bug Something isn't working and removed feature Is an improvement or enhancement labels Nov 8, 2022
@mergify mergify bot added the has conflicts label Nov 8, 2022
@mergify mergify bot added ready PRs ready to be merged and removed has conflicts ready PRs ready to be merged labels Nov 8, 2022
@awaelchli awaelchli mentioned this pull request Nov 8, 2022
12 tasks
@awaelchli awaelchli merged commit e4611ef into master Nov 8, 2022
@awaelchli awaelchli deleted the bugfix/fused-optimizers branch November 8, 2022 14:53
Borda pushed a commit that referenced this pull request Nov 8, 2022
Co-authored-by: Carlos Mocholí <[email protected]>
(cherry picked from commit e4611ef)
lexierule pushed a commit that referenced this pull request Nov 10, 2022
Co-authored-by: Carlos Mocholí <[email protected]>
(cherry picked from commit e4611ef)
@awaelchli awaelchli added fun Staff contributions outside working hours - to differentiate from the "community" label and removed community This PR is from the community labels Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fun Staff contributions outside working hours - to differentiate from the "community" label optimization pl Generic label for PyTorch Lightning package precision: amp Automatic Mixed Precision ready PRs ready to be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FusedAdam & PyTorch Lightning AMP

3 participants