Download ket_optimizer.py from ArpeciousAI/FlowRes-1: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/ArpeciousAI/FlowRes-1/resolve/main/ket_optimizer.py
- Command line
-
hf download hf://ArpeciousAI/FlowRes-1/ket_optimizer.py
-
curl -L -o ket_optimizer.py https://huggingface.co/ArpeciousAI/FlowRes-1/resolve/main/ket_optimizer.py
2.97 kB
| import torch | |
| import math | |
| class KetOptimizer(torch.optim.Optimizer): | |
| def __init__(self, params, lr=1e-3, dt=0.01, A=-1.0, B=1.0, C=1.0, rank_ratio=20): | |
| """ | |
| The Ket Optimizer: A zero-overhead, hyper-compressed State Space Optimizer. | |
| Uses BlackMamba / GaLore Subspace Projection to kill the Adam RAM bottleneck by 95%. | |
| rank_ratio: Shrinks the state space to 1/rank_ratio of the original size. | |
| (e.g., 20 means it uses exactly 5% of the original RAM). | |
| """ | |
| defaults = dict(lr=lr, dt=dt, A=A, B=B, C=C, rank_ratio=rank_ratio) | |
| super().__init__(params, defaults) | |
| def step(self, closure=None): | |
| loss = None | |
| if closure is not None: | |
| with torch.enable_grad(): | |
| loss = closure() | |
| for group in self.param_groups: | |
| lr = group['lr'] | |
| dt = group['dt'] | |
| # Discretize continuous A via Exact Exponential for the SSM | |
| A_bar = math.exp(group['A'] * dt) | |
| B_bar = group['B'] * dt | |
| C_val = group['C'] | |
| rank_ratio = group['rank_ratio'] | |
| for p in group['params']: | |
| if p.grad is None: | |
| continue | |
| grad = p.grad | |
| state = self.state[p] | |
| if len(state) == 0: | |
| state['step'] = 0 | |
| if len(p.shape) == 2: | |
| M, N = p.shape | |
| # track the row and column momentums! | |
| state['h_row'] = torch.zeros(M, 1, device=p.device, dtype=p.dtype) | |
| state['h_col'] = torch.zeros(1, N, device=p.device, dtype=p.dtype) | |
| state['is_projected'] = True | |
| else: | |
| state['h'] = torch.zeros_like(p, memory_format=torch.preserve_format) | |
| state['is_projected'] = False | |
| state['step'] += 1 | |
| if state['is_projected']: | |
| h_row = state['h_row'] | |
| h_col = state['h_col'] | |
| # Track momentum of the row averages and column averages separately | |
| G_row = grad.mean(dim=1, keepdim=True) | |
| G_col = grad.mean(dim=0, keepdim=True) | |
| h_row.mul_(A_bar).add_(G_row, alpha=B_bar) | |
| h_col.mul_(A_bar).add_(G_col, alpha=B_bar) | |
| y = (h_row + h_col) * C_val | |
| else: | |
| h = state['h'] | |
| h.mul_(A_bar).add_(grad, alpha=B_bar) | |
| y = h * C_val | |
| p.add_(y, alpha=-lr) | |
| return loss | |