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 / bench / inference_cost / scorer.om
| 1 | note bench/inference_cost/scorer.om -- inference-cost scorer in native .om |
| 2 | note Reads line-oriented plain-text attempt records from stdin (fd 0) |
| 3 | note and emits the evaluated benchmark summary to stdout (fd 1). |
| 4 | note |
| 5 | note Wire version 1 (one language, one arm): |
| 6 | note version 1 / attempts N / N rows "<task> <attempt> <first> <comp_rc> <verified> |
| 7 | note <wall_ms> <prompt_tok> <completion_tok> <total_tok>" |
| 8 | note Wire version 2 (per language and arm): |
| 9 | note version 2 / attempts N / N rows "<task> <lang> <arm> <attempt> <first> <comp_rc> |
| 10 | note <verified> <wall_ms> <prompt_tok> <completion_tok> <total_tok>", lang one of |
| 11 | note om c py rs, arm plain or feedback. Version 2 reports one block per (lang, arm) |
| 12 | note present, in that order, and adds tokens_per_verified (none when nothing verified). |
| 13 | note A group PASSes when every task in it verified (verified_count = tasks_total > 0); |
| 14 | note exit 0 when every group passes, 1 otherwise, 1 for empty input, 2 for truncated or |
| 15 | note malformed rows (or 983040 bytes of input or more), 3 for an unknown version. |
| 16 | note Compile as om2: { printf 'om2\n'; cat scorer.om; } | omc |
| 17 | |
| 18 | global inbuf uchar 983040 |
| 19 | global obuf uchar 64 |
| 20 | |
| 21 | global pos ulong 1 |
| 22 | global inlen ulong 1 |
| 23 | global tokb ulong 1 |
| 24 | note 8 groups (lang * 2 + arm) of 12 counters: 0 tasks (first rows), 1 compiled, |
| 25 | note 2 verified, 3 first-attempt verified, 4..6 prompt/completion/total tokens, |
| 26 | note 7 wall ms, 8 rows, 9 present |
| 27 | global stats ulong 96 |
| 28 | note Exact task identity per group. Open-addressed entries point into immutable inbuf. |
| 29 | note A row has at least 18 bytes, so the input byte limit cannot fill 65536 entries. |
| 30 | global taskbeg ulong 65536 |
| 31 | global tasklen ulong 65536 |
| 32 | global taskgrp ulong 65536 |
| 33 | global taskatt ulong 65536 |
| 34 | global taskver ulong 65536 |
| 35 | global rowbeg ulong 1 |
| 36 | global rowlen ulong 1 |
| 37 | |
| 38 | fn read_task to void |
| 39 | read_tok |
| 40 | put rowbeg 0 get tokb 0 |
| 41 | put rowlen 0 sub get pos 0 get tokb 0 |
| 42 | end |
| 43 | |
| 44 | fn same_task slot ulong b ulong to bool |
| 45 | if ne get taskgrp slot b |
| 46 | ret false |
| 47 | end |
| 48 | if ne get tasklen slot get rowlen 0 |
| 49 | ret false |
| 50 | end |
| 51 | start = get taskbeg slot |
| 52 | other = get rowbeg 0 |
| 53 | for i ulong get rowlen 0 |
| 54 | left = get inbuf add start i |
| 55 | right = get inbuf add other i |
| 56 | if ne left right |
| 57 | ret false |
| 58 | end |
| 59 | end |
| 60 | ret true |
| 61 | end |
| 62 | |
| 63 | fn task_slot b ulong to ulong |
| 64 | h = b |
| 65 | start = get rowbeg 0 |
| 66 | for i ulong get rowlen 0 |
| 67 | c = ulong get inbuf add start i |
| 68 | mix = add h shl h 5 |
| 69 | h = rem xor mix c 65536 |
| 70 | end |
| 71 | for i ulong 65536 |
| 72 | if eq get tasklen h 0 |
| 73 | put taskbeg h get rowbeg 0 |
| 74 | put tasklen h get rowlen 0 |
| 75 | put taskgrp h b |
| 76 | ret h |
| 77 | end |
| 78 | if same_task h b |
| 79 | ret h |
| 80 | end |
| 81 | h = rem add h 1 65536 |
| 82 | end |
| 83 | fail 2 |
| 84 | ret 0 |
| 85 | end |
| 86 | |
| 87 | fn record_task b ulong att ulong first ulong comp ulong ver ulong to void |
| 88 | if or gt first 1 gt ver 1 |
| 89 | fail 2 |
| 90 | end |
| 91 | if and ne comp 0 eq ver 1 |
| 92 | fail 2 |
| 93 | end |
| 94 | s = task_slot b |
| 95 | prev = get taskatt s |
| 96 | if ne att add prev 1 |
| 97 | fail 2 |
| 98 | end |
| 99 | if ne (eq first 1) (eq prev 0) |
| 100 | fail 2 |
| 101 | end |
| 102 | if eq get taskver s 1 |
| 103 | fail 2 |
| 104 | end |
| 105 | if eq prev 0 |
| 106 | bump b 1 |
| 107 | end |
| 108 | put taskatt s att |
| 109 | if eq ver 1 |
| 110 | put taskver s 1 |
| 111 | bump add b 2 1 |
| 112 | if eq first 1 |
| 113 | bump add b 3 1 |
| 114 | end |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | fn require_eof to void |
| 119 | skip_ws |
| 120 | if ne get pos 0 get inlen 0 |
| 121 | fail 2 |
| 122 | end |
| 123 | end |
| 124 | |
| 125 | text err "scorer: error\n" |
| 126 | fn fail code int to void |
| 127 | w = write 2 err 0 len err |
| 128 | exit code |
| 129 | end |
| 130 | |
| 131 | fn is_space c uchar to bool |
| 132 | if or eq c 32 eq c 10 |
| 133 | ret true |
| 134 | end |
| 135 | if or eq c 9 eq c 13 |
| 136 | ret true |
| 137 | end |
| 138 | ret false |
| 139 | end |
| 140 | |
| 141 | fn is_digit c uchar to bool |
| 142 | if and ge c 48 le c 57 |
| 143 | ret true |
| 144 | end |
| 145 | ret false |
| 146 | end |
| 147 | |
| 148 | fn read_all to ulong |
| 149 | p = ulong 0 |
| 150 | note every pass adds at least one byte or leaves the loop |
| 151 | while lt p 983040 bound 999999 |
| 152 | k = read 0 inbuf p sub 983040 p |
| 153 | if lt k 0 |
| 154 | fail 1 |
| 155 | end |
| 156 | if eq k 0 |
| 157 | break |
| 158 | end |
| 159 | p = add p ulong k |
| 160 | end |
| 161 | if eq p 983040 |
| 162 | fail 2 |
| 163 | end |
| 164 | ret p |
| 165 | end |
| 166 | |
| 167 | fn skip_ws to void |
| 168 | p = get pos 0 |
| 169 | lim = get inlen 0 |
| 170 | while and lt p lim (is_space (get inbuf p)) |
| 171 | p = add p 1 |
| 172 | end |
| 173 | put pos 0 p |
| 174 | end |
| 175 | |
| 176 | fn read_uint to ulong |
| 177 | skip_ws |
| 178 | p = get pos 0 |
| 179 | lim = get inlen 0 |
| 180 | if ge p lim |
| 181 | fail 2 |
| 182 | end |
| 183 | c = get inbuf p |
| 184 | if not (is_digit c) |
| 185 | fail 2 |
| 186 | end |
| 187 | val = ulong 0 |
| 188 | while and lt p lim (is_digit c) |
| 189 | val = add (mul val 10) (ulong sub c 48) |
| 190 | p = add p 1 |
| 191 | if lt p lim |
| 192 | c = get inbuf p |
| 193 | end |
| 194 | end |
| 195 | put pos 0 p |
| 196 | ret val |
| 197 | end |
| 198 | |
| 199 | fn skip_token to void |
| 200 | skip_ws |
| 201 | p = get pos 0 |
| 202 | lim = get inlen 0 |
| 203 | while and lt p lim (not (is_space (get inbuf p))) |
| 204 | p = add p 1 |
| 205 | end |
| 206 | put pos 0 p |
| 207 | end |
| 208 | |
| 209 | note read_tok: the next token is inbuf[tokb .. pos); a missing token is truncated input |
| 210 | fn read_tok to void |
| 211 | skip_ws |
| 212 | put tokb 0 get pos 0 |
| 213 | skip_token |
| 214 | if eq get pos 0 get tokb 0 |
| 215 | fail 2 |
| 216 | end |
| 217 | end |
| 218 | |
| 219 | fn tok_eq t view uchar * to bool |
| 220 | b = get tokb 0 |
| 221 | if ne sub get pos 0 b len t |
| 222 | ret false |
| 223 | end |
| 224 | for i ulong len t |
| 225 | if ne get inbuf add b i get t i |
| 226 | ret false |
| 227 | end |
| 228 | end |
| 229 | ret true |
| 230 | end |
| 231 | |
| 232 | text l_om "om" |
| 233 | text l_c "c" |
| 234 | text l_py "py" |
| 235 | text l_rs "rs" |
| 236 | text a_plain "plain" |
| 237 | text a_feed "feedback" |
| 238 | |
| 239 | fn lang_idx to ulong |
| 240 | read_tok |
| 241 | if tok_eq l_om |
| 242 | ret 0 |
| 243 | end |
| 244 | if tok_eq l_c |
| 245 | ret 1 |
| 246 | end |
| 247 | if tok_eq l_py |
| 248 | ret 2 |
| 249 | end |
| 250 | if tok_eq l_rs |
| 251 | ret 3 |
| 252 | end |
| 253 | fail 2 |
| 254 | ret 0 |
| 255 | end |
| 256 | |
| 257 | fn arm_idx to ulong |
| 258 | read_tok |
| 259 | if tok_eq a_plain |
| 260 | ret 0 |
| 261 | end |
| 262 | if tok_eq a_feed |
| 263 | ret 1 |
| 264 | end |
| 265 | fail 2 |
| 266 | ret 0 |
| 267 | end |
| 268 | |
| 269 | fn wtext t view uchar * to void |
| 270 | w = write 1 t 0 len t |
| 271 | end |
| 272 | |
| 273 | text zero_nl "0\n" |
| 274 | fn put_num v ulong to void |
| 275 | if eq v 0 |
| 276 | w = write 1 zero_nl 0 len zero_nl |
| 277 | ret |
| 278 | end |
| 279 | put obuf 31 10 |
| 280 | p = ulong 31 |
| 281 | x = v |
| 282 | while gt x 9 |
| 283 | p = sub p 1 |
| 284 | put obuf p uchar add rem x 10 48 |
| 285 | x = div x 10 |
| 286 | end |
| 287 | p = sub p 1 |
| 288 | put obuf p uchar add x 48 |
| 289 | w = write 1 obuf p sub 32 p |
| 290 | end |
| 291 | |
| 292 | text zero_dot "0." |
| 293 | fn put_fixed3 v ulong to void |
| 294 | intp = div v 1000 |
| 295 | frac = rem v 1000 |
| 296 | |
| 297 | if eq intp 0 |
| 298 | w = write 1 zero_dot 0 len zero_dot |
| 299 | else |
| 300 | put obuf 31 46 |
| 301 | p = ulong 31 |
| 302 | x = intp |
| 303 | while gt x 9 |
| 304 | p = sub p 1 |
| 305 | put obuf p uchar add rem x 10 48 |
| 306 | x = div x 10 |
| 307 | end |
| 308 | p = sub p 1 |
| 309 | put obuf p uchar add x 48 |
| 310 | w = write 1 obuf p sub 32 p |
| 311 | end |
| 312 | |
| 313 | d1 = uchar add (div frac 100) 48 |
| 314 | d2 = uchar add (rem (div frac 10) 10) 48 |
| 315 | d3 = uchar add (rem frac 10) 48 |
| 316 | put obuf 0 d1 |
| 317 | put obuf 1 d2 |
| 318 | put obuf 2 d3 |
| 319 | put obuf 3 10 |
| 320 | w = write 1 obuf 0 4 |
| 321 | end |
| 322 | |
| 323 | text t_ver "version 1\n" |
| 324 | text t_ver2 "version 2\n" |
| 325 | text t_groups "groups " |
| 326 | text t_group "group " |
| 327 | text t_sp " " |
| 328 | text t_nl "\n" |
| 329 | text t_end "end\n" |
| 330 | text t_tasks "tasks_total " |
| 331 | text t_att "attempts_total " |
| 332 | text t_comp "compiled_count " |
| 333 | text t_verif "verified_count " |
| 334 | text t_fver "first_attempt_verified " |
| 335 | text t_ptok "total_prompt_tokens " |
| 336 | text t_ctok "total_completion_tokens " |
| 337 | text t_ttok "total_tokens " |
| 338 | text t_wms "total_wall_ms " |
| 339 | text t_wsec "total_wall_seconds " |
| 340 | text t_tpv "tokens_per_verified " |
| 341 | text t_none "none\n" |
| 342 | text t_p1k "verified_per_1k_tokens " |
| 343 | text t_pmin "verified_per_minute " |
| 344 | text t_stat "status " |
| 345 | text ztok "0.000\n" |
| 346 | text zmin "0.000\n" |
| 347 | text t_pass "PASS\n" |
| 348 | text t_fail "FAIL\n" |
| 349 | |
| 350 | fn bump i ulong v ulong to void |
| 351 | put stats i add get stats i v |
| 352 | end |
| 353 | |
| 354 | note parse_row: the eight numbers after the row's labels, added into group base b |
| 355 | fn parse_row b ulong to void |
| 356 | att_n = read_uint |
| 357 | first = read_uint |
| 358 | comp_rc = read_uint |
| 359 | ver_ok = read_uint |
| 360 | record_task b att_n first comp_rc ver_ok |
| 361 | wms = read_uint |
| 362 | ptok = read_uint |
| 363 | ctok = read_uint |
| 364 | ttok = read_uint |
| 365 | |
| 366 | if eq comp_rc 0 |
| 367 | bump add b 1 1 |
| 368 | end |
| 369 | bump add b 4 ptok |
| 370 | bump add b 5 ctok |
| 371 | bump add b 6 ttok |
| 372 | bump add b 7 wms |
| 373 | bump add b 8 1 |
| 374 | put stats add b 9 1 |
| 375 | end |
| 376 | |
| 377 | note print_group: the counters of group base b; 0 when it passes, else 1 |
| 378 | fn print_group b ulong v2 bool to int |
| 379 | wtext t_tasks |
| 380 | put_num get stats b |
| 381 | wtext t_att |
| 382 | put_num get stats add b 8 |
| 383 | wtext t_comp |
| 384 | put_num get stats add b 1 |
| 385 | wtext t_verif |
| 386 | put_num get stats add b 2 |
| 387 | wtext t_fver |
| 388 | put_num get stats add b 3 |
| 389 | wtext t_ptok |
| 390 | put_num get stats add b 4 |
| 391 | wtext t_ctok |
| 392 | put_num get stats add b 5 |
| 393 | wtext t_ttok |
| 394 | put_num get stats add b 6 |
| 395 | wtext t_wms |
| 396 | put_num get stats add b 7 |
| 397 | wtext t_wsec |
| 398 | put_num div get stats add b 7 1000 |
| 399 | |
| 400 | tot_tok = get stats add b 6 |
| 401 | n_ver = get stats add b 2 |
| 402 | if v2 |
| 403 | wtext t_tpv |
| 404 | if gt n_ver 0 |
| 405 | put_fixed3 div mul tot_tok 1000 n_ver |
| 406 | else |
| 407 | wtext t_none |
| 408 | end |
| 409 | end |
| 410 | |
| 411 | wtext t_p1k |
| 412 | if gt tot_tok 0 |
| 413 | put_fixed3 div (mul (mul n_ver 1000) 1000) tot_tok |
| 414 | else |
| 415 | wtext ztok |
| 416 | end |
| 417 | |
| 418 | wtext t_pmin |
| 419 | tot_ms = get stats add b 7 |
| 420 | if gt tot_ms 0 |
| 421 | put_fixed3 div (mul (mul n_ver 60000) 1000) tot_ms |
| 422 | else |
| 423 | wtext zmin |
| 424 | end |
| 425 | |
| 426 | wtext t_stat |
| 427 | n_tasks = get stats b |
| 428 | if and eq n_ver n_tasks gt n_tasks 0 |
| 429 | wtext t_pass |
| 430 | ret 0 |
| 431 | end |
| 432 | wtext t_fail |
| 433 | ret 1 |
| 434 | end |
| 435 | |
| 436 | fn put_label g ulong to void |
| 437 | lg = div g 2 |
| 438 | if eq lg 0 |
| 439 | wtext l_om |
| 440 | end |
| 441 | if eq lg 1 |
| 442 | wtext l_c |
| 443 | end |
| 444 | if eq lg 2 |
| 445 | wtext l_py |
| 446 | end |
| 447 | if eq lg 3 |
| 448 | wtext l_rs |
| 449 | end |
| 450 | wtext t_sp |
| 451 | if eq rem g 2 0 |
| 452 | wtext a_plain |
| 453 | else |
| 454 | wtext a_feed |
| 455 | end |
| 456 | wtext t_nl |
| 457 | end |
| 458 | |
| 459 | fn print_v2 to int |
| 460 | wtext t_ver2 |
| 461 | ng = ulong 0 |
| 462 | for g ulong 8 |
| 463 | if eq get stats add mul g 12 9 1 |
| 464 | ng = add ng 1 |
| 465 | end |
| 466 | end |
| 467 | wtext t_groups |
| 468 | put_num ng |
| 469 | rc = int 0 |
| 470 | if eq ng 0 |
| 471 | rc = 1 |
| 472 | end |
| 473 | for g ulong 8 |
| 474 | b = mul g 12 |
| 475 | if eq get stats add b 9 1 |
| 476 | wtext t_group |
| 477 | put_label g |
| 478 | s = print_group b true |
| 479 | if ne s 0 |
| 480 | rc = 1 |
| 481 | end |
| 482 | wtext t_end |
| 483 | end |
| 484 | end |
| 485 | wtext t_stat |
| 486 | if eq rc 0 |
| 487 | wtext t_pass |
| 488 | else |
| 489 | wtext t_fail |
| 490 | end |
| 491 | ret rc |
| 492 | end |
| 493 | |
| 494 | fn main to int |
| 495 | n = read_all |
| 496 | if eq n 0 |
| 497 | fail 1 |
| 498 | end |
| 499 | put inlen 0 n |
| 500 | put pos 0 0 |
| 501 | |
| 502 | note Check header: version 1 or 2 |
| 503 | skip_token |
| 504 | v = read_uint |
| 505 | if and ne v 1 ne v 2 |
| 506 | fail 3 |
| 507 | end |
| 508 | |
| 509 | note attempts N |
| 510 | skip_token |
| 511 | att_cnt = read_uint |
| 512 | |
| 513 | if eq v 1 |
| 514 | for i ulong att_cnt |
| 515 | read_task |
| 516 | parse_row 0 |
| 517 | end |
| 518 | require_eof |
| 519 | wtext t_ver |
| 520 | ret print_group 0 false |
| 521 | end |
| 522 | for i ulong att_cnt |
| 523 | read_task |
| 524 | lg = lang_idx |
| 525 | am = arm_idx |
| 526 | parse_row mul add mul lg 2 am 12 |
| 527 | end |
| 528 | require_eof |
| 529 | ret print_v2 |
| 530 | end |