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
omega-mv / tests / algebra / bench_select.c
| 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 | /* MA-2 selector run: reads MA-2 bench receipts, decides every measured cell |
| 18 | * in both pack modes (per run and over all runs), checks that the per-run |
| 19 | * choices agree or are recorded ties, and writes the decision receipt. |
| 20 | * STAND-IN selector, not wired to rx_costmodel. |
| 21 | * Usage: bench_select OUT.json RUN1.json [RUN2.json ...] */ |
| 22 | #include "algebra/oma_select.h" |
| 23 | #include "algebra/realize_common.h" |
| 24 | #include "sha256.h" |
| 25 | |
| 26 | #include <math.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | static oma_sel_table g_t; |
| 32 | |
| 33 | static void file_sha256(const char *path, char hex[65]) { |
| 34 | FILE *f = fopen(path, "rb"); |
| 35 | sha256_ctx c; |
| 36 | uint8_t d[32], buf[8192]; |
| 37 | size_t k; |
| 38 | sha256_init(&c); |
| 39 | if (f) { |
| 40 | while ((k = fread(buf, 1, sizeof buf, f)) > 0) sha256_update(&c, buf, k); |
| 41 | fclose(f); |
| 42 | } |
| 43 | sha256_final(&c, d); |
| 44 | for (int i = 0; i < 32; i++) snprintf(hex + 2 * i, 3, "%02x", d[i]); |
| 45 | } |
| 46 | |
| 47 | typedef struct { size_t n, m; double sp; } cell; |
| 48 | |
| 49 | static int is_binary(const char *id) { return strncmp(id, "R1_", 3) == 0 && strcmp(id, "R1_plain") != 0; } |
| 50 | static int is_ternary(const char *id) { |
| 51 | return strncmp(id, "R2", 2) == 0 || strncmp(id, "R3", 2) == 0 || strncmp(id, "R5", 2) == 0; |
| 52 | } |
| 53 | |
| 54 | int main(int argc, char **argv) { |
| 55 | if (argc < 3) { fprintf(stderr, "usage: %s OUT.json RUN1.json [RUN2.json ...]\n", argv[0]); return 2; } |
| 56 | oma_sel_init(&g_t); |
| 57 | for (int i = 2; i < argc; i++) { |
| 58 | int rc = oma_sel_load(&g_t, argv[i]); |
| 59 | if (rc) { fprintf(stderr, "load %s: rc %d\n", argv[i], rc); return 2; } |
| 60 | } |
| 61 | cell cells[256]; |
| 62 | size_t nc = 0; |
| 63 | for (size_t i = 0; i < g_t.nrows; i++) { |
| 64 | const oma_sel_row *r = &g_t.rows[i]; |
| 65 | size_t k; |
| 66 | for (k = 0; k < nc; k++) |
| 67 | if (cells[k].n == r->n && cells[k].m == r->m && fabs(cells[k].sp - r->sparsity) < 1e-9) break; |
| 68 | if (k == nc && nc < 256) cells[nc++] = (cell){r->n, r->m, r->sparsity}; |
| 69 | } |
| 70 | FILE *o = fopen(argv[1], "w"); |
| 71 | if (!o) { perror(argv[1]); return 2; } |
| 72 | fprintf(o, "{\n \"schema\": \"OMEGA_MIXED_ALGEBRA_MA2_SELECT_V1\",\n"); |
| 73 | fprintf(o, " \"label\": \"stand-in selector, not wired to rx_costmodel\",\n"); |
| 74 | fprintf(o, " \"operation\": \"Omega-X: y = W.x, W ternary m x n, x int8, y int32, exact\",\n"); |
| 75 | fprintf(o, " \"policy\": \"exact-contract filter (registry exact, verified in every run used, n <= max_n); cost = mean over runs of median ns per call (+ median pack ns when packing per call); noise band = max over winner and runner-up of within-run (q75-q25)/median and across-run (max-min)/mean; tied set = every eligible candidate within the band of the cheapest; TIE when the tied set has more than one member; a TIE is resolved only inside the tied set: incumbent if in it, else the digital reference realization (" OMA_SEL_REFERENCE ") if in it, else the cheapest member (ADR 0019 section 9.1, amended); these decisions have no incumbent\",\n"); |
| 76 | fprintf(o, " \"sources\": ["); |
| 77 | for (int r = 0; r < g_t.nruns; r++) { |
| 78 | char hex[65]; |
| 79 | file_sha256(g_t.source[r], hex); |
| 80 | fprintf(o, "%s{\"path\": \"%s\", \"run_id\": \"%s\", \"sha256\": \"%s\"}", r ? ", " : "", g_t.source[r], |
| 81 | g_t.run_id[r], hex); |
| 82 | } |
| 83 | fprintf(o, "],\n"); |
| 84 | |
| 85 | /* reproducibility: per-run decisions */ |
| 86 | unsigned agree = 0, tie_ok = 0, disagree = 0, total = 0; |
| 87 | fprintf(o, " \"reproducibility\": {\"runs\": %d, \"cells\": [\n", g_t.nruns); |
| 88 | int first = 1; |
| 89 | if (g_t.nruns >= 2) { |
| 90 | for (size_t k = 0; k < nc; k++) |
| 91 | for (int mode = 0; mode < 2; mode++) { |
| 92 | oma_sel_decision a, b; |
| 93 | oma_sel_query qa = {cells[k].n, cells[k].m, cells[k].sp, mode, 0, NULL}; |
| 94 | oma_sel_query qb = qa; |
| 95 | qb.run = 1; |
| 96 | if (oma_sel_decide(&g_t, &qa, &a) || oma_sel_decide(&g_t, &qb, &b)) continue; |
| 97 | const char *st; |
| 98 | total++; |
| 99 | if (strcmp(a.chosen, b.chosen) == 0) { agree++; st = "agree"; } |
| 100 | else if ((a.tie && oma_sel_in_tie_set(&a, b.chosen)) || (b.tie && oma_sel_in_tie_set(&b, a.chosen))) { |
| 101 | tie_ok++; |
| 102 | st = "recorded_tie"; |
| 103 | } else { disagree++; st = "DISAGREE"; } |
| 104 | fprintf(o, "%s {\"n\": %zu, \"m\": %zu, \"sparsity\": %.2f, \"pack\": \"%s\", \"run1\": \"%s\", " |
| 105 | "\"run1_verdict\": \"%s\", \"run1_margin\": %.4f, \"run1_band\": %.4f, \"run2\": \"%s\", " |
| 106 | "\"run2_verdict\": \"%s\", \"run2_margin\": %.4f, \"run2_band\": %.4f, \"status\": \"%s\"}", |
| 107 | first ? "" : ",\n", cells[k].n, cells[k].m, cells[k].sp, mode ? "per_call" : "once_amortized", |
| 108 | a.chosen, a.tie ? "TIE" : "CHOSEN", a.margin_rel, a.noise_band_rel, b.chosen, |
| 109 | b.tie ? "TIE" : "CHOSEN", b.margin_rel, b.noise_band_rel, st); |
| 110 | first = 0; |
| 111 | if (strcmp(st, "DISAGREE") == 0) |
| 112 | printf("DISAGREE n=%zu m=%zu sp=%.1f %s: run1 %s (margin %.3f band %.3f) run2 %s (margin %.3f band %.3f)\n", |
| 113 | cells[k].n, cells[k].m, cells[k].sp, mode ? "per_call" : "amortized", a.chosen, |
| 114 | a.margin_rel, a.noise_band_rel, b.chosen, b.margin_rel, b.noise_band_rel); |
| 115 | } |
| 116 | } |
| 117 | fprintf(o, "\n ], \"decisions\": %u, \"agree\": %u, \"recorded_ties\": %u, \"disagree\": %u},\n", total, agree, |
| 118 | tie_ok, disagree); |
| 119 | |
| 120 | unsigned ties_total = 0, ties_per_call = 0, ties_once = 0, ndec = 0, ref_picks = 0; |
| 121 | unsigned res_inc = 0, res_ref = 0, res_cheap = 0, outside = 0; |
| 122 | /* decisions over all runs + win/lose table */ |
| 123 | fprintf(o, " \"decisions\": [\n"); |
| 124 | first = 1; |
| 125 | printf("%-6s %-5s %-4s | %-12s %-6s | %-12s %-6s | %-10s %-6s | %s\n", "n", "m", "sp", "amortized", "", "per_call", |
| 126 | "", "R1 best", "%floor", "ternary best / binary best (amortized)"); |
| 127 | fprintf(stdout, "-----------------------------------------------------------------------------------------------\n"); |
| 128 | char winlose[256][512]; |
| 129 | for (size_t k = 0; k < nc; k++) { |
| 130 | oma_sel_decision d[2]; |
| 131 | for (int mode = 0; mode < 2; mode++) { |
| 132 | oma_sel_query q = {cells[k].n, cells[k].m, cells[k].sp, mode, -1, NULL}; |
| 133 | if (oma_sel_decide(&g_t, &q, &d[mode])) continue; |
| 134 | ndec++; |
| 135 | if (d[mode].tie) { ties_total++; if (mode) ties_per_call++; else ties_once++; } |
| 136 | if (strcmp(d[mode].chosen, OMA_SEL_REFERENCE) == 0) ref_picks++; |
| 137 | if (d[mode].tie) { |
| 138 | const char *tr = d[mode].tie_resolution; |
| 139 | if (strcmp(tr, "incumbent") == 0) res_inc++; |
| 140 | else if (strcmp(tr, "reference") == 0) res_ref++; |
| 141 | else if (strcmp(tr, "cheapest") == 0) res_cheap++; |
| 142 | } |
| 143 | int in = 0; |
| 144 | for (size_t i = 0; i < d[mode].ntie; i++) in |= strcmp(d[mode].tie_set[i], d[mode].chosen) == 0; |
| 145 | if (!in) outside++; |
| 146 | fprintf(o, "%s", first ? "" : ",\n"); |
| 147 | oma_sel_decision_json(o, &d[mode], " "); |
| 148 | first = 0; |
| 149 | } |
| 150 | /* binary vs ternary families (amortized) */ |
| 151 | const oma_sel_candidate *bb = NULL, *tb = NULL; |
| 152 | for (size_t c = 0; c < d[0].ncand; c++) { |
| 153 | const oma_sel_candidate *x = &d[0].cand[c]; |
| 154 | if (!x->eligible) continue; |
| 155 | if (is_binary(x->rz) && (!bb || x->cost_ns < bb->cost_ns)) bb = x; |
| 156 | if (is_ternary(x->rz) && (!tb || x->cost_ns < tb->cost_ns)) tb = x; |
| 157 | } |
| 158 | double ratio = (bb && tb) ? bb->cost_ns / tb->cost_ns : 0; |
| 159 | double band = 0; |
| 160 | if (bb && tb) { |
| 161 | band = fmax(fmax(bb->within_noise_rel, tb->within_noise_rel), fmax(bb->cross_run_rel, tb->cross_run_rel)); |
| 162 | } |
| 163 | const char *verdict = !(bb && tb) ? "n/a" |
| 164 | : fabs(ratio - 1.0) <= band ? "tie" |
| 165 | : ratio > 1.0 ? "ternary wins" : "binary wins"; |
| 166 | snprintf(winlose[k], sizeof winlose[k], |
| 167 | "{\"n\": %zu, \"m\": %zu, \"sparsity\": %.2f, \"best_binary\": \"%s\", \"binary_ns\": %.1f, " |
| 168 | "\"binary_pct_floor\": %.1f, \"best_ternary\": \"%s\", \"ternary_ns\": %.1f, \"ternary_pct_floor\": %.1f, " |
| 169 | "\"binary_over_ternary\": %.3f, \"band\": %.4f, \"verdict\": \"%s\"}", |
| 170 | cells[k].n, cells[k].m, cells[k].sp, bb ? bb->rz : "-", bb ? bb->cost_ns : 0, bb ? bb->pct_floor : 0, |
| 171 | tb ? tb->rz : "-", tb ? tb->cost_ns : 0, tb ? tb->pct_floor : 0, ratio, band, verdict); |
| 172 | printf("%-6zu %-5zu %-4.1f | %-12s %-6s | %-12s %-6s | %-10s %5.0f%% | %.2fx %s (%s)\n", cells[k].n, cells[k].m, |
| 173 | cells[k].sp, d[0].chosen, d[0].tie ? "TIE" : "", d[1].chosen, d[1].tie ? "TIE" : "", bb ? bb->rz : "-", |
| 174 | bb ? bb->pct_floor : 0, ratio, verdict, tb ? tb->rz : "-"); |
| 175 | } |
| 176 | fprintf(o, "\n ],\n \"decision_summary\": {\"decisions\": %u, \"ties\": %u, \"ties_per_call\": %u, " |
| 177 | "\"ties_once_amortized\": %u, \"chosen_reference\": %u, \"tie_resolution\": {\"incumbent\": %u, " |
| 178 | "\"reference\": %u, \"cheapest\": %u}, \"chosen_outside_tied_set\": %u},\n", |
| 179 | ndec, ties_total, ties_per_call, ties_once, ref_picks, res_inc, res_ref, res_cheap, outside); |
| 180 | fprintf(o, " \"binary_vs_ternary\": [\n"); |
| 181 | for (size_t k = 0; k < nc; k++) fprintf(o, " %s%s\n", winlose[k], k + 1 < nc ? "," : ""); |
| 182 | fprintf(o, " ],\n"); |
| 183 | |
| 184 | /* off-grid queries: nearest measured cell, contract filter by n */ |
| 185 | static const oma_sel_query extra[] = { |
| 186 | {2048, 256, 0.5, 0, -1, NULL}, {16384, 2048, 0.0, 1, -1, NULL}, {100000, 64, 0.0, 0, -1, NULL}, {1024, 1, 0.95, 0, -1, NULL}}; |
| 187 | fprintf(o, " \"off_grid_queries\": [\n"); |
| 188 | for (size_t i = 0; i < sizeof extra / sizeof extra[0]; i++) { |
| 189 | oma_sel_decision d; |
| 190 | if (oma_sel_decide(&g_t, &extra[i], &d)) continue; |
| 191 | oma_sel_decision_json(o, &d, " "); |
| 192 | fprintf(o, "%s\n", i + 1 < sizeof extra / sizeof extra[0] ? "," : ""); |
| 193 | } |
| 194 | fprintf(o, " ],\n"); |
| 195 | const char *gate = g_t.nruns < 2 ? "NOT_RUN" : (disagree == 0 && outside == 0) ? "PASS" : "FAIL"; |
| 196 | fprintf(o, " \"gates\": {\"MA2_SELECTOR_REPRO\": \"%s\", \"criteria\": \"for every measured cell and both pack modes, the choices from run 1 alone and run 2 alone are equal, or one run records a TIE whose tied set contains the other run's choice; and every decision's chosen realization is a member of its tied set\"},\n", gate); |
| 197 | fprintf(o, " \"not_claimed\": [\"integration with rx_costmodel or the resident omega.select reaction\", \"shapes between grid points beyond nearest-cell lookup\", \"multi-core or GPU realizations\", \"energy-based selection\"]\n}\n"); |
| 198 | fclose(o); |
| 199 | printf("decisions over all runs: %u, TIE %u (per_call %u, once_amortized %u), %s chosen %u\n", ndec, ties_total, |
| 200 | ties_per_call, ties_once, OMA_SEL_REFERENCE, ref_picks); |
| 201 | printf("tie resolution: incumbent %u, reference %u, cheapest %u; chosen outside tied set: %u\n", res_inc, res_ref, |
| 202 | res_cheap, outside); |
| 203 | printf("reproducibility: %u decisions, %u agree, %u recorded ties, %u disagree -> MA2_SELECTOR_REPRO %s\n", total, |
| 204 | agree, tie_ok, disagree, gate); |
| 205 | return 0; |
| 206 | } |