-
Notifications
You must be signed in to change notification settings - Fork 662
add linear lr warmup and lr decay scheduler #23
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
713ccd2
add linear warmup and decay scheduler
lessw2020 9c23af2
ruff check and formatting
lessw2020 39cb348
update lr scheduler to lambdaLR with full linear up and down
lessw2020 4215885
Merge branch 'pytorch-labs:main' into lr_scheduler
lessw2020 697c0f9
Merge branch 'pytorch-labs:main' into lr_scheduler
lessw2020 f42fa47
update with pr feedback
lessw2020 be43463
Merge branch 'lr_scheduler' of github.com:lessw2020/torchtrain into l…
lessw2020 7f088e7
update with pr feedback, ruff format
lessw2020 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
|
|
||
| from torchtrain.logging_utils import rank0_log | ||
|
|
||
|
|
||
| class LinearScheduler: | ||
| def __init__(self, args): | ||
| self.lr_max = args.lr | ||
| self.lr_min = args.lr / 10 | ||
| self.lr_warmup_pct = 0.10 | ||
| # enforce min of 2 steps for warmup | ||
| self.warmup_steps = max(int(args.steps * self.lr_warmup_pct), 2) | ||
|
|
||
| rank0_log( | ||
| f"LR Warmup Schedule: {self.lr_min} -> {self.lr_max} with {self.warmup_steps} warmup steps" | ||
| ) | ||
| self.decay_steps = args.steps - self.warmup_steps | ||
| self.curr_lr = 0 | ||
|
|
||
| def set_lr(self, optimizer, step): | ||
| """Set the learning rate for the optimizer""" | ||
| if step < self.warmup_steps: | ||
| self.curr_lr = self.lr_max * (step / self.warmup_steps) | ||
| else: | ||
| self.curr_lr = self.lr_min + ( | ||
| (self.lr_max - self.lr_min) | ||
| * (1 - (step - self.warmup_steps) / self.decay_steps) | ||
| ) | ||
| # apply across all optim groups | ||
| for param_group in optimizer.param_groups: | ||
| param_group["lr"] = self.curr_lr | ||
| rank0_log(f"Optimizer LR Update: {step=}, lr = {round(self.curr_lr,6)}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.