AbstractPhil commited on
Commit
7befba4
·
verified ·
1 Parent(s): 1f66e32

mini-beatrix-2s automodel: bank.py (mission final 16.101B, alephllm 0.8.6)

Browse files
Files changed (1) hide show
  1. bank.py +51 -0
bank.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """AnchoredBank — the E1-form anchored FFN, born on its own null path.
2
+
3
+ Bank(x) = trunk(x) + sum_k w_k(x) * sigmoid(g_k) * expert_k(x)
4
+
5
+ Trunk: always-on d->ff->d GELU expert. Dispatch: the signed aleph address
6
+ over a learned K x d codebook read against the layer input (K=3 fat
7
+ experts, the shape validated at parity under encoder pressure). Gates
8
+ init -3.0; expert OUTPUT projections zero-init, so at birth the dispatch
9
+ contributes exactly zero and the bank is bit-identical to its dense
10
+ control (the C6 null path). No balance machinery of any kind —
11
+ differentiation is an attractor, pressure stays out of the task gradient.
12
+ """
13
+ from __future__ import annotations
14
+
15
+ import torch
16
+ import torch.nn as nn
17
+ import torch.nn.functional as F
18
+
19
+ from .address import AlephAddress
20
+
21
+
22
+ class AnchoredBank(nn.Module):
23
+ def __init__(self, d: int, n_experts: int = 3, ff: int | None = None,
24
+ tau: float = 0.1, gate_init: float = -3.0):
25
+ super().__init__()
26
+ ff = ff or d
27
+ self.n_experts = n_experts
28
+ self.t_in = nn.Linear(d, ff, bias=False)
29
+ self.t_out = nn.Linear(ff, d, bias=False)
30
+ nn.init.orthogonal_(self.t_in.weight)
31
+ nn.init.orthogonal_(self.t_out.weight)
32
+ self.addr = AlephAddress(n_experts, d, tau)
33
+ w_in = torch.empty(n_experts, d, ff)
34
+ for k in range(n_experts):
35
+ nn.init.orthogonal_(w_in[k])
36
+ self.w_in = nn.Parameter(w_in)
37
+ self.w_out = nn.Parameter(torch.zeros(n_experts, ff, d)) # null path
38
+ self.gates = nn.Parameter(torch.full((n_experts,), gate_init))
39
+ self.last_dispatch = None # (mean|w| per expert, w sample) for instruments
40
+
41
+ def forward(self, x, disable_dispatch: bool = False):
42
+ trunk = self.t_out(F.gelu(self.t_in(x)))
43
+ if disable_dispatch:
44
+ return trunk
45
+ w = self.addr.signed(x) # (B, n, K)
46
+ with torch.no_grad():
47
+ self.last_dispatch = w.detach()
48
+ h = F.gelu(torch.einsum("bnd,kdf->bnkf", x, self.w_in))
49
+ e = torch.einsum("bnkf,kfd->bnkd", h, self.w_out)
50
+ return trunk + torch.einsum("bnk,bnkd->bnd",
51
+ w * torch.sigmoid(self.gates), e)