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 / seed / nki / irnki.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note irnki: OSC IR v1 elementwise scalar functions to AWS NKI kernels at the nki.isa level. |
| 3 | note Wire authority spec/one-layer-format.md. NKI ISA reference (read 2026-10-26): |
| 4 | note https://awsdocs-neuron.readthedocs-hosted.com/en/latest/nki/api/nki.isa.html |
| 5 | note A liftable function is exactly: two parameters and a result of tag 4 (u32), three vregs, |
| 6 | note one block of two instructions, BIN sub dst=2 a,b in {0,1}, then RET 2. |
| 7 | note Lane dtype: uint32 (default, same meaning as the IR) or float32 with argument f32, |
| 8 | note an explicit retyping because IR v1 has no float tag (spec section 10.3 is not produced yet). |
| 9 | note Output is buffered: every refusal happens before the first stdout byte. |
| 10 | global input uchar 2620252 |
| 11 | global output uchar 1048576 |
| 12 | note state slots: 0 input cursor, 1 input size, 2 output size, 3 lane (0 uint32, 1 float32), 4 nfuncs |
| 13 | global state ulong 8 |
| 14 | global names ulong 32 |
| 15 | global namelen ulong 32 |
| 16 | global subs ulong 32 |
| 17 | global lhs ulong 32 |
| 18 | global rhs ulong 32 |
| 19 | text emagic "irnki: MAGIC\n" |
| 20 | text ever "irnki: VERSION\n" |
| 21 | text etrunc "irnki: TRUNCATED\n" |
| 22 | text ecap "irnki: CAPACITY\n" |
| 23 | text eop "irnki: UNSUPPORTED_OP\n" |
| 24 | text etype "irnki: UNSUPPORTED_TAG\n" |
| 25 | text einval "irnki: INVALID\n" |
| 26 | text etrail "irnki: TRAILING\n" |
| 27 | text eio "irnki: IO\n" |
| 28 | text eshape "irnki: NOT_ELEMENTWISE\n" |
| 29 | text eusage "irnki: USAGE irnki [f32] < unit.ir > kernels.py\n" |
| 30 | text th0 "# Generated by seed/nki/irnki.om (seeded Omega) from an OSC IR v1 unit. Do not edit.\n# Level: nki.isa (NISA) instructions on explicit SBUF tiles of 128 partitions x 512.\n# Lane dtype: " |
| 31 | text th1 "\nimport nki\nimport nki.isa as nisa\nimport nki.language as nl\n\nTILE_P = 128\nTILE_F = 512\n" |
| 32 | text tf0 "\n\n@nki.jit\ndef om_" |
| 33 | text tf1 "(a, b):\n assert a.shape == b.shape\n rows, cols = a.shape\n assert rows % TILE_P == 0\n assert cols % TILE_F == 0\n out = nl.ndarray(a.shape, dtype=nl." |
| 34 | text tf2 ", buffer=nl.shared_hbm)\n for m in nl.affine_range(rows // TILE_P):\n for n in nl.affine_range(cols // TILE_F):\n" |
| 35 | text tal0 " t" |
| 36 | text tal1 " = nl.ndarray((TILE_P, TILE_F), dtype=nl." |
| 37 | text tal2 ", buffer=nl.sbuf)\n" |
| 38 | text td0 " nisa.dma_copy(dst=t" |
| 39 | text td1 ", src=" |
| 40 | text tslc "[m * TILE_P:(m + 1) * TILE_P, n * TILE_F:(n + 1) * TILE_F]" |
| 41 | text td2 ")\n" |
| 42 | text tt0 " nisa.tensor_tensor(dst=tc, data1=t" |
| 43 | text tt1 ", data2=t" |
| 44 | text tt2 ", op=nl." |
| 45 | text ts0 " nisa.dma_copy(dst=out" |
| 46 | text ts1 ", src=tc)\n return out\n" |
| 47 | text tk0 "\n\nKERNELS = {\n" |
| 48 | text tk1 " \"" |
| 49 | text tk2 "\": (om_" |
| 50 | text tk3 ", \"" |
| 51 | text tk4 "\"),\n" |
| 52 | text tk5 "}\n" |
| 53 | text tdf "float32" |
| 54 | text tdu "uint32" |
| 55 | text tadd "add" |
| 56 | text tsub "subtract" |
| 57 | text tmul "multiply" |
| 58 | text tand "bitwise_and" |
| 59 | text tor "bitwise_or" |
| 60 | text txor "bitwise_xor" |
| 61 | fn refuse code int to void |
| 62 | k = long 0 |
| 63 | if eq code 20 |
| 64 | k = write 2 emagic 0 len emagic |
| 65 | end |
| 66 | if eq code 21 |
| 67 | k = write 2 ever 0 len ever |
| 68 | end |
| 69 | if eq code 22 |
| 70 | k = write 2 etrunc 0 len etrunc |
| 71 | end |
| 72 | if eq code 23 |
| 73 | k = write 2 ecap 0 len ecap |
| 74 | end |
| 75 | if eq code 24 |
| 76 | k = write 2 eop 0 len eop |
| 77 | end |
| 78 | if eq code 25 |
| 79 | k = write 2 etype 0 len etype |
| 80 | end |
| 81 | if eq code 26 |
| 82 | k = write 2 einval 0 len einval |
| 83 | end |
| 84 | if eq code 27 |
| 85 | k = write 2 etrail 0 len etrail |
| 86 | end |
| 87 | if eq code 28 |
| 88 | k = write 2 eio 0 len eio |
| 89 | end |
| 90 | if eq code 29 |
| 91 | k = write 2 eshape 0 len eshape |
| 92 | end |
| 93 | if eq code 30 |
| 94 | k = write 2 eusage 0 len eusage |
| 95 | end |
| 96 | exit code |
| 97 | end |
| 98 | fn need ok bool to void |
| 99 | if not ok |
| 100 | refuse 26 |
| 101 | end |
| 102 | end |
| 103 | fn rd n ulong to ulong |
| 104 | p = get state 0 |
| 105 | if gt n sub get state 1 p |
| 106 | refuse 22 |
| 107 | end |
| 108 | v = ulong 0 |
| 109 | for j ulong n |
| 110 | v = or v shl ulong get input add p j mul j 8 |
| 111 | end |
| 112 | put state 0 add p n |
| 113 | ret v |
| 114 | end |
| 115 | fn shape ok bool to void |
| 116 | if not ok |
| 117 | refuse 29 |
| 118 | end |
| 119 | end |
| 120 | note Every register and the result must be u32 (tag 4). Tags 0..12 other than 4 are |
| 121 | note unsupported; tags above 12 do not exist in any OSC version. |
| 122 | fn lane t ulong to void |
| 123 | if ne t 4 |
| 124 | refuse 25 |
| 125 | end |
| 126 | end |
| 127 | fn namech c ulong to bool |
| 128 | if and ge c 48 le c 57 |
| 129 | ret true |
| 130 | end |
| 131 | if and ge c 65 le c 90 |
| 132 | ret true |
| 133 | end |
| 134 | if and ge c 97 le c 122 |
| 135 | ret true |
| 136 | end |
| 137 | ret eq c 95 |
| 138 | end |
| 139 | fn same a ulong b ulong to bool |
| 140 | if ne get namelen a get namelen b |
| 141 | ret false |
| 142 | end |
| 143 | for j ulong get namelen a |
| 144 | x = get input add get names a j |
| 145 | y = get input add get names b j |
| 146 | if ne x y |
| 147 | ret false |
| 148 | end |
| 149 | end |
| 150 | ret true |
| 151 | end |
| 152 | note BIN subcodes 1 add, 2 sub, 3 mul, 6 and, 7 or, 8 xor are liftable for uint32 lanes; |
| 153 | note float32 lanes take only add, sub, mul (float bitwise BIN is invalid, spec 10.3). |
| 154 | note div/rem trap on zero and shl/shr trap on wide counts in Omega; NKI has no trap, |
| 155 | note so 4, 5, 9 and 10 are refused rather than silently changed. |
| 156 | fn binsub s ulong to void |
| 157 | need and ge s 1 le s 10 |
| 158 | if or eq s 4 eq s 5 |
| 159 | refuse 24 |
| 160 | end |
| 161 | if or eq s 9 eq s 10 |
| 162 | refuse 24 |
| 163 | end |
| 164 | if and eq get state 3 1 gt s 3 |
| 165 | refuse 24 |
| 166 | end |
| 167 | end |
| 168 | fn loadfn f ulong to void |
| 169 | n = rd 1 |
| 170 | need and ne n 0 le n 63 |
| 171 | put namelen f n |
| 172 | put names f get state 0 |
| 173 | for j ulong n |
| 174 | need namech rd 1 |
| 175 | end |
| 176 | for j ulong f |
| 177 | need not same f j |
| 178 | end |
| 179 | shape eq rd 1 2 |
| 180 | lane rd 1 |
| 181 | shape eq rd 2 3 |
| 182 | for j ulong 3 |
| 183 | lane rd 1 |
| 184 | end |
| 185 | shape eq rd 2 1 |
| 186 | need eq rd 4 0 |
| 187 | shape eq rd 4 2 |
| 188 | shape eq rd 4 2 |
| 189 | if ne rd 1 3 |
| 190 | refuse 24 |
| 191 | end |
| 192 | s = rd 1 |
| 193 | binsub s |
| 194 | put subs f s |
| 195 | need eq rd 2 2 |
| 196 | x = rd 2 |
| 197 | need lt x 2 |
| 198 | put lhs f x |
| 199 | x = rd 2 |
| 200 | need lt x 2 |
| 201 | put rhs f x |
| 202 | if ne rd 1 15 |
| 203 | refuse 24 |
| 204 | end |
| 205 | need eq rd 2 2 |
| 206 | for c ulong 2 |
| 207 | n = rd 2 |
| 208 | need le n 255 |
| 209 | for j ulong n |
| 210 | need ne rd 1 0 |
| 211 | end |
| 212 | end |
| 213 | end |
| 214 | fn emit v ulong to void |
| 215 | p = get state 2 |
| 216 | if ge p len output |
| 217 | refuse 23 |
| 218 | end |
| 219 | put output p uchar v |
| 220 | put state 2 add p 1 |
| 221 | end |
| 222 | note Fragment table: one id per text above, emitted byte for byte. |
| 223 | fn frag k ulong to void |
| 224 | if eq k 0 |
| 225 | for j ulong len th0 |
| 226 | emit ulong get th0 j |
| 227 | end |
| 228 | end |
| 229 | if eq k 1 |
| 230 | for j ulong len th1 |
| 231 | emit ulong get th1 j |
| 232 | end |
| 233 | end |
| 234 | if eq k 2 |
| 235 | for j ulong len tf0 |
| 236 | emit ulong get tf0 j |
| 237 | end |
| 238 | end |
| 239 | if eq k 3 |
| 240 | for j ulong len tf1 |
| 241 | emit ulong get tf1 j |
| 242 | end |
| 243 | end |
| 244 | if eq k 4 |
| 245 | for j ulong len tf2 |
| 246 | emit ulong get tf2 j |
| 247 | end |
| 248 | end |
| 249 | if eq k 5 |
| 250 | for j ulong len tal0 |
| 251 | emit ulong get tal0 j |
| 252 | end |
| 253 | end |
| 254 | if eq k 6 |
| 255 | for j ulong len tal1 |
| 256 | emit ulong get tal1 j |
| 257 | end |
| 258 | end |
| 259 | if eq k 7 |
| 260 | for j ulong len tal2 |
| 261 | emit ulong get tal2 j |
| 262 | end |
| 263 | end |
| 264 | if eq k 8 |
| 265 | for j ulong len td0 |
| 266 | emit ulong get td0 j |
| 267 | end |
| 268 | end |
| 269 | if eq k 9 |
| 270 | for j ulong len td1 |
| 271 | emit ulong get td1 j |
| 272 | end |
| 273 | end |
| 274 | if eq k 10 |
| 275 | for j ulong len tslc |
| 276 | emit ulong get tslc j |
| 277 | end |
| 278 | end |
| 279 | if eq k 11 |
| 280 | for j ulong len td2 |
| 281 | emit ulong get td2 j |
| 282 | end |
| 283 | end |
| 284 | if eq k 12 |
| 285 | for j ulong len tt0 |
| 286 | emit ulong get tt0 j |
| 287 | end |
| 288 | end |
| 289 | if eq k 13 |
| 290 | for j ulong len tt1 |
| 291 | emit ulong get tt1 j |
| 292 | end |
| 293 | end |
| 294 | if eq k 14 |
| 295 | for j ulong len tt2 |
| 296 | emit ulong get tt2 j |
| 297 | end |
| 298 | end |
| 299 | if eq k 15 |
| 300 | for j ulong len ts0 |
| 301 | emit ulong get ts0 j |
| 302 | end |
| 303 | end |
| 304 | if eq k 16 |
| 305 | for j ulong len ts1 |
| 306 | emit ulong get ts1 j |
| 307 | end |
| 308 | end |
| 309 | if eq k 17 |
| 310 | for j ulong len tk0 |
| 311 | emit ulong get tk0 j |
| 312 | end |
| 313 | end |
| 314 | if eq k 18 |
| 315 | for j ulong len tk1 |
| 316 | emit ulong get tk1 j |
| 317 | end |
| 318 | end |
| 319 | if eq k 19 |
| 320 | for j ulong len tk2 |
| 321 | emit ulong get tk2 j |
| 322 | end |
| 323 | end |
| 324 | if eq k 20 |
| 325 | for j ulong len tk3 |
| 326 | emit ulong get tk3 j |
| 327 | end |
| 328 | end |
| 329 | if eq k 21 |
| 330 | for j ulong len tk4 |
| 331 | emit ulong get tk4 j |
| 332 | end |
| 333 | end |
| 334 | if eq k 22 |
| 335 | for j ulong len tk5 |
| 336 | emit ulong get tk5 j |
| 337 | end |
| 338 | end |
| 339 | end |
| 340 | fn dtype to void |
| 341 | if eq get state 3 1 |
| 342 | for j ulong len tdf |
| 343 | emit ulong get tdf j |
| 344 | end |
| 345 | else |
| 346 | for j ulong len tdu |
| 347 | emit ulong get tdu j |
| 348 | end |
| 349 | end |
| 350 | end |
| 351 | fn opname s ulong to void |
| 352 | if eq s 1 |
| 353 | for j ulong len tadd |
| 354 | emit ulong get tadd j |
| 355 | end |
| 356 | end |
| 357 | if eq s 2 |
| 358 | for j ulong len tsub |
| 359 | emit ulong get tsub j |
| 360 | end |
| 361 | end |
| 362 | if eq s 3 |
| 363 | for j ulong len tmul |
| 364 | emit ulong get tmul j |
| 365 | end |
| 366 | end |
| 367 | if eq s 6 |
| 368 | for j ulong len tand |
| 369 | emit ulong get tand j |
| 370 | end |
| 371 | end |
| 372 | if eq s 7 |
| 373 | for j ulong len tor |
| 374 | emit ulong get tor j |
| 375 | end |
| 376 | end |
| 377 | if eq s 8 |
| 378 | for j ulong len txor |
| 379 | emit ulong get txor j |
| 380 | end |
| 381 | end |
| 382 | end |
| 383 | fn fname f ulong to void |
| 384 | for j ulong get namelen f |
| 385 | emit ulong get input add get names f j |
| 386 | end |
| 387 | end |
| 388 | note Parameter register 0 is a, 1 is b; their SBUF tiles are ta and tb. |
| 389 | fn param r ulong to void |
| 390 | emit add 97 r |
| 391 | end |
| 392 | fn tile c ulong to void |
| 393 | frag 5 |
| 394 | emit c |
| 395 | frag 6 |
| 396 | dtype |
| 397 | frag 7 |
| 398 | end |
| 399 | fn load r ulong to void |
| 400 | frag 8 |
| 401 | param r |
| 402 | frag 9 |
| 403 | param r |
| 404 | frag 10 |
| 405 | frag 11 |
| 406 | end |
| 407 | fn kernel f ulong to void |
| 408 | frag 2 |
| 409 | fname f |
| 410 | frag 3 |
| 411 | dtype |
| 412 | frag 4 |
| 413 | tile 97 |
| 414 | tile 98 |
| 415 | tile 99 |
| 416 | load 0 |
| 417 | load 1 |
| 418 | frag 12 |
| 419 | param get lhs f |
| 420 | frag 13 |
| 421 | param get rhs f |
| 422 | frag 14 |
| 423 | opname get subs f |
| 424 | frag 11 |
| 425 | frag 15 |
| 426 | frag 10 |
| 427 | frag 16 |
| 428 | end |
| 429 | fn module to void |
| 430 | frag 0 |
| 431 | dtype |
| 432 | frag 1 |
| 433 | for f ulong get state 4 |
| 434 | kernel f |
| 435 | end |
| 436 | frag 17 |
| 437 | for f ulong get state 4 |
| 438 | frag 18 |
| 439 | fname f |
| 440 | frag 19 |
| 441 | fname f |
| 442 | frag 20 |
| 443 | dtype |
| 444 | frag 21 |
| 445 | end |
| 446 | frag 22 |
| 447 | end |
| 448 | fn args to void |
| 449 | c = argcnt |
| 450 | if eq c 1 |
| 451 | put state 3 0 |
| 452 | ret |
| 453 | end |
| 454 | if ne c 2 |
| 455 | refuse 30 |
| 456 | end |
| 457 | b = new uchar 4 0 |
| 458 | k = argread 1 b 0 4 |
| 459 | if ne k 3 |
| 460 | refuse 30 |
| 461 | end |
| 462 | if or ne get b 0 102 or ne get b 1 51 ne get b 2 50 |
| 463 | refuse 30 |
| 464 | end |
| 465 | put state 3 1 |
| 466 | end |
| 467 | fn main to int |
| 468 | args |
| 469 | n = ulong 0 |
| 470 | for attempt ulong 2620252 |
| 471 | if eq n len input |
| 472 | refuse 23 |
| 473 | end |
| 474 | k = read 0 input n sub len input n |
| 475 | if lt k 0 |
| 476 | refuse 28 |
| 477 | end |
| 478 | if eq k 0 |
| 479 | break |
| 480 | end |
| 481 | n = add n ulong k |
| 482 | end |
| 483 | if eq n len input |
| 484 | refuse 23 |
| 485 | end |
| 486 | put state 1 n |
| 487 | magic = new uchar 7 0 |
| 488 | put magic 0 79 |
| 489 | put magic 1 83 |
| 490 | put magic 2 67 |
| 491 | put magic 3 49 |
| 492 | put magic 4 73 |
| 493 | put magic 5 82 |
| 494 | for j ulong 7 |
| 495 | v = rd 1 |
| 496 | if ne v ulong get magic j |
| 497 | refuse 20 |
| 498 | end |
| 499 | end |
| 500 | if ne rd 1 1 |
| 501 | refuse 21 |
| 502 | end |
| 503 | nf = rd 2 |
| 504 | if gt nf 32 |
| 505 | refuse 23 |
| 506 | end |
| 507 | need ne nf 0 |
| 508 | put state 4 nf |
| 509 | for f ulong nf |
| 510 | loadfn f |
| 511 | end |
| 512 | if ne get state 0 n |
| 513 | refuse 27 |
| 514 | end |
| 515 | module |
| 516 | p = ulong 0 |
| 517 | for attempt ulong 1048576 |
| 518 | if eq p get state 2 |
| 519 | break |
| 520 | end |
| 521 | k = write 1 output p sub get state 2 p |
| 522 | if le k 0 |
| 523 | refuse 28 |
| 524 | end |
| 525 | p = add p ulong k |
| 526 | end |
| 527 | ret 0 |
| 528 | end |