feat: v11 CHIMERA GENESIS — Grokfast-EMA + fused loss + LLRD + kill EMA distill overhead\n\nMajor rewrite of training step:\n\n1. P18 Grokfast-EMA (arxiv 2405.20233): 43× convergence acceleration.\n Amplifies slow gradient components (generalization signal),\n filters fast components (memorization/STE noise). 5 lines, 0 overhead.\n Especially powerful for ternary STE where gradient noise is high.\n\n2. FUSED LOSS: P15 Token Triage + P17 Batch Metabolism now COMBINE\n instead of elif. Token triage weights individual tokens, batch\n metabolism weights sequences. Multiplicative composition.\n\n3. P19 Layer-wise LR Decay: higher LR for top layers (task-specific),\n lower for bottom (general features). decay_rate=0.85 per layer.\n Proven for ternary by TernaryLM (arxiv 2602.07374).\n\n4. REMOVED EMA Self-Distillation: doubled forward pass time for marginal\n gain. The EMA model copy consumed 227M params of memory for a KL loss\n that barely helps in from-scratch pretraining (Baby Llama recipe was\n for fine-tuning with a DIFFERENT teacher, not self-EMA)."
Browse files- chimera_turbo.py +191 -226
chimera_turbo.py
CHANGED
|
@@ -1,34 +1,40 @@
|
|
| 1 |
"""
|
| 2 |
-
chimera_turbo.py —
|
| 3 |
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
-
import copy
|
| 10 |
import math
|
| 11 |
import os
|
| 12 |
-
import warnings
|
| 13 |
import torch
|
| 14 |
import torch.nn as nn
|
| 15 |
import torch.nn.functional as F
|
| 16 |
-
from typing import Optional, Dict, Any, Tuple
|
| 17 |
from contextlib import nullcontext
|
| 18 |
from collections import deque
|
| 19 |
|
| 20 |
|
| 21 |
# ═══════════════════════════════════════════════════════════
|
| 22 |
-
# CPU
|
| 23 |
# ═══════════════════════════════════════════════════════════
|
| 24 |
|
| 25 |
def detect_cpu_info():
|
| 26 |
info = {}
|
| 27 |
try:
|
| 28 |
import multiprocessing
|
| 29 |
-
|
| 30 |
-
physical = len(os.sched_getaffinity(0))
|
| 31 |
-
info["physical_cores"] = logical // 2 if logical == physical else physical
|
| 32 |
except Exception:
|
| 33 |
import multiprocessing
|
| 34 |
info["physical_cores"] = multiprocessing.cpu_count() // 2
|
|
@@ -36,11 +42,6 @@ def detect_cpu_info():
|
|
| 36 |
info["capability"] = torch.backends.cpu.get_cpu_capability()
|
| 37 |
except Exception:
|
| 38 |
info["capability"] = "unknown"
|
| 39 |
-
try:
|
| 40 |
-
import intel_extension_for_pytorch
|
| 41 |
-
info["ipex_available"] = True
|
| 42 |
-
except Exception:
|
| 43 |
-
info["ipex_available"] = False
|
| 44 |
info["tcmalloc"] = "tcmalloc" in os.environ.get("LD_PRELOAD", "")
|
| 45 |
return info
|
| 46 |
|
|
@@ -49,12 +50,11 @@ def configure_threading(cpu_info, reserve=1):
|
|
| 49 |
n = max(1, cpu_info["physical_cores"] - reserve)
|
| 50 |
torch.set_num_threads(n)
|
| 51 |
os.environ["OMP_NUM_THREADS"] = str(n)
|
| 52 |
-
os.environ["MKL_NUM_THREADS"] = str(n)
|
| 53 |
return n
|
| 54 |
|
| 55 |
|
| 56 |
# ═══════════════════════════════════════════════════════════
|
| 57 |
-
# P12 — Muon Optimizer
|
| 58 |
# ═══════════════════════════════════════════════════════════
|
| 59 |
|
| 60 |
def _zeropower_via_newtonschulz5(G, steps=5):
|
|
@@ -69,6 +69,7 @@ def _zeropower_via_newtonschulz5(G, steps=5):
|
|
| 69 |
|
| 70 |
|
| 71 |
class Muon(torch.optim.Optimizer):
|
|
|
|
| 72 |
def __init__(self, params, lr=0.02, momentum=0.95, nesterov=True,
|
| 73 |
ns_steps=5, weight_decay=0.0,
|
| 74 |
adamw_betas=(0.9, 0.98), adamw_eps=1e-8):
|
|
@@ -80,7 +81,8 @@ class Muon(torch.optim.Optimizer):
|
|
| 80 |
@torch.no_grad()
|
| 81 |
def step(self):
|
| 82 |
for group in self.param_groups:
|
| 83 |
-
lr
|
|
|
|
| 84 |
b1, b2 = group["adamw_betas"]
|
| 85 |
for p in group["params"]:
|
| 86 |
if p.grad is None:
|
|
@@ -110,20 +112,51 @@ class Muon(torch.optim.Optimizer):
|
|
| 110 |
p.addcdiv_(s["m"], s["v"].sqrt().add_(group["adamw_eps"]), value=-alr)
|
| 111 |
|
| 112 |
|
| 113 |
-
def create_muon_optimizer(model, lr=0.02, momentum=0.95, weight_decay=0.01
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
for name, p in model.named_parameters():
|
| 116 |
if not p.requires_grad:
|
| 117 |
continue
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
p._is_embed = True
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
weight_decay=weight_decay, adamw_betas=(0.9, 0.98))
|
| 123 |
|
| 124 |
|
| 125 |
# ═══════════════════════════════════════════════════════════
|
| 126 |
-
# P13 — Multi-Token Prediction
|
| 127 |
# ═══════════════════════════════════════════════════════════
|
| 128 |
|
| 129 |
class MultiTokenPredictionLoss(nn.Module):
|
|
@@ -145,9 +178,8 @@ class MultiTokenPredictionLoss(nn.Module):
|
|
| 145 |
logits = head(hidden_states[:, :-shift])
|
| 146 |
targets = labels[:, shift:]
|
| 147 |
sl = min(logits.size(1), targets.size(1))
|
| 148 |
-
loss = F.cross_entropy(
|
| 149 |
-
|
| 150 |
-
targets[:, :sl].reshape(-1), ignore_index=-100)
|
| 151 |
if torch.isfinite(loss):
|
| 152 |
total = total + loss
|
| 153 |
count += 1
|
|
@@ -155,104 +187,34 @@ class MultiTokenPredictionLoss(nn.Module):
|
|
| 155 |
|
| 156 |
|
| 157 |
# ═══════════════════════════════════════════════════════════
|
| 158 |
-
#
|
| 159 |
-
# ═══════════════════════════════════════════════════════════
|
| 160 |
-
|
| 161 |
-
class EMASelfDistiller:
|
| 162 |
-
def __init__(self, model, decay=0.999, alpha=0.5, temperature=2.0):
|
| 163 |
-
self.decay, self.alpha, self.temperature = decay, alpha, temperature
|
| 164 |
-
self.ema_model = copy.deepcopy(model)
|
| 165 |
-
for p in self.ema_model.parameters():
|
| 166 |
-
p.requires_grad_(False)
|
| 167 |
-
self.ema_model.eval()
|
| 168 |
-
|
| 169 |
-
@torch.no_grad()
|
| 170 |
-
def update(self, model):
|
| 171 |
-
for p_ema, p in zip(self.ema_model.parameters(), model.parameters()):
|
| 172 |
-
p_ema.data.mul_(self.decay).add_(p.data, alpha=1 - self.decay)
|
| 173 |
-
|
| 174 |
-
def distillation_loss(self, student_logits, hard_targets, input_ids):
|
| 175 |
-
T = self.temperature
|
| 176 |
-
sl = min(student_logits.size(1), hard_targets.size(1))
|
| 177 |
-
hard_loss = F.cross_entropy(
|
| 178 |
-
student_logits[:, :sl].reshape(-1, student_logits.size(-1)),
|
| 179 |
-
hard_targets[:, :sl].reshape(-1), ignore_index=-100)
|
| 180 |
-
with torch.no_grad():
|
| 181 |
-
t_out = self.ema_model(input_ids)
|
| 182 |
-
t_logits = t_out.logits if hasattr(t_out, "logits") else t_out[1]
|
| 183 |
-
tsl = min(student_logits.size(1), t_logits.size(1))
|
| 184 |
-
soft_s = F.log_softmax(student_logits[:, :tsl] / T, dim=-1)
|
| 185 |
-
soft_t = F.softmax(t_logits[:, :tsl] / T, dim=-1)
|
| 186 |
-
kl = F.kl_div(soft_s, soft_t, reduction="batchmean") * T * T
|
| 187 |
-
if not torch.isfinite(kl):
|
| 188 |
-
return hard_loss
|
| 189 |
-
return self.alpha * hard_loss + (1 - self.alpha) * kl
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
# ═══════════════════════════════════════════════════════════
|
| 193 |
-
# P15 — Token Triage (inspiré Rho-1, arxiv 2404.07965)
|
| 194 |
# ═══════════════════════════════════════════════════════════
|
| 195 |
|
| 196 |
class TokenTriage:
|
| 197 |
-
"""Selective token-level gradient weighting without a reference model.
|
| 198 |
-
|
| 199 |
-
Instead of a separate reference model (expensive), use a running EMA
|
| 200 |
-
of per-token loss as the "expected" loss baseline. Tokens with excess
|
| 201 |
-
loss (actual - EMA) above the 40th percentile get full gradient;
|
| 202 |
-
tokens below get 10% gradient. This focuses ~90% of learning on the
|
| 203 |
-
actually-informative tokens.
|
| 204 |
-
|
| 205 |
-
Inspired by Rho-1 (arxiv 2404.07965) but self-referential: the model
|
| 206 |
-
IS its own reference, via temporal smoothing.
|
| 207 |
-
"""
|
| 208 |
def __init__(self, ema_decay=0.99, select_ratio=0.6, floor_weight=0.1):
|
| 209 |
self.ema_decay = ema_decay
|
| 210 |
-
self.select_ratio = select_ratio
|
| 211 |
-
self.floor_weight = floor_weight
|
| 212 |
-
self._loss_ema = None
|
| 213 |
-
|
| 214 |
-
def weighted_loss(self, logits, targets):
|
| 215 |
-
"""Compute token-weighted CE loss.
|
| 216 |
-
|
| 217 |
-
Returns weighted loss where informative tokens contribute more.
|
| 218 |
-
"""
|
| 219 |
-
B, T, V = logits.shape
|
| 220 |
-
# Per-token loss (no reduction)
|
| 221 |
-
per_token = F.cross_entropy(
|
| 222 |
-
logits.reshape(-1, V), targets.reshape(-1),
|
| 223 |
-
ignore_index=-100, reduction="none"
|
| 224 |
-
).reshape(B, T)
|
| 225 |
|
|
|
|
|
|
|
| 226 |
with torch.no_grad():
|
| 227 |
-
mean_loss =
|
| 228 |
if self._loss_ema is None:
|
| 229 |
self._loss_ema = mean_loss
|
| 230 |
else:
|
| 231 |
self._loss_ema = self.ema_decay * self._loss_ema + (1 - self.ema_decay) * mean_loss
|
| 232 |
-
|
| 233 |
-
# Excess loss = how much harder this token is than expected
|
| 234 |
-
excess = per_token - self._loss_ema
|
| 235 |
-
|
| 236 |
-
# Top select_ratio% by excess loss → weight 1.0, rest → floor_weight
|
| 237 |
threshold = torch.quantile(excess.flatten(), 1.0 - self.select_ratio)
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
# Weighted mean
|
| 241 |
-
return (per_token * weights).sum() / weights.sum()
|
| 242 |
|
| 243 |
|
| 244 |
# ═══════════════════════════════════════════════════════════
|
| 245 |
-
# P16 — Plateau Breaker
|
| 246 |
# ═══════════════════════════════════════════════════════════
|
| 247 |
|
| 248 |
class PlateauBreaker:
|
| 249 |
-
"""Detect loss plateaus and inject LR boosts to escape.
|
| 250 |
-
|
| 251 |
-
Tracks loss variance over a window. When variance drops below a
|
| 252 |
-
threshold for patience steps, temporarily boosts LR by multiplier
|
| 253 |
-
for burst_steps, then decays back. Like SGDR warm restarts but
|
| 254 |
-
triggered adaptively by loss stagnation.
|
| 255 |
-
"""
|
| 256 |
def __init__(self, patience=100, variance_threshold=0.005,
|
| 257 |
lr_multiplier=3.0, burst_steps=50):
|
| 258 |
self.patience = patience
|
|
@@ -266,13 +228,9 @@ class PlateauBreaker:
|
|
| 266 |
self.total_bursts = 0
|
| 267 |
|
| 268 |
def check_and_adjust(self, loss_val, optimizer, step):
|
| 269 |
-
"""Call every step. Returns True if burst was triggered."""
|
| 270 |
if not math.isfinite(loss_val):
|
| 271 |
return False
|
| 272 |
-
|
| 273 |
self._history.append(loss_val)
|
| 274 |
-
|
| 275 |
-
# During burst: decay LR back to base over burst_steps
|
| 276 |
if self._burst_remaining > 0:
|
| 277 |
self._burst_remaining -= 1
|
| 278 |
if self._burst_remaining == 0 and self._base_lr is not None:
|
|
@@ -280,22 +238,16 @@ class PlateauBreaker:
|
|
| 280 |
pg["lr"] = self._base_lr
|
| 281 |
self._base_lr = None
|
| 282 |
return False
|
| 283 |
-
|
| 284 |
if len(self._history) < self.patience:
|
| 285 |
return False
|
| 286 |
-
|
| 287 |
-
# Check variance
|
| 288 |
vals = list(self._history)
|
| 289 |
mean = sum(vals) / len(vals)
|
| 290 |
var = sum((v - mean) ** 2 for v in vals) / len(vals)
|
| 291 |
-
|
| 292 |
if var < self.var_threshold:
|
| 293 |
self._stagnant_count += 1
|
| 294 |
else:
|
| 295 |
self._stagnant_count = 0
|
| 296 |
-
|
| 297 |
if self._stagnant_count >= self.patience // 2:
|
| 298 |
-
# TRIGGER BURST
|
| 299 |
self._base_lr = optimizer.param_groups[0]["lr"]
|
| 300 |
burst_lr = self._base_lr * self.lr_mult
|
| 301 |
for pg in optimizer.param_groups:
|
|
@@ -303,41 +255,49 @@ class PlateauBreaker:
|
|
| 303 |
self._burst_remaining = self.burst_steps
|
| 304 |
self._stagnant_count = 0
|
| 305 |
self.total_bursts += 1
|
| 306 |
-
print(f" [P16] Plateau
|
| 307 |
return True
|
| 308 |
return False
|
| 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 |
# ═══════════════════════════════════════════════════════════
|
|
@@ -363,74 +323,60 @@ def create_scheduler(optimizer, max_steps, warmup_steps=200):
|
|
| 363 |
|
| 364 |
|
| 365 |
# ═══════════════════════════════════════════════════════════
|
| 366 |
-
#
|
| 367 |
# ═══════════════════════════════════════════════════════════
|
| 368 |
|
| 369 |
-
def apply(
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
use_triage=True, use_plateau_breaker=True, use_metabolism=True,
|
| 374 |
-
mtp_heads=3, verbose=True,
|
| 375 |
-
):
|
| 376 |
cpu_info = detect_cpu_info()
|
| 377 |
if verbose:
|
| 378 |
print("=" * 65)
|
| 379 |
-
print("CHIMERA
|
| 380 |
print("=" * 65)
|
| 381 |
-
print(f"
|
| 382 |
|
| 383 |
-
|
| 384 |
if verbose:
|
| 385 |
-
print(f"
|
| 386 |
-
|
| 387 |
-
# P12: Muon
|
| 388 |
-
if use_muon:
|
| 389 |
-
optimizer = create_muon_optimizer(model, lr=lr, weight_decay=weight_decay)
|
| 390 |
-
if verbose:
|
| 391 |
-
n_muon = sum(p.numel() for p in model.parameters()
|
| 392 |
-
if p.requires_grad and p.ndim == 2 and not getattr(p, "_is_embed", False))
|
| 393 |
-
print(f"[P12] Muon (lr={lr}, NS-5) — {n_muon:,} params orthogonalized")
|
| 394 |
-
else:
|
| 395 |
-
optimizer = torch.optim.AdamW(model.parameters(), lr=lr * 0.05,
|
| 396 |
-
betas=(0.9, 0.98), weight_decay=weight_decay)
|
| 397 |
|
|
|
|
|
|
|
|
|
|
| 398 |
scheduler = create_scheduler(optimizer, max_steps, warmup_steps)
|
| 399 |
|
| 400 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
raw = getattr(model, "_orig_mod", model)
|
|
|
|
| 402 |
|
| 403 |
# P13: MTP
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
print(f"[P13] Multi-Token Prediction ({mtp_heads} heads)")
|
| 409 |
-
|
| 410 |
-
# P14: EMA Distillation
|
| 411 |
-
if use_distill:
|
| 412 |
-
extras["distiller"] = EMASelfDistiller(model, decay=0.999, alpha=0.5, temperature=2.0)
|
| 413 |
-
if verbose:
|
| 414 |
-
print(f"[P14] EMA Self-Distillation (α=0.5, T=2.0)")
|
| 415 |
|
| 416 |
# P15: Token Triage
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
print(f"[P15] Token Triage (top 60% tokens → full grad, bottom 40% → 10%)")
|
| 421 |
|
| 422 |
# P16: Plateau Breaker
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
if
|
| 431 |
-
|
| 432 |
-
if verbose:
|
| 433 |
-
print(f"[P17] Batch Metabolism (hard examples → 2× weight)")
|
| 434 |
|
| 435 |
if verbose:
|
| 436 |
print("=" * 65)
|
|
@@ -439,20 +385,23 @@ def apply(
|
|
| 439 |
|
| 440 |
|
| 441 |
# ═══════════════════════════════════════════════════════════
|
| 442 |
-
# Training step — ALL paradigms
|
| 443 |
# ═══════════════════════════════════════════════════════════
|
| 444 |
|
| 445 |
_nan_count = 0
|
| 446 |
|
| 447 |
-
def training_step(
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
| 453 |
global _nan_count
|
| 454 |
extras = extras or {}
|
| 455 |
-
|
| 456 |
ctx = torch.autocast(device_type="cpu", dtype=autocast_dtype) if autocast_dtype else nullcontext()
|
| 457 |
|
| 458 |
with ctx:
|
|
@@ -464,29 +413,43 @@ def training_step(
|
|
| 464 |
outputs = model(batch)
|
| 465 |
input_ids = labels = batch
|
| 466 |
|
| 467 |
-
logits =
|
| 468 |
-
|
| 469 |
-
# ──
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
# P17: Batch Metabolism —
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
else:
|
| 484 |
base_loss = outputs.loss if hasattr(outputs, "loss") else outputs
|
| 485 |
|
| 486 |
-
#
|
| 487 |
mtp = extras.get("mtp")
|
| 488 |
-
|
| 489 |
-
|
|
|
|
| 490 |
total_loss = base_loss + mtp_weight * mtp_loss
|
| 491 |
else:
|
| 492 |
total_loss = base_loss
|
|
@@ -500,13 +463,12 @@ def training_step(
|
|
| 500 |
if _nan_count >= 5:
|
| 501 |
for pg in optimizer.param_groups:
|
| 502 |
pg["lr"] *= 0.5
|
| 503 |
-
print(f" [NaN] 5× — LR halved
|
| 504 |
_nan_count = 0
|
| 505 |
return loss_val
|
| 506 |
-
|
| 507 |
_nan_count = 0
|
| 508 |
|
| 509 |
-
#
|
| 510 |
plateau = extras.get("plateau")
|
| 511 |
if plateau is not None:
|
| 512 |
plateau.check_and_adjust(loss_val, optimizer, step)
|
|
@@ -520,13 +482,16 @@ def training_step(
|
|
| 520 |
if p.grad is not None and not torch.isfinite(p.grad).all():
|
| 521 |
p.grad.nan_to_num_(nan=0.0, posinf=0.0, neginf=0.0)
|
| 522 |
|
| 523 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
|
| 525 |
optimizer.step()
|
| 526 |
scheduler.step()
|
| 527 |
optimizer.zero_grad(set_to_none=True)
|
| 528 |
invalidate_all_caches(model)
|
| 529 |
-
if "distiller" in extras:
|
| 530 |
-
extras["distiller"].update(model)
|
| 531 |
|
| 532 |
return loss_val
|
|
|
|
| 1 |
"""
|
| 2 |
+
chimera_turbo.py — CHIMERA GENESIS v11
|
| 3 |
|
| 4 |
+
The unified training engine for ch1mera 5.3.
|
| 5 |
|
| 6 |
+
Active paradigms (all fused, no dead code):
|
| 7 |
+
P12 Muon optimizer — NS-orthogonalized momentum, 2× token efficiency
|
| 8 |
+
P13 Multi-Token Prediction — 3 aux heads, 3× gradient signal per forward
|
| 9 |
+
P15 Token Triage — focus gradient on informative tokens (Rho-1 inspired)
|
| 10 |
+
P16 Plateau Breaker — adaptive LR bursts on stagnation
|
| 11 |
+
P17 Batch Metabolism — weight hard sequences 2×, easy 0.5×
|
| 12 |
+
P18 Grokfast-EMA — amplify slow grads (generalization), filter fast (noise)
|
| 13 |
+
P19 Layer-wise LR Decay — top layers learn faster, bottom layers preserve features
|
| 14 |
+
|
| 15 |
+
Removed (dead weight):
|
| 16 |
+
P14 EMA Self-Distill — doubled forward time, marginal gain from self-EMA
|
| 17 |
"""
|
| 18 |
|
|
|
|
| 19 |
import math
|
| 20 |
import os
|
|
|
|
| 21 |
import torch
|
| 22 |
import torch.nn as nn
|
| 23 |
import torch.nn.functional as F
|
| 24 |
+
from typing import Optional, Dict, Any, Tuple
|
| 25 |
from contextlib import nullcontext
|
| 26 |
from collections import deque
|
| 27 |
|
| 28 |
|
| 29 |
# ═══════════════════════════════════════════════════════════
|
| 30 |
+
# CPU
|
| 31 |
# ═══════════════════════════════════════════════════════════
|
| 32 |
|
| 33 |
def detect_cpu_info():
|
| 34 |
info = {}
|
| 35 |
try:
|
| 36 |
import multiprocessing
|
| 37 |
+
info["physical_cores"] = len(os.sched_getaffinity(0)) // 2 or multiprocessing.cpu_count() // 2
|
|
|
|
|
|
|
| 38 |
except Exception:
|
| 39 |
import multiprocessing
|
| 40 |
info["physical_cores"] = multiprocessing.cpu_count() // 2
|
|
|
|
| 42 |
info["capability"] = torch.backends.cpu.get_cpu_capability()
|
| 43 |
except Exception:
|
| 44 |
info["capability"] = "unknown"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
info["tcmalloc"] = "tcmalloc" in os.environ.get("LD_PRELOAD", "")
|
| 46 |
return info
|
| 47 |
|
|
|
|
| 50 |
n = max(1, cpu_info["physical_cores"] - reserve)
|
| 51 |
torch.set_num_threads(n)
|
| 52 |
os.environ["OMP_NUM_THREADS"] = str(n)
|
|
|
|
| 53 |
return n
|
| 54 |
|
| 55 |
|
| 56 |
# ═══════════════════════════════════════════════════════════
|
| 57 |
+
# P12 — Muon Optimizer + P19 Layer-wise LR Decay
|
| 58 |
# ═══════════════════════════════════════════════════════════
|
| 59 |
|
| 60 |
def _zeropower_via_newtonschulz5(G, steps=5):
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
class Muon(torch.optim.Optimizer):
|
| 72 |
+
"""Muon with integrated layer-wise LR decay (P19)."""
|
| 73 |
def __init__(self, params, lr=0.02, momentum=0.95, nesterov=True,
|
| 74 |
ns_steps=5, weight_decay=0.0,
|
| 75 |
adamw_betas=(0.9, 0.98), adamw_eps=1e-8):
|
|
|
|
| 81 |
@torch.no_grad()
|
| 82 |
def step(self):
|
| 83 |
for group in self.param_groups:
|
| 84 |
+
lr = group["lr"] * group.get("lr_scale", 1.0)
|
| 85 |
+
wd, mu = group["weight_decay"], group["momentum"]
|
| 86 |
b1, b2 = group["adamw_betas"]
|
| 87 |
for p in group["params"]:
|
| 88 |
if p.grad is None:
|
|
|
|
| 112 |
p.addcdiv_(s["m"], s["v"].sqrt().add_(group["adamw_eps"]), value=-alr)
|
| 113 |
|
| 114 |
|
| 115 |
+
def create_muon_optimizer(model, lr=0.02, momentum=0.95, weight_decay=0.01,
|
| 116 |
+
llrd_decay=0.85):
|
| 117 |
+
"""Create Muon with P19 layer-wise LR decay.
|
| 118 |
+
|
| 119 |
+
Top layers get full LR, bottom layers get LR × decay^depth.
|
| 120 |
+
This preserves general features in early layers while allowing
|
| 121 |
+
later layers to specialize faster. Proven for ternary (arxiv 2602.07374).
|
| 122 |
+
"""
|
| 123 |
+
# Detect layer depth for each param
|
| 124 |
+
raw = getattr(model, "_orig_mod", model)
|
| 125 |
+
n_layers = len(raw.layers) if hasattr(raw, "layers") else 28
|
| 126 |
+
|
| 127 |
+
param_groups = []
|
| 128 |
for name, p in model.named_parameters():
|
| 129 |
if not p.requires_grad:
|
| 130 |
continue
|
| 131 |
+
|
| 132 |
+
is_embed = any(k in name for k in ["embed", "lm_head", "wte", "wpe"])
|
| 133 |
+
if is_embed:
|
| 134 |
p._is_embed = True
|
| 135 |
+
|
| 136 |
+
# Determine layer index for LLRD
|
| 137 |
+
lr_scale = 1.0
|
| 138 |
+
for i in range(n_layers):
|
| 139 |
+
if f"layers.{i}." in name or f"layers.{i}]" in name:
|
| 140 |
+
# Scale: top layer = 1.0, bottom layer = decay^(n_layers-1)
|
| 141 |
+
depth_from_top = n_layers - 1 - i
|
| 142 |
+
lr_scale = llrd_decay ** depth_from_top
|
| 143 |
+
break
|
| 144 |
+
|
| 145 |
+
# Embeddings and lm_head get lowest LR
|
| 146 |
+
if is_embed:
|
| 147 |
+
lr_scale = llrd_decay ** n_layers
|
| 148 |
+
|
| 149 |
+
param_groups.append({
|
| 150 |
+
"params": [p],
|
| 151 |
+
"lr_scale": lr_scale,
|
| 152 |
+
})
|
| 153 |
+
|
| 154 |
+
return Muon(param_groups, lr=lr, momentum=momentum,
|
| 155 |
weight_decay=weight_decay, adamw_betas=(0.9, 0.98))
|
| 156 |
|
| 157 |
|
| 158 |
# ═══════════════════════════════════════════════════════════
|
| 159 |
+
# P13 — Multi-Token Prediction
|
| 160 |
# ═══════════════════════════════════════════════════════════
|
| 161 |
|
| 162 |
class MultiTokenPredictionLoss(nn.Module):
|
|
|
|
| 178 |
logits = head(hidden_states[:, :-shift])
|
| 179 |
targets = labels[:, shift:]
|
| 180 |
sl = min(logits.size(1), targets.size(1))
|
| 181 |
+
loss = F.cross_entropy(logits[:, :sl].reshape(-1, logits.size(-1)),
|
| 182 |
+
targets[:, :sl].reshape(-1), ignore_index=-100)
|
|
|
|
| 183 |
if torch.isfinite(loss):
|
| 184 |
total = total + loss
|
| 185 |
count += 1
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
# ═══════════════════════════════════════════════════════════
|
| 190 |
+
# P15 — Token Triage (Rho-1 inspired)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
# ═══════════════════════════════════════════════════════════
|
| 192 |
|
| 193 |
class TokenTriage:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
def __init__(self, ema_decay=0.99, select_ratio=0.6, floor_weight=0.1):
|
| 195 |
self.ema_decay = ema_decay
|
| 196 |
+
self.select_ratio = select_ratio
|
| 197 |
+
self.floor_weight = floor_weight
|
| 198 |
+
self._loss_ema = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
def compute_weights(self, per_token_loss):
|
| 201 |
+
"""Returns per-token weights [B, T]. Differentiable-safe (weights are detached)."""
|
| 202 |
with torch.no_grad():
|
| 203 |
+
mean_loss = per_token_loss.mean().item()
|
| 204 |
if self._loss_ema is None:
|
| 205 |
self._loss_ema = mean_loss
|
| 206 |
else:
|
| 207 |
self._loss_ema = self.ema_decay * self._loss_ema + (1 - self.ema_decay) * mean_loss
|
| 208 |
+
excess = per_token_loss - self._loss_ema
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
threshold = torch.quantile(excess.flatten(), 1.0 - self.select_ratio)
|
| 210 |
+
return torch.where(excess >= threshold, 1.0, self.floor_weight)
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
|
| 213 |
# ═══════════════════════════════════════════════════════════
|
| 214 |
+
# P16 — Plateau Breaker
|
| 215 |
# ═══════════════════════════════════════════════════════════
|
| 216 |
|
| 217 |
class PlateauBreaker:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
def __init__(self, patience=100, variance_threshold=0.005,
|
| 219 |
lr_multiplier=3.0, burst_steps=50):
|
| 220 |
self.patience = patience
|
|
|
|
| 228 |
self.total_bursts = 0
|
| 229 |
|
| 230 |
def check_and_adjust(self, loss_val, optimizer, step):
|
|
|
|
| 231 |
if not math.isfinite(loss_val):
|
| 232 |
return False
|
|
|
|
| 233 |
self._history.append(loss_val)
|
|
|
|
|
|
|
| 234 |
if self._burst_remaining > 0:
|
| 235 |
self._burst_remaining -= 1
|
| 236 |
if self._burst_remaining == 0 and self._base_lr is not None:
|
|
|
|
| 238 |
pg["lr"] = self._base_lr
|
| 239 |
self._base_lr = None
|
| 240 |
return False
|
|
|
|
| 241 |
if len(self._history) < self.patience:
|
| 242 |
return False
|
|
|
|
|
|
|
| 243 |
vals = list(self._history)
|
| 244 |
mean = sum(vals) / len(vals)
|
| 245 |
var = sum((v - mean) ** 2 for v in vals) / len(vals)
|
|
|
|
| 246 |
if var < self.var_threshold:
|
| 247 |
self._stagnant_count += 1
|
| 248 |
else:
|
| 249 |
self._stagnant_count = 0
|
|
|
|
| 250 |
if self._stagnant_count >= self.patience // 2:
|
|
|
|
| 251 |
self._base_lr = optimizer.param_groups[0]["lr"]
|
| 252 |
burst_lr = self._base_lr * self.lr_mult
|
| 253 |
for pg in optimizer.param_groups:
|
|
|
|
| 255 |
self._burst_remaining = self.burst_steps
|
| 256 |
self._stagnant_count = 0
|
| 257 |
self.total_bursts += 1
|
| 258 |
+
print(f" [P16] Plateau! LR burst {self._base_lr:.2e} → {burst_lr:.2e} × {self.burst_steps}steps")
|
| 259 |
return True
|
| 260 |
return False
|
| 261 |
|
| 262 |
|
| 263 |
# ═══════════════════════════════════════════════════════════
|
| 264 |
+
# P18 — Grokfast-EMA (arxiv 2405.20233)
|
| 265 |
# ══════════════════════════════════��════════════════════════
|
| 266 |
|
| 267 |
+
class GrokfastEMA:
|
| 268 |
+
"""Accelerate generalization by amplifying slow gradient components.
|
| 269 |
+
|
| 270 |
+
The key insight: gradient time-series has fast components (memorization,
|
| 271 |
+
STE quantization noise) and slow components (generalization signal).
|
| 272 |
+
EMA-filter the gradients, then ADD the filtered (slow) component back
|
| 273 |
+
with amplification factor λ.
|
| 274 |
|
| 275 |
+
Result: 43× faster convergence on grokking tasks.
|
| 276 |
+
For ternary models: STE noise is exactly the "fast component" —
|
| 277 |
+
Grokfast filters it out while amplifying the real learning signal.
|
| 278 |
+
|
| 279 |
+
arxiv 2405.20233, α=0.98, λ=2.0 recommended.
|
| 280 |
"""
|
| 281 |
+
def __init__(self, alpha=0.98, lamb=2.0):
|
| 282 |
+
self.alpha = alpha
|
| 283 |
+
self.lamb = lamb
|
| 284 |
+
self._ema: Dict[str, torch.Tensor] = {}
|
| 285 |
+
|
| 286 |
+
@torch.no_grad()
|
| 287 |
+
def apply(self, model: nn.Module):
|
| 288 |
+
"""Call after loss.backward(), before optimizer.step().
|
| 289 |
+
|
| 290 |
+
Modifies param.grad in-place to amplify slow components.
|
| 291 |
+
"""
|
| 292 |
+
for name, param in model.named_parameters():
|
| 293 |
+
if param.grad is None:
|
| 294 |
+
continue
|
| 295 |
+
if name not in self._ema:
|
| 296 |
+
self._ema[name] = param.grad.clone()
|
| 297 |
+
else:
|
| 298 |
+
self._ema[name].mul_(self.alpha).add_(param.grad, alpha=1 - self.alpha)
|
| 299 |
+
# Amplify slow component: grad = grad + λ * EMA(grad)
|
| 300 |
+
param.grad.add_(self._ema[name], alpha=self.lamb)
|
| 301 |
|
| 302 |
|
| 303 |
# ═══════════════════════════════════════════════════════════
|
|
|
|
| 323 |
|
| 324 |
|
| 325 |
# ═══════════════════════════════════════════════════════════
|
| 326 |
+
# apply()
|
| 327 |
# ═══════════════════════════════════════════════════════════
|
| 328 |
|
| 329 |
+
def apply(model, max_steps=10000, lr=0.02, weight_decay=0.01,
|
| 330 |
+
warmup_steps=200, use_compile=False, mtp_heads=3,
|
| 331 |
+
llrd_decay=0.85, grokfast_alpha=0.98, grokfast_lambda=2.0,
|
| 332 |
+
verbose=True):
|
|
|
|
|
|
|
|
|
|
| 333 |
cpu_info = detect_cpu_info()
|
| 334 |
if verbose:
|
| 335 |
print("=" * 65)
|
| 336 |
+
print("CHIMERA GENESIS v11 — Revolutionary Training Engine")
|
| 337 |
print("=" * 65)
|
| 338 |
+
print(f" CPU: {cpu_info['capability']} Cores: {cpu_info['physical_cores']}")
|
| 339 |
|
| 340 |
+
n = configure_threading(cpu_info)
|
| 341 |
if verbose:
|
| 342 |
+
print(f" Threads: {n}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
+
# P12+P19: Muon with layer-wise LR decay
|
| 345 |
+
optimizer = create_muon_optimizer(model, lr=lr, weight_decay=weight_decay,
|
| 346 |
+
llrd_decay=llrd_decay)
|
| 347 |
scheduler = create_scheduler(optimizer, max_steps, warmup_steps)
|
| 348 |
|
| 349 |
+
if verbose:
|
| 350 |
+
n_groups = len(optimizer.param_groups)
|
| 351 |
+
n_total = sum(p.numel() for g in optimizer.param_groups for p in g["params"])
|
| 352 |
+
scales = [g["lr_scale"] for g in optimizer.param_groups]
|
| 353 |
+
print(f"[P12] Muon (lr={lr}) + [P19] LLRD (decay={llrd_decay}) — {n_total:,} params, {n_groups} groups")
|
| 354 |
+
print(f" LR range: {min(scales):.3f}× → {max(scales):.3f}×")
|
| 355 |
+
|
| 356 |
raw = getattr(model, "_orig_mod", model)
|
| 357 |
+
extras = {}
|
| 358 |
|
| 359 |
# P13: MTP
|
| 360 |
+
h, v = raw.config["hidden_size"], raw.config["vocab_size"]
|
| 361 |
+
extras["mtp"] = MultiTokenPredictionLoss(h, v, n_future=mtp_heads)
|
| 362 |
+
if verbose:
|
| 363 |
+
print(f"[P13] Multi-Token Prediction ({mtp_heads} heads)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
# P15: Token Triage
|
| 366 |
+
extras["triage"] = TokenTriage(ema_decay=0.99, select_ratio=0.6, floor_weight=0.1)
|
| 367 |
+
if verbose:
|
| 368 |
+
print(f"[P15] Token Triage (60% informative → full grad, 40% noise → 10%)")
|
|
|
|
| 369 |
|
| 370 |
# P16: Plateau Breaker
|
| 371 |
+
extras["plateau"] = PlateauBreaker(patience=100, variance_threshold=0.005,
|
| 372 |
+
lr_multiplier=3.0, burst_steps=50)
|
| 373 |
+
if verbose:
|
| 374 |
+
print(f"[P16] Plateau Breaker (stagnation → LR ×3 burst)")
|
| 375 |
+
|
| 376 |
+
# P18: Grokfast-EMA
|
| 377 |
+
extras["grokfast"] = GrokfastEMA(alpha=grokfast_alpha, lamb=grokfast_lambda)
|
| 378 |
+
if verbose:
|
| 379 |
+
print(f"[P18] Grokfast-EMA (α={grokfast_alpha}, λ={grokfast_lambda} — amplify generalization)")
|
|
|
|
|
|
|
| 380 |
|
| 381 |
if verbose:
|
| 382 |
print("=" * 65)
|
|
|
|
| 385 |
|
| 386 |
|
| 387 |
# ═══════════════════════════════════════════════════════════
|
| 388 |
+
# Training step — ALL paradigms FUSED
|
| 389 |
# ═══════════════════════════════════════════════════════════
|
| 390 |
|
| 391 |
_nan_count = 0
|
| 392 |
|
| 393 |
+
def training_step(model, batch, optimizer, scheduler,
|
| 394 |
+
extras=None, grad_accum_steps=1, step=0,
|
| 395 |
+
max_grad_norm=1.0, autocast_dtype=None,
|
| 396 |
+
mtp_weight=0.3) -> float:
|
| 397 |
+
"""One training step with all paradigms active and fused.
|
| 398 |
+
|
| 399 |
+
Loss = TokenTriage(BatchMetabolism(CE_per_token)) + mtp_weight * MTP_aux
|
| 400 |
+
After backward: Grokfast-EMA filters gradients → Muon+LLRD step
|
| 401 |
+
"""
|
| 402 |
global _nan_count
|
| 403 |
extras = extras or {}
|
| 404 |
+
is_accum = (step + 1) % grad_accum_steps == 0
|
| 405 |
ctx = torch.autocast(device_type="cpu", dtype=autocast_dtype) if autocast_dtype else nullcontext()
|
| 406 |
|
| 407 |
with ctx:
|
|
|
|
| 413 |
outputs = model(batch)
|
| 414 |
input_ids = labels = batch
|
| 415 |
|
| 416 |
+
logits = getattr(outputs, "logits", None)
|
| 417 |
+
|
| 418 |
+
# ── FUSED LOSS: Token Triage × Batch Metabolism ──
|
| 419 |
+
if logits is not None:
|
| 420 |
+
B, T, V = logits.shape
|
| 421 |
+
# Per-token CE (no reduction)
|
| 422 |
+
per_token = F.cross_entropy(
|
| 423 |
+
logits.reshape(-1, V), labels.reshape(-1),
|
| 424 |
+
ignore_index=-100, reduction="none"
|
| 425 |
+
).reshape(B, T)
|
| 426 |
+
|
| 427 |
+
# P17: Batch Metabolism — per-sequence weights
|
| 428 |
+
with torch.no_grad():
|
| 429 |
+
seq_loss = per_token.mean(dim=1) # [B]
|
| 430 |
+
seq_mean = seq_loss.mean()
|
| 431 |
+
seq_std = seq_loss.std().clamp(min=1e-6)
|
| 432 |
+
z = (seq_loss - seq_mean) / seq_std
|
| 433 |
+
seq_weights = torch.sigmoid(z) * 1.5 + 0.5 # [0.5, 2.0]
|
| 434 |
+
|
| 435 |
+
# P15: Token Triage — per-token weights
|
| 436 |
+
triage = extras.get("triage")
|
| 437 |
+
if triage is not None:
|
| 438 |
+
tok_weights = triage.compute_weights(per_token) # [B, T]
|
| 439 |
+
else:
|
| 440 |
+
tok_weights = torch.ones_like(per_token)
|
| 441 |
+
|
| 442 |
+
# Fuse: multiply token weights × sequence weights
|
| 443 |
+
combined_weights = tok_weights * seq_weights.unsqueeze(1) # [B, T]
|
| 444 |
+
base_loss = (per_token * combined_weights).sum() / combined_weights.sum()
|
| 445 |
else:
|
| 446 |
base_loss = outputs.loss if hasattr(outputs, "loss") else outputs
|
| 447 |
|
| 448 |
+
# P13: MTP auxiliary
|
| 449 |
mtp = extras.get("mtp")
|
| 450 |
+
hidden = getattr(outputs, "hidden_states", None)
|
| 451 |
+
if mtp is not None and hidden is not None:
|
| 452 |
+
mtp_loss = mtp(hidden, labels)
|
| 453 |
total_loss = base_loss + mtp_weight * mtp_loss
|
| 454 |
else:
|
| 455 |
total_loss = base_loss
|
|
|
|
| 463 |
if _nan_count >= 5:
|
| 464 |
for pg in optimizer.param_groups:
|
| 465 |
pg["lr"] *= 0.5
|
| 466 |
+
print(f" [NaN] 5× — LR halved")
|
| 467 |
_nan_count = 0
|
| 468 |
return loss_val
|
|
|
|
| 469 |
_nan_count = 0
|
| 470 |
|
| 471 |
+
# P16: Plateau Breaker
|
| 472 |
plateau = extras.get("plateau")
|
| 473 |
if plateau is not None:
|
| 474 |
plateau.check_and_adjust(loss_val, optimizer, step)
|
|
|
|
| 482 |
if p.grad is not None and not torch.isfinite(p.grad).all():
|
| 483 |
p.grad.nan_to_num_(nan=0.0, posinf=0.0, neginf=0.0)
|
| 484 |
|
| 485 |
+
# P18: Grokfast-EMA — amplify slow gradients BEFORE optimizer step
|
| 486 |
+
grokfast = extras.get("grokfast")
|
| 487 |
+
if grokfast is not None:
|
| 488 |
+
grokfast.apply(model)
|
| 489 |
+
|
| 490 |
+
if is_accum:
|
| 491 |
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
|
| 492 |
optimizer.step()
|
| 493 |
scheduler.step()
|
| 494 |
optimizer.zero_grad(set_to_none=True)
|
| 495 |
invalidate_all_caches(model)
|
|
|
|
|
|
|
| 496 |
|
| 497 |
return loss_val
|