Prahaladha commited on
Commit
2b7e6d0
·
verified ·
1 Parent(s): 30bd7c0

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

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ neurv/data/smoke/programs.jsonl filter=lfs diff=lfs merge=lfs -text
neurv/data/smoke/programs.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:01abdd63e9c8d7598194f3960c25f1645b2052ae782e4e68d4976255029f70e6
3
+ size 13092206
neurv/data/smoke/stats.json ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "counts": {
3
+ "ok": 1558,
4
+ "too_big": 306,
5
+ "compile": 416,
6
+ "tiny": 307,
7
+ "no_halt": 311,
8
+ "undef": 102
9
+ },
10
+ "undef_top": [
11
+ [
12
+ "pow",
13
+ 39
14
+ ],
15
+ [
16
+ "strstr",
17
+ 12
18
+ ],
19
+ [
20
+ "getchar_unlocked",
21
+ 8
22
+ ],
23
+ [
24
+ "hypot",
25
+ 6
26
+ ],
27
+ [
28
+ "scanf_s",
29
+ 6
30
+ ],
31
+ [
32
+ "strtok",
33
+ 5
34
+ ],
35
+ [
36
+ "cbrt",
37
+ 3
38
+ ],
39
+ [
40
+ "main",
41
+ 3
42
+ ],
43
+ [
44
+ "acos",
45
+ 3
46
+ ],
47
+ [
48
+ "putchar_unlocked",
49
+ 2
50
+ ],
51
+ [
52
+ "atof",
53
+ 2
54
+ ],
55
+ [
56
+ "assert",
57
+ 2
58
+ ],
59
+ [
60
+ "atan2",
61
+ 2
62
+ ],
63
+ [
64
+ "fclose",
65
+ 1
66
+ ],
67
+ [
68
+ "fopen",
69
+ 1
70
+ ],
71
+ [
72
+ "write",
73
+ 1
74
+ ],
75
+ [
76
+ "strnlen",
77
+ 1
78
+ ],
79
+ [
80
+ "atan",
81
+ 1
82
+ ],
83
+ [
84
+ "realloc",
85
+ 1
86
+ ],
87
+ [
88
+ "fprint",
89
+ 1
90
+ ],
91
+ [
92
+ "orintf",
93
+ 1
94
+ ],
95
+ [
96
+ "print",
97
+ 1
98
+ ],
99
+ [
100
+ "pritnf",
101
+ 1
102
+ ],
103
+ [
104
+ "memchr",
105
+ 1
106
+ ],
107
+ [
108
+ "ungetc",
109
+ 1
110
+ ],
111
+ [
112
+ "time",
113
+ 1
114
+ ],
115
+ [
116
+ "feof",
117
+ 1
118
+ ],
119
+ [
120
+ "maxHeapify",
121
+ 1
122
+ ],
123
+ [
124
+ "itoa",
125
+ 1
126
+ ],
127
+ [
128
+ "rewind",
129
+ 1
130
+ ],
131
+ [
132
+ "system",
133
+ 1
134
+ ],
135
+ [
136
+ "strtol",
137
+ 1
138
+ ],
139
+ [
140
+ "freopen",
141
+ 1
142
+ ],
143
+ [
144
+ "read",
145
+ 1
146
+ ],
147
+ [
148
+ "snprintf",
149
+ 1
150
+ ]
151
+ ]
152
+ }
neurv/logs/prep_smoke.log ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ jobs 3000
2
+ 2000 {'ok': 1040, 'too_big': 205, 'compile': 269, 'tiny': 216, 'no_halt': 201, 'undef': 69}
3
+ PREP_DONE {'ok': 1558, 'too_big': 306, 'compile': 416, 'tiny': 307, 'no_halt': 311, 'undef': 102}
4
+ undef top: [('pow', 39), ('strstr', 12), ('getchar_unlocked', 8), ('hypot', 6), ('scanf_s', 6), ('strtok', 5), ('cbrt', 3), ('main', 3), ('acos', 3), ('putchar_unlocked', 2), ('atof', 2), ('assert', 2), ('atan2', 2), ('fclose', 1), ('fopen', 1), ('write', 1), ('strnlen', 1), ('atan', 1), ('realloc', 1), ('fprint', 1), ('orintf', 1), ('print', 1), ('pritnf', 1), ('memchr', 1), ('ungetc', 1), ('time', 1), ('feof', 1), ('maxHeapify', 1), ('itoa', 1), ('rewind', 1)]
5
+ rc=0
neurv/prep_codenet.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """CodeNet C submissions -> programs that compile for our 8 KiB RV32IM machine and run on it.
2
+
3
+ For each submission: drop its #include lines, add our io.h, compile with zig, run on a random input on the real chip (Unicorn).
4
+ Keeps: image + input + instruction count. Logs why the others failed (link error symbol, too big, crash).
5
+ Usage: python prep_codenet.py <parquet ...> --out DIR [--limit N] [--procs P] [--max_steps S]
6
+ """
7
+ import argparse
8
+ import collections
9
+ import json
10
+ import multiprocessing as mp
11
+ import os
12
+ import random
13
+ import re
14
+ import subprocess
15
+ import sys
16
+
17
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
18
+
19
+ INC = re.compile(r"^\s*#\s*include\s*[<\"][^>\"]*[>\"]\s*$", re.M)
20
+ UNDEF = re.compile(r"undefined symbol: (\S+)")
21
+
22
+
23
+ def rand_input(rng):
24
+ """A plausible stdin: a few lines of small integers (most CodeNet programs read numbers)."""
25
+ k = rng.choice([1, 2, 3, 5, 8])
26
+ lines = [str(k)] + [" ".join(str(rng.randint(0, rng.choice([9, 99, 999]))) for _ in range(rng.randint(1, 6))) for _ in range(k)]
27
+ return ("\n".join(lines) + "\n").encode()
28
+
29
+
30
+ def work(job):
31
+ import rv
32
+ sid, code, seed = job
33
+ rng = random.Random(seed)
34
+ src = '#include "io.h"\n' + INC.sub("", code)
35
+ try:
36
+ img = rv.compile_c(src, "p")
37
+ except subprocess.CalledProcessError as e:
38
+ err = (e.stderr or b"").decode(errors="replace")
39
+ u = UNDEF.findall(err)
40
+ if u:
41
+ return sid, "undef", sorted(set(u))[:4], None
42
+ if "overflow" in err or "will not fit" in err or "exceeds" in err:
43
+ return sid, "too_big", [], None
44
+ return sid, "compile", [err.strip().splitlines()[-1][:120] if err.strip() else "?"], None
45
+ except ValueError:
46
+ return sid, "too_big", [], None
47
+ except Exception as e: # timeout etc
48
+ return sid, "compile", [type(e).__name__], None
49
+ inp = rand_input(rng)
50
+ steps, out, halted = rv.run(img, inp, max_steps=MAX_STEPS)
51
+ if steps < 50:
52
+ return sid, "tiny", [], None
53
+ return sid, "ok" if halted else "no_halt", [], {"id": sid, "img": img.hex(), "inp": inp.hex(), "steps": steps,
54
+ "halted": halted, "out": out[:120].decode("latin-1")}
55
+
56
+
57
+ def init(ms):
58
+ global MAX_STEPS
59
+ MAX_STEPS = ms
60
+ os.environ["RV_TMP"] = "/dev/shm/neurv/tmp"
61
+
62
+
63
+ if __name__ == "__main__":
64
+ ap = argparse.ArgumentParser()
65
+ ap.add_argument("parquet", nargs="+")
66
+ ap.add_argument("--out", required=True)
67
+ ap.add_argument("--limit", type=int, default=0)
68
+ ap.add_argument("--procs", type=int, default=64)
69
+ ap.add_argument("--max_steps", type=int, default=300000)
70
+ a = ap.parse_args()
71
+ import pyarrow.parquet as pq
72
+ os.makedirs(a.out, exist_ok=True)
73
+ os.makedirs("/dev/shm/neurv/tmp", exist_ok=True)
74
+ jobs = []
75
+ for f in a.parquet:
76
+ t = pq.read_table(f, columns=["s_id", "code", "status"])
77
+ for sid, code, st in zip(t["s_id"].to_pylist(), t["code"].to_pylist(), t["status"].to_pylist()):
78
+ if code and len(code) < 6000:
79
+ jobs.append((sid, code, hash(sid) & 0xFFFFFFFF))
80
+ random.Random(0).shuffle(jobs)
81
+ if a.limit:
82
+ jobs = jobs[:a.limit]
83
+ print(f"jobs {len(jobs)}", flush=True)
84
+ cnt, undef = collections.Counter(), collections.Counter()
85
+ with open(f"{a.out}/programs.jsonl", "w") as fo, mp.get_context("fork").Pool(a.procs, init, (a.max_steps,)) as pool:
86
+ for i, (sid, kind, info, rec) in enumerate(pool.imap_unordered(work, jobs, chunksize=8)):
87
+ cnt[kind] += 1
88
+ if kind == "undef":
89
+ undef.update(info)
90
+ if rec:
91
+ fo.write(json.dumps(rec) + "\n")
92
+ if (i + 1) % 2000 == 0:
93
+ print(f"{i + 1} {dict(cnt)}", flush=True)
94
+ json.dump({"counts": cnt, "undef_top": undef.most_common(60)}, open(f"{a.out}/stats.json", "w"), indent=1)
95
+ print("PREP_DONE", dict(cnt), "\nundef top:", undef.most_common(30), flush=True)
neurv/prep_full.sh ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # full CodeNet C filter -> $HOME/neurv_data/codenet (disk, not RAM disk)
3
+ cd /dev/shm/neurv
4
+ OUT=$HOME/neurv_data/codenet
5
+ mkdir -p $OUT logs
6
+ export PYTHONPATH=/dev/shm/neurv/pkgs ZIG_GLOBAL_CACHE_DIR=/dev/shm/neurv/zigcache OMP_NUM_THREADS=1
7
+ echo "$(date -u +%T) PREP_FULL start out=$OUT"
8
+ nice -n 19 /dev/shm/prh/venv/bin/python -u prep_codenet.py src/codenet/c0.parquet src/codenet/c1.parquet --out $OUT --procs 100 > logs/prep_full.log 2>&1
9
+ echo "rc=$?" >> logs/prep_full.log
neurv/prep_smoke.sh ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ cd /dev/shm/neurv
3
+ mkdir -p logs
4
+ export PYTHONPATH=/dev/shm/neurv/pkgs ZIG_GLOBAL_CACHE_DIR=/dev/shm/neurv/zigcache OMP_NUM_THREADS=1
5
+ nice -n 19 /dev/shm/prh/venv/bin/python -u prep_codenet.py src/codenet/c0.parquet src/codenet/c1.parquet --out data/smoke --limit 3000 --procs 48 > logs/prep_smoke.log 2>&1
6
+ echo "rc=$?" >> logs/prep_smoke.log
neurv/rt/crt0.S ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # entry: stack below the I/O area, call main, then spin forever on one instruction (the halted state)
2
+ .section .text.start
3
+ .globl _start
4
+ _start:
5
+ li sp, 0x1C00
6
+ call main
7
+ .globl _halt
8
+ _halt:
9
+ j _halt
neurv/rt/io.c ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "io.h"
2
+ #include <stdarg.h>
3
+ int putchar(int c){ int p = OUT_PTR; if(p < OUT_LEN){ OUT_BASE[p] = (char)c; OUT_PTR = p + 1; } return c; }
4
+ int getchar(void){ int p = IN_PTR; if(p >= IN_LEN || IN_BASE[p] == 0) return EOF; IN_PTR = p + 1; return (unsigned char)IN_BASE[p]; }
5
+ int puts(const char *s){ while(*s) putchar(*s++); putchar('\n'); return 0; }
6
+ static void put_u(unsigned long long v, int base, int width, char pad){ char b[24]; int n = 0;
7
+ do { int d = (int)(v % base); b[n++] = d < 10 ? '0' + d : 'a' + d - 10; v /= base; } while(v);
8
+ while(n < width--) putchar(pad); while(n) putchar(b[--n]); }
9
+ int printf(const char *f, ...){ va_list ap; va_start(ap, f);
10
+ for(; *f; f++){ if(*f != '%'){ putchar(*f); continue; } f++;
11
+ char pad = ' '; int width = 0, lng = 0;
12
+ if(*f == '0'){ pad = '0'; f++; } while(*f >= '0' && *f <= '9') width = width * 10 + (*f++ - '0');
13
+ while(*f == 'l'){ lng++; f++; } if(*f == 'z' || *f == 'h') f++;
14
+ if(*f == 'd' || *f == 'i'){ long long v = lng >= 2 ? va_arg(ap, long long) : va_arg(ap, int);
15
+ if(v < 0){ putchar('-'); v = -v; if(width) width--; } put_u((unsigned long long)v, 10, width, pad); }
16
+ else if(*f == 'u'){ unsigned long long v = lng >= 2 ? va_arg(ap, unsigned long long) : va_arg(ap, unsigned); put_u(v, 10, width, pad); }
17
+ else if(*f == 'x'){ unsigned long long v = lng >= 2 ? va_arg(ap, unsigned long long) : va_arg(ap, unsigned); put_u(v, 16, width, pad); }
18
+ else if(*f == 'c') putchar(va_arg(ap, int));
19
+ else if(*f == 's'){ const char *s = va_arg(ap, const char *); while(*s) putchar(*s++); }
20
+ else if(*f == '%') putchar('%'); }
21
+ va_end(ap); return 0; }
22
+ static int peekc = -2;
23
+ static int gc(void){ if(peekc != -2){ int c = peekc; peekc = -2; return c; } return getchar(); }
24
+ static void ungc(int c){ peekc = c; }
25
+ static int skipws(void){ int c; do c = gc(); while(c == ' ' || c == '\n' || c == '\t' || c == '\r'); return c; }
26
+ int scanf(const char *f, ...){ va_list ap; va_start(ap, f); int n = 0;
27
+ for(; *f; f++){ if(*f == ' ' || *f == '\n'){ int c = skipws(); ungc(c); continue; } if(*f != '%'){ int c = gc(); if(c != *f){ ungc(c); break; } continue; }
28
+ f++; int lng = 0; while(*f == 'l'){ lng++; f++; }
29
+ if(*f == 'd' || *f == 'u'){ int c = skipws(), neg = 0; long long v = 0; if(c == '-'){ neg = 1; c = gc(); }
30
+ if(c < '0' || c > '9'){ ungc(c); break; } while(c >= '0' && c <= '9'){ v = v * 10 + (c - '0'); c = gc(); } ungc(c); if(neg) v = -v;
31
+ if(lng >= 2) *va_arg(ap, long long *) = v; else *va_arg(ap, int *) = (int)v; n++; }
32
+ else if(*f == 's'){ char *s = va_arg(ap, char *); int c = skipws(); if(c == EOF) break; while(c != EOF && c != ' ' && c != '\n'){ *s++ = (char)c; c = gc(); } *s = 0; ungc(c); n++; }
33
+ else if(*f == 'c'){ int c = gc(); if(c == EOF) break; *va_arg(ap, char *) = (char)c; n++; } }
34
+ va_end(ap); return n ? n : EOF; }
35
+ size_t strlen(const char *s){ size_t n = 0; while(s[n]) n++; return n; }
36
+ int strcmp(const char *a, const char *b){ while(*a && *a == *b){ a++; b++; } return (unsigned char)*a - (unsigned char)*b; }
37
+ char *strcpy(char *d, const char *s){ char *r = d; while((*d++ = *s++)); return r; }
38
+ void *memset(void *p, int c, size_t n){ char *q = p; while(n--) *q++ = (char)c; return p; }
39
+ void *memcpy(void *d, const void *s, size_t n){ char *a = d; const char *b = s; while(n--) *a++ = *b++; return d; }
40
+ extern char __heap_start[]; static char *brk_ = 0;
41
+ void *malloc(size_t n){ if(!brk_) brk_ = __heap_start; n = (n + 3) & ~3u; char *p = brk_; if((unsigned)(brk_ + n) > 0x1800) return NULL; brk_ += n; return p; }
42
+ void *calloc(size_t n, size_t m){ void *p = malloc(n * m); if(p) memset(p, 0, n * m); return p; }
43
+ void free(void *p){ (void)p; }
44
+ int abs(int x){ return x < 0 ? -x : x; }
45
+ /* ---- the rest of the common C library, kept tiny ---- */
46
+ static FILE f_in = {0}, f_out = {1}, f_err = {2};
47
+ FILE *stdin = &f_in, *stdout = &f_out, *stderr = &f_err;
48
+ int fputc(int c, FILE *f){ (void)f; return putchar(c); }
49
+ int putc(int c, FILE *f){ (void)f; return putchar(c); }
50
+ int fputs(const char *s, FILE *f){ (void)f; while(*s) putchar(*s++); return 0; }
51
+ int fgetc(FILE *f){ (void)f; return gc(); }
52
+ int getc(FILE *f){ (void)f; return gc(); }
53
+ int fflush(FILE *f){ (void)f; return 0; }
54
+ char *fgets(char *s, int n, FILE *f){ (void)f; int i = 0, c = 0; while(i < n - 1 && (c = gc()) != EOF){ s[i++] = (char)c; if(c == '\n') break; } if(i == 0 && c == EOF) return NULL; s[i] = 0; return s; }
55
+ char *gets(char *s){ int i = 0, c; while((c = gc()) != EOF && c != '\n') s[i++] = (char)c; s[i] = 0; return (i == 0 && c == EOF) ? NULL : s; }
56
+ extern void _halt(void);
57
+ void exit(int code){ (void)code; _halt(); }
58
+ long long atoll(const char *s){ long long v = 0; int neg = 0; while(*s == ' ') s++; if(*s == '-'){ neg = 1; s++; } else if(*s == '+') s++; while(*s >= '0' && *s <= '9') v = v * 10 + (*s++ - '0'); return neg ? -v : v; }
59
+ int atoi(const char *s){ return (int)atoll(s); }
60
+ long atol(const char *s){ return (long)atoll(s); }
61
+ long labs(long x){ return x < 0 ? -x : x; }
62
+ long long llabs(long long x){ return x < 0 ? -x : x; }
63
+ static unsigned rnd_ = 1;
64
+ int rand(void){ rnd_ = rnd_ * 1103515245u + 12345u; return (rnd_ >> 16) & 0x7fff; }
65
+ void srand(unsigned s){ rnd_ = s; }
66
+ static void swapb(char *a, char *b, size_t n){ while(n--){ char t = *a; *a++ = *b; *b++ = t; } }
67
+ void qsort(void *base, size_t n, size_t sz, int (*cmp)(const void *, const void *)){ char *b = base;
68
+ for(size_t i = 1; i < n; i++) for(size_t j = i; j > 0 && cmp(b + (j - 1) * sz, b + j * sz) > 0; j--) swapb(b + (j - 1) * sz, b + j * sz, sz); }
69
+ int strncmp(const char *a, const char *b, size_t n){ while(n && *a && *a == *b){ a++; b++; n--; } return n ? (unsigned char)*a - (unsigned char)*b : 0; }
70
+ char *strcat(char *d, const char *s){ char *r = d; while(*d) d++; while((*d++ = *s++)); return r; }
71
+ char *strncpy(char *d, const char *s, size_t n){ char *r = d; while(n && (*d++ = *s++)) n--; while(n--) *d++ = 0; return r; }
72
+ char *strchr(const char *s, int c){ while(*s){ if(*s == (char)c) return (char *)s; s++; } return c ? NULL : (char *)s; }
73
+ char *strrev(char *s){ size_t n = strlen(s); for(size_t i = 0; i < n / 2; i++){ char t = s[i]; s[i] = s[n - 1 - i]; s[n - 1 - i] = t; } return s; }
74
+ void *memmove(void *d, const void *s, size_t n){ char *a = d; const char *b = s; if(a < b){ while(n--) *a++ = *b++; } else { a += n; b += n; while(n--) *--a = *--b; } return d; }
75
+ int memcmp(const void *a, const void *b, size_t n){ const unsigned char *x = a, *y = b; while(n--){ if(*x != *y) return *x - *y; x++; y++; } return 0; }
76
+ int isdigit(int c){ return c >= '0' && c <= '9'; } int isalpha(int c){ return (c | 32) >= 'a' && (c | 32) <= 'z'; }
77
+ int isspace(int c){ return c == ' ' || (c >= 9 && c <= 13); } int isupper(int c){ return c >= 'A' && c <= 'Z'; }
78
+ int islower(int c){ return c >= 'a' && c <= 'z'; } int isalnum(int c){ return isdigit(c) || isalpha(c); }
79
+ int toupper(int c){ return islower(c) ? c - 32 : c; } int tolower(int c){ return isupper(c) ? c + 32 : c; }
80
+ static char *sp_; static int sput(int c){ *sp_++ = (char)c; *sp_ = 0; return c; }
81
+ int sprintf(char *s, const char *f, ...){ (void)s; (void)f; return 0; }
82
+ int sscanf(const char *s, const char *f, ...){ (void)s; (void)f; return 0; }
83
+ int fscanf(FILE *fp, const char *f, ...){ (void)fp; (void)f; return 0; }
84
+ int fprintf(FILE *fp, const char *f, ...){ (void)fp; (void)f; return 0; }
neurv/rt/io.h ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* tiny libc for the learned RISC-V machine. Output and input live in memory, so the whole machine state is just RAM + registers. */
2
+ #ifndef IO_H
3
+ #define IO_H
4
+ #define OUT_BASE ((volatile char *)0x1C00)
5
+ #define OUT_LEN 0x300
6
+ #define OUT_PTR (*(volatile int *)0x1F00)
7
+ #define IN_PTR (*(volatile int *)0x1F04)
8
+ #define IN_BASE ((volatile char *)0x1F08)
9
+ #define IN_LEN 0xF8
10
+ typedef unsigned int size_t;
11
+ #define NULL ((void *)0)
12
+ #define EOF (-1)
13
+ #define INT_MAX 2147483647
14
+ #define INT_MIN (-INT_MAX - 1)
15
+ #define LLONG_MAX 9223372036854775807LL
16
+ #define bool _Bool
17
+ #define true 1
18
+ #define false 0
19
+ typedef signed char int8_t; typedef unsigned char uint8_t; typedef short int16_t; typedef unsigned short uint16_t;
20
+ typedef int int32_t; typedef unsigned int uint32_t; typedef long long int64_t; typedef unsigned long long uint64_t;
21
+ typedef int ssize_t; typedef long ptrdiff_t; typedef unsigned int uintptr_t; typedef int intptr_t;
22
+ #define UINT_MAX 4294967295U
23
+ #define LONG_MAX 2147483647L
24
+ #define LONG_MIN (-LONG_MAX - 1L)
25
+ #define LLONG_MIN (-LLONG_MAX - 1LL)
26
+ #define ULLONG_MAX 18446744073709551615ULL
27
+ #define CHAR_BIT 8
28
+ #define SHRT_MAX 32767
29
+ #define RAND_MAX 32767
30
+ typedef struct { int fd; } FILE;
31
+ extern FILE *stdin, *stdout, *stderr;
32
+ int fprintf(FILE *f, const char *fmt, ...);
33
+ int fputc(int c, FILE *f);
34
+ int fputs(const char *s, FILE *f);
35
+ int fgetc(FILE *f);
36
+ char *fgets(char *s, int n, FILE *f);
37
+ char *gets(char *s);
38
+ int sprintf(char *s, const char *fmt, ...);
39
+ int sscanf(const char *s, const char *fmt, ...);
40
+ int fscanf(FILE *f, const char *fmt, ...);
41
+ int putc(int c, FILE *f);
42
+ int getc(FILE *f);
43
+ int fflush(FILE *f);
44
+ void exit(int code);
45
+ int atoi(const char *s);
46
+ long atol(const char *s);
47
+ long long atoll(const char *s);
48
+ long labs(long x);
49
+ long long llabs(long long x);
50
+ int rand(void);
51
+ void srand(unsigned s);
52
+ void qsort(void *base, size_t n, size_t sz, int (*cmp)(const void *, const void *));
53
+ int strncmp(const char *a, const char *b, size_t n);
54
+ char *strcat(char *d, const char *s);
55
+ char *strncpy(char *d, const char *s, size_t n);
56
+ char *strchr(const char *s, int c);
57
+ char *strrev(char *s);
58
+ void *memmove(void *d, const void *s, size_t n);
59
+ int memcmp(const void *a, const void *b, size_t n);
60
+ int isdigit(int c); int isalpha(int c); int isspace(int c); int isupper(int c); int islower(int c); int isalnum(int c);
61
+ int toupper(int c); int tolower(int c);
62
+ #define max(a,b) ((a) > (b) ? (a) : (b))
63
+ #define min(a,b) ((a) < (b) ? (a) : (b))
64
+ int putchar(int c);
65
+ int getchar(void);
66
+ int puts(const char *s);
67
+ int printf(const char *fmt, ...);
68
+ int scanf(const char *fmt, ...);
69
+ size_t strlen(const char *s);
70
+ int strcmp(const char *a, const char *b);
71
+ char *strcpy(char *d, const char *s);
72
+ void *memset(void *p, int c, size_t n);
73
+ void *memcpy(void *d, const void *s, size_t n);
74
+ void *malloc(size_t n);
75
+ void *calloc(size_t n, size_t m);
76
+ void free(void *p);
77
+ int abs(int x);
78
+ #endif
neurv/rt/link.ld ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ /* 8 KiB machine: program + data from 0, stack grows down from 0x1C00, I/O block at 0x1C00..0x1FFF */
2
+ MEMORY { RAM (rwx) : ORIGIN = 0x0, LENGTH = 0x1A00 }
3
+ SECTIONS {
4
+ .text : { *(.text.start) *(.text .text.*) } > RAM
5
+ .rodata : { *(.rodata .rodata.* .srodata .srodata.*) } > RAM
6
+ .data : { *(.data .data.* .sdata .sdata.*) } > RAM
7
+ .bss : { __bss_start = .; *(.bss .bss.* .sbss .sbss.* COMMON) __bss_end = .; } > RAM
8
+ __heap_start = .;
9
+ /DISCARD/ : { *(.comment) *(.riscv.attributes) *(.eh_frame*) }
10
+ }
neurv/rt/t_check.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import sys, time
2
+ sys.path.insert(0, "/dev/shm/neurv")
3
+ import rv
4
+ img = rv.compile_c(open("/dev/shm/neurv/rt/t_fib.c").read())
5
+ print("image bytes", len(img))
6
+ t = time.time(); steps, out, h = rv.run(img, b"15\n"); dt = time.time() - t
7
+ print("steps", steps, "halted", h, "sec", round(dt, 2), "out", out)
8
+ m = rv.Machine(img, b"15\n"); s0 = m.state(); m.step(); s1 = m.state()
9
+ print("state len", len(s0), "changed words", int((s0 != s1).sum()), "pc", s0[32], "->", s1[32])
neurv/rt/t_fib.c ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #include "io.h"
2
+ int fib(int n){ return n < 2 ? n : fib(n - 1) + fib(n - 2); }
3
+ int main(void){ int n; scanf("%d", &n); for(int i = 0; i <= n; i++) printf("%d ", fib(i)); printf("\n%lld\n", (long long)fib(n) * 1000003LL); return 0; }
neurv/rv.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The real chip: RV32IM with 8 KiB of memory, run by Unicorn. Compile C with zig cc, load, step, read the full state.
2
+
3
+ State = 32 registers (x0..x31) + PC + 8 KiB of memory. Output and input live in memory (see rt/io.h), so this IS the whole machine.
4
+ """
5
+ import os
6
+ import subprocess
7
+ import tempfile
8
+
9
+ import numpy as np
10
+ from unicorn import Uc, UC_ARCH_RISCV, UC_MODE_RISCV32, UcError
11
+ from unicorn.riscv_const import UC_RISCV_REG_PC, UC_RISCV_REG_X0
12
+
13
+ HERE = os.path.dirname(os.path.abspath(__file__))
14
+ RT = os.path.join(HERE, "rt")
15
+ ZIG = os.environ.get("ZIG", "/dev/shm/neurv/pkgs/ziglang/zig")
16
+ MEM = 0x2000
17
+ OUT_BASE, OUT_LEN, OUT_PTR, IN_PTR, IN_BASE, IN_LEN = 0x1C00, 0x300, 0x1F00, 0x1F04, 0x1F08, 0xF8
18
+ HALT_WORD = 0x0000006F # "j ." : the program has finished
19
+ CFLAGS = ["-target", "riscv32-freestanding-none", "-mcpu=generic_rv32+m", "-Os", "-ffreestanding", "-nostartfiles",
20
+ "-fno-stack-protector", "-fno-builtin", "-std=gnu99", "-Wno-error=implicit-int", "-Wno-error=implicit-function-declaration", "-Wno-error=int-conversion", "-Wno-error=incompatible-pointer-types", "-Wno-error=return-mismatch", "-Wno-error=return-type", "-I", RT, "-T", os.path.join(RT, "link.ld"), "-Wl,--gc-sections",
21
+ "-ffunction-sections", "-fdata-sections", "-w"]
22
+
23
+
24
+ def compile_c(src_text, tag="p"):
25
+ """C source text -> flat memory image (bytes) or raises CalledProcessError / ValueError."""
26
+ with tempfile.TemporaryDirectory(dir=os.environ.get("RV_TMP")) as d:
27
+ c = os.path.join(d, tag + ".c")
28
+ open(c, "w").write(src_text)
29
+ elf, binf = os.path.join(d, "a.elf"), os.path.join(d, "a.bin")
30
+ subprocess.run([ZIG, "cc", *CFLAGS, os.path.join(RT, "crt0.S"), os.path.join(RT, "io.c"), c, "-o", elf],
31
+ check=True, capture_output=True, timeout=60, env={**os.environ, "ZIG_GLOBAL_CACHE_DIR": os.environ.get("ZIG_GLOBAL_CACHE_DIR", "/dev/shm/neurv/zigcache"), "ZIG_LOCAL_CACHE_DIR": os.path.join(d, "zc")})
32
+ subprocess.run([ZIG, "objcopy", "-O", "binary", elf, binf], check=True, capture_output=True, timeout=30)
33
+ img = open(binf, "rb").read()
34
+ if len(img) > 0x1A00:
35
+ raise ValueError(f"image {len(img)} bytes > 0x1A00")
36
+ return img
37
+
38
+
39
+ class Machine:
40
+ def __init__(self, image, inp=b""):
41
+ self.uc = Uc(UC_ARCH_RISCV, UC_MODE_RISCV32)
42
+ self.uc.mem_map(0, MEM)
43
+ self.uc.mem_write(0, bytes(image))
44
+ inp = bytes(inp)[:IN_LEN - 1]
45
+ self.uc.mem_write(IN_BASE, inp)
46
+ self.uc.reg_write(UC_RISCV_REG_PC, 0)
47
+
48
+ def state(self):
49
+ """[33 + 2048] uint32: x0..x31, pc, then memory as 2048 little-endian words."""
50
+ regs = [self.uc.reg_read(UC_RISCV_REG_X0 + i) for i in range(32)] + [self.uc.reg_read(UC_RISCV_REG_PC)]
51
+ mem = np.frombuffer(bytes(self.uc.mem_read(0, MEM)), dtype="<u4")
52
+ return np.concatenate([np.array(regs, dtype=np.uint32), mem.astype(np.uint32)])
53
+
54
+ def halted(self):
55
+ pc = self.uc.reg_read(UC_RISCV_REG_PC)
56
+ return int.from_bytes(bytes(self.uc.mem_read(pc, 4)), "little") == HALT_WORD
57
+
58
+ def step(self, n=1):
59
+ pc = self.uc.reg_read(UC_RISCV_REG_PC)
60
+ self.uc.emu_start(pc, MEM, count=n)
61
+
62
+ def output(self):
63
+ n = int.from_bytes(bytes(self.uc.mem_read(OUT_PTR, 4)), "little")
64
+ return bytes(self.uc.mem_read(OUT_BASE, min(n, OUT_LEN)))
65
+
66
+
67
+ def run(image, inp=b"", max_steps=1_000_000, chunk=1000):
68
+ """Run to the halt loop. Returns (steps, output bytes, halted)."""
69
+ m = Machine(image, inp)
70
+ steps = 0
71
+ while steps < max_steps:
72
+ if m.halted():
73
+ return steps, m.output(), True
74
+ try:
75
+ m.step(1)
76
+ except UcError:
77
+ return steps, m.output(), False
78
+ steps += 1
79
+ return steps, m.output(), False
neurv/whyfail.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys, os, re, random, subprocess, collections
2
+ sys.path.insert(0, "/dev/shm/neurv"); os.environ["RV_TMP"] = "/dev/shm/neurv/tmp"
3
+ import rv, pyarrow.parquet as pq
4
+ from multiprocessing import Pool
5
+ INC = re.compile(r"^\s*#\s*include\s*[<\"][^>\"]*[>\"]\s*$", re.M)
6
+ t = pq.read_table("/dev/shm/neurv/src/codenet/c0.parquet", columns=["s_id", "code"])
7
+ codes = [c for c in t["code"].to_pylist()[:6000] if c and len(c) < 6000]
8
+ random.Random(1).shuffle(codes)
9
+ def f(code):
10
+ try: rv.compile_c('#include "io.h"\n' + INC.sub("", code)); return None
11
+ except subprocess.CalledProcessError as e:
12
+ err = (e.stderr or b"").decode(errors="replace")
13
+ m = re.findall(r"error: (.*)", err)
14
+ return m[0][:90] if m else err.strip()[-90:]
15
+ except ValueError: return "too big image"
16
+ with Pool(48) as p: r = p.map(f, codes[:800])
17
+ c = collections.Counter(re.sub(r"'[^']*'", "'X'", x) for x in r if x)
18
+ for k, v in c.most_common(25): print(v, k)
19
+ c2 = collections.Counter(re.findall(r"'([^']*)'", x)[0] for x in r if x and "undeclared function" in x or (x and "implicit" in x and re.findall(r"'([^']*)'", x)))
20
+ print(c2.most_common(40))