Prahaladha commited on
Commit
6cd7cb2
·
verified ·
1 Parent(s): a333576

sync learned_computer: 4 files from /dev/shm/neurv

Browse files
neurv/rt/t_agu.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys, collections, numpy as np, torch, torch.nn as nn, torch.nn.functional as F
2
+ sys.path.insert(0, "/dev/shm/neurv"); import rv, gen_rv as G
3
+ N_REG, N_TOK = rv.N_REG, G.N_TOK
4
+ src = open(sys.argv[1]).read()
5
+ ns = {"np": np, "torch": torch, "nn": nn, "F": F, "N_REG": N_REG, "N_TOK": N_TOK}
6
+ exec(src[src.index("def slot_features"):src.index("def loss_fn")], ns)
7
+ g = np.random.default_rng(3); uc = rv.new_uc()
8
+ progs = G.load_programs("/dev/shm/neurv/data/smoke/programs.jsonl")[:300]
9
+ pairs = [pr for p in progs for pr in G.program_samples(uc, p, g, 24)]
10
+ S = torch.from_numpy(np.stack([a for a, _ in pairs]).astype(np.int64)); N = np.stack([b for _, b in pairs])
11
+ pc = S[:, 32]; ins = torch.gather(S, 1, (N_REG + (pc // 4).clamp(0, 2047))[:, None])[:, 0] * ((pc < 0x2000) & (pc % 4 == 0))
12
+ v = ns["agu_values"](S, ins).numpy()
13
+ c = collections.Counter()
14
+ for i in range(len(pairs)):
15
+ s, n = S[i].numpy(), N[i]; w = int(ins[i]); op = w & 0x7F
16
+ if int(n[32]) == rv.TRAP_PC: continue
17
+ if op == 0x23: # store: the changed memory word (if any) must be the AGU's store word
18
+ ch = [j - N_REG for j in np.where(s != n)[0] if j >= N_REG]
19
+ if ch: c["store " + ("ok" if all(k == v[i, 3] >> 2 for k in ch) else "BAD")] += 1
20
+ elif op == 0x6F: c["jal " + ("ok" if n[32] == v[i, 5] else "BAD")] += 1
21
+ elif op == 0x67: c["jalr " + ("ok" if n[32] == v[i, 6] else "BAD")] += 1
22
+ elif op == 0x63: c["branch " + ("ok" if n[32] in (v[i, 4], v[i, 7]) else "BAD")] += 1
23
+ else: c["other pc+4 " + ("ok" if n[32] == v[i, 7] else "BAD")] += 1
24
+ print(dict(sorted(c.items())))
25
+ m = ns["RVNet"](256, 1); m(S[:4]) # the forward runs with the new wiring
26
+ print("forward ok")
neurv/rt/t_drills.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys, collections, numpy as np
2
+ sys.path.insert(0, sys.argv[1]); sys.path.insert(1, "/dev/shm/neurv")
3
+ import rv, gen_rv as G
4
+ g = np.random.default_rng(2); uc = rv.new_uc(); c = collections.Counter()
5
+ for _ in range(6000):
6
+ s = G.random_state(g, 0.7); n = rv.step_state(uc, s)
7
+ pc = int(s[32]); w = int(s[rv.N_REG + pc // 4]); op = w & 0x7F
8
+ k = {0x63: "branch", 0x03: "load", 0x23: "store"}.get(op)
9
+ if not k: continue
10
+ if int(n[32]) == rv.TRAP_PC: c[k + " trap"] += 1
11
+ elif op == 0x63: c["branch " + ("taken" if int(n[32]) != pc + 4 else "falls through")] += 1
12
+ else: c[k + " ok"] += 1
13
+ print(dict(sorted(c.items())))
neurv/rt/t_stream2.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys, time, collections, numpy as np
2
+ sys.path.insert(0, sys.argv[1]); sys.path.insert(1, "/dev/shm/neurv")
3
+ import rv, gen_rv as G
4
+ print("gen from", G.__file__)
5
+ it = iter(G.Stream("/dev/shm/neurv/data/smoke/programs.jsonl", seed=5, prog_share=0.5, fuzz_share=0.1, hard=0.7, new_input_p=0.4))
6
+ c = collections.Counter(); t0 = time.time(); n = 4000
7
+ for _ in range(n):
8
+ s, x = next(it); s, x = s.numpy(), x.numpy()
9
+ if int(x[32]) == rv.TRAP_PC: c["trap"] += 1
10
+ elif (s == x).all(): c["unchanged"] += 1
11
+ else:
12
+ pc = int(s[32]); w = int(s[rv.N_REG + pc // 4]) if pc < rv.MEM and pc % 4 == 0 else 0
13
+ c["mul-family" if (w & 0x7F) == 0x33 and (w >> 25) == 1 else ("load" if (w & 0x7F) == 0x03 else "other")] += 1
14
+ dt = time.time() - t0
15
+ print(f"{n} samples {dt:.1f}s = {n / dt:.0f}/s per worker;", {k: round(v / n, 3) for k, v in c.most_common()})
16
+ g = np.random.default_rng(1)
17
+ for _ in range(4): print(repr(G.rand_input(g)[:60]))
neurv/runs/rv5best/step_2000.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5a71bb135ab496179d5eee2890a8a4fd8e738fdd575906ca31f3fb48f294fcd8
3
+ size 3087802401