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/omega-mv/blob/main/om/gpu/q36/native_run.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / gpu / q36 / native_run.om` 1396 lines ยท 27624 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note Pure seeded-Omega zero-CUDA native GB10 runner for Qwen 3.6 35B-A3B FP8 + Arboris. 3 note ZERO libcuda.so.1, ZERO libc, ZERO Python, ZERO C (static AArch64 ELF). 4 note Compose after: 5 note gpu/nvidia/native/nvrm/context.om 6 note gpu/nvidia/native/blackwell/vector.om 7 note gpu/nvidia/native/blackwell/launch.om 8 note gpu/nvidia/native/blackwell/batch.om 9 note om/encoder/bw_enc_sm121.om 10 note om/gpu/q36/emit_fast.om 11 note om/gpu/q36/arboris/prefetch.om 12 13 global qCodePos ulong 1 14 global qEntry ulong 16 15 global qSmem ulong 16 16 global qCodeMap long 1 17 global qCodeFail uint 1 18 global qBind uint 32 19 global qBindN ulong 1 20 global qDirty uint 1 21 global qFastMode ulong 1 22 global qFastSlot ulong 1 23 global qFastPos ulong 1 24 25 note Device allocations (all via native NVRM/UVM ioctl nvalloc) 26 global qLut long 1 27 global qWork long 1 28 global qKvK long 1 29 global qKvV long 1 30 global qGdnState long 1 31 global qLmBf16 long 1 32 global qLmFp8 long 1 33 global qLmFp8S long 1 34 global qLmW4 long 1 35 global qLmW4S long 1 36 global qCandIds long 1 37 global qCandLog long 1 38 global qLogits long 1 39 40 note 4 cold weight banks (> 110x GB10 24 MiB L2) 41 global qWGdnQkv long 4 42 global qWGdnQkvS long 4 43 global qWGdnZ long 4 44 global qWGdnZS long 4 45 global qWGdnAb long 4 46 global qWOut long 4 47 global qWOutS long 4 48 global qWAttnQkvg long 4 49 global qWAttnQkvgS long 4 50 global qWRouter long 4 51 global qWExpGu long 4 52 global qWExpGuS long 4 53 global qWExpDn long 4 54 global qWExpDnS long 4 55 global qW4ExpGu long 4 56 global qW4ExpGuS long 4 57 global qW4ExpDn long 4 58 global qW4ExpDnS long 4 59 global qWShGu long 4 60 global qWShGuS long 4 61 global qWShDn long 4 62 global qWShDnS long 4 63 64 note Pre-recorded native GPFIFO command packets: 0=40L backbone, 1=40L+FP8 head, 2=40L+BF16 head 65 global qPkt long 4 66 global qPktBytes ulong 4 67 global qFillBuf uchar 65536 68 69 text qHdr "=== QWEN 3.6 35B-A3B FP8 + ARBORIS 100% PURE SEEDED-OMEGA NATIVE GB10 RUNNER (ZERO libcuda.so.1) ===\n" 70 text qOpenErr "FAIL: native /dev/nvidia* open/alloc error (stage): " 71 text qSubErr "FAIL: native GPFIFO submit error (stage): " 72 text qArbOk "PASS: Arboris online adaptive sum-over-paths prefetcher (40L x 256E x top-8) self-test\n" 73 text qArbFail "FAIL: Arboris self-test failed\n" 74 text qP1Ok "PASS: native GB10 9-kernel numerical parity self-test (bf16_gemv, fp8_b128_gemv, fp8_b128_grouped8, router_top8_256, swiglu8_q36, moe_combine_rms, gdn_step_bf16, attn_hd256_split+reduce_gate)\n" 75 text qP1Fail "FAIL: native GB10 kernel parity check failed at test id: " 76 text qRes0 "NATIVE_GPFIFO_40L_FP8_W4HEAD_2.86GB: median_us=" 77 text qRes1 "NATIVE_GPFIFO_40L_W4MOE_W4HEAD_2.13GB: median_us=" 78 text qRes2 "NATIVE_GPFIFO_40L_FULLW4_BACKBONE_0.79GB: median_us=" 79 text qRes3 "NATIVE_GPFIFO_40L_FULLW4_W4HEAD_1.07GB: median_us=" 80 text qArbRes "ARBORIS_40L_ONLINE_OVERHEAD: us_per_tok=" 81 text qArbHit " hit_rate_pct=" 82 text qTokS " tok_s_x100=" 83 text qFinOk " finite=OK\n" 84 text qFinBad "FAIL: non-finite output detected!\n" 85 text qNl "\n" 86 text qDbgK " kernel_test #" 87 text qDbgOk " OK\n" 88 text qDbgTo " TIMEOUT\n" 89 90 fn qPrint s view uchar * to void 91 r = write 1 s 0 len s 92 end 93 94 fn qPrintU x ulong to void 95 b = new uchar 24 0 96 pos = ulong 24 97 v = x 98 while true 99 pos = sub pos 1 100 put b pos uchar add 48 rem v 10 101 v = div v 10 102 if eq v 0 103 break 104 end 105 end 106 r = write 1 b pos sub 24 pos 107 end 108 109 fn qW off ulong to ulong 110 ret add mapaddr get qWork 0 off 111 end 112 113 fn q36Write w view ulong 4 to void 114 p = get qCodePos 0 115 b = new uchar 16 0 116 if gt get qCodeMap 0 0 117 for i ulong 16 118 put b i uchar and shr get w div i 4 mul rem i 4 8 255 119 end 120 if ne mapwrite get qCodeMap 0 p b 0 16 16 121 put qCodeFail 0 1 122 end 123 end 124 put qCodePos 0 add p 16 125 end 126 127 fn qEmitAll to void 128 put qCodePos 0 0 129 for i ulong 13 130 p = mul div add get qCodePos 0 255 256 256 131 put qEntry i p 132 put qCodePos 0 p 133 put q36Pc 0 0 134 put q36Guard 0 0 135 ok = q36Kernel uchar add 97 i 136 if not ok 137 exit 4 138 end 139 q36Done 140 end 141 put qSmem 0 16384 142 put qSmem 1 17472 143 put qSmem 2 17472 144 put qSmem 3 2048 145 put qSmem 4 0 146 put qSmem 5 64 147 put qSmem 6 1024 148 put qSmem 7 0 149 put qSmem 8 0 150 put qSmem 9 8192 151 put qSmem 10 0 152 put qSmem 11 8192 153 put qSmem 12 16384 154 end 155 156 fn qArg u uint to void 157 i = get qBindN 0 158 put qBind i u 159 put qBindN 0 add i 1 160 end 161 162 fn qPtr va ulong to void 163 qArg gblo va 164 qArg gbhi va 165 end 166 167 fn qFastEmit words ulong to bool 168 cb = mapaddr get gbmem 1 169 gbdma add cb 896 uint words 170 for j ulong words 171 gbemit get qBind j 172 end 173 q = mapaddr get gbmem 2 174 gbinline q false 175 gbdma add q 8192 1 176 gbemit 1 177 gbinline add q 4096 true 178 gbrel add mapaddr get gbmem 3 32 6 179 put qBindN 0 0 180 n = get gbused 0 181 pos = get qFastPos 0 182 if gt add pos n `2096000` 183 ret false 184 end 185 if ne mapwrite get qPkt get qFastSlot 0 pos gbcmd 0 n long n 186 ret false 187 end 188 put qFastPos 0 add pos n 189 ret true 190 end 191 192 fn qRecord id ulong gx uint gy uint gz uint bx uint to bool 193 gbqmd 1 194 a = add mapaddr get qCodeMap 0 get qEntry id 195 put gbq1 32 gblo shr a 4 196 put gbq1 33 or uint and shr a 36 `2097151` `20971520` 197 put gbq1 59 gblo shr a 8 198 put gbq1 34 or uint 65536 bx 199 put gbq1 35 or uint 131073 shl uint 64 8 200 sm = get qSmem id 201 s7 = ulong 8 202 if ne sm 0 203 s7 = div add sm 127 128 204 end 205 put gbq1 36 uint or `78923776` s7 206 put gbq1 37 0 207 put gbq1 39 gx 208 put gbq1 40 gy 209 put gbq1 41 gz 210 put gbcb 216 bx 211 put gbcb 220 gx 212 put gbcb 221 gy 213 put gbcb 222 gz 214 cb = mapaddr get gbmem 1 215 words = get qBindN 0 216 endva = add cb add 896 mul words 4 217 put gbcb 212 gbhi endva 218 put gbcb 213 gblo endva 219 put gbused 0 0 220 if eq get qFastMode 0 1 221 ret qFastEmit words 222 end 223 gbsetup 224 if ne get qDirty 0 0 225 gbmth 0 40 4 226 gbemit 0 227 gbemit 0 228 gbemit 0 229 gbemit `2281701376` 230 put qDirty 0 0 231 end 232 gbdma cb 224 233 for j ulong 224 234 gbemit get gbcb j 235 end 236 gbdma add cb 896 uint words 237 for j ulong words 238 gbemit get qBind j 239 end 240 q = mapaddr get gbmem 2 241 gbinline q false 242 gbdma add q 8192 1 243 gbemit 1 244 gbinline add q 4096 true 245 gbrel add mapaddr get gbmem 3 32 6 246 put qBindN 0 0 247 ret gbappend id get gbst 1 248 end 249 250 fn qWriteLut m long to bool 251 for code ulong 256 252 exp = and shr code 3 15 253 frac = and code 7 254 bits = uint 0 255 if and eq exp 15 eq frac 7 256 bits = uint 0x7fc00000 257 else 258 v = f32 0.0 259 if eq exp 0 260 v = div f32 frac f32 512.0 261 else 262 p = f32 1.0 263 if ge exp 7 264 for s ulong sub exp 7 bound 16 265 p = mul p f32 2.0 266 end 267 else 268 for s ulong sub 7 exp bound 16 269 p = mul p f32 0.5 270 end 271 end 272 v = mul add f32 1.0 div f32 frac f32 8.0 p 273 end 274 if ne and code 128 0 275 v = sub f32 0.0 v 276 end 277 bits = bitcast uint v 278 end 279 if ne mapset m mul code 4 4 ulong bits 0 280 ret false 281 end 282 end 283 ret true 284 end 285 286 fn qFillPattern m long bytes ulong pat uint to bool 287 for i ulong 16384 288 for j ulong 4 289 put qFillBuf add mul i 4 j uchar and shr pat mul j 8 255 290 end 291 end 292 off = ulong 0 293 while lt off bytes bound `4000000000` 294 n = sub bytes off 295 if gt n 65536 296 n = ulong 65536 297 end 298 if ne mapwrite m off qFillBuf 0 n long n 299 ret false 300 end 301 off = add off n 302 end 303 ret true 304 end 305 306 fn qCheckF32 m long off ulong want f32 tol f32 to bool 307 got = bitcast f32 uint mapget m off 4 308 diff = sub got want 309 if lt diff 0.0f32 310 diff = sub f32 0.0 diff 311 end 312 ret le diff tol 313 end 314 315 fn qIsFiniteU32 bits ulong to bool 316 exp = and shr bits 23 255 317 ret lt exp 255 318 end 319 320 fn qRunOne id ulong gx uint gy uint gz uint bx uint tid ulong to bool 321 put qDirty 0 1 322 if not gbbegin 1 323 ret false 324 end 325 if not qRecord id gx gy gz bx 326 ret false 327 end 328 if not gbbsubmit `1000000000` 329 qPrint qDbgK 330 qPrintU tid 331 qPrint qDbgTo 332 ret false 333 end 334 qPrint qDbgK 335 qPrintU tid 336 qPrint qDbgOk 337 ret true 338 end 339 340 note ========================================================================= 341 note Phase 1: Live GB10 Numerical Parity Self-Test (all 9 SM121 kernels) 342 note ========================================================================= 343 fn qSelfTest to bool 344 wk = get qWork 0 345 wva = mapaddr wk 346 lutva = mapaddr get qLut 0 347 note 0x000000: W_bf16[32,2048] = 0.5 (0x3f003f00) 348 if not qFillPattern wk 0x040000 0x3f003f00 349 ret false 350 end 351 note 0x040000: x[2048] = 1.0 (0x3f800000) 352 for i ulong 2048 353 r = mapset wk add 0x040000 mul i 4 4 0x3f800000 354 end 355 note 0x060000: W_fp8[256,2048] = 1.0 (0x38 = E4M3FN 1.0 -> 0x38383838) 356 for i ulong 131072 357 r = mapset wk add 0x060000 mul i 4 4 0x38383838 358 end 359 note 0x0e0000: S_f32[2,16] = 0.5 (0x3f000000) 360 for i ulong 256 361 r = mapset wk add 0x0e0000 mul i 4 4 0x3f000000 362 end 363 note 0x0f0000: ids[8] = 0..7 364 for i ulong 8 365 r = mapset wk add 0x0f0000 mul i 4 4 i 366 end 367 note 0x100000: W_pool[8, 128, 2048] = 1.0 (0x38383838) = 2 MiB -> 0x100000..0x2fffff 368 for i ulong 524288 369 r = mapset wk add 0x100000 mul i 4 4 0x38383838 370 end 371 note 0x300000: S_pool[8, 1, 16] = 0.25 (0x3e800000) 372 for i ulong 128 373 r = mapset wk add 0x300000 mul i 4 4 0x3e800000 374 end 375 note 0x320000: router logits[256] = 0.01 * i (so top-8 are 255,254,...,248) 376 for i ulong 256 377 v = mul f32 i 0.01f32 378 r = mapset wk add 0x320000 mul i 4 4 ulong bitcast uint v 379 end 380 note 0x330000: swiglu gu[8, 1024] = 1.0, wts[8] = 1.0 381 for i ulong 8192 382 r = mapset wk add 0x330000 mul i 4 4 0x3f800000 383 end 384 for i ulong 8 385 r = mapset wk add 0x340000 mul i 4 4 0x3f800000 386 end 387 note 0x360000: moe_combine downs[8,2048]=0.125, shared[2048]=1.0, res[2048]=1.0, gain[2048]=2.0 -> res_out=3.0, norm_out=2.0 388 for i ulong 16384 389 r = mapset wk add 0x360000 mul i 4 4 0x3e000000 390 end 391 for i ulong 2048 392 r = mapset wk add 0x370000 mul i 4 4 0x3f800000 393 r = mapset wk add 0x372000 mul i 4 4 0x3f800000 394 r = mapset wk add 0x374000 mul i 4 4 0x40000000 395 end 396 note 0x380000: GDN qkv[8192]=1.0 (q=1,k=1,v=1), z[4096]=0.0, ab[64]=0.0 (decay=0.5,beta=0.5), state_bf16=0 397 for i ulong 8192 398 r = mapset wk add 0x380000 mul i 4 4 0x3f800000 399 end 400 for i ulong 4096 401 r = mapset wk add 0x388000 mul i 4 4 0x3f800000 402 end 403 for i ulong 64 404 r = mapset wk add 0x38c000 mul i 4 4 0 405 end 406 for i ulong 262144 407 r = mapset wk add 0x400000 mul i 4 4 0 408 end 409 note 0x500000: Attn Q[16,256]=1.0, K_bf16[2,16,256]=1.0 (0x3f803f80), V_bf16[2,16,256]=2.0 (0x40004000), gate[16,256]=0.0 -> out=1.0 410 for i ulong 4096 411 r = mapset wk add 0x500000 mul i 4 4 0x3f800000 412 r = mapset wk add 0x504000 mul i 4 4 0 413 end 414 for i ulong 4096 415 r = mapset wk add 0x510000 mul i 4 4 0x3f803f80 416 r = mapset wk add 0x520000 mul i 4 4 0x40004000 417 end 418 419 note Run each kernel individually with 1s timeout for exact fault/hang pinpointing 420 note 1. bf16_gemv_fast (32x2048, W=0.5, x=1.0 -> y=1024.0) 421 qPtr wva 422 qPtr add wva 0x040000 423 qPtr add wva 0x050000 424 qArg 2048 425 qArg 32 426 if not qRunOne 0 1 1 1 256 1 427 ret false 428 end 429 note 2. fp8_b128_gemv_fast (256x2048, W=1.0, S=0.5, x=1.0 -> C=1024.0) 430 qPtr add wva 0x040000 431 qPtr add wva 0x060000 432 qPtr add wva 0x0e0000 433 qPtr lutva 434 qPtr add wva 0x054000 435 qArg 2048 436 qArg 256 437 if not qRunOne 1 8 1 1 256 2 438 ret false 439 end 440 note 3. fp8_b128_grouped8_fast (8x128x2048, W=1.0, S=0.25, x=1.0 -> C=512.0) 441 qPtr add wva 0x040000 442 qPtr add wva 0x100000 443 qPtr add wva 0x300000 444 qPtr add wva 0x0f0000 445 qPtr lutva 446 qPtr add wva 0x310000 447 qArg 2048 448 qArg 128 449 qArg 0 450 if not qRunOne 2 4 8 1 256 3 451 ret false 452 end 453 note 4. router_top8_256_fast (logits=0.01*i -> ids[0]=255) 454 qPtr add wva 0x320000 455 qPtr add wva 0x321000 456 qPtr add wva 0x322000 457 if not qRunOne 3 1 1 1 256 4 458 ret false 459 end 460 note 5. swiglu8_q36_fast (gate=1, up=1, w=1 -> silu(1)=0.731059) 461 qPtr add wva 0x330000 462 qPtr add wva 0x340000 463 qPtr add wva 0x350000 464 qArg 512 465 if not qRunOne 4 4 8 1 128 5 466 ret false 467 end 468 note 6. moe_combine_rms_fast (downs=0.125*8=1, sh=1, res=1 -> res_out=3.0, gain=2 -> norm_out=2.0) 469 qPtr add wva 0x360000 470 qPtr add wva 0x370000 471 qPtr add wva 0x372000 472 qPtr add wva 0x374000 473 qPtr add wva 0x376000 474 qPtr add wva 0x378000 475 if not qRunOne 5 1 1 1 256 6 476 ret false 477 end 478 note 7. gdn_step_bf16_fast (S=0, decay=0.5, beta=0.5, k=1, v=1 -> S_new=0.5, q=1 -> S^Tq=64.0, z=1 -> silu(1)=0.731059 -> out=46.7877) 479 qPtr add wva 0x380000 480 qPtr add wva 0x388000 481 qPtr add wva 0x38c000 482 qPtr add wva 0x400000 483 qPtr add wva 0x390000 484 if not qRunOne 6 32 1 1 128 7 485 ret false 486 end 487 note 8 & 9. attn_hd256_split_fast + attn_hd256_reduce_gate_fast (V=2.0, gate=0 -> sigmoid(0)=0.5 -> out=1.0) 488 qPtr add wva 0x500000 489 qPtr add wva 0x510000 490 qPtr add wva 0x520000 491 qPtr add wva 0x530000 492 qPtr add wva 0x550000 493 qArg 16 494 qArg 8 495 if not qRunOne 7 16 8 1 32 8 496 ret false 497 end 498 qPtr add wva 0x530000 499 qPtr add wva 0x550000 500 qPtr add wva 0x504000 501 qPtr add wva 0x560000 502 qArg 8 503 if not qRunOne 8 16 1 1 256 9 504 ret false 505 end 506 507 note 10. w4_b32_grouped8_fast (8x128x512, W=0x99999999 -> q-8=1.0, S=0x3f003f00 -> 0.5, x=1.0 -> C=256.0) 508 for i ulong 65536 509 r = mapset wk add 0x600000 mul i 4 4 0x99999999 510 end 511 for i ulong 8192 512 r = mapset wk add 0x680000 mul i 4 4 0x3f003f00 513 end 514 qPtr add wva 0x040000 515 qPtr add wva 0x600000 516 qPtr add wva 0x680000 517 qPtr add wva 0x0f0000 518 qPtr add wva 0x690000 519 qArg 512 520 qArg 128 521 qArg 0 522 if not qRunOne 11 4 8 1 256 10 523 ret false 524 end 525 526 note 11. w4_b32_gemv_fast (128x4096, W=0x99999999 -> q-8=1.0, S=0x3e803e80 -> 0.25, x=1.0 -> C=1024.0) 527 for i ulong 4096 528 r = mapset wk add 0x700000 mul i 4 4 0x3f800000 529 end 530 for i ulong 65536 531 r = mapset wk add 0x710000 mul i 4 4 0x99999999 532 end 533 for i ulong 8192 534 r = mapset wk add 0x760000 mul i 4 4 0x3e803e80 535 end 536 qPtr add wva 0x700000 537 qPtr add wva 0x710000 538 qPtr add wva 0x760000 539 qPtr add wva 0x770000 540 qArg 4096 541 qArg 128 542 if not qRunOne 12 4 1 1 256 11 543 ret false 544 end 545 546 if not qCheckF32 wk 0x050000 1024.0f32 0.5f32 547 qPrint qP1Fail 548 qPrintU 1 549 qPrint qNl 550 ret false 551 end 552 if not qCheckF32 wk 0x054000 1024.0f32 0.5f32 553 qPrint qP1Fail 554 qPrintU 2 555 qPrint qNl 556 ret false 557 end 558 if not qCheckF32 wk 0x310000 512.0f32 0.5f32 559 qPrint qP1Fail 560 qPrintU 3 561 qPrint qNl 562 ret false 563 end 564 if ne mapget wk 0x321000 4 255 565 qPrint qP1Fail 566 qPrintU 4 567 qPrint qNl 568 ret false 569 end 570 if not qCheckF32 wk 0x350000 0.`731059f32` 0.02f32 571 qPrint qP1Fail 572 qPrintU 5 573 qPrint qNl 574 ret false 575 end 576 if not qCheckF32 wk 0x376000 3.0f32 0.02f32 577 qPrint qP1Fail 578 qPrintU 6 579 qPrint qNl 580 ret false 581 end 582 if not qCheckF32 wk 0x378000 2.0f32 0.02f32 583 qPrint qP1Fail 584 qPrintU 7 585 qPrint qNl 586 ret false 587 end 588 if not qCheckF32 wk 0x390000 46.`7877f32` 0.5f32 589 qPrint qP1Fail 590 qPrintU 8 591 qPrint qNl 592 ret false 593 end 594 if not qCheckF32 wk 0x560000 1.0f32 0.02f32 595 qPrint qP1Fail 596 qPrintU 9 597 qPrint qNl 598 ret false 599 end 600 if not qCheckF32 wk 0x690000 256.0f32 0.5f32 601 qPrint qP1Fail 602 qPrintU 10 603 qPrint qNl 604 ret false 605 end 606 if not qCheckF32 wk 0x770000 1024.0f32 0.5f32 607 qPrint qP1Fail 608 qPrintU 11 609 qPrint qNl 610 ret false 611 end 612 qPrint qP1Ok 613 ret true 614 end 615 616 note ========================================================================= 617 note Phase 2: Full 40-Layer Qwen 3.6 35B-A3B FP8 Native GPFIFO Pipeline 618 note 30 GDN layers + 10 Full-Attn HD256 layers (L = 3,7,11,...,39) 619 note 4 cold weight banks (> 2.8 GB total > 115x L2), 256 experts, BF16 GDN state 620 note ========================================================================= 621 fn qWaitFast timeout ulong to bool 622 t0 = monons 623 while lt ulong sub monons t0 timeout bound `5000000` 624 if eq mapget get gbmem 3 16 4 `1179010630` 625 put nvret 0 get nvput 0 626 ret true 627 end 628 r = sleepns long 5000 629 end 630 ret false 631 end 632 633 fn qReplayPkt slot ulong to bool 634 r = mapset get gbmem 2 8192 4 0 635 r = mapset get gbmem 3 0 4 0 636 r = mapset get gbmem 3 16 4 0 637 nb = get qPktBytes slot 638 if not nvsend get qPkt slot 0 uint div nb 4 639 ret false 640 end 641 if not qWaitFast `15000000000` 642 ret false 643 end 644 ret eq mapget get gbmem 3 0 4 `1145324612` 645 end 646 647 fn qRecordLayer l ulong bk ulong useW4Moe bool useW4Trunk bool to bool 648 lutva = mapaddr get qLut 0 649 isAttn = eq rem l 4 3 650 if isAttn 651 note Full-Attn layer: qkvg[9216,2048] (W4 or FP8) -> Split-K HD256 (T=1024, S=8) -> reduce+gate 652 if useW4Trunk 653 qPtr qW 0x004000 654 qPtr mapaddr get qWAttnQkvg bk 655 qPtr add mapaddr get qWAttnQkvg bk `9437184` 656 qPtr qW 0x010000 657 qArg 2048 658 qArg 9216 659 if not qRecord 12 288 1 1 256 660 ret false 661 end 662 else 663 qPtr qW 0x004000 664 qPtr mapaddr get qWAttnQkvg bk 665 qPtr mapaddr get qWAttnQkvgS bk 666 qPtr lutva 667 qPtr qW 0x010000 668 qArg 2048 669 qArg 9216 670 if not qRecord 1 288 1 1 256 671 ret false 672 end 673 end 674 qPtr qW 0x010000 675 qPtr mapaddr get qKvK 0 676 qPtr mapaddr get qKvV 0 677 qPtr qW 0x040000 678 qPtr qW 0x060000 679 qArg 1024 680 qArg 8 681 if not qRecord 7 16 8 1 32 682 ret false 683 end 684 qPtr qW 0x040000 685 qPtr qW 0x060000 686 qPtr qW 0x014000 687 qPtr qW 0x020000 688 qArg 8 689 if not qRecord 8 16 1 1 256 690 ret false 691 end 692 else 693 note GDN layer: fused in_proj_qkvz[12288,2048] (W4 or FP8) + in_proj_ab[64,2048] BF16 -> coalesced gdn_step_bf16 694 if useW4Trunk 695 qPtr qW 0x004000 696 qPtr mapaddr get qWGdnQkv bk 697 qPtr add mapaddr get qWGdnQkv bk `12582912` 698 qPtr qW 0x010000 699 qArg 2048 700 qArg 12288 701 if not qRecord 12 384 1 1 256 702 ret false 703 end 704 else 705 qPtr qW 0x004000 706 qPtr mapaddr get qWGdnQkv bk 707 qPtr mapaddr get qWGdnQkvS bk 708 qPtr lutva 709 qPtr qW 0x010000 710 qArg 2048 711 qArg 12288 712 if not qRecord 1 384 1 1 256 713 ret false 714 end 715 end 716 qPtr mapaddr get qWGdnAb bk 717 qPtr qW 0x004000 718 qPtr qW 0x01c000 719 qArg 2048 720 qArg 64 721 if not qRecord 0 2 1 1 256 722 ret false 723 end 724 gdnIdx = sub l div add l 1 4 725 gdnOff = mul gdnIdx `1048576` 726 qPtr qW 0x010000 727 qPtr qW 0x018000 728 qPtr qW 0x01c000 729 qPtr add mapaddr get qGdnState 0 gdnOff 730 qPtr qW 0x020000 731 if not qRecord 6 32 1 1 128 732 ret false 733 end 734 end 735 note Mixer out_proj[2048,4096] (W4 or FP8) 736 if useW4Trunk 737 qPtr qW 0x020000 738 qPtr mapaddr get qWOut bk 739 qPtr add mapaddr get qWOut bk `4194304` 740 qPtr qW 0x024000 741 qArg 4096 742 qArg 2048 743 if not qRecord 12 64 1 1 256 744 ret false 745 end 746 else 747 qPtr qW 0x020000 748 qPtr mapaddr get qWOut bk 749 qPtr mapaddr get qWOutS bk 750 qPtr lutva 751 qPtr qW 0x024000 752 qArg 4096 753 qArg 2048 754 if not qRecord 1 64 1 1 256 755 ret false 756 end 757 end 758 note Router: bf16_gemv_fast [256, 2048] + router_top8_256_fast 759 qPtr mapaddr get qWRouter bk 760 qPtr qW 0x004000 761 qPtr qW 0x028000 762 qArg 2048 763 qArg 256 764 if not qRecord 0 8 1 1 256 765 ret false 766 end 767 qPtr qW 0x028000 768 qPtr qW 0x029000 769 qPtr qW 0x029100 770 if not qRecord 3 1 1 1 256 771 ret false 772 end 773 note Routed MoE: either W4 block-32 grouped-8 or FP8 block-128 grouped-8 774 if useW4Moe 775 qPtr qW 0x004000 776 qPtr mapaddr get qW4ExpGu bk 777 qPtr add mapaddr get qW4ExpGu bk `268435456` 778 qPtr qW 0x029000 779 qPtr qW 0x080000 780 qArg 2048 781 qArg 1024 782 qArg 0 783 if not qRecord 11 32 8 1 256 784 ret false 785 end 786 qPtr qW 0x080000 787 qPtr qW 0x029100 788 qPtr qW 0x090000 789 qArg 512 790 if not qRecord 4 4 8 1 128 791 ret false 792 end 793 qPtr qW 0x090000 794 qPtr mapaddr get qW4ExpDn bk 795 qPtr add mapaddr get qW4ExpDn bk `134217728` 796 qPtr qW 0x029000 797 qPtr qW 0x0a0000 798 qArg 512 799 qArg 2048 800 qArg 512 801 if not qRecord 11 64 8 1 256 802 ret false 803 end 804 else 805 qPtr qW 0x004000 806 qPtr mapaddr get qWExpGu bk 807 qPtr mapaddr get qWExpGuS bk 808 qPtr qW 0x029000 809 qPtr lutva 810 qPtr qW 0x080000 811 qArg 2048 812 qArg 1024 813 qArg 0 814 if not qRecord 2 32 8 1 256 815 ret false 816 end 817 qPtr qW 0x080000 818 qPtr qW 0x029100 819 qPtr qW 0x090000 820 qArg 512 821 if not qRecord 4 4 8 1 128 822 ret false 823 end 824 qPtr qW 0x090000 825 qPtr mapaddr get qWExpDn bk 826 qPtr mapaddr get qWExpDnS bk 827 qPtr qW 0x029000 828 qPtr lutva 829 qPtr qW 0x0a0000 830 qArg 512 831 qArg 2048 832 qArg 512 833 if not qRecord 2 64 8 1 256 834 ret false 835 end 836 end 837 note Shared expert: gu[1024, 2048] + down[2048, 512] (W4 or FP8) 838 if useW4Trunk 839 qPtr qW 0x004000 840 qPtr mapaddr get qWShGu bk 841 qPtr add mapaddr get qWShGu bk `1048576` 842 qPtr qW 0x0b0000 843 qArg 2048 844 qArg 1024 845 if not qRecord 12 32 1 1 256 846 ret false 847 end 848 qPtr qW 0x0b0000 849 qPtr mapaddr get qWShDn bk 850 qPtr add mapaddr get qWShDn bk 524288 851 qPtr qW 0x0b2000 852 qArg 512 853 qArg 2048 854 if not qRecord 12 64 1 1 256 855 ret false 856 end 857 else 858 qPtr qW 0x004000 859 qPtr mapaddr get qWShGu bk 860 qPtr mapaddr get qWShGuS bk 861 qPtr lutva 862 qPtr qW 0x0b0000 863 qArg 2048 864 qArg 1024 865 if not qRecord 1 32 1 1 256 866 ret false 867 end 868 qPtr qW 0x0b0000 869 qPtr mapaddr get qWShDn bk 870 qPtr mapaddr get qWShDnS bk 871 qPtr lutva 872 qPtr qW 0x0b2000 873 qArg 512 874 qArg 2048 875 if not qRecord 1 64 1 1 256 876 ret false 877 end 878 end 879 note Fused MoE combine + shared + residual + next-layer RMSNorm (D=2048) 880 qPtr qW 0x0a0000 881 qPtr qW 0x0b2000 882 qPtr qW 0x000000 883 qPtr qW 0x008000 884 qPtr qW 0x000000 885 qPtr qW 0x004000 886 if not qRecord 5 1 1 1 256 887 ret false 888 end 889 ret true 890 end 891 892 fn qRecordAll40 slot ulong headMode ulong to bool 893 put qFastMode 0 1 894 put qFastSlot 0 slot 895 put qFastPos 0 0 896 put gbused 0 0 897 gbsetup 898 gbmth 0 40 4 899 gbemit 0 900 gbemit 0 901 gbemit 0 902 gbemit `2281701376` 903 cb = mapaddr get gbmem 1 904 gbdriver cb 905 gbdma cb 224 906 for j ulong 224 907 gbemit get gbcb j 908 end 909 n = get gbused 0 910 if ne mapwrite get qPkt slot 0 gbcmd 0 n long n 911 ret false 912 end 913 put qFastPos 0 n 914 useW4Moe = ge headMode 1 915 useW4Trunk = ge headMode 2 916 for l ulong 40 917 bk = rem l 4 918 if not qRecordLayer l bk useW4Moe useW4Trunk 919 put qFastMode 0 0 920 ret false 921 end 922 end 923 lutva = mapaddr get qLut 0 924 if ne headMode 2 925 note W4 block-32 two-pass exact lm_head: 0.286 GB W4 screen + Top-64 exact BF16 rescore! 926 qPtr mapaddr get qLmW4 0 927 qPtr mapaddr get qLmW4S 0 928 qPtr qW 0x004000 929 qPtr mapaddr get qLogits 0 930 qArg 248320 931 if not qRecord 9 7760 1 1 256 932 put qFastMode 0 0 933 ret false 934 end 935 qPtr mapaddr get qLmBf16 0 936 qPtr qW 0x004000 937 qPtr mapaddr get qCandIds 0 938 qPtr mapaddr get qCandLog 0 939 qArg 64 940 if not qRecord 10 64 1 1 32 941 put qFastMode 0 0 942 ret false 943 end 944 end 945 put gbused 0 0 946 gbtail 947 n = get gbused 0 948 pos = get qFastPos 0 949 if ne mapwrite get qPkt slot pos gbcmd 0 n long n 950 put qFastMode 0 0 951 ret false 952 end 953 put qPktBytes slot add pos n 954 put qFastMode 0 0 955 ret true 956 end 957 958 fn qAllocModel to bool 959 for b ulong 4 960 note GDN fused in_proj_qkvz[12288,2048] = 24 MiB, S[96,16] = 8 KiB 961 m = nvalloc `25165824` true 962 if le m 0 963 ret false 964 end 965 put qWGdnQkv b m 966 if not qFillPattern m `25165824` 0x30303030 967 ret false 968 end 969 m = nvalloc 8192 true 970 if le m 0 971 ret false 972 end 973 put qWGdnQkvS b m 974 if not qFillPattern m 8192 0x3b800000 975 ret false 976 end 977 note GDN in_proj_ab[64,2048] BF16 = 256 KiB 978 m = nvalloc 262144 true 979 if le m 0 980 ret false 981 end 982 put qWGdnAb b m 983 if not qFillPattern m 262144 0x3c003c00 984 ret false 985 end 986 note Mixer out_proj[2048,4096] = 8 MiB, S[16,32] = 4 KiB 987 m = nvalloc `8388608` true 988 if le m 0 989 ret false 990 end 991 put qWOut b m 992 if not qFillPattern m `8388608` 0x30303030 993 ret false 994 end 995 m = nvalloc 4096 true 996 if le m 0 997 ret false 998 end 999 put qWOutS b m 1000 if not qFillPattern m 4096 0x3b800000 1001 ret false 1002 end 1003 note Full-Attn qkvg[9216,2048] = 18 MiB, S[72,16] = 8 KiB 1004 m = nvalloc `18874368` true 1005 if le m 0 1006 ret false 1007 end 1008 put qWAttnQkvg b m 1009 if not qFillPattern m `18874368` 0x30303030 1010 ret false 1011 end 1012 m = nvalloc 8192 true 1013 if le m 0 1014 ret false 1015 end 1016 put qWAttnQkvgS b m 1017 if not qFillPattern m 8192 0x3b800000 1018 ret false 1019 end 1020 note Router[256,2048] BF16 = 1 MiB 1021 m = nvalloc `1048576` true 1022 if le m 0 1023 ret false 1024 end 1025 put qWRouter b m 1026 if not qFillPattern m `1048576` 0x3c003c00 1027 ret false 1028 end 1029 note Routed Experts: 256 x gate_up[1024,2048] = 512 MiB/bank, S = 128 KiB 1030 m = nvalloc `536870912` true 1031 if le m 0 1032 ret false 1033 end 1034 put qWExpGu b m 1035 if not qFillPattern m `536870912` 0x30303030 1036 ret false 1037 end 1038 m = nvalloc 131072 true 1039 if le m 0 1040 ret false 1041 end 1042 put qWExpGuS b m 1043 if not qFillPattern m 131072 0x3b800000 1044 ret false 1045 end 1046 note Routed Experts: 256 x down[2048,512] = 256 MiB/bank, S = 64 KiB 1047 m = nvalloc `268435456` true 1048 if le m 0 1049 ret false 1050 end 1051 put qWExpDn b m 1052 if not qFillPattern m `268435456` 0x30303030 1053 ret false 1054 end 1055 m = nvalloc 65536 true 1056 if le m 0 1057 ret false 1058 end 1059 put qWExpDnS b m 1060 if not qFillPattern m 65536 0x3b800000 1061 ret false 1062 end 1063 note W4 Routed Experts share the 4 cold banks (zero extra nvalloc slots!): gu at +0 (256 MiB), guS at +256 MiB (32 MiB) 1064 note dn at +0 (128 MiB), dnS at +128 MiB (16 MiB) 1065 put qW4ExpGu b get qWExpGu b 1066 put qW4ExpDn b get qWExpDn b 1067 note Shared Expert: gu[1024,2048] = 2 MiB, dn[2048,512] = 1 MiB 1068 m = nvalloc `2097152` true 1069 if le m 0 1070 ret false 1071 end 1072 put qWShGu b m 1073 if not qFillPattern m `2097152` 0x30303030 1074 ret false 1075 end 1076 m = nvalloc 4096 true 1077 if le m 0 1078 ret false 1079 end 1080 put qWShGuS b m 1081 if not qFillPattern m 4096 0x3b800000 1082 ret false 1083 end 1084 m = nvalloc `1048576` true 1085 if le m 0 1086 ret false 1087 end 1088 put qWShDn b m 1089 if not qFillPattern m `1048576` 0x30303030 1090 ret false 1091 end 1092 m = nvalloc 4096 true 1093 if le m 0 1094 ret false 1095 end 1096 put qWShDnS b m 1097 if not qFillPattern m 4096 0x3b800000 1098 ret false 1099 end 1100 end 1101 note 30 GDN BF16 states: 30 x 32 x 128 x 128 x 2B = 30 MiB 1102 m = nvalloc `31457280` true 1103 if le m 0 1104 ret false 1105 end 1106 put qGdnState 0 m 1107 if not qFillPattern m `31457280` 0 1108 ret false 1109 end 1110 note Full-Attn BF16 KV (T=1024, 2 KV heads, hd=256): 1 MiB K + 1 MiB V 1111 m = nvalloc `1048576` true 1112 if le m 0 1113 ret false 1114 end 1115 put qKvK 0 m 1116 if not qFillPattern m `1048576` 0x3c003c00 1117 ret false 1118 end 1119 m = nvalloc `1048576` true 1120 if le m 0 1121 ret false 1122 end 1123 put qKvV 0 m 1124 if not qFillPattern m `1048576` 0x3c003c00 1125 ret false 1126 end 1127 note FP8 lm_head [248320, 2048] = 508,559,360 B + scales [1940, 16] = 126,976 B 1128 m = nvalloc `508559360` true 1129 if le m 0 1130 ret false 1131 end 1132 put qLmFp8 0 m 1133 if not qFillPattern m `508559360` 0x30303030 1134 ret false 1135 end 1136 m = nvalloc 131072 true 1137 if le m 0 1138 ret false 1139 end 1140 put qLmFp8S 0 m 1141 if not qFillPattern m 131072 0x3b800000 1142 ret false 1143 end 1144 note BF16 lm_head [248320, 2048] = 1,017,118,720 B 1145 m = nvalloc `1017118720` true 1146 if le m 0 1147 ret false 1148 end 1149 put qLmBf16 0 m 1150 if not qFillPattern m `1017118720` 0x3c003c00 1151 ret false 1152 end 1153 note Logits [248320] = 993,280 B -> 1 MiB 1154 m = nvalloc `1048576` true 1155 if le m 0 1156 ret false 1157 end 1158 put qLogits 0 m 1159 note W4 block-32 lm_head [248320, 256] u32 = 254,279,680 B + scales [248320, 32] u32 = 31,784,960 B 1160 m = nvalloc `254279680` true 1161 if le m 0 1162 ret false 1163 end 1164 put qLmW4 0 m 1165 if not qFillPattern m `254279680` 0x99999999 1166 ret false 1167 end 1168 m = nvalloc `31784960` true 1169 if le m 0 1170 ret false 1171 end 1172 put qLmW4S 0 m 1173 if not qFillPattern m `31784960` 0x3c003c00 1174 ret false 1175 end 1176 m = nvalloc 4096 true 1177 if le m 0 1178 ret false 1179 end 1180 put qCandIds 0 m 1181 for ci ulong 64 1182 r = mapset m mul ci 4 4 mul ci 17 1183 end 1184 m = nvalloc 4096 true 1185 if le m 0 1186 ret false 1187 end 1188 put qCandLog 0 m 1189 note 4 GPFIFO command packet buffers (2 MiB each) 1190 for s ulong 4 1191 m = nvalloc `2097152` true 1192 if le m 0 1193 ret false 1194 end 1195 put qPkt s m 1196 end 1197 ret true 1198 end 1199 1200 fn qInitWork to bool 1201 wk = get qWork 0 1202 for i ulong 2048 1203 r = mapset wk mul i 4 4 0x3dcccccd 1204 r = mapset wk add 0x004000 mul i 4 4 0x3dcccccd 1205 r = mapset wk add 0x008000 mul i 4 4 0x3f800000 1206 end 1207 ret true 1208 end 1209 1210 fn qSortTimes t edit ulong 16 to void 1211 for i ulong 16 1212 for j ulong 15 1213 if gt get t j get t add j 1 1214 tmp = get t j 1215 put t j get t add j 1 1216 put t add j 1 tmp 1217 end 1218 end 1219 end 1220 end 1221 1222 fn qBenchConfig slot ulong labelMode ulong to bool 1223 for w ulong 2 1224 if not qReplayPkt slot 1225 ret false 1226 end 1227 end 1228 ts = new ulong 16 0 1229 for it ulong 16 1230 t0 = monons 1231 if not qReplayPkt slot 1232 ret false 1233 end 1234 t1 = monons 1235 put ts it div ulong sub t1 t0 1000 1236 end 1237 qSortTimes ts 1238 medUs = get ts 8 1239 tokSx100 = div `100000000` medUs 1240 if eq labelMode 0 1241 qPrint qRes0 1242 end 1243 if eq labelMode 1 1244 qPrint qRes1 1245 end 1246 if eq labelMode 2 1247 qPrint qRes2 1248 end 1249 if eq labelMode 3 1250 qPrint qRes3 1251 end 1252 qPrintU medUs 1253 qPrint qTokS 1254 qPrintU tokSx100 1255 for i ulong 2048 1256 if not qIsFiniteU32 mapget get qWork 0 mul i 4 4 1257 qPrint qFinBad 1258 ret false 1259 end 1260 end 1261 if gt labelMode 0 1262 for i ulong 1024 1263 if not qIsFiniteU32 mapget get qLogits 0 mul i 4 4 1264 qPrint qFinBad 1265 ret false 1266 end 1267 end 1268 end 1269 qPrint qFinOk 1270 ret true 1271 end 1272 1273 fn qFillIds ids edit uint 8 l ulong tok ulong to void 1274 t = rem tok 4 1275 for k ulong 8 1276 v = add mul l 7 mul k 13 1277 v = rem add v t 256 1278 put ids k uint v 1279 end 1280 end 1281 1282 fn qBenchArbTok tok ulong to void 1283 ids = new uint 8 0 1284 pr = new uint 8 0 1285 q36ArbResetToken 1286 for l ulong 40 1287 qFillIds ids l tok 1288 if gt l 0 1289 put q36ArbHits 0 add get q36ArbHits 0 q36ArbScorePred pr 8 ids 1290 put q36ArbTotal 0 add get q36ArbTotal 0 8 1291 end 1292 q36ArbObserve l ids 1293 if lt l 39 1294 n = q36ArbPredict l ids 8 pr 1295 end 1296 end 1297 end 1298 1299 fn qBenchArboris40L to void 1300 q36ArbResetAll 1301 t0 = monons 1302 for tok ulong 64 1303 qBenchArbTok tok 1304 end 1305 t1 = monons 1306 usPerTok = div ulong sub t1 t0 64000 1307 hitPct = div mul get q36ArbHits 0 100 get q36ArbTotal 0 1308 qPrint qArbRes 1309 qPrintU usPerTok 1310 qPrint qArbHit 1311 qPrintU hitPct 1312 qPrint qNl 1313 end 1314 1315 fn main to int 1316 qPrint qHdr 1317 if not q36ArbSelfTest 1318 qPrint qArbFail 1319 ret 1 1320 end 1321 qPrint qArbOk 1322 if not gbopen 1 1323 qPrint qOpenErr 1324 qPrintU ulong get nvstage 0 1325 qPrint qNl 1326 ret 2 1327 end 1328 if not gbbopen 1329 qPrint qOpenErr 1330 qPrintU ulong get nvstage 0 1331 qPrint qNl 1332 ret 3 1333 end 1334 put qCodeMap 0 0 1335 qEmitAll 1336 csize = mul div add get qCodePos 0 4095 4096 4096 1337 cm = nvalloc csize false 1338 if le cm 0 1339 qPrint qOpenErr 1340 qPrintU ulong get nvstage 0 1341 qPrint qNl 1342 ret 4 1343 end 1344 put qCodeMap 0 cm 1345 put qCodeFail 0 0 1346 qEmitAll 1347 if ne get qCodeFail 0 0 1348 ret 5 1349 end 1350 lm = nvalloc 4096 true 1351 if le lm 0 1352 ret 6 1353 end 1354 put qLut 0 lm 1355 if not qWriteLut lm 1356 ret 7 1357 end 1358 wm = nvalloc `16777216` true 1359 if le wm 0 1360 ret 8 1361 end 1362 put qWork 0 wm 1363 if not qSelfTest 1364 rc = nvmclose 1365 ret 10 1366 end 1367 if not qAllocModel 1368 qPrint qOpenErr 1369 qPrintU ulong get nvstage 0 1370 qPrint qNl 1371 rc = nvmclose 1372 ret 11 1373 end 1374 if not qInitWork 1375 rc = nvmclose 1376 ret 12 1377 end 1378 for slot ulong 4 1379 if not qRecordAll40 slot slot 1380 qPrint qSubErr 1381 qPrintU ulong get gbst 4 1382 qPrint qNl 1383 rc = nvmclose 1384 ret 13 1385 end 1386 end 1387 for slot ulong 4 1388 if not qBenchConfig slot slot 1389 rc = nvmclose 1390 ret 14 1391 end 1392 end 1393 qBenchArboris40L 1394 rc = nvmclose 1395 ret 0 1396 end