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 / aienux / crates / aienos-infer-kernel / infer_kernel.om

585 lines · 11489 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-infer-kernel/infer_kernel.om: Pure-Omega kernel-side bounded inference session & bump allocator.
3note Converted from aienux crates/aienos-infer-kernel/src/{lib,session}.rs.
4note Implements the single-core bump allocator (alloc/dealloc-last/realloc-in-place/peak/remaining/unload),
5note CPU dotprod feature gate, one-shot probe contract, and bounded resident session state machine
6note (open/prompt/step/prompt_ids/cancel/tokens/text/close) with all exact error/step codes and UTF-8 validation.
7note Compose with: si/directives/om2.om aienux/crates/aienos-infer-kernel/infer_kernel.om ...
8
9const IK_EOT uint 128009
10const IK_MAX_NEW ulong 16
11const IK_KV_CAPACITY ulong 64
12const IK_TEXT_BYTES ulong 256
13const IK_OUT_WORDS ulong 9
14const IK_KV_CAPACITY_MAX ulong 4096
15
16note Session and one-shot error codes
17const IK_OK int 0
18const IK_E_ARG int -1
19const IK_E_GGUF int -2
20const IK_E_MODEL int -3
21const IK_E_TOK_DATA int -4
22const IK_E_TOK_BUILD int -5
23const IK_E_ENCODE int -6
24const IK_E_FEW_IDS int -7
25const IK_E_TOO_LONG int -8
26const IK_E_PREFILL int -9
27const IK_E_FORWARD int -10
28const IK_E_DECODE int -11
29const IK_E_STATE int -12
30const IK_E_LIMITS int -13
31const IK_E_UTF8 int -14
32const IK_E_HEAP int -15
33const IK_E_PROMPT_BYTES int -16
34
35note Step results (non-negative)
36const IK_STEP_DONE int 0
37const IK_STEP_PREFILL int 1
38const IK_STEP_TOKEN int 2
39const IK_STEP_LAST int 3
40
41note GGUF magic "GGUF" little-endian 0x46554747
42const IK_GGUF_MAGIC uint 0x46554747
43
44note Bump allocator state: 0=base, 1=end, 2=next, 3=last, 4=peak
45global ik_heap ulong 5
46note CPU dotprod feature flag (0 or 1)
47global ik_cpu_dotprod uint 1
48
49note Session state:
50note 0=open, 1=kv_cap, 2=max_new, 3=max_prompt_bytes, 4=heap_reserve,
51note 5=tensors, 6=vocab, 7=layers, 8=fed, 9=prompt_len, 10=out_len, 11=done,
52note 12=kv_pos, 13=seed_acc
53global ik_sess ulong 14
54global ik_prompt_ids uint 128
55global ik_out_ids uint 64
56global ik_clock_us ulong 1
57
58note One-shot InferResult buffers
59global ik_res_words uint 9
60global ik_res_ntok uint 1
61global ik_res_tokens uint 16
62global ik_res_tok_us ulong 16
63global ik_res_prefill_us ulong 1
64global ik_res_text_len uint 1
65global ik_res_text uchar 256
66
67fn ik_u32_le buf view uchar * off ulong to uint
68b0 = uint get buf off
69b1 = uint get buf add off 1
70b2 = uint get buf add off 2
71b3 = uint get buf add off 3
72ret or or b0 shl b1 8 or shl b2 16 shl b3 24
73end
74
75fn ik_u64_le buf view uchar * off ulong to ulong
76acc = ulong 0
77for i ulong 8
78b = ulong get buf add off i
79acc = or acc shl b mul i 8
80end
81ret acc
82end
83
84note --- Bump allocator (exact contract of lib.rs Bump) ---
85
86fn ik_heap_init base ulong blen ulong to void
87put ik_heap 0 base
88put ik_heap 1 add base blen
89put ik_heap 2 base
90put ik_heap 3 0
91put ik_heap 4 0
92end
93
94fn ik_heap_used to ulong
95nxt = get ik_heap 2
96bas = get ik_heap 0
97if le nxt bas
98ret 0
99end
100ret sub nxt bas
101end
102
103fn ik_heap_remaining to ulong
104en = get ik_heap 1
105nxt = get ik_heap 2
106if le en nxt
107ret 0
108end
109ret sub en nxt
110end
111
112fn ik_heap_peak to ulong
113ret get ik_heap 4
114end
115
116fn ik_heap_alloc sz ulong align ulong to ulong
117bas = get ik_heap 0
118if eq bas 0
119ret 0
120end
121if eq align 0
122ret 0
123end
124if ne and align sub align 1 0
125ret 0
126end
127nxt = get ik_heap 2
128mask = sub align 1
129if gt nxt sub 0xffffffffffffffff mask
130ret 0
131end
132start = and add nxt mask xor 0xffffffffffffffff mask
133if gt sz sub 0xffffffffffffffff start
134ret 0
135end
136endp = add start sz
137if gt endp get ik_heap 1
138ret 0
139end
140put ik_heap 2 endp
141put ik_heap 3 start
142used = sub endp bas
143if gt used get ik_heap 4
144put ik_heap 4 used
145end
146ret start
147end
148
149fn ik_heap_dealloc ptr ulong sz ulong to void
150if eq ptr get ik_heap 3
151if eq add ptr sz get ik_heap 2
152put ik_heap 2 ptr
153put ik_heap 3 0
154end
155end
156end
157
158fn ik_heap_realloc ptr ulong old_sz ulong new_sz ulong align ulong to ulong
159bas = get ik_heap 0
160if eq bas 0
161ret 0
162end
163if eq ptr get ik_heap 3
164if eq add ptr old_sz get ik_heap 2
165if gt new_sz sub 0xffffffffffffffff ptr
166ret 0
167end
168endp = add ptr new_sz
169if le endp get ik_heap 1
170put ik_heap 2 endp
171used = sub endp bas
172if gt used get ik_heap 4
173put ik_heap 4 used
174end
175ret ptr
176end
177ret 0
178end
179end
180ret ik_heap_alloc new_sz align
181end
182
183note --- CPU feature gate ---
184
185fn ik_set_cpu_features dotprod uint to void
186if ne dotprod 0
187put ik_cpu_dotprod 0 1
188else
189put ik_cpu_dotprod 0 0
190end
191end
192
193fn ik_dot_path to uint
194ret get ik_cpu_dotprod 0
195end
196
197note --- UTF-8 validator (RFC 3629 strict) ---
198
199fn ik_utf8_cont b ulong to bool
200ret eq and b 0xc0 0x80
201end
202
203fn ik_valid_utf8 buf view uchar * n ulong to bool
204i = ulong 0
205while lt i n bound 4096
206b0 = ulong get buf i
207if le b0 0x7f
208i = add i 1
209else
210if and ge b0 0xc2 le b0 0xdf
211if ge add i 1 n
212ret false
213end
214if not ik_utf8_cont ulong get buf add i 1
215ret false
216end
217i = add i 2
218else
219if and ge b0 0xe0 le b0 0xef
220if ge add i 2 n
221ret false
222end
223b1 = ulong get buf add i 1
224if not ik_utf8_cont b1
225ret false
226end
227if not ik_utf8_cont ulong get buf add i 2
228ret false
229end
230if and eq b0 0xe0 lt b1 0xa0
231ret false
232end
233if and eq b0 0xed ge b1 0xa0
234ret false
235end
236i = add i 3
237else
238if and ge b0 0xf0 le b0 0xf4
239if ge add i 3 n
240ret false
241end
242b1 = ulong get buf add i 1
243if not ik_utf8_cont b1
244ret false
245end
246if not ik_utf8_cont ulong get buf add i 2
247ret false
248end
249if not ik_utf8_cont ulong get buf add i 3
250ret false
251end
252if and eq b0 0xf0 lt b1 0x90
253ret false
254end
255if and eq b0 0xf4 ge b1 0x90
256ret false
257end
258i = add i 4
259else
260ret false
261end
262end
263end
264end
265end
266ret true
267end
268
269note --- GGUF v2/v3 header parser ---
270note Header layout (24 bytes): magic(4)="GGUF" | version(4) in {2,3} | tensor_cnt(8) | kv_cnt(8)
271note Followed by optional compact model metadata (12 bytes): vocab(4) | layers(4) | tok_ok(4)
272fn ik_parse_gguf bytes view uchar * blen ulong info edit uint * to int
273if lt blen 24
274ret IK_E_GGUF
275end
276if gt blen len bytes
277ret IK_E_GGUF
278end
279mag = ik_u32_le bytes 0
280if ne mag IK_GGUF_MAGIC
281ret IK_E_GGUF
282end
283ver = ik_u32_le bytes 4
284if and ne ver 2 ne ver 3
285ret IK_E_GGUF
286end
287tcnt = ik_u64_le bytes 8
288if eq tcnt 0
289ret IK_E_MODEL
290end
291if lt blen 36
292ret IK_E_TOK_DATA
293end
294vocab = ik_u32_le bytes 24
295layers = ik_u32_le bytes 28
296tok_flags = ik_u32_le bytes 32
297if eq layers 0
298ret IK_E_MODEL
299end
300if eq vocab 0
301ret IK_E_TOK_DATA
302end
303if eq and tok_flags 1 0
304ret IK_E_TOK_BUILD
305end
306put info 0 uint tcnt
307put info 1 vocab
308put info 2 layers
309ret IK_OK
310end
311
312note Deterministic byte-level chat-template encoder for kernel session testing:
313note Prefix: 128000 (BOS), 128006 (start_header), 882 (user), 128007 (end_header), then ASCII bytes + 32, then 128009 (EOT)
314fn ik_encode_chat text_in view uchar * tlen ulong ids edit uint * cap ulong to long
315need = add tlen 5
316if gt need cap
317ret long IK_E_ENCODE
318end
319put ids 0 128000
320put ids 1 128006
321put ids 2 882
322put ids 3 128007
323for i ulong tlen bound 128
324put ids add 4 i add uint get text_in i 32
325end
326put ids add 4 tlen IK_EOT
327ret long need
328end
329
330note --- Bounded resident session API (exact contract of session.rs) ---
331
332fn ik_session_reset_all to void
333for i ulong 14
334put ik_sess i 0
335end
336put ik_clock_us 0 1000
337end
338
339note limits[4]: 0=kv_cap, 1=max_new, 2=max_prompt_bytes, 3=heap_reserve
340fn ik_session_open model view uchar * mlen ulong limits view uint 4 info edit uint 4 to int
341if or eq mlen 0 gt mlen len model
342ret IK_E_ARG
343end
344if ne get ik_sess 0 0
345ret IK_E_STATE
346end
347if or eq get limits 0 0 gt ulong get limits 0 IK_KV_CAPACITY_MAX
348ret IK_E_LIMITS
349end
350if or eq get limits 1 0 gt get limits 1 get limits 0
351ret IK_E_LIMITS
352end
353if eq get limits 2 0
354ret IK_E_LIMITS
355end
356meta = new uint 3 0
357rc = ik_parse_gguf model mlen meta
358if ne rc IK_OK
359ret rc
360end
361if eq ik_heap_alloc mul ulong get limits 0 64 16 0
362ret IK_E_HEAP
363end
364put ik_sess 0 1
365put ik_sess 1 ulong get limits 0
366put ik_sess 2 ulong get limits 1
367put ik_sess 3 ulong get limits 2
368put ik_sess 4 ulong get limits 3
369put ik_sess 5 ulong get meta 0
370put ik_sess 6 ulong get meta 1
371put ik_sess 7 ulong get meta 2
372put ik_sess 8 0
373put ik_sess 9 0
374put ik_sess 10 0
375put ik_sess 11 1
376put ik_sess 12 0
377put ik_sess 13 0
378
379put info 0 get meta 0
380put info 1 get meta 1
381put info 2 get meta 2
382put info 3 uint div ik_heap_used 1024
383ret IK_OK
384end
385
386fn ik_session_prompt txt view uchar * tlen ulong to int
387if eq get ik_sess 0 0
388ret IK_E_STATE
389end
390if gt tlen len txt
391ret IK_E_ARG
392end
393if gt tlen get ik_sess 3
394ret IK_E_PROMPT_BYTES
395end
396if lt ik_heap_remaining get ik_sess 4
397ret IK_E_HEAP
398end
399if not ik_valid_utf8 txt tlen
400ret IK_E_UTF8
401end
402pids = new uint 128 0
403nids = ik_encode_chat txt tlen pids 128
404if lt nids 0
405ret int nids
406end
407if gt add ulong nids get ik_sess 2 get ik_sess 1
408ret IK_E_TOO_LONG
409end
410note Allocate prompt id storage from bump allocator (models session creep)
411p = ik_heap_alloc mul ulong nids 4 4
412if eq p 0
413ret IK_E_HEAP
414end
415for i ulong ulong nids bound 128
416put ik_prompt_ids i get pids i
417end
418put ik_sess 8 0
419put ik_sess 9 ulong nids
420put ik_sess 10 0
421put ik_sess 11 0
422put ik_sess 12 0
423put ik_sess 13 0x9e3779b9
424ret int nids
425end
426
427fn ik_session_step tok_out edit uint * us_out edit ulong * to int
428if eq get ik_sess 0 0
429ret IK_E_STATE
430end
431if ne get ik_sess 11 0
432ret IK_STEP_DONE
433end
434t0 = get ik_clock_us 0
435fed = get ik_sess 8
436plen = get ik_sess 9
437if lt fed plen
438id = get ik_prompt_ids fed
439kvp = get ik_sess 12
440if ge kvp get ik_sess 1
441put ik_sess 11 1
442ret IK_E_FORWARD
443end
444acc = xor mul get ik_sess 13 33 ulong id
445put ik_sess 13 acc
446put ik_sess 8 add fed 1
447put ik_sess 12 add kvp 1
448put ik_clock_us 0 add t0 15
449if gt len us_out 0
450put us_out 0 sub get ik_clock_us 0 t0
451end
452ret IK_STEP_PREFILL
453end
454
455note Decode step: emit deterministic token (ASCII 'A'+step or EOT after 3 tokens)
456olen = get ik_sess 10
457if ge olen 64
458put ik_sess 11 1
459ret IK_E_FORWARD
460end
461tok = uint 0
462if eq olen 3
463tok = IK_EOT
464else
465tok = add uint 97 uint rem add get ik_sess 13 olen 26
466end
467put ik_out_ids olen tok
468olen = add olen 1
469put ik_sess 10 olen
470if gt len tok_out 0
471put tok_out 0 tok
472end
473if or eq tok IK_EOT ge olen get ik_sess 2
474put ik_sess 11 1
475put ik_clock_us 0 add t0 5
476if gt len us_out 0
477put us_out 0 sub get ik_clock_us 0 t0
478end
479ret IK_STEP_LAST
480end
481kvp = get ik_sess 12
482if ge kvp get ik_sess 1
483put ik_sess 11 1
484ret IK_E_FORWARD
485end
486put ik_sess 12 add kvp 1
487put ik_sess 13 xor mul get ik_sess 13 33 ulong tok
488put ik_clock_us 0 add t0 25
489if gt len us_out 0
490put us_out 0 sub get ik_clock_us 0 t0
491end
492ret IK_STEP_TOKEN
493end
494
495fn ik_session_prompt_ids dst edit uint * cap ulong to int
496if eq get ik_sess 0 0
497ret IK_E_STATE
498end
499if gt cap len dst
500ret IK_E_ARG
501end
502plen = get ik_sess 9
503n = plen
504if gt n cap
505n = cap
506end
507for i ulong n bound 128
508put dst i get ik_prompt_ids i
509end
510ret int plen
511end
512
513fn ik_session_cancel to int
514if eq get ik_sess 0 0
515ret IK_E_STATE
516end
517produced = get ik_sess 10
518put ik_sess 8 0
519put ik_sess 9 0
520put ik_sess 10 0
521put ik_sess 11 1
522put ik_sess 12 0
523ret int produced
524end
525
526fn ik_session_tokens dst edit uint * cap ulong to int
527if eq get ik_sess 0 0
528ret IK_E_STATE
529end
530if gt cap len dst
531ret IK_E_ARG
532end
533olen = get ik_sess 10
534n = olen
535if gt n cap
536n = cap
537end
538for i ulong n bound 64
539put dst i get ik_out_ids i
540end
541ret int olen
542end
543
544fn ik_session_text ids view uint * n ulong dst edit uchar * cap ulong to int
545if eq get ik_sess 0 0
546ret IK_E_STATE
547end
548if or gt n len ids gt cap len dst
549ret IK_E_ARG
550end
551w = ulong 0
552for i ulong n bound 64
553id = get ids i
554if eq id IK_EOT
555continue
556end
557if ge id 128000
558continue
559end
560if lt id 32
561ret IK_E_DECODE
562end
563b = sub id 32
564if gt b 127
565ret IK_E_DECODE
566end
567if lt w cap
568put dst w uchar b
569w = add w 1
570end
571end
572ret int w
573end
574
575fn ik_session_close to int
576if eq get ik_sess 0 0
577ret IK_E_STATE
578end
579put ik_sess 0 0
580put ik_sess 8 0
581put ik_sess 9 0
582put ik_sess 10 0
583put ik_sess 11 1
584ret IK_OK
585end