File size: 17,476 Bytes
f90f02b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | """
Part 2: Custom Optimizers from Scratch
Strictly subclassing torch.optim.Optimizer without predefined torch.optim modules.
Covers all required categories from 'Fantastic Pretraining Optimizers and Where to Find Them' (2025):
1. Category 1: AdamW (Decoupled weight decay)
2. Category 2: Adan (Adaptive Nesterov Momentum - Variance-Reduced AdamW)
3. Category 3: Lion (Memory-Efficient Sign-Momentum)
4. Category 4: Muon (Matrix-Based Polar Newton-Schulz Optimizer)
5. Bonus: Sophia (Hessian-Based Clipped Stochastic Optimizer)
"""
import math
from typing import Callable, Iterable, Optional, Tuple, Union, Dict, Any
import torch
from torch.optim import Optimizer
class CustomAdamW(Optimizer):
"""
Category 1: AdamW with decoupled weight decay (Loshchilov & Hutter, 2019).
theta_t = theta_{t-1} - lr * (m_hat / (sqrt(v_hat) + eps) + weight_decay * theta_{t-1})
"""
def __init__(
self,
params: Iterable[torch.nn.Parameter],
lr: float = 1e-3,
betas: Tuple[float, float] = (0.9, 0.999),
eps: float = 1e-8,
weight_decay: float = 0.01
):
if lr < 0.0:
raise ValueError(f"Invalid learning rate: {lr}")
if not 0.0 <= betas[0] < 1.0:
raise ValueError(f"Invalid beta1: {betas[0]}")
if not 0.0 <= betas[1] < 1.0:
raise ValueError(f"Invalid beta2: {betas[1]}")
if eps < 0.0:
raise ValueError(f"Invalid epsilon: {eps}")
if weight_decay < 0.0:
raise ValueError(f"Invalid weight_decay: {weight_decay}")
defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
super().__init__(params, defaults)
@torch.no_grad()
def step(self, closure: Optional[Callable[[], float]] = None) -> Optional[float]:
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
lr = group["lr"]
beta1, beta2 = group["betas"]
eps = group["eps"]
wd = group["weight_decay"]
for p in group["params"]:
if p.grad is None:
continue
grad = p.grad
if grad.is_sparse:
raise RuntimeError("CustomAdamW does not support sparse gradients")
state = self.state[p]
if len(state) == 0:
state["step"] = 0
state["exp_avg"] = torch.zeros_like(p, memory_format=torch.preserve_format)
state["exp_avg_sq"] = torch.zeros_like(p, memory_format=torch.preserve_format)
state["step"] += 1
step = state["step"]
exp_avg = state["exp_avg"]
exp_avg_sq = state["exp_avg_sq"]
# 1. Perform decoupled weight decay
if wd != 0.0:
p.mul_(1.0 - lr * wd)
# 2. Update biased 1st and 2nd moment estimates
exp_avg.mul_(beta1).add_(grad, alpha=1.0 - beta1)
exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1.0 - beta2)
# 3. Bias corrections
bias_correction1 = 1.0 - beta1 ** step
bias_correction2 = 1.0 - beta2 ** step
step_size = lr / bias_correction1
denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(eps)
p.addcdiv_(exp_avg, denom, value=-step_size)
return loss
class CustomAdan(Optimizer):
"""
Category 2: Adan (Adaptive Nesterov Momentum Algorithm, Xie et al., 2022).
Variance-reduced AdamW variant incorporating gradient differences to estimate
future gradient trajectory and damp oscillations.
"""
def __init__(
self,
params: Iterable[torch.nn.Parameter],
lr: float = 1e-3,
betas: Tuple[float, float, float] = (0.98, 0.92, 0.99),
eps: float = 1e-8,
weight_decay: float = 0.02
):
if lr < 0.0:
raise ValueError(f"Invalid learning rate: {lr}")
if eps < 0.0:
raise ValueError(f"Invalid epsilon: {eps}")
defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
super().__init__(params, defaults)
@torch.no_grad()
def step(self, closure: Optional[Callable[[], float]] = None) -> Optional[float]:
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
lr = group["lr"]
beta1, beta2, beta3 = group["betas"]
eps = group["eps"]
wd = group["weight_decay"]
for p in group["params"]:
if p.grad is None:
continue
grad = p.grad
if grad.is_sparse:
raise RuntimeError("CustomAdan does not support sparse gradients")
state = self.state[p]
if len(state) == 0:
state["step"] = 0
state["exp_avg"] = torch.zeros_like(p) # m_k: EMA of grad
state["exp_avg_diff"] = torch.zeros_like(p) # v_k: EMA of grad diff
state["exp_avg_sq"] = torch.zeros_like(p) # n_k: second moment
state["prev_grad"] = grad.clone()
state["step"] += 1
step = state["step"]
exp_avg = state["exp_avg"]
exp_avg_diff = state["exp_avg_diff"]
exp_avg_sq = state["exp_avg_sq"]
prev_grad = state["prev_grad"]
b1 = beta1 if beta1 > 0.5 else 1.0 - beta1
b2 = beta2 if beta2 > 0.5 else 1.0 - beta2
b3 = beta3 if beta3 > 0.5 else 1.0 - beta3
grad_diff = grad - prev_grad
# Update 1st moment of grad
exp_avg.mul_(b1).add_(grad, alpha=1.0 - b1)
# Update 1st moment of grad difference
exp_avg_diff.mul_(b2).add_(grad_diff, alpha=1.0 - b2)
# Update 2nd moment of update vector
update_term = grad + (1.0 - b2) * grad_diff
exp_avg_sq.mul_(b3).addcmul_(update_term, update_term, value=1.0 - b3)
bias_correction1 = 1.0 - b1 ** step
bias_correction2 = 1.0 - b2 ** step
bias_correction3 = 1.0 - b3 ** step
m_hat = exp_avg / bias_correction1
v_hat = exp_avg_diff / bias_correction2
n_hat = exp_avg_sq / bias_correction3
denom = n_hat.sqrt().add_(eps)
step_direction = (m_hat + (1.0 - b2) * v_hat) / denom
# Apply parameter update and decoupled weight decay
p.data.add_(step_direction, alpha=-lr)
if wd != 0.0:
p.data.div_(1.0 + lr * wd)
prev_grad.copy_(grad)
return loss
class CustomLion(Optimizer):
"""
Category 3: Lion (EvoLved Sign Momentum, Chen et al., Google Brain 2023).
Memory-efficient optimizer tracking only 1 momentum buffer (half the memory of AdamW).
Uses coordinate-wise sign operation for uniform step magnitude.
"""
def __init__(
self,
params: Iterable[torch.nn.Parameter],
lr: float = 1e-4,
betas: Tuple[float, float] = (0.9, 0.99),
weight_decay: float = 0.1
):
if lr < 0.0:
raise ValueError(f"Invalid learning rate: {lr}")
defaults = dict(lr=lr, betas=betas, weight_decay=weight_decay)
super().__init__(params, defaults)
@torch.no_grad()
def step(self, closure: Optional[Callable[[], float]] = None) -> Optional[float]:
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
lr = group["lr"]
beta1, beta2 = group["betas"]
wd = group["weight_decay"]
for p in group["params"]:
if p.grad is None:
continue
grad = p.grad
if grad.is_sparse:
raise RuntimeError("CustomLion does not support sparse gradients")
state = self.state[p]
if len(state) == 0:
state["step"] = 0
state["exp_avg"] = torch.zeros_like(p)
state["step"] += 1
exp_avg = state["exp_avg"]
# 1. Weight decay
if wd != 0.0:
p.mul_(1.0 - lr * wd)
# 2. Lion update direction: c_t = beta1 * m_{t-1} + (1 - beta1) * grad
update = exp_avg.clone().mul_(beta1).add_(grad, alpha=1.0 - beta1)
p.add_(torch.sign(update), alpha=-lr)
# 3. Update momentum buffer: m_t = beta2 * m_{t-1} + (1 - beta2) * grad
exp_avg.mul_(beta2).add_(grad, alpha=1.0 - beta2)
return loss
def newton_schulz5(G: torch.Tensor, steps: int = 5, eps: float = 1e-7) -> torch.Tensor:
"""
Quintic Newton-Schulz iteration for matrix orthogonalization (Muon).
Computes an approximate polar decomposition (orthogonal projection of G).
"""
assert G.ndim == 2
a, b, c = (3.4445, -4.7750, 2.0315)
X = G.bfloat16() if G.is_cuda else G.float()
X /= (X.norm() + eps)
if X.size(0) > X.size(1):
X = X.T
transposed = True
else:
transposed = False
for _ in range(steps):
A = X @ X.T
B = b * A + c * A @ A
X = a * X + B @ X
if transposed:
X = X.T
return X.to(G.dtype)
class CustomMuon(Optimizer):
"""
Category 4: Muon (Momentum Orthogonalized by Newton-Schulz, 2024/2025).
Matrix-based optimizer. Applies orthogonalized momentum to 2D weight matrices,
while falling back to standard AdamW for 1D vectors and non-matrix parameters.
"""
def __init__(
self,
params: Iterable[torch.nn.Parameter],
lr: float = 0.02,
momentum: float = 0.95,
nesterov: bool = True,
ns_steps: int = 5,
weight_decay: float = 0.01,
fallback_lr: float = 1e-3
):
defaults = dict(
lr=lr,
momentum=momentum,
nesterov=nesterov,
ns_steps=ns_steps,
weight_decay=weight_decay,
fallback_lr=fallback_lr,
betas=(0.9, 0.999),
eps=1e-8
)
super().__init__(params, defaults)
@torch.no_grad()
def step(self, closure: Optional[Callable[[], float]] = None) -> Optional[float]:
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
lr = group["lr"]
mu = group["momentum"]
nesterov = group["nesterov"]
ns_steps = group["ns_steps"]
wd = group["weight_decay"]
fallback_lr = group["fallback_lr"]
for p in group["params"]:
if p.grad is None:
continue
grad = p.grad
state = self.state[p]
is_matrix = (p.ndim == 2 and min(p.shape) >= 2)
if len(state) == 0:
state["step"] = 0
state["exp_avg"] = torch.zeros_like(p)
if not is_matrix:
state["exp_avg_sq"] = torch.zeros_like(p)
state["step"] += 1
step = state["step"]
if is_matrix:
# 2D Matrix parameter: Muon Newton-Schulz update
exp_avg = state["exp_avg"]
if wd != 0.0:
p.mul_(1.0 - lr * wd)
exp_avg.mul_(mu).add_(grad)
if nesterov:
buf = grad + mu * exp_avg
else:
buf = exp_avg
ortho_buf = newton_schulz5(buf, steps=ns_steps)
# RMS-matched scaling: scales update by sqrt(max(n, m)) to match unit RMS
scale = max(1.0, float(max(p.shape[-2:])) ** 0.5)
p.add_(ortho_buf, alpha=-lr * scale)
else:
# 1D/Non-matrix parameter: Standard AdamW fallback
exp_avg = state["exp_avg"]
exp_avg_sq = state["exp_avg_sq"]
beta1, beta2 = group["betas"]
eps = group["eps"]
if wd != 0.0:
p.mul_(1.0 - fallback_lr * wd)
exp_avg.mul_(beta1).add_(grad, alpha=1.0 - beta1)
exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1.0 - beta2)
bias_correction1 = 1.0 - beta1 ** step
bias_correction2 = 1.0 - beta2 ** step
step_size = fallback_lr / bias_correction1
denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(eps)
p.addcdiv_(exp_avg, denom, value=-step_size)
return loss
class CustomSophia(Optimizer):
"""
Bonus: Sophia (Second-order Clipped Stochastic Optimization, Liu et al., 2023).
Hessian-based optimizer utilizing EMA of diagonal Hessian estimates and coordinate-wise clipping.
"""
def __init__(
self,
params: Iterable[torch.nn.Parameter],
lr: float = 1e-3,
betas: Tuple[float, float] = (0.96, 0.99),
rho: float = 1.0,
gamma: float = 0.01,
eps: float = 1e-12,
weight_decay: float = 0.01
):
if lr < 0.0:
raise ValueError(f"Invalid learning rate: {lr}")
if rho <= 0.0:
raise ValueError(f"Invalid rho: {rho}")
defaults = dict(
lr=lr,
betas=betas,
rho=rho,
gamma=gamma,
eps=eps,
weight_decay=weight_decay
)
super().__init__(params, defaults)
def update_hessian(self) -> None:
"""
Updates diagonal Hessian estimate using Hutchinson's estimator or gradient-squared approximation.
"""
for group in self.param_groups:
beta2 = group["betas"][1]
for p in group["params"]:
if p.grad is None:
continue
state = self.state[p]
if "hessian" not in state:
state["hessian"] = torch.zeros_like(p)
state["hessian_step"] = 0
state["hessian_step"] += 1
state["hessian_called_externally"] = True
# Diagonal Hessian approximation via stochastic gradient outer product
h_hat = p.grad.pow(2)
state["hessian"].mul_(beta2).add_(h_hat, alpha=1.0 - beta2)
@torch.no_grad()
def step(self, closure: Optional[Callable[[], float]] = None) -> Optional[float]:
loss = None
if closure is not None:
with torch.enable_grad():
loss = closure()
for group in self.param_groups:
lr = group["lr"]
beta1, beta2 = group["betas"]
rho = group["rho"]
gamma = group["gamma"]
eps = group["eps"]
wd = group["weight_decay"]
for p in group["params"]:
if p.grad is None:
continue
grad = p.grad
if grad.is_sparse:
raise RuntimeError("CustomSophia does not support sparse gradients")
state = self.state[p]
if "step" not in state:
state["step"] = 0
state["exp_avg"] = torch.zeros_like(p)
if "hessian" not in state:
state["hessian"] = torch.zeros_like(p)
state["hessian_step"] = 0
state["step"] += 1
step = state["step"]
exp_avg = state["exp_avg"]
hessian = state["hessian"]
# 1. Weight decay
if wd != 0.0:
p.mul_(1.0 - lr * wd)
# 2. First moment update
exp_avg.mul_(beta1).add_(grad, alpha=1.0 - beta1)
# 3. Bias corrections
bias_correction1 = 1.0 - beta1 ** step
m_hat = exp_avg / bias_correction1
if state.get("hessian_called_externally", False):
h_step = max(1, state.get("hessian_step", 1))
bias_correction2 = 1.0 - beta2 ** h_step
h_hat = hessian / bias_correction2
else:
# Dynamically update diagonal Hessian estimate from grad^2 each step
hessian.mul_(beta2).add_(grad.pow(2), alpha=1.0 - beta2)
bias_correction2 = 1.0 - beta2 ** step
h_hat = hessian / bias_correction2
# 4. Scaled and clipped update step (Sophia: clip(m_hat / max(h_hat, gamma), rho))
denom = torch.clamp(h_hat, min=gamma)
scaled_step = torch.clamp(m_hat / denom, min=-rho, max=rho)
p.add_(scaled_step, alpha=-lr)
return loss
|