sync learned_computer: 4 files from /dev/shm/neurv
Browse filesFiles changed (4)
- neurv/runs/rv3_s2000_copy.ptAdded+3
- neurv/rv_regs.pyAdded+30
- neurv/stop_watch.shAdded+6
- neurv/train_rv.py+13-1
123version https://git-lfs.github.com/spec/v1oid sha256:fe8ef36c3497daef7c43e9711189162a3b6eb14c490efc4d28d639723e2f92f9size 3071399157123456789101112131415161718192021222324252627282930"""Show unchanged-register mistakes: input value, predicted value, true value, which register."""import collections, os, sysimport numpy as np, torch, torch.nn as nn, torch.nn.functional as FHERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE)import rv, gen_rv as GN_REG, N_TOK = rv.N_REG, G.N_TOKsrc = open(os.path.join(HERE, "train_rv.py")).read()ns = {"np": np, "torch": torch, "nn": nn, "F": F, "N_REG": N_REG, "N_TOK": N_TOK}exec(src[src.index("def slot_features"):src.index("def loss_fn")], ns)st = torch.load(sys.argv[1], map_location="cpu", weights_only=False)model = ns["RVNet"](st["args"]["d"], st["args"]["layers"]).cuda().eval(); model.load_state_dict(st["model"])progs = G.load_programs(sys.argv[2])[:40]g = np.random.default_rng(7); uc = rv.new_uc(); X, Y = [], []for p in progs: for s, n in G.program_samples(uc, p, g, 6): X.append(s); Y.append(n)X = np.stack(X).astype(np.int64); Y = np.stack(Y).astype(np.int64)with torch.no_grad(), torch.autocast("cuda", dtype=torch.bfloat16): by = model(torch.from_numpy(X).cuda()).argmax(-1)P = (by.long() << torch.tensor([0, 8, 16, 24], device="cuda")).sum(-1).cpu().numpy()bad = collections.Counter(); tot = collections.Counter(); ex = []for i in range(len(X)): for r in range(32): if X[i, r] == Y[i, r]: tot[r] += 1 if P[i, r] != Y[i, r]: bad[r] += 1 if len(ex) < 12: ex.append((r, hex(X[i, r]), hex(P[i, r]), hex(Y[i, r])))print("wrong unchanged copies per register:", {r: f"{bad[r]}/{tot[r]}" for r in range(32) if tot[r]})for e in ex: print("x%d in %s pred %s true %s" % e)123456#!/bin/bash# stop a watched learned-RISC-V run: its watchdog first (so it cannot relaunch), then the run. usage: stop_watch.sh <name>N=$1for p in $(pgrep -f "bash /dev/shm/neurv/watch_rv.sh $N "); do kill $p; donebash /dev/shm/neurv/kill_rv.sh $Necho "watchdogs left $(pgrep -fc "watch_rv.sh $N ")"79 unmodified lines8081828384851 unmodified line87888990919265 unmodified lines15815916016116216316479 unmodified linesself.norm = nn.RMSNorm(d)self.head = nn.Linear(d, 4 * 256, bias=False)self.copy = nn.Parameter(torch.tensor(4.0)) # copy prior: each output byte starts leaning toward its input byteself.ckpt_grad = ckpt_graddef forward(self, s): # s [B, N_TOK] int64 holding uint32 values -> logits [B, N_TOK, 4, 256]1 unmodified linefor k in range(4):x = x + self.byte[k]((s >> (8 * k)) & 255)x = x + self.bits(((s[..., None] >> self.shift) & 1).to(x.dtype))for b in self.blocks:x = torch.utils.checkpoint.checkpoint(b, x, use_reentrant=False) if (self.ckpt_grad and self.training) else b(x)lg = self.head(self.norm(x)).view(s.shape[0], N_TOK, 4, 256)65 unmodified lineselif a.init:st = torch.load(a.init, map_location="cpu", weights_only=False)miss, unexp = model.load_state_dict(st["model"], strict=False)assert set(miss) <= {"copy"} and not unexp, (miss, unexp)say(event="init", path=a.init, from_step=st["step"], missing=list(miss))loader = iter(torch.utils.data.DataLoader(G.Stream(a.programs, seed=rank + 97 * start + 1, prog_share=a.prog_share, hard=a.hard),batch_size=a.batch, num_workers=a.workers, pin_memory=True, persistent_workers=True,79 unmodified lines80818283848586878889901 unmodified line929394959697989910010110210310465 unmodified lines17017117217317417517679 unmodified linesself.norm = nn.RMSNorm(d)self.head = nn.Linear(d, 4 * 256, bias=False)self.copy = nn.Parameter(torch.tensor(4.0)) # copy prior: each output byte starts leaning toward its input byte# fetch unit (fixed wiring, as in a real CPU): the word at memory[PC], read from the network's OWN input state,# is shown to every token. Decoding and executing it is still entirely the network's job.self.fbyte = nn.ModuleList([nn.Embedding(256, d) for _ in range(4)])self.fbits = nn.Linear(32, d, bias=False)self.fvalid = nn.Embedding(2, d)self.ckpt_grad = ckpt_graddef forward(self, s): # s [B, N_TOK] int64 holding uint32 values -> logits [B, N_TOK, 4, 256]1 unmodified linefor k in range(4):x = x + self.byte[k]((s >> (8 * k)) & 255)x = x + self.bits(((s[..., None] >> self.shift) & 1).to(x.dtype))pc = s[:, 32]valid = (pc < (N_TOK - N_REG) * 4) & (pc % 4 == 0)ins = torch.gather(s, 1, (N_REG + (pc // 4).clamp(0, N_TOK - N_REG - 1))[:, None])[:, 0] * validf = self.fvalid(valid.long()) + self.fbits(((ins[:, None] >> self.shift) & 1).to(x.dtype))for k in range(4):f = f + self.fbyte[k]((ins >> (8 * k)) & 255)x = x + f[:, None, :]for b in self.blocks:x = torch.utils.checkpoint.checkpoint(b, x, use_reentrant=False) if (self.ckpt_grad and self.training) else b(x)lg = self.head(self.norm(x)).view(s.shape[0], N_TOK, 4, 256)65 unmodified lineselif a.init:st = torch.load(a.init, map_location="cpu", weights_only=False)miss, unexp = model.load_state_dict(st["model"], strict=False)assert all(k == "copy" or k.startswith(("fbyte", "fbits", "fvalid")) for k in miss) and not unexp, (miss, unexp)say(event="init", path=a.init, from_step=st["step"], missing=list(miss))loader = iter(torch.utils.data.DataLoader(G.Stream(a.programs, seed=rank + 97 * start + 1, prog_share=a.prog_share, hard=a.hard),batch_size=a.batch, num_workers=a.workers, pin_memory=True, persistent_workers=True,
neurv/runs/rv3_s2000_copy.ptAdded+3
neurv/rv_regs.pyAdded+30
neurv/stop_watch.shAdded+6
neurv/train_rv.py+13-1