-
Notifications
You must be signed in to change notification settings - Fork 16
Add GC and LinearWarmupCosineAnnealingLRScheduler #439
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 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
97d0cfc
chore: temporarily reverted combination of fsdp units
le1nux 40b2de2
feat: added garbage collection
le1nux 534bb46
chore: setting cycle_momentum to False in OneCycleLRSchedulerConfig
le1nux 20c678e
refactor: reintroducing layers_per_fsdp_unit in model_factory for bet…
le1nux d7b1513
feat: added LinearWarmupCosineAnnealingLRScheduler
le1nux 6daad37
chore: added documentatoin of linear warmup cosine annealing learning…
le1nux 48c423d
chore: removed comments
le1nux 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
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
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 |
|---|---|---|
| @@ -1,21 +1,64 @@ | ||
| import warnings | ||
| from typing import Optional | ||
|
|
||
| from torch import Tensor | ||
| from torch.optim import Optimizer | ||
| from torch.optim.lr_scheduler import LRScheduler | ||
| from torch.optim.lr_scheduler import CosineAnnealingLR, LinearLR, LRScheduler, SequentialLR | ||
|
|
||
|
|
||
| class DummyLRScheduler(LRScheduler): | ||
| def __init__(self, optimizer: Optimizer, last_epoch: Optional[int] = -1): | ||
| def __init__(self, optimizer: Optimizer, last_epoch: int = -1): | ||
| super().__init__(optimizer, last_epoch) | ||
|
|
||
| def get_lr(self) -> list[float]: | ||
| def get_lr(self) -> list[float | Tensor]: | ||
| if not self._get_lr_called_within_step: # type error expected due to internal pytorch implementation | ||
| warnings.warn( | ||
| "To get the last learning rate computed by the scheduler, " "please use `get_last_lr()`.", UserWarning | ||
| "To get the last learning rate computed by the scheduler, please use `get_last_lr()`.", | ||
| UserWarning, | ||
| ) | ||
|
|
||
| return [group["lr"] for group in self.optimizer.param_groups] | ||
|
|
||
| def _get_closed_form_lr(self) -> list[float]: | ||
| def _get_closed_form_lr(self) -> list[float | Tensor]: | ||
| return self.base_lrs | ||
|
|
||
|
|
||
| class LRSchedulerFactory: | ||
| @staticmethod | ||
| def get_linear_warmup_cosine_annealing_lr_scheduler( | ||
| optimizer: Optimizer, | ||
| warmup_steps: int, | ||
| total_steps: int, | ||
| initial_lr: float, | ||
| final_lr: float, | ||
| max_lr: float, | ||
| last_epoch: int = -1, | ||
| ) -> SequentialLR: | ||
| if warmup_steps <= 0: | ||
| raise ValueError("warmup_steps must be greater than 0.") | ||
| if total_steps <= warmup_steps: | ||
| raise ValueError("total_steps must be greater than warmup_steps.") | ||
|
|
||
| if not all(base_lr == max_lr for base_lr in [group["lr"] for group in optimizer.param_groups]): | ||
| raise ValueError( | ||
| "All parameter groups must have the same initial_lr." | ||
| "and it must be equal to the initial_lr passed to the LR scheduler factory." | ||
| ) | ||
|
|
||
| warmup_scheduler = LinearLR( | ||
| optimizer=optimizer, | ||
| start_factor=initial_lr / max_lr, | ||
| end_factor=1, | ||
| total_iters=warmup_steps, | ||
| ) | ||
| cosine_scheduler = CosineAnnealingLR( | ||
| optimizer=optimizer, | ||
| T_max=total_steps - warmup_steps, | ||
| eta_min=final_lr, | ||
| ) | ||
|
|
||
| return SequentialLR( | ||
| optimizer=optimizer, | ||
| schedulers=[warmup_scheduler, cosine_scheduler], | ||
| milestones=[warmup_steps], | ||
| last_epoch=last_epoch, | ||
| ) |
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
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
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
Oops, something went wrong.
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.