Commit 886790b
authored
Validate warmup_type in WarmupCosineLR like WarmupLR (#8151)
## Problem
`WarmupLR.__init__` validates `warmup_type` and falls back to `log` with
a warning for unknown values, and `WarmupDecayLR` inherits that
behavior. `WarmupCosineLR` documents the same `{'log', 'linear'}`
contract but stores `warmup_type` unvalidated, and `get_lr_ratio()`'s
warmup branch only assigns `ratio` for the two known types. Any other
value (e.g. a typo like `'Linear'` or `'cosine'`) crashes on the first
`step()`:
```
File "deepspeed/runtime/lr_schedules.py", line 850, in get_lr_ratio
ratio = self.warmup_min_ratio + ratio * ratio_delta
UnboundLocalError: cannot access local variable 'ratio' where it is not associated with a value
```
a confusing crash deep inside the scheduler instead of the documented
warn-and-default behavior its sibling classes give.
## Fix
Normalize `warmup_type` in `WarmupCosineLR.__init__` exactly as
`WarmupLR.__init__` already does (same warning text, same fallback to
`log`). Behavior for valid `log`/`linear` values is unchanged.
## Testing
Added `test_warmup_cosine_lr_unknown_warmup_type_falls_back_to_log`,
which fails with the `UnboundLocalError` above on current master and
passes with this change; it asserts an unknown `warmup_type` produces
the same lr-ratio trajectory as an explicit `log` scheduler through
warmup and into cosine decay.
```
pytest tests/unit/runtime/test_lr_schedulers.py -k "warmup_cosine or reject_invalid"
9 passed, 45 deselected
```
yapf/flake8 clean on both touched files. Follows up on the recent
scheduler hardening in #8126 and #8142, which did not cover
`warmup_type`.
---------
Signed-off-by: Sohum Trivedi <trivsohum@gmail.com>1 parent 0c36f6d commit 886790b
2 files changed
Lines changed: 55 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
822 | 822 | | |
823 | 823 | | |
824 | 824 | | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
825 | 830 | | |
826 | 831 | | |
827 | 832 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
577 | 577 | | |
578 | 578 | | |
579 | 579 | | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
0 commit comments