-
Notifications
You must be signed in to change notification settings - Fork 644
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 all 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,39 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
|
|
||
| from torch.optim.lr_scheduler import LambdaLR | ||
|
|
||
| # global states for scheduling | ||
| # these are needed as LambdaLR does not support argument passing | ||
| _warmup_steps = 2 | ||
| _decay_steps = 0 | ||
|
|
||
|
|
||
| def linear_warmup_linear_decay(current_step: int) -> float: | ||
| """Computes linear warmup followed by linear decay. | ||
| Per LambdaLR requirement, this is accomplished by returning | ||
| a multiplicative factor to adjust the learning rate to | ||
| create the desired schedule. | ||
| """ | ||
| if current_step < _warmup_steps: | ||
| # linear warmup | ||
| # 0-indexed step, hence + 1 adjustments | ||
| current_step += 1 | ||
| curr_adjustment = float(current_step / (_warmup_steps + 1)) | ||
|
|
||
| else: | ||
| # linear decay | ||
| normalized_step = _decay_steps - (current_step - _warmup_steps) | ||
| curr_adjustment = 1 - (_decay_steps - normalized_step) / _decay_steps | ||
|
|
||
| return curr_adjustment | ||
|
|
||
|
|
||
| def get_lr_scheduler(optimizer, args): | ||
| """Build a linear warmup and linear decay scheduler""" | ||
| global _warmup_steps, _decay_steps | ||
| _warmup_steps = max(int(args.steps * args.warmup_pct), 2) | ||
| _decay_steps = float(max(1, args.steps - _warmup_steps)) | ||
|
|
||
| warmup_scheduler = LambdaLR(optimizer, lr_lambda=linear_warmup_linear_decay) | ||
| return warmup_scheduler | ||
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.