MCPcopy Create free account
hub / github.com/AnswerDotAI/ModernBERT / StableAdamW

Class StableAdamW

src/optimizer.py:30–180  ·  view source on GitHub ↗

StableAdamW optimizer. An AdamW-Adafactor hybrid with learning rate update clipping. This version is modified to only run foreach which has the option to return the model's l1 and l2 grad norm. This only works because the bert24 model is being trained with DDP and the gradients are synced a

Source from the content-addressed store, hash-verified

28
29
30class StableAdamW(OptimiOptimizer):
31 """StableAdamW optimizer. An AdamW-Adafactor hybrid with learning rate update clipping.
32
33 This version is modified to only run foreach which has the option to return the model's l1 and l2 grad norm.
34 This only works because the bert24 model is being trained with DDP and the gradients are synced across all devices.
35 If trained with FSDP, then this will not work.
36
37 Args:
38 params: Iterable of parameters to optimize or dicts defining parameter groups
39 lr: Learning rate
40 betas: Coefficients for gradient and squared gradient moving averages (default: (0.9, 0.99))
41 weight_decay: Weight decay coefficient. If `decouple_lr` is False, applies decoupled weight
42 decay (default: 1e-2)
43 eps: Added to denominator to improve numerical stability (default: 1e-6)
44 decouple_lr: Apply fully decoupled weight decay instead of decoupled weight decay
45 (default: False)
46 max_lr: Maximum scheduled learning rate. Set if `lr` is not the maximum scheduled learning
47 rate and `decouple_lr` is True (default: None)
48 kahan_sum: Enables Kahan summation for more accurate parameter updates when training in low
49 precision (float16 or bfloat16). If unspecified, automatically applies for low precision
50 parameters (default: None)
51 """
52
53 def __init__(
54 self,
55 params: Iterable[Tensor] | Iterable[dict],
56 lr: float,
57 betas: tuple[float, float] = (0.9, 0.99),
58 weight_decay: float = 1e-2,
59 eps: float = 1e-6,
60 decouple_lr: bool = False,
61 max_lr: float | None = None,
62 kahan_sum: bool | None = None,
63 return_norms: bool = True,
64 ):
65 if not 0.0 <= betas[0] < 1.0:
66 raise ValueError(f"Invalid beta1 parameter: {betas[0]=}")
67 if not 0.0 <= betas[1] < 1.0:
68 raise ValueError(f"Invalid beta2 parameter: {betas[1]=}")
69 if not 0.0 <= eps:
70 raise ValueError(f"Invalid epsilon: {eps=}")
71
72 defaults = dict(
73 lr=lr,
74 beta1=betas[0],
75 beta2=betas[1],
76 eps=eps,
77 weight_decay=weight_decay,
78 decouple_lr=decouple_lr,
79 max_lr=max_lr,
80 kahan_sum=kahan_sum,
81 foreach=True,
82 gradient_release=False,
83 setup=False,
84 )
85 super().__init__(params, defaults)
86 self.return_norms = return_norms
87 self.grad_norms = {}

Callers 2

build_optimizerFunction · 0.90
build_optimizerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected