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 / bench / inference_cost / scorer.om

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