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 / qwen-native / flash / graph.om
| 1 | note One text sequence on the frozen Qwen3.8-Flash-Next-FP8 model: four residual streams, gated |
| 2 | note hyper-connections around every block, one lexical n-gram layer, and 512 routed experts per layer. |
| 3 | note Tensors that the reference stores as BF16 are rounded to BF16 at the same boundaries. |
| 4 | note Working rows are globals because a global is never passed as an argument. Products go through fin and fout. |
| 5 | note qrun: 0 model, 1 layer, 2 position, 3 stopped, 4 emitted. |
| 6 | global qrun ulong 6 |
| 7 | global fh f32 10240 |
| 8 | global fnrm f32 10240 |
| 9 | global fkn f32 10240 |
| 10 | global fgv f32 10240 |
| 11 | global fx f32 2560 |
| 12 | global fdy f32 2560 |
| 13 | global fsh f32 2560 |
| 14 | global fval f32 2560 |
| 15 | global fga f32 640 |
| 16 | global finj f32 4 |
| 17 | global fvp f32 8388608 |
| 18 | global fvo uint 196608 |
| 19 | global fvn ulong 1 |
| 20 | fn qwroleof slot ulong to ulong |
| 21 | ret ulong get qrole add mul get qrun 1 1600 slot |
| 22 | end |
| 23 | |
| 24 | note Host copy of one small BF16 tensor, widened once. Returns its start in fvp. |
| 25 | fn fvec id ulong to ulong |
| 26 | o = ulong get fvo id |
| 27 | if ne o 0 |
| 28 | ret sub o 1 |
| 29 | end |
| 30 | if ne get qwdtype id 2 |
| 31 | qwfail 51 |
| 32 | end |
| 33 | n = get qwcount id |
| 34 | base = get fvn 0 |
| 35 | if or gt mul n 2 len qbuf gt add base n len fvp |
| 36 | qwfail 51 |
| 37 | end |
| 38 | qwread id 0 n |
| 39 | for j ulong n |
| 40 | put fvp add base j qbf j |
| 41 | end |
| 42 | put fvn 0 add base n |
| 43 | put fvo id uint add base 1 |
| 44 | ret base |
| 45 | end |
| 46 | |
| 47 | fn fsig x f32 to f32 |
| 48 | ret qrb f32 qb_sig f64 x |
| 49 | end |
| 50 | |
| 51 | fn fsilu x f32 to f32 |
| 52 | ret qrb mul x f32 qb_sig f64 x |
| 53 | end |
| 54 | |
| 55 | note Element i of a normalization source: 0 the residual streams, 1 the last product, 2 the gated lexical value. |
| 56 | fn fsrc sel ulong i ulong to f32 |
| 57 | if eq sel 0 |
| 58 | ret get fh i |
| 59 | end |
| 60 | if eq sel 1 |
| 61 | ret get fout i |
| 62 | end |
| 63 | ret get fgv i |
| 64 | end |
| 65 | |
| 66 | note Grouped RMS normalization of four 2560-wide streams with zero-centred gains. The result is fnrm. |
| 67 | fn fgnorm wid ulong sel ulong to void |
| 68 | wo = fvec wid |
| 69 | for s ulong 4 |
| 70 | acc = 0.0f64 |
| 71 | for j ulong 2560 |
| 72 | v = f64 fsrc sel add mul s 2560 j |
| 73 | acc = add acc mul v v |
| 74 | end |
| 75 | inv = div 1.0f64 fsqrt add div acc 2560.0f64 0.000001f64 |
| 76 | for j ulong 2560 |
| 77 | i = add mul s 2560 j |
| 78 | gain = add 1.0f64 f64 get fvp add wo i |
| 79 | put fnrm i qrb f32 mul mul f64 fsrc sel i inv gain |
| 80 | end |
| 81 | end |
| 82 | end |
| 83 | |
| 84 | fn fnrmin to void |
| 85 | for j ulong 10240 |
| 86 | put fin j get fnrm j |
| 87 | end |
| 88 | end |
| 89 | |
| 90 | fn fxin to void |
| 91 | for j ulong 2560 |
| 92 | put fin j get fx j |
| 93 | end |
| 94 | end |
| 95 | |
| 96 | note Gated residual input. Reads fh, leaves the mixed block input in fx and, when iid is nonzero, the stream injection weights in finj. |
| 97 | fn fhyper nid ulong did ulong uid ulong iid ulong to void |
| 98 | fgnorm nid 0 |
| 99 | if ne iid 0 |
| 100 | fnrmin |
| 101 | fmat iid |
| 102 | for s ulong 4 |
| 103 | put finj s mul 2.0f32 fsig qrb mul get fout s 0.25f32 |
| 104 | end |
| 105 | end |
| 106 | fnrmin |
| 107 | fmat did |
| 108 | for j ulong 320 |
| 109 | put fin j fsilu qrb mul get fout j 0.25f32 |
| 110 | end |
| 111 | fmat uid |
| 112 | for j ulong 2560 |
| 113 | acc = 0.0f32 |
| 114 | for s ulong 4 |
| 115 | i = add mul s 2560 j |
| 116 | acc = add acc qrb mul fsig get fout i get fnrm i |
| 117 | end |
| 118 | put fx j qrb mul acc 0.25f32 |
| 119 | end |
| 120 | end |
| 121 | |
| 122 | note fh = fh + injection weight of each stream times the block output in fdy. |
| 123 | fn fapply to void |
| 124 | for s ulong 4 |
| 125 | wt = get finj s |
| 126 | for j ulong 2560 |
| 127 | i = add mul s 2560 j |
| 128 | put fh i qrb add get fh i qrb mul get fdy j wt |
| 129 | end |
| 130 | end |
| 131 | end |
| 132 | |
| 133 | fn foutdy to void |
| 134 | for j ulong 2560 |
| 135 | put fdy j get fout j |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | fn fdelta to void |
| 140 | p = new f32 16480 0.0f32 |
| 141 | w = new f32 41184 0.0f32 |
| 142 | z = new f32 6144 0.0f32 |
| 143 | fxin |
| 144 | fmat qwroleof 6 |
| 145 | for j ulong 10240 |
| 146 | put p j get fout j |
| 147 | end |
| 148 | fmat qwroleof 7 |
| 149 | for j ulong 6144 |
| 150 | put p add 10240 j get fout j |
| 151 | end |
| 152 | fmat qwroleof 4 |
| 153 | for j ulong 48 |
| 154 | put p add 16384 mul j 2 get fout j |
| 155 | end |
| 156 | fmat qwroleof 5 |
| 157 | for j ulong 48 |
| 158 | put p add 16385 mul j 2 get fout j |
| 159 | end |
| 160 | o = fvec qwroleof 10 |
| 161 | for j ulong 40960 |
| 162 | put w j get fvp add o j |
| 163 | end |
| 164 | o = fvec qwroleof 2 |
| 165 | for j ulong 48 |
| 166 | put w add 40960 j get fvp add o j |
| 167 | end |
| 168 | o = fvec qwroleof 3 |
| 169 | for j ulong 48 |
| 170 | put w add 41008 j get fvp add o j |
| 171 | end |
| 172 | o = fvec qwroleof 8 |
| 173 | for j ulong 128 |
| 174 | put w add 41056 j get fvp add o j |
| 175 | end |
| 176 | qwen_delta_step 38 get qrun 1 get qrun 2 p w z |
| 177 | for j ulong 6144 |
| 178 | put fin j qrb get z j |
| 179 | end |
| 180 | fmat qwroleof 9 |
| 181 | foutdy |
| 182 | end |
| 183 | |
| 184 | fn fattention to void |
| 185 | p = new f32 14336 0.0f32 |
| 186 | w = new f32 512 0.0f32 |
| 187 | z = new f32 6144 0.0f32 |
| 188 | fxin |
| 189 | fmat qwroleof 13 |
| 190 | for j ulong 12288 |
| 191 | put p j get fout j |
| 192 | end |
| 193 | fmat qwroleof 14 |
| 194 | for j ulong 512 |
| 195 | put p add 12288 j get fout j |
| 196 | end |
| 197 | fmat qwroleof 15 |
| 198 | for j ulong 512 |
| 199 | put p add 12800 j get fout j |
| 200 | end |
| 201 | o = fvec qwroleof 11 |
| 202 | for j ulong 256 |
| 203 | put w j get fvp add o j |
| 204 | end |
| 205 | o = fvec qwroleof 12 |
| 206 | for j ulong 256 |
| 207 | put w add 256 j get fvp add o j |
| 208 | end |
| 209 | qwen_attention_step 38 get qrun 1 get qrun 2 p w z |
| 210 | for j ulong 6144 |
| 211 | put fin j qrb get z j |
| 212 | end |
| 213 | fmat qwroleof 16 |
| 214 | foutdy |
| 215 | end |
| 216 | |
| 217 | global qtop uint 10 |
| 218 | global qprob f32 10 |
| 219 | note Top ten of the 512 router scores now in fout, with their renormalized softmax weights. |
| 220 | fn froute to void |
| 221 | den = 0.0f64 |
| 222 | top = 0.0f64 |
| 223 | for rank ulong 10 |
| 224 | best = ulong 512 |
| 225 | for e ulong 512 |
| 226 | used = false |
| 227 | for j ulong rank |
| 228 | if eq ulong get qtop j e |
| 229 | used = true |
| 230 | end |
| 231 | end |
| 232 | if not used |
| 233 | if eq best 512 |
| 234 | best = e |
| 235 | else |
| 236 | if gt get fout e get fout best |
| 237 | best = e |
| 238 | end |
| 239 | end |
| 240 | end |
| 241 | end |
| 242 | put qtop rank uint best |
| 243 | if eq rank 0 |
| 244 | top = f64 get fout best |
| 245 | end |
| 246 | p = qb_exp sub f64 get fout best top |
| 247 | den = add den p |
| 248 | put qprob rank f32 p |
| 249 | end |
| 250 | for rank ulong 10 |
| 251 | put qprob rank qrb f32 div f64 get qprob rank den |
| 252 | end |
| 253 | note The reference accumulates expert outputs in ascending expert order. |
| 254 | for i ulong 10 |
| 255 | for j ulong sub 9 i |
| 256 | if gt get qtop j get qtop add j 1 |
| 257 | e = get qtop j |
| 258 | put qtop j get qtop add j 1 |
| 259 | put qtop add j 1 e |
| 260 | p = get qprob j |
| 261 | put qprob j get qprob add j 1 |
| 262 | put qprob add j 1 p |
| 263 | end |
| 264 | end |
| 265 | end |
| 266 | end |
| 267 | |
| 268 | note fin = silu(fga) * fout over the 640 expert channels. |
| 269 | fn fswiglu to void |
| 270 | for j ulong 640 |
| 271 | put fin j qrb mul fsilu get fga j get fout j |
| 272 | end |
| 273 | end |
| 274 | |
| 275 | fn fgate to void |
| 276 | for j ulong 640 |
| 277 | put fga j get fout j |
| 278 | end |
| 279 | end |
| 280 | |
| 281 | note Routed expert block. Reads fx, leaves the block output in fdy. |
| 282 | fn fmoe to void |
| 283 | layer = get qrun 1 |
| 284 | fxin |
| 285 | fmat qwroleof 22 |
| 286 | fgate |
| 287 | fxin |
| 288 | fmat qwroleof 23 |
| 289 | fswiglu |
| 290 | fmat qwroleof 24 |
| 291 | for j ulong 2560 |
| 292 | put fsh j get fout j |
| 293 | put fdy j 0.0f32 |
| 294 | end |
| 295 | fxin |
| 296 | fmat qwroleof 20 |
| 297 | froute |
| 298 | for r ulong 10 |
| 299 | key = mul add mul layer 512 ulong get qtop r 4 |
| 300 | fxin |
| 301 | fexpert key |
| 302 | fgate |
| 303 | fxin |
| 304 | fexpert add key 1 |
| 305 | fswiglu |
| 306 | fexpert add key 2 |
| 307 | wt = get qprob r |
| 308 | for j ulong 2560 |
| 309 | put fdy j qrb add get fdy j qrb mul get fout j wt |
| 310 | end |
| 311 | end |
| 312 | fxin |
| 313 | fmat qwroleof 21 |
| 314 | sg = fsig get fout 0 |
| 315 | for j ulong 2560 |
| 316 | put fdy j qrb add get fdy j qrb mul get fsh j sg |
| 317 | end |
| 318 | end |
| 319 | |
| 320 | note Lexical layer state: n-gram parameters, token history and nine past rows of the convolution input. |
| 321 | note fng: 0-2 multipliers, 3-18 head offsets, 19-34 head sizes, 35 previous token, 36 older token, 37 loaded. |
| 322 | global fng ulong 40 |
| 323 | global fngscale f32 1 |
| 324 | global fpast f32 92160 |
| 325 | fn fle64 j ulong to ulong |
| 326 | v = ulong 0 |
| 327 | for i ulong 8 |
| 328 | v = or v shl ulong get qbuf add mul j 8 i mul 8 i |
| 329 | end |
| 330 | ret v |
| 331 | end |
| 332 | |
| 333 | fn fngload to void |
| 334 | if eq get fng 37 1 |
| 335 | ret |
| 336 | end |
| 337 | qwread qwroleof 42 0 3 |
| 338 | for j ulong 3 |
| 339 | put fng j fle64 j |
| 340 | end |
| 341 | qwread qwroleof 43 0 16 |
| 342 | for j ulong 16 |
| 343 | put fng add 3 j fle64 j |
| 344 | end |
| 345 | qwread qwroleof 44 0 16 |
| 346 | for j ulong 16 |
| 347 | put fng add 19 j fle64 j |
| 348 | end |
| 349 | total = ulong 0 |
| 350 | for j ulong 16 |
| 351 | n = get fng add 19 j |
| 352 | if or ne get fng add 3 j total eq n 0 |
| 353 | qwfail 47 |
| 354 | end |
| 355 | total = add total n |
| 356 | end |
| 357 | if gt total 320001536 |
| 358 | qwfail 47 |
| 359 | end |
| 360 | for j ulong 3 |
| 361 | m = get fng j |
| 362 | if or eq m 0 gt m 37143239288394 |
| 363 | qwfail 47 |
| 364 | end |
| 365 | end |
| 366 | qwread qwroleof 45 0 1 |
| 367 | put fngscale 0 qbf 0 |
| 368 | put fng 37 1 |
| 369 | end |
| 370 | |
| 371 | note One hashed FP8 row, scaled and rounded to BF16, into fin at head h. |
| 372 | fn fnghead mix ulong h ulong to void |
| 373 | row = add get fng add 3 h rem mix get fng add 19 h |
| 374 | qwread ulong get qngram div row 2500012 mul rem row 2500012 160 160 |
| 375 | scale = get fngscale 0 |
| 376 | for j ulong 160 |
| 377 | put fin add mul h 160 j qrb mul scale bitcast f32 lqbits uint get qbuf j |
| 378 | end |
| 379 | end |
| 380 | |
| 381 | note The sixteen hashed rows of this token: eight from the bigram and eight from the trigram. |
| 382 | fn fngather tok ulong to void |
| 383 | bi = xor mul tok get fng 0 mul get fng 35 get fng 1 |
| 384 | tri = xor bi mul get fng 36 get fng 2 |
| 385 | for h ulong 8 |
| 386 | fnghead bi h |
| 387 | fnghead tri add h 8 |
| 388 | end |
| 389 | end |
| 390 | |
| 391 | note Signed square-root gate of one stream from its normalized key (fkn) and query (fnrm) rows. |
| 392 | fn flexgate s ulong to f32 |
| 393 | acc = 0.0f32 |
| 394 | for j ulong 2560 |
| 395 | i = add mul s 2560 j |
| 396 | acc = add acc qrb mul get fkn i get fnrm i |
| 397 | end |
| 398 | g = qrb div qrb acc 50.596442562694072f32 |
| 399 | mag = qrb fsqrt fmax fabs g 0.000001f32 |
| 400 | if lt g 0.0f32 |
| 401 | mag = neg mag |
| 402 | end |
| 403 | if eq g 0.0f32 |
| 404 | mag = 0.0f32 |
| 405 | end |
| 406 | ret fsig mag |
| 407 | end |
| 408 | |
| 409 | note Dilated depthwise convolution of channel c: taps at 9, 6 and 3 rows back, then the current row in fnrm. |
| 410 | fn flexconv c ulong w ulong to f32 |
| 411 | pos = get qrun 2 |
| 412 | slot = rem pos 9 |
| 413 | tap = add w mul c 4 |
| 414 | total = mul get fnrm c get fvp add tap 3 |
| 415 | for j ulong 3 |
| 416 | if ge pos sub 9 mul j 3 |
| 417 | idx = rem add slot mul j 3 9 |
| 418 | idx = add mul idx 10240 c |
| 419 | old = get fpast idx |
| 420 | total = add total mul old get fvp add tap j |
| 421 | end |
| 422 | end |
| 423 | ret fsilu qrb total |
| 424 | end |
| 425 | |
| 426 | fn flexhistory tok ulong to void |
| 427 | if eq get qrun 2 0 |
| 428 | put fng 35 248044 |
| 429 | put fng 36 248044 |
| 430 | end |
| 431 | fngather tok |
| 432 | put fng 36 get fng 35 |
| 433 | if eq tok 248044 |
| 434 | put fng 36 248044 |
| 435 | end |
| 436 | put fng 35 tok |
| 437 | end |
| 438 | |
| 439 | fn flexical tok ulong to void |
| 440 | fngload |
| 441 | flexhistory tok |
| 442 | note fin holds the n-gram embedding for both projections; products do not modify fin. |
| 443 | fmat qwroleof 41 |
| 444 | for j ulong 2560 |
| 445 | put fval j get fout j |
| 446 | end |
| 447 | fmat qwroleof 37 |
| 448 | fgnorm qwroleof 39 1 |
| 449 | for j ulong 10240 |
| 450 | put fkn j get fnrm j |
| 451 | end |
| 452 | fgnorm qwroleof 40 0 |
| 453 | for s ulong 4 |
| 454 | sg = flexgate s |
| 455 | for j ulong 2560 |
| 456 | put fgv add mul s 2560 j qrb mul sg get fval j |
| 457 | end |
| 458 | end |
| 459 | fgnorm qwroleof 38 2 |
| 460 | w = fvec qwroleof 36 |
| 461 | for c ulong 10240 |
| 462 | put fh c qrb add get fh c qrb add get fgv c flexconv c w |
| 463 | end |
| 464 | slot = rem get qrun 2 9 |
| 465 | for c ulong 10240 |
| 466 | put fpast add mul slot 10240 c get fnrm c |
| 467 | end |
| 468 | end |
| 469 | |
| 470 | fn flayer tok ulong to void |
| 471 | if eq get qrun 1 1 |
| 472 | flexical tok |
| 473 | end |
| 474 | fhyper qwroleof 25 qwroleof 26 qwroleof 27 qwroleof 28 |
| 475 | if eq rem get qrun 1 4 3 |
| 476 | fattention |
| 477 | else |
| 478 | fdelta |
| 479 | end |
| 480 | fdebughalf |
| 481 | fapply |
| 482 | fhyper qwroleof 29 qwroleof 30 qwroleof 31 qwroleof 32 |
| 483 | fmoe |
| 484 | fdebughalf |
| 485 | fapply |
| 486 | end |
| 487 | |
| 488 | fn qargmax n ulong to ulong |
| 489 | best = ulong 0 |
| 490 | for j ulong n |
| 491 | b = bitcast uint get fout j |
| 492 | if eq and b 2139095040 2139095040 |
| 493 | qwfail 54 |
| 494 | end |
| 495 | if gt get fout j get fout best |
| 496 | best = j |
| 497 | end |
| 498 | end |
| 499 | ret best |
| 500 | end |
| 501 | |
| 502 | note Instrument: with fdebug set, the four residual streams after every layer go to stdout as BF16. |
| 503 | global fdebug ulong 1 |
| 504 | fn fdebugrow to void |
| 505 | for j ulong 10240 |
| 506 | bits = shr bitcast uint get fh j 16 |
| 507 | tkpc ulong and bits 255 |
| 508 | tkpc ulong shr bits 8 |
| 509 | end |
| 510 | end |
| 511 | |
| 512 | note Instrument: with fdebug 2, the block input, injection weights and block output of both halves of |
| 513 | note layers 0 to 3 go to stdout as raw binary32, 5124 values per half. |
| 514 | fn fdebugword bits uint to void |
| 515 | tkpc ulong and bits 255 |
| 516 | tkpc ulong and shr bits 8 255 |
| 517 | tkpc ulong and shr bits 16 255 |
| 518 | tkpc ulong shr bits 24 |
| 519 | end |
| 520 | |
| 521 | fn fdebughalf to void |
| 522 | if or ne get fdebug 0 2 ge get qrun 1 4 |
| 523 | ret |
| 524 | end |
| 525 | for j ulong 2560 |
| 526 | fdebugword bitcast uint get fx j |
| 527 | end |
| 528 | for j ulong 4 |
| 529 | fdebugword bitcast uint get finj j |
| 530 | end |
| 531 | for j ulong 2560 |
| 532 | fdebugword bitcast uint get fdy j |
| 533 | end |
| 534 | end |
| 535 | |
| 536 | note One token through the model. With logits the final mixed row stays in fx and the vocabulary scores in fout. |
| 537 | fn qtoken tok ulong pos ulong logits bool to ulong |
| 538 | if or ge tok 248320 ge pos 2048 |
| 539 | qwfail 55 |
| 540 | end |
| 541 | qdiskrecheck |
| 542 | put qrun 2 pos |
| 543 | qwread ulong get qglob 0 mul tok 2560 2560 |
| 544 | for j ulong 2560 |
| 545 | v = qbf j |
| 546 | put fh j v |
| 547 | put fh add 2560 j v |
| 548 | put fh add 5120 j v |
| 549 | put fh add 7680 j v |
| 550 | end |
| 551 | for l ulong 48 |
| 552 | put qrun 1 l |
| 553 | flayer tok |
| 554 | if eq get fdebug 0 1 |
| 555 | fdebugrow |
| 556 | end |
| 557 | end |
| 558 | if not logits |
| 559 | ret 0 |
| 560 | end |
| 561 | fhyper ulong get qglob 2 ulong get qglob 3 ulong get qglob 4 0 |
| 562 | fxin |
| 563 | fmat ulong get qglob 1 |
| 564 | qdiskrecheck |
| 565 | ret qargmax 248320 |
| 566 | end |