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/CUDApp/blob/main/tests/runtime/rx_costmodel_unit.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / runtime / rx_costmodel_unit.c` 709 lines · 31786 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 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_unit -- the empirical cost model on synthetic measurements. 19 * 20 * Any host. Checks fit, calibration of the predictive intervals on held-out 21 * points, the decision rules (select, measure when unsure, frozen, budget, 22 * eligibility, failures, fallback, latency/energy budgets) and the canonical 23 * blob (round trip, tamper, truncation, version, cell count). An unsupported 24 * semantic operation is refused. `make test-costmodel` restores the old 25 * matvec clamp in a mutant; that binary must fail 26 * unsupported-op-not-aliased-to-matvec. 27 */ 28 #include "runtime/rx_costmodel.h" 29 #include "sha256.h" 30 31 #include 32 #include 33 #include 34 #include 35 36 static int g_checks, g_fail; 37 38 #define CHECK(cond, ...) do { \ 39 g_checks++; \ 40 if (!(cond)) { \ 41 g_fail++; \ 42 fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ 43 fprintf(stderr, __VA_ARGS__); \ 44 fputc('\n', stderr); \ 45 } \ 46 } while (0) 47 48 static uint64_t rng = 0x9E3779B97F4A7C15ull; 49 static uint64_t next(void) { 50 rng ^= rng << 13; 51 rng ^= rng >> 7; 52 rng ^= rng << 17; 53 return rng; 54 } 55 static double unif(void) { return (double)(next() >> 11) / `9007199254740992`.0; } 56 static double gauss(void) { 57 double u = unif(), v = unif(); 58 if (u < 1e-300) u = 1e-300; 59 return sqrt(-2.0 * log(u)) * cos(6.`283185307179586` * v); 60 } 61 62 /* Synthetic truth: log2 ps for three arms, shaped like the Spark probe 63 * (a fixed cost, a per-element cost, a thin-matrix penalty for arm 2). */ 64 static int g_exact; /* 1: truth lies in the model's basis */ 65 static double truth(uint32_t arm, uint32_t M, uint32_t N, uint32_t core) { 66 double w = g_exact ? log2((double)M) + log2((double)N) : log2((double)M * N + 1.0); 67 double base = 10.0 + 0.95 * w + (core ? 0.4 : 0.0); 68 if (arm == 1) base += 0.15; 69 if (arm == 2) base += (N <= 3 ? 0.8 : -0.7); 70 return base; 71 } 72 73 static RxCmFeatures feat(uint32_t M, uint32_t N, uint32_t core, uint32_t pressure) { 74 RxCmFeatures f; 75 memset(&f, 0, sizeof f); 76 f.op = RX_CM_OP_MATVEC; 77 f.M = M; 78 f.N = N; 79 f.state_bytes = ((uint64_t)M * N + M + N) * 8u; 80 f.cache_bytes = 2u << 20; 81 f.core = core; 82 f.pressure = pressure; 83 f.eligible = 0x7; 84 f.need_evidence = 1; 85 return f; 86 } 87 88 static const uint32_t TRAIN_M[] = { 1, 4, 16, 64, 256 }; 89 /* Odd and even sizes, so "N <= 3" and "N % 4 != 0" are not confounded. */ 90 static const uint32_t TRAIN_N[] = { 1, 2, 3, 5, 8, 12, 33, 64, 100, 256, 1024 }; 91 #define N_TRAIN_N (sizeof TRAIN_N / sizeof TRAIN_N[0]) 92 93 static void train(RxCostModel *m, double noise, uint32_t core, uint32_t pressure, int reps) { 94 for (int r = 0; r < reps; r++) 95 for (unsigned i = 0; i < 5; i++) 96 for (unsigned j = 0; j < N_TRAIN_N; j++) 97 for (uint32_t a = 0; a < 3; a++) { 98 RxCmFeatures f = feat(TRAIN_M[i], TRAIN_N[j], core, pressure); 99 double y = truth(a, f.M, f.N, core) + noise * gauss(); 100 RxCmObservation o = { (uint64_t)exp2(y), 0 }; 101 rx_cm_observe(m, &f, a, &o); 102 } 103 } 104 105 static void t_basis(void) { 106 RxCmFeatures f = feat(8, 3, RX_CM_CORE_A725, 1); 107 f.state_bytes = 8u << 20; 108 double phi[RX_CM_D]; 109 rx_cm_basis(&f, phi); 110 CHECK(phi[0] == 1.0 && phi[1] == 3.0 && fabs(phi[2] - log2(3.0)) < 1e-12, "basis logs"); 111 CHECK(phi[5] == 1.0 && phi[6] == 1.0 && fabs(phi[7] - 2.0) < 1e-12, "basis indicators"); 112 CHECK(rx_cm_cell(&f) == 1u * RX_CM_PRESSURE + 1u, "cell index"); 113 f.core = 99; 114 f.pressure = 0; 115 CHECK(rx_cm_cell(&f) == (RX_CM_OP_MATVEC * RX_CM_CORES + RX_CM_CORE_OTHER) * RX_CM_PRESSURE, 116 "out-of-range core is the OTHER class"); 117 f.core = RX_CM_CORE_A725; 118 f.pressure = 99; 119 CHECK(rx_cm_cell(&f) == (RX_CM_OP_MATVEC * RX_CM_CORES + RX_CM_CORE_A725) * RX_CM_PRESSURE + 120 (RX_CM_PRESSURE - 1u), 121 "out-of-range pressure saturates to the last bucket"); 122 f.op = RX_CM_OPS; 123 CHECK(rx_cm_cell(&f) == RX_CM_CELL_NONE && RX_CM_CELL_NONE >= RX_CM_CELLS, 124 "first unsupported op is not a cell index"); 125 f.op = UINT32_MAX; 126 CHECK(rx_cm_cell(&f) == RX_CM_CELL_NONE, "UINT32_MAX op is not a cell index"); 127 } 128 129 static void t_fit_and_calibration(int exact, double *cov80, double *cov95, double *rmse) { 130 g_exact = exact; 131 RxCostModel m; 132 rx_cm_init(&m, 3); 133 const double noise = 0.08; 134 train(&m, noise, RX_CM_CORE_X925, 0, 2); 135 uint32_t in80 = 0, in95 = 0, n = 0; 136 double se = 0.0; 137 double q80 = 0, q95 = 0; 138 for (int k = 0; k < 4000; k++) { 139 /* held out: shapes not on the training grid */ 140 uint32_t M = 1 + (uint32_t)(next() % 300); 141 uint32_t N = 1 + (uint32_t)(next() % 1000); 142 uint32_t a = (uint32_t)(next() % 3); 143 RxCmFeatures f = feat(M, N, RX_CM_CORE_X925, 0); 144 RxCmPrediction p; 145 rx_cm_predict(&m, &f, a, &p); 146 double y = truth(a, M, N, 0) + noise * gauss(); 147 double z = fabs(y - p.mean_log2_ps); 148 q80 = rx_cm_t_quantile(0.80, p.dof) * p.sd_log2; 149 q95 = rx_cm_t_quantile(0.95, p.dof) * p.sd_log2; 150 in80 += z <= q80; 151 in95 += z <= q95; 152 se += (y - p.mean_log2_ps) * (y - p.mean_log2_ps); 153 n++; 154 } 155 *cov80 = (double)in80 / n; 156 *cov95 = (double)in95 / n; 157 *rmse = sqrt(se / n); 158 printf(" held-out synthetic (%s): rmse %.3f log2 (noise %.2f), coverage 80%%: %.3f, 95%%: %.3f\n", 159 exact ? "truth in basis" : "truth outside basis", *rmse, noise, *cov80, *cov95); 160 g_exact = 0; 161 CHECK(*rmse < 0.2, "held-out error %.3f", *rmse); 162 if (exact) { 163 CHECK(*cov80 > 0.74 && *cov80 < 0.86, "80%% interval covers %.3f", *cov80); 164 CHECK(*cov95 > 0.92 && *cov95 < 0.98, "95%% interval covers %.3f", *cov95); 165 } else { /* misspecified: may be wide, must not be overconfident */ 166 CHECK(*cov80 > 0.74, "80%% interval covers %.3f", *cov80); 167 CHECK(*cov95 > 0.92, "95%% interval covers %.3f", *cov95); 168 } 169 } 170 171 static void t_decide(void) { 172 RxCmPolicy pol; 173 rx_cm_default_policy(&pol); 174 RxCostModel m; 175 rx_cm_init(&m, 3); 176 RxCmDecision d; 177 RxCmFeatures f = feat(64, 256, RX_CM_CORE_X925, 0); 178 179 rx_cm_decide(&m, &pol, &f, 1, &d); 180 CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_UNKNOWN_ARM, "empty model measures"); 181 rx_cm_decide(&m, &pol, &f, 0, &d); 182 CHECK(d.action == RX_CM_FALLBACK && d.arm == 0, "empty frozen model falls back"); 183 184 train(&m, 0.03, RX_CM_CORE_X925, 0, 2); 185 rx_cm_decide(&m, &pol, &f, 1, &d); 186 CHECK(d.action == RX_CM_SELECT && d.arm == 2 && d.why == RX_CM_WHY_CONFIDENT, 187 "wide matrix: arm 2 selected confidently (action %d arm %u why %u)", d.action, d.arm, 188 d.why); 189 f = feat(256, 1, RX_CM_CORE_X925, 0); 190 rx_cm_decide(&m, &pol, &f, 1, &d); 191 CHECK(d.action == RX_CM_SELECT && d.arm == 0, "thin matrix: arm 0 selected (arm %u)", d.arm); 192 193 /* two arms predicted equal: worth a run only if the model is unsure */ 194 RxCostModel c, noisy; 195 rx_cm_init(&c, 2); 196 rx_cm_init(&noisy, 2); 197 RxCmFeatures g = feat(16, 16, RX_CM_CORE_X925, 0); 198 g.eligible = 0x3; 199 for (int r = 0; r < 30; r++) { 200 RxCmObservation o0 = { (uint64_t)exp2(20.0 + 0.02 * gauss()), 0 }; 201 RxCmObservation o1 = { (uint64_t)exp2(20.01 + 0.02 * gauss()), 0 }; 202 rx_cm_observe(&c, &g, 0, &o0); 203 rx_cm_observe(&c, &g, 1, &o1); 204 RxCmObservation n0 = { (uint64_t)exp2(20.0 + 0.4 * gauss()), 0 }; 205 RxCmObservation n1 = { (uint64_t)exp2(20.0 + 0.4 * gauss()), 0 }; 206 rx_cm_observe(&noisy, &g, 0, &n0); 207 rx_cm_observe(&noisy, &g, 1, &n1); 208 } 209 rx_cm_decide(&c, &pol, &g, 1, &d); 210 CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_CONFIDENT, 211 "equal and tight: not worth a run (action %d, ei %.3f)", d.action, d.expected_improvement); 212 rx_cm_decide(&noisy, &pol, &g, 1, &d); 213 CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_WORTH_MEASURING, 214 "equal and unsure: measured (action %d, ei %.3f)", d.action, d.expected_improvement); 215 rx_cm_decide(&noisy, &pol, &g, 0, &d); 216 CHECK(d.action == RX_CM_SELECT, "frozen: never measures"); 217 rx_cm_charge(&noisy, 0, pol.explore_allow_ps + 1); 218 rx_cm_decide(&noisy, &pol, &g, 1, &d); 219 CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_BUDGET_SPENT, 220 "spent budget: select instead of measure"); 221 rx_cm_charge(&noisy, (uint64_t)((double)(pol.explore_allow_ps + 1) / pol.explore_frac) * 2, 0); 222 rx_cm_decide(&noisy, &pol, &g, 1, &d); 223 CHECK(d.action == RX_CM_MEASURE, "budget refills as chosen work accrues"); 224 225 /* unseen pressure: borrowed and widened */ 226 f = feat(64, 256, RX_CM_CORE_X925, 2); 227 RxCmPrediction p0, p2; 228 RxCmFeatures f0 = feat(64, 256, RX_CM_CORE_X925, 0); 229 rx_cm_predict(&m, &f0, 2, &p0); 230 rx_cm_predict(&m, &f, 2, &p2); 231 CHECK(p2.borrowed && !p0.borrowed && p2.mean_log2_ps == p0.mean_log2_ps && 232 fabs(p2.sd_log2 - sqrt(p0.sd_log2 * p0.sd_log2 + RX_CM_UNSEEN_SHIFT * RX_CM_UNSEEN_SHIFT)) < 1e-9, 233 "unseen pressure: quiet prediction, widened by the unseen-shift prior"); 234 235 /* Pressure learned as a correction: arm 2 slows by 1.5 doublings, arm 0 236 * by 0.2. A handful of observations flips the choice. */ 237 RxCostModel pm = m; 238 RxCmFeatures pf[6]; 239 for (int i = 0; i < 6; i++) { 240 pf[i] = feat(TRAIN_M[i % 5], TRAIN_N[(i * 3) % N_TRAIN_N], RX_CM_CORE_X925, 1); 241 for (uint32_t a = 0; a < 3; a++) { 242 double shift = a == 2 ? 1.5 : 0.2; 243 RxCmObservation o = { (uint64_t)exp2(truth(a, pf[i].M, pf[i].N, 0) + shift + 0.03 * gauss()), 0 }; 244 CHECK(rx_cm_observe(&pm, &pf[i], a, &o) == RX_CM_OK, "pressure observation"); 245 } 246 } 247 RxCmFeatures q1 = feat(64, 256, RX_CM_CORE_X925, 1); 248 RxCmPrediction pq; 249 rx_cm_predict(&pm, &q1, 2, &pq); 250 CHECK(pq.known && !pq.borrowed && fabs(pq.mean_log2_ps - (p0.mean_log2_ps + 1.5)) < 0.15, 251 "pressure correction learned from 6 points (%.2f vs %.2f)", pq.mean_log2_ps, 252 p0.mean_log2_ps + 1.5); 253 rx_cm_decide(&pm, &pol, &q1, 0, &d); 254 CHECK(d.arm == 0, "under pressure the choice flips to arm 0 (got %u)", d.arm); 255 RxCostModel bare; 256 rx_cm_init(&bare, 3); 257 RxCmObservation ob = { 1000, 0 }; 258 CHECK(rx_cm_observe(&bare, &q1, 0, &ob) == RX_CM_SKIPPED, "no quiet model: pressure data skipped"); 259 rx_cm_decide(&m, &pol, &f, 0, &d); 260 CHECK(d.action == RX_CM_SELECT && d.arm == 2, "frozen uses the borrowed prediction"); 261 RxCostModel mid; /* close, moderately sure: not worth a run here, worth one when borrowed */ 262 rx_cm_init(&mid, 2); 263 for (int r = 0; r < 40; r++) { 264 RxCmObservation o0 = { (uint64_t)exp2(20.0 + 0.1 * gauss()), 0 }; 265 RxCmObservation o1 = { (uint64_t)exp2(20.0 + 0.1 * gauss()), 0 }; 266 rx_cm_observe(&mid, &g, 0, &o0); 267 rx_cm_observe(&mid, &g, 1, &o1); 268 } 269 rx_cm_decide(&mid, &pol, &g, 1, &d); 270 CHECK(d.action == RX_CM_SELECT, "close and moderately sure: select (ei %.3f)", d.expected_improvement); 271 RxCmFeatures gp = g; 272 gp.pressure = 1; 273 rx_cm_decide(&mid, &pol, &gp, 1, &d); 274 CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_WORTH_MEASURING, 275 "same arms under unseen pressure: widened spread makes it worth a run (ei %.3f)", 276 d.expected_improvement); 277 278 /* unseen core class with no data anywhere: fallback or measure */ 279 f = feat(64, 256, RX_CM_CORE_A725, 0); 280 rx_cm_decide(&m, &pol, &f, 0, &d); 281 CHECK(d.action == RX_CM_FALLBACK && d.arm == 0, "no data for this core class: fallback"); 282 } 283 284 static void t_eligibility_property(void) { 285 RxCmPolicy pol; 286 rx_cm_default_policy(&pol); 287 RxCostModel m; 288 rx_cm_init(&m, 4); 289 /* arm 3 would be the fastest by far, but is never eligible */ 290 for (int r = 0; r < 3; r++) 291 for (unsigned i = 0; i < 5; i++) 292 for (unsigned j = 0; j < N_TRAIN_N; j++) { 293 RxCmFeatures f = feat(TRAIN_M[i], TRAIN_N[j], RX_CM_CORE_X925, 0); 294 for (uint32_t a = 0; a < 3; a++) { 295 RxCmObservation o = { (uint64_t)exp2(truth(a, f.M, f.N, 0) + 0.05 * gauss()), 0 }; 296 rx_cm_observe(&m, &f, a, &o); 297 } 298 RxCmObservation o3 = { (uint64_t)exp2(truth(0, f.M, f.N, 0) - 3.0), 0 }; 299 rx_cm_observe(&m, &f, 3, &o3); 300 } 301 uint32_t touched = 0, decisions = 0; 302 for (int k = 0; k < 20000; k++) { 303 RxCmFeatures f = feat(1 + (uint32_t)(next() % 2000), 1 + (uint32_t)(next() % 4000), 304 (uint32_t)(next() % 3), (uint32_t)(next() % 3)); 305 f.eligible = 0x7; 306 f.latency_budget_ps = (next() & 1) ? (uint64_t)exp2(10 + (double)(next() % 20)) : 0; 307 RxCmDecision d; 308 rx_cm_decide(&m, &pol, &f, (int)(next() & 1), &d); 309 decisions++; 310 if (d.arm == 3 || (d.action == RX_CM_MEASURE && (d.measure[0] == 3 || d.measure[1] == 3))) 311 touched++; 312 } 313 CHECK(touched == 0, "an ineligible arm was chosen or measured %u times in %u decisions", 314 touched, decisions); 315 316 /* failures exclude an arm; the fallback is never excluded */ 317 RxCmFeatures f = feat(64, 256, RX_CM_CORE_X925, 0); 318 RxCmDecision d; 319 rx_cm_decide(&m, &pol, &f, 0, &d); 320 uint32_t fast = d.arm; 321 CHECK(fast == 2, "before failures, arm 2 wins"); 322 RxCmObservation bad = { 0, 1 }; 323 for (int i = 0; i < 3; i++) rx_cm_observe(&m, &f, 2, &bad); 324 rx_cm_decide(&m, &pol, &f, 0, &d); 325 CHECK(d.arm != 2 && d.pred[2].fail_p > pol.max_fail_p, "a failing arm is no longer selected"); 326 for (int i = 0; i < 400; i++) rx_cm_observe(&m, &f, 0, &bad); 327 f.eligible = 0x1; 328 rx_cm_decide(&m, &pol, &f, 1, &d); 329 CHECK(d.action == RX_CM_FALLBACK && d.arm == 0 && d.why == RX_CM_WHY_NO_ELIGIBLE, 330 "the fallback stays available"); 331 } 332 333 static void t_budgets(void) { 334 RxCmPolicy pol; 335 rx_cm_default_policy(&pol); 336 RxCostModel m; 337 rx_cm_init(&m, 2); 338 RxCmFeatures f = feat(32, 32, RX_CM_CORE_X925, 0); 339 /* arm 0: 2^20 ps, tight. arm 1: 2^19.8 ps, noisy. */ 340 for (int r = 0; r < 40; r++) { 341 RxCmObservation o0 = { (uint64_t)exp2(20.0 + 0.02 * gauss()), 0 }; 342 RxCmObservation o1 = { (uint64_t)exp2(19.8 + 0.6 * gauss()), 0 }; 343 rx_cm_observe(&m, &f, 0, &o0); 344 rx_cm_observe(&m, &f, 1, &o1); 345 } 346 RxCmDecision d; 347 f.eligible = 0x3; 348 rx_cm_decide(&m, &pol, &f, 0, &d); 349 uint32_t free_pick = d.arm; 350 f.latency_budget_ps = (uint64_t)exp2(20.2); 351 rx_cm_decide(&m, &pol, &f, 0, &d); 352 CHECK(d.arm == 0 && d.why == RX_CM_WHY_CONFIDENT, 353 "latency budget picks the arm whose 90%% bound fits (free pick %u, got %u)", free_pick, 354 d.arm); 355 f.latency_budget_ps = 1000; 356 rx_cm_decide(&m, &pol, &f, 0, &d); 357 CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_OVER_BUDGET, "no arm fits: reported"); 358 f.latency_budget_ps = 0; 359 m.power_mw[RX_CM_CORE_X925][0] = 1000.0; 360 m.power_mw[RX_CM_CORE_X925][1] = 5000.0; 361 RxCmPrediction p0, p1; 362 rx_cm_predict(&m, &f, 0, &p0); 363 rx_cm_predict(&m, &f, 1, &p1); 364 f.energy_budget_pj = (uint64_t)((p0.energy_pj + p1.energy_pj) / 2.0); 365 rx_cm_decide(&m, &pol, &f, 0, &d); 366 CHECK(d.arm == 0, "energy budget excludes the hungrier arm"); 367 CHECK(fabs(p0.energy_pj - exp2(p0.mean_log2_ps) * 1.0) < 1e-6 * p0.energy_pj, 368 "energy = predicted ps x measured mW"); 369 } 370 371 static void t_calibrate(void) { 372 /* Trained on quiet data; later the machine drifts (extra 0.15 log2). */ 373 RxCostModel m; 374 rx_cm_init(&m, 3); 375 g_exact = 1; 376 train(&m, 0.03, RX_CM_CORE_X925, 0, 2); 377 RxCmFeatures cf[600]; 378 uint32_t ca[600]; 379 uint64_t cps[600]; 380 for (int i = 0; i < 600; i++) { 381 cf[i] = feat(1 + (uint32_t)(next() % 300), 1 + (uint32_t)(next() % 1000), RX_CM_CORE_X925, 0); 382 ca[i] = (uint32_t)(next() % 3); 383 cps[i] = (uint64_t)exp2(truth(ca[i], cf[i].M, cf[i].N, 0) + 0.03 * gauss() + 0.15 * gauss()); 384 } 385 uint32_t before = 0, after = 0, n = 0; 386 for (int pass = 0; pass < 2; pass++) { 387 if (pass == 1) { 388 double s = rx_cm_calibrate(&m, cf, ca, cps, 300, 0.80); 389 printf(" calibration scale after drift: %.2f\n", s); 390 CHECK(s > 1.5, "drift widens the spread (%.2f)", s); 391 } 392 for (int i = 300; i < 600; i++) { 393 RxCmPrediction p; 394 rx_cm_predict(&m, &cf[i], ca[i], &p); 395 double z = fabs(log2((double)cps[i]) - p.mean_log2_ps); 396 int in = z <= rx_cm_t_quantile(0.80, p.dof) * p.sd_log2; 397 if (pass == 0) before += in; 398 else after += in; 399 n += pass == 0; 400 } 401 } 402 g_exact = 0; 403 printf(" 80%% coverage on later, drifted data: %.3f before calibration, %.3f after\n", 404 (double)before / n, (double)after / n); 405 CHECK((double)before / n < 0.6, "uncalibrated model is overconfident under drift"); 406 CHECK((double)after / n > 0.72 && (double)after / n < 0.88, "calibrated coverage %.3f", 407 (double)after / n); 408 RxCostModel few; 409 rx_cm_init(&few, 3); 410 CHECK(rx_cm_calibrate(&few, cf, ca, cps, 300, 0.8) == 0.0 && few.sd_scale == 1.0, 411 "no usable predictions: scale unchanged"); 412 } 413 414 static void t_blob(void) { 415 RxCostModel m, r; 416 rx_cm_init(&m, 5); 417 train(&m, 0.05, RX_CM_CORE_A725, 1, 1); 418 m.power_mw[1][2] = 1234.5; 419 m.verify_ns[2] = 77; 420 m.synth_ns[2] = 88; 421 m.code_bytes[2] = 99; 422 m.explore_ps = 5; 423 m.sd_scale = 1.7; 424 size_t n = rx_cm_blob_size(), got = 0; 425 printf(" model blob: %zu bytes\n", n); 426 CHECK(n <= 65536u, "fits the generation store blob limit"); 427 uint8_t *a = malloc(n), *b = malloc(n); 428 CHECK(rx_cm_serialize(&m, a, n, &got) == RX_CM_OK && got == n, "serialize"); 429 CHECK(rx_cm_deserialize(&r, a, n) == RX_CM_OK, "deserialize"); 430 CHECK(r.explore_ps == 0 && r.work_ps == 0, "runtime counters are not durable"); 431 CHECK(rx_cm_serialize(&r, b, n, &got) == RX_CM_OK && memcmp(a, b, n) == 0, 432 "round trip is byte-identical"); 433 uint8_t d1[32], d2[32]; 434 rx_cm_digest(&m, d1); 435 rx_cm_digest(&r, d2); 436 CHECK(memcmp(d1, d2, 32) == 0, "digest survives the round trip"); 437 RxCmFeatures f = feat(100, 30, RX_CM_CORE_A725, 1); 438 RxCmPrediction p, q; 439 rx_cm_predict(&m, &f, 1, &p); 440 rx_cm_predict(&r, &f, 1, &q); 441 CHECK(p.mean_log2_ps == q.mean_log2_ps && p.sd_log2 == q.sd_log2, "same predictions"); 442 CHECK(r.sd_scale == 1.7, "calibration scale is durable"); 443 memcpy(b, a, n); 444 b[100] ^= 1; 445 CHECK(rx_cm_deserialize(&r, b, n) == RX_CM_ERR_DIGEST, "tampered body refused"); 446 CHECK(rx_cm_deserialize(&r, a, n - 1) == RX_CM_ERR_FORMAT, "truncated blob refused"); 447 memcpy(b, a, n); 448 b[4] = 9; /* version */ 449 sha256_hash(b, n - 32, b + n - 32); 450 CHECK(rx_cm_deserialize(&r, b, n) == RX_CM_ERR_FORMAT, "unknown version refused"); 451 memcpy(b, a, n); 452 /* Header.cells is the fifth uint32. A wider operation family changes it. */ 453 uint32_t cells = 0; 454 memcpy(&cells, b + 16, sizeof cells); 455 cells += 1u; 456 memcpy(b + 16, &cells, sizeof cells); 457 sha256_hash(b, n - 32, b + n - 32); 458 CHECK(rx_cm_deserialize(&r, b, n) == RX_CM_ERR_FORMAT, "cell-count mismatch refused"); 459 free(a); 460 free(b); 461 } 462 463 /* Issue 704: an unsupported semantic operation used to clamp onto matvec, so a 464 * learned matvec prediction was reported for it and its observations trained 465 * that cell. `make test-costmodel` rebuilds rx_costmodel.c with that clamp 466 * restored; the named assertion unsupported-op-not-aliased-to-matvec must fail. 467 * Decision modes for a supported operation are asserted here without the shared 468 * rng. t_decide, t_budgets and t_eligibility cover the same modes on the 469 * synthetic fit. */ 470 static RxCmFeatures repro_features(uint32_t op) { 471 RxCmFeatures f; 472 memset(&f, 0, sizeof f); 473 f.op = op; 474 f.M = 64; 475 f.N = 64; 476 f.state_bytes = (64ull * 64ull + 64ull + 64ull) * 8ull; 477 f.cache_bytes = 1048576ull; 478 f.core = RX_CM_CORE_X925; 479 f.eligible = 1; 480 f.need_evidence = 1; 481 return f; 482 } 483 484 static void t_refuse_unsupported_op(void) { 485 RxCostModel m; 486 rx_cm_init(&m, 1); 487 RxCmFeatures good = repro_features(RX_CM_OP_MATVEC); 488 RxCmObservation seen = { 1000000ull, 0 }; 489 for (int i = 0; i < 20; i++) 490 CHECK(rx_cm_observe(&m, &good, 0, &seen) == RX_CM_OK, "matvec observation accepted"); 491 m.explore_ps = 42; 492 m.work_ps = 99; 493 494 RxCmPrediction learned; 495 int lr = rx_cm_predict(&m, &good, 0, &learned); 496 CHECK(lr == RX_CM_OK && learned.known && !learned.refused && learned.n == 20 && 497 learned.mean_log2_ps > 19.0 && learned.mean_log2_ps < 21.0, 498 "valid matvec prediction is learned (rc %d known %d n %u mean %.6f)", lr, learned.known, 499 learned.n, learned.mean_log2_ps); 500 501 RxCmFeatures bad = good; 502 bad.op = UINT32_MAX; 503 RxCmPrediction aliased; 504 int ur = rx_cm_predict(&m, &bad, 0, &aliased); 505 RxCostModel snap = m; 506 RxCmObservation failed = { 1000000ull, 1 }; 507 RxCmObservation zero_fail = { 0, 1 }; 508 int orc = rx_cm_observe(&m, &bad, 0, &seen); 509 int frc = rx_cm_observe(&m, &bad, 0, &failed); 510 int zrc = rx_cm_observe(&m, &bad, 0, &zero_fail); 511 CHECK(ur == RX_CM_ERR_UNSUPPORTED && aliased.refused && !aliased.known && !aliased.borrowed && 512 aliased.mean_log2_ps == 0.0 && aliased.n == 0 && aliased.fail_p == 0.0 && 513 rx_cm_cell(&bad) == RX_CM_CELL_NONE && 514 orc == RX_CM_ERR_UNSUPPORTED && frc == RX_CM_ERR_UNSUPPORTED && 515 zrc == RX_CM_ERR_UNSUPPORTED && memcmp(&m, &snap, sizeof m) == 0 && 516 m.cell[0][0].n == 20 && m.cell[0][0].trials == 20 && m.cell[0][0].failures == 0, 517 "unsupported-op-not-aliased-to-matvec"); 518 519 size_t blob_n = rx_cm_blob_size(), got = 0; 520 uint8_t *blob0 = malloc(blob_n), *blob1 = malloc(blob_n); 521 CHECK(blob0 && blob1 && rx_cm_serialize(&snap, blob0, blob_n, &got) == RX_CM_OK && got == blob_n, 522 "serialize before rejected observations"); 523 CHECK(blob0 && blob1 && rx_cm_serialize(&m, blob1, blob_n, &got) == RX_CM_OK && 524 memcmp(blob0, blob1, blob_n) == 0, 525 "frozen model serialization identical after rejected observations"); 526 free(blob0); 527 free(blob1); 528 529 bad.op = RX_CM_OPS; 530 RxCmPrediction first; 531 int fr = rx_cm_predict(&m, &bad, 0, &first); 532 snap = m; 533 orc = rx_cm_observe(&m, &bad, 0, &seen); 534 frc = rx_cm_observe(&m, &bad, 0, &failed); 535 CHECK(fr == RX_CM_ERR_UNSUPPORTED && first.refused && !first.known && 536 rx_cm_cell(&bad) == RX_CM_CELL_NONE && orc == RX_CM_ERR_UNSUPPORTED && 537 frc == RX_CM_ERR_UNSUPPORTED && memcmp(&m, &snap, sizeof m) == 0, 538 "first unsupported id RX_CM_OPS is refused"); 539 540 /* A supported observation still trains matvec after the refusals. */ 541 CHECK(rx_cm_observe(&m, &good, 0, &seen) == RX_CM_OK && m.cell[0][0].n == 21, 542 "supported matvec observation still trains"); 543 544 RxCmPolicy pol; 545 rx_cm_default_policy(&pol); 546 RxCmDecision d; 547 bad.op = UINT32_MAX; 548 rx_cm_decide(&m, &pol, &bad, 1, &d); 549 CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP && d.arm == m.n_arms && 550 d.measure[0] == m.n_arms && d.measure[1] == m.n_arms && d.expected_improvement == 0.0 && 551 d.pred[0].refused && !d.pred[0].known && !d.pred[0].borrowed && 552 d.pred[0].mean_log2_ps == 0.0, 553 "decide refuses UINT32_MAX without ranking"); 554 rx_cm_decide(&m, &pol, &bad, 0, &d); 555 CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP, 556 "frozen decide refuses UINT32_MAX"); 557 bad.op = RX_CM_OPS; 558 rx_cm_decide(&m, &pol, &bad, 1, &d); 559 CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP && d.pred[0].refused, 560 "decide refuses RX_CM_OPS"); 561 562 /* Pressure on a real matvec is still a residual. Unsupported pressure is not. */ 563 RxCostModel quiet; 564 rx_cm_init(&quiet, 1); 565 RxCmFeatures qf = repro_features(RX_CM_OP_MATVEC); 566 RxCmObservation exact = { (uint64_t)exp2(20.0), 0 }; 567 for (int i = 0; i < 16; i++) rx_cm_observe(&quiet, &qf, 0, &exact); 568 RxCmFeatures unseen = qf; 569 unseen.pressure = 2; 570 RxCmPrediction pq, p0; 571 rx_cm_predict(&quiet, &qf, 0, &p0); 572 rx_cm_predict(&quiet, &unseen, 0, &pq); 573 CHECK(pq.borrowed && !pq.refused && pq.mean_log2_ps == p0.mean_log2_ps, 574 "unseen matvec pressure borrows the quiet prediction"); 575 RxCmFeatures pressed = qf; 576 pressed.pressure = 1; 577 RxCostModel bare; 578 rx_cm_init(&bare, 1); 579 CHECK(rx_cm_observe(&bare, &pressed, 0, &exact) == RX_CM_SKIPPED && 580 bare.cell[rx_cm_cell(&pressed)][0].trials == 1 && 581 bare.cell[rx_cm_cell(&pressed)][0].n == 0, 582 "valid matvec pressure without a quiet model is skipped"); 583 pressed.op = RX_CM_OPS; 584 RxCostModel bare_snap = bare; 585 CHECK(rx_cm_observe(&bare, &pressed, 0, &exact) == RX_CM_ERR_UNSUPPORTED && 586 memcmp(&bare, &bare_snap, sizeof bare) == 0, 587 "unsupported op under pressure is refused, not skipped into a bucket"); 588 pressed = qf; 589 pressed.pressure = 1; 590 RxCmObservation slow = { (uint64_t)exp2(21.0), 0 }; 591 for (int i = 0; i < 6; i++) 592 CHECK(rx_cm_observe(&quiet, &pressed, 0, &slow) == RX_CM_OK, "pressure residual accepted"); 593 uint64_t residual_n = quiet.cell[rx_cm_cell(&pressed)][0].n; 594 CHECK(residual_n == 6, "pressure residual has its own samples"); 595 rx_cm_predict(&quiet, &pressed, 0, &pq); 596 CHECK(pq.known && !pq.borrowed && !pq.refused && fabs(pq.mean_log2_ps - (p0.mean_log2_ps + 1.0)) < 0.25, 597 "pressure correction learned (%.3f vs %.3f)", pq.mean_log2_ps, p0.mean_log2_ps + 1.0); 598 pressed.op = UINT32_MAX; 599 snap = quiet; 600 CHECK(rx_cm_observe(&quiet, &pressed, 0, &slow) == RX_CM_ERR_UNSUPPORTED && 601 memcmp(&quiet, &snap, sizeof quiet) == 0, 602 "unsupported op does not train the pressure residual"); 603 604 /* OTHER core and a saturated pressure bucket stay real cells. */ 605 RxCostModel other_m; 606 rx_cm_init(&other_m, 1); 607 RxCmFeatures other = qf; 608 other.core = 99; 609 other.pressure = 0; 610 uint32_t oc = rx_cm_cell(&other); 611 CHECK(oc < RX_CM_CELLS && oc != rx_cm_cell(&qf), "OTHER core is a real cell"); 612 CHECK(rx_cm_observe(&other_m, &other, 0, &exact) == RX_CM_OK && other_m.cell[oc][0].n == 1 && 613 other_m.cell[0][0].n == 0, 614 "OTHER core observation stays out of the matvec X925 cell"); 615 other.pressure = 99; 616 uint32_t pc = rx_cm_cell(&other); 617 CHECK(pc < RX_CM_CELLS && pc != oc && pc != RX_CM_CELL_NONE, 618 "saturated pressure on OTHER is its own bucket"); 619 620 /* Each decision mode on a supported matvec, then refusal beside them. */ 621 RxCostModel empty; 622 rx_cm_init(&empty, 2); 623 RxCmFeatures g = feat(16, 16, RX_CM_CORE_X925, 0); 624 g.eligible = 0x3; 625 rx_cm_decide(&empty, &pol, &g, 1, &d); 626 CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_UNKNOWN_ARM, "empty model measures"); 627 rx_cm_decide(&empty, &pol, &g, 0, &d); 628 CHECK(d.action == RX_CM_FALLBACK && d.arm == 0 && d.why == RX_CM_WHY_UNKNOWN_ARM, 629 "empty frozen model falls back"); 630 RxCostModel one; 631 rx_cm_init(&one, 1); 632 rx_cm_decide(&one, &pol, &g, 1, &d); 633 CHECK(d.action == RX_CM_FALLBACK && d.why == RX_CM_WHY_NO_ELIGIBLE, "only the fallback is eligible"); 634 635 RxCostModel tight; 636 rx_cm_init(&tight, 2); 637 RxCmObservation hi = { (uint64_t)exp2(22.0), 0 }; 638 for (int i = 0; i < 40; i++) { 639 rx_cm_observe(&tight, &g, 0, &exact); 640 rx_cm_observe(&tight, &g, 1, &hi); 641 } 642 rx_cm_decide(&tight, &pol, &g, 1, &d); 643 CHECK(d.action == RX_CM_SELECT && d.arm == 0 && d.why == RX_CM_WHY_CONFIDENT, 644 "separated arms select confidently (action %d arm %u why %u)", d.action, d.arm, d.why); 645 g.latency_budget_ps = 1000; 646 rx_cm_decide(&tight, &pol, &g, 0, &d); 647 CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_OVER_BUDGET, "no arm fits the latency budget"); 648 g.latency_budget_ps = 0; 649 650 RxCostModel equal; 651 rx_cm_init(&equal, 2); 652 for (int i = 0; i < 40; i++) { 653 rx_cm_observe(&equal, &g, 0, &exact); 654 rx_cm_observe(&equal, &g, 1, &exact); 655 } 656 rx_cm_decide(&equal, &pol, &g, 1, &d); 657 CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_CONFIDENT, 658 "equal and tight: not worth a run (action %d ei %.3f)", d.action, d.expected_improvement); 659 RxCmFeatures gp = g; 660 gp.pressure = 1; 661 rx_cm_decide(&equal, &pol, &gp, 1, &d); 662 CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_WORTH_MEASURING, 663 "unseen pressure is worth measuring (action %d why %u ei %.3f)", d.action, d.why, 664 d.expected_improvement); 665 rx_cm_charge(&equal, 0, pol.explore_allow_ps + 1); 666 rx_cm_decide(&equal, &pol, &gp, 1, &d); 667 CHECK(d.action == RX_CM_SELECT && d.why == RX_CM_WHY_BUDGET_SPENT, 668 "spent exploration budget selects (action %d why %u)", d.action, d.why); 669 rx_cm_charge(&equal, (uint64_t)((double)(pol.explore_allow_ps + 1) / pol.explore_frac) * 2, 0); 670 rx_cm_decide(&equal, &pol, &gp, 1, &d); 671 CHECK(d.action == RX_CM_MEASURE && d.why == RX_CM_WHY_WORTH_MEASURING, 672 "budget refills as chosen work accrues"); 673 gp.op = RX_CM_OPS; 674 rx_cm_decide(&equal, &pol, &gp, 1, &d); 675 CHECK(d.action == RX_CM_REFUSE && d.why == RX_CM_WHY_UNSUPPORTED_OP && d.pred[0].refused && 676 d.pred[1].refused && !d.pred[0].known && !d.pred[1].borrowed, 677 "unsupported op is not ranked in any decision mode"); 678 679 RxCmFeatures cal[16]; 680 uint32_t carm[16]; 681 uint64_t cps[16]; 682 for (int i = 0; i < 16; i++) { 683 cal[i] = repro_features(i & 1 ? UINT32_MAX : RX_CM_OPS); 684 carm[i] = 0; 685 cps[i] = (uint64_t)exp2(30.0); 686 } 687 double scale_before = equal.sd_scale; 688 double scale = rx_cm_calibrate(&equal, cal, carm, cps, 16, 0.80); 689 CHECK(scale == 0.0 && equal.sd_scale == scale_before, 690 "unsupported calibration points do not change the scale"); 691 } 692 693 int main(int argc, char **argv) { 694 int only_refuse = argc > 1 && strcmp(argv[1], "refuse") == 0; 695 if (!only_refuse) { 696 double c80, c95, rmse; 697 t_basis(); 698 t_fit_and_calibration(1, &c80, &c95, &rmse); 699 t_fit_and_calibration(0, &c80, &c95, &rmse); 700 t_decide(); 701 t_eligibility_property(); 702 t_budgets(); 703 t_calibrate(); 704 t_blob(); 705 } 706 t_refuse_unsupported_op(); 707 printf("checks %d failures %d\n", g_checks, g_fail); 708 return g_fail ? 1 : 0; 709 }