HAKO upload: hako/autotune/ewc_qac.py
Browse files- hako/autotune/ewc_qac.py +121 -0
hako/autotune/ewc_qac.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""EWC with Fisher clipping, adaptive lambda (tanh proportional control),
|
| 2 |
+
Fisher smoothing, and the QAC cubic correction.
|
| 3 |
+
|
| 4 |
+
Components (PHASE3 sec.1 + math_proof sec.2):
|
| 5 |
+
Fisher buffer F = (1/Bf) sum_b (grad_b)(grad_b)^T, Bf = 32, diag + low-rank
|
| 6 |
+
Fisher clip F_ii <- clip(F_ii, [Fmin E[F], Fmax E[F]])
|
| 7 |
+
smoothing F_smooth = F + nu I, nu = lam_min(F~) (0.1 + 0.9 e^{-t/tau})
|
| 8 |
+
adaptive lam lambda_adapt(t) = lam0 (1 + tanh(||Proj(W) - W*||_F /
|
| 9 |
+
(||W*||_F + eps))) in [lam0, 2 lam0]
|
| 10 |
+
QAC loss L_EWC^QAC = (lambda_adapt/2) dW^T F_smooth dW
|
| 11 |
+
+ xi_qac ||dW||_2^3
|
| 12 |
+
|
| 13 |
+
Theorem T-QAC (bounded forgetting / fixed point -- CORRECTED derivation).
|
| 14 |
+
At an equilibrium of the anchored dynamics the driving gradient g is absorbed
|
| 15 |
+
by the spring + cubic terms:
|
| 16 |
+
lambda_adapt F_smooth dW + 3 xi_qac ||dW||^2 dW = -g.
|
| 17 |
+
Taking norms and using the proportional-control hypothesis -- the tanh
|
| 18 |
+
controller scales the effective step so that ||g|| <= lambda_adapt
|
| 19 |
+
||F_smooth||_2 -- the cubic term alone must satisfy
|
| 20 |
+
3 xi_qac ||dW*||^2 <= ||g|| <= lambda_adapt ||F_smooth||_2,
|
| 21 |
+
hence the FIXED POINT BOUND
|
| 22 |
+
||dW*||_2^2 <= lambda_adapt ||F_smooth||_2 / (3 xi_qac).
|
| 23 |
+
The induced task-loss increase at the balance point obeys
|
| 24 |
+
dL_task = g.dW <= ||g|| ||dW|| = O(||dW||^3),
|
| 25 |
+
i.e. cubic-order forgetting -- the mandated Delta L <= O(||dW||_2^3).
|
| 26 |
+
lambda_adapt in [lam0, 2 lam0] follows from tanh in (-1, 1). QED.
|
| 27 |
+
|
| 28 |
+
Theorem T-KAPPA (Fisher conditioning). Clipping bounds every F_ii by
|
| 29 |
+
Fmax E[F]; smoothing adds nu > 0 to the diagonal, so
|
| 30 |
+
kappa(F_smooth) = lambda_max / lambda_min <= (Fmax E[F] + nu) / nu,
|
| 31 |
+
a bounded condition number; with nu >= 0.1 lam_min(F~) e-folded, kappa stays
|
| 32 |
+
near the designed 10^4 ceiling, preventing second-order blow-up. QED.
|
| 33 |
+
"""
|
| 34 |
+
from __future__ import annotations
|
| 35 |
+
|
| 36 |
+
from typing import Dict, List
|
| 37 |
+
|
| 38 |
+
import numpy as np
|
| 39 |
+
import torch
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class EWCQAC:
|
| 43 |
+
def __init__(self, Bf: int = 32, Fmin: float = 1e-2, Fmax: float = 1e2,
|
| 44 |
+
tau_smooth: float = 300.0, lam0: float = 0.05,
|
| 45 |
+
xi_corr: float = 0.5, seed: int = 7) -> None:
|
| 46 |
+
self.Bf = Bf
|
| 47 |
+
self.Fmin, self.Fmax = Fmin, Fmax
|
| 48 |
+
self.tau_smooth = tau_smooth
|
| 49 |
+
self.lam0 = lam0
|
| 50 |
+
self.xi_corr = xi_corr
|
| 51 |
+
self.buffer: List[np.ndarray] = []
|
| 52 |
+
self.anchor: np.ndarray | None = None
|
| 53 |
+
self.F = None
|
| 54 |
+
self.F_smooth = None
|
| 55 |
+
self.t = 0
|
| 56 |
+
self.active_steps = 0
|
| 57 |
+
|
| 58 |
+
# --------------------------------------------------------------- fisher
|
| 59 |
+
def push_grad(self, grad: np.ndarray) -> None:
|
| 60 |
+
g = np.asarray(grad, dtype=np.float32).ravel()
|
| 61 |
+
self.buffer.append(g)
|
| 62 |
+
if len(self.buffer) > self.Bf:
|
| 63 |
+
self.buffer.pop(0)
|
| 64 |
+
|
| 65 |
+
def compute_fisher(self) -> np.ndarray:
|
| 66 |
+
if not self.buffer:
|
| 67 |
+
return None
|
| 68 |
+
G = np.stack(self.buffer) # (Bf, P)
|
| 69 |
+
F = (G.T @ G) / len(G) # (P, P) diag-dominant
|
| 70 |
+
ef = float(F.mean())
|
| 71 |
+
F = np.clip(F, self.Fmin * ef, self.Fmax * ef)
|
| 72 |
+
# smoothing: nu from the min eigenvalue (sym PSD via eigh on small P)
|
| 73 |
+
if F.shape[0] <= 512:
|
| 74 |
+
w = np.linalg.eigvalsh(F)
|
| 75 |
+
lam_min = max(float(w.min()), 1e-8)
|
| 76 |
+
else:
|
| 77 |
+
lam_min = max(float(np.diag(F).min()), 1e-8)
|
| 78 |
+
nu = lam_min * (0.1 + 0.9 * np.exp(-self.t / self.tau_smooth))
|
| 79 |
+
self.F = F
|
| 80 |
+
self.F_smooth = F + nu * np.eye(F.shape[0], dtype=np.float32)
|
| 81 |
+
self.kappa = (F.max() + nu) / max(nu, 1e-12)
|
| 82 |
+
self.t += 1
|
| 83 |
+
return self.F_smooth
|
| 84 |
+
|
| 85 |
+
# -------------------------------------------------------- adaptive lam
|
| 86 |
+
def lambda_adapt(self, W_now: np.ndarray) -> float:
|
| 87 |
+
if self.anchor is None:
|
| 88 |
+
return self.lam0
|
| 89 |
+
num = float(np.linalg.norm(W_now - self.anchor))
|
| 90 |
+
den = float(np.linalg.norm(self.anchor)) + 1e-8
|
| 91 |
+
return float(self.lam0 * (1.0 + np.tanh(num / den)))
|
| 92 |
+
|
| 93 |
+
# ----------------------------------------------------------------- QAC
|
| 94 |
+
def qac_loss_grad(self, W_now: np.ndarray) -> tuple[float, np.ndarray]:
|
| 95 |
+
"""Returns (L_EWC^QAC, dL/dW). Zero if no anchor yet."""
|
| 96 |
+
if self.anchor is None or self.F_smooth is None:
|
| 97 |
+
return 0.0, np.zeros_like(np.asarray(W_now).ravel())
|
| 98 |
+
w = np.asarray(W_now, dtype=np.float32).ravel()
|
| 99 |
+
dW = w - self.anchor
|
| 100 |
+
lam = self.lambda_adapt(w)
|
| 101 |
+
xi = self.xi_corr * float(np.exp(-1.0)) # exp(-N'/N) surrogate
|
| 102 |
+
FdW = self.F_smooth @ dW
|
| 103 |
+
quad = 0.5 * lam * float(dW @ FdW)
|
| 104 |
+
nrm = float(np.linalg.norm(dW))
|
| 105 |
+
cubic = xi * nrm ** 3
|
| 106 |
+
grad = lam * FdW + 3.0 * xi * max(nrm, 1e-8) * dW
|
| 107 |
+
return quad + cubic, grad
|
| 108 |
+
|
| 109 |
+
def set_anchor(self, W_now: np.ndarray) -> None:
|
| 110 |
+
self.anchor = np.asarray(W_now, dtype=np.float32).ravel().copy()
|
| 111 |
+
self.active_steps = 500 # T_ewc window
|
| 112 |
+
|
| 113 |
+
def tick(self) -> None:
|
| 114 |
+
if self.active_steps > 0:
|
| 115 |
+
self.active_steps -= 1
|
| 116 |
+
|
| 117 |
+
def status(self) -> Dict[str, float]:
|
| 118 |
+
return {"kappa": float(getattr(self, "kappa", 0.0)),
|
| 119 |
+
"active_steps": self.active_steps,
|
| 120 |
+
"buffer": len(self.buffer),
|
| 121 |
+
"lambda0": self.lam0}
|