Upload scripts/fast4gpu_stage2_ss.py with huggingface_hub
Browse files
scripts/fast4gpu_stage2_ss.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch, sys, os, time, json, random
|
| 2 |
+
sys.path.insert(0, "/workspace/fractus-cte")
|
| 3 |
+
os.chdir("/workspace/fractus-cte")
|
| 4 |
+
from fractus.continuous_engine import ContinuousThoughtEngine
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
|
| 7 |
+
GPU = int(os.environ.get("GPU_ID", "0"))
|
| 8 |
+
LB_COEF = float(os.environ.get("LB_COEF", "0.02"))
|
| 9 |
+
GATE_TEMP = float(os.environ.get("GATE_TEMP", "2.5"))
|
| 10 |
+
LR = float(os.environ.get("LR", "5e-4"))
|
| 11 |
+
EMA_BETA = 0.98
|
| 12 |
+
SS_PROB = float(os.environ.get("SS_PROB", "0.2"))
|
| 13 |
+
SS_RATE = float(os.environ.get("SS_RATE", "0.3"))
|
| 14 |
+
torch.manual_seed(42 + GPU)
|
| 15 |
+
device = torch.device("cuda:0")
|
| 16 |
+
|
| 17 |
+
TARGET = dict(d_model=1280, n_heads=20, d_head=64, n_levels=2, n_oscillators=16,
|
| 18 |
+
coupling_rank=8, n_experts=128, top_k=2, expert_d_ff=2048, siren_rank=64, n_layers=16)
|
| 19 |
+
|
| 20 |
+
print(f"GPU {GPU}: SS sequential LR={LR} SS_PROB={SS_PROB}", flush=True)
|
| 21 |
+
ck = torch.load(f"checkpoints/fractus_1b_gpu{GPU}.pt", map_location="cpu", weights_only=False)
|
| 22 |
+
sd = ck.get("model_state", ck)
|
| 23 |
+
clean = {(k[10:] if k.startswith("_orig_mod.") else k): v for k,v in sd.items()}
|
| 24 |
+
eng = ContinuousThoughtEngine(vocab_size=50257, **{k: TARGET[k] for k in TARGET})
|
| 25 |
+
own = eng.state_dict()
|
| 26 |
+
for k,v in clean.items():
|
| 27 |
+
if k in own and own[k].shape == v.shape:
|
| 28 |
+
own[k] = v
|
| 29 |
+
elif k in own and v.dim()>=1 and own[k].dim()>=1 and v.shape[0]>own[k].shape[0] and v.shape[1:]==own[k].shape[1:]:
|
| 30 |
+
own[k] = v[:own[k].shape[0]].contiguous()
|
| 31 |
+
eng.load_state_dict(own, strict=False)
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
for blk in eng.blocks:
|
| 34 |
+
blk.moe.temperature = GATE_TEMP
|
| 35 |
+
eng = eng.to(device)
|
| 36 |
+
eng.reset_thought(2)
|
| 37 |
+
opt = torch.optim.SGD(eng.parameters(), lr=LR, momentum=0.9)
|
| 38 |
+
tokens = torch.load(f"data/shard_gpu{GPU}.pt", weights_only=False).to(torch.int64)
|
| 39 |
+
B, seq_len = 2, 128
|
| 40 |
+
step_tokens = B * seq_len
|
| 41 |
+
man = json.load(open("/workspace/RESUME_MANIFEST_SS.json"))
|
| 42 |
+
start_token = int(man["gpus"][str(GPU)]["start_token"])
|
| 43 |
+
print(f"GPU {GPU}: RESUME {start_token}", flush=True)
|
| 44 |
+
t0=time.time(); ema_tf=None; ema_ss=None; n=0; tok_sess=0
|
| 45 |
+
for start in range(start_token, len(tokens)-step_tokens-seq_len-1, step_tokens):
|
| 46 |
+
chunk = tokens[start:start+step_tokens].view(B, seq_len).to(device)
|
| 47 |
+
target = tokens[start+1:start+step_tokens+1].view(B, seq_len).to(device)
|
| 48 |
+
with torch.autocast("cuda", dtype=torch.bfloat16):
|
| 49 |
+
out = eng.tick_chunk_train(chunk)
|
| 50 |
+
logits, lb = out if isinstance(out, tuple) else (out, eng.last_lb_loss)
|
| 51 |
+
ce_tf = F.cross_entropy(logits.reshape(-1, logits.size(-1)), target.reshape(-1))
|
| 52 |
+
loss = ce_tf + LB_COEF * lb
|
| 53 |
+
opt.zero_grad(); loss.backward()
|
| 54 |
+
torch.nn.utils.clip_grad_norm_(eng.parameters(), 1.0); opt.step()
|
| 55 |
+
tf_v=float(ce_tf.item()); lb_v=float(lb.detach().item()) if torch.is_tensor(lb) else float(lb)
|
| 56 |
+
ema_tf = tf_v if ema_tf is None else EMA_BETA*ema_tf+(1-EMA_BETA)*tf_v
|
| 57 |
+
ce_ss_v=None
|
| 58 |
+
if random.random() < SS_RATE:
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
samp = torch.multinomial(torch.softmax(logits.detach().float().reshape(-1, logits.size(-1))/0.9, -1), 1).view(B, seq_len)
|
| 61 |
+
mixed = chunk.clone()
|
| 62 |
+
use_ss = torch.rand(B, seq_len, device=device) < SS_PROB
|
| 63 |
+
use_ss[:,0]=False
|
| 64 |
+
prev = torch.cat([chunk[:,:1], samp[:,:-1]], 1)
|
| 65 |
+
mixed = torch.where(use_ss, prev, mixed)
|
| 66 |
+
with torch.autocast("cuda", dtype=torch.bfloat16):
|
| 67 |
+
out2 = eng.tick_chunk_train(mixed)
|
| 68 |
+
logits2, lb2 = out2 if isinstance(out2, tuple) else (out2, eng.last_lb_loss)
|
| 69 |
+
ce_ss = F.cross_entropy(logits2.reshape(-1, logits2.size(-1)), target.reshape(-1))
|
| 70 |
+
loss2 = 0.5*ce_ss + LB_COEF*lb2
|
| 71 |
+
opt.zero_grad(); loss2.backward()
|
| 72 |
+
torch.nn.utils.clip_grad_norm_(eng.parameters(), 1.0); opt.step()
|
| 73 |
+
ce_ss_v=float(ce_ss.item())
|
| 74 |
+
ema_ss = ce_ss_v if ema_ss is None else EMA_BETA*ema_ss+(1-EMA_BETA)*ce_ss_v
|
| 75 |
+
n+=1; tok_sess+=step_tokens
|
| 76 |
+
if n%50==0:
|
| 77 |
+
tps=tok_sess/max(time.time()-t0,1e-6)
|
| 78 |
+
extra=f" ss={ce_ss_v:.3f} ema_ss={ema_ss:.3f}" if ce_ss_v is not None and ema_ss is not None else ""
|
| 79 |
+
print(f"GPU {GPU}: {start+step_tokens:>12,} tf={tf_v:.3f} ema_tf={ema_tf:.3f}{extra} lb={lb_v:.3f} {tps:.0f} tok/s [ss]", flush=True)
|
| 80 |
+
if n%1000==0:
|
| 81 |
+
torch.save({"model_state": eng.state_dict(), "config": {**TARGET, "gpu": GPU, "ss": True}}, f"checkpoints/fractus_1b_gpu{GPU}.pt")
|
| 82 |
+
print(f"GPU {GPU}: saved [ss]", flush=True)
|
| 83 |
+
print("DONE", flush=True)
|