aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / model / check_local_model.c
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Qualification only. Compare native Omega to the pinned independent C decoder. |
| 3 | // |
| 4 | // Usage: check_local_model EVIDENCE_DIR |
| 5 | // The directory contains omega-model-probe, reference-probe, llama.raw and the |
| 6 | // two pinned checkpoints. No Python or C inference runtime is linked into the |
| 7 | // iMac image; this host program only drives the two probes and compares them. |
| 8 | #ifndef _DEFAULT_SOURCE |
| 9 | #define _DEFAULT_SOURCE |
| 10 | #endif |
| 11 | #include <ctype.h> |
| 12 | #include <errno.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <math.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/wait.h> |
| 21 | #include <sys/utsname.h> |
| 22 | #include <unistd.h> |
| 23 | #include "sha256.h" |
| 24 | |
| 25 | #define MAX_ARGS 48 |
| 26 | #define MAX_OUT 8192 |
| 27 | #define MAX_CASES 16 |
| 28 | |
| 29 | static const char *root; |
| 30 | static bool need_qemu; |
| 31 | |
| 32 | static void fail(const char *what, const char *detail) { |
| 33 | fprintf(stderr, "LOCAL_MODEL_HOST: FAIL %s%s%s\n", what, detail ? ": " : "", detail ? detail : ""); |
| 34 | exit(1); |
| 35 | } |
| 36 | |
| 37 | static void path_in(char *out, size_t cap, const char *name) { |
| 38 | if ((size_t)snprintf(out, cap, "%s/%s", root, name) >= cap) fail("path too long", name); |
| 39 | } |
| 40 | |
| 41 | static uint8_t *slurp(const char *path, size_t *len) { |
| 42 | FILE *f = fopen(path, "rb"); |
| 43 | if (!f) fail("cannot open", path); |
| 44 | size_t cap = 1 << 20, n = 0; |
| 45 | uint8_t *buf = malloc(cap); |
| 46 | for (size_t got; buf && (got = fread(buf + n, 1, cap - n, f)) > 0;) { |
| 47 | n += got; |
| 48 | if (n == cap) buf = realloc(buf, cap *= 2); |
| 49 | } |
| 50 | if (!buf || ferror(f)) fail("cannot read", path); |
| 51 | fclose(f); |
| 52 | *len = n; |
| 53 | return buf; |
| 54 | } |
| 55 | |
| 56 | static void check_pin(const char *name, const char *pin) { |
| 57 | char path[4096], hex[65]; |
| 58 | uint8_t digest[SHA256_DIGEST_SIZE]; |
| 59 | size_t len; |
| 60 | path_in(path, sizeof path, name); |
| 61 | uint8_t *data = slurp(path, &len); |
| 62 | sha256_hash(data, len, digest); |
| 63 | free(data); |
| 64 | for (int i = 0; i < SHA256_DIGEST_SIZE; i++) sprintf(hex + 2 * i, "%02x", digest[i]); |
| 65 | if (strcmp(hex, pin)) fail("checkpoint hash does not match its pin", name); |
| 66 | } |
| 67 | |
| 68 | /* Run ARGV (without the qemu prefix), capture stdout, return the exit status. */ |
| 69 | static int run(char **argv, bool with_prefix, bool quiet_stderr, char *out, size_t cap) { |
| 70 | char *full[MAX_ARGS + 2]; |
| 71 | int n = 0; |
| 72 | if (with_prefix && need_qemu) full[n++] = (char *)"qemu-x86_64"; |
| 73 | for (int i = 0; argv[i]; i++) { |
| 74 | if (n >= MAX_ARGS) fail("too many arguments", NULL); |
| 75 | full[n++] = argv[i]; |
| 76 | } |
| 77 | full[n] = NULL; |
| 78 | int fds[2]; |
| 79 | if (pipe(fds)) fail("pipe", strerror(errno)); |
| 80 | pid_t pid = fork(); |
| 81 | if (pid < 0) fail("fork", strerror(errno)); |
| 82 | if (pid == 0) { |
| 83 | dup2(fds[1], 1); |
| 84 | close(fds[0]); close(fds[1]); |
| 85 | if (quiet_stderr) dup2(open("/dev/null", O_WRONLY), 2); |
| 86 | execvp(full[0], full); |
| 87 | _exit(127); |
| 88 | } |
| 89 | close(fds[1]); |
| 90 | if (cap == 0) fail("empty capture buffer", NULL); |
| 91 | size_t len = 0; |
| 92 | bool overflow = false; |
| 93 | char drain[256]; |
| 94 | for (;;) { |
| 95 | bool full = len + 1 == cap; |
| 96 | ssize_t got = read(fds[0], full ? drain : out + len, |
| 97 | full ? sizeof drain : cap - 1 - len); |
| 98 | if (got < 0 && errno == EINTR) continue; |
| 99 | if (got < 0) fail("read probe output", strerror(errno)); |
| 100 | if (got == 0) break; |
| 101 | if (full) overflow = true; |
| 102 | else len += (size_t)got; |
| 103 | } |
| 104 | close(fds[0]); |
| 105 | out[len] = 0; |
| 106 | int status; |
| 107 | while (waitpid(pid, &status, 0) < 0) |
| 108 | if (errno != EINTR) fail("waitpid", strerror(errno)); |
| 109 | if (overflow) fail("probe output exceeds capture limit", full[0]); |
| 110 | return WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status); |
| 111 | } |
| 112 | |
| 113 | /* Text after the first "ids=", trimmed, as the greedy-token fingerprint. */ |
| 114 | static const char *ids_of(char *text, const char *case_name) { |
| 115 | char *p = strstr(text, "ids="); |
| 116 | if (!p) fail("no ids= in probe output", case_name); |
| 117 | p += 4; |
| 118 | size_t n = strlen(p); |
| 119 | while (n && isspace((unsigned char)p[n - 1])) p[--n] = 0; |
| 120 | return p; |
| 121 | } |
| 122 | |
| 123 | static void json_string(FILE *f, const char *s) { |
| 124 | fputc('"', f); |
| 125 | for (; *s; s++) { |
| 126 | if (*s == '"' || *s == '\\') fprintf(f, "\\%c", *s); |
| 127 | else if (*s == '\n') fputs("\\n", f); |
| 128 | else if ((unsigned char)*s < 0x20) fprintf(f, "\\u%04x", *s); |
| 129 | else fputc(*s, f); |
| 130 | } |
| 131 | fputc('"', f); |
| 132 | } |
| 133 | |
| 134 | typedef struct { |
| 135 | char name[64]; |
| 136 | bool invalid; |
| 137 | double worst, max_abs; |
| 138 | char native[MAX_OUT]; |
| 139 | int invalid_count, prompt_length; |
| 140 | } result; |
| 141 | |
| 142 | static result results[MAX_CASES]; |
| 143 | static int nresults; |
| 144 | |
| 145 | static bool logit_lengths_ok(size_t a, size_t b, size_t vocab) { |
| 146 | return vocab != 0 && vocab <= SIZE_MAX / 4 && a == vocab * 4 && b == a; |
| 147 | } |
| 148 | |
| 149 | /* These negative controls require no weights or model runtime. An empty/short |
| 150 | * pair must not qualify, nor may capture silently hide a probe's trailing output. */ |
| 151 | static int self_test(void) { |
| 152 | static const struct { size_t a, b, vocab; bool ok; } sizes[] = { |
| 153 | { 0, 0, 512, false }, { 4, 4, 512, false }, |
| 154 | { 2044, 2044, 512, false }, { 2049, 2049, 512, false }, |
| 155 | { 2048, 2044, 512, false }, { 2048, 2048, 512, true }, |
| 156 | { 128000, 128000, 32000, true }, { 0, 0, SIZE_MAX, false }, |
| 157 | }; |
| 158 | unsigned checks = 0; |
| 159 | for (size_t i = 0; i < sizeof sizes / sizeof *sizes; i++) { |
| 160 | if (logit_lengths_ok(sizes[i].a, sizes[i].b, sizes[i].vocab) != sizes[i].ok) |
| 161 | fail("self-test: incomplete logits accepted or valid logits refused", NULL); |
| 162 | checks++; |
| 163 | } |
| 164 | static const struct { const char *cmd; int status; } captures[] = { |
| 165 | { "head -c 0 /dev/zero", 0 }, { "head -c 31 /dev/zero", 0 }, |
| 166 | { "head -c 32 /dev/zero", 1 }, { "head -c 4096 /dev/zero", 1 }, |
| 167 | }; |
| 168 | for (size_t i = 0; i < sizeof captures / sizeof *captures; i++) { |
| 169 | pid_t pid = fork(); |
| 170 | if (pid < 0) fail("self-test fork", strerror(errno)); |
| 171 | if (pid == 0) { |
| 172 | int fd = open("/dev/null", O_WRONLY); |
| 173 | if (fd < 0 || dup2(fd, 2) < 0) _exit(2); |
| 174 | close(fd); |
| 175 | char out[32]; |
| 176 | char *args[] = { (char *)"sh", (char *)"-c", (char *)captures[i].cmd, NULL }; |
| 177 | _exit(run(args, false, false, out, sizeof out)); |
| 178 | } |
| 179 | int status; |
| 180 | while (waitpid(pid, &status, 0) < 0) |
| 181 | if (errno != EINTR) fail("self-test waitpid", strerror(errno)); |
| 182 | if (!WIFEXITED(status) || WEXITSTATUS(status) != captures[i].status) |
| 183 | fail("self-test: capture boundary was not enforced", captures[i].cmd); |
| 184 | checks++; |
| 185 | } |
| 186 | printf("LOCAL_MODEL_CHECKER_SELF_TEST: PASS checks=%u\n", checks); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int main(int argc, char **argv) { |
| 191 | if (argc == 2 && !strcmp(argv[1], "--self-test")) return self_test(); |
| 192 | if (argc != 2) { fputs("usage: check_local_model EVIDENCE_DIR | --self-test\n", stderr); return 2; } |
| 193 | root = realpath(argv[1], NULL); |
| 194 | if (!root) fail("no such directory", argv[1]); |
| 195 | struct utsname un; |
| 196 | uname(&un); |
| 197 | need_qemu = strcmp(un.machine, "x86_64") != 0; |
| 198 | |
| 199 | check_pin("stories15M.bin", "cd590644d963867a2b6e5a1107f51fad663c41d79c149fbecbbb1f95fa81f49a"); |
| 200 | check_pin("stories260K.bin", "b0a507e7ad0f626624f17112325e66691f9076d622e1d3274d103d00299f2696"); |
| 201 | |
| 202 | static const struct { const char *model; int count; int plen; int prompt[8]; } cases[] = { |
| 203 | { "stories260K", 1, 1, { 1 } }, { "stories260K", 8, 1, { 1 } }, |
| 204 | { "stories15M", 1, 1, { 1 } }, { "stories15M", 8, 1, { 1 } }, |
| 205 | { "stories15M", 32, 1, { 1 } }, { "stories15M", 8, 5, { 1, 9038, 2501, 263, 931 } }, |
| 206 | }; |
| 207 | char omega_probe[4096], ref_probe[4096], raw[4096]; |
| 208 | path_in(omega_probe, sizeof omega_probe, "omega-model-probe"); |
| 209 | path_in(ref_probe, sizeof ref_probe, "reference-probe"); |
| 210 | path_in(raw, sizeof raw, "llama.raw"); |
| 211 | |
| 212 | for (size_t c = 0; c < sizeof cases / sizeof *cases; c++) { |
| 213 | result *r = &results[nresults++]; |
| 214 | snprintf(r->name, sizeof r->name, "%s-%d-p%d", cases[c].model, cases[c].count, cases[c].plen); |
| 215 | char ckpt[4096], omega_out[4096], ref_out[4096], count[16], file[96]; |
| 216 | snprintf(file, sizeof file, "%s.bin", cases[c].model); |
| 217 | path_in(ckpt, sizeof ckpt, file); |
| 218 | snprintf(file, sizeof file, "%s-omega.f32", r->name); |
| 219 | path_in(omega_out, sizeof omega_out, file); |
| 220 | snprintf(file, sizeof file, "%s-reference.f32", r->name); |
| 221 | path_in(ref_out, sizeof ref_out, file); |
| 222 | snprintf(count, sizeof count, "%d", cases[c].count); |
| 223 | char prompt_s[8][16]; |
| 224 | char *na[MAX_ARGS], *ra[MAX_ARGS]; |
| 225 | int nn = 0, rn = 0; |
| 226 | na[nn++] = omega_probe; na[nn++] = raw; na[nn++] = ckpt; na[nn++] = count; na[nn++] = omega_out; |
| 227 | ra[rn++] = ref_probe; ra[rn++] = ckpt; ra[rn++] = count; ra[rn++] = ref_out; |
| 228 | for (int i = 0; i < cases[c].plen; i++) { |
| 229 | snprintf(prompt_s[i], sizeof prompt_s[i], "%d", cases[c].prompt[i]); |
| 230 | na[nn++] = prompt_s[i]; ra[rn++] = prompt_s[i]; |
| 231 | } |
| 232 | na[nn] = ra[rn] = NULL; |
| 233 | static char ref_text[MAX_OUT]; |
| 234 | if (run(na, true, false, r->native, sizeof r->native)) fail("omega probe failed", r->name); |
| 235 | if (run(ra, false, false, ref_text, sizeof ref_text)) fail("reference probe failed", r->name); |
| 236 | char native_copy[MAX_OUT], ref_copy[MAX_OUT]; |
| 237 | snprintf(native_copy, sizeof native_copy, "%s", r->native); |
| 238 | snprintf(ref_copy, sizeof ref_copy, "%s", ref_text); |
| 239 | if (strcmp(ids_of(native_copy, r->name), ids_of(ref_copy, r->name))) fail("greedy token ids differ", r->name); |
| 240 | |
| 241 | size_t alen, blen; |
| 242 | uint8_t *ab = slurp(omega_out, &alen), *bb = slurp(ref_out, &blen); |
| 243 | size_t vocab = !strcmp(cases[c].model, "stories260K") ? 512 : 32000; |
| 244 | if (!logit_lengths_ok(alen, blen, vocab)) fail("logit files must contain the full vocabulary", r->name); |
| 245 | const float *a = (const float *)(void *)ab, *b = (const float *)(void *)bb; |
| 246 | for (size_t i = 0; i < alen / 4; i++) |
| 247 | if (!isfinite(a[i]) || !isfinite(b[i])) fail("non-finite logit", r->name); |
| 248 | r->worst = r->max_abs = 0; |
| 249 | for (size_t i = 0; i < alen / 4; i++) { |
| 250 | double x = a[i], y = b[i]; |
| 251 | double scaled = fabs(x - y) / (1e-3 + 1e-4 * fabs(y)); |
| 252 | if (scaled > r->worst) r->worst = scaled; |
| 253 | if (fabs(x - y) > r->max_abs) r->max_abs = fabs(x - y); |
| 254 | } |
| 255 | free(ab); free(bb); |
| 256 | if (r->worst > 1) { fprintf(stderr, "scaled error %.17g\n", r->worst); fail("logits outside tolerance", r->name); } |
| 257 | snprintf(file, sizeof file, "%s.txt", r->name); |
| 258 | char txt[4096]; |
| 259 | path_in(txt, sizeof txt, file); |
| 260 | int f_fd = open(txt, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 261 | FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "w"); |
| 262 | if (!f || fputs(r->native, f) == EOF || fputs(ref_text, f) == EOF || fclose(f)) fail("cannot write", txt); |
| 263 | } |
| 264 | |
| 265 | /* Invalid inputs must trap instead of returning any generated tokens. */ |
| 266 | static const struct { int count; int plen; int token; } bad[] = { |
| 267 | { 0, 1, 1 }, { 33, 1, 1 }, { 1, 1, 32000 }, { 1, 33, 1 }, |
| 268 | }; |
| 269 | char ckpt[4096], refused[4096]; |
| 270 | path_in(ckpt, sizeof ckpt, "stories15M.bin"); |
| 271 | path_in(refused, sizeof refused, "refused.f32"); |
| 272 | for (size_t c = 0; c < sizeof bad / sizeof *bad; c++) { |
| 273 | char count[16], tok[16]; |
| 274 | snprintf(count, sizeof count, "%d", bad[c].count); |
| 275 | snprintf(tok, sizeof tok, "%d", bad[c].token); |
| 276 | char *args[MAX_ARGS]; |
| 277 | int n = 0; |
| 278 | args[n++] = omega_probe; args[n++] = raw; args[n++] = ckpt; args[n++] = count; args[n++] = refused; |
| 279 | for (int i = 0; i < bad[c].plen; i++) args[n++] = tok; |
| 280 | args[n] = NULL; |
| 281 | static char out[MAX_OUT]; |
| 282 | int code = run(args, true, true, out, sizeof out); |
| 283 | if (code != 1 || !strstr(out, "trap=3") || !strstr(out, "REFUSED")) { |
| 284 | char why[64]; |
| 285 | snprintf(why, sizeof why, "count=%d prompt_length=%d exit=%d", bad[c].count, bad[c].plen, code); |
| 286 | fail("invalid input was not refused", why); |
| 287 | } |
| 288 | result *r = &results[nresults++]; |
| 289 | r->invalid = true; |
| 290 | r->invalid_count = bad[c].count; |
| 291 | r->prompt_length = bad[c].plen; |
| 292 | } |
| 293 | |
| 294 | char json_path[4096]; |
| 295 | path_in(json_path, sizeof json_path, "host-parity.json"); |
| 296 | int f_fd = open(json_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); |
| 297 | FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "w"); |
| 298 | if (!f) fail("cannot write", json_path); |
| 299 | fputs("[\n", f); |
| 300 | for (int i = 0; i < nresults; i++) { |
| 301 | const result *r = &results[i]; |
| 302 | fputs(" {\n", f); |
| 303 | if (r->invalid) { |
| 304 | fprintf(f, " \"invalid_count\": %d,\n \"prompt_length\": %d,\n \"refused\": true\n", |
| 305 | r->invalid_count, r->prompt_length); |
| 306 | } else { |
| 307 | char *nat = (char *)r->native; |
| 308 | size_t nl = strlen(nat); |
| 309 | while (nl && isspace((unsigned char)nat[nl - 1])) nat[--nl] = 0; |
| 310 | fprintf(f, " \"case\": \"%s\",\n \"tokens_match\": true,\n", r->name); |
| 311 | fprintf(f, " \"worst_scaled_error\": %.17g,\n \"max_abs_error\": %.17g,\n \"native\": ", |
| 312 | r->worst, r->max_abs); |
| 313 | json_string(f, nat); |
| 314 | fputc('\n', f); |
| 315 | } |
| 316 | fprintf(f, " }%s\n", i + 1 < nresults ? "," : ""); |
| 317 | } |
| 318 | fputs("]\n", f); |
| 319 | if (fclose(f)) fail("cannot write", json_path); |
| 320 | printf("LOCAL_MODEL_HOST: PASS cases=%d independent_reference=yes\n", nresults); |
| 321 | return 0; |
| 322 | } |