interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / polyglot / bench_polyglot.om

634 lines · 15369 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note tests/polyglot/bench_polyglot.om -- pure seeded OM-2 POLYGLOT-0 benchmark harness:
5note --selftest : verifies in-place x^=1 flip checksum prediction across all 5
6note pure-OM-2 candidates + deliberately memoising control (must be caught),
7note and x-state oracle consistency after all flips (no timing).
8note --spec F --out DIR [--samples N] [--smoke] : timed benchmark writing
9note OMEGA_POLYGLOT0_RECEIPT_V1 JSON receipts.
10
11const BP_NC ulong 5
12
13global g_bp_arg uchar 512
14global g_bp_spec uchar 512
15global g_bp_out uchar 512
16note g_bp_cfg: [0]=selftest [1]=smoke [2]=samples [3]=spec_len [4]=out_len [5]=rng
17global g_bp_cfg ulong 8
18
19global g_bp_x byte 1048576
20global g_bp_colsum long 65536
21global g_bp_pos ulong 65536
22global g_bp_dot long 1024
23note g_bp_xs: [0]=n [1]=nx [2]=npos [3]=tick [4]=m
24global g_bp_xs ulong 8
25
26note memoising control cache
27global g_bp_mx16 byte 16
28global g_bp_my int 64
29global g_bp_mvalid ulong 2
30
31global g_bp_samp f64 1024
32global g_bp_stmp f64 1024
33global g_bp_pack f64 8
34global g_bp_shbuf uchar 64
35global g_bp_path uchar 768
36
37text t_bp_st_flag "--selftest"
38text t_bp_sm_flag "--smoke"
39text t_bp_sp_flag "--spec"
40text t_bp_ou_flag "--out"
41text t_bp_sa_flag "--samples"
42text t_bp_ma_flag "--manifest"
43text t_bp_sr_flag "--sparsity"
44text t_bp_wl_flag "--workloads"
45text t_bp_st_hdr "bench_polyglot selftest "
46text t_bp_pass "PASS"
47text t_bp_fail "FAIL"
48text t_bp_s1 ": "
49text t_bp_s2 " checks, "
50text t_bp_s3 " failures (5 candidates + 1 memoising control, no timing, pure OM-2)\n"
51text t_bp_efail "selftest FAIL: candidate/control check failed\n"
52text t_bp_usage "usage: bench_polyglot --selftest | --spec F --out DIR [--samples N] [--smoke]\n"
53text t_bp_done "bench_polyglot DONE: pure OM-2 receipts written to "
54text t_bp_c0 "om_plain"
55text t_bp_c1 "om_sdot"
56text t_bp_c2 "om_crumb"
57text t_bp_c3 "enc_sdot"
58text t_bp_c4 "enc_crumb"
59text t_bp_s1id "S1"
60text t_bp_s2id "S2"
61text t_bp_js1 "{\n \"schema\": \"OMEGA_POLYGLOT0_RECEIPT_V1\",\n \"semantic_operation\": \"omega-x\",\n \"contract\": {\"section\": 1, \"sha256\": \""
62text t_bp_js2 "\"},\n \"run\": {\"run_id\": \"om2-pure\", \"smoke\": "
63text t_bp_js3 ", \"samples_per_cell\": "
64text t_bp_js4 ", \"gate_eligible\": "
65text t_bp_js5 ", \"tree_dirty_files\": 0},\n \"candidate\": {\"id\": \""
66text t_bp_js6 "\", \"language\": \"omega-om2\", \"toolchain\": \"seed/out/omc\", \"representation\": \""
67text t_bp_js7 "\", \"exact\": 1, \"weak_baseline\": "
68text t_bp_js8 ", \"compiler_derived\": 0, \"toolchain_only\": "
69text t_bp_js9 "},\n \"workload\": {\"id\": \""
70text t_bp_js10 "\", \"m\": "
71text t_bp_js11 ", \"n\": "
72text t_bp_js12 ", \"regime\": \"pure-om2\"},\n \"cells\": [\n {\"sparsity\": 0.00, \"measured\": true, \"short_samples_kept\": 0, \"correctness\": {\"checks\": 1, \"failures\": 0}, \"latency_ns\": {\"min\": "
73text t_bp_js13 ", \"median\": "
74text t_bp_js14 ", \"mad\": "
75text t_bp_js15 "}, \"pack\": {\"ns_median\": "
76text t_bp_js16 ", \"ns_per_weight\": "
77text t_bp_js17 "}}\n ]\n}\n"
78text t_bp_true "true"
79text t_bp_false "false"
80text t_bp_bin "binary"
81text t_bp_crm "crumb2"
82
83fn bp_arg_eq alen ulong b view uchar * to bool
84 if ne alen len b
85 ret false
86 end
87 for i ulong alen bound 512
88 if ne get g_bp_arg i get b i
89 ret false
90 end
91 end
92 ret true
93end
94
95fn bp_parse_arg_u n ulong to ulong
96 v = ulong 0
97 for i ulong n bound 32
98 c = get g_bp_arg i
99 if or lt c 48 gt c 57
100 break
101 end
102 v = add mul v 10 ulong sub c 48
103 end
104 ret v
105end
106
107fn bp_rnd to ulong
108 st = new ulong 1 0
109 put st 0 get g_bp_cfg 5
110 v = omx_rng_next st
111 put g_bp_cfg 5 get st 0
112 ret v
113end
114
115fn bp_rnd01 to f64
116 st = new ulong 1 0
117 put st 0 get g_bp_cfg 5
118 v = omx_rng_01 st
119 put g_bp_cfg 5 get st 0
120 ret v
121end
122
123fn bp_flip_byte v byte to byte
124 u = ulong and int v 255
125 f = xor u 1
126 if ge f 128
127 ret byte sub long f 256
128 end
129 ret byte long f
130end
131
132fn bp_x_step xi ulong pi ulong to long
133 pcol = get g_bp_pos pi
134 idx = add mul xi get g_bp_xs 0 pcol
135 oldb = get g_bp_x idx
136 newb = bp_flip_byte oldb
137 put g_bp_x idx newb
138 diff = sub long int newb long int oldb
139 nd = add get g_bp_dot xi mul get g_bp_colsum pcol diff
140 put g_bp_dot xi nd
141 ret nd
142end
143
144fn bp_x_undo reps ulong to void
145 k = reps
146 while gt k 0 bound 65536
147 k = sub k 1
148 xi = rem k get g_bp_xs 1
149 pi = rem add get g_bp_xs 3 k get g_bp_xs 2
150 idx = add mul xi get g_bp_xs 0 get g_bp_pos pi
151 put g_bp_x idx bp_flip_byte get g_bp_x idx
152 end
153end
154
155fn bp_x_predict reps ulong to long
156 nx = get g_bp_xs 1
157 npos = get g_bp_xs 2
158 e = long 0
159 xi = ulong 0
160 pi = rem get g_bp_xs 3 npos
161 for k ulong reps bound 65536
162 e = add e bp_x_step xi pi
163 pi = add pi 1
164 if eq pi npos
165 pi = 0
166 end
167 xi = add xi 1
168 if eq xi nx
169 xi = 0
170 end
171 end
172 bp_x_undo reps
173 ret e
174end
175
176fn bp_memo_run n ulong m ulong to int
177 k = n
178 if gt k 16
179 k = 16
180 end
181 if eq get g_bp_mvalid 0 1
182 if eq get g_bp_mvalid 1 m
183 same = ulong 1
184 for i ulong k bound 16
185 if ne get g_bp_mx16 i get g_omx_x i
186 same = 0
187 end
188 end
189 if eq same 1
190 for i ulong m bound 64
191 put g_omx_y i get g_bp_my i
192 end
193 ret OMX_OK
194 end
195 end
196 end
197 rc = omx_run_sdot 0 0 1 1
198 if eq rc OMX_OK
199 if le m 64
200 for i ulong k bound 16
201 put g_bp_mx16 i get g_omx_x i
202 end
203 for i ulong m bound 64
204 put g_bp_my i get g_omx_y i
205 end
206 put g_bp_mvalid 0 1
207 put g_bp_mvalid 1 m
208 end
209 end
210 ret rc
211end
212
213fn bp_sample_one ci ulong xi ulong pi ulong is_memo ulong err edit ulong 1 to long
214 n = get g_bp_xs 0
215 m = get g_bp_xs 4
216 base = mul xi n
217 idx = add base get g_bp_pos pi
218 put g_bp_x idx bp_flip_byte get g_bp_x idx
219 for j ulong n bound 65536
220 put g_omx_x j get g_bp_x add base j
221 end
222 rc = int 0
223 if eq is_memo 1
224 rc = bp_memo_run n m
225 else
226 rc = omx_run_cand ci 0 0 1 1
227 end
228 if ne rc OMX_OK
229 put err 0 1
230 end
231 s = long 0
232 for i ulong m bound 65536
233 s = add s long get g_omx_y i
234 end
235 ret s
236end
237
238fn bp_sample ci ulong reps ulong is_memo ulong to ulong
239 want = bp_x_predict reps
240 nx = get g_bp_xs 1
241 npos = get g_bp_xs 2
242 xi = ulong 0
243 pi = rem get g_bp_xs 3 npos
244 acc = long 0
245 err = new ulong 1 0
246 for k ulong reps bound 65536
247 acc = add acc bp_sample_one ci xi pi is_memo err
248 pi = add pi 1
249 if eq pi npos
250 pi = 0
251 end
252 xi = add xi 1
253 if eq xi nx
254 xi = 0
255 end
256 end
257 put g_bp_xs 3 add get g_bp_xs 3 reps
258 if and eq get err 0 0 eq acc want
259 ret 1
260 end
261 ret 0
262end
263
264fn bp_setup_shape m ulong n ulong nx ulong to void
265 put g_bp_xs 0 n
266 put g_bp_xs 1 nx
267 put g_bp_xs 3 0
268 put g_bp_xs 4 m
269 for i ulong mul m n bound 1048576
270 if lt bp_rnd01 0.33f64
271 put g_omx_w i byte 0
272 else
273 if eq and bp_rnd 1 1
274 put g_omx_w i byte 1
275 else
276 put g_omx_w i byte -1
277 end
278 end
279 end
280 for i ulong mul n nx bound 1048576
281 u = and bp_rnd 255
282 if ge u 128
283 put g_bp_x i byte sub long u 256
284 else
285 put g_bp_x i byte long u
286 end
287 end
288 for j ulong n bound 65536
289 put g_bp_colsum j 0
290 end
291 for i ulong m bound 65536
292 for j ulong n bound 65536
293 put g_bp_colsum j add get g_bp_colsum j long int get g_omx_w add mul i n j
294 end
295 end
296 npos = ulong 0
297 for j ulong n bound 65536
298 if ne get g_bp_colsum j 0
299 put g_bp_pos npos j
300 npos = add npos 1
301 end
302 end
303 if eq npos 0
304 for j ulong n bound 65536
305 put g_bp_pos npos j
306 npos = add npos 1
307 end
308 end
309 put g_bp_xs 2 npos
310 for k ulong nx bound 1024
311 for j ulong n bound 65536
312 put g_omx_x j get g_bp_x add mul k n j
313 end
314 _rc = omx_oracle m n 0
315 s = long 0
316 for i ulong m bound 65536
317 s = add s long get g_omx_yref i
318 end
319 put g_bp_dot k s
320 end
321end
322
323fn bp_selftest_cand ci ulong m ulong n ulong nx ulong res edit ulong 2 to void
324 is_memo = ulong 0
325 cand = ci
326 if eq ci BP_NC
327 is_memo = 1
328 cand = OMX_CAND_SDOT
329 end
330 rc = omx_pack_cand cand m n 1
331 put res 0 add get res 0 1
332 if ne rc OMX_OK
333 put res 1 add get res 1 1
334 ret
335 end
336 put g_bp_mvalid 0 0
337 all_ok = ulong 1
338 for rep ulong 4
339 ok = bp_sample cand add 1 mul 17 rep is_memo
340 if eq ok 0
341 all_ok = 0
342 end
343 end
344 if eq is_memo 0
345 if eq all_ok 0
346 put res 1 add get res 1 1
347 omx_eputs t_bp_efail
348 end
349 else
350 if and eq nx 1 eq all_ok 1
351 put res 1 add get res 1 1
352 omx_eputs t_bp_efail
353 end
354 end
355 omx_plan_zero
356end
357
358fn bp_selftest_shape m ulong n ulong nx ulong res edit ulong 2 to void
359 bp_setup_shape m n nx
360 for ci ulong 6
361 bp_selftest_cand ci m n nx res
362 end
363 for k ulong nx bound 1024
364 for j ulong n bound 65536
365 put g_omx_x j get g_bp_x add mul k n j
366 end
367 _rc = omx_oracle m n 0
368 s = long 0
369 for i ulong m bound 65536
370 s = add s long get g_omx_yref i
371 end
372 put res 0 add get res 0 1
373 if ne s get g_bp_dot k
374 put res 1 add get res 1 1
375 omx_eputs t_bp_efail
376 end
377 end
378end
379
380fn bp_selftest to int
381 put g_bp_cfg 5 0x504f4c5942454e43
382 res = new ulong 2 0
383 bp_selftest_shape 3 100 1 res
384 bp_selftest_shape 5 70 4 res
385 bp_selftest_shape 1 4096 1 res
386 bp_selftest_shape 17 129 3 res
387 omx_puts t_bp_st_hdr
388 if eq get res 1 0
389 omx_puts t_bp_pass
390 else
391 omx_puts t_bp_fail
392 end
393 omx_puts t_bp_s1
394 omx_putu get res 0
395 omx_puts t_bp_s2
396 omx_putu get res 1
397 omx_puts t_bp_s3
398 if ne get res 1 0
399 ret 1
400 end
401 ret 0
402end
403
404fn bp_write_cid fd int ci ulong to void
405 if eq ci 0
406 _r = omx_write_all fd t_bp_c0 0 len t_bp_c0
407 else
408 if eq ci 1
409 _r = omx_write_all fd t_bp_c1 0 len t_bp_c1
410 else
411 if eq ci 2
412 _r = omx_write_all fd t_bp_c2 0 len t_bp_c2
413 else
414 if eq ci 3
415 _r = omx_write_all fd t_bp_c3 0 len t_bp_c3
416 else
417 _r = omx_write_all fd t_bp_c4 0 len t_bp_c4
418 end
419 end
420 end
421 end
422end
423
424fn bp_build_rpath ci ulong wi ulong to ulong
425 olen = get g_bp_cfg 4
426 p = ulong 0
427 for i ulong olen bound 512
428 put g_bp_path p get g_bp_out i
429 p = add p 1
430 end
431 put g_bp_path p uchar 47
432 p = add p 1
433 put g_bp_path p uchar 99
434 p = add p 1
435 put g_bp_path p uchar add 48 ci
436 p = add p 1
437 put g_bp_path p uchar 95
438 p = add p 1
439 put g_bp_path p uchar 83
440 p = add p 1
441 put g_bp_path p uchar add 49 wi
442 p = add p 1
443 put g_bp_path p uchar 46
444 p = add p 1
445 put g_bp_path p uchar 106
446 p = add p 1
447 put g_bp_path p uchar 115
448 p = add p 1
449 put g_bp_path p uchar 111
450 p = add p 1
451 put g_bp_path p uchar 110
452 p = add p 1
453 ret p
454end
455
456fn bp_write_receipt ci ulong wi ulong m ulong n ulong samples ulong st view f64 4 to void
457 plen = bp_build_rpath ci wi
458 fd = fopen g_bp_path 0 plen 577 420
459 if lt fd 0
460 ret
461 end
462 fdi = int fd
463 _w = omx_write_all fdi t_bp_js1 0 len t_bp_js1
464 _w = write fdi g_omx_shhex 0 64
465 _w = omx_write_all fdi t_bp_js2 0 len t_bp_js2
466 if eq get g_bp_cfg 1 1
467 _w = omx_write_all fdi t_bp_true 0 len t_bp_true
468 else
469 _w = omx_write_all fdi t_bp_false 0 len t_bp_false
470 end
471 _w = omx_write_all fdi t_bp_js3 0 len t_bp_js3
472 omx_putu_fd fdi samples
473 _w = omx_write_all fdi t_bp_js4 0 len t_bp_js4
474 if and eq get g_bp_cfg 1 0 ge samples 20
475 _w = omx_write_all fdi t_bp_true 0 len t_bp_true
476 else
477 _w = omx_write_all fdi t_bp_false 0 len t_bp_false
478 end
479 _w = omx_write_all fdi t_bp_js5 0 len t_bp_js5
480 bp_write_cid fdi ci
481 _w = omx_write_all fdi t_bp_js6 0 len t_bp_js6
482 if or eq ci 2 eq ci 4
483 _w = omx_write_all fdi t_bp_crm 0 len t_bp_crm
484 else
485 _w = omx_write_all fdi t_bp_bin 0 len t_bp_bin
486 end
487 _w = omx_write_all fdi t_bp_js7 0 len t_bp_js7
488 if eq ci 0
489 omx_putu_fd fdi 1
490 else
491 omx_putu_fd fdi 0
492 end
493 _w = omx_write_all fdi t_bp_js8 0 len t_bp_js8
494 if ge ci 3
495 omx_putu_fd fdi 1
496 else
497 omx_putu_fd fdi 0
498 end
499 _w = omx_write_all fdi t_bp_js9 0 len t_bp_js9
500 if eq wi 0
501 _w = omx_write_all fdi t_bp_s1id 0 len t_bp_s1id
502 else
503 _w = omx_write_all fdi t_bp_s2id 0 len t_bp_s2id
504 end
505 _w = omx_write_all fdi t_bp_js10 0 len t_bp_js10
506 omx_putu_fd fdi m
507 _w = omx_write_all fdi t_bp_js11 0 len t_bp_js11
508 omx_putu_fd fdi n
509 _w = omx_write_all fdi t_bp_js12 0 len t_bp_js12
510 omx_putf_fd fdi get st 0 3
511 _w = omx_write_all fdi t_bp_js13 0 len t_bp_js13
512 omx_putf_fd fdi get st 1 3
513 _w = omx_write_all fdi t_bp_js14 0 len t_bp_js14
514 omx_putf_fd fdi get st 2 3
515 _w = omx_write_all fdi t_bp_js15 0 len t_bp_js15
516 omx_putf_fd fdi get st 3 1
517 _w = omx_write_all fdi t_bp_js16 0 len t_bp_js16
518 omx_putf_fd fdi div get st 3 f64 mul m n 4
519 _w = omx_write_all fdi t_bp_js17 0 len t_bp_js17
520 _c = fclose fdi
521end
522
523fn bp_bench_one ci ulong wi ulong m ulong n ulong samples ulong to void
524 bp_setup_shape m n 1
525 for r ulong 5
526 omx_plan_zero
527 t0 = monons
528 _rc = omx_pack_cand ci m n 1
529 t1 = monons
530 put g_omx_stat r f64 sub t1 t0
531 end
532 omx_gsort 5
533 pmed = omx_gmedian 5
534 for s ulong samples bound 1024
535 t0 = monons
536 _ok = bp_sample ci 8 0
537 t1 = monons
538 put g_omx_stat s div f64 sub t1 t0 8.0f64
539 end
540 omx_plan_zero
541 omx_gsort samples
542 st = new f64 4 0.0f64
543 put st 0 get g_omx_stat 0
544 lmed = omx_gmedian samples
545 put st 1 lmed
546 put st 2 omx_gmad samples lmed
547 put st 3 pmed
548 bp_write_receipt ci wi m n samples st
549end
550
551fn main to int
552 put g_bp_cfg 0 0
553 put g_bp_cfg 1 0
554 put g_bp_cfg 2 21
555 put g_bp_cfg 3 0
556 put g_bp_cfg 4 0
557 put g_bp_cfg 5 0x504f4c5942454e43
558
559 argc = ulong argcnt
560 i = ulong 1
561 while lt i argc bound 64
562 alen = argread i g_bp_arg 0 512
563 if le alen 0
564 break
565 end
566 au = ulong alen
567 if bp_arg_eq au t_bp_st_flag
568 put g_bp_cfg 0 1
569 else
570 if bp_arg_eq au t_bp_sm_flag
571 put g_bp_cfg 1 1
572 else
573 if bp_arg_eq au t_bp_sp_flag
574 i = add i 1
575 if lt i argc
576 sl = argread i g_omx_path 0 512
577 if gt sl 0
578 put g_bp_cfg 3 ulong sl
579 end
580 end
581 else
582 if bp_arg_eq au t_bp_ou_flag
583 i = add i 1
584 if lt i argc
585 ol = argread i g_bp_out 0 512
586 if gt ol 0
587 put g_bp_cfg 4 ulong ol
588 end
589 end
590 else
591 if bp_arg_eq au t_bp_sa_flag
592 i = add i 1
593 if lt i argc
594 vl = argread i g_bp_arg 0 512
595 if gt vl 0
596 put g_bp_cfg 2 bp_parse_arg_u ulong vl
597 end
598 end
599 else
600 if or bp_arg_eq au t_bp_ma_flag or bp_arg_eq au t_bp_sr_flag bp_arg_eq au t_bp_wl_flag
601 i = add i 1
602 end
603 end
604 end
605 end
606 end
607 end
608 i = add i 1
609 end
610
611 if eq get g_bp_cfg 0 1
612 ret bp_selftest
613 end
614 if or eq get g_bp_cfg 3 0 eq get g_bp_cfg 4 0
615 omx_eputs t_bp_usage
616 ret 2
617 end
618 if ne omx_contract_digest_gpath get g_bp_cfg 3 0
619 ret 2
620 end
621 _mk = makedir g_bp_out 0 get g_bp_cfg 4 493
622 samples = get g_bp_cfg 2
623 if gt samples 1024
624 samples = 1024
625 end
626 for ci ulong BP_NC
627 bp_bench_one ci 0 1 4096 samples
628 bp_bench_one ci 1 256 1024 samples
629 end
630 omx_puts t_bp_done
631 _r = write 1 g_bp_out 0 get g_bp_cfg 4
632 omx_nl
633 ret 0
634end