Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/runtime/rx_empirical_optimizer.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / runtime / rx_empirical_optimizer.c` 1831 lines ยท 79663 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * OMEGA_EMPIRICAL_OPTIMIZER -- realization choice from measured experience. 19 * 20 * Arms are the ways Omega can realize its integer matvec on this machine: 21 * the compiled semantic reference, the three M12 emitters, quad4 (R10), and 22 * one planted wrong realization. Each is verified in a forked child before 23 * the parent may run it. The planted one must be refused and never run. 24 * 25 * The learner (rx_costmodel) is trained here by its own runs only, under a 26 * bounded exploration budget. A candidate model is proposed as the `model` 27 * blob of an R9 generation; an independent judge re-checks it on validation 28 * work and a separate subject holding the promotion right promotes it. The 29 * learner cannot promote. After a restart the durable model is read back and 30 * faces held-out workloads it never saw: 31 * 32 * H1 unseen shapes 33 * H2 a thin-matrix-heavy mix (where the best single realization loses) 34 * H3 H1's shapes under memory pressure, a condition absent from training 35 * 36 * Every policy is charged from the same per-job cost table: each eligible 37 * arm timed in rotating order, best of 5 rounds. The learner never sees the 38 * table; it learns only from the runs it chose to make. The online learner's 39 * cost includes every extra run it made to measure. Every run of every arm 40 * is checked against the reference digest for that job. 41 * 42 * A second, independent comparison replays each policy's choices end to end 43 * in alternating blocks and reads wall time and the cluster energy meter. 44 */ 45 #include "runtime/aienos_cap.h" 46 #include "runtime/rx_caproot.h" 47 #include "runtime/rx_costmodel.h" 48 #include "runtime/rx_generation.h" 49 #include "omega_evidence.h" 50 #include "omega_machine.h" 51 #include "omega_matvec.h" 52 #include "omega_matvec_quad.h" 53 #include "sha256.h" 54 55 #include 56 #include 57 #include 58 #include 59 #include 60 #include 61 #include 62 #include 63 #include 64 #include 65 #include 66 #include 67 #include 68 #include 69 #include 70 #include 71 #include 72 73 static int g_checks; 74 static int g_fail; 75 76 #define CHECK(cond, ...) do { \ 77 g_checks++; \ 78 if (!(cond)) { \ 79 g_fail++; \ 80 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ 81 fprintf(stderr, __VA_ARGS__); \ 82 fputc('\n', stderr); \ 83 } \ 84 } while (0) 85 86 static uint64_t now_ns(void) { 87 struct timespec ts; 88 clock_gettime(CLOCK_MONOTONIC, &ts); 89 return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; 90 } 91 92 /* ---- arms ---- */ 93 94 enum { ARM_REF = 0, ARM_SCALAR, ARM_U2, ARM_U4D, ARM_QUAD4, ARM_PLANTED, N_ARMS }; 95 static const char *const ARM_NAME[N_ARMS] = { "reference", "scalar", "unroll2", 96 "unroll4_dual", "quad4", "planted_wrong" }; 97 typedef void (*MvFn)(const uint64_t *, const uint64_t *, uint64_t *, uint64_t, uint64_t); 98 99 static MvFn g_fn[N_ARMS]; 100 static int g_verified[N_ARMS]; 101 static uint64_t g_parent_runs[N_ARMS]; 102 static uint64_t g_refused_runs; 103 static uint32_t g_eligible; 104 105 static void arm_reference(const uint64_t *A, const uint64_t *x, uint64_t *y, uint64_t M, 106 uint64_t N) { 107 omega_matvec_reference(A, x, y, (uint32_t)M, (uint32_t)N); 108 } 109 110 /* Planted: drops the last N % 4 columns. Right whenever N % 4 == 0. */ 111 static void arm_planted(const uint64_t *A, const uint64_t *x, uint64_t *y, uint64_t M, 112 uint64_t N) { 113 uint64_t n4 = N - (N % 4u); 114 for (uint64_t i = 0; i < M; i++) { 115 uint64_t s = 0; 116 for (uint64_t j = 0; j < n4; j++) s += A[i * N + j] * x[j]; 117 y[i] = s; 118 } 119 } 120 121 /* The only way the parent runs an arm. */ 122 static int exec_arm(uint32_t arm, const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, 123 uint32_t N) { 124 if (arm >= N_ARMS || !g_verified[arm]) { 125 g_refused_runs++; 126 return -1; 127 } 128 g_parent_runs[arm]++; 129 g_fn[arm](A, x, y, M, N); 130 return 0; 131 } 132 133 /* ---- buffers and jobs ---- */ 134 135 #define MAX_ELEMS (1u << 19) 136 #define MAX_N 8192u 137 #define MAX_M 8192u 138 #define GUARD 8u 139 140 static uint64_t *g_A, *g_x, *g_y; 141 142 typedef struct { 143 uint32_t M, N, calls; 144 uint64_t seed; 145 uint64_t ref_digest; 146 } Job; 147 148 static uint64_t xs(uint64_t *s) { 149 *s ^= *s << 13; 150 *s ^= *s >> 7; 151 *s ^= *s << 17; 152 return *s; 153 } 154 155 static uint64_t digest_y(const uint64_t *y, uint32_t M) { 156 uint8_t d[32]; 157 sha256_hash((const uint8_t *)y, (size_t)M * 8u, d); 158 uint64_t v; 159 memcpy(&v, d, 8); 160 return v; 161 } 162 163 static void job_load(Job *j) { 164 uint64_t s = j->seed | 1u; 165 for (uint64_t i = 0; i < (uint64_t)j->M * j->N; i++) g_A[i] = xs(&s); 166 for (uint32_t i = 0; i < j->N; i++) g_x[i] = xs(&s); 167 omega_matvec_reference(g_A, g_x, g_y, j->M, j->N); 168 j->ref_digest = digest_y(g_y, j->M); 169 } 170 171 static uint32_t calls_for(uint32_t M, uint32_t N) { 172 uint64_t c = 600000ull / ((uint64_t)M * N + 64u); 173 if (c < 3) c = 3; 174 if (c > 3000) c = 3000; 175 return (uint32_t)c; 176 } 177 178 static struct { 179 uint64_t runs_checked, mismatches; 180 } K; 181 182 /* Time `calls` calls of one arm on the loaded job; ps per call. Checks the 183 * output against the reference digest. 0 if the arm may not run. */ 184 static uint64_t time_arm(uint32_t arm, const Job *j) { 185 memset(g_y, 0, (size_t)j->M * 8u); 186 uint64_t t0 = now_ns(); 187 for (uint32_t c = 0; c < j->calls; c++) 188 if (exec_arm(arm, g_A, g_x, g_y, j->M, j->N) != 0) return 0; 189 uint64_t dt = now_ns() - t0; 190 K.runs_checked++; 191 if (digest_y(g_y, j->M) != j->ref_digest) K.mismatches++; 192 uint64_t ps = dt * 1000u / j->calls; 193 return ps ? ps : 1; 194 } 195 196 /* The learner's own measurement of `calls` calls: the job's calls in three 197 * chunks, fastest chunk kept (another tenant on the core inflates a chunk, 198 * not the floor). Checks the output like time_arm. */ 199 static uint64_t time_arm_chunked(uint32_t arm, const Job *j, uint32_t calls) { 200 if (calls < 3) calls = 3; 201 uint32_t per = calls / 3u, best = 0; 202 uint64_t best_ps = UINT64_MAX; 203 memset(g_y, 0, (size_t)j->M * 8u); 204 for (uint32_t k = 0; k < 3; k++) { 205 uint64_t t0 = now_ns(); 206 for (uint32_t c = 0; c < per; c++) 207 if (exec_arm(arm, g_A, g_x, g_y, j->M, j->N) != 0) return 0; 208 uint64_t ps = (now_ns() - t0) * 1000u / per; 209 if (ps < best_ps) best_ps = ps; 210 best++; 211 } 212 (void)best; 213 K.runs_checked++; 214 if (digest_y(g_y, j->M) != j->ref_digest) K.mismatches++; 215 return best_ps ? best_ps : 1; 216 } 217 218 /* The harness's cost table: every eligible arm, rotating order, best of 5. */ 219 static void truth_table(const Job *j, uint64_t table[N_ARMS]) { 220 uint32_t arms[N_ARMS], n = 0; 221 for (uint32_t a = 0; a < N_ARMS; a++) { 222 table[a] = UINT64_MAX; 223 if (g_eligible & (1u << a)) arms[n++] = a; 224 } 225 for (uint32_t r = 0; r < 5; r++) 226 for (uint32_t i = 0; i < n; i++) { 227 uint32_t a = arms[(i + r) % n]; 228 uint64_t t = time_arm(a, j); 229 if (t && t < table[a]) table[a] = t; 230 } 231 } 232 233 /* ---- placement, thermal, energy, pressure ---- */ 234 235 typedef struct { 236 int cpus[64]; 237 int n; 238 int timed; 239 const char *name; 240 const char *meter; /* spbm label of the cluster, NULL if none */ 241 uint64_t cache_bytes; /* private L2 per core (omega_machine: X925 2 MiB, A725 512 KiB) */ 242 } CoreClass; 243 244 static CoreClass g_class[RX_CM_CORES]; 245 static char g_cpus_desc[256]; 246 static cpu_set_t g_allowed; /* the affinity mask the test started with: the only cpus it may use */ 247 248 static void discover(void) { 249 g_class[RX_CM_CORE_X925] = (CoreClass){ .name = "Cortex-X925", .meter = "cpu_p", 250 .cache_bytes = 2u << 20 }; 251 g_class[RX_CM_CORE_A725] = (CoreClass){ .name = "Cortex-A725", .meter = "cpu_e", 252 .cache_bytes = 512u << 10 }; 253 g_class[RX_CM_CORE_OTHER] = (CoreClass){ .name = "other", .meter = NULL, 254 .cache_bytes = 1u << 20 }; 255 CPU_ZERO(&g_allowed); 256 if (sched_getaffinity(0, sizeof g_allowed, &g_allowed) != 0) 257 for (int c = 0; c < 64; c++) CPU_SET(c, &g_allowed); 258 long n = sysconf(_SC_NPROCESSORS_CONF); 259 for (long c = 0; c < n && c < 64; c++) { 260 if (!CPU_ISSET((int)c, &g_allowed)) continue; /* taskset/cgroup: not ours to time on */ 261 char path[128]; 262 snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c); 263 FILE *fp = fopen(path, "r"); 264 unsigned long long midr = 0; 265 int ok = fp && fscanf(fp, "%llx", &midr) == 1; 266 if (fp) fclose(fp); 267 uint32_t part = ok ? (uint32_t)((midr >> 4) & 0xfffu) : 0; 268 uint32_t k = part == 0xd85u ? RX_CM_CORE_X925 : part == 0xd87u ? RX_CM_CORE_A725 269 : RX_CM_CORE_OTHER; 270 g_class[k].cpus[g_class[k].n++] = (int)c; 271 } 272 char *p = g_cpus_desc; 273 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 274 CoreClass *cc = &g_class[k]; 275 if (!cc->n) continue; 276 cc->timed = cc->cpus[cc->n - 1]; 277 p += snprintf(p, sizeof g_cpus_desc - (size_t)(p - g_cpus_desc), "%s%s x%d timed on cpu%d", 278 p == g_cpus_desc ? "" : "; ", cc->name, cc->n, cc->timed); 279 } 280 } 281 282 /* Busy fraction of each cpu over ~150 ms, from /proc/stat. */ 283 static void cpu_busy(double *busy, int ncpu) { 284 unsigned long long a[2][64][2]; 285 memset(a, 0, sizeof a); 286 for (int s = 0; s < 2; s++) { 287 FILE *fp = fopen("/proc/stat", "r"); 288 char line[512]; 289 while (fp && fgets(line, sizeof line, fp)) { 290 int c; 291 unsigned long long u, n, sy, id, io, irq, sirq, st; 292 if (sscanf(line, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu", &c, &u, &n, &sy, &id, 293 &io, &irq, &sirq, &st) == 9 && c >= 0 && c < 64) { 294 a[s][c][0] = u + n + sy + irq + sirq + st; 295 a[s][c][1] = u + n + sy + irq + sirq + st + id + io; 296 } 297 } 298 if (fp) fclose(fp); 299 if (s == 0) { 300 struct timespec ts = { 0, `150000000` }; 301 nanosleep(&ts, NULL); 302 } 303 } 304 for (int c = 0; c < ncpu && c < 64; c++) { 305 unsigned long long tot = a[1][c][1] - a[0][c][1]; 306 busy[c] = tot ? (double)(a[1][c][0] - a[0][c][0]) / (double)tot : 1.0; 307 } 308 } 309 310 static int pin(int cpu) { 311 cpu_set_t s; 312 CPU_ZERO(&s); 313 CPU_SET(cpu, &s); 314 return sched_setaffinity(0, sizeof s, &s); 315 } 316 317 /* Time on the quietest core of the class right now: this machine is shared. */ 318 static char g_timed_log[512]; 319 static void place_quiet(uint32_t k) { 320 double busy[64]; 321 cpu_busy(busy, 64); 322 CoreClass *cc = &g_class[k]; 323 int best = cc->cpus[cc->n - 1]; 324 for (int i = 0; i < cc->n; i++) 325 if (busy[cc->cpus[i]] < busy[best]) best = cc->cpus[i]; 326 cc->timed = best; 327 pin(best); 328 size_t l = strlen(g_timed_log); 329 if (l < sizeof g_timed_log - 16) 330 snprintf(g_timed_log + l, sizeof g_timed_log - l, "%s%d", l ? "," : "", best); 331 } 332 333 static void loadavg(char *out, size_t n) { 334 FILE *fp = fopen("/proc/loadavg", "r"); 335 double a = 0, b = 0, c = 0; 336 if (fp) { 337 if (fscanf(fp, "%lf %lf %lf", &a, &b, &c) != 3) a = b = c = 0; 338 fclose(fp); 339 } 340 snprintf(out, n, "%.2f %.2f %.2f", a, b, c); 341 } 342 343 static uint32_t thermal_c(void) { 344 uint32_t best = 0; 345 for (int h = 0; h < 16; h++) { 346 char path[128], name[64] = { 0 }; 347 snprintf(path, sizeof path, "/sys/class/hwmon/hwmon%d/name", h); 348 FILE *fp = fopen(path, "r"); 349 if (!fp) continue; 350 int ok = fscanf(fp, "%63s", name) == 1; 351 fclose(fp); 352 if (!ok || strcmp(name, "acpitz") != 0) continue; 353 for (int t = 1; t <= 8; t++) { 354 snprintf(path, sizeof path, "/sys/class/hwmon/hwmon%d/temp%d_input", h, t); 355 fp = fopen(path, "r"); 356 if (!fp) continue; 357 long v = 0; 358 if (fscanf(fp, "%ld", &v) == 1 && v / 1000 > (long)best) best = (uint32_t)(v / 1000); 359 fclose(fp); 360 } 361 } 362 return best; 363 } 364 365 static char g_meter_dir[128]; 366 367 /* ---- experience record tap (default off; aien-train #24 ORIG-1, omega #731) ---- 368 * OMEGA_EMPIRICAL_RECORDS= appends one fixed 256-byte little-endian 369 * record per cost table to descriptor fd (3..1023), which the caller opened 370 * for append on a regular file, e.g. OMEGA_EMPIRICAL_RECORDS=3 ... 3>>records.bin. 371 * The harness opens no path named by its environment; any other value leaves 372 * the tap off. Records hold the state the consumer saw and every eligible arm's 373 * measured, digest-checked cost. Observation only: no decision, measurement 374 * or verdict of this harness reads it. Layout omexp-v1, 32 words of 8 bytes: 375 * 0 magic 0x314f4d4558505243 ("CRPXEMO1" LE) 1 version 1 376 * 2 family (0 train grid, 1 validation grid, 2 H1, 3 H2, 4 H3, 5 coverage sample) 377 * 3 core class (RX_CM_CORE_*) 4 pressure bucket 5 M 6 N 7 calls 378 * 8 job seed 9 state bytes 10 cache bytes 11 thermal C 12 eligible mask 379 * 13 arm count 14 sequence 15 monotonic ns 380 * 16..23 ps per call per arm (0 = not measured) 24..29 verified flag per arm 381 * 30 reference digest 31 digest mismatches during this table (0 = all verified) */ 382 #define OMEXP_MAGIC 0x314f4d4558505243ull 383 static int g_tap_fd = -1; 384 static uint64_t g_tap_seq; 385 static uint32_t g_tap_family, g_tap_core, g_tap_pressure; 386 387 static void tap_open(void) { 388 const char *p = getenv("OMEGA_EMPIRICAL_RECORDS"); 389 if (!p || !p[0]) return; 390 char *end; 391 errno = 0; 392 long fd = strtol(p, &end, 10); 393 struct stat sb; 394 int fl = -1; 395 if (errno != 0 || *end != '\0' || fd < 3 || fd > 1023 || (fl = fcntl((int)fd, F_GETFL)) < 0 || 396 (fl & O_ACCMODE) == O_RDONLY || !(fl & O_APPEND) || fstat((int)fd, &sb) != 0 || !S_ISREG(sb.st_mode)) { 397 fprintf(stderr, " record tap: OMEGA_EMPIRICAL_RECORDS=%s is not a descriptor 3..1023 open for append " 398 "on a regular file; tap off\n", p); 399 return; 400 } 401 g_tap_fd = (int)fd; 402 } 403 404 static void tap_ctx(uint32_t family, uint32_t core, uint32_t pressure) { 405 g_tap_family = family; 406 g_tap_core = core; 407 g_tap_pressure = pressure; 408 } 409 410 static void tap_record(const Job *j, const uint64_t table[N_ARMS], uint64_t mismatches) { 411 if (g_tap_fd < 0) return; 412 uint64_t w[32]; 413 uint8_t b[256]; 414 memset(w, 0, sizeof w); 415 w[0] = OMEXP_MAGIC; 416 w[1] = 1; 417 w[2] = g_tap_family; 418 w[3] = g_tap_core; 419 w[4] = g_tap_pressure; 420 w[5] = j->M; 421 w[6] = j->N; 422 w[7] = j->calls; 423 w[8] = j->seed; 424 w[9] = ((uint64_t)j->M * j->N + j->M + j->N) * 8u; 425 w[10] = g_class[g_tap_core].cache_bytes; 426 w[11] = thermal_c(); 427 w[12] = g_eligible; 428 w[13] = N_ARMS; 429 w[14] = g_tap_seq++; 430 w[15] = now_ns(); 431 for (uint32_t a = 0; a < N_ARMS && a < 8; a++) { 432 w[16 + a] = table[a] == UINT64_MAX ? 0 : table[a]; 433 w[24 + a] = g_verified[a] ? 1u : 0u; 434 } 435 w[30] = j->ref_digest; 436 w[31] = mismatches; 437 for (int i = 0; i < 32; i++) 438 for (int k = 0; k < 8; k++) b[i * 8 + k] = (uint8_t)(w[i] >> (8 * k)); 439 size_t off = 0; 440 while (off < sizeof b) { 441 ssize_t n = write(g_tap_fd, b + off, sizeof b - off); 442 if (n <= 0) break; 443 off += (size_t)n; 444 } 445 } 446 447 /* truth_table plus the tap; the table itself is unchanged. */ 448 static void truth_table_tap(const Job *j, uint64_t table[N_ARMS]) { 449 uint64_t before = K.mismatches; 450 truth_table(j, table); 451 tap_record(j, table, K.mismatches - before); 452 } 453 454 static void find_meter(void) { 455 for (int h = 0; h < 32; h++) { 456 char path[128], name[64] = { 0 }; 457 snprintf(path, sizeof path, "/sys/class/hwmon/hwmon%d/name", h); 458 FILE *fp = fopen(path, "r"); 459 if (!fp) continue; 460 int ok = fscanf(fp, "%63s", name) == 1; 461 fclose(fp); 462 if (ok && strcmp(name, "aien_spbm") == 0) { 463 snprintf(g_meter_dir, sizeof g_meter_dir, "/sys/class/hwmon/hwmon%d", h); 464 return; 465 } 466 } 467 } 468 469 /* Cumulative microjoules of the cluster meter with this label; 0 if none. */ 470 static uint64_t meter_uj(const char *label) { 471 if (!label || !g_meter_dir[0]) return 0; 472 for (int i = 1; i <= 8; i++) { 473 char path[192], lab[64] = { 0 }; 474 snprintf(path, sizeof path, "%s/energy%d_label", g_meter_dir, i); 475 FILE *fp = fopen(path, "r"); 476 if (!fp) continue; 477 int ok = fscanf(fp, "%63s", lab) == 1; 478 fclose(fp); 479 if (!ok || strcmp(lab, label) != 0) continue; 480 snprintf(path, sizeof path, "%s/energy%d_input", g_meter_dir, i); 481 fp = fopen(path, "r"); 482 unsigned long long v = 0; 483 if (fp) { 484 if (fscanf(fp, "%llu", &v) != 1) v = 0; 485 fclose(fp); 486 } 487 return v; 488 } 489 return 0; 490 } 491 492 #define PRESS_THREADS 6 493 #define PRESS_BYTES (64u << 20) 494 495 static atomic_int g_press_stop; 496 static pthread_t g_press[PRESS_THREADS]; 497 static int g_press_n; 498 static uint64_t g_press_sweeps[PRESS_THREADS]; 499 500 typedef struct { 501 int cpu; 502 int idx; 503 } PressArg; 504 static PressArg g_press_arg[PRESS_THREADS]; 505 506 static void *press_main(void *arg) { 507 PressArg *pa = arg; 508 pin(pa->cpu); 509 uint64_t *buf = malloc(PRESS_BYTES); 510 if (!buf) return NULL; 511 size_t n = PRESS_BYTES / 8u; 512 for (size_t i = 0; i < n; i++) buf[i] = i; 513 uint64_t sweeps = 0; 514 while (!atomic_load(&g_press_stop)) { 515 for (size_t i = 0; i < n; i += 8) buf[i] = buf[i] * 3u + 1u; 516 sweeps++; 517 } 518 g_press_sweeps[pa->idx] = sweeps; 519 free(buf); 520 return NULL; 521 } 522 523 static void pressure_on(int timed) { 524 atomic_store(&g_press_stop, 0); 525 long n = sysconf(_SC_NPROCESSORS_CONF); 526 g_press_n = 0; 527 for (long c = n - 1; c >= 0 && g_press_n < PRESS_THREADS; c--) { 528 if ((int)c == timed || !CPU_ISSET((int)c, &g_allowed)) continue; 529 g_press_arg[g_press_n] = (PressArg){ (int)c, g_press_n }; 530 if (pthread_create(&g_press[g_press_n], NULL, press_main, &g_press_arg[g_press_n]) == 0) 531 g_press_n++; 532 } 533 struct timespec ts = { 0, `50000000` }; 534 nanosleep(&ts, NULL); 535 } 536 537 static void pressure_off(void) { 538 atomic_store(&g_press_stop, 1); 539 for (int i = 0; i < g_press_n; i++) pthread_join(g_press[i], NULL); 540 g_press_n = 0; 541 } 542 543 /* ---- synthesis and verification ---- */ 544 545 static RxCostModel g_meta; /* verify/synth/code/power figures, copied into every model */ 546 547 static int map_code(const uint8_t *code, size_t len, MvFn *out) { 548 void *p = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 549 if (p == MAP_FAILED) return -1; 550 memcpy(p, code, len); 551 __builtin___clear_cache((char *)p, (char *)p + len); 552 if (mprotect(p, 4096, PROT_READ | PROT_EXEC) != 0) return -1; 553 union { void *v; MvFn f; } u = { p }; 554 *out = u.f; 555 return 0; 556 } 557 558 static const uint32_t VM[] = { 1, 2, 3, 4, 5, 7, 8, 16, 31, 64 }; 559 static const uint32_t VN[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 560 255, 256, 257 }; 561 562 /* Differential against the semantic reference, in the child. 0 pass, 1 563 * wrong, 2 wrote outside y or changed A or x. */ 564 static int differential(MvFn fn) { 565 static uint64_t A[64 * 257], A0[64 * 257], x[257], x0[257], y[64 + GUARD], yr[64]; 566 const uint64_t CANARY = 0xC0FFEE0DDBA11AD5ull; 567 int wrong = 0; 568 for (unsigned mi = 0; mi < sizeof VM / sizeof VM[0]; mi++) 569 for (unsigned ni = 0; ni < sizeof VN / sizeof VN[0]; ni++) 570 for (int kind = 0; kind < 3; kind++) { 571 uint32_t M = VM[mi], N = VN[ni]; 572 uint64_t s = 0x1234567ull + mi * 131u + ni * 7u + (uint64_t)kind; 573 for (uint32_t i = 0; i < M * N; i++) 574 A[i] = kind == 0 ? xs(&s) : kind == 1 ? UINT64_MAX : (1ull << 63) + i; 575 for (uint32_t i = 0; i < N; i++) 576 x[i] = kind == 0 ? xs(&s) : kind == 1 ? UINT64_MAX : (1ull << 63) - i; 577 memcpy(A0, A, sizeof(uint64_t) * M * N); 578 memcpy(x0, x, sizeof(uint64_t) * N); 579 for (uint32_t i = 0; i < M + GUARD; i++) y[i] = CANARY; 580 omega_matvec_reference(A, x, yr, M, N); 581 fn(A, x, y, M, N); 582 for (uint32_t i = M; i < M + GUARD; i++) 583 if (y[i] != CANARY) return 2; 584 if (memcmp(A, A0, sizeof(uint64_t) * M * N) || memcmp(x, x0, sizeof(uint64_t) * N)) 585 return 2; 586 if (memcmp(y, yr, sizeof(uint64_t) * M)) wrong = 1; 587 } 588 return wrong; 589 } 590 591 /* Fork, run the differential with a deadline, report. */ 592 static int verify_in_child(MvFn fn, uint64_t *ns) { 593 uint64_t t0 = now_ns(); 594 pid_t pid = fork(); 595 if (pid == 0) { 596 alarm(10); 597 _exit(differential(fn)); 598 } 599 int st = 0; 600 if (pid < 0 || waitpid(pid, &st, 0) != pid) return -1; 601 *ns = now_ns() - t0; 602 if (!WIFEXITED(st)) return 3; 603 return WEXITSTATUS(st); 604 } 605 606 static struct { 607 int verdict[N_ARMS]; 608 } V; 609 610 static void setup_arms(void) { 611 OmegaMachineGraph mg; 612 MatVecSemanticSpec spec; 613 CHECK(omega_machine_build_dgx_spark(&mg) == 0, "machine graph"); 614 CHECK(omega_matvec_spec_init(&spec, "empirical_matvec", MAX_M, MAX_N) == 0, "spec"); 615 MatVecLivingKernel kernel; 616 memset(&kernel, 0, sizeof kernel); 617 kernel.spec = spec; 618 kernel.machine = &mg; 619 rx_cm_init(&g_meta, N_ARMS); 620 g_fn[ARM_REF] = arm_reference; 621 g_fn[ARM_PLANTED] = arm_planted; 622 for (uint32_t a = ARM_SCALAR; a <= ARM_QUAD4; a++) { 623 MatVecRealization r; 624 uint64_t t0 = now_ns(); 625 int rc = a == ARM_QUAD4 ? omega_matvec_synthesize_quad4(&spec, &mg, &r) 626 : omega_matvec_synthesize(&kernel, (MatVecRealizationKind)(a - 1), &r); 627 g_meta.synth_ns[a] = now_ns() - t0; 628 CHECK(rc == 0, "synthesize %s", ARM_NAME[a]); 629 g_meta.code_bytes[a] = r.realization.code_len; 630 CHECK(map_code(r.realization.code_bytes, r.realization.code_len, &g_fn[a]) == 0, "map %s", 631 ARM_NAME[a]); 632 } 633 for (uint32_t a = 0; a < N_ARMS; a++) { 634 uint64_t ns = 0; 635 V.verdict[a] = verify_in_child(g_fn[a], &ns); 636 g_meta.verify_ns[a] = ns; 637 g_verified[a] = V.verdict[a] == 0; 638 if (g_verified[a]) g_eligible |= 1u << a; 639 printf(" %-13s code %4llu B synth %6llu ns verify %8llu ns %s\n", ARM_NAME[a], 640 (unsigned long long)g_meta.code_bytes[a], (unsigned long long)g_meta.synth_ns[a], 641 (unsigned long long)ns, g_verified[a] ? "verified" : "REFUSED"); 642 } 643 for (uint32_t a = 0; a < ARM_PLANTED; a++) CHECK(g_verified[a], "%s verified", ARM_NAME[a]); 644 CHECK(V.verdict[ARM_PLANTED] == 1, "planted arm refused as wrong (%d)", V.verdict[ARM_PLANTED]); 645 CHECK(g_parent_runs[ARM_PLANTED] == 0, "planted arm never ran in the parent"); 646 } 647 648 /* Power while an arm runs, by core class, from the cluster meter, minus the 649 * cluster's idle draw. Each window is long enough for the ~0.1 s meter. */ 650 static double g_idle_mw[RX_CM_CORES]; 651 652 static double window_mw(const char *label, uint32_t arm, const Job *j, uint64_t ns_target) { 653 uint64_t e0 = meter_uj(label), t0 = now_ns(), t = t0; 654 if (arm == UINT32_MAX) { 655 struct timespec ts = { (time_t)(ns_target / 1000000000ull), (long)(ns_target % 1000000000ull) }; 656 nanosleep(&ts, NULL); 657 t = now_ns(); 658 } else { 659 while (t - t0 < ns_target) { 660 for (uint32_t c = 0; c < j->calls; c++) exec_arm(arm, g_A, g_x, g_y, j->M, j->N); 661 t = now_ns(); 662 } 663 } 664 uint64_t e1 = meter_uj(label); 665 if (!e0 || e1 <= e0) return 0.0; 666 return (double)(e1 - e0) / ((double)(t - t0) / 1e9) / 1000.0; /* uJ/s = uW -> mW */ 667 } 668 669 static void measure_power(void) { 670 Job j = { 256, 256, 0, 77, 0 }; 671 j.calls = calls_for(j.M, j.N); 672 job_load(&j); 673 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 674 CoreClass *cc = &g_class[k]; 675 if (!cc->n || !cc->meter || !g_meter_dir[0]) continue; 676 pin(cc->timed); 677 g_idle_mw[k] = window_mw(cc->meter, UINT32_MAX, &j, 500000000ull); 678 for (uint32_t a = 0; a < N_ARMS; a++) { 679 if (!g_verified[a]) continue; 680 double mw = window_mw(cc->meter, a, &j, 500000000ull) - g_idle_mw[k]; 681 g_meta.power_mw[k][a] = mw > 1.0 ? mw : 1.0; 682 } 683 printf(" power on %s: idle %.0f mW; running above idle:", cc->name, g_idle_mw[k]); 684 for (uint32_t a = 0; a < N_ARMS; a++) 685 if (g_verified[a]) printf(" %s %.0f", ARM_NAME[a], g_meta.power_mw[k][a]); 686 printf(" mW\n"); 687 } 688 } 689 690 static void model_fresh(RxCostModel *m) { 691 rx_cm_init(m, N_ARMS); 692 memcpy(m->power_mw, g_meta.power_mw, sizeof m->power_mw); 693 memcpy(m->verify_ns, g_meta.verify_ns, sizeof m->verify_ns); 694 memcpy(m->synth_ns, g_meta.synth_ns, sizeof m->synth_ns); 695 memcpy(m->code_bytes, g_meta.code_bytes, sizeof m->code_bytes); 696 /* Verification outcomes are observations too: the refusal counts as a failure. */ 697 RxCmFeatures f; 698 memset(&f, 0, sizeof f); 699 f.M = f.N = 1; 700 for (uint32_t a = 0; a < N_ARMS; a++) { 701 RxCmObservation o = { 1, V.verdict[a] != 0 }; 702 if (o.failed) rx_cm_observe(m, &f, a, &o); 703 } 704 } 705 706 /* ---- policies ---- */ 707 708 static uint32_t dispatch_rule(uint32_t N) { 709 /* omega_matvec_dispatch's fixed rule, over the pre-mapped arms. */ 710 return N <= 8 ? ARM_U2 : ARM_U4D; 711 } 712 713 static RxCmFeatures features(const Job *j, uint32_t core, uint32_t pressure, uint32_t current) { 714 RxCmFeatures f; 715 memset(&f, 0, sizeof f); 716 f.op = RX_CM_OP_MATVEC; 717 f.M = j->M; 718 f.N = j->N; 719 f.state_bytes = ((uint64_t)j->M * j->N + j->M + j->N) * 8u; 720 f.cache_bytes = g_class[core].cache_bytes; 721 f.core = core; 722 f.pressure = pressure; 723 f.thermal_c = thermal_c(); 724 f.current_arm = current; 725 f.eligible = g_eligible; 726 f.need_evidence = 1; 727 return f; 728 } 729 730 typedef struct { 731 uint64_t measures, selects, fallbacks, touched_ineligible; 732 uint64_t explore_ps; 733 } LearnStats; 734 735 /* The online exploration budget, audited per core class over H1, H2 and H3: 736 * the online model's counters run across all three. The rule the learner 737 * spends by: a measurement starts only while spent exploration is under 738 * explore_frac of chosen work plus the allowance. */ 739 typedef struct { 740 uint64_t started_over; /* measurements started at or over the line */ 741 uint64_t last_extra_ps; /* the latest exploration charge since the counters were reset */ 742 } BudgetAudit; 743 744 static double budget_line(const RxCostModel *m, const RxCmPolicy *p) { 745 return p->explore_frac * (double)m->work_ps + (double)p->explore_allow_ps; 746 } 747 748 /* Before a measurement runs, with the counters the policy decided on. */ 749 static void audit_start(BudgetAudit *a, const RxCostModel *m, const RxCmPolicy *p) { 750 if ((double)m->explore_ps >= budget_line(m, p)) a->started_over++; 751 } 752 753 /* After a group of jobs. Every measurement started under the line and the 754 * line only grows with chosen work, so spent exploration minus the latest 755 * charge is still under it: the total exceeds the line by less than that one 756 * charge. A group that measured nothing is held to the charge that last 757 * crossed the line, which an earlier group may have made. */ 758 static int audit_over(const BudgetAudit *a, const RxCostModel *m, const RxCmPolicy *p) { 759 return a->started_over != 0 || m->explore_ps < a->last_extra_ps || 760 (double)(m->explore_ps - a->last_extra_ps) >= budget_line(m, p); 761 } 762 763 /* One job for an online learner: decide, run what it decided, learn from 764 * those runs only. Returns the policy's cost in table terms (ps). `audit` 765 * is NULL where the budget is not judged (training). */ 766 static double learner_job(RxCostModel *m, const RxCmPolicy *p, const Job *j, uint32_t core, 767 uint32_t pressure, uint32_t *current, const uint64_t *table, 768 LearnStats *s, BudgetAudit *audit) { 769 RxCmFeatures f = features(j, core, pressure, *current); 770 RxCmDecision d; 771 rx_cm_decide(m, p, &f, 1, &d); 772 if (!(g_eligible & (1u << d.arm)) || 773 (d.action == RX_CM_MEASURE && 774 (!(g_eligible & (1u << d.measure[0])) || !(g_eligible & (1u << d.measure[1]))))) 775 s->touched_ineligible++; 776 if (d.action == RX_CM_MEASURE) { 777 /* The job runs on the chosen arm; the competitor is probed on a 778 * quarter of the calls. The probe is the exploration cost. */ 779 uint32_t a = d.measure[0], b = d.measure[1]; 780 uint32_t probe = j->calls / 4u < 3u ? 3u : j->calls / 4u; 781 if (audit) audit_start(audit, m, p); 782 uint64_t ta = time_arm_chunked(a, j, j->calls), tb = time_arm_chunked(b, j, probe); 783 RxCmObservation oa = { ta, ta == 0 }, ob = { tb, tb == 0 }; 784 rx_cm_observe(m, &f, a, &oa); 785 rx_cm_observe(m, &f, b, &ob); 786 rx_cm_charge(m, ta * j->calls, tb * probe); 787 if (audit) audit->last_extra_ps = tb * probe; 788 *current = ta <= tb ? a : b; 789 s->measures++; 790 s->explore_ps += table[b] * probe; 791 return (double)table[a] * j->calls + (double)table[b] * probe; 792 } 793 uint64_t t = time_arm_chunked(d.arm, j, j->calls); 794 RxCmObservation o = { t, t == 0 }; 795 rx_cm_observe(m, &f, d.arm, &o); 796 rx_cm_charge(m, t * j->calls, 0); 797 *current = d.arm; 798 if (d.action == RX_CM_SELECT) s->selects++; 799 else s->fallbacks++; 800 return (double)table[d.arm] * j->calls; 801 } 802 803 /* The audit on a fixed sequence through the cost model's own decisions, no 804 * timing: H1 measures until its last probe ends over the line, H2 is refused 805 * every measurement and runs chosen work only, so it measures nothing while 806 * the total is still over the line. Neither is a violation; a measurement 807 * started over the line and exploration charged without a measurement are. */ 808 static void budget_audit_selftest(const RxCmPolicy *p) { 809 RxCostModel m; 810 rx_cm_init(&m, N_ARMS); 811 Job j = { 64, 64, 1, 1, 0 }; 812 RxCmFeatures f = features(&j, 0, 0, ARM_REF); 813 f.eligible = (1u << ARM_REF) | (1u << ARM_U2); 814 const uint64_t work = 100000000ull, extra = 400000000ull; /* 0.1 ms chosen, 0.4 ms probed */ 815 BudgetAudit a = { 0, 0 }; 816 uint32_t measured = 0; 817 for (uint32_t i = 0; i < 8; i++) { 818 RxCmDecision d; 819 rx_cm_decide(&m, p, &f, 1, &d); 820 if (d.action != RX_CM_MEASURE) break; 821 audit_start(&a, &m, p); 822 rx_cm_charge(&m, work, extra); 823 a.last_extra_ps = extra; 824 measured++; 825 } 826 CHECK(measured > 0 && (double)m.explore_ps >= budget_line(&m, p), 827 "budget self-test: H1 measured %u and ends over the line", measured); 828 CHECK(!audit_over(&a, &m, p), "budget self-test: H1 within its budget"); 829 uint32_t h2_measured = 0; 830 for (uint32_t i = 0; i < 4; i++) { 831 RxCmDecision d; 832 rx_cm_decide(&m, p, &f, 1, &d); 833 if (d.action == RX_CM_MEASURE) h2_measured++; 834 rx_cm_charge(&m, work, 0); 835 } 836 CHECK(h2_measured == 0 && (double)m.explore_ps >= budget_line(&m, p), 837 "budget self-test: H2 measured %u and stays over the line", h2_measured); 838 CHECK(!audit_over(&a, &m, p), "budget self-test: a group that measured nothing is held to the last charge"); 839 BudgetAudit over = a; 840 audit_start(&over, &m, p); 841 CHECK(audit_over(&over, &m, p), "budget self-test: a measurement started over the line is caught"); 842 RxCostModel unmeasured = m; 843 rx_cm_charge(&unmeasured, 0, extra / 2u); 844 CHECK(audit_over(&a, &unmeasured, p), "budget self-test: exploration charged with no measurement is caught"); 845 } 846 847 static uint32_t frozen_choice(const RxCostModel *m, const RxCmPolicy *p, const Job *j, 848 uint32_t core, uint32_t pressure, RxCmDecision *out) { 849 RxCmFeatures f = features(j, core, pressure, ARM_REF); 850 rx_cm_decide(m, p, &f, 0, out); 851 return out->arm; 852 } 853 854 /* ---- workloads ---- */ 855 856 static const uint32_t TRAIN_M[] = { 1, 4, 16, 64, 256, 1024 }; 857 static const uint32_t TRAIN_N[] = { 1, 2, 3, 5, 8, 12, 16, 33, 64, 100, 256, 512, 1024 }; 858 static const uint32_t VAL_M[] = { 2, 8, 32, 128, 512 }; 859 static const uint32_t VAL_N[] = { 4, 6, 10, 24, 48, 200, 700 }; 860 #define NA(a) (sizeof(a) / sizeof((a)[0])) 861 862 static int in_set(uint32_t v, const uint32_t *s, size_t n) { 863 for (size_t i = 0; i < n; i++) 864 if (s[i] == v) return 1; 865 return 0; 866 } 867 868 #define MAX_JOBS 128 869 870 static uint32_t grid(Job *out, const uint32_t *Ms, size_t nm, const uint32_t *Ns, size_t nn, 871 uint64_t seed) { 872 uint32_t n = 0; 873 for (size_t i = 0; i < nm; i++) 874 for (size_t k = 0; k < nn; k++) { 875 if ((uint64_t)Ms[i] * Ns[k] > MAX_ELEMS || n >= MAX_JOBS) continue; 876 out[n] = (Job){ Ms[i], Ns[k], calls_for(Ms[i], Ns[k]), seed + n * 7919u, 0 }; 877 n++; 878 } 879 return n; 880 } 881 882 static uint32_t log_uniform(uint64_t *s, double lo, double hi) { 883 double u = (double)(xs(s) >> 11) / `9007199254740992`.0; 884 return (uint32_t)llround(exp2(lo + (hi - lo) * u)); 885 } 886 887 static int seen(uint32_t M, uint32_t N) { 888 return in_set(M, TRAIN_M, NA(TRAIN_M)) || in_set(M, VAL_M, NA(VAL_M)) || 889 in_set(N, TRAIN_N, NA(TRAIN_N)) || in_set(N, VAL_N, NA(VAL_N)); 890 } 891 892 static uint32_t unseen_shapes(Job *out, uint32_t count, uint64_t seed) { 893 uint64_t s = seed; 894 uint32_t n = 0; 895 while (n < count) { 896 uint32_t M = log_uniform(&s, 1.5, 11.0), N = log_uniform(&s, 1.5, 12.0); 897 if (seen(M, N) || (uint64_t)M * N > MAX_ELEMS || M > MAX_M || N > MAX_N) continue; 898 out[n] = (Job){ M, N, calls_for(M, N), seed + n * 104729u, 0 }; 899 n++; 900 } 901 return n; 902 } 903 904 static uint32_t thin_mix(Job *out, uint32_t count, uint64_t seed) { 905 uint64_t s = seed; 906 uint32_t n = 0; 907 while (n < count) { 908 uint32_t M, N; 909 if (xs(&s) % 10u < 7u) { 910 N = 1u + (uint32_t)(xs(&s) % 7u); 911 M = log_uniform(&s, 4.0, 12.5); 912 if (in_set(M, TRAIN_M, NA(TRAIN_M)) || in_set(M, VAL_M, NA(VAL_M))) continue; 913 } else { 914 M = log_uniform(&s, 1.5, 11.0); 915 N = log_uniform(&s, 1.5, 12.0); 916 if (seen(M, N)) continue; 917 } 918 if ((uint64_t)M * N > MAX_ELEMS || M > MAX_M || N > MAX_N) continue; 919 out[n] = (Job){ M, N, calls_for(M, N), seed + n * 15485863u, 0 }; 920 n++; 921 } 922 return n; 923 } 924 925 static void shuffle(Job *j, uint32_t n, uint64_t seed) { 926 uint64_t s = seed; 927 for (uint32_t i = n; i > 1; i--) { 928 uint32_t k = (uint32_t)(xs(&s) % i); 929 Job t = j[i - 1]; 930 j[i - 1] = j[k]; 931 j[k] = t; 932 } 933 } 934 935 /* ---- receipt figures ---- */ 936 937 enum { D_H1 = 0, D_H2, D_H3, N_DIST }; 938 static const char *const DIST_NAME[N_DIST] = { "H1_unseen_shapes", "H2_thin_heavy_mix", 939 "H3_memory_pressure" }; 940 enum { P_DISPATCH = 0, P_REFERENCE, P_BEST_STATIC, P_FROZEN, P_ONLINE, P_ORACLE, N_POL }; 941 static const char *const POL_NAME[N_POL] = { "fixed_dispatch_rule", "always_reference", 942 "best_static_from_training", "learned_frozen", 943 "learned_online", "oracle" }; 944 945 typedef struct { 946 uint32_t jobs; 947 double cost[N_POL]; /* ps, table terms */ 948 uint64_t measures, fallbacks, explore_ps; 949 uint32_t cov_n, cov_in80, cov_in95; 950 double wall_ns[4], energy_uj[4], wait_ns[4]; 951 int contended_blocks_only; /* no block ran without waiting for its core */ /* replay: dispatch, reference, static, frozen */ 952 int replayed; 953 double contention_median; /* H3 only: pressure / no pressure, same arm and shape */ 954 } DistResult; 955 956 static struct { 957 int present[RX_CM_CORES]; 958 DistResult d[RX_CM_CORES][N_DIST]; 959 double train_cost[RX_CM_CORES][N_ARMS]; 960 uint32_t best_static[RX_CM_CORES]; 961 uint32_t train_jobs[RX_CM_CORES], train_measures[RX_CM_CORES]; 962 double val_learned, val_dispatch, val_reference, val_poison; 963 uint32_t val_jobs, val_cov_n, val_cov_in80; 964 int proofs_ok; 965 /* promotion */ 966 int learner_promote_rc, learner_with_promoter_cap_rc, poison_judge_ok, poison_promote_rc, 967 promote_rc, recover_coherent, durable_digest_match, torn_rc, torn_fallback_ok, 968 online_not_durable; 969 uint64_t active_id; 970 char model_digest[65]; 971 size_t model_bytes; 972 uint64_t explore_budget_violations; 973 uint64_t touched_ineligible; 974 uint32_t thermal_start, thermal_end; 975 double sd_scale; 976 char load_start[48], load_end[48]; 977 } R; 978 979 static void hex(const uint8_t *d, size_t n, char *out) { 980 for (size_t i = 0; i < n; i++) sprintf(out + i * 2, "%02x", d[i]); 981 } 982 983 /* ---- phases ---- */ 984 985 static RxCmPolicy g_train_policy, g_online_policy, g_frozen_policy; 986 987 static void train_class(RxCostModel *m, uint32_t k) { 988 Job jobs[MAX_JOBS]; 989 uint32_t n = grid(jobs, TRAIN_M, NA(TRAIN_M), TRAIN_N, NA(TRAIN_N), 1000u + k); 990 LearnStats s; 991 memset(&s, 0, sizeof s); 992 uint32_t current = ARM_REF; 993 tap_ctx(0, k, 0); 994 for (int pass = 0; pass < 2; pass++) { 995 shuffle(jobs, n, 99u + (uint64_t)pass + k); 996 for (uint32_t i = 0; i < n; i++) { 997 job_load(&jobs[i]); 998 uint64_t table[N_ARMS]; 999 truth_table_tap(&jobs[i], table); 1000 for (uint32_t a = 0; a < N_ARMS; a++) 1001 if (g_eligible & (1u << a)) R.train_cost[k][a] += (double)table[a] * jobs[i].calls; 1002 learner_job(m, &g_train_policy, &jobs[i], k, 0, ¤t, table, &s, NULL); 1003 } 1004 } 1005 R.train_jobs[k] = 2 * n; 1006 R.train_measures[k] = (uint32_t)s.measures; 1007 R.touched_ineligible += s.touched_ineligible; 1008 uint32_t best = ARM_REF; 1009 for (uint32_t a = 0; a < N_ARMS; a++) 1010 if ((g_eligible & (1u << a)) && R.train_cost[k][a] < R.train_cost[k][best]) best = a; 1011 R.best_static[k] = best; 1012 printf(" trained on %s: %u jobs, measured twice in %llu of them; best single arm on " 1013 "training: %s\n", 1014 g_class[k].name, 2 * n, (unsigned long long)s.measures, ARM_NAME[best]); 1015 } 1016 1017 static void coverage(const RxCostModel *m, const Job *j, uint32_t core, uint32_t pressure, 1018 const uint64_t *table, uint32_t *n, uint32_t *in80, uint32_t *in95) { 1019 RxCmFeatures f = features(j, core, pressure, ARM_REF); 1020 for (uint32_t a = 0; a < N_ARMS; a++) { 1021 if (!(g_eligible & (1u << a)) || table[a] == UINT64_MAX) continue; 1022 RxCmPrediction p; 1023 rx_cm_predict(m, &f, a, &p); 1024 if (!p.n) continue; 1025 double z = fabs(log2((double)table[a]) - p.mean_log2_ps); 1026 (*n)++; 1027 *in80 += z <= rx_cm_t_quantile(0.80, p.dof) * p.sd_log2; 1028 if (in95) *in95 += z <= rx_cm_t_quantile(0.95, p.dof) * p.sd_log2; 1029 } 1030 } 1031 1032 /* The judge: evaluates a candidate model on validation work it measures 1033 * itself. Returns the candidate's cost; also the fixed rule's. */ 1034 typedef struct { 1035 Job jobs[RX_CM_CORES][MAX_JOBS]; 1036 uint64_t table[RX_CM_CORES][MAX_JOBS][N_ARMS]; 1037 uint32_t n[RX_CM_CORES]; 1038 } ValSet; 1039 1040 static ValSet g_val; 1041 1042 static void build_validation(void) { 1043 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1044 if (!R.present[k]) continue; 1045 place_quiet(k); 1046 tap_ctx(1, k, 0); 1047 g_val.n[k] = grid(g_val.jobs[k], VAL_M, NA(VAL_M), VAL_N, NA(VAL_N), 5000u + k); 1048 for (uint32_t i = 0; i < g_val.n[k]; i++) { 1049 job_load(&g_val.jobs[k][i]); 1050 truth_table_tap(&g_val.jobs[k][i], g_val.table[k][i]); 1051 } 1052 } 1053 } 1054 1055 static void judge(const RxCostModel *m, double *learned, double *dispatch, double *reference, 1056 uint32_t *cov_n, uint32_t *cov_in80) { 1057 *learned = *dispatch = *reference = 0; 1058 for (uint32_t k = 0; k < RX_CM_CORES; k++) 1059 for (uint32_t i = 0; i < g_val.n[k]; i++) { 1060 if (i % 2u == 0) continue; /* even jobs are the calibration split */ 1061 const Job *j = &g_val.jobs[k][i]; 1062 const uint64_t *t = g_val.table[k][i]; 1063 RxCmDecision d; 1064 uint32_t arm = frozen_choice(m, &g_frozen_policy, j, k, 0, &d); 1065 *learned += (double)t[arm] * j->calls; 1066 *dispatch += (double)t[dispatch_rule(j->N)] * j->calls; 1067 *reference += (double)t[ARM_REF] * j->calls; 1068 if (cov_n) coverage(m, j, k, 0, t, cov_n, cov_in80, NULL); 1069 } 1070 } 1071 1072 /* Calibration split: the even validation jobs, handed to the learner. The 1073 * judge grades on the odd ones only. */ 1074 static double calibrate_learned(RxCostModel *m) { 1075 static RxCmFeatures cf[RX_CM_CORES * MAX_JOBS * N_ARMS]; 1076 static uint32_t ca[RX_CM_CORES * MAX_JOBS * N_ARMS]; 1077 static uint64_t cps[RX_CM_CORES * MAX_JOBS * N_ARMS]; 1078 uint32_t n = 0; 1079 for (uint32_t k = 0; k < RX_CM_CORES; k++) 1080 for (uint32_t i = 0; i < g_val.n[k]; i += 2) 1081 for (uint32_t a = 0; a < N_ARMS; a++) { 1082 if (!(g_eligible & (1u << a)) || g_val.table[k][i][a] == UINT64_MAX) continue; 1083 cf[n] = features(&g_val.jobs[k][i], k, 0, ARM_REF); 1084 ca[n] = a; 1085 cps[n] = g_val.table[k][i][a]; 1086 n++; 1087 } 1088 return rx_cm_calibrate(m, cf, ca, cps, n, 0.80); 1089 } 1090 1091 /* The judge accepts a candidate only if it beats the fixed rule by 5%, and 1092 * is no worse than the known-good fallback (always the reference) and no 1093 * worse than the model in force, if there is one. Beating a bad rule is not 1094 * enough: a model with its knowledge scrambled can still do that. */ 1095 static int judge_accepts(double learned, double dispatch, double reference, double incumbent) { 1096 return learned <= 0.95 * dispatch && learned <= reference && (incumbent <= 0 || learned <= incumbent); 1097 } 1098 1099 /* ---- promotion through the R9 generation barrier ---- */ 1100 1101 enum { ISSUER = 3, SUBJ_LEARNER = 71, SUBJ_PROMOTER = 72 }; 1102 1103 typedef struct { 1104 AienosCapAdmin *admin; 1105 AienosCapView *view; 1106 } Auth; 1107 1108 static int native_auth(void *ctx, uint32_t cap_id, uint64_t cap_generation, uint32_t subject, 1109 uint64_t resource, uint32_t rights) { 1110 Auth *a = ctx; 1111 AienosCapRef ref = { cap_id, cap_generation }; 1112 AienosCapEntry e; 1113 return aienos_cap_validate(a->view, ref, subject, resource, rights, &e); 1114 } 1115 1116 static AienosCapRef mint(Auth *a, uint32_t subject, uint32_t rights) { 1117 AienosCapRef office, out = { UINT32_MAX, 0 }; 1118 aienos_cap_office(a->admin, &office); 1119 AienosCapMint m = { ISSUER, subject, RX_GEN_RES_PROMOTION, rights, 0, { UINT32_MAX, 0 }, office }; 1120 if (aienos_cap_mint(a->admin, &m, &out) != 0) out = (AienosCapRef){ UINT32_MAX, 0 }; 1121 return out; 1122 } 1123 1124 static RxPromotionRequest request(uint64_t id, uint32_t subject, AienosCapRef cap) { 1125 RxPromotionRequest r; 1126 memset(&r, 0, sizeof r); 1127 r.candidate_id = id; 1128 r.subject = subject; 1129 r.cap_id = cap.cap_id; 1130 r.cap_generation = cap.generation; 1131 r.resource = RX_GEN_RES_PROMOTION; 1132 r.rights = RX_GEN_RIGHT_PROMOTE; 1133 return r; 1134 } 1135 1136 static uint8_t g_blob[65536], g_blob2[65536]; 1137 static char g_store_dir[512]; 1138 1139 static int propose(RxGenStore *st, const RxCostModel *m, int proofs_ok, const char *evidence, 1140 uint64_t *id) { 1141 size_t n = 0; 1142 if (rx_cm_serialize(m, g_blob, sizeof g_blob, &n) != RX_CM_OK) return -1; 1143 static const uint8_t config[] = "rx_costmodel v1; ei_min 0.08 log2; min_obs 12; unseen shift 1.0 log2; explore 5% + 0.5 ms"; 1144 static const uint8_t provenance[] = "OMEGA_EMPIRICAL_OPTIMIZER host run"; 1145 char real[256]; 1146 int rl = snprintf(real, sizeof real, "arms: reference scalar unroll2 unroll4_dual quad4 " 1147 "planted_wrong; eligible mask 0x%x", g_eligible); 1148 RxGenDraft d; 1149 memset(&d, 0, sizeof d); 1150 d.authority_epoch = 1; 1151 d.authority_generation = 1; 1152 d.proofs_ok = proofs_ok; 1153 d.evidence = (const uint8_t *)evidence; 1154 d.evidence_len = strlen(evidence); 1155 d.model = g_blob; 1156 d.model_len = n; 1157 d.realization = (const uint8_t *)real; 1158 d.realization_len = (size_t)rl; 1159 d.config = config; 1160 d.config_len = sizeof config - 1; 1161 d.provenance = provenance; 1162 d.provenance_len = sizeof provenance - 1; 1163 return rx_gen_propose(st, SUBJ_LEARNER, &d, id); 1164 } 1165 1166 /* Load the durable model, or report why not. */ 1167 static int load_durable(const char *dir, RxCostModel *m, uint64_t *active) { 1168 RxGenStore *st = NULL; 1169 if (rx_gen_open(dir, &st) != RX_GEN_OK) return RX_GEN_ERR_IO; 1170 uint64_t id = 0, lineage = 0; 1171 int rc = rx_gen_active(st, &id, &lineage); 1172 size_t n = 0; 1173 if (rc == RX_GEN_OK) rc = rx_gen_read_blob(st, id, "model", g_blob2, sizeof g_blob2, &n); 1174 rx_gen_close(st); 1175 if (rc != RX_GEN_OK) return rc; 1176 if (rx_cm_deserialize(m, g_blob2, n) != RX_CM_OK) return RX_GEN_ERR_TORN; 1177 if (active) *active = id; 1178 return RX_GEN_OK; 1179 } 1180 1181 static void poison(RxCostModel *p, const RxCostModel *m) { 1182 /* Swap what the model learned about quad4 and scalar in every cell. */ 1183 *p = *m; 1184 for (uint32_t c = 0; c < RX_CM_CELLS; c++) { 1185 RxCmArmStats t = p->cell[c][ARM_QUAD4]; 1186 p->cell[c][ARM_QUAD4] = p->cell[c][ARM_SCALAR]; 1187 p->cell[c][ARM_SCALAR] = t; 1188 } 1189 } 1190 1191 static int promote_phase(const RxCostModel *m, RxCostModel *durable) { 1192 Auth a; 1193 memset(&a, 0, sizeof a); 1194 CHECK(aienos_cap_start(&a.admin, &a.view) == 0, "native authority"); 1195 AienosCapRef learner_cap = mint(&a, SUBJ_LEARNER, RX_RIGHT_READ); 1196 AienosCapRef promoter_cap = mint(&a, SUBJ_PROMOTER, RX_GEN_RIGHT_PROMOTE); 1197 CHECK(learner_cap.cap_id != UINT32_MAX && promoter_cap.cap_id != UINT32_MAX, "mint"); 1198 1199 if (omega_evidence_path("EMPIRICAL/generations/.keep", g_store_dir, sizeof g_store_dir) == 0) { 1200 char *slash = strrchr(g_store_dir, '/'); 1201 if (slash) *slash = 0; 1202 } 1203 mkdir(g_store_dir, 0755); 1204 RxGenStore *st = NULL; 1205 CHECK(rx_gen_open(g_store_dir, &st) == RX_GEN_OK, "open generation store %s", g_store_dir); 1206 if (!st) return -1; 1207 uint64_t active0 = 0, lin0 = 0; 1208 rx_gen_active(st, &active0, &lin0); 1209 1210 /* Poisoned candidate: its maker claims proofs; the judge disagrees. */ 1211 RxCostModel bad; 1212 poison(&bad, m); 1213 double bl, bd, br; 1214 judge(&bad, &bl, &bd, &br, NULL, NULL); 1215 R.val_poison = bl; 1216 R.poison_judge_ok = judge_accepts(bl, bd, br, 0); 1217 CHECK(!R.poison_judge_ok, "judge rejects the poisoned model (%.3g; rule %.3g, reference %.3g)", bl, bd, br); 1218 uint64_t bad_id = 0; 1219 CHECK(propose(st, &bad, 0, "{\"validation\":\"lost to the fixed rule\"}", &bad_id) == RX_GEN_OK, 1220 "propose poisoned"); 1221 RxPromotionRequest rq = request(bad_id, SUBJ_PROMOTER, promoter_cap); 1222 R.poison_promote_rc = rx_gen_promote(st, &rq, native_auth, &a, NULL, NULL, NULL, NULL); 1223 CHECK(R.poison_promote_rc == RX_GEN_ERR_VERIFY, "poisoned candidate without proofs refused (%d)", 1224 R.poison_promote_rc); 1225 1226 char ev[512]; 1227 snprintf(ev, sizeof ev, 1228 "{\"validation_jobs\":%u,\"learned_ps\":%.0f,\"fixed_rule_ps\":%.0f," 1229 "\"mismatches\":%llu,\"coverage80\":%.3f}", 1230 R.val_jobs, R.val_learned, R.val_dispatch, (unsigned long long)K.mismatches, 1231 R.val_cov_n ? (double)R.val_cov_in80 / R.val_cov_n : 0.0); 1232 uint64_t id = 0; 1233 CHECK(propose(st, m, R.proofs_ok, ev, &id) == RX_GEN_OK, "propose candidate model"); 1234 1235 /* The learner may not promote: not with its own capability, and not 1236 * with the promoter's capability under its own name. */ 1237 rq = request(id, SUBJ_LEARNER, learner_cap); 1238 R.learner_promote_rc = rx_gen_promote(st, &rq, native_auth, &a, NULL, NULL, NULL, NULL); 1239 CHECK(R.learner_promote_rc == RX_GEN_ERR_AUTHORITY, "learner promotion refused (%d)", 1240 R.learner_promote_rc); 1241 rq = request(id, SUBJ_LEARNER, promoter_cap); 1242 R.learner_with_promoter_cap_rc = rx_gen_promote(st, &rq, native_auth, &a, NULL, NULL, NULL, NULL); 1243 CHECK(R.learner_with_promoter_cap_rc == RX_GEN_ERR_AUTHORITY, 1244 "learner presenting the promoter's capability refused (%d)", 1245 R.learner_with_promoter_cap_rc); 1246 uint64_t act = 0, lin = 0; 1247 CHECK(rx_gen_active(st, &act, &lin) == RX_GEN_OK && act == active0, "refusals left the active generation as it was"); 1248 1249 rq = request(id, SUBJ_PROMOTER, promoter_cap); 1250 R.promote_rc = rx_gen_promote(st, &rq, native_auth, &a, NULL, NULL, NULL, NULL); 1251 if (!R.proofs_ok) { 1252 /* The judge rejected the learned model on this machine. Where the best 1253 * static arm is the reference (a CI runner with one core class), the 1254 * learned model can at best tie it on validation and timing noise 1255 * decides. That is a no-win result, not a broken barrier: the barrier 1256 * must refuse the candidate and the generation in force stays. The 1257 * gate then reports no win (main). Recovery and the torn copy need a 1258 * promoted generation and are not run; the receipt records why. */ 1259 CHECK(R.promote_rc == RX_GEN_ERR_VERIFY, "barrier refuses the model the judge rejected (%d)", 1260 R.promote_rc); 1261 CHECK(rx_gen_active(st, &act, &lin) == RX_GEN_OK && act == active0, 1262 "the refused promotion left the active generation as it was"); 1263 rx_gen_close(st); 1264 aienos_cap_stop(a.admin, a.view); 1265 printf(" judge rejected the learned model (learned %.4f of always-reference): nothing promoted\n", 1266 R.val_reference > 0 ? R.val_learned / R.val_reference : 0.0); 1267 model_fresh(durable); 1268 return 0; 1269 } 1270 CHECK(R.promote_rc == RX_GEN_OK, "promoter promotes the judged model (%d)", R.promote_rc); 1271 rx_gen_close(st); 1272 aienos_cap_stop(a.admin, a.view); 1273 1274 /* Restart: recover, reopen, read the model back. */ 1275 RxRecoveryRecord rec; 1276 memset(&rec, 0, sizeof rec); 1277 CHECK(rx_gen_recover(g_store_dir, &rec) == RX_GEN_OK, "recover"); 1278 R.recover_coherent = rec.coherent && rec.active_id == id; 1279 CHECK(R.recover_coherent, "recovery selects the promoted generation"); 1280 uint64_t active = 0; 1281 CHECK(load_durable(g_store_dir, durable, &active) == RX_GEN_OK && active == id, 1282 "durable model read back"); 1283 R.active_id = active; 1284 uint8_t d1[32], d2[32]; 1285 rx_cm_digest(m, d1); 1286 rx_cm_digest(durable, d2); 1287 R.durable_digest_match = memcmp(d1, d2, 32) == 0; 1288 CHECK(R.durable_digest_match, "durable model is the judged model"); 1289 hex(d2, 32, R.model_digest); 1290 R.model_bytes = rx_cm_blob_size(); 1291 1292 /* A torn copy: the store refuses the blob and Omega runs the known-good 1293 * fallback instead of anything the torn model says. */ 1294 char torn[600], cmd[2048]; 1295 snprintf(torn, sizeof torn, "%s-torn", g_store_dir); 1296 snprintf(cmd, sizeof cmd, "rm -rf '%s' && cp -r '%s' '%s'", torn, g_store_dir, torn); 1297 CHECK(system(cmd) == 0, "copy store"); 1298 char mp[700]; 1299 snprintf(mp, sizeof mp, "%s/g/%llu/model", torn, (unsigned long long)id); 1300 FILE *fp = fopen(mp, "r+b"); 1301 CHECK(fp != NULL, "open torn model %s", mp); 1302 if (fp) { 1303 fseek(fp, 200, SEEK_SET); 1304 int c = fgetc(fp); 1305 fseek(fp, 200, SEEK_SET); 1306 fputc(c ^ 0x5a, fp); 1307 fclose(fp); 1308 } 1309 RxCostModel tm; 1310 R.torn_rc = load_durable(torn, &tm, NULL); 1311 CHECK(R.torn_rc == RX_GEN_ERR_TORN, "torn model refused (%d)", R.torn_rc); 1312 RxCostModel empty; 1313 model_fresh(&empty); 1314 Job j = { 64, 256, 1, 1, 0 }; 1315 RxCmDecision d; 1316 uint32_t arm = frozen_choice(&empty, &g_frozen_policy, &j, RX_CM_CORE_X925, 0, &d); 1317 R.torn_fallback_ok = d.action == RX_CM_FALLBACK && arm == ARM_REF; 1318 CHECK(R.torn_fallback_ok, "without a durable model, the known-good fallback runs"); 1319 snprintf(cmd, sizeof cmd, "rm -rf '%s'", torn); 1320 if (system(cmd) != 0) fprintf(stderr, " could not remove %s\n", torn); 1321 return 0; 1322 } 1323 1324 /* ---- coverage sample (default off; ORIG v2, aien-train#24/#25, benchmarks#45) ---- 1325 * OMEGA_EMPIRICAL_COVERAGE= with the record tap on: after the held-out 1326 * phase, n further log-uniform shapes per present core class (M, N in 1327 * [1, 8192], M N <= MAX_ELEMS, seed 9000 + core) are measured with the same 1328 * truth table and tapped as family 5. Observation only: no policy, judge, 1329 * learner or verdict of this harness sees them. Without the tap it does nothing; 1330 * n above 640 is capped, a negative or non-numeric n takes no sample. */ 1331 #define COVERAGE_MAX 640u 1332 1333 static uint32_t coverage_count(void) { 1334 const char *e = getenv("OMEGA_EMPIRICAL_COVERAGE"); 1335 if (g_tap_fd < 0 || !e || !e[0]) return 0; 1336 char *end; 1337 errno = 0; 1338 long v = strtol(e, &end, 10); 1339 if (errno != 0 || *end != '\0' || v < 0) { 1340 fprintf(stderr, " coverage sample: OMEGA_EMPIRICAL_COVERAGE=%s is not a count >= 0; no sample\n", e); 1341 return 0; 1342 } 1343 return v > (long)COVERAGE_MAX ? COVERAGE_MAX : (uint32_t)v; 1344 } 1345 1346 static void coverage_sample(uint32_t k, uint32_t count) { 1347 uint64_t s = 9000u + k; 1348 uint32_t n = 0; 1349 tap_ctx(5, k, 0); 1350 while (n < count) { 1351 uint32_t M = log_uniform(&s, 0.0, 13.0), N = log_uniform(&s, 0.0, 13.0); 1352 if (M < 1 || N < 1 || (uint64_t)M * N > MAX_ELEMS || M > MAX_M || N > MAX_N) continue; 1353 Job j = { M, N, calls_for(M, N), 9000u + k + (uint64_t)n * 2654435761u, 0 }; 1354 job_load(&j); 1355 uint64_t table[N_ARMS]; 1356 truth_table_tap(&j, table); 1357 n++; 1358 } 1359 } 1360 1361 /* ---- held-out ---- */ 1362 1363 static Job g_h1[MAX_JOBS], g_h2[MAX_JOBS]; 1364 static uint64_t g_h1_table[RX_CM_CORES][MAX_JOBS][N_ARMS]; 1365 static uint32_t g_choice[RX_CM_CORES][2][4][MAX_JOBS]; /* [dist H1/H2][policy] */ 1366 1367 static int cmp_d(const void *a, const void *b) { 1368 double x = *(const double *)a, y = *(const double *)b; 1369 return (x > y) - (x < y); 1370 } 1371 1372 static void heldout(const RxCostModel *durable, uint32_t k, uint32_t dist, Job *jobs, uint32_t n, 1373 RxCostModel *online, BudgetAudit *audit) { 1374 DistResult *D = &R.d[k][dist]; 1375 uint32_t pressure = dist == D_H3 ? 1u : 0u; 1376 LearnStats s; 1377 memset(&s, 0, sizeof s); 1378 uint32_t current = ARM_REF; 1379 double contention[MAX_JOBS]; 1380 uint32_t n_cont = 0; 1381 if (pressure) pressure_on(g_class[k].timed); 1382 tap_ctx(2 + dist, k, pressure); 1383 for (uint32_t i = 0; i < n; i++) { 1384 Job *j = &jobs[i]; 1385 job_load(j); 1386 uint64_t table[N_ARMS]; 1387 truth_table_tap(j, table); 1388 if (dist == D_H1) memcpy(g_h1_table[k][i], table, sizeof table); 1389 RxCmDecision d; 1390 uint32_t fz = frozen_choice(durable, &g_frozen_policy, j, k, pressure, &d); 1391 if (!(g_eligible & (1u << fz))) s.touched_ineligible++; 1392 uint32_t oracle = ARM_REF; 1393 for (uint32_t a = 0; a < N_ARMS; a++) 1394 if ((g_eligible & (1u << a)) && table[a] < table[oracle]) oracle = a; 1395 uint32_t choice[4] = { dispatch_rule(j->N), ARM_REF, R.best_static[k], fz }; 1396 for (uint32_t p = 0; p < 4; p++) D->cost[p] += (double)table[choice[p]] * j->calls; 1397 D->cost[P_ORACLE] += (double)table[oracle] * j->calls; 1398 D->cost[P_ONLINE] += learner_job(online, &g_online_policy, j, k, pressure, ¤t, table, &s, audit); 1399 if (dist < D_H3) 1400 for (uint32_t p = 0; p < 4; p++) g_choice[k][dist][p][i] = choice[p]; 1401 coverage(durable, j, k, pressure, table, &D->cov_n, &D->cov_in80, &D->cov_in95); 1402 if (dist == D_H3 && n_cont < MAX_JOBS) /* same job as H1 i: same shape, same data */ 1403 contention[n_cont++] = (double)table[oracle] / (double)g_h1_table[k][i][oracle]; 1404 } 1405 if (pressure) pressure_off(); 1406 D->jobs = n; 1407 D->measures = s.measures; 1408 D->fallbacks = s.fallbacks; 1409 D->explore_ps = s.explore_ps; 1410 R.touched_ineligible += s.touched_ineligible; 1411 if (audit_over(audit, online, &g_online_policy)) R.explore_budget_violations++; 1412 if (n_cont) { 1413 qsort(contention, n_cont, sizeof(double), cmp_d); 1414 D->contention_median = contention[n_cont / 2]; 1415 } 1416 } 1417 1418 /* Nanoseconds this thread has spent runnable but waiting for its cpu: time 1419 * another task held the core. */ 1420 static uint64_t run_delay_ns(void) { 1421 FILE *fp = fopen("/proc/thread-self/schedstat", "r"); 1422 unsigned long long run = 0, wait = 0; 1423 if (fp) { 1424 if (fscanf(fp, "%llu %llu", &run, &wait) != 2) wait = 0; 1425 fclose(fp); 1426 } 1427 return wait; 1428 } 1429 1430 /* Replay each policy's choices end to end, alternating blocks, with the 1431 * cluster meter. Policies: dispatch, reference, best static, learned frozen. */ 1432 static void replay(uint32_t k, uint32_t dist, Job *jobs, uint32_t n) { 1433 DistResult *D = &R.d[k][dist]; 1434 const char *label = g_class[k].meter; 1435 /* Correctness pass: every policy's choice on every job, real data. */ 1436 for (uint32_t p = 0; p < 4; p++) 1437 for (uint32_t i = 0; i < n; i++) { 1438 job_load(&jobs[i]); 1439 exec_arm(g_choice[k][dist][p][i], g_A, g_x, g_y, jobs[i].M, jobs[i].N); 1440 K.runs_checked++; 1441 if (digest_y(g_y, jobs[i].M) != jobs[i].ref_digest) K.mismatches++; 1442 } 1443 /* Timed and metered blocks. Values do not change the work, so the 1444 * buffers are left as they are and nothing but the realizations runs. */ 1445 uint64_t t0 = now_ns(); 1446 for (uint32_t i = 0; i < n; i++) 1447 for (uint32_t c = 0; c < jobs[i].calls; c++) 1448 exec_arm(g_choice[k][dist][0][i], g_A, g_x, g_y, jobs[i].M, jobs[i].N); 1449 uint64_t once = now_ns() - t0; 1450 uint32_t reps = (uint32_t)(600000000ull / (once ? once : 1)) + 1u; 1451 if (reps > 400) reps = 400; 1452 enum { ROUNDS = 7 }; 1453 double wall[4][ROUNDS], en[4][ROUNDS], wait[4][ROUNDS]; 1454 for (uint32_t round = 0; round < ROUNDS; round++) 1455 for (uint32_t q = 0; q < 4; q++) { 1456 uint32_t p = (q + round) % 4; 1457 uint64_t e0 = meter_uj(label), w0 = now_ns(), q0 = run_delay_ns(); 1458 for (uint32_t r = 0; r < reps; r++) 1459 for (uint32_t i = 0; i < n; i++) 1460 for (uint32_t c = 0; c < jobs[i].calls; c++) 1461 exec_arm(g_choice[k][dist][p][i], g_A, g_x, g_y, jobs[i].M, jobs[i].N); 1462 uint64_t w1 = now_ns(), e1 = meter_uj(label), q1 = run_delay_ns(); 1463 wall[p][round] = (double)(w1 - w0); 1464 wait[p][round] = (double)(q1 - q0); 1465 en[p][round] = e1 > e0 ? (double)(e1 - e0) : 0.0; 1466 if (getenv("EMPIRICAL_DEBUG")) 1467 fprintf(stderr, "replay core%u dist%u round%u policy%u reps %u wall %.1f ms waited %.2f ms energy %.0f uJ\n", k, 1468 dist, round, p, reps, wall[p][round] / 1e6, wait[p][round] / 1e6, en[p][round]); 1469 } 1470 /* The least disturbed block per policy: among blocks where the thread 1471 * waited for its core under 1% of the block, the fastest; if none, the 1472 * fastest of all. Energy is the meter reading of that same block. */ 1473 for (uint32_t p = 0; p < 4; p++) { 1474 int b = -1; 1475 for (uint32_t r = 0; r < ROUNDS; r++) 1476 if (wait[p][r] < 0.01 * wall[p][r] && (b < 0 || wall[p][r] < wall[p][b])) b = (int)r; 1477 if (b < 0) { 1478 b = 0; 1479 for (uint32_t r = 1; r < ROUNDS; r++) 1480 if (wall[p][r] < wall[p][b]) b = (int)r; 1481 D->contended_blocks_only = 1; 1482 } 1483 D->wall_ns[p] = wall[p][b]; 1484 D->energy_uj[p] = en[p][b]; 1485 D->wait_ns[p] = wait[p][b]; 1486 } 1487 D->replayed = 1; 1488 } 1489 1490 /* ---- receipt ---- */ 1491 1492 static void binary_digest(char out[65]) { 1493 strcpy(out, "unavailable"); 1494 FILE *fp = fopen("/proc/self/exe", "rb"); 1495 if (!fp) return; 1496 sha256_ctx c; 1497 sha256_init(&c); 1498 uint8_t buf[65536]; 1499 size_t n; 1500 while ((n = fread(buf, 1, sizeof buf, fp)) > 0) sha256_update(&c, buf, n); 1501 fclose(fp); 1502 uint8_t d[32]; 1503 sha256_final(&c, d); 1504 hex(d, 32, out); 1505 } 1506 1507 typedef struct { 1508 int win; 1509 char why[512]; 1510 } Verdict; 1511 1512 static Verdict judge_gate(void) { 1513 Verdict v = { 1, "" }; 1514 double frozen_all = 0, static_all = 0; 1515 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1516 if (!R.present[k]) continue; 1517 for (uint32_t d = 0; d < N_DIST; d++) { 1518 DistResult *D = &R.d[k][d]; 1519 frozen_all += D->cost[P_FROZEN]; 1520 static_all += D->cost[P_BEST_STATIC]; 1521 const char *fail = NULL; 1522 if (D->cost[P_FROZEN] > 0.95 * D->cost[P_DISPATCH]) fail = "frozen not 5% under the fixed rule"; 1523 else if (D->cost[P_ONLINE] > 0.95 * D->cost[P_DISPATCH]) fail = "online not 5% under the fixed rule"; 1524 else if (D->cost[P_FROZEN] > 1.03 * D->cost[P_BEST_STATIC]) fail = "frozen >3% over best static"; 1525 else if (D->replayed && D->wall_ns[3] >= D->wall_ns[0]) fail = "replay: frozen not faster than the fixed rule"; 1526 /* Coverage is gated where the model has seen the condition. Under H3 1527 * (never trained on) it rests on the unseen-shift prior; reported. */ 1528 else if (d != D_H3 && D->cov_n && (double)D->cov_in80 / D->cov_n < 0.6) 1529 fail = "80% intervals cover < 60%"; 1530 if (fail && v.win) { 1531 v.win = 0; 1532 snprintf(v.why, sizeof v.why, "%s, %s: %s", g_class[k].name, DIST_NAME[d], fail); 1533 } 1534 } 1535 } 1536 if (v.win && frozen_all >= static_all) { 1537 v.win = 0; 1538 snprintf(v.why, sizeof v.why, "frozen not under best static over all held-out work"); 1539 } 1540 return v; 1541 } 1542 1543 static void write_receipt(Verdict v, int allow_no_win) { 1544 char path[512]; 1545 if (omega_evidence_path("EMPIRICAL/rx_empirical_optimizer_receipt.json", path, sizeof path) != 0) 1546 return; 1547 FILE *fp = fopen(path, "w"); 1548 if (!fp) return; 1549 char commit[41]; 1550 memset(commit, 0, sizeof commit); 1551 if (!omega_evidence_run_commit(commit)) memcpy(commit, "unknown", 8); 1552 const char *candidate = getenv("OMEGA_CANDIDATE_COMMIT"); 1553 int bound = candidate && candidate[0] && strcmp(candidate, commit) == 0 && 1554 !omega_evidence_tree_dirty(); 1555 const char *aienos = getenv("AIENOS_COMMIT"); 1556 char digest[65]; 1557 binary_digest(digest); 1558 struct utsname u; 1559 memset(&u, 0, sizeof u); 1560 uname(&u); 1561 int core_ok = g_fail == 0 && K.mismatches == 0 && g_parent_runs[ARM_PLANTED] == 0 && 1562 R.touched_ineligible == 0 && R.explore_budget_violations == 0; 1563 const char *gate = !core_ok ? "FAIL" : v.win ? "PASS" : allow_no_win ? "CHAIN_PASS_NO_WIN" : "FAIL"; 1564 1565 fprintf(fp, "{\n \"schema\": \"OMEGA_EMPIRICAL_OPTIMIZER_V1\",\n"); 1566 fprintf(fp, " \"run_id\": \"%s\",\n", omega_evidence_run_id()); 1567 fprintf(fp, " \"candidate_commit\": %s%s%s,\n", candidate ? "\"" : "", 1568 candidate ? candidate : "null", candidate ? "\"" : ""); 1569 fprintf(fp, " \"candidate_bound\": %s,\n", bound ? "true" : "false"); 1570 fprintf(fp, " \"run_commit\": \"%s\",\n \"tree_dirty\": %s,\n", commit, 1571 omega_evidence_tree_dirty() ? "true" : "false"); 1572 fprintf(fp, " \"aienos_commit\": %s%s%s,\n", aienos ? "\"" : "", aienos ? aienos : "null", 1573 aienos ? "\"" : ""); 1574 fprintf(fp, " \"checks\": %d,\n \"failures\": %d,\n", g_checks, g_fail); 1575 fprintf(fp, " \"test_binary_sha256\": \"%s\",\n", digest); 1576 fprintf(fp, " \"host\": {\"sysname\": \"%s\", \"release\": \"%s\", \"machine\": \"%s\", " 1577 "\"cpus\": \"%s\", \"timed_cpus_by_phase\": \"%s\", \"loadavg_start\": \"%s\", \"loadavg_end\": \"%s\", " 1578 "\"thermal_c_start\": %u, \"thermal_c_end\": %u, \"shared_machine\": \"other sessions ran tests during this run; costs are floors (best of 5, fastest chunk, fastest replay block)\"},\n", 1579 u.sysname, u.release, u.machine, g_cpus_desc, g_timed_log, R.load_start, R.load_end, 1580 R.thermal_start, R.thermal_end); 1581 fprintf(fp, " \"arms\": ["); 1582 for (uint32_t a = 0; a < N_ARMS; a++) 1583 fprintf(fp, "%s\n {\"name\": \"%s\", \"verified\": %s, \"code_bytes\": %llu, " 1584 "\"synth_ns\": %llu, \"verify_ns\": %llu, \"parent_runs\": %llu}", 1585 a ? "," : "", ARM_NAME[a], g_verified[a] ? "true" : "false", 1586 (unsigned long long)g_meta.code_bytes[a], (unsigned long long)g_meta.synth_ns[a], 1587 (unsigned long long)g_meta.verify_ns[a], (unsigned long long)g_parent_runs[a]); 1588 fprintf(fp, "\n ],\n"); 1589 fprintf(fp, " \"correctness\": {\"runs_checked_against_reference\": %llu, \"mismatches\": %llu, " 1590 "\"refused_runs_of_unverified_arms\": %llu, \"ineligible_arm_chosen_or_measured\": %llu},\n", 1591 (unsigned long long)K.runs_checked, (unsigned long long)K.mismatches, 1592 (unsigned long long)g_refused_runs, (unsigned long long)R.touched_ineligible); 1593 fprintf(fp, " \"power_mw_above_idle\": {"); 1594 int first = 1; 1595 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1596 if (!R.present[k] || !g_class[k].meter || !g_meter_dir[0]) continue; 1597 fprintf(fp, "%s\"%s\": {\"idle_cluster_mw\": %.0f", first ? "" : ", ", g_class[k].name, 1598 g_idle_mw[k]); 1599 for (uint32_t a = 0; a < N_ARMS; a++) 1600 if (g_verified[a]) fprintf(fp, ", \"%s\": %.0f", ARM_NAME[a], g_meta.power_mw[k][a]); 1601 fprintf(fp, "}"); 1602 first = 0; 1603 } 1604 fprintf(fp, "},\n"); 1605 fprintf(fp, " \"training\": {"); 1606 first = 1; 1607 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1608 if (!R.present[k]) continue; 1609 fprintf(fp, "%s\"%s\": {\"jobs\": %u, \"jobs_measured_twice\": %u, \"best_static_arm\": \"%s\"}", 1610 first ? "" : ", ", g_class[k].name, R.train_jobs[k], R.train_measures[k], 1611 ARM_NAME[R.best_static[k]]); 1612 first = 0; 1613 } 1614 fprintf(fp, "},\n"); 1615 fprintf(fp, " \"validation\": {\"jobs\": %u, \"learned_ps\": %.0f, \"fixed_rule_ps\": %.0f, " 1616 "\"always_reference_ps\": %.0f, \"poisoned_model_ps\": %.0f, \"coverage80\": %.3f, \"calibration_sd_scale\": %.3f, \"proofs_ok\": %s},\n", 1617 R.val_jobs, R.val_learned, R.val_dispatch, R.val_reference, R.val_poison, 1618 R.val_cov_n ? (double)R.val_cov_in80 / R.val_cov_n : 0.0, R.sd_scale, R.proofs_ok ? "true" : "false"); 1619 fprintf(fp, " \"promotion\": {\"model_bytes\": %zu, \"model_sha256\": \"%s\", \"active_generation\": %llu, " 1620 "\"learner_promote_rc\": %d, \"learner_with_promoter_cap_rc\": %d, " 1621 "\"poisoned_judged_better\": %s, \"poisoned_promote_rc\": %d, \"promote_rc\": %d, " 1622 "\"recovery_selects_promoted\": %s, \"durable_equals_judged\": %s, " 1623 "\"torn_blob_rc\": %d, \"torn_falls_back_to_reference\": %s, " 1624 "\"online_updates_not_durable\": %s},\n", 1625 R.model_bytes, R.model_digest, (unsigned long long)R.active_id, R.learner_promote_rc, 1626 R.learner_with_promoter_cap_rc, R.poison_judge_ok ? "true" : "false", 1627 R.poison_promote_rc, R.promote_rc, R.recover_coherent ? "true" : "false", 1628 R.durable_digest_match ? "true" : "false", R.torn_rc, 1629 R.torn_fallback_ok ? "true" : "false", R.online_not_durable ? "true" : "false"); 1630 fprintf(fp, " \"exploration\": {\"online_explore_frac\": %.2f, \"budget_violations\": %llu},\n", 1631 g_online_policy.explore_frac, (unsigned long long)R.explore_budget_violations); 1632 fprintf(fp, " \"heldout\": {"); 1633 first = 1; 1634 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1635 if (!R.present[k]) continue; 1636 fprintf(fp, "%s\n \"%s\": {", first ? "" : ",", g_class[k].name); 1637 first = 0; 1638 for (uint32_t d = 0; d < N_DIST; d++) { 1639 DistResult *D = &R.d[k][d]; 1640 fprintf(fp, "%s\n \"%s\": {\"jobs\": %u, \"cost_ps\": {", d ? "," : "", DIST_NAME[d], 1641 D->jobs); 1642 for (uint32_t p = 0; p < N_POL; p++) 1643 fprintf(fp, "%s\"%s\": %.0f", p ? ", " : "", POL_NAME[p], D->cost[p]); 1644 fprintf(fp, "},\n \"frozen_vs_fixed_rule\": %.3f, \"online_vs_fixed_rule\": %.3f, " 1645 "\"frozen_vs_best_static\": %.3f, \"frozen_vs_oracle\": %.3f,\n" 1646 " \"online_measure_jobs\": %llu, \"online_explore_ps\": %llu, " 1647 "\"coverage80\": %.3f, \"coverage95\": %.3f", 1648 D->cost[P_FROZEN] / D->cost[P_DISPATCH], D->cost[P_ONLINE] / D->cost[P_DISPATCH], 1649 D->cost[P_FROZEN] / D->cost[P_BEST_STATIC], D->cost[P_FROZEN] / D->cost[P_ORACLE], 1650 (unsigned long long)D->measures, (unsigned long long)D->explore_ps, 1651 D->cov_n ? (double)D->cov_in80 / D->cov_n : 0.0, 1652 D->cov_n ? (double)D->cov_in95 / D->cov_n : 0.0); 1653 if (d == D_H3) fprintf(fp, ", \"contention_median\": %.3f", D->contention_median); 1654 if (D->replayed) 1655 fprintf(fp, ",\n \"replay\": {\"wall_ns\": {\"fixed_rule\": %.0f, \"reference\": %.0f, " 1656 "\"best_static\": %.0f, \"learned_frozen\": %.0f}, \"cluster_energy_uj\": " 1657 "{\"fixed_rule\": %.0f, \"reference\": %.0f, \"best_static\": %.0f, " 1658 "\"learned_frozen\": %.0f}, \"waited_for_core_ns\": {\"fixed_rule\": %.0f, \"reference\": %.0f, " 1659 "\"best_static\": %.0f, \"learned_frozen\": %.0f}, \"only_contended_blocks\": %s}", 1660 D->wall_ns[0], D->wall_ns[1], D->wall_ns[2], D->wall_ns[3], D->energy_uj[0], 1661 D->energy_uj[1], D->energy_uj[2], D->energy_uj[3], D->wait_ns[0], D->wait_ns[1], 1662 D->wait_ns[2], D->wait_ns[3], D->contended_blocks_only ? "true" : "false"); 1663 fprintf(fp, "}"); 1664 } 1665 fprintf(fp, "\n }"); 1666 } 1667 fprintf(fp, "\n },\n"); 1668 fprintf(fp, 1669 " \"measurability\": {\n" 1670 " \"features_used_in_fit\": [\"input shape (M, N)\", \"state size / memory locality (working set over the core's L2)\", " 1671 "\"hardware state (core class)\", \"resource pressure (co-runner bucket)\"],\n" 1672 " \"features_recorded_not_fit\": [\"thermal state (acpitz, degrees C)\", \"current realization\", " 1673 "\"evidence requirement (always: verified arms only)\", \"latency budget\", \"energy budget\"],\n" 1674 " \"features_constant_here\": [\"semantic operation class (matvec only)\", \"branch count (0)\", " 1675 "\"cognitive requirement (none for matvec)\"],\n" 1676 " \"targets_measured\": [\"latency (ps/call)\", \"energy (cluster meter, per arm power and per replay block)\", " 1677 "\"memory (code bytes + working set)\", \"throughput (derived: elements / latency)\", " 1678 "\"failure probability (verification refusals and wrong results)\", \"verification cost (ns)\", " 1679 "\"recompute cost (synthesis + verification ns)\", \"contention (H3 / H1 latency, same shape and arm)\", " 1680 "\"quality (exact: digest equal to the reference)\"],\n" 1681 " \"targets_not_measured\": [\"transfer cost (one shared-memory CPU; no device transfer in this operation)\"]\n" 1682 " },\n"); 1683 fprintf(fp, " \"gates\": {\n \"OMEGA_EMPIRICAL_OPTIMIZER_PASS\": \"%s\",\n", gate); 1684 fprintf(fp, " \"win_reason\": \"%s\",\n", v.win ? "all held-out criteria met" : v.why); 1685 fprintf(fp, 1686 " \"criteria\": \"per core class and held-out distribution: learned (frozen and online, " 1687 "online including its exploration) at least 5%% under the fixed rule; frozen within 3%% of the " 1688 "best static arm and under it over all held-out work; replay wall time under the fixed rule; " 1689 "80%% intervals cover at least 60%% on H1 and H2 (H3 is a condition absent from training: " 1690 "reported, not gated); zero mismatches; planted arm never run\",\n"); 1691 fprintf(fp, 1692 " \"not_claimed\": [\"integration into the resident omega.select reaction (the R10 " 1693 "faculty still selects by its own measurement)\", \"operations other than integer matvec\", " 1694 "\"graphics-processor realizations\", \"thermal or cognitive features varied in a controlled way\", " 1695 "\"energy per call (the meter updates every ~0.1 s; energy is per arm window and per replay block)\", " 1696 "\"transfer cost\", \"AIENOS kernel (the authority runs as a host library)\"]\n }\n}\n"); 1697 fclose(fp); 1698 printf("receipt: %s\n", path); 1699 } 1700 1701 int main(void) { 1702 signal(SIGPIPE, SIG_IGN); 1703 setvbuf(stdout, NULL, _IOLBF, 0); 1704 discover(); 1705 find_meter(); 1706 tap_open(); 1707 for (uint32_t k = 0; k < RX_CM_CORES; k++) R.present[k] = g_class[k].n > 0; 1708 printf("[*] cores: %s; energy meter: %s\n", g_cpus_desc, g_meter_dir[0] ? g_meter_dir : "none"); 1709 R.thermal_start = thermal_c(); 1710 loadavg(R.load_start, sizeof R.load_start); 1711 g_A = calloc(MAX_ELEMS + GUARD, 8); 1712 g_x = calloc(MAX_N + GUARD, 8); 1713 g_y = calloc(MAX_M + GUARD, 8); 1714 if (!g_A || !g_x || !g_y) return 2; 1715 1716 rx_cm_default_policy(&g_online_policy); 1717 g_frozen_policy = g_online_policy; 1718 g_train_policy = g_online_policy; 1719 g_train_policy.explore_frac = 1.0; /* training is the exploration phase */ 1720 g_train_policy.explore_allow_ps = 200000000000ull; 1721 g_online_policy.explore_frac = 0.05; /* held-out: 5% of chosen work */ 1722 g_online_policy.explore_allow_ps = 500000000ull; /* plus 0.5 ms to get started */ 1723 budget_audit_selftest(&g_online_policy); 1724 1725 printf("[*] arms: synthesize, verify in a forked child\n"); 1726 setup_arms(); 1727 printf("[*] power per arm from the cluster meter\n"); 1728 measure_power(); 1729 1730 printf("[*] training: the learner runs and measures on its own\n"); 1731 RxCostModel learned; 1732 model_fresh(&learned); 1733 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1734 if (!R.present[k]) continue; 1735 place_quiet(k); 1736 train_class(&learned, k); 1737 } 1738 1739 printf("[*] validation: an independent judge measures its own work\n"); 1740 build_validation(); 1741 for (uint32_t k = 0; k < RX_CM_CORES; k++) R.val_jobs += g_val.n[k] / 2u; 1742 R.sd_scale = calibrate_learned(&learned); 1743 printf(" calibration on the other half: spread x%.2f\n", R.sd_scale); 1744 judge(&learned, &R.val_learned, &R.val_dispatch, &R.val_reference, &R.val_cov_n, &R.val_cov_in80); 1745 R.proofs_ok = judge_accepts(R.val_learned, R.val_dispatch, R.val_reference, 0) && K.mismatches == 0; 1746 printf(" learned %.3f of the fixed rule's time on validation; 80%% intervals cover %.3f\n", 1747 R.val_learned / R.val_dispatch, R.val_cov_n ? (double)R.val_cov_in80 / R.val_cov_n : 0.0); 1748 1749 printf("[*] promotion through the generation barrier\n"); 1750 RxCostModel durable; 1751 memset(&durable, 0, sizeof durable); 1752 promote_phase(&learned, &durable); 1753 1754 printf("[*] held-out work with the durable model\n"); 1755 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1756 if (!R.present[k]) continue; 1757 place_quiet(k); 1758 RxCostModel online = durable; 1759 online.explore_ps = online.work_ps = 0; 1760 BudgetAudit audit = { 0, 0 }; /* runs with the online counters, across H1, H2, H3 */ 1761 uint32_t n1 = unseen_shapes(g_h1, 60, 7000u + k); 1762 uint32_t n2 = thin_mix(g_h2, 60, 8000u + k); 1763 heldout(&durable, k, D_H1, g_h1, n1, &online, &audit); 1764 place_quiet(k); 1765 heldout(&durable, k, D_H2, g_h2, n2, &online, &audit); 1766 place_quiet(k); 1767 heldout(&durable, k, D_H3, g_h1, n1, &online, &audit); 1768 if (g_class[k].n) { 1769 place_quiet(k); 1770 replay(k, D_H1, g_h1, n1); 1771 place_quiet(k); 1772 replay(k, D_H2, g_h2, n2); 1773 } 1774 for (uint32_t d = 0; d < N_DIST; d++) { 1775 DistResult *D = &R.d[k][d]; 1776 printf(" %s %-19s frozen %.3f / online %.3f of the fixed rule; frozen %.3f of best " 1777 "static (%s), %.3f of oracle; online measured %llu jobs; cover80 %.2f\n", 1778 g_class[k].name, DIST_NAME[d], D->cost[P_FROZEN] / D->cost[P_DISPATCH], 1779 D->cost[P_ONLINE] / D->cost[P_DISPATCH], D->cost[P_FROZEN] / D->cost[P_BEST_STATIC], 1780 ARM_NAME[R.best_static[k]], D->cost[P_FROZEN] / D->cost[P_ORACLE], 1781 (unsigned long long)D->measures, D->cov_n ? (double)D->cov_in80 / D->cov_n : 0.0); 1782 if (D->replayed) 1783 printf(" replay wall: frozen %.3f of the fixed rule, energy %.3f\n", 1784 D->wall_ns[3] / D->wall_ns[0], 1785 D->energy_uj[0] > 0 ? D->energy_uj[3] / D->energy_uj[0] : 0.0); 1786 } 1787 uint8_t a[32], b[32]; 1788 rx_cm_digest(&online, a); 1789 if (!R.proofs_ok) continue; /* nothing was promoted: no durable model to reload */ 1790 RxCostModel again; 1791 CHECK(load_durable(g_store_dir, &again, NULL) == RX_GEN_OK, "reload durable"); 1792 rx_cm_digest(&again, b); 1793 int changed = memcmp(a, b, 32) != 0; 1794 uint8_t c[32]; 1795 rx_cm_digest(&durable, c); 1796 R.online_not_durable = changed && memcmp(b, c, 32) == 0; 1797 CHECK(R.online_not_durable, "online learning changed only the working copy"); 1798 } 1799 uint32_t cover = coverage_count(); 1800 if (cover) { 1801 printf("[*] coverage sample: %u shapes per core class, tapped as family 5\n", cover); 1802 for (uint32_t k = 0; k < RX_CM_CORES; k++) { 1803 if (!R.present[k]) continue; 1804 place_quiet(k); 1805 coverage_sample(k, cover); 1806 } 1807 } 1808 R.thermal_end = thermal_c(); 1809 loadavg(R.load_end, sizeof R.load_end); 1810 1811 CHECK(K.mismatches == 0, "%llu runs disagreed with the reference", (unsigned long long)K.mismatches); 1812 CHECK(g_parent_runs[ARM_PLANTED] == 0, "planted arm ran %llu times", 1813 (unsigned long long)g_parent_runs[ARM_PLANTED]); 1814 CHECK(R.touched_ineligible == 0, "an ineligible arm was chosen or measured"); 1815 CHECK(R.explore_budget_violations == 0, "exploration exceeded its budget"); 1816 Verdict v = judge_gate(); 1817 if (!R.proofs_ok) { 1818 v.win = 0; 1819 snprintf(v.why, sizeof v.why, "the judge rejected the learned model on validation; nothing promoted"); 1820 } 1821 const char *env = getenv("EMPIRICAL_ALLOW_NO_WIN"); 1822 int allow = env && strcmp(env, "1") == 0; 1823 printf(" checked %llu runs against the reference: %llu mismatches\n", 1824 (unsigned long long)K.runs_checked, (unsigned long long)K.mismatches); 1825 printf(" held-out verdict: %s%s%s\n", v.win ? "learned selection wins" : "no win: ", 1826 v.win ? "" : v.why, ""); 1827 printf("checks %d failures %d\n", g_checks, g_fail); 1828 write_receipt(v, allow); 1829 if (g_fail) return 1; 1830 return v.win || allow ? 0 : 1; 1831 }