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