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 / runtime / rx_costmodel.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 | /* |
| 18 | * rx_costmodel.h -- Omega's empirical cost model (OMEGA_EMPIRICAL_OPTIMIZER). |
| 19 | * |
| 20 | * Omega picks a realization for an operation from what it has measured, not |
| 21 | * from a fixed rule. This is a calculator only: |
| 22 | * |
| 23 | * features of a call -> predicted cost of every eligible realization, |
| 24 | * with how sure the prediction is |
| 25 | * -> SELECT one, or MEASURE two when unsure |
| 26 | * |
| 27 | * It never runs code, never holds or reads authority, never writes a |
| 28 | * generation. Whoever calls it executes the realizations and reports what |
| 29 | * they cost. A learned model becomes durable only as the `model` blob of a |
| 30 | * generation promoted through the R9 barrier by a subject that holds the |
| 31 | * promotion right; the Makefile checks that rx_costmodel.o references none |
| 32 | * of those operations. |
| 33 | * |
| 34 | * This is not authority learning. No feature is derived from a capability, |
| 35 | * and a realization is eligible only if the caller says it was verified. |
| 36 | * |
| 37 | * Predictor: one Bayesian linear regression (normal-inverse-gamma, ridge |
| 38 | * prior) per (cell, arm), on log2 picoseconds per call. A cell is |
| 39 | * (operation class, core class, pressure bucket). Pressure cells hold a |
| 40 | * two-number correction over the quiet cell of the same core class. The basis is small and |
| 41 | * readable: 1, log2 M, log2 N, their product, (log2 N)^2, N <= 3, |
| 42 | * N % 4 != 0, and how far the working set overflows the core's cache, in |
| 43 | * doublings. Each (cell, arm) keeps only sufficient statistics, so the model |
| 44 | * serializes to a fixed-size blob. |
| 45 | */ |
| 46 | #ifndef RX_COSTMODEL_H |
| 47 | #define RX_COSTMODEL_H |
| 48 | |
| 49 | #include <stddef.h> |
| 50 | #include <stdint.h> |
| 51 | |
| 52 | #define RX_CM_ARMS 8u |
| 53 | #define RX_CM_OPS 1u /* matvec only. A new family widens this, changes |
| 54 | * RX_CM_CELLS, and must bump the blob version. |
| 55 | * An id outside 0 .. RX_CM_OPS-1 is refused; |
| 56 | * it is not aliased onto the last family. */ |
| 57 | #define RX_CM_CORES 3u |
| 58 | #define RX_CM_PRESSURE 3u |
| 59 | #define RX_CM_CELLS (RX_CM_OPS * RX_CM_CORES * RX_CM_PRESSURE) |
| 60 | #define RX_CM_D 8u /* basis size */ |
| 61 | #define RX_CM_TRI (RX_CM_D * (RX_CM_D + 1u) / 2u) |
| 62 | #define RX_CM_MIN_OBS 12u /* own-cell observations before a cell is used alone */ |
| 63 | #define RX_CM_RES_MIN_OBS 4u /* observations before a pressure correction is used */ |
| 64 | #define RX_CM_UNSEEN_SHIFT 1.0 /* prior sd, log2: an unseen pressure may halve or double cost */ |
| 65 | |
| 66 | #define RX_CM_OK 0 |
| 67 | #define RX_CM_ERR_ARG -1 |
| 68 | #define RX_CM_ERR_FORMAT -2 |
| 69 | #define RX_CM_ERR_DIGEST -3 |
| 70 | #define RX_CM_ERR_UNSUPPORTED -4 /* semantic operation id is not a modeled family */ |
| 71 | #define RX_CM_SKIPPED 1 /* observe: pressure data with no quiet model to correct */ |
| 72 | #define RX_CM_CELL_NONE UINT32_MAX /* not an index into RxCostModel.cell */ |
| 73 | enum { RX_CM_OP_MATVEC = 0 }; |
| 74 | enum { RX_CM_CORE_X925 = 0, RX_CM_CORE_A725 = 1, RX_CM_CORE_OTHER = 2 }; |
| 75 | |
| 76 | /* What the caller knows about one call. Fields the matvec workload cannot |
| 77 | * vary are carried and recorded, never used to fit (see spec). */ |
| 78 | typedef struct { |
| 79 | uint32_t op; /* semantic operation class; only ids below RX_CM_OPS */ |
| 80 | uint32_t M, N; /* input shape; branch count is 0 for matvec */ |
| 81 | uint64_t state_bytes; /* working set: (M*N + N + M) * 8 */ |
| 82 | uint64_t cache_bytes; /* last private cache of this core class */ |
| 83 | uint32_t core; /* hardware state: core class */ |
| 84 | uint32_t pressure; /* resource pressure bucket, 0 = none */ |
| 85 | uint32_t thermal_c; /* thermal state, degrees C (recorded) */ |
| 86 | uint32_t current_arm; /* realization in use now */ |
| 87 | uint32_t eligible; /* bit per arm: verified and allowed here */ |
| 88 | uint32_t need_evidence; /* 1: only arms with a passing verdict (always 1 here) */ |
| 89 | uint32_t cognitive; /* cognitive requirement (recorded; 0 for matvec) */ |
| 90 | uint64_t latency_budget_ps; /* per call; 0 = none */ |
| 91 | uint64_t energy_budget_pj; /* per call; 0 = none */ |
| 92 | } RxCmFeatures; |
| 93 | |
| 94 | /* One measurement the caller made. */ |
| 95 | typedef struct { |
| 96 | uint64_t ps_per_call; /* latency */ |
| 97 | int failed; /* wrong result, crash or verification refusal */ |
| 98 | } RxCmObservation; |
| 99 | |
| 100 | typedef struct { |
| 101 | double mean_log2_ps; /* predicted log2 picoseconds per call */ |
| 102 | double sd_log2; /* predictive standard deviation, log2 units */ |
| 103 | double dof; /* degrees of freedom of the predictive t */ |
| 104 | uint32_t n; /* observations behind it */ |
| 105 | int known; /* its own cell has at least RX_CM_MIN_OBS observations */ |
| 106 | int borrowed; /* unseen pressure: the quiet prediction, widened */ |
| 107 | double energy_pj; /* predicted energy per call (0 if no power figure) */ |
| 108 | double fail_p; /* posterior mean failure probability */ |
| 109 | int refused; /* 1: this operation id is not modeled */ |
| 110 | } RxCmPrediction; |
| 111 | |
| 112 | enum { RX_CM_SELECT = 1, RX_CM_MEASURE = 2, RX_CM_FALLBACK = 3, RX_CM_REFUSE = 4 }; |
| 113 | |
| 114 | /* Why a MEASURE, FALLBACK or REFUSE was returned. */ |
| 115 | enum { |
| 116 | RX_CM_WHY_CONFIDENT = 0, |
| 117 | RX_CM_WHY_UNKNOWN_ARM, /* an eligible arm has too few observations here */ |
| 118 | RX_CM_WHY_WORTH_MEASURING, /* expected improvement from measuring exceeds ei_min */ |
| 119 | RX_CM_WHY_BUDGET_SPENT, /* would measure, but the exploration budget is spent */ |
| 120 | RX_CM_WHY_NO_ELIGIBLE, /* nothing but the fallback may run */ |
| 121 | RX_CM_WHY_OVER_BUDGET, /* no arm is predicted to meet the latency/energy budget */ |
| 122 | RX_CM_WHY_UNSUPPORTED_OP /* semantic operation id is not a modeled family */ |
| 123 | }; |
| 124 | |
| 125 | typedef struct { |
| 126 | int action; |
| 127 | uint32_t arm; /* SELECT / FALLBACK: run this */ |
| 128 | uint32_t measure[2]; /* MEASURE: run both, keep the faster */ |
| 129 | uint32_t why; |
| 130 | double p_runner_up_better; /* P(competitor faster than the chosen arm) */ |
| 131 | double expected_improvement; /* E[max(0, log2 cost chosen - log2 cost competitor)] */ |
| 132 | RxCmPrediction pred[RX_CM_ARMS]; |
| 133 | } RxCmDecision; |
| 134 | |
| 135 | typedef struct { |
| 136 | /* Measure only when the expected improvement of the likeliest competitor |
| 137 | * over the chosen arm exceeds this, in log2 units (0.08 is about 6%). |
| 138 | * Two arms predicted equal and tight are not worth a run; an arm the |
| 139 | * model knows little about, or two arms it cannot separate, are. */ |
| 140 | double ei_min; |
| 141 | double max_fail_p; /* an arm that has failed and is above this is not selected */ |
| 142 | uint32_t fallback_arm; /* known-good: always verified, never removed */ |
| 143 | /* Exploration budget: extra time spent measuring may not exceed |
| 144 | * explore_frac of the time spent on chosen work plus explore_allow_ps. */ |
| 145 | double explore_frac; |
| 146 | uint64_t explore_allow_ps; |
| 147 | } RxCmPolicy; |
| 148 | |
| 149 | typedef struct { |
| 150 | double tri[RX_CM_TRI]; /* X'X + lambda I, upper triangle, row-major */ |
| 151 | double xty[RX_CM_D]; |
| 152 | double yty; |
| 153 | uint64_t n; |
| 154 | uint64_t trials, failures; /* failure probability: Beta(1 + f, 1 + t - f) */ |
| 155 | } RxCmArmStats; |
| 156 | |
| 157 | typedef struct { |
| 158 | uint32_t n_arms; |
| 159 | double lambda; /* ridge prior precision */ |
| 160 | RxCmArmStats cell[RX_CM_CELLS][RX_CM_ARMS]; |
| 161 | double power_mw[RX_CM_CORES][RX_CM_ARMS]; /* measured power while running, 0 = unknown */ |
| 162 | uint64_t verify_ns[RX_CM_ARMS]; /* verification cost */ |
| 163 | uint64_t synth_ns[RX_CM_ARMS]; /* synthesis cost; recompute = synth + verify */ |
| 164 | uint64_t code_bytes[RX_CM_ARMS]; /* memory held by the realization */ |
| 165 | /* Calibration: predictive spread is multiplied by this. Set from residuals |
| 166 | * on validation work measured apart from training (rx_cm_calibrate). */ |
| 167 | double sd_scale; |
| 168 | /* Runtime only; not serialized. */ |
| 169 | uint64_t explore_ps, work_ps; |
| 170 | } RxCostModel; |
| 171 | |
| 172 | void rx_cm_default_policy(RxCmPolicy *p); |
| 173 | void rx_cm_init(RxCostModel *m, uint32_t n_arms); |
| 174 | |
| 175 | /* Fold one measurement in. RX_CM_OK on success; RX_CM_SKIPPED for a pressure |
| 176 | * measurement when there is no quiet model yet to correct. |
| 177 | * RX_CM_ERR_UNSUPPORTED when `f->op` is not a modeled family: no sufficient |
| 178 | * statistic, trial, failure, exploration counter or other model field changes. |
| 179 | * RX_CM_ERR_ARG for a null argument, an arm this model does not have, or a |
| 180 | * successful observation with zero latency. */ |
| 181 | int rx_cm_observe(RxCostModel *m, const RxCmFeatures *f, uint32_t arm, const RxCmObservation *o); |
| 182 | |
| 183 | /* Predict one arm. Returns RX_CM_ERR_UNSUPPORTED and sets out->refused when |
| 184 | * the semantic operation is not a modeled family. That output is zeros aside |
| 185 | * from `refused`: no mean, spread, failure rate or ranking is borrowed from |
| 186 | * another operation. Returns RX_CM_ERR_ARG for a null argument or an arm this |
| 187 | * model does not have. Otherwise returns RX_CM_OK. |
| 188 | * A supported operation needs RX_CM_MIN_OBS quiet observations for the core |
| 189 | * class before out->known is set. Under pressure, adds the learned correction |
| 190 | * once it has RX_CM_RES_MIN_OBS observations; before that, widens by |
| 191 | * RX_CM_UNSEEN_SHIFT and marks it borrowed. */ |
| 192 | int rx_cm_predict(const RxCostModel *m, const RxCmFeatures *f, uint32_t arm, RxCmPrediction *out); |
| 193 | |
| 194 | /* Decide. An unsupported semantic operation sets RX_CM_REFUSE and |
| 195 | * RX_CM_WHY_UNSUPPORTED_OP, leaves every prediction refused, and does not |
| 196 | * rank arms (`arm` and `measure[]` are set to n_arms, not a selection). |
| 197 | * `allow_measure` 0 freezes the model's behaviour for a supported operation: |
| 198 | * it never asks to measure and picks the best prediction. The fallback arm is |
| 199 | * chosen when no other eligible arm has any prediction at all. */ |
| 200 | void rx_cm_decide(const RxCostModel *m, const RxCmPolicy *p, const RxCmFeatures *f, |
| 201 | int allow_measure, RxCmDecision *out); |
| 202 | |
| 203 | /* Split calibration. For n measurements taken apart from the training data, |
| 204 | * set sd_scale so that the central q interval covers a fraction q of them |
| 205 | * (never below 1). Returns the scale, or 0 if fewer than 10 were usable. */ |
| 206 | double rx_cm_calibrate(RxCostModel *m, const RxCmFeatures *f, const uint32_t *arm, |
| 207 | const uint64_t *ps, uint32_t n, double q); |
| 208 | |
| 209 | /* Account for time spent: chosen work, and extra time spent measuring. */ |
| 210 | void rx_cm_charge(RxCostModel *m, uint64_t work_ps, uint64_t explore_ps); |
| 211 | |
| 212 | /* Canonical bytes: header, the fixed-size body, SHA-256 of both. The size is |
| 213 | * rx_cm_blob_size(). Runtime counters are not included. */ |
| 214 | size_t rx_cm_blob_size(void); |
| 215 | int rx_cm_serialize(const RxCostModel *m, uint8_t *out, size_t cap, size_t *out_len); |
| 216 | int rx_cm_deserialize(RxCostModel *m, const uint8_t *in, size_t len); |
| 217 | void rx_cm_digest(const RxCostModel *m, uint8_t out[32]); |
| 218 | |
| 219 | /* Basis for these features, exposed so tests can see exactly what is fit. */ |
| 220 | void rx_cm_basis(const RxCmFeatures *f, double phi[RX_CM_D]); |
| 221 | |
| 222 | /* Cell index for these features. RX_CM_CELL_NONE if `f` is null or `f->op` |
| 223 | * is not one of the RX_CM_OPS modeled families. That sentinel is never a |
| 224 | * legal index of RxCostModel.cell. An unknown core class maps to |
| 225 | * RX_CM_CORE_OTHER. Pressure above the last bucket saturates into that |
| 226 | * bucket. Those two mappings are hardware and pressure buckets, not a |
| 227 | * catch-all operation family. */ |
| 228 | uint32_t rx_cm_cell(const RxCmFeatures *f); |
| 229 | |
| 230 | /* Two-sided central interval half-width multiplier for coverage q with |
| 231 | * `dof` degrees of freedom (Student t, Cornish-Fisher approximation). */ |
| 232 | double rx_cm_t_quantile(double q, double dof); |
| 233 | |
| 234 | #endif /* RX_COSTMODEL_H */ |