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 / fake_model.om

465 lines · 9341 bytes Raw Blame History
1note bench/inference_cost/fake_model.om -- a deterministic stand-in for an OpenAI
2note chat-completions endpoint, so driver.sh --self-test can exercise --run-model end to end
3note without a live model.
4note Compose: { printf 'om2\n'; cat seed/json/json.om seed/json/jsonw.om seed/net/http.om seed/net/notls.om bench/inference_cost/fake_model.om; } | omc
5note Run: fake_model MODE TASKSDIR [MAXCONN]; it listens on an ephemeral 127.0.0.1 port,
6note prints "PORT n" and serves POST /v1/chat/completions until MAXCONN connections
7note (default 1000) or 30 s without one. The request names its task and language in the
8note headers X-Bench-Task and X-Bench-Lang (1..16 bytes of [a-z0-9]); the body must be a
9note JSON object with a "messages" array. The reply's content is one fenced block holding
10note TASKSDIR/TASK/reference.LANG or TASKSDIR/TASK/mutant.LANG:
11note good the reference
12note bad the mutant (it compiles and returns wrong results)
13note bad-then-good the mutant while the request has fewer than 3 messages, else the
14note reference: a retry that carries the failed answer and feedback verifies
15note refuse a text that compiles in no language
16note busy the 1st, 3rd, 5th... chat request answers 503 with an empty body (what the
17note live server answers while it generates another reply), the others as good
18note down every chat request answers 503 with an empty body
19note Every reply reports model "fake-omega-bench" and usage 111 / 37 / 148 tokens, so a run's
20note token sums are exact multiples of the call count. Missing or bad headers, a body that
21note is not such an object, or a missing file answer 400 / 404 with an empty body.
22global fkmode ulong 1
23global fkcnt ulong 1
24global fkdir uchar 4096
25global fkdirn ulong 1
26global fkpath uchar 4400
27global fkfile uchar 65536
28global fkfilen ulong 1
29global fklang uchar 16
30global fklangn ulong 1
31text fkgood "good"
32text fkbad "bad"
33text fkbtg "bad-then-good"
34text fkrefm "refuse"
35text fkbusy "busy"
36text fkdown "down"
37text fkchat "/v1/chat/completions"
38text fkpost "POST"
39text fkhtask "X-Bench-Task"
40text fkhlang "X-Bench-Lang"
41text fkmsgs "messages"
42text fkref "reference."
43text fkmut "mutant."
44text fkslash "/"
45text fkrefus "@@@ not a program @@@\n"
46text fkfence "```"
47text fknl "\n"
48text fkctype "Content-Type"
49text fkjson "application/json"
50text fkid "id"
51text fkidv "fake-omega-bench-1"
52text fkobj "object"
53text fkobjv "chat.completion"
54text fkmodel "model"
55text fkmodv "fake-omega-bench"
56text fkchoic "choices"
57text fkindex "index"
58text fkmessg "message"
59text fkrole "role"
60text fkasst "assistant"
61text fkcont "content"
62text fkfin "finish_reason"
63text fkstop "stop"
64text fkusage "usage"
65text fkptok "prompt_tokens"
66text fkctok "completion_tokens"
67text fkttok "total_tokens"
68text fkportl "PORT "
69
70note fk_text: append the bytes of t to fkpath at p; the new end
71fn fk_text t view uchar * p ulong to ulong
72n = len t
73for i ulong n
74put fkpath add p i get t i
75end
76ret add p n
77end
78
79note fk_namec: c is in [a-z0-9]
80fn fk_namec c uchar to bool
81if and ge c 97 le c 122
82ret true
83end
84ret and ge c 48 le c 57
85end
86
87note fk_hcopy: append header value i (1..16 bytes of [a-z0-9]) to fkpath at p; the new end,
88note or 0 when the value is not such a name
89fn fk_hcopy cs edit ulong * io edit uchar * i ulong p ulong to ulong
90o = hhvoff cs i
91n = hhvlen cs i
92if or eq n 0 gt n 16
93ret 0
94end
95for j ulong n
96c = get io add o j
97if not fk_namec c
98ret 0
99end
100put fkpath add p j c
101end
102ret add p n
103end
104
105note fk_dir: fkpath = DIR/ ; the length
106fn fk_dir to ulong
107p = get fkdirn 0
108for i ulong p
109put fkpath i get fkdir i
110end
111ret fk_text fkslash p
112end
113
114note fk_setlang: fklang = fkpath[s..p)
115fn fk_setlang s ulong p ulong to void
116for i ulong sub p s
117put fklang i get fkpath add s i
118end
119put fklangn 0 sub p s
120end
121
122note fk_path: fkpath = DIR/TASK/(reference.|mutant.)LANG for sel 1 / 0, the language
123note into fklang; the path length or 0 for a missing or bad header
124fn fk_path cs edit ulong * io edit uchar * sel ulong to ulong
125ti = hhfind cs io fkhtask
126if lt ti 0
127ret 0
128end
129p = fk_dir
130p = fk_hcopy cs io ulong ti p
131if eq p 0
132ret 0
133end
134p = fk_text fkslash p
135if eq sel 1
136p = fk_text fkref p
137else
138p = fk_text fkmut p
139end
140li = hhfind cs io fkhlang
141if lt li 0
142ret 0
143end
144s = p
145p = fk_hcopy cs io ulong li p
146if eq p 0
147ret 0
148end
149fk_setlang s p
150ret p
151end
152
153note fk_load: read fkpath[0..n) into fkfile; false when it cannot be read whole
154fn fk_load n ulong to bool
155fd = fopen fkpath 0 n 0 0
156if lt fd 0
157ret false
158end
159got = ulong 0
160note every pass adds at least one byte or leaves the loop
161while lt got 65536 bound 70000
162k = read int fd fkfile got sub 65536 got
163if le k 0
164break
165end
166got = add got ulong k
167end
168c = fclose int fd
169put fkfilen 0 got
170ret lt got 65536
171end
172
173note fk_nmsgs: the element count of the root object's "messages" array, jsnone when absent
174fn fk_nmsgs s view uchar * t view uint * to ulong
175if ne js_type t 0 jsobj
176ret jsnone
177end
178m = js_first t 0
179while ne m jsnone
180if js_streq s js_off t m fkmsgs
181v = add m 1
182if ne js_type t v jsarr
183ret jsnone
184end
185ret js_nkids t v
186end
187m = js_mnext t m
188end
189ret jsnone
190end
191
192fn fk_key ob edit uchar * t view uchar * to void
193js_wsbuf ob t 0 len t
194end
195
196note fk_content: the content string: one fence around fkfile (sel 0 or 1) or the refusal (2)
197fn fk_content ob edit uchar * sel ulong to void
198js_wsbeg ob
199for i ulong len fkfence
200js_wsb ob get fkfence i
201end
202if ne sel 2
203for i ulong get fklangn 0
204js_wsb ob get fklang i
205end
206end
207js_wsb ob 10
208if eq sel 2
209for i ulong len fkrefus
210js_wsb ob get fkrefus i
211end
212else
213for i ulong get fkfilen 0
214js_wsb ob get fkfile i
215end
216end
217for i ulong len fkfence
218js_wsb ob get fkfence i
219end
220js_wsb ob 10
221js_wsend ob
222end
223
224note fk_send: the 200 reply carrying content sel
225fn fk_send cs edit ulong * io edit uchar * sel ulong to long
226ob = new uchar 525328 0
227r = js_winit ob
228if lt r 0
229ret hrsperr cs io 500
230end
231js_wobj ob
232fk_key ob fkid
233fk_key ob fkidv
234fk_key ob fkobj
235fk_key ob fkobjv
236fk_key ob fkmodel
237fk_key ob fkmodv
238fk_key ob fkchoic
239js_warr ob
240js_wobj ob
241fk_key ob fkindex
242js_wulong ob 0
243fk_key ob fkmessg
244js_wobj ob
245fk_key ob fkrole
246fk_key ob fkasst
247fk_key ob fkcont
248fk_content ob sel
249js_wobje ob
250fk_key ob fkfin
251fk_key ob fkstop
252js_wobje ob
253js_warre ob
254fk_key ob fkusage
255js_wobj ob
256fk_key ob fkptok
257js_wulong ob 111
258fk_key ob fkctok
259js_wulong ob 37
260fk_key ob fkttok
261js_wulong ob 148
262js_wobje ob
263js_wobje ob
264jl = js_wdone ob
265if lt jl 0
266ret hrsperr cs io 500
267end
268r = hrspst cs io 200
269r = hfe r hhname cs io fkctype
270r = hfe r hhval cs io fkjson
271r = hfe r hhend cs io 0 ulong jl
272r = hfe r hput cs io view part ob jswh ulong jl
273ret hfe r hwfin cs io
274end
275
276note fk_chat: one POST /v1/chat/completions
277fn fk_chat cs edit ulong * io edit uchar * to long
278body = new uchar 983040 0
279tape = new uint 65536 0
280put cs HBCAP 983040
281n = hbodyall cs io body
282if lt n 0
283if eq n HEBIG
284ret hrsperr cs io 413
285end
286ret hrsperr cs io 400
287end
288if eq n 0
289ret hrsperr cs io 400
290end
291r = js_init tape ulong n 64
292if lt r 0
293ret hrsperr cs io 400
294end
295r = js_parse body tape ulong n
296if lt r 0
297ret hrsperr cs io 400
298end
299nm = fk_nmsgs body tape
300if or eq nm jsnone eq nm 0
301ret hrsperr cs io 400
302end
303md = get fkmode 0
304if eq md 5
305ret hrsperr cs io 503
306end
307if eq md 4
308k = get fkcnt 0
309put fkcnt 0 add k 1
310if eq rem k 2 0
311ret hrsperr cs io 503
312end
313end
314sel = ulong 1
315if eq md 1
316sel = 0
317end
318if and eq md 2 lt nm 3
319sel = 0
320end
321p = fk_path cs io sel
322if eq p 0
323ret hrsperr cs io 400
324end
325if eq md 3
326ret fk_send cs io 2
327end
328if not fk_load p
329ret hrsperr cs io 404
330end
331ret fk_send cs io sel
332end
333
334fn fk_serve1 lf int cs edit ulong * io edit uchar * to long
335c = haccept lf 30000000000
336if lt c 0
337ret c
338end
339r = hinit cs io int c 1
340if lt r 0
341x = hclose cs
342ret 0
343end
344hsettmo cs 10000000000 10000000000
345hsetbdl cs 20000000000
346for it ulong 100
347r = hreqrd cs io
348if le r 0
349if lt r 0
350x = hreject cs io r
351end
352break
353end
354if hispath cs io fkchat
355if hismeth cs io fkpost
356r = fk_chat cs io
357else
358r = hrsperr cs io 405
359end
360else
361r = hrsperr cs io 404
362end
363if lt r 0
364break
365end
366if eq hnext cs io 0
367break
368end
369end
370r = hclose cs
371ret 0
372end
373
374fn fk_putport v ulong to void
375d = new uchar 32 0
376for i ulong len fkportl
377put d i get fkportl i
378end
379q = new uchar 24 0
380i = ulong 24
381x = v
382for it ulong 21
383i = sub i 1
384put q i uchar add rem x 10 48
385x = div x 10
386if eq x 0
387break
388end
389end
390p = len fkportl
391for j ulong sub 24 i
392put d add p j get q add i j
393end
394p = add p sub 24 i
395put d p 10
396w = write 1 d 0 add p 1
397end
398
399fn main to int
400cs = new ulong 320 0
401io = new uchar 131072 0
402a = new uchar 16 0
403ab = new uchar 32 0
404na = argcnt
405if or lt na 3 gt na 4
406ret 2
407end
408n = argread 1 ab 0 32
409if lt n 0
410ret 2
411end
412md = ulong 9
413if heq view part ab 0 ulong n fkgood
414md = 0
415end
416if heq view part ab 0 ulong n fkbad
417md = 1
418end
419if heq view part ab 0 ulong n fkbtg
420md = 2
421end
422if heq view part ab 0 ulong n fkrefm
423md = 3
424end
425if heq view part ab 0 ulong n fkbusy
426md = 4
427end
428if heq view part ab 0 ulong n fkdown
429md = 5
430end
431if eq md 9
432ret 2
433end
434put fkmode 0 md
435n = argread 2 fkdir 0 4000
436if or le n 0 ge n 4000
437ret 2
438end
439put fkdirn 0 ulong n
440mc = ulong 1000
441if eq na 4
442n = argread 3 ab 0 32
443if le n 0
444ret 2
445end
446if not hdigits view part ab 0 ulong n
447ret 2
448end
449mc = hdecv view part ab 0 ulong n
450end
451r = haddr4 a 2130706433 0
452lf = hlisten a 16
453if lt lf 0
454ret 3
455end
456fk_putport hport a
457for i ulong mc
458r = fk_serve1 int lf cs io
459if lt r 0
460break
461end
462end
463c = fclose int lf
464ret 0
465end