kdirgul commited on
Commit
8be2b5b
·
verified ·
1 Parent(s): 0f21611

lamba_cpu: kalibre bayraklari (ANG_SIGN/ANG_DT/ROPE_INTER) grid-search icin

Browse files
Files changed (1) hide show
  1. code/kod/lamba_cpu.py +24 -11
code/kod/lamba_cpu.py CHANGED
@@ -33,6 +33,11 @@ def rms_norm(x, w, eps=1e-5):
33
 
34
  # ───────────── Mamba-3 mixer (saf-PyTorch, fork matematiği) ─────────────
35
  class Mamba3CPU(nn.Module):
 
 
 
 
 
36
  def __init__(self, cfg):
37
  super().__init__()
38
  d = cfg["d_model"]
@@ -58,15 +63,20 @@ class Mamba3CPU(nn.Module):
58
  self.B_norm = nn.Parameter(torch.ones(bc))
59
  self.C_norm = nn.Parameter(torch.ones(bc))
60
 
61
- def _rope_pairwise(self, t, cos, sin):
62
- """rope_fraction=0.5 pairwise: ilk `rot` boyutta j ↔ j+rot/2 çiftleri döner; gerisi sabit.
63
- t: (..., d_state) cos/sin: (..., n_ang)"""
64
  rot, n = self.rot, self.n_ang
65
- a = t[..., :n] # ilk yarı (döner çift sol)
66
- b = t[..., n:rot] # ikinci yarı (döner çift sağ)
67
- ra = a * cos - b * sin
68
- rb = a * sin + b * cos
69
- return torch.cat([ra, rb, t[..., rot:]], dim=-1)
 
 
 
 
 
70
 
71
  def forward(self, u):
72
  """u: (B, L, d_model) → (B, L, d_model). Token-token recurrence."""
@@ -96,10 +106,13 @@ class Mamba3CPU(nn.Module):
96
  cum = torch.zeros(B, H, self.n_ang)
97
  ys = []
98
  for t in range(L):
99
- cum = cum - DT[:, t].unsqueeze(-1) * ang[:, t].float().unsqueeze(1) # (B,H,n_ang) # KALİBRE: negatif birikim (minimal gibi)
 
 
 
100
  cos, sin = torch.cos(cum), torch.sin(cum)
101
- Bk = self._rope_pairwise(Bm[:, t], cos, sin) # (B,H,S)
102
- Cq = self._rope_pairwise(Cm[:, t], cos, sin)
103
  xt = x[:, t] # (B,H,P)
104
  a = alpha[:, t].view(B, H, 1, 1)
105
  diff = (beta[:, t].view(B, H, 1, 1) * x_prev.unsqueeze(-1) * Bk_prev.unsqueeze(-2)
 
33
 
34
  # ───────────── Mamba-3 mixer (saf-PyTorch, fork matematiği) ─────────────
35
  class Mamba3CPU(nn.Module):
36
+ # KALİBRE bayrakları (Colab grid-search ile fork'a karşı belirlenir)
37
+ ANG_SIGN = 1.0 # angle birikim işareti (+1 / -1)
38
+ ANG_DT = True # increment DT ile ölçekli mi (DT·ang vs ang)
39
+ ROPE_INTER = False # RoPE: interleaved (True) vs pairwise j↔j+n (False)
40
+
41
  def __init__(self, cfg):
42
  super().__init__()
43
  d = cfg["d_model"]
 
63
  self.B_norm = nn.Parameter(torch.ones(bc))
64
  self.C_norm = nn.Parameter(torch.ones(bc))
65
 
66
+ def _rope(self, t, cos, sin):
67
+ """Partial RoPE (ilk `rot`=d_state/2 boyut döner; gerisi sabit).
68
+ pairwise: j ↔ j+n çiftleri | interleaved: (2j, 2j+1) çiftleri."""
69
  rot, n = self.rot, self.n_ang
70
+ rest = t[..., rot:]
71
+ if Mamba3CPU.ROPE_INTER:
72
+ head = t[..., :rot]
73
+ x1, x2 = head[..., 0::2], head[..., 1::2]
74
+ ra, rb = x1 * cos - x2 * sin, x1 * sin + x2 * cos
75
+ out = torch.stack([ra, rb], dim=-1).flatten(-2)
76
+ return torch.cat([out, rest], dim=-1)
77
+ a, b = t[..., :n], t[..., n:rot]
78
+ ra, rb = a * cos - b * sin, a * sin + b * cos
79
+ return torch.cat([ra, rb, rest], dim=-1)
80
 
81
  def forward(self, u):
82
  """u: (B, L, d_model) → (B, L, d_model). Token-token recurrence."""
 
106
  cum = torch.zeros(B, H, self.n_ang)
107
  ys = []
108
  for t in range(L):
109
+ inc = ang[:, t].float().unsqueeze(1) # (B,1,n_ang)→broadcast head
110
+ if Mamba3CPU.ANG_DT:
111
+ inc = inc * DT[:, t].unsqueeze(-1) # DT ölçeği
112
+ cum = cum + Mamba3CPU.ANG_SIGN * inc # (B,H,n_ang)
113
  cos, sin = torch.cos(cum), torch.sin(cum)
114
+ Bk = self._rope(Bm[:, t], cos, sin) # (B,H,S)
115
+ Cq = self._rope(Cm[:, t], cos, sin)
116
  xt = x[:, t] # (B,H,P)
117
  a = alpha[:, t].view(B, H, 1, 1)
118
  diff = (beta[:, t].view(B, H, 1, 1) * x_prev.unsqueeze(-1) * Bk_prev.unsqueeze(-2)