mini-beatrix-2s automodel: address.py (mission final 16.101B, alephllm 0.8.6)
Browse files- address.py +95 -0
address.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""AlephAddress — the closed-form signed address over 2K oriented half-axes.
|
| 2 |
+
|
| 3 |
+
The mechanism is reconstructive, never comparative: no softmax-over-choices,
|
| 4 |
+
no argmax, no top-k anywhere. Dispatch weights are
|
| 5 |
+
w_k = sinh(u_k) / sum_j cosh(u_j), u_k = cos(x_hat, a_hat_k) / tau
|
| 6 |
+
which is exactly the signed difference of the two halves of a 2K-softmax
|
| 7 |
+
over oriented axes (+a_k, -a_k). Inhibition (negative w) is first-class.
|
| 8 |
+
"""
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
import torch.nn.functional as F
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def dtype_floor(t: torch.Tensor) -> float:
|
| 17 |
+
"""Dtype-aware clamp floor. Half dtypes flush 1e-12 to zero — the
|
| 18 |
+
measured fp16 landmine; use a floor the dtype can actually represent."""
|
| 19 |
+
if t.dtype in (torch.float32, torch.float64):
|
| 20 |
+
return 1e-12
|
| 21 |
+
return float(torch.finfo(t.dtype).tiny) * 8
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class AlephAddress(nn.Module):
|
| 25 |
+
"""K unit anchors in D dims, cosine-read at temperature tau.
|
| 26 |
+
|
| 27 |
+
signed(x) -> (..., K) w_k = sinh(u_k)/sum_j cosh(u_j)
|
| 28 |
+
oriented(x)-> ((..., K), (..., K)) the two positive halves of the
|
| 29 |
+
2K-softmax (ep/Z, en/Z); HUB feature map.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
def __init__(self, K: int, D: int, tau: float = 0.1):
|
| 33 |
+
super().__init__()
|
| 34 |
+
self.K, self.D, self.tau = K, D, tau
|
| 35 |
+
self.codebook = nn.Parameter(F.normalize(torch.randn(K, D), dim=-1))
|
| 36 |
+
self.register_buffer("home", self.codebook.detach().clone())
|
| 37 |
+
|
| 38 |
+
def _u(self, x: torch.Tensor) -> torch.Tensor:
|
| 39 |
+
A = F.normalize(self.codebook, dim=-1)
|
| 40 |
+
return (F.normalize(x, dim=-1) @ A.transpose(-1, -2)) / self.tau
|
| 41 |
+
|
| 42 |
+
def oriented(self, x: torch.Tensor):
|
| 43 |
+
u = self._u(x)
|
| 44 |
+
m = u.abs().amax(dim=-1, keepdim=True)
|
| 45 |
+
ep, en = torch.exp(u - m), torch.exp(-u - m)
|
| 46 |
+
Z = (ep + en).sum(dim=-1, keepdim=True)
|
| 47 |
+
return ep / Z, en / Z
|
| 48 |
+
|
| 49 |
+
def oriented_cat(self, x: torch.Tensor) -> torch.Tensor:
|
| 50 |
+
"""cat(ep, en)/Z along the last dim in ONE exp/normalize pass —
|
| 51 |
+
mathematically identical to torch.cat(self.oriented(x), -1).
|
| 52 |
+
Public API; the hub's training forward no longer calls it (0.7.3:
|
| 53 |
+
CausalSplatHUB._code_cat_qk builds q+k codes for all books in one
|
| 54 |
+
batched softmax pass) — probes and external readers still may."""
|
| 55 |
+
u = self._u(x)
|
| 56 |
+
m = u.abs().amax(dim=-1, keepdim=True)
|
| 57 |
+
e = torch.exp(torch.cat([u - m, -u - m], dim=-1))
|
| 58 |
+
return e / e.sum(dim=-1, keepdim=True)
|
| 59 |
+
|
| 60 |
+
def signed(self, x: torch.Tensor) -> torch.Tensor:
|
| 61 |
+
u = self._u(x)
|
| 62 |
+
m = u.abs().amax(dim=-1, keepdim=True)
|
| 63 |
+
ep, en = torch.exp(u - m), torch.exp(-u - m)
|
| 64 |
+
return (ep - en) / (ep + en).sum(dim=-1, keepdim=True)
|
| 65 |
+
|
| 66 |
+
@torch.no_grad()
|
| 67 |
+
def health(self, x_sample: torch.Tensor) -> dict:
|
| 68 |
+
"""Codebook + consumption vitals for the instrument suite."""
|
| 69 |
+
A = F.normalize(self.codebook.float(), dim=-1)
|
| 70 |
+
gram = A @ A.T
|
| 71 |
+
off = gram - torch.eye(self.K, device=gram.device)
|
| 72 |
+
drift = 1.0 - F.cosine_similarity(
|
| 73 |
+
A, F.normalize(self.home.float(), dim=-1), dim=-1)
|
| 74 |
+
s = torch.linalg.svdvals(A)
|
| 75 |
+
ps = (s * s) / (s * s).sum().clamp_min(1e-12)
|
| 76 |
+
out = {
|
| 77 |
+
"anchor_max_abs_cos": off.abs().max().item(),
|
| 78 |
+
"anchor_merge_pairs": int((off.abs() > 0.99).sum().item() // 2),
|
| 79 |
+
"drift_mean": drift.mean().item(),
|
| 80 |
+
"drift_max": drift.max().item(),
|
| 81 |
+
# frame health: effective rank of the codebook itself — a
|
| 82 |
+
# collapsing frame (anchors folding into a subspace) shows here
|
| 83 |
+
"codebook_erank": float(
|
| 84 |
+
torch.exp(-(ps * ps.clamp_min(1e-12).log()).sum()).item()),
|
| 85 |
+
}
|
| 86 |
+
if x_sample is not None:
|
| 87 |
+
p, n = self.oriented(x_sample.reshape(-1, x_sample.shape[-1]).float())
|
| 88 |
+
mass = torch.cat([p, n], dim=-1).mean(0) # (2K,) mean usage
|
| 89 |
+
mass = mass / mass.sum().clamp_min(1e-12)
|
| 90 |
+
ent = -(mass * mass.clamp_min(1e-12).log()).sum()
|
| 91 |
+
out["usage_ppl"] = float(ent.exp().item()) # of 2K half-axes
|
| 92 |
+
out["usage_cv"] = float((mass.std() / mass.mean().clamp_min(1e-12)).item())
|
| 93 |
+
w = self.signed(x_sample.reshape(-1, x_sample.shape[-1]).float())
|
| 94 |
+
out["sign_frac_neg"] = float((w < 0).float().mean().item())
|
| 95 |
+
return out
|