vigneshwar234 commited on
Commit
38f4c5d
·
verified ·
1 Parent(s): 0f2c365

Add source: tmt/training/scheduler.py

Browse files
Files changed (1) hide show
  1. tmt/training/scheduler.py +28 -0
tmt/training/scheduler.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ scheduler.py — Cosine annealing with linear warmup.
3
+ """
4
+ from __future__ import annotations
5
+
6
+ import math
7
+
8
+ from torch.optim import Optimizer
9
+ from torch.optim.lr_scheduler import LambdaLR
10
+
11
+
12
+ def cosine_warmup_scheduler(
13
+ optimizer: Optimizer,
14
+ warmup_steps: int,
15
+ total_steps: int,
16
+ min_lr_ratio: float = 0.1,
17
+ ) -> LambdaLR:
18
+ """
19
+ Linear warmup for `warmup_steps`, then cosine decay to `min_lr_ratio * lr`.
20
+ """
21
+ def lr_lambda(step: int) -> float:
22
+ if step < warmup_steps:
23
+ return step / max(warmup_steps, 1)
24
+ progress = (step - warmup_steps) / max(total_steps - warmup_steps, 1)
25
+ cosine = 0.5 * (1.0 + math.cos(math.pi * progress))
26
+ return min_lr_ratio + (1.0 - min_lr_ratio) * cosine
27
+
28
+ return LambdaLR(optimizer, lr_lambda)