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 / aienux / crates / aienos-infer-kernel / infer_kernel_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-infer-kernel/infer_kernel_test.om: Behavioral self-test for aienos-infer-kernel. |
| 3 | note Compose with: si/directives/om2.om aienux/crates/aienos-infer-kernel/infer_kernel.om aienux/crates/aienos-infer-kernel/infer_kernel_test.om |
| 4 | |
| 5 | text ikt_prompt "What is the capital of France?" |
| 6 | text ikt_hi "Hi" |
| 7 | |
| 8 | fn ikt_put_u32_le buf edit uchar * off ulong v uint to void |
| 9 | put buf off uchar and v 0xff |
| 10 | put buf add off 1 uchar and shr v 8 0xff |
| 11 | put buf add off 2 uchar and shr v 16 0xff |
| 12 | put buf add off 3 uchar and shr v 24 0xff |
| 13 | end |
| 14 | |
| 15 | fn ikt_put_u64_le buf edit uchar * off ulong v ulong to void |
| 16 | for i ulong 8 |
| 17 | put buf add off i uchar and shr v mul i 8 0xff |
| 18 | end |
| 19 | end |
| 20 | |
| 21 | fn ikt_make_gguf buf edit uchar * tensors ulong vocab uint layers uint tok_flags uint to void |
| 22 | ikt_put_u32_le buf 0 IK_GGUF_MAGIC |
| 23 | ikt_put_u32_le buf 4 3 |
| 24 | ikt_put_u64_le buf 8 tensors |
| 25 | ikt_put_u64_le buf 16 4 |
| 26 | ikt_put_u32_le buf 24 vocab |
| 27 | ikt_put_u32_le buf 28 layers |
| 28 | ikt_put_u32_le buf 32 tok_flags |
| 29 | end |
| 30 | |
| 31 | fn ikt_test_bump to int |
| 32 | note 1. Unloaded heap refuses allocations |
| 33 | ik_heap_init 0 0 |
| 34 | if ne ik_heap_alloc 16 8 0 |
| 35 | ret 1 |
| 36 | end |
| 37 | |
| 38 | note 2. Aligned alloc, peak, dealloc-last, realloc-in-place, exhaustion |
| 39 | ik_heap_init 0x10000 1024 |
| 40 | p1 = ik_heap_alloc 10 8 |
| 41 | if ne p1 0x10000 |
| 42 | ret 2 |
| 43 | end |
| 44 | p2 = ik_heap_alloc 20 16 |
| 45 | if ne p2 0x10010 |
| 46 | ret 3 |
| 47 | end |
| 48 | note Realloc newest block in place |
| 49 | p2b = ik_heap_realloc p2 20 40 16 |
| 50 | if ne p2b p2 |
| 51 | ret 4 |
| 52 | end |
| 53 | if ne ik_heap_used 56 |
| 54 | ret 5 |
| 55 | end |
| 56 | note Dealloc newest block reclaims cursor, preserves peak |
| 57 | ik_heap_dealloc p2b 40 |
| 58 | if ne ik_heap_used 16 |
| 59 | ret 6 |
| 60 | end |
| 61 | if ne ik_heap_peak 56 |
| 62 | ret 7 |
| 63 | end |
| 64 | note Non-newest dealloc is no-op |
| 65 | ik_heap_dealloc p1 10 |
| 66 | if ne ik_heap_used 16 |
| 67 | ret 8 |
| 68 | end |
| 69 | note Exhaustion returns 0 |
| 70 | if ne ik_heap_alloc 2048 16 0 |
| 71 | ret 9 |
| 72 | end |
| 73 | |
| 74 | note 3. CPU dotprod feature flag |
| 75 | ik_set_cpu_features 0 |
| 76 | if ne ik_dot_path 0 |
| 77 | ret 10 |
| 78 | end |
| 79 | ik_set_cpu_features 1 |
| 80 | if ne ik_dot_path 1 |
| 81 | ret 11 |
| 82 | end |
| 83 | ret 0 |
| 84 | end |
| 85 | |
| 86 | fn ikt_test_session_steps to int |
| 87 | tok_o = new uint 1 0 |
| 88 | us_o = new ulong 1 0 |
| 89 | note Prefill 35 steps, then decode until STEP_LAST, then STEP_DONE |
| 90 | for i ulong 35 |
| 91 | if ne ik_session_step tok_o us_o IK_STEP_PREFILL |
| 92 | ret 34 |
| 93 | end |
| 94 | end |
| 95 | for i ulong 3 |
| 96 | if ne ik_session_step tok_o us_o IK_STEP_TOKEN |
| 97 | ret 35 |
| 98 | end |
| 99 | end |
| 100 | if ne ik_session_step tok_o us_o IK_STEP_LAST |
| 101 | ret 36 |
| 102 | end |
| 103 | if ne get tok_o 0 IK_EOT |
| 104 | ret 37 |
| 105 | end |
| 106 | if ne ik_session_step tok_o us_o IK_STEP_DONE |
| 107 | ret 38 |
| 108 | end |
| 109 | |
| 110 | note Read back produced tokens (4) and decode to text (3 ASCII bytes, EOT omitted) |
| 111 | out_toks = new uint 8 0 |
| 112 | if ne ik_session_tokens out_toks 8 4 |
| 113 | ret 39 |
| 114 | end |
| 115 | txt_out = new uchar 16 0 |
| 116 | tlen = ik_session_text view out_toks 4 txt_out 16 |
| 117 | if ne tlen 3 |
| 118 | ret 40 |
| 119 | end |
| 120 | |
| 121 | note Cancel mid-generation returns count produced before cancel and resets to STEP_DONE |
| 122 | if ne ik_session_prompt ikt_hi len ikt_hi 7 |
| 123 | ret 41 |
| 124 | end |
| 125 | for i ulong 7 |
| 126 | if ne ik_session_step tok_o us_o IK_STEP_PREFILL |
| 127 | ret 42 |
| 128 | end |
| 129 | end |
| 130 | if ne ik_session_step tok_o us_o IK_STEP_TOKEN |
| 131 | ret 43 |
| 132 | end |
| 133 | if ne ik_session_cancel 1 |
| 134 | ret 44 |
| 135 | end |
| 136 | if ne ik_session_step tok_o us_o IK_STEP_DONE |
| 137 | ret 45 |
| 138 | end |
| 139 | ret 0 |
| 140 | end |
| 141 | |
| 142 | fn ikt_test_session_heap_reserve to int |
| 143 | gg = new uchar 36 0 |
| 144 | ikt_make_gguf gg 147 128256 16 1 |
| 145 | info = new uint 4 0 |
| 146 | lim = new uint 4 0 |
| 147 | rc_c = ik_session_close |
| 148 | ik_heap_init 0x20000 5000 |
| 149 | put lim 0 64 |
| 150 | put lim 1 16 |
| 151 | put lim 2 64 |
| 152 | put lim 3 2000 |
| 153 | if ne ik_session_open view gg 36 view lim info IK_OK |
| 154 | ret 46 |
| 155 | end |
| 156 | if ne ik_session_prompt ikt_hi len ikt_hi IK_E_HEAP |
| 157 | ret 47 |
| 158 | end |
| 159 | if ne ik_session_close IK_OK |
| 160 | ret 48 |
| 161 | end |
| 162 | ret 0 |
| 163 | end |
| 164 | |
| 165 | fn ikt_test_session to int |
| 166 | ik_session_reset_all |
| 167 | ik_heap_init 0x20000 16384 |
| 168 | |
| 169 | note Operations without open session return E_STATE |
| 170 | if ne ik_session_close IK_E_STATE |
| 171 | ret 20 |
| 172 | end |
| 173 | if ne ik_session_cancel IK_E_STATE |
| 174 | ret 21 |
| 175 | end |
| 176 | tok_o = new uint 1 0 |
| 177 | us_o = new ulong 1 0 |
| 178 | if ne ik_session_step tok_o us_o IK_E_STATE |
| 179 | ret 22 |
| 180 | end |
| 181 | |
| 182 | note Bad GGUF rejected |
| 183 | bad = new uchar 36 0 |
| 184 | info = new uint 4 0 |
| 185 | lim = new uint 4 0 |
| 186 | put lim 0 64 |
| 187 | put lim 1 16 |
| 188 | put lim 2 128 |
| 189 | put lim 3 256 |
| 190 | if ne ik_session_open view bad 36 view lim info IK_E_GGUF |
| 191 | ret 23 |
| 192 | end |
| 193 | |
| 194 | note Valid GGUF with bad limits rejected |
| 195 | gg = new uchar 36 0 |
| 196 | ikt_make_gguf gg 147 128256 16 1 |
| 197 | put lim 0 0 |
| 198 | if ne ik_session_open view gg 36 view lim info IK_E_LIMITS |
| 199 | ret 24 |
| 200 | end |
| 201 | put lim 0 64 |
| 202 | put lim 1 65 |
| 203 | if ne ik_session_open view gg 36 view lim info IK_E_LIMITS |
| 204 | ret 25 |
| 205 | end |
| 206 | |
| 207 | note Open succeeds and reports metadata |
| 208 | put lim 0 64 |
| 209 | put lim 1 16 |
| 210 | put lim 2 64 |
| 211 | put lim 3 256 |
| 212 | if ne ik_session_open view gg 36 view lim info IK_OK |
| 213 | ret 26 |
| 214 | end |
| 215 | if ne get info 0 147 |
| 216 | ret 27 |
| 217 | end |
| 218 | if ne get info 1 128256 |
| 219 | ret 27 |
| 220 | end |
| 221 | if ne get info 2 16 |
| 222 | ret 27 |
| 223 | end |
| 224 | |
| 225 | note Double open rejected with E_STATE |
| 226 | if ne ik_session_open view gg 36 view lim info IK_E_STATE |
| 227 | ret 28 |
| 228 | end |
| 229 | |
| 230 | note Invalid UTF-8 prompt rejected with E_UTF8 |
| 231 | bad_u8 = new uchar 3 0 |
| 232 | put bad_u8 0 0xed |
| 233 | put bad_u8 1 0xa0 |
| 234 | put bad_u8 2 0x80 |
| 235 | if ne ik_session_prompt view bad_u8 3 IK_E_UTF8 |
| 236 | ret 29 |
| 237 | end |
| 238 | |
| 239 | note Oversized prompt rejected with E_PROMPT_BYTES |
| 240 | bigbuf = new uchar 80 65 |
| 241 | if ne ik_session_prompt view bigbuf 80 IK_E_PROMPT_BYTES |
| 242 | ret 30 |
| 243 | end |
| 244 | |
| 245 | note Valid prompt returns token count (5 + len("What is the capital of France?") = 35) |
| 246 | nids = ik_session_prompt ikt_prompt len ikt_prompt |
| 247 | if ne nids 35 |
| 248 | ret 31 |
| 249 | end |
| 250 | first4 = new uint 4 0 |
| 251 | if ne ik_session_prompt_ids first4 4 35 |
| 252 | ret 32 |
| 253 | end |
| 254 | if ne get first4 0 128000 |
| 255 | ret 33 |
| 256 | end |
| 257 | if ne get first4 1 128006 |
| 258 | ret 33 |
| 259 | end |
| 260 | if ne get first4 2 882 |
| 261 | ret 33 |
| 262 | end |
| 263 | if ne get first4 3 128007 |
| 264 | ret 33 |
| 265 | end |
| 266 | |
| 267 | rc = ikt_test_session_steps |
| 268 | if ne rc 0 |
| 269 | ret rc |
| 270 | end |
| 271 | ret ikt_test_session_heap_reserve |
| 272 | end |
| 273 | |
| 274 | fn main to int |
| 275 | rc = ikt_test_bump |
| 276 | if ne rc 0 |
| 277 | ret rc |
| 278 | end |
| 279 | rc = ikt_test_session |
| 280 | if ne rc 0 |
| 281 | ret rc |
| 282 | end |
| 283 | ret 0 |
| 284 | end |