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 / core / sovereign-core / om / runtime / inference_abi.om

241 lines · 6593 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/sovereign-core/om/runtime/inference_abi.om
3note Pure OM-2 conversion of aien-inference-abi/src/lib.rs and aien-inference-protocol/src/{limits,request,events}.rs.
4note Canonical little-endian binary wire framing for inference requests and decode outputs,
5note strict SamplingParams validation (NaN/range guards), and StopTokenGuard finish state machine.
6
7const ABI_MAGIC ulong 0x4149454e
8const ABI_VERSION ulong 1
9const ABI_REQ_FRAME_BYTES ulong 48
10const ABI_OUT_FRAME_BYTES ulong 32
11const ABI_MAX_PROMPT_TOKENS ulong 32768
12const ABI_MAX_GEN_TOKENS ulong 4096
13const ABI_MAX_STOP_TOKENS ulong 8
14
15const ABI_OK ulong 0
16const ABI_ERR_BAD_MAGIC ulong 1
17const ABI_ERR_BAD_VERSION ulong 2
18const ABI_ERR_TRUNCATED ulong 3
19const ABI_ERR_BAD_LIMITS ulong 4
20const ABI_ERR_BAD_TEMP ulong 5
21const ABI_ERR_BAD_TOP_P ulong 6
22const ABI_ERR_STOP_FULL ulong 7
23const ABI_ERR_ALREADY_FINISHED ulong 8
24
25const FINISH_NONE ulong 0
26const FINISH_STOP_TOKEN ulong 1
27const FINISH_LENGTH ulong 2
28const FINISH_CANCELLED ulong 3
29const FINISH_ERROR ulong 4
30
31struct InferenceReqFrame req_id ulong model_handle ulong prompt_tokens ulong max_tokens ulong priority ulong flags ulong end
32struct DecodeOutFrame req_id ulong token_id ulong finish_reason ulong total_tokens ulong end
33struct SamplingParams temp f32 top_p f32 max_tokens ulong top_k ulong stop_cnt ulong stop0 uint stop1 uint stop2 uint stop3 uint end
34struct StopGuard max_tokens ulong emitted ulong finished ulong reason ulong stop_cnt ulong stop0 uint stop1 uint stop2 uint stop3 uint end
35
36note Little-endian u32/u64 wire helpers.
37fn abi_put_u32 buf edit uchar * off ulong v ulong to void
38for i ulong 4
39sh = mul i 8
40put buf add off i uchar and shr v sh ulong 255
41end
42end
43
44fn abi_get_u32 buf view uchar * off ulong to ulong
45acc = ulong 0
46for i ulong 4
47sh = mul i 8
48acc = or acc shl ulong get buf add off i sh
49end
50ret acc
51end
52
53fn abi_put_u64 buf edit uchar * off ulong v ulong to void
54for i ulong 8
55sh = mul i 8
56put buf add off i uchar and shr v sh ulong 255
57end
58end
59
60fn abi_get_u64 buf view uchar * off ulong to ulong
61acc = ulong 0
62for i ulong 8
63sh = mul i 8
64acc = or acc shl ulong get buf add off i sh
65end
66ret acc
67end
68
69note Encode InferenceReqFrame into a 48-byte little-endian wire buffer.
70note Layout: [0..4) magic, [4..8) version, [8..16) req_id, [16..24) model_handle,
71note [24..32) prompt_tokens, [32..40) max_tokens, [40..44) priority, [44..48) flags.
72fn abi_encode_req f view InferenceReqFrame dst edit uchar 48 to ulong
73if or eq get f max_tokens 0 gt get f max_tokens ABI_MAX_GEN_TOKENS
74ret ABI_ERR_BAD_LIMITS
75end
76if gt get f prompt_tokens ABI_MAX_PROMPT_TOKENS
77ret ABI_ERR_BAD_LIMITS
78end
79abi_put_u32 dst 0 ABI_MAGIC
80abi_put_u32 dst 4 ABI_VERSION
81abi_put_u64 dst 8 get f req_id
82abi_put_u64 dst 16 get f model_handle
83abi_put_u64 dst 24 get f prompt_tokens
84abi_put_u64 dst 32 get f max_tokens
85abi_put_u32 dst 40 get f priority
86abi_put_u32 dst 44 get f flags
87ret ABI_OK
88end
89
90note Decode and validate InferenceReqFrame from wire slice (fail-closed on magic/version/len/bounds).
91fn abi_decode_req src view uchar * out_f edit InferenceReqFrame to ulong
92if lt len src ABI_REQ_FRAME_BYTES
93ret ABI_ERR_TRUNCATED
94end
95if ne abi_get_u32 src 0 ABI_MAGIC
96ret ABI_ERR_BAD_MAGIC
97end
98if ne abi_get_u32 src 4 ABI_VERSION
99ret ABI_ERR_BAD_VERSION
100end
101pt = abi_get_u64 src 24
102mt = abi_get_u64 src 32
103if or eq mt 0 gt mt ABI_MAX_GEN_TOKENS
104ret ABI_ERR_BAD_LIMITS
105end
106if gt pt ABI_MAX_PROMPT_TOKENS
107ret ABI_ERR_BAD_LIMITS
108end
109put out_f req_id abi_get_u64 src 8
110put out_f model_handle abi_get_u64 src 16
111put out_f prompt_tokens pt
112put out_f max_tokens mt
113put out_f priority abi_get_u32 src 40
114put out_f flags abi_get_u32 src 44
115ret ABI_OK
116end
117
118note Encode DecodeOutFrame into a 32-byte little-endian wire buffer.
119fn abi_encode_out f view DecodeOutFrame dst edit uchar 32 to ulong
120if gt get f finish_reason FINISH_ERROR
121ret ABI_ERR_BAD_LIMITS
122end
123abi_put_u32 dst 0 ABI_MAGIC
124abi_put_u32 dst 4 ABI_VERSION
125abi_put_u64 dst 8 get f req_id
126abi_put_u32 dst 16 get f token_id
127abi_put_u32 dst 20 get f finish_reason
128abi_put_u64 dst 24 get f total_tokens
129ret ABI_OK
130end
131
132note Decode DecodeOutFrame from wire slice.
133fn abi_decode_out src view uchar * out_f edit DecodeOutFrame to ulong
134if lt len src ABI_OUT_FRAME_BYTES
135ret ABI_ERR_TRUNCATED
136end
137if ne abi_get_u32 src 0 ABI_MAGIC
138ret ABI_ERR_BAD_MAGIC
139end
140if ne abi_get_u32 src 4 ABI_VERSION
141ret ABI_ERR_BAD_VERSION
142end
143fr = abi_get_u32 src 20
144if gt fr FINISH_ERROR
145ret ABI_ERR_BAD_LIMITS
146end
147put out_f req_id abi_get_u64 src 8
148put out_f token_id abi_get_u32 src 16
149put out_f finish_reason fr
150put out_f total_tokens abi_get_u64 src 24
151ret ABI_OK
152end
153
154note Check IEEE-754 f32 is finite (not NaN, not Inf).
155fn abi_f32_is_finite x f32 to bool
156u = bitcast uint x
157exp = and shr u uint 23 uint 0xff
158ret ne exp uint 0xff
159end
160
161note Validate SamplingParams fail-closed (finite temp in [0.0, 2.0], finite top_p in (0.0, 1.0],
162note max_tokens in 1..4096, top_k <= 2048, stop_cnt <= 4).
163fn abi_validate_sampling sp view SamplingParams to ulong
164t = get sp temp
165if not abi_f32_is_finite t
166ret ABI_ERR_BAD_TEMP
167end
168if or lt t 0.0f32 gt t 2.0f32
169ret ABI_ERR_BAD_TEMP
170end
171tp = get sp top_p
172if not abi_f32_is_finite tp
173ret ABI_ERR_BAD_TOP_P
174end
175if or le tp 0.0f32 gt tp 1.0f32
176ret ABI_ERR_BAD_TOP_P
177end
178mt = get sp max_tokens
179if or eq mt 0 gt mt ABI_MAX_GEN_TOKENS
180ret ABI_ERR_BAD_LIMITS
181end
182if gt get sp top_k 2048
183ret ABI_ERR_BAD_LIMITS
184end
185if gt get sp stop_cnt 4
186ret ABI_ERR_STOP_FULL
187end
188ret ABI_OK
189end
190
191note Initialize StopGuard from validated SamplingParams.
192fn abi_stop_guard_init g edit StopGuard sp view SamplingParams to ulong
193st = abi_validate_sampling sp
194if ne st ABI_OK
195ret st
196end
197put g max_tokens get sp max_tokens
198put g emitted 0
199put g finished 0
200put g reason FINISH_NONE
201put g stop_cnt get sp stop_cnt
202put g stop0 get sp stop0
203put g stop1 get sp stop1
204put g stop2 get sp stop2
205put g stop3 get sp stop3
206ret ABI_OK
207end
208
209note Observe one generated token; updates StopGuard and returns ABI_OK or ABI_ERR_ALREADY_FINISHED.
210fn abi_stop_guard_step g edit StopGuard tok uint to ulong
211if ne get g finished 0
212ret ABI_ERR_ALREADY_FINISHED
213end
214sc = get g stop_cnt
215hit = false
216if and gt sc 0 eq tok get g stop0
217hit = true
218end
219if and gt sc 1 eq tok get g stop1
220hit = true
221end
222if and gt sc 2 eq tok get g stop2
223hit = true
224end
225if and gt sc 3 eq tok get g stop3
226hit = true
227end
228ne_cnt = add get g emitted 1
229put g emitted ne_cnt
230if hit
231put g finished 1
232put g reason FINISH_STOP_TOKEN
233ret ABI_OK
234end
235if ge ne_cnt get g max_tokens
236put g finished 1
237put g reason FINISH_LENGTH
238ret ABI_OK
239end
240ret ABI_OK
241end