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 / native / orig4 / shadow.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note omega-mv#731 ORIG-4: default-off shadow advice for the matvec arm chooser |
| 3 | note (spec/shadow-advice-v0.md). Unit after the pinned units add32 mul32 metadata affine |
| 4 | note fixture world omexp omexpfix select (native/orig4/orig4.pin); the program is this unit |
| 5 | note plus shadowmain.om (fn main), the tests this unit plus tests/orig4/shadow-test.om. |
| 6 | note Modes (first stdin byte, dispatched by shadowmain.om): S shadow, G fixture. |
| 7 | note S: config 64 bytes (8 LE words: 0 enable, 1 scoring budget in records (<= 2048), |
| 8 | note 2 image length in the stream (1..32768; select.om's slload decides admissibility), |
| 9 | note 3 admission version (0 v1 oxload, 1 v2 oxadm2: ORIG1-CONTRACT-v2.md; with advisor kind |
| 10 | note 0 the image's v2 flag SS[20] must equal it, else FALLBACK all reason=image-9), 4 advisor |
| 11 | note kind (0 the image's scorer, 1 retrieval: the v2/v1 nearest-TRAIN-record rule of |
| 12 | note select.om slnear, used to measure its per-decision overhead; the image is admitted |
| 13 | note but not consulted), 5..7 reserved 0), the allow-list block (4096 bytes), the |
| 14 | note checkpoint image, then omexp-v1 records to end of input (<= 524288 bytes). |
| 15 | note Admission in order: enable word (anything but 1 prints SHADOW disabled, exit 0) -> |
| 16 | note allow-list status of the records' FNV-1a 64 (oxallow) -> record admission (oxload) -> |
| 17 | note checkpoint admission (sladmit: every slload check except the training-dataset binding; |
| 18 | note slbound is printed as bound_matches=yes|no and is information, not a fallback reason: |
| 19 | note the advisor by design scores records the model never trained on) -> per record: past the |
| 20 | note budget, out-of-distribution state (core class 2 or pressure above 1), fewer than two |
| 21 | note admissible arms, any non-finite score -> FALLBACK; otherwise ADVICE with the argmin |
| 22 | note score among the admissible arms (lowest index on a tie) under the best-on-DEV params. |
| 23 | note The advisor writes only to fd 1, never reads past the end of input, and never widens |
| 24 | note the candidate set: hvscore ranks only arms for which oxcand holds. |
| 25 | note Exit: 0 (also for a refused image), 64 bad mode, 66 records refused, 67 unauthorized |
| 26 | note or revoked, 68 config invalid, 74 I/O. |
| 27 | note G: 16 bytes (seed, count <= 2048); the SYNTHETIC records of sxgen go to fd 1 raw. |
| 28 | note H: the record bytes to end of input; their FNV-1a 64 in decimal on fd 1 (the glue |
| 29 | note authorizes a mutated fixture through it for the record-refusal runs). |
| 30 | note HV: 0 score bits of the last hvscore, 1 scored, 2 per-record fallbacks, 3 advised |
| 31 | note cost, 4 oracle cost, 5 incumbent cost (ps per call, scored records only), 6 budget, |
| 32 | note 7 advisor kind. A v2-refused record (oxpart 3) falls back with reason=refused. |
| 33 | global HM uchar 1 |
| 34 | global HK uchar 64 |
| 35 | global HV ulong 8 |
| 36 | text HNL "\n" |
| 37 | text HSDIS "SHADOW disabled" |
| 38 | text HSREC "SHADOW records=" |
| 39 | text HSDF " data_fnv=" |
| 40 | text HSAL " allow=" |
| 41 | text HSIMG "SHADOW image_fnv=" |
| 42 | text HSKI " kind=" |
| 43 | text HSNP " nparams=" |
| 44 | text HSDB " data_bound=" |
| 45 | text HSBM " bound_matches=" |
| 46 | text HSYES "yes" |
| 47 | text HSNO "no" |
| 48 | text HFREC "FALLBACK all reason=records-" |
| 49 | text HFALW "FALLBACK all reason=allow-" |
| 50 | text HFIMG "FALLBACK all reason=image-" |
| 51 | text HFSEQ "FALLBACK seq=" |
| 52 | text HREAS " reason=" |
| 53 | text HROOD "ood" |
| 54 | text HRSIN "single" |
| 55 | text HRNON "nonfinite" |
| 56 | text HRBUD "budget" |
| 57 | text HRREF "refused" |
| 58 | text HSV2 "SHADOW admission=v2 admitted=" |
| 59 | text HSKD " advisor=" |
| 60 | text HASEQ "ADVICE seq=" |
| 61 | text HAARM " arm=" |
| 62 | text HAINC " incumbent=" |
| 63 | text HAORA " oracle=" |
| 64 | text HASCO " score=" |
| 65 | text HCREC "COST records=" |
| 66 | text HCSCO " scored=" |
| 67 | text HCFB " fallback=" |
| 68 | text HCADV " adv_cost=" |
| 69 | text HCORA " oracle_cost=" |
| 70 | text HCINC " incumbent_cost=" |
| 71 | |
| 72 | fn hvnl to void |
| 73 | w = write 1 HNL 0 1 |
| 74 | end |
| 75 | |
| 76 | fn hkw i ulong to ulong |
| 77 | note Config word i (little-endian) from HK. |
| 78 | v = ulong 0 |
| 79 | for k ulong 8 |
| 80 | v = or v shl ulong get HK add mul i 8 k mul k 8 |
| 81 | end |
| 82 | ret v |
| 83 | end |
| 84 | |
| 85 | fn hvrdk n ulong to ulong |
| 86 | note Up to n bytes into HK[0..n); returns the count read. |
| 87 | o = ulong 0 |
| 88 | while lt o n bound 64 |
| 89 | r = read 0 HK o sub n o |
| 90 | if lt r 0 |
| 91 | exit 74 |
| 92 | end |
| 93 | if eq r 0 |
| 94 | ret o |
| 95 | end |
| 96 | o = add o ulong r |
| 97 | end |
| 98 | ret o |
| 99 | end |
| 100 | |
| 101 | fn hvrdlb to ulong |
| 102 | note The allow-list block: 4096 bytes, zero padded; returns the used length. |
| 103 | o = ulong 0 |
| 104 | while lt o 4096 bound 4096 |
| 105 | r = read 0 LB o sub 4096 o |
| 106 | if lt r 0 |
| 107 | exit 74 |
| 108 | end |
| 109 | if eq r 0 |
| 110 | ret o |
| 111 | end |
| 112 | o = add o ulong r |
| 113 | end |
| 114 | n = ulong 0 |
| 115 | for i ulong 4096 |
| 116 | if ne get LB i 0 |
| 117 | n = add i 1 |
| 118 | end |
| 119 | end |
| 120 | ret n |
| 121 | end |
| 122 | |
| 123 | fn hvrdimg n ulong to ulong |
| 124 | note Up to n image bytes into SI[0..n); never reads past them. |
| 125 | o = ulong 0 |
| 126 | while lt o n bound 32768 |
| 127 | r = read 0 SI o sub n o |
| 128 | if lt r 0 |
| 129 | exit 74 |
| 130 | end |
| 131 | if eq r 0 |
| 132 | ret o |
| 133 | end |
| 134 | o = add o ulong r |
| 135 | end |
| 136 | ret o |
| 137 | end |
| 138 | |
| 139 | fn hvrdrb to ulong |
| 140 | note Records to end of input into RB; 524289 means more than the capacity arrived. |
| 141 | o = ulong 0 |
| 142 | while lt o 524288 bound 999999 |
| 143 | r = read 0 RB o sub 524288 o |
| 144 | if lt r 0 |
| 145 | exit 74 |
| 146 | end |
| 147 | if eq r 0 |
| 148 | ret o |
| 149 | end |
| 150 | o = add o ulong r |
| 151 | end |
| 152 | r = read 0 HM 0 1 |
| 153 | if ne r 0 |
| 154 | ret 524289 |
| 155 | end |
| 156 | ret o |
| 157 | end |
| 158 | |
| 159 | fn hvwrb n ulong to void |
| 160 | o = ulong 0 |
| 161 | while lt o n bound 999999 |
| 162 | w = write 1 RB o sub n o |
| 163 | if le w 0 |
| 164 | exit 74 |
| 165 | end |
| 166 | o = add o ulong w |
| 167 | end |
| 168 | end |
| 169 | |
| 170 | fn hvood r ulong to bool |
| 171 | note Never in training: core class 2 or a pressure bucket above 1. |
| 172 | ret or eq oxcore r 2 gt oxpress r 1 |
| 173 | end |
| 174 | |
| 175 | fn hvnarms r ulong to ulong |
| 176 | note Admissible arms of record r (oxcand: eligible, measured, verified). |
| 177 | c = ulong 0 |
| 178 | for a ulong 8 |
| 179 | if oxcand r a |
| 180 | c = add c 1 |
| 181 | end |
| 182 | end |
| 183 | ret c |
| 184 | end |
| 185 | |
| 186 | fn hvbudget r ulong to bool |
| 187 | note Record index r is past the declared scoring budget HV[6]. |
| 188 | ret ge r get HV 6 |
| 189 | end |
| 190 | |
| 191 | fn hvscore r ulong to long |
| 192 | note The advice: argmin score over the admissible arms under the current params (lowest |
| 193 | note index on a tie); HV[0] keeps the winning score bits. Negative: -1 out of distribution, |
| 194 | note -2 fewer than two admissible arms, -3 a non-finite score among the admissible arms. |
| 195 | if hvood r |
| 196 | ret -1 |
| 197 | end |
| 198 | if lt hvnarms r 2 |
| 199 | ret -2 |
| 200 | end |
| 201 | best = ulong 8 |
| 202 | if eq get HV 7 1 |
| 203 | put HV 0 0 |
| 204 | ret long slnear r |
| 205 | end |
| 206 | note Seed from the lowest admissible arm, never from arm 0 unchecked (oxchk2 and oxpair2 |
| 207 | note make arm 0 admissible today; this loop does not rely on it). |
| 208 | bs = ulong 0 |
| 209 | ok = true |
| 210 | for a ulong 8 |
| 211 | if oxcand r a |
| 212 | s = slfwd r a |
| 213 | if not affinite s |
| 214 | ok = false |
| 215 | end |
| 216 | if or eq best 8 sllt s bs |
| 217 | bs = s |
| 218 | best = a |
| 219 | end |
| 220 | end |
| 221 | end |
| 222 | put HV 0 bs |
| 223 | if not ok |
| 224 | ret -3 |
| 225 | end |
| 226 | ret long best |
| 227 | end |
| 228 | |
| 229 | fn hvimage n ulong to int |
| 230 | note Checkpoint admission through select.om's sladmit: every image check except the |
| 231 | note training-dataset binding (0 admitted, state installed; negative refused, nothing |
| 232 | note installed). The binding is information (slbound, printed as bound_matches), never a |
| 233 | note refusal: the advisor by design scores records the model never trained on. |
| 234 | ret sladmit 0 n |
| 235 | end |
| 236 | |
| 237 | fn hvcfgok to bool |
| 238 | note budget <= 2048, image length 1..32768 (the SI capacity), admission version and |
| 239 | note advisor kind 0 or 1, reserved words zero. |
| 240 | if gt hkw 1 2048 |
| 241 | ret false |
| 242 | end |
| 243 | if or eq hkw 2 0 gt hkw 2 32768 |
| 244 | ret false |
| 245 | end |
| 246 | if or gt hkw 3 1 gt hkw 4 1 |
| 247 | ret false |
| 248 | end |
| 249 | for i ulong 3 |
| 250 | if ne hkw add 5 i 0 |
| 251 | ret false |
| 252 | end |
| 253 | end |
| 254 | ret true |
| 255 | end |
| 256 | |
| 257 | fn hvload n ulong to int |
| 258 | note Record admission by the config's version word: 1 oxadm2 (v2), otherwise oxload (v1). |
| 259 | if eq hkw 3 1 |
| 260 | ret oxadm2 n |
| 261 | end |
| 262 | ret oxload n |
| 263 | end |
| 264 | |
| 265 | fn hvadmit n ulong lbn ulong to int |
| 266 | note Allow-list status (67) then record admission (66); 0 when admitted, with the line |
| 267 | note SHADOW records=N data_fnv=H allow=S. |
| 268 | if gt n 524288 |
| 269 | w = write 1 HFREC 0 len HFREC |
| 270 | wnum 1 |
| 271 | hvnl |
| 272 | ret 66 |
| 273 | end |
| 274 | st = oxallow lbn oxfnv n |
| 275 | if lt st 0 |
| 276 | w = write 1 HFALW 0 len HFALW |
| 277 | wnum ulong neg long st |
| 278 | hvnl |
| 279 | ret 67 |
| 280 | end |
| 281 | ld = hvload n |
| 282 | if ne ld 0 |
| 283 | w = write 1 HFREC 0 len HFREC |
| 284 | wnum ulong neg long ld |
| 285 | hvnl |
| 286 | ret 66 |
| 287 | end |
| 288 | w = write 1 HSREC 0 len HSREC |
| 289 | wnum get RC 0 |
| 290 | w = write 1 HSDF 0 len HSDF |
| 291 | wnum get RC 2 |
| 292 | w = write 1 HSAL 0 len HSAL |
| 293 | wnum ulong st |
| 294 | hvnl |
| 295 | if eq hkw 3 1 |
| 296 | w = write 1 HSV2 0 len HSV2 |
| 297 | wnum get RC 10 |
| 298 | w = write 1 HSKD 0 len HSKD |
| 299 | wnum get HV 7 |
| 300 | hvnl |
| 301 | end |
| 302 | ret 0 |
| 303 | end |
| 304 | |
| 305 | fn hvimg n ulong to int |
| 306 | note Checkpoint admission with its SHADOW line; 1 when refused (FALLBACK all printed). |
| 307 | st = hvimage n |
| 308 | if and ge st 0 and eq get HV 7 0 ne get SS 20 hkw 3 |
| 309 | st = -9 |
| 310 | end |
| 311 | if lt st 0 |
| 312 | w = write 1 HFIMG 0 len HFIMG |
| 313 | wnum ulong neg long st |
| 314 | hvnl |
| 315 | ret 1 |
| 316 | end |
| 317 | w = write 1 HSIMG 0 len HSIMG |
| 318 | wnum siget sub n 8 |
| 319 | w = write 1 HSKI 0 len HSKI |
| 320 | wnum get SS 0 |
| 321 | w = write 1 HSNP 0 len HSNP |
| 322 | wnum get SS 15 |
| 323 | w = write 1 HSDB 0 len HSDB |
| 324 | wnum get SS 11 |
| 325 | w = write 1 HSBM 0 len HSBM |
| 326 | if slbound 0 |
| 327 | w = write 1 HSYES 0 len HSYES |
| 328 | else |
| 329 | w = write 1 HSNO 0 len HSNO |
| 330 | end |
| 331 | hvnl |
| 332 | ret 0 |
| 333 | end |
| 334 | |
| 335 | fn hvfb r ulong code long to void |
| 336 | note FALLBACK seq=S reason=... for one record; counts it. |
| 337 | w = write 1 HFSEQ 0 len HFSEQ |
| 338 | wnum oxseq r |
| 339 | w = write 1 HREAS 0 len HREAS |
| 340 | if eq code -1 |
| 341 | w = write 1 HROOD 0 len HROOD |
| 342 | end |
| 343 | if eq code -2 |
| 344 | w = write 1 HRSIN 0 len HRSIN |
| 345 | end |
| 346 | if eq code -3 |
| 347 | w = write 1 HRNON 0 len HRNON |
| 348 | end |
| 349 | if eq code -4 |
| 350 | w = write 1 HRBUD 0 len HRBUD |
| 351 | end |
| 352 | if eq code -5 |
| 353 | w = write 1 HRREF 0 len HRREF |
| 354 | end |
| 355 | hvnl |
| 356 | put HV 2 add get HV 2 1 |
| 357 | end |
| 358 | |
| 359 | fn hvadv r ulong a ulong to void |
| 360 | note ADVICE seq=S arm=A incumbent=R oracle=O score=BITS; cost sums over scored records. |
| 361 | w = write 1 HASEQ 0 len HASEQ |
| 362 | wnum oxseq r |
| 363 | w = write 1 HAARM 0 len HAARM |
| 364 | wnum a |
| 365 | w = write 1 HAINC 0 len HAINC |
| 366 | wnum oxrule r |
| 367 | w = write 1 HAORA 0 len HAORA |
| 368 | wnum oxbest r |
| 369 | w = write 1 HASCO 0 len HASCO |
| 370 | wnum get HV 0 |
| 371 | hvnl |
| 372 | put HV 1 add get HV 1 1 |
| 373 | put HV 3 add get HV 3 oxcost r a |
| 374 | put HV 4 add get HV 4 oxcost r oxbest r |
| 375 | put HV 5 add get HV 5 oxcost r oxrule r |
| 376 | end |
| 377 | |
| 378 | fn hvrec r ulong to void |
| 379 | if hvbudget r |
| 380 | hvfb r -4 |
| 381 | ret |
| 382 | end |
| 383 | if eq oxpart r 3 |
| 384 | hvfb r -5 |
| 385 | ret |
| 386 | end |
| 387 | c = hvscore r |
| 388 | if lt c 0 |
| 389 | hvfb r c |
| 390 | ret |
| 391 | end |
| 392 | hvadv r ulong c |
| 393 | end |
| 394 | |
| 395 | fn hvcost to void |
| 396 | w = write 1 HCREC 0 len HCREC |
| 397 | wnum get RC 0 |
| 398 | w = write 1 HCSCO 0 len HCSCO |
| 399 | wnum get HV 1 |
| 400 | w = write 1 HCFB 0 len HCFB |
| 401 | wnum get HV 2 |
| 402 | w = write 1 HCADV 0 len HCADV |
| 403 | wnum get HV 3 |
| 404 | w = write 1 HCORA 0 len HCORA |
| 405 | wnum get HV 4 |
| 406 | w = write 1 HCINC 0 len HCINC |
| 407 | wnum get HV 5 |
| 408 | hvnl |
| 409 | end |
| 410 | |
| 411 | fn hvrun to int |
| 412 | note S: read the stream in admission order, then advise record by record. |
| 413 | if ne hvrdk 64 64 |
| 414 | ret 68 |
| 415 | end |
| 416 | if ne hkw 0 1 |
| 417 | w = write 1 HSDIS 0 len HSDIS |
| 418 | hvnl |
| 419 | ret 0 |
| 420 | end |
| 421 | if not hvcfgok |
| 422 | ret 68 |
| 423 | end |
| 424 | for i ulong 8 |
| 425 | put HV i 0 |
| 426 | end |
| 427 | put HV 6 hkw 1 |
| 428 | put HV 7 hkw 4 |
| 429 | lbn = hvrdlb |
| 430 | img = hkw 2 |
| 431 | if ne hvrdimg img img |
| 432 | w = write 1 HFIMG 0 len HFIMG |
| 433 | wnum 1 |
| 434 | hvnl |
| 435 | ret 0 |
| 436 | end |
| 437 | n = hvrdrb |
| 438 | st = hvadmit n lbn |
| 439 | if ne st 0 |
| 440 | ret st |
| 441 | end |
| 442 | if ne hvimg img 0 |
| 443 | ret 0 |
| 444 | end |
| 445 | slbest |
| 446 | for r ulong get RC 0 |
| 447 | hvrec r |
| 448 | end |
| 449 | hvcost |
| 450 | ret 0 |
| 451 | end |
| 452 | |
| 453 | fn hvgen to int |
| 454 | if ne hvrdk 16 16 |
| 455 | ret 68 |
| 456 | end |
| 457 | if or eq hkw 1 0 gt hkw 1 2048 |
| 458 | ret 68 |
| 459 | end |
| 460 | hvwrb sxgen hkw 0 hkw 1 |
| 461 | ret 0 |
| 462 | end |
| 463 | |
| 464 | fn hvhash to int |
| 465 | note H: the FNV-1a 64 of the record bytes on stdin (the allow-list identity), decimal. |
| 466 | n = hvrdrb |
| 467 | if gt n 524288 |
| 468 | ret 66 |
| 469 | end |
| 470 | wnum oxfnv n |
| 471 | hvnl |
| 472 | ret 0 |
| 473 | end |