Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/om/flint/ops.om # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / om / flint / ops.om` 1836 lines ยท 34466 bytes Raw Blame History 1 note flint-ops: forward-operator parity for the pinned Flint checkpoint 2 note (aien-architecture-mv#353, aien-sovereign-core-mv#802, P0 forward lane). 3 note 4 note Usage: flint-ops OP FIXTURE INVENTORY SHARD_DIR 5 note flint-ops selftest 6 note OP is rmsnorm, qkv, attn, router, shared, expert or moe. FIXTURE is written by 7 note test/opref.py from the pinned reference modules; INVENTORY is the TSV flint-manifest 8 note printed for SHARD_DIR. Each operator recomputes the reference output in f64 from the 9 note real BF16 shard weights and carries a first-order bound E on the error of the 10 note reference's own f32 computation (gamma_n = n u / (1 - n u), u = 2^-24). An element 11 note passes when |om - ref| <= 2 E. The router reports a token as a tie, not a mismatch, when 12 note the reference could hold the om rank-7 expert in its top-6 within those per-element bounds. 13 note 14 note Prints one line: flint-op OP layer L elems N worst_ppm P ties K PASS|FAIL, where P is 15 note max |om - ref| / (2 E) in millionths. Exit 0 PASS, 1 FAIL, 2 and up refused. 16 note 17 note Layer kinds follow the pinned config.json (manifest CFG_PIN): no_rope_layer_indices 18 note 0, 1, 5, 9, .., 45, 48 use full attention without RoPE; every other layer uses 19 note RoPE and the sliding window of FWIN keys including the query itself. 20 21 const FWIN ulong 513 22 const FHID ulong 2048 23 note FULP: libm budget in ulps for each of the reference's powf, cosf and sinf calls in RoPE. 24 note glibc 2.39 lists 1 ulp for AArch64 powf/cosf/sinf, SLEEF documents 1.0 (u10) and 3.5 (u35); 25 note 4 covers all of them (test/TOLERANCE.md, RoPE libm budget). test/opref.py reads this line, 26 note measures the reference's calls against float64 and refuses fixtures above FULP ulps. 27 const FULP ulong 4 28 global ga uchar 16384 29 global gas ulong 8 30 global gal ulong 8 31 global gpth uchar 4352 32 global gfx uchar `8388608` 33 global gtsv uchar `8388608` 34 global gstg uchar `4194304` 35 global gw f32 `16777216` 36 global gv f64 8192 37 global gve f64 8192 38 global gacc f64 16 39 global gy f64 4096 40 global gye f64 4096 41 global gx0 f64 `1048576` 42 global ge0 f64 `1048576` 43 global gx1 f64 `1048576` 44 global ge1 f64 `1048576` 45 global gx2 f64 `1048576` 46 global ge2 f64 `1048576` 47 global gx3 f64 `1048576` 48 global ge3 f64 `1048576` 49 global gacm f64 16384 50 global gace f64 16384 51 global gaca f64 16384 52 global gsc f64 1024 53 global gse f64 1024 54 global gsg f64 1024 55 global gsge f64 1024 56 global gsk f64 64 57 global gch uchar 256 58 global gtk ulong 8 59 global gtw f64 8 60 global gtwe f64 8 61 global gmk ulong 512 62 global gmw f64 512 63 global gmwe f64 512 64 global gtie uchar 64 65 global gcm f64 8 66 global gst ulong 32 67 global gnm uchar 256 68 global gob uchar 4096 69 global gdg uchar 24 70 global gh8 uchar 8 71 global gstb uchar 128 72 global gfh f64 4096 73 global gfa f64 4096 74 global gfq f64 8192 75 global gfo f64 8192 76 global gfs ulong 4 77 text tofwd "forward" 78 text tembd "model.embed_tokens.weight" 79 text tfnorm "model.norm.weight" 80 text tlmhead "lm_head.weight" 81 text tdense "mlp." 82 83 text tu "usage: flint-ops OP FIXTURE INVENTORY SHARD_DIR | flint-ops selftest" 84 text tio "io" 85 text tnreg "not-regular-file" 86 text tfx "fixture" 87 text tnf "tensor-not-in-inventory" 88 text tdt "tensor-dtype-or-shape" 89 text tref "refused " 90 text tfop "flint-op " 91 text tlay " layer " 92 text telm " elems " 93 text tppm " worst_ppm " 94 text tties " ties " 95 text tpass " PASS" 96 text tfail " FAIL" 97 text tbf "BF16" 98 text tpre "model.layers." 99 text tln0 "input_layernorm.weight" 100 text tln1 "post_attention_layernorm.weight" 101 text tq "self_attn.q_proj.weight" 102 text tk "self_attn.k_proj.weight" 103 text tv "self_attn.v_proj.weight" 104 text tob "self_attn.o_proj.weight" 105 text tqn "self_attn.q_norm.weight" 106 text tkn "self_attn.k_norm.weight" 107 text tsnk "self_attn.softmax_offset" 108 text trt "mlp.gate.weight" 109 text tshp "mlp.shared_expert." 110 text texp "mlp.experts." 111 text tgp "gate_proj.weight" 112 text tup "up_proj.weight" 113 text tdp "down_proj.weight" 114 text tormsn "rmsnorm" 115 text toqkv "qkv" 116 text toatt "attn" 117 text tortr "router" 118 text toshr "shared" 119 text toexp "expert" 120 text tomoe "moe" 121 text tostt "selftest" 122 123 note gst slots: 0 fixture bytes, 1 inventory bytes, 2 op, 3 layer, 4 T, 5 S, 6 aux, 124 note 7 tensor-name length, 8 output fill, 9 fstat size, 10 shard-name start in gtsv, 125 note 11 shard-name length, 12 begin, 13 end, 14 rows, 15 cols. 126 note gcm slots: 0 worst |om - ref| / (2 E), 1 failures, 2 elements, 3 ties. 127 128 fn op_dec v ulong to ulong 129 n = ulong 0 130 x = v 131 while true bound 25 132 put gdg n add 48 uchar rem x 10 133 n = add n 1 134 x = div x 10 135 if eq x 0 136 break 137 end 138 end 139 ret n 140 end 141 142 fn ob_c c uchar to void 143 p = get gst 8 144 if lt p 4096 145 put gob p c 146 put gst 8 add p 1 147 end 148 end 149 150 fn ob_tx t view uchar * to void 151 for i ulong len t 152 ob_c get t i 153 end 154 end 155 156 fn ob_u v ulong to void 157 n = op_dec v 158 for i ulong n 159 ob_c get gdg sub sub n i 1 160 end 161 end 162 163 fn ob_flush fd int to void 164 o = ulong 0 165 m = get gst 8 166 while lt o m bound 4096 167 w = write fd gob o sub m o 168 if le w 0 169 exit 3 170 end 171 o = add o ulong w 172 end 173 put gst 8 0 174 end 175 176 fn op_die c ulong t view uchar * to void 177 put gst 8 0 178 ob_tx tref 179 ob_u c 180 ob_c 32 181 ob_tx t 182 ob_c 10 183 ob_flush 2 184 exit int c 185 end 186 187 fn op_le64 o ulong to ulong 188 v = ulong 0 189 for i ulong 8 190 v = or v shl ulong get gstb add o i mul 8 i 191 end 192 ret v 193 end 194 195 fn op_open n ulong to long 196 fd = fopen gpth 0 n 557056 0 197 if lt fd 0 198 if eq fd -40 199 op_die 4 tnreg 200 end 201 op_die 3 tio 202 end 203 r = fstat int fd gstb 0 128 204 if ne r 0 205 op_die 3 tio 206 end 207 note S_IFMT sits in the low 16 bits of st_mode 208 md = or ulong get gstb 16 shl ulong get gstb 17 8 209 if ne and md 61440 32768 210 op_die 4 tnreg 211 end 212 put gst 9 op_le64 48 213 ret fd 214 end 215 216 fn op_close fd long to void 217 r = fclose int fd 218 if ne r 0 219 op_die 3 tio 220 end 221 end 222 223 fn op_argp a ulong to ulong 224 n = get gal a 225 if or eq n 0 gt n 4095 226 op_die 2 tu 227 end 228 o = get gas a 229 for i ulong n 230 put gpth i get ga add o i 231 end 232 ret n 233 end 234 235 fn op_argeq a ulong t view uchar * to bool 236 n = get gal a 237 if ne n len t 238 ret false 239 end 240 o = get gas a 241 for i ulong n 242 if ne get ga add o i get t i 243 ret false 244 end 245 end 246 ret true 247 end 248 249 fn rd_fx to void 250 fd = op_open op_argp 2 251 n = get gst 9 252 if or lt n 32 gt n len gfx 253 op_die 7 tfx 254 end 255 o = ulong 0 256 while lt o n bound 999999 257 r = pread int fd gfx o sub n o long o 258 if le r 0 259 op_die 3 tio 260 end 261 o = add o ulong r 262 end 263 op_close fd 264 put gst 0 n 265 end 266 267 fn rd_tsv to void 268 fd = op_open op_argp 3 269 n = get gst 9 270 if gt n len gtsv 271 op_die 5 tnf 272 end 273 o = ulong 0 274 while lt o n bound 999999 275 r = pread int fd gtsv o sub n o long o 276 if le r 0 277 op_die 3 tio 278 end 279 o = add o ulong r 280 end 281 op_close fd 282 put gst 1 n 283 end 284 285 note fixture words are little-endian u32 or f32 286 fn fx_w i ulong to ulong 287 b = mul i 4 288 v = or ulong get gfx b shl ulong get gfx add b 1 8 289 v = or v shl ulong get gfx add b 2 16 290 v = or v shl ulong get gfx add b 3 24 291 ret v 292 end 293 294 fn fx_f i ulong to f64 295 ret f64 bitcast f32 uint fx_w i 296 end 297 298 fn fx_need w ulong to void 299 if ne get gst 0 mul 4 w 300 op_die 7 tfx 301 end 302 end 303 304 note load T rows of 2048 fixture words at word xo into gx0, with zero error in ge0 305 fn fx_x0 xo ulong T ulong to void 306 for i ulong mul T FHID 307 put gx0 i fx_f add xo i 308 put ge0 i 0.0f64 309 end 310 end 311 312 fn gam n ulong to f64 313 x = mul f64 n 5.`9604644775390625e`-8f64 314 ret div x sub 1.0f64 x 315 end 316 317 fn fl_swa L ulong to bool 318 ret not or or lt L 2 eq L 48 eq rem L 4 1 319 end 320 321 note cmp: one output element, om against the reference value ref, passing when 322 note |om - ref| <= 2 e; a NaN anywhere fails 323 fn cmp om f64 ref f64 e f64 to void 324 d = fabs sub om ref 325 t = mul 2.0f64 e 326 put gcm 2 add get gcm 2 1.0f64 327 if not le d t 328 put gcm 1 add get gcm 1 1.0f64 329 end 330 if gt d 0.0f64 331 r = div d fabs t 332 if gt r get gcm 0 333 put gcm 0 r 334 end 335 end 336 end 337 338 fn cm_reset to void 339 for i ulong 8 340 put gcm i 0.0f64 341 end 342 end 343 344 fn report nm view uchar * to int 345 put gst 8 0 346 ob_tx tfop 347 ob_tx nm 348 ob_tx tlay 349 ob_u get gst 3 350 ob_tx telm 351 ob_u ulong get gcm 2 352 ob_tx tppm 353 ob_u ulong mul get gcm 0 `1000000`.0f64 354 ob_tx tties 355 ob_u ulong get gcm 3 356 f = gt get gcm 1 0.0f64 357 if f 358 ob_tx tfail 359 else 360 ob_tx tpass 361 end 362 ob_c 10 363 ob_flush 1 364 if f 365 ret 1 366 end 367 ret 0 368 end 369 370 note f64 exp: k = round(x / ln 2), Cody-Waite reduction with ln2_hi (32 bits) and 371 note ln2_lo, Taylor degree 13 on |r| <= ln 2 / 2, scale by 2^k through the exponent field 372 fn om_exp x f64 to f64 373 if lt x -708.0f64 374 ret 0.0f64 375 end 376 if gt x 709.0f64 377 ret div 1.0f64 0.0f64 378 end 379 kf = mul x 1.`442695040888963407f64` 380 if lt kf 0.0f64 381 kf = sub kf 0.5f64 382 else 383 kf = add kf 0.5f64 384 end 385 k = long kf 386 fk = f64 k 387 r = sub sub x mul fk 0.`6931471803691238165f64` mul fk 1.`908214929270587816e`-10f64 388 p = 1.0f64 389 for j ulong 13 390 p = add 1.0f64 mul div r f64 sub 13 j p 391 end 392 s = bitcast f64 shl ulong add k 1023 52 393 ret mul p s 394 end 395 396 fn om_sig x f64 to f64 397 ret div 1.0f64 add 1.0f64 om_exp neg x 398 end 399 400 note sin (c false) or cos (c true) of x in [0, 2^20): k = round(x 2 / pi), reduction by 401 note pio2_hi (33 bits) and pio2_lo, Taylor through degree 21 on |r| <= pi / 4 402 fn om_sc x f64 c bool to f64 403 k = ulong add mul x 0.`6366197723675813431f64` 0.5f64 404 fk = f64 k 405 r = sub sub x mul fk 1.`570796326734125614f64` mul fk 6.`077100506506192601e`-11f64 406 z = mul r r 407 s = 1.0f64 408 co = 1.0f64 409 for j ulong 10 410 m = f64 mul 2 sub 10 j 411 s = sub 1.0f64 mul div z mul m add m 1.0f64 s 412 co = sub 1.0f64 mul div z mul sub m 1.0f64 m co 413 end 414 s = mul r s 415 q = k 416 if c 417 q = add q 1 418 end 419 q = and q 3 420 if eq q 0 421 ret s 422 end 423 if eq q 1 424 ret co 425 end 426 if eq q 2 427 ret neg s 428 end 429 ret neg co 430 end 431 432 note tensor names: model.layers.L. followed by a suffix 433 fn nm_c c uchar to void 434 n = get gst 7 435 if ge n 256 436 op_die 5 tnf 437 end 438 put gnm n c 439 put gst 7 add n 1 440 end 441 442 fn nm_tx t view uchar * to void 443 for i ulong len t 444 nm_c get t i 445 end 446 end 447 448 fn nm_u v ulong to void 449 n = op_dec v 450 for i ulong n 451 nm_c get gdg sub sub n i 1 452 end 453 end 454 455 fn nm_lay L ulong to void 456 put gst 7 0 457 nm_tx tpre 458 nm_u L 459 nm_c 46 460 end 461 462 fn tsv_eqn p ulong n ulong to bool 463 if gt add add p n 1 get gst 1 464 ret false 465 end 466 for i ulong n 467 if ne get gtsv add p i get gnm i 468 ret false 469 end 470 end 471 ret eq get gtsv add p n 9 472 end 473 474 fn tsv_nl p ulong to ulong 475 m = get gst 1 476 q = p 477 while lt q m bound 99999 478 if eq get gtsv q 10 479 ret add q 1 480 end 481 q = add q 1 482 end 483 ret m 484 end 485 486 note index of the tab or newline that ends the field starting at p 487 fn tsv_fld p ulong to ulong 488 m = get gst 1 489 q = p 490 while lt q m bound 99999 491 c = get gtsv q 492 if or eq c 9 eq c 10 493 ret q 494 end 495 q = add q 1 496 end 497 op_die 5 tnf 498 ret m 499 end 500 501 fn tsv_num p ulong e ulong to ulong 502 if or ge p e gt sub e p 19 503 op_die 6 tdt 504 end 505 v = ulong 0 506 for i ulong sub e p 507 c = get gtsv add p i 508 if or lt c 48 gt c 57 509 op_die 6 tdt 510 end 511 v = add mul v 10 ulong sub c 48 512 end 513 ret v 514 end 515 516 fn tsv_bf p ulong e ulong to bool 517 if ne sub e p 4 518 ret false 519 end 520 for i ulong 4 521 if ne get gtsv add p i get tbf i 522 ret false 523 end 524 end 525 ret true 526 end 527 528 fn tsv_x p ulong e ulong to ulong 529 q = p 530 while lt q e bound 99999 531 if eq get gtsv q 120 532 ret q 533 end 534 q = add q 1 535 end 536 ret e 537 end 538 539 note row fields after the name: shard, dtype, shape (R or RxC), begin, end 540 fn tsv_row p ulong to void 541 e = tsv_fld p 542 put gst 10 p 543 put gst 11 sub e p 544 p2 = add e 1 545 e2 = tsv_fld p2 546 if not tsv_bf p2 e2 547 op_die 6 tdt 548 end 549 p3 = add e2 1 550 e3 = tsv_fld p3 551 x = tsv_x p3 e3 552 put gst 14 tsv_num p3 x 553 put gst 15 0 554 if lt x e3 555 put gst 15 tsv_num add x 1 e3 556 end 557 p4 = add e3 1 558 e4 = tsv_fld p4 559 put gst 12 tsv_num p4 e4 560 p5 = add e4 1 561 e5 = tsv_fld p5 562 put gst 13 tsv_num p5 e5 563 end 564 565 fn tsv_find to void 566 n = get gst 7 567 m = get gst 1 568 p = ulong 0 569 f = false 570 while lt p m bound 99999 571 if tsv_eqn p n 572 tsv_row add p add n 1 573 f = true 574 break 575 end 576 p = tsv_nl p 577 end 578 if not f 579 op_die 5 tnf 580 end 581 end 582 583 fn sh_open to long 584 d = get gal 4 585 if or eq d 0 gt d 2048 586 op_die 2 tu 587 end 588 n = op_argp 4 589 put gpth n 47 590 n = add n 1 591 s = get gst 10 592 k = get gst 11 593 if gt k 1024 594 op_die 5 tnf 595 end 596 for i ulong k 597 put gpth add n i get gtsv add s i 598 end 599 ret op_open add n k 600 end 601 602 fn pf_stg fd long n ulong pos ulong to void 603 o = ulong 0 604 while lt o n bound 999999 605 r = pread int fd gstg o sub n o long add pos o 606 if le r 0 607 op_die 3 tio 608 end 609 o = add o ulong r 610 end 611 end 612 613 note a BF16 value is the high half of the f32 with the same bits, so the decode is exact 614 fn dec_stg c ulong d ulong to void 615 for i ulong div c 2 616 lo = uint get gstg mul i 2 617 hi = uint get gstg add mul i 2 1 618 put gw add d i bitcast f32 or shl hi 24 shl lo 16 619 end 620 end 621 622 note ld_t: the tensor named in gnm must be BF16 of shape rows x cols (cols 0: a vector); 623 note it lands in gw[0 ..) as f32 624 fn ld_t rows ulong cols ulong to void 625 tsv_find 626 if or ne get gst 14 rows ne get gst 15 cols 627 op_die 6 tdt 628 end 629 k = rows 630 if gt cols 0 631 k = mul rows cols 632 end 633 nb = mul k 2 634 if or ne sub get gst 13 get gst 12 nb gt k len gw 635 op_die 6 tdt 636 end 637 ld_bytes nb 0 638 end 639 640 note ld_rows: bounded row slice of a manifest-admitted BF16 matrix, never allocate the vocabulary. 641 fn ld_rows rows ulong cols ulong first ulong count ulong to void 642 tsv_find 643 if or ne get gst 14 rows ne get gst 15 cols 644 op_die 6 tdt 645 end 646 if or gt first rows gt count sub rows first 647 op_die 6 tdt 648 end 649 n = mul count cols 650 if gt n len gw 651 op_die 6 tdt 652 end 653 size = mul mul rows cols 2 654 extent = sub get gst 13 get gst 12 655 if ne extent size 656 op_die 6 tdt 657 end 658 ld_bytes mul n 2 mul mul first cols 2 659 end 660 661 fn ld_bytes nb ulong off ulong to void 662 fd = sh_open 663 r = pread int fd gh8 0 8 long 0 664 if ne r 8 665 op_die 3 tio 666 end 667 hl = ulong 0 668 for i ulong 8 669 hl = or hl shl ulong get gh8 i mul 8 i 670 end 671 if gt hl `100000000` 672 op_die 3 tio 673 end 674 base = add add add 8 hl get gst 12 off 675 o = ulong 0 676 while lt o nb bound 99999 677 c = sub nb o 678 if gt c `1048576` 679 c = ulong `1048576` 680 end 681 pf_stg fd c add base o 682 dec_stg c div o 2 683 o = add o c 684 end 685 op_close fd 686 end 687 688 note dotr: row r of gw (K columns) against gv; gacc 1 gets sum |w v|, gacc 2 gets 689 note sum |w| gve; returns sum w v 690 fn dotr r ulong k ulong to f64 691 b = mul r k 692 s = 0.0f64 693 a = 0.0f64 694 e = 0.0f64 695 for i ulong k 696 w = f64 get gw add b i 697 p = mul w get gv i 698 s = add s p 699 a = add a fabs p 700 e = add e mul fabs w get gve i 701 end 702 put gacc 1 a 703 put gacc 2 e 704 ret s 705 end 706 707 note error bound of the last dotr: propagated input error plus gamma_K sum |w v| 708 fn dote k ulong to f64 709 ret add get gacc 2 mul gam k get gacc 1 710 end 711 712 note matdot: forward integration has its own fixed comparison budget, not dote bounds. 713 note Avoid accumulating two unused per-product error sums there. Keep the f64 products 714 note and reduction order identical to dotr. Router dotr remains bounded for tie reporting; 715 note every local operator still calls the original bounded dotr path. 716 fn matdot r ulong k ulong to f64 717 if ne get gst 2 8 718 ret dotr r k 719 end 720 b = mul r k 721 s = 0.0f64 722 for i ulong k 723 w = f64 get gw add b i 724 p = mul w get gv i 725 s = add s p 726 end 727 put gacc 1 0.0f64 728 put gacc 2 0.0f64 729 ret s 730 end 731 732 note rmsv: RMSNorm of gv[0 .. n) with weight gw[wo ..) into gy, bound into gye: 733 note E_i = |w_i| r (E_xi + |x_i| r^2 / n sum |x_j| E_xj) + gamma_(n+6) |y_i| 734 fn rms_rc n ulong to void 735 ss = 0.0f64 736 ea = 0.0f64 737 for i ulong n 738 v = get gv i 739 ss = add ss mul v v 740 ea = add ea mul fabs v get gve i 741 end 742 eps = f64 f32 0.000001 743 r = div 1.0f64 fsqrt add div ss f64 n eps 744 put gacc 6 r 745 put gacc 7 div mul mul r r ea f64 n 746 end 747 748 fn rmsv n ulong wo ulong to void 749 rms_rc n 750 r = get gacc 6 751 c = get gacc 7 752 g = gam add n 6 753 for i ulong n 754 w = f64 get gw add wo i 755 v = get gv i 756 y = mul mul w v r 757 put gy i y 758 q = add get gve i mul fabs v c 759 q = mul q mul fabs w r 760 put gye i add q mul g fabs y 761 end 762 end 763 764 note rope_cs: cos and sin of pos inv_freq_i, inv_freq_i = exp(-(2 i / 128) ln 10000), into 765 note gacc 3 and 4; gacc 5 gets the reference's angle and cos/sin error under FULP ulps per 766 note libm call (1 ulp <= 2u relative): the angle 1 / pow(10000, 2i/128) * pos carries 767 note gamma_(2 FULP + 2) relative, and cos/sin add gamma_(2 FULP) absolute 768 fn rope_cs pos ulong i ulong to void 769 f = om_exp neg mul f64 i div 9.`210340371976182736f64` 64.0f64 770 th = mul f64 pos f 771 put gacc 3 om_sc th true 772 put gacc 4 om_sc th false 773 put gacc 5 add mul th gam add mul 2 FULP 2 gam mul 2 FULP 774 end 775 776 note rope_one: rotate_half RoPE of the pair (i, i + 64) of gy at pos, in place; 777 note E_out = |c| E_a + |s| E_b + (|a| + |b|) E_cs + gamma_2 (|a c| + |b s|) 778 fn rope_one pos ulong i ulong to void 779 rope_cs pos i 780 g2 = gam 2 781 c = get gacc 3 782 s = get gacc 4 783 a = get gy i 784 b = get gy add i 64 785 x = mul add fabs a fabs b get gacc 5 786 t = add fabs mul a c fabs mul b s 787 put gacc 9 add x mul g2 t 788 t = add fabs mul b c fabs mul a s 789 put gacc 10 add x mul g2 t 790 put gy i sub mul a c mul b s 791 put gy add i 64 add mul b c mul a s 792 a = get gye i 793 b = get gye add i 64 794 t = add mul fabs c a mul fabs s b 795 put gye i add get gacc 9 t 796 t = add mul fabs c b mul fabs s a 797 put gye add i 64 add get gacc 10 t 798 end 799 800 fn ropev pos ulong to void 801 for i ulong 64 802 rope_one pos i 803 end 804 end 805 806 fn op_rmsn to int 807 L = get gst 3 808 T = get gst 4 809 aux = get gst 6 810 if or or eq T 0 gt T 512 gt aux 1 811 op_die 7 tfx 812 end 813 fx_need add 8 mul T 4096 814 nm_lay L 815 if eq aux 0 816 nm_tx tln0 817 else 818 nm_tx tln1 819 end 820 ld_t FHID 0 821 yo = add 8 mul T FHID 822 for t ulong T 823 for i ulong FHID 824 put gv i fx_f add 8 add mul t FHID i 825 put gve i 0.0f64 826 end 827 rmsv FHID 0 828 for i ulong FHID 829 cmp get gy i fx_f add yo add mul t FHID i get gye i 830 end 831 end 832 r = report tormsn 833 ret r 834 end 835 836 note cmp_x1: gx1/ge1[o .. o + n) against fixture words fo + o .. 837 fn cmp_x1 o ulong n ulong fo ulong to void 838 for i ulong n 839 j = add o i 840 cmp get gx1 j fx_f add fo j get ge1 j 841 end 842 end 843 844 note projfx: rows x 2048 weights in gw against the S fixture rows at word xo, into gx1/ge1 845 fn projfx rows ulong xo ulong S ulong to void 846 for t ulong S 847 for i ulong FHID 848 put gv i fx_f add xo add mul t FHID i 849 put gve i 0.0f64 850 end 851 for r ulong rows 852 o = add mul t rows r 853 put gx1 o dotr r FHID 854 put ge1 o dote FHID 855 end 856 end 857 end 858 859 note hnorm: head h of row t of gx1 (nh heads of 128) through q_norm/k_norm in gw, RoPE at 860 note pos when rp, compared with the fixture head at word ref 861 fn hnorm t ulong h ulong nh ulong rp bool ref ulong to void 862 b = mul add mul t nh h 128 863 for d ulong 128 864 put gv d get gx1 add b d 865 put gve d get ge1 add b d 866 end 867 rmsv 128 0 868 if rp 869 ropev fx_w add 8 t 870 end 871 for d ulong 128 872 cmp get gy d fx_f add ref add b d get gye d 873 end 874 end 875 876 fn op_qkv to int 877 L = get gst 3 878 S = get gst 5 879 if or eq S 0 gt S 64 880 op_die 7 tfx 881 end 882 xo = add 8 S 883 qo = add xo mul S FHID 884 ko = add qo mul S 4096 885 vo = add ko mul S 512 886 fx_need add vo mul S 512 887 rp = fl_swa L 888 nm_lay L 889 nm_tx tq 890 ld_t 4096 FHID 891 projfx 4096 xo S 892 nm_lay L 893 nm_tx tqn 894 ld_t 128 0 895 for t ulong S 896 for h ulong 32 897 hnorm t h 32 rp qo 898 end 899 end 900 nm_lay L 901 nm_tx tk 902 ld_t 512 FHID 903 projfx 512 xo S 904 nm_lay L 905 nm_tx tkn 906 ld_t 128 0 907 for t ulong S 908 for h ulong 4 909 hnorm t h 4 rp ko 910 end 911 end 912 nm_lay L 913 nm_tx tv 914 ld_t 512 FHID 915 projfx 512 xo S 916 cmp_x1 0 mul S 512 vo 917 r = report toqkv 918 ret r 919 end 920 921 note qk_dot: gv[0 .. 128) against gx2[kb ..); gacc 1 gets sum |q k| 922 fn qk_dot kb ulong to f64 923 s = 0.0f64 924 a = 0.0f64 925 for d ulong 128 926 x = mul get gv d get gx2 add kb d 927 s = add s x 928 a = add a fabs x 929 end 930 put gacc 1 a 931 ret s 932 end 933 934 note scores of the query head in gv against keys lo .. p of kv head g (gx2) into gsc/gse; 935 note E_s = gamma_130 scale sum |q k| (the fixture q and k are exact inputs); returns count 936 fn att_sc g ulong p ulong lo ulong to ulong 937 sc = 0.`08838834764831844055f64` 938 es = mul gam 130 sc 939 n = add sub p lo 1 940 for j ulong n 941 s = qk_dot mul add mul add lo j 4 g 128 942 put gsc j mul sc s 943 put gse j mul es get gacc 1 944 end 945 ret n 946 end 947 948 note att_sm: softmax over the n scores and the sink sk, in place: gsc becomes p_j, gse the 949 note relative bound rho_j = eps_j + eps_Z + gamma_(T+4), eps_j = E_sj + E_m + u |s_j - m| + 950 note gamma_2, eps_Z = sum_k e_k eps_k / Z (sink included, weighted by its own term) 951 fn sm_max n ulong sk f64 to void 952 m = sk 953 em = 0.0f64 954 for j ulong n 955 m = fmax m get gsc j 956 em = fmax em get gse j 957 end 958 put gacc 6 m 959 put gacc 7 em 960 end 961 962 note sm_eps: eps for a score x with bound e against the max m 963 fn sm_eps x f64 e f64 to f64 964 u = 5.`9604644775390625e`-8f64 965 t = add e get gacc 7 966 t = add t mul u fabs sub x get gacc 6 967 ret add t gam 2 968 end 969 970 fn sm_exp n ulong to void 971 m = get gacc 6 972 z = 0.0f64 973 ez = 0.0f64 974 for j ulong n 975 x = get gsc j 976 ex = om_exp sub x m 977 ej = sm_eps x get gse j 978 put gsc j ex 979 put gse j ej 980 z = add z ex 981 ez = add ez mul ex ej 982 end 983 put gacc 8 z 984 put gacc 9 ez 985 end 986 987 fn att_sm n ulong sk f64 T ulong to void 988 sm_max n sk 989 sm_exp n 990 es = om_exp sub sk get gacc 6 991 z = add get gacc 8 es 992 ez = add get gacc 9 mul es sm_eps sk 0.0f64 993 ez = add div ez z gam add T 4 994 for j ulong n 995 put gsc j div get gsc j z 996 put gse j add get gse j ez 997 end 998 end 999 1000 note one head h of selected row t at position p: core output into gx1/ge1 row t; 1001 note E_o = sum_j p_j |v_jd| (rho_j + gamma_T) 1002 fn att_hd t ulong h ulong p ulong lo ulong qo ulong to void 1003 g = div h 8 1004 qb = mul add mul t 32 h 128 1005 for d ulong 128 1006 put gv d fx_f add qo add qb d 1007 end 1008 n = att_sc g p lo 1009 att_sm n get gsk h get gst 4 1010 put gacc 11 gam get gst 4 1011 kb = mul add mul lo 4 g 128 1012 for d ulong 128 1013 put gx1 add qb d pv_d n add kb d 1014 put ge1 add qb d get gacc 1 1015 end 1016 end 1017 1018 note pv_d: sum_j p_j v_jd over the n kept keys, v of key j at gx3[b + 512 j]; gacc 1 gets 1019 note the bound sum_j p_j |v_jd| (rho_j + gamma_T), gamma_T in gacc 11 1020 fn pv_d n ulong b ulong to f64 1021 s = 0.0f64 1022 e = 0.0f64 1023 for j ulong n 1024 pv = mul get gsc j get gx3 add b mul j 512 1025 s = add s pv 1026 r = add get gse j get gacc 11 1027 e = add e mul fabs pv r 1028 end 1029 put gacc 1 e 1030 ret s 1031 end 1032 1033 note att_row: all 32 heads of selected row t, compared with the fixture core output at oo 1034 fn att_row t ulong qo ulong oo ulong to void 1035 p = fx_w add 8 t 1036 if ge p get gst 4 1037 op_die 7 tfx 1038 end 1039 lo = ulong 0 1040 if and fl_swa get gst 3 gt add p 1 FWIN 1041 lo = sub add p 1 FWIN 1042 end 1043 for h ulong 32 1044 att_hd t h p lo qo 1045 end 1046 cmp_x1 mul t 4096 4096 oo 1047 end 1048 1049 note att_y: o_proj (in gw) of core row t of gx1/ge1, compared with the fixture at yo 1050 fn att_y t ulong yo ulong to void 1051 b = mul t 4096 1052 for i ulong 4096 1053 put gv i get gx1 add b i 1054 put gve i get ge1 add b i 1055 end 1056 for r ulong FHID 1057 y = dotr r 4096 1058 cmp y fx_f add yo add mul t FHID r dote 4096 1059 end 1060 end 1061 1062 fn att_kv ko ulong vo ulong n ulong to void 1063 for i ulong n 1064 put gx2 i fx_f add ko i 1065 put gx3 i fx_f add vo i 1066 end 1067 end 1068 1069 fn op_attn to int 1070 T = get gst 4 1071 S = get gst 5 1072 if or or eq S 0 gt S 64 or eq T 0 gt T 2048 1073 op_die 7 tfx 1074 end 1075 qo = add 8 S 1076 ko = add qo mul S 4096 1077 vo = add ko mul T 512 1078 oo = add vo mul T 512 1079 yo = add oo mul S 4096 1080 fx_need add yo mul S FHID 1081 att_kv ko vo mul T 512 1082 nm_lay get gst 3 1083 nm_tx tsnk 1084 ld_t 32 0 1085 for h ulong 32 1086 put gsk h f64 get gw h 1087 end 1088 for t ulong S 1089 att_row t qo oo 1090 end 1091 nm_lay get gst 3 1092 nm_tx tob 1093 ld_t FHID 4096 1094 for t ulong S 1095 att_y t yo 1096 end 1097 r = report toatt 1098 ret r 1099 end 1100 1101 note rt_tie: true when some om top-6 score s_k (gtk ranks 1..6) is within 2 (E_k + E_7) of 1102 note the rank-7 score, the only case in which a reference whose scores sit within 2 E of the 1103 note om scores can rank the rank-7 expert above one of the om top-6. 1104 fn rt_tie to bool 1105 s7 = get gsg get gtk 6 1106 e7 = get gsge get gtk 6 1107 tie = false 1108 for k ulong 6 1109 c = get gtk k 1110 if lt sub get gsg c s7 mul 2.0f64 add get gsge c e7 1111 tie = true 1112 end 1113 end 1114 ret tie 1115 end 1116 1117 note route: router logits of gv against gw (256 x 2048) into gsc/gse, sigmoid scores into 1118 note gsg/gsge, top-7 by score into gtk (rank 7 is the boundary competitor), renormalized 1119 note top-6 weights into gtw/gtwe. Returns rt_tie. 1120 fn route to bool 1121 g4 = gam 4 1122 for e ulong 256 1123 l = dotr e FHID 1124 el = dote FHID 1125 put gsc e l 1126 put gse e el 1127 sc = om_sig l 1128 put gsg e sc 1129 put gsge e add mul fabs mul sc sub 1.0f64 sc el mul g4 sc 1130 put gch e 0 1131 end 1132 for k ulong 7 1133 b = ulong 256 1134 bv = 0.0f64 1135 for e ulong 256 1136 if eq get gch e 0 1137 if or eq b 256 gt get gsg e bv 1138 b = e 1139 bv = get gsg e 1140 end 1141 end 1142 end 1143 put gtk k b 1144 put gch b 1 1145 end 1146 s = 0.0f64 1147 es = 0.0f64 1148 for k ulong 6 1149 s = add s get gsg get gtk k 1150 es = add es get gsge get gtk k 1151 end 1152 g7 = gam 7 1153 for k ulong 6 1154 e = get gtk k 1155 w = div get gsg e s 1156 put gtw k w 1157 put gtwe k add div add get gsge e mul w es s mul g7 w 1158 end 1159 t = rt_tie 1160 ret t 1161 end 1162 1163 note rt_cmp: the reference top-6 of token t as an index -> weight map; an index missing from 1164 note the om top-6 is accepted only on a tie token and only as the om rank-7 competitor 1165 fn rt_cmp t ulong tie bool io ulong wo ulong to void 1166 for k ulong 6 1167 e = fx_w add io add mul t 6 k 1168 rw = fx_f add wo add mul t 6 k 1169 f = ulong 9 1170 for j ulong 6 1171 if eq get gtk j e 1172 f = j 1173 end 1174 end 1175 if lt f 6 1176 cmp get gtw f rw get gtwe f 1177 else 1178 put gcm 2 add get gcm 2 1.0f64 1179 if not and tie eq e get gtk 6 1180 put gcm 1 add get gcm 1 1.0f64 1181 end 1182 end 1183 end 1184 if tie 1185 put gcm 3 add get gcm 3 1.0f64 1186 end 1187 end 1188 1189 fn op_route to int 1190 L = get gst 3 1191 T = get gst 4 1192 if or eq T 0 gt T 512 1193 op_die 7 tfx 1194 end 1195 lo = add 8 mul T FHID 1196 io = add lo mul T 256 1197 wo = add io mul T 6 1198 fx_need add wo mul T 6 1199 nm_lay L 1200 nm_tx trt 1201 ld_t 256 FHID 1202 for t ulong T 1203 for i ulong FHID 1204 put gv i fx_f add 8 add mul t FHID i 1205 put gve i 0.0f64 1206 end 1207 tie = route 1208 for e ulong 256 1209 cmp get gsc e fx_f add lo add mul t 256 e get gse e 1210 end 1211 rt_cmp t tie io wo 1212 end 1213 r = report tortr 1214 ret r 1215 end 1216 1217 fn ld_mlp L ulong ex ulong j ulong I ulong to void 1218 nm_lay L 1219 if eq ex 998 1220 nm_tx tdense 1221 else 1222 if lt ex 256 1223 nm_tx texp 1224 nm_u ex 1225 nm_c 46 1226 else 1227 nm_tx tshp 1228 end 1229 end 1230 if eq j 0 1231 nm_tx tgp 1232 ld_t I FHID 1233 end 1234 if eq j 1 1235 nm_tx tup 1236 ld_t I FHID 1237 end 1238 if eq j 2 1239 nm_tx tdp 1240 ld_t FHID I 1241 end 1242 end 1243 1244 note mlp_g/mlp_u: gate and up projections of rows t0 .. t1 of gx0/ge0 into gx1 and gx2 1245 fn mlp_g I ulong t0 ulong t1 ulong to void 1246 for t ulong sub t1 t0 1247 b = mul add t0 t FHID 1248 for i ulong FHID 1249 put gv i get gx0 add b i 1250 put gve i get ge0 add b i 1251 end 1252 for r ulong I 1253 o = add mul add t0 t I r 1254 put gx1 o matdot r FHID 1255 put ge1 o dote FHID 1256 end 1257 end 1258 end 1259 1260 fn mlp_u I ulong t0 ulong t1 ulong to void 1261 for t ulong sub t1 t0 1262 b = mul add t0 t FHID 1263 for i ulong FHID 1264 put gv i get gx0 add b i 1265 put gve i get ge0 add b i 1266 end 1267 for r ulong I 1268 o = add mul add t0 t I r 1269 put gx2 o matdot r FHID 1270 put ge2 o dote FHID 1271 end 1272 end 1273 end 1274 1275 note mlp_h: h = silu(g) up into gx1/ge1; E_s = 1.1 E_g + gamma_4 |s| (1.1 bounds max |silu'|), 1276 note E_h = |up| E_s + |s| E_up + gamma_1 |h| 1277 fn mlp_h I ulong t0 ulong t1 ulong to void 1278 for i ulong mul sub t1 t0 I 1279 mlp_h1 add mul t0 I i 1280 end 1281 end 1282 1283 fn mlp_h1 o ulong to void 1284 g = get gx1 o 1285 s = mul g om_sig g 1286 es = mul 1.1f64 get ge1 o 1287 es = add es mul gam 4 fabs s 1288 up = get gx2 o 1289 h = mul s up 1290 put gx1 o h 1291 e = add mul fabs up es mul fabs s get ge2 o 1292 put ge1 o add e mul gam 1 fabs h 1293 end 1294 1295 fn mlp_d I ulong t0 ulong t1 ulong to void 1296 for t ulong sub t1 t0 1297 b = mul add t0 t I 1298 for i ulong I 1299 put gv i get gx1 add b i 1300 put gve i get ge1 add b i 1301 end 1302 for r ulong FHID 1303 o = add mul add t0 t FHID r 1304 put gx3 o matdot r I 1305 put ge3 o dote I 1306 end 1307 end 1308 end 1309 1310 note mlp: FlintMLP of layer L (ex >= 256: shared expert) or routed expert ex, intermediate 1311 note size I, rows t0 .. t1 of gx0/ge0 into gx3/ge3: down(silu(gate x) up x) 1312 fn mlp L ulong ex ulong I ulong t0 ulong t1 ulong to void 1313 ld_mlp L ex 0 I 1314 mlp_g I t0 t1 1315 ld_mlp L ex 1 I 1316 mlp_u I t0 t1 1317 mlp_h I t0 t1 1318 ld_mlp L ex 2 I 1319 mlp_d I t0 t1 1320 end 1321 1322 fn cmp_x3 n ulong fo ulong to void 1323 for i ulong n 1324 cmp get gx3 i fx_f add fo i get ge3 i 1325 end 1326 end 1327 1328 fn op_mlp ex ulong I ulong nm view uchar * to int 1329 L = get gst 3 1330 T = get gst 4 1331 if or eq T 0 gt T 8 1332 op_die 7 tfx 1333 end 1334 yo = add 8 mul T FHID 1335 fx_need add yo mul T FHID 1336 fx_x0 8 T 1337 mlp L ex I 0 T 1338 cmp_x3 mul T FHID yo 1339 r = report nm 1340 ret r 1341 end 1342 1343 note moe_acc: add w_k y_k of token t into gacm with bound gace and sum |w y| in gaca 1344 fn moe_acc t ulong w f64 ew f64 to void 1345 for i ulong FHID 1346 o = add mul t FHID i 1347 y = get gx3 o 1348 put gacm o add get gacm o mul w y 1349 e = add get gace o mul fabs w get ge3 o 1350 put gace o add e mul fabs y ew 1351 put gaca o add get gaca o fabs mul w y 1352 end 1353 end 1354 1355 note op_moe: FlintSparseMoeBlock = sum_k w_k expert_k(x) + shared_expert(x); bound 1356 note sum_k (|w_k| E_yk + |y_k| E_wk) + E_S + gamma_8 (sum_k |w_k y_k| + |S|) 1357 fn moe_rt t ulong to void 1358 for i ulong FHID 1359 put gv i get gx0 add mul t FHID i 1360 put gve i 0.0f64 1361 end 1362 tie = route 1363 put gtie t 0 1364 if tie 1365 put gtie t 1 1366 put gcm 3 add get gcm 3 1.0f64 1367 end 1368 for k ulong 6 1369 j = add mul t 6 k 1370 put gmk j get gtk k 1371 put gmw j get gtw k 1372 put gmwe j get gtwe k 1373 end 1374 end 1375 1376 fn moe_ex L ulong t ulong to void 1377 for k ulong 6 1378 j = add mul t 6 k 1379 mlp L get gmk j 512 t add t 1 1380 moe_acc t get gmw j get gmwe j 1381 end 1382 end 1383 1384 fn moe_cmp t ulong yo ulong to void 1385 g8 = gam 8 1386 for i ulong FHID 1387 o = add mul t FHID i 1388 v = add get gacm o get gx3 o 1389 e = add get gace o get ge3 o 1390 a = add get gaca o fabs get gx3 o 1391 cmp v fx_f add yo o add e mul g8 a 1392 end 1393 end 1394 1395 fn op_moe to int 1396 L = get gst 3 1397 T = get gst 4 1398 if or eq T 0 gt T 8 1399 op_die 7 tfx 1400 end 1401 yo = add 8 mul T FHID 1402 fx_need add yo mul T FHID 1403 fx_x0 8 T 1404 nm_lay L 1405 nm_tx trt 1406 ld_t 256 FHID 1407 for t ulong T 1408 moe_rt t 1409 end 1410 for i ulong mul T FHID 1411 put gacm i 0.0f64 1412 put gace i 0.0f64 1413 put gaca i 0.0f64 1414 end 1415 for t ulong T 1416 if eq get gtie t 0 1417 moe_ex L t 1418 end 1419 end 1420 mlp L 999 1024 0 T 1421 for t ulong T 1422 if eq get gtie t 0 1423 moe_cmp t yo 1424 end 1425 end 1426 r = report tomoe 1427 ret r 1428 end 1429 1430 note selftest: om_exp, om_sig and om_sc against Wolfram Language values (20 digits) 1431 fn st_one om f64 ref f64 rel f64 to void 1432 cmp om ref mul rel fabs ref 1433 end 1434 1435 fn op_self to int 1436 put gst 3 0 1437 st_one om_exp 1.0f64 2.`718281828459045235f64` 2.0e-15f64 1438 st_one om_exp -20.25f64 1.`605228055185611609e`-9f64 2.0e-15f64 1439 st_one om_exp 8.125f64 3377.`867931673534884f64` 2.0e-15f64 1440 st_one om_exp -87.5f64 9.`98235093056924756e`-39f64 2.0e-15f64 1441 st_one om_sc 1.0f64 false 0.`8414709848078965067f64` 2.0e-15f64 1442 st_one om_sc 1.0f64 true 0.`5403023058681397174f64` 2.0e-15f64 1443 st_one om_sc 518.75f64 false -0.`3776084404242524912f64` 2.0e-15f64 1444 st_one om_sc 518.75f64 true -0.`925965369611825039f64` 2.0e-15f64 1445 st_one om_sc 0.001f64 false 9.`99999833333341667e`-4f64 2.0e-15f64 1446 st_one om_sc 3.0f64 true -0.`989992496600445457f64` 2.0e-15f64 1447 st_one om_sig 1.5f64 0.`8175744761936436596f64` 2.0e-15f64 1448 rope_cs 519 1 1449 st_one get gacc 3 -0.`982429600544478341f64` 1.0e-12f64 1450 st_one get gacc 4 -0.`1866335446108676431f64` 1.0e-12f64 1451 st_one get gacc 5 2.`683614206850094353e`-4f64 1.0e-12f64 1452 r = report tostt 1453 ret r 1454 end 1455 1456 note Complete bounded host forward, two prompt tokens maximum. No teacher forcing: 1457 note only token IDs are inputs; the fixture's intermediate tensors are comparison targets. 1458 note This f64-compute/BF16-storage numerical profile is NOT bf16-activation qualification. 1459 note Integration acceptance is |om-ref| <= 8 gamma_2048 (1+|ref|), fixed before execution. 1460 note The existing per-operator bounds remain the tighter local checks. 1461 fn fw_cmp n ulong to void 1462 o = get gfs 1 1463 for i ulong n 1464 r = fx_f add o i 1465 e = mul mul 4.0f64 gam 2048 add 1.0f64 fabs r 1466 cmp get gfa i r e 1467 end 1468 put gfs 1 add o n 1469 end 1470 1471 fn fw_hidden to void 1472 for i ulong mul get gfs 0 FHID 1473 put gfa i get gfh i 1474 end 1475 fw_cmp mul get gfs 0 FHID 1476 end 1477 1478 fn fw_norm L ulong post bool to void 1479 nm_lay L 1480 if post 1481 nm_tx tln1 1482 else 1483 nm_tx tln0 1484 end 1485 ld_t FHID 0 1486 for t ulong get gfs 0 1487 for i ulong FHID 1488 put gv i get gfh add mul t FHID i 1489 put gve i 0.0f64 1490 end 1491 rmsv FHID 0 1492 for i ulong FHID 1493 o = add mul t FHID i 1494 put gx0 o get gy i 1495 put ge0 o 0.0f64 1496 put gfa o get gy i 1497 end 1498 end 1499 fw_cmp mul get gfs 0 FHID 1500 end 1501 1502 fn fw_proj rows ulong to void 1503 for t ulong get gfs 0 1504 for i ulong FHID 1505 put gv i get gx0 add mul t FHID i 1506 put gve i 0.0f64 1507 end 1508 for r ulong rows 1509 put gx1 add mul t rows r matdot r FHID 1510 end 1511 end 1512 end 1513 1514 fn fw_head t ulong h ulong nh ulong rp bool to void 1515 b = mul add mul t nh h 128 1516 for d ulong 128 1517 put gv d get gx1 add b d 1518 put gve d 0.0f64 1519 end 1520 rmsv 128 0 1521 if rp 1522 ropev t 1523 end 1524 for d ulong 128 1525 if eq nh 32 1526 put gfq add b d get gy d 1527 else 1528 put gx2 add b d get gy d 1529 end 1530 end 1531 end 1532 1533 fn fw_qk L ulong q bool to void 1534 rows = ulong 512 1535 nh = ulong 4 1536 nm_lay L 1537 if q 1538 nm_tx tq 1539 rows = ulong 4096 1540 nh = ulong 32 1541 else 1542 nm_tx tk 1543 end 1544 ld_t rows FHID 1545 fw_proj rows 1546 nm_lay L 1547 if q 1548 nm_tx tqn 1549 else 1550 nm_tx tkn 1551 end 1552 ld_t 128 0 1553 for t ulong get gfs 0 1554 for h ulong nh 1555 fw_head t h nh fl_swa L 1556 end 1557 end 1558 end 1559 1560 fn fw_atthead t ulong h ulong to void 1561 b = mul add mul t 32 h 128 1562 for d ulong 128 1563 put gv d get gfq add b d 1564 end 1565 g = div h 8 1566 n = att_sc g t 0 1567 att_sm n get gsk h get gfs 0 1568 put gacc 11 0.0f64 1569 for d ulong 128 1570 v = pv_d n add mul g 128 d 1571 put gfo add b d v 1572 end 1573 end 1574 1575 fn fw_att L ulong to void 1576 fw_qk L true 1577 fw_qk L false 1578 nm_lay L 1579 nm_tx tv 1580 ld_t 512 FHID 1581 fw_proj 512 1582 for i ulong mul get gfs 0 512 1583 put gx3 i get gx1 i 1584 end 1585 nm_lay L 1586 nm_tx tsnk 1587 ld_t 32 0 1588 for h ulong 32 1589 put gsk h f64 get gw h 1590 end 1591 for t ulong get gfs 0 1592 for h ulong 32 1593 fw_atthead t h 1594 end 1595 end 1596 nm_lay L 1597 nm_tx tob 1598 ld_t FHID 4096 1599 for t ulong get gfs 0 1600 for i ulong 4096 1601 put gv i get gfo add mul t 4096 i 1602 put gve i 0.0f64 1603 end 1604 for r ulong FHID 1605 put gfa add mul t FHID r matdot r 4096 1606 end 1607 end 1608 fw_cmp mul get gfs 0 FHID 1609 for i ulong mul get gfs 0 FHID 1610 put gfh i add get gfh i get gfa i 1611 end 1612 end 1613 1614 fn fw_moe L ulong to void 1615 T = get gfs 0 1616 nm_lay L 1617 nm_tx trt 1618 ld_t 256 FHID 1619 for t ulong T 1620 moe_rt t 1621 end 1622 for i ulong mul T FHID 1623 put gacm i 0.0f64 1624 put gace i 0.0f64 1625 put gaca i 0.0f64 1626 end 1627 for t ulong T 1628 moe_ex L t 1629 end 1630 mlp L 999 1024 0 T 1631 for i ulong mul T FHID 1632 put gx3 i add get gacm i get gx3 i 1633 end 1634 end 1635 1636 fn fw_layer L ulong to void 1637 fw_norm L false 1638 fw_att L 1639 fw_norm L true 1640 if lt L 2 1641 mlp L 998 6144 0 get gfs 0 1642 else 1643 fw_moe L 1644 end 1645 for i ulong mul get gfs 0 FHID 1646 put gfa i get gx3 i 1647 end 1648 fw_cmp mul get gfs 0 FHID 1649 for i ulong mul get gfs 0 FHID 1650 put gfh i add get gfh i get gfa i 1651 end 1652 fw_hidden 1653 put gst 3 L 1654 r = report tofwd 1655 if ne r 0 1656 exit 1 1657 end 1658 end 1659 1660 fn fw_embed to void 1661 for t ulong get gfs 0 1662 id = fx_w add 8 t 1663 if ge id 150144 1664 op_die 7 tfx 1665 end 1666 put gst 7 0 1667 nm_tx tembd 1668 ld_rows 150144 FHID id 1 1669 for i ulong FHID 1670 put gfh add mul t FHID i f64 get gw i 1671 end 1672 end 1673 fw_hidden 1674 end 1675 1676 fn fw_final to void 1677 put gst 7 0 1678 nm_tx tfnorm 1679 ld_t FHID 0 1680 for t ulong get gfs 0 1681 for i ulong FHID 1682 put gv i get gfh add mul t FHID i 1683 put gve i 0.0f64 1684 end 1685 rmsv FHID 0 1686 for i ulong FHID 1687 o = add mul t FHID i 1688 put gfh o get gy i 1689 put gfa o get gy i 1690 end 1691 end 1692 fw_cmp mul get gfs 0 FHID 1693 end 1694 1695 fn fw_logits to void 1696 for t ulong get gfs 0 1697 for r ulong 150144 1698 if eq rem r 2048 0 1699 put gst 7 0 1700 nm_tx tlmhead 1701 n = sub ulong 150144 r 1702 if gt n 2048 1703 n = ulong 2048 1704 end 1705 ld_rows 150144 FHID r n 1706 for i ulong FHID 1707 put gv i get gfh add mul t FHID i 1708 put gve i 0.0f64 1709 end 1710 end 1711 v = matdot rem r 2048 FHID 1712 put gfa 0 v 1713 fw_cmp 1 1714 end 1715 end 1716 end 1717 1718 fn op_forward to int 1719 T = get gst 4 1720 if or eq T 0 gt T 2 1721 op_die 7 tfx 1722 end 1723 note words = header + IDs + embeddings + five tensors/layer + final norm + logits 1724 fx_need add add add 8 T mul mul T FHID 247 mul T 150144 1725 put gfs 0 T 1726 put gfs 1 add 8 T 1727 fw_embed 1728 for L ulong 49 1729 fw_layer L 1730 end 1731 fw_final 1732 fw_logits 1733 put gst 3 49 1734 r = report tofwd 1735 ret r 1736 end 1737 1738 fn op_code to ulong 1739 if op_argeq 1 tormsn 1740 ret 1 1741 end 1742 if op_argeq 1 toqkv 1743 ret 2 1744 end 1745 if op_argeq 1 toatt 1746 ret 3 1747 end 1748 if op_argeq 1 tortr 1749 ret 4 1750 end 1751 if op_argeq 1 toshr 1752 ret 5 1753 end 1754 if op_argeq 1 toexp 1755 ret 6 1756 end 1757 if op_argeq 1 tomoe 1758 ret 7 1759 end 1760 if op_argeq 1 tofwd 1761 ret 8 1762 end 1763 op_die 2 tu 1764 ret 0 1765 end 1766 1767 fn main to int 1768 c = argcnt 1769 if and ne c 5 ne c 2 1770 op_die 2 tu 1771 end 1772 o = ulong 0 1773 n = ulong c 1774 for i ulong n 1775 r = argread i ga o sub len ga o 1776 if lt r 0 1777 op_die 2 tu 1778 end 1779 put gas i o 1780 put gal i ulong r 1781 o = add o ulong r 1782 end 1783 cm_reset 1784 if eq c 2 1785 if not op_argeq 1 tostt 1786 op_die 2 tu 1787 end 1788 s = op_self 1789 ret s 1790 end 1791 op = op_code 1792 rd_fx 1793 rd_tsv 1794 if or ne fx_w 0 `1347374150` ne fx_w 1 op 1795 op_die 7 tfx 1796 end 1797 put gst 2 op 1798 put gst 3 fx_w 2 1799 put gst 4 fx_w 3 1800 put gst 5 fx_w 4 1801 put gst 6 fx_w 5 1802 if eq op 8 1803 s = op_forward 1804 ret s 1805 end 1806 if eq op 1 1807 s = op_rmsn 1808 ret s 1809 end 1810 if eq op 2 1811 s = op_qkv 1812 ret s 1813 end 1814 if eq op 3 1815 s = op_attn 1816 ret s 1817 end 1818 if eq op 4 1819 s = op_route 1820 ret s 1821 end 1822 if eq op 5 1823 s = op_mlp 999 1024 toshr 1824 ret s 1825 end 1826 if eq op 6 1827 e = get gst 6 1828 if ge e 256 1829 op_die 7 tfx 1830 end 1831 s = op_mlp e 512 toexp 1832 ret s 1833 end 1834 s = op_moe 1835 ret s 1836 end