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

Pass lr_scheduler to Accelerator.prepare #301

Merged
merged 6 commits into from
Mar 31, 2022
Merged

Conversation

sgugger
Copy link
Collaborator

@sgugger sgugger commented Mar 30, 2022

This PR adds support for passing the learning rate scheduler to Accelerator.prepare. This only concerns learning rate schedulers that are called at each optimizer step, not schedulers that are called at the end of each epoch.

Why?

There are three reasons motivating this change:

  • users don't always remember they have to define the scheduler after they have prepared the training dataloader (since its size changes). This way is easier.
  • the scheduler step should be synchronized with the optimizer step, so when the step is skipped for the optimizer (in mixed precision), it should also be in the scheduler. With this PR, this is now the case.
  • this will unlock more cases where the optimizer step is skipped: having Accelerate automatically handle gradient accumulation

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Mar 30, 2022

The documentation is not available anymore as the PR was closed or merged.

Copy link
Collaborator

@muellerzr muellerzr 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 on this! Left a few suggestions for documentation consistency and a copy/paste victim 😉

src/accelerate/accelerator.py Outdated Show resolved Hide resolved
Comment on lines +478 to +481
for opt in self._optimizers:
if getattr(scheduler, "optimizer", None) == opt.optimizer:
optimizer = opt
break
Copy link
Collaborator

Choose a reason for hiding this comment

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

Clever! I had wondered how you'd do this 😄

src/accelerate/checkpointing.py Outdated Show resolved Hide resolved
src/accelerate/scheduler.py Outdated Show resolved Hide resolved
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

As said offline, LGTM!

@sgugger sgugger merged commit 2c554b0 into main Mar 31, 2022
@sgugger sgugger deleted the lr_scheduler_prepare branch March 31, 2022 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants