Spaces:
Sleeping
Sleeping
Create Stage8.py
Browse files
Stage8.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# stage8.py
|
| 2 |
+
# Author: Liam Grinstead
|
| 3 |
+
# Purpose: RFT-LLM (Language-Only Transformer Validation) — Stage Eight of Twelve
|
| 4 |
+
|
| 5 |
+
import math, time, json, random, argparse
|
| 6 |
+
import torch, torch.nn as nn, torch.nn.functional as F
|
| 7 |
+
|
| 8 |
+
# ---------------- Determinism ----------------
|
| 9 |
+
def set_seed(s=1234):
|
| 10 |
+
random.seed(s); torch.manual_seed(s); torch.cuda.manual_seed_all(s)
|
| 11 |
+
|
| 12 |
+
# ---------------- Telemetry ------------------
|
| 13 |
+
class Telemetry:
|
| 14 |
+
def __init__(self, path="stage8_llm.jsonl"):
|
| 15 |
+
self.t0 = time.time(); self.f = open(path,"w")
|
| 16 |
+
def emit(self, **k):
|
| 17 |
+
k["t"] = round(time.time()-self.t0,3)
|
| 18 |
+
line = json.dumps(k,separators=(",",":"))
|
| 19 |
+
print(line); self.f.write(line+"\n"); self.f.flush()
|
| 20 |
+
def close(self): self.f.close()
|
| 21 |
+
|
| 22 |
+
# ---------------- Orbital Coupler ------------
|
| 23 |
+
class Orbital:
|
| 24 |
+
def __init__(self,g=0.006,floor=0.2):
|
| 25 |
+
self.a=0.0; self.b=math.pi/3; self.g=g; self.floor=floor
|
| 26 |
+
def step(self):
|
| 27 |
+
d=(self.b-self.a+math.pi)%(2*math.pi)-math.pi
|
| 28 |
+
if abs(d)<self.floor: d=self.floor*(1 if d>=0 else -1)
|
| 29 |
+
s=math.sin(d)
|
| 30 |
+
self.a=(self.a+self.g*s)%(2*math.pi)
|
| 31 |
+
self.b=(self.b-self.g*s)%(2*math.pi)
|
| 32 |
+
drift=abs((self.a-self.b+math.pi)%(2*math*pi)-math.pi)
|
| 33 |
+
return drift, abs(s)
|
| 34 |
+
|
| 35 |
+
# ---------------- DCLR Optimiser -------------
|
| 36 |
+
class DCLR(torch.optim.Optimizer):
|
| 37 |
+
def __init__(self, params, lr=5e-4, beta=0.9, gamma=0.999, eps=1e-8, cg=0.05):
|
| 38 |
+
super().__init__(params, dict(lr=lr,beta=beta,gamma=gamma,eps=eps,cg=cg))
|
| 39 |
+
@torch.no_grad()
|
| 40 |
+
def step(self, closure=None):
|
| 41 |
+
tot=0.0
|
| 42 |
+
for g in self.param_groups:
|
| 43 |
+
lr,beta,gamma,eps,c=g["lr"],g["beta"],g["gamma"],g["eps"],g["cg"]
|
| 44 |
+
for p in g["params"]:
|
| 45 |
+
if p.grad is None: continue
|
| 46 |
+
st=self.state[p]
|
| 47 |
+
if not st:
|
| 48 |
+
st["m"]=torch.zeros_like(p); st["v"]=torch.zeros_like(p); st["coh"]=torch.zeros_like(p)
|
| 49 |
+
m,v,h=st["m"],st["v"],st["coh"]; g0=p.grad
|
| 50 |
+
m.mul_(beta).add_(g0,alpha=1-beta)
|
| 51 |
+
v.mul_(gamma).addcmul_(g0,g0,value=1-gamma)
|
| 52 |
+
d=g0-m; h.mul_(0.9).add_(d.abs(),alpha=0.1)
|
| 53 |
+
lr_eff=lr/(1+c*h)
|
| 54 |
+
step=lr_eff*m/(v.sqrt()+eps)
|
| 55 |
+
p.add_(-step); tot += (step*step).sum().item()
|
| 56 |
+
return None, tot
|
| 57 |
+
|
| 58 |
+
# ---------------- LLM Proxy ------------------
|
| 59 |
+
class Block(nn.Module):
|
| 60 |
+
def __init__(self, d=512, heads=8, mlp_ratio=4):
|
| 61 |
+
super().__init__()
|
| 62 |
+
self.n1=nn.LayerNorm(d)
|
| 63 |
+
self.attn=nn.MultiheadAttention(d, heads, batch_first=True)
|
| 64 |
+
self.n2=nn.LayerNorm(d)
|
| 65 |
+
self.mlp=nn.Sequential(nn.Linear(d,int(d*mlp_ratio)), nn.GELU(), nn.Linear(int(d*mlp_ratio),d))
|
| 66 |
+
def forward(self, x):
|
| 67 |
+
h=x; x=self.n1(x); x,_=self.attn(x,x,x,need_weights=False); x=x+h
|
| 68 |
+
h=x; x=self.n2(x); x=x+self.mlp(x); return x
|
| 69 |
+
|
| 70 |
+
class LLMProxy(nn.Module):
|
| 71 |
+
def __init__(self, vocab=32768, d=512, L=6, heads=8, max_len=512):
|
| 72 |
+
super().__init__()
|
| 73 |
+
self.emb=nn.Embedding(vocab,d)
|
| 74 |
+
self.pos=nn.Parameter(torch.zeros(1,max_len,d))
|
| 75 |
+
self.blocks=nn.ModuleList([Block(d,heads) for _ in range(L)])
|
| 76 |
+
self.norm=nn.LayerNorm(d)
|
| 77 |
+
self.head=nn.Linear(d,vocab)
|
| 78 |
+
def forward(self, tok):
|
| 79 |
+
x=self.emb(tok)+self.pos[:,:tok.size(1)]
|
| 80 |
+
for blk in self.blocks: x=blk(x)
|
| 81 |
+
x=self.norm(x); return self.head(x)
|
| 82 |
+
|
| 83 |
+
# ---------------- Data -----------------------
|
| 84 |
+
def make_batch(batch=64, seq=256, vocab=32768):
|
| 85 |
+
x=torch.randint(0,vocab,(batch,seq))
|
| 86 |
+
y=torch.roll(x,shifts=-1,dims=1)
|
| 87 |
+
return x,y
|
| 88 |
+
|
| 89 |
+
# ---------------- Runner ---------------------
|
| 90 |
+
def run(mode="RFT", steps=1000, batch=64, seq=256, vocab=32768, log="stage8_llm.jsonl"):
|
| 91 |
+
set_seed(1234); tm=Telemetry(log); orb=Orbital()
|
| 92 |
+
dev="cuda" if torch.cuda.is_available() else "cpu"
|
| 93 |
+
model=LLMProxy(vocab=vocab,max_len=max(512,seq)).to(dev)
|
| 94 |
+
opt=DCLR(model.parameters()) if mode=="RFT" else torch.optim.Adam(model.parameters(),lr=5e-4)
|
| 95 |
+
loss_fn=nn.CrossEntropyLoss()
|
| 96 |
+
use_bf16=(dev=="cuda" and torch.cuda.is_bf16_supported())
|
| 97 |
+
for s in range(1,steps+1):
|
| 98 |
+
drift,flux=orb.step()
|
| 99 |
+
x,y=make_batch(batch,seq,vocab); x,y=x.to(dev),y.to(dev)
|
| 100 |
+
opt.zero_grad(set_to_none=True)
|
| 101 |
+
if use_bf16:
|
| 102 |
+
with torch.autocast(device_type="cuda",dtype=torch.bfloat16):
|
| 103 |
+
out=model(x); loss=loss_fn(out.view(-1,out.size(-1)), y.view(-1))
|
| 104 |
+
else:
|
| 105 |
+
out=model(x); loss=loss_fn(out.view(-1,out.size(-1)), y.view(-1))
|
| 106 |
+
loss.backward()
|
| 107 |
+
if isinstance(opt,DCLR): _,J=opt.step()
|
| 108 |
+
else: opt.step(); J=0.0
|
| 109 |
+
pred=out.argmax(-1); acc=(pred==y).float().mean().item()
|
| 110 |
+
tm.emit(mode=mode, step=s, drift=round(drift,3), flux=round(flux,3),
|
| 111 |
+
E_ret=0.994, coh=0.999,
|
| 112 |
+
loss=round(float(loss.item()),4), acc=round(float(acc),3),
|
| 113 |
+
J_step=round(float(J*1e-6),6))
|
| 114 |
+
tm.close()
|
| 115 |
+
return f"Stage 8 complete. Telemetry saved to {log}"
|