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

Function build_optimizer

eval.py:132–181  ·  view source on GitHub ↗
(cfg, model)

Source from the content-addressed store, hash-verified

130
131
132def build_optimizer(cfg, model):
133 if cfg is None:
134 return None
135
136 if cfg.get("filter_bias_norm_wd", False):
137 params = param_groups_weight_decay(model, weight_decay=cfg.weight_decay)
138 else:
139 params = model.parameters()
140
141 if cfg.name == "decoupled_adamw":
142 return DecoupledAdamW(params, lr=cfg.lr, betas=list(cfg.betas), eps=cfg.eps, weight_decay=cfg.weight_decay)
143 elif cfg.name == "adamw":
144 print(
145 "INFO: You might want to increase the weight decay because in AdamW it is scaled by the lr."
146 f" Default weight decay is ``1e-2`` -> {cfg.weight_decay}. Default lr is `lr=1e-3` -> {cfg.lr}."
147 )
148 return AdamW(params, lr=cfg.lr, betas=list(cfg.betas), eps=cfg.eps, weight_decay=cfg.weight_decay)
149 elif cfg.name == "stableadamw":
150 try:
151 if cfg.get("log_grad_norm", False):
152 from src.optimizer import StableAdamW
153 else:
154 from optimi import StableAdamW
155 except ImportError:
156 raise ImportError("Install `pip install torch-optimi` to use the StableAdamW optimizer.")
157
158 print(
159 "INFO: You might want to increase the weight decay because in StableAdamW it is scaled by the lr."
160 f" Default weight decay is ``1e-2`` -> {cfg.weight_decay}. Default lr is `lr=1e-3` -> {cfg.lr}."
161 )
162 return StableAdamW(params, lr=cfg.lr, betas=list(cfg.betas), eps=cfg.eps, weight_decay=cfg.weight_decay)
163 elif cfg.name == "decoupled_stableadamw":
164 try:
165 if cfg.get("log_grad_norm", False):
166 from src.optimizer import StableAdamW
167 else:
168 from optimi import StableAdamW
169 except ImportError:
170 raise ImportError("Install `pip install torch-optimi` to use the StableAdamW optimizer.")
171
172 return StableAdamW(
173 params,
174 lr=cfg.lr,
175 betas=list(cfg.betas),
176 eps=cfg.eps,
177 weight_decay=cfg.weight_decay,
178 decouple_lr=True,
179 )
180 else:
181 raise ValueError(f"Not sure how to build optimizer: {cfg.name}")
182
183
184def build_model(cfg: DictConfig, num_labels: int, multiple_choice: bool = False, **kwargs):

Callers 1

run_job_workerFunction · 0.70

Calls 2

StableAdamWClass · 0.90

Tested by

no test coverage detected