mini-beatrix-2s automodel: head.py (mission final 16.101B, alephllm 0.8.6)
Browse files
head.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""DualHead — the standard readout plus the L-012 aleph read, born null.
|
| 2 |
+
|
| 3 |
+
logits = W_h h + W_s s(h), W_s == 0 at init
|
| 4 |
+
|
| 5 |
+
s(h) is the signed address of a learned low-D projection of h through a
|
| 6 |
+
K_h-anchor head codebook. New structure enters at zero and earns its way
|
| 7 |
+
in by gradient — via WEIGHT-zero init, never gate-zero: mini-beatrix-1
|
| 8 |
+
measured (8.4B tokens) that a gamma-gated branch DEADLOCKS (W_s grad is
|
| 9 |
+
scaled by gamma=0, gamma grad through a random frozen readout is
|
| 10 |
+
zero-mean noise), while the bank's E_out weight-zero self-started into
|
| 11 |
+
+2 bpb of function. gamma remains as a frozen scalar for checkpoint
|
| 12 |
+
compatibility and as the ablation knob; the election gauge is ||W_s||
|
| 13 |
+
and the head_aleph_off toggle.
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import torch
|
| 18 |
+
import torch.nn as nn
|
| 19 |
+
|
| 20 |
+
from .address import AlephAddress
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class DualHead(nn.Module):
|
| 24 |
+
def __init__(self, d: int, vocab: int, K: int = 512, D: int = 32,
|
| 25 |
+
tau: float = 0.1, tied_weight: nn.Parameter | None = None):
|
| 26 |
+
super().__init__()
|
| 27 |
+
self.tied = tied_weight is not None
|
| 28 |
+
if self.tied:
|
| 29 |
+
# tuple wrapper: reference the embedding table WITHOUT registering
|
| 30 |
+
# it as a duplicate parameter (state_dict must stay alias-free
|
| 31 |
+
# for safetensors export)
|
| 32 |
+
self._tied_ref = (tied_weight,)
|
| 33 |
+
self.bias = nn.Parameter(torch.zeros(vocab))
|
| 34 |
+
else:
|
| 35 |
+
self.w_h = nn.Linear(d, vocab, bias=True)
|
| 36 |
+
self.proj = nn.Linear(d, D, bias=False)
|
| 37 |
+
nn.init.orthogonal_(self.proj.weight)
|
| 38 |
+
self.addr = AlephAddress(K, D, tau)
|
| 39 |
+
self.w_s = nn.Linear(K, vocab, bias=False)
|
| 40 |
+
nn.init.zeros_(self.w_s.weight) # weight-zero: self-starting
|
| 41 |
+
self.gamma = nn.Parameter(torch.ones(1)) # frozen; ablation knob only
|
| 42 |
+
self.gamma.requires_grad_(False)
|
| 43 |
+
|
| 44 |
+
def forward(self, h, disable_aleph: bool = False):
|
| 45 |
+
if self.tied:
|
| 46 |
+
base = h @ self._tied_ref[0].T + self.bias
|
| 47 |
+
else:
|
| 48 |
+
base = self.w_h(h)
|
| 49 |
+
if disable_aleph:
|
| 50 |
+
return base
|
| 51 |
+
return base + self.gamma * self.w_s(self.addr.signed(self.proj(h)))
|