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 / src / algebra / oma_select.h
| 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 for Omega-X realizations: RETIRED for new decisions. |
| 18 | * spec/mixed-algebra-ma2.md |
| 19 | * |
| 20 | * RETIRED (TURING docs/turing/TURING_W0_PROPOSAL.md K.6/K.7; ADR 0019 section |
| 21 | * 9.1 amendment proposed): the incumbent > reference > cheapest tie rule below |
| 22 | * failed the pre-registered TURING Wave 1 kill test. This selector is kept |
| 23 | * unchanged only to reproduce evidence/MIXED_ALGEBRA/ma2_select_receipt.json |
| 24 | * and as the regression subject of test-realize. Do not add new callers. New |
| 25 | * selections use src/turing turing_rank_min_cost (lowest measured cost wins) |
| 26 | * and are recorded as turing.decision.v1 records via turing-field. |
| 27 | * |
| 28 | * Historical note: this selector is not wired to rx_costmodel (src/runtime). It reads |
| 29 | * the measured cost table from MA-2 benchmark receipts (never hard-coded |
| 30 | * costs), filters candidates by the exact contract, and picks the cheapest |
| 31 | * (subject to the tie rule below). |
| 32 | * |
| 33 | * Exact-contract filter: a realization is a candidate only if the registry |
| 34 | * marks it exact, the receipt marks it verified (bit-identical to the oracle) |
| 35 | * in every run used, and the query's n is within its max_n. |
| 36 | * |
| 37 | * Cost of a candidate in one run: median ns per call ("pack once, |
| 38 | * amortized") or median pack ns + median run ns ("pack per call"). With |
| 39 | * several runs, cost = mean over runs. |
| 40 | * Noise band (relative): max, over the winner and the runner-up, of |
| 41 | * - within-run spread (q75 - q25) / median of the timed blocks, and |
| 42 | * - across-run spread (max - min) / mean of the cell's costs over EVERY |
| 43 | * run in the table (>= 2 runs), also when the costs come from one run. |
| 44 | * With pack per call the within-run spread weights run and pack spreads by |
| 45 | * their share of the cost. |
| 46 | * margin = (cost runner-up - cost winner) / cost winner. |
| 47 | * Tied set: every eligible candidate whose cost is within the band of the |
| 48 | * cheapest, the cheapest included (recorded as tie_set / "tied_set"). TIE |
| 49 | * when the tied set has more than one member (equivalently margin <= band). |
| 50 | * Tie rule (ADR 0019 section 9.1, amended): a TIE is resolved only inside the |
| 51 | * tied set. The incumbent selection (query.incumbent) stands if it is in the |
| 52 | * tied set; otherwise the digital reference realization OMA_SEL_REFERENCE if |
| 53 | * it is in the tied set; otherwise the cheapest member. A realization outside |
| 54 | * the tied set is never selected by tie resolution. The decision records |
| 55 | * which rule applied in tie_resolution. Without a TIE the cheapest is chosen. */ |
| 56 | #ifndef OMA_SELECT_H |
| 57 | #define OMA_SELECT_H |
| 58 | |
| 59 | #include <stddef.h> |
| 60 | #include <stdio.h> |
| 61 | |
| 62 | #define OMA_SEL_MAX_ROWS 2048 |
| 63 | #define OMA_SEL_MAX_RUNS 4 |
| 64 | #define OMA_SEL_MAX_CAND 16 |
| 65 | #define OMA_SEL_ID 32 |
| 66 | /* Digital reference realization (ADR 0019 section 9.1 tie rule). */ |
| 67 | #define OMA_SEL_REFERENCE "R1_plain" |
| 68 | |
| 69 | enum { OMA_SEL_OK = 0, OMA_SEL_E_IO = -1, OMA_SEL_E_FORMAT = -2, OMA_SEL_E_FULL = -3, OMA_SEL_E_EMPTY = -4 }; |
| 70 | |
| 71 | typedef struct { |
| 72 | int run; /* index of the receipt it came from */ |
| 73 | size_t n, m; |
| 74 | double sparsity; |
| 75 | char rz[OMA_SEL_ID]; |
| 76 | int eligible, verified; |
| 77 | double median_ns, min_ns, noise_rel, pack_ns, pack_noise_rel, pct_floor; |
| 78 | } oma_sel_row; |
| 79 | |
| 80 | typedef struct { |
| 81 | int nruns; |
| 82 | char run_id[OMA_SEL_MAX_RUNS][96]; |
| 83 | char source[OMA_SEL_MAX_RUNS][256]; |
| 84 | size_t nrows; |
| 85 | oma_sel_row rows[OMA_SEL_MAX_ROWS]; |
| 86 | } oma_sel_table; |
| 87 | |
| 88 | typedef struct { |
| 89 | size_t n, m; |
| 90 | double sparsity; |
| 91 | int pack_per_call; /* 0: pack once, amortized; 1: pack every call */ |
| 92 | int run; /* -1: all runs in the table; else that run only */ |
| 93 | const char *incumbent; /* NULL or "": no incumbent selection */ |
| 94 | } oma_sel_query; |
| 95 | |
| 96 | typedef struct { |
| 97 | char rz[OMA_SEL_ID]; |
| 98 | int eligible; |
| 99 | const char *excluded_because; /* NULL when eligible */ |
| 100 | double cost_ns; /* mean over runs used */ |
| 101 | double run_cost_ns[OMA_SEL_MAX_RUNS]; |
| 102 | int runs_used; |
| 103 | double within_noise_rel; /* max over runs used */ |
| 104 | double cross_run_rel; /* over all runs in the table; 0 with one run */ |
| 105 | double pct_floor; /* mean over runs used, amortized run only */ |
| 106 | } oma_sel_candidate; |
| 107 | |
| 108 | typedef struct { |
| 109 | oma_sel_query q; |
| 110 | size_t cell_n, cell_m; /* measured cell the costs came from */ |
| 111 | double cell_sparsity; |
| 112 | int exact_cell; /* 1 if the query hit a measured cell */ |
| 113 | char chosen[OMA_SEL_ID]; |
| 114 | char cheapest[OMA_SEL_ID]; /* lowest measured cost */ |
| 115 | char runner_up[OMA_SEL_ID]; /* second lowest; margin is runner-up vs cheapest */ |
| 116 | double chosen_cost_ns, cheapest_cost_ns, runner_up_cost_ns; |
| 117 | double margin_rel, noise_band_rel; |
| 118 | int tie; |
| 119 | /* "none" (no TIE: cheapest), or on TIE "incumbent", "reference" or |
| 120 | * "cheapest" (the rule of ADR 0019 9.1 that applied inside the tied set) */ |
| 121 | const char *tie_resolution; |
| 122 | char tie_set[OMA_SEL_MAX_CAND][OMA_SEL_ID]; |
| 123 | size_t ntie; |
| 124 | size_t ncand; |
| 125 | oma_sel_candidate cand[OMA_SEL_MAX_CAND]; |
| 126 | char reason[256]; |
| 127 | } oma_sel_decision; |
| 128 | |
| 129 | void oma_sel_init(oma_sel_table *t); |
| 130 | /* Append one MA-2 bench receipt as a new run. */ |
| 131 | int oma_sel_load(oma_sel_table *t, const char *receipt_path); |
| 132 | int oma_sel_decide(const oma_sel_table *t, const oma_sel_query *q, oma_sel_decision *d); |
| 133 | /* 1 if id is in the decision's tie set (or is the chosen one). */ |
| 134 | int oma_sel_in_tie_set(const oma_sel_decision *d, const char *id); |
| 135 | void oma_sel_decision_json(FILE *f, const oma_sel_decision *d, const char *indent); |
| 136 | |
| 137 | #endif /* OMA_SELECT_H */ |