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/engine/runtime_qwen4_b8.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 / engine / runtime_qwen4_b8.om` 961 lines ยท 22256 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-only 2 note om/gpu/engine/runtime_qwen4_b8.om: Pure Seeded-OM-2 Zero-CUDA Batched B=8 Multi-Stream Runtime 3 note for Qwen 3.8 Flash Next (qwen4_exp). 4 note - Partitions the existing 128K KV cache into 8 streams x 16,384 tokens (0 extra KV RAM) 5 note - Allocates 8 independent GDN states + PLE conv states + 32 MiB batched activation slab 6 note - Executes ALL dense W4 & BF16 projections + lm_head in 1 launch across all 8 streams 7 note (uW4GemmB8 kid 30 + uBf16GemmB8 kid 31 -> 8x weight reuse & 8x launch amortization!) 8 note - Keeps Full-APU (20 ARMv9 CPU cores + 48 Blackwell SMs) + Live Arboris ON by default! 9 10 const Q4_B8 ulong 8 11 const Q4_B8_WORK_BYTES ulong `33554432` 12 const Q4_B8_SHM_BYTES ulong 524288 13 14 note Offsets inside q4B8Work (32 MiB) 15 const B8_HC_X ulong 0 16 const B8_HC_NORM ulong 524288 17 const B8_HC_DOWN ulong `1048576` 18 const B8_HC_UP ulong `1114112` 19 const B8_HC_INJ ulong `1638400` 20 const B8_XN ulong `1703936` 21 const B8_QKV ulong `1835008` 22 const B8_K ulong `2359296` 23 const B8_V ulong `2424832` 24 const B8_Z ulong `2490368` 25 const B8_AB ulong `2752512` 26 const B8_Y ulong `2818048` 27 const B8_AOUT ulong `3145728` 28 const B8_MOE_LOG ulong `3276800` 29 const B8_MOE_W ulong `3342336` 30 const B8_MOE_MID ulong `3407872` 31 const B8_GU ulong `3670016` 32 const B8_SW ulong `3801088` 33 const B8_SH_OUT ulong `3866624` 34 const B8_SH_GATE ulong `3997696` 35 const B8_PLE_KEY ulong `4063232` 36 const B8_PLE_KN ulong `4587520` 37 const B8_PLE_QN ulong `5111808` 38 const B8_PLE_VAL ulong `5636096` 39 const B8_PLE_GV ulong `5767168` 40 const B8_PLE_GN ulong `6291456` 41 const B8_POS ulong `6815744` 42 const B8_LOGW4 ulong `7340032` 43 const B8_CIDS ulong `15728640` 44 const B8_CLOG ulong `15794176` 45 const B8_SH_UP ulong `16777216` 46 const B8_X_NV4 ulong `16908288` 47 const B8_SX_NV4 ulong `17432576` 48 49 note Offsets inside q4B8Shm (512 KiB uncached NVLink-C2C shm) 50 const B8_SHM_XN ulong 0 51 const B8_SHM_PLE ulong 131072 52 const B8_SHM_TRACE ulong 262144 53 54 global q4B8Work long 1 55 global q4B8WorkVa ulong 1 56 global q4B8Shm long 1 57 global q4B8ShmVa ulong 1 58 global q4B8GdnStateMap long 1 59 global q4B8GdnStateVa ulong 1 60 global q4B8GdnStateStride ulong 1 61 global q4B8GdnTailMap long 1 62 global q4B8GdnTailVa ulong 1 63 global q4B8GdnTailStride ulong 1 64 global q4B8PleConvMap long 1 65 global q4B8PleConvVa ulong 1 66 global q4B8PleConvStride ulong 1 67 global q4B8Ready ulong 1 68 69 note Per-stream PLE n-gram history (8 streams) 70 global q4B8PleHist long 16 71 global q4B8PleSegPos ulong 8 72 73 note Per-stream input tokens, positions, and output tokens for batched step 74 global q4B8InTok ulong 8 75 global q4B8InPos ulong 8 76 global q4B8Active ulong 8 77 global q4B8OutTok ulong 8 78 79 fn b8W off ulong to ulong 80 ret add get q4B8WorkVa 0 off 81 end 82 83 fn q4B8QuantActNv4 xVa ulong K ulong to bool 84 engPtr xVa 85 engPtr mapaddr get engLut 0 86 engPtr b8W B8_X_NV4 87 engPtr b8W B8_SX_NV4 88 engArg uint K 89 ret engPktEmitGy 40 8 1 256 90 end 91 92 fn q4B8EmitNv4GemmCached wVa ulong sVa ulong cVa ulong K ulong N ulong to bool 93 engPtr b8W B8_X_NV4 94 engPtr b8W B8_SX_NV4 95 engPtr wVa 96 engPtr sVa 97 engPtr cVa 98 engArg uint K 99 engArg uint N 100 engArg uint 8 101 ret engPktEmitGy 41 uint div add N 63 64 1 128 102 end 103 104 fn q4B8EmitW4Gemm xVa ulong wVa ulong sVa ulong cVa ulong K ulong N ulong to bool 105 if eq get engUseNvfp4 0 1 106 if eq rem K 64 0 107 if not q4B8QuantActNv4 xVa K 108 ret false 109 end 110 ret q4B8EmitNv4GemmCached wVa sVa cVa K N 111 end 112 engPtr xVa 113 engPtr wVa 114 engPtr sVa 115 engPtr cVa 116 engArg uint K 117 engArg uint N 118 ret engPktEmitGy 35 uint div add N 7 8 1 256 119 end 120 engPtr xVa 121 engPtr wVa 122 engPtr sVa 123 engPtr cVa 124 engArg uint K 125 engArg uint N 126 ret engPktEmitGy 30 uint div add N 7 8 1 256 127 end 128 129 fn q4B8EmitBf16Gemm wVa ulong xVa ulong cVa ulong K ulong N ulong to bool 130 engPtr wVa 131 engPtr xVa 132 engPtr cVa 133 engArg uint K 134 engArg uint N 135 ret engPktEmitGy 31 uint div add N 7 8 1 256 136 end 137 138 fn q4B8EmitHcPrep L ulong isMlp bool to bool 139 H = get engSpec MS_H 140 hc = get engSpec MS_HC_COUNT 141 hcH = mul hc H 142 lr = get engSpec MS_HC_LOWRANK 143 normVa = get q4AHcNormVa L 144 dnVa = get q4AHcDnVa L 145 upVa = get q4AHcUpVa L 146 injVa = get q4AHcInjVa L 147 if isMlp 148 normVa = get q4MHcNormVa L 149 dnVa = get q4MHcDnVa L 150 upVa = get q4MHcUpVa L 151 injVa = get q4MHcInjVa L 152 end 153 engPtr b8W B8_HC_X 154 engPtr normVa 155 engPtr b8W B8_HC_NORM 156 if not engPktEmitGy 44 uint hc uint Q4_B8 256 157 ret false 158 end 159 if not q4B8EmitBf16Gemm dnVa b8W B8_HC_NORM b8W B8_HC_DOWN hcH lr 160 ret false 161 end 162 for b ulong Q4_B8 163 engPtr add b8W B8_HC_DOWN mul b mul lr 4 164 engArg uint lr 165 if not engPktEmitGy 18 uint div add lr 255 256 1 256 166 ret false 167 end 168 end 169 if not q4B8EmitBf16Gemm upVa b8W B8_HC_DOWN b8W B8_HC_UP lr hcH 170 ret false 171 end 172 engPtr b8W B8_HC_UP 173 engPtr b8W B8_HC_NORM 174 engPtr b8W B8_XN 175 if not engPktEmitGy 45 uint div H 256 uint Q4_B8 256 176 ret false 177 end 178 ret q4B8EmitBf16Gemm injVa b8W B8_HC_NORM b8W B8_HC_INJ hcH hc 179 end 180 181 fn q4B8EmitHcInject to bool 182 H = get engSpec MS_H 183 hc = get engSpec MS_HC_COUNT 184 engPtr b8W B8_HC_X 185 engPtr b8W B8_AOUT 186 engPtr b8W B8_HC_INJ 187 ret engPktEmitGy 46 uint div H 256 uint mul hc Q4_B8 256 188 end 189 190 fn q4B8EmitPleLayer causal bool to bool 191 H = get engSpec MS_H 192 hc = get engSpec MS_HC_COUNT 193 hcH = mul hc H 194 pleVa = add get q4B8ShmVa 0 B8_SHM_PLE 195 if not q4B8EmitBf16Gemm get q4PleKeyProjVa 0 pleVa b8W B8_PLE_KEY H hcH 196 ret false 197 end 198 if not q4B8EmitBf16Gemm get q4PleValProjVa 0 pleVa b8W B8_PLE_VAL H H 199 ret false 200 end 201 csStride = get q4B8PleConvStride 0 202 for b ulong Q4_B8 203 offHc = mul b mul hcH 4 204 engPtr add b8W B8_PLE_KEY offHc 205 engPtr get q4PleNormKeyVa 0 206 engPtr add b8W B8_PLE_KN offHc 207 if not engPktEmitGy 17 uint hc 1 256 208 ret false 209 end 210 engPtr add b8W B8_HC_X offHc 211 engPtr get q4PleNormQryVa 0 212 engPtr add b8W B8_PLE_QN offHc 213 if not engPktEmitGy 17 uint hc 1 256 214 ret false 215 end 216 engPtr add b8W B8_PLE_KN offHc 217 engPtr add b8W B8_PLE_QN offHc 218 engPtr add b8W B8_PLE_VAL mul b mul H 4 219 engPtr add b8W B8_PLE_GV offHc 220 if not engPktEmitGy 24 uint hc 1 256 221 ret false 222 end 223 engPtr add b8W B8_PLE_GV offHc 224 engPtr get q4PleNormConvVa 0 225 engPtr add b8W B8_PLE_GN offHc 226 if not engPktEmitGy 17 uint hc 1 256 227 ret false 228 end 229 convVa = add get q4B8PleConvVa 0 mul b csStride 230 if causal 231 convVa = get q4PleConvStateVa 0 232 end 233 engPtr add b8W B8_PLE_GV offHc 234 engPtr add b8W B8_PLE_GN offHc 235 engPtr get q4PleConvWVa 0 236 engPtr convVa 237 engPtr add b8W B8_HC_X offHc 238 if not engPktEmitGy 25 uint div hcH 256 1 256 239 ret false 240 end 241 end 242 ret true 243 end 244 245 fn q4B8EmitFaAttnOne L ulong faIdx ulong b ulong qN ulong kvN ulong causal bool to bool 246 kBase = add get engKvVa faIdx mul b div get engKvSlabBytes 0 16 247 if causal 248 kBase = get engKvVa faIdx 249 end 250 posVa = add b8W B8_POS mul b 4096 251 engPtr add b8W B8_K mul b mul kvN 4 252 engPtr add b8W B8_V mul b mul kvN 4 253 engPtr get engLKNormVa L 254 engPtr get engRopeVa 0 255 engPtr kBase 256 engPtr add kBase div get engKvSlabBytes 0 2 257 engPtr posVa 258 if not engPktEmitGy 7 uint get engSpec MS_NKV 1 uint get engSpec MS_HD 259 ret false 260 end 261 engPtr add b8W B8_QKV mul b mul qN 4 262 engPtr get engLQNormVa L 263 engPtr get engRopeVa 0 264 engPtr engW EW_QROT 265 engPtr posVa 266 if not engPktEmitGy 8 uint get engSpec MS_NQ 1 uint get engSpec MS_HD 267 ret false 268 end 269 engPtr engW EW_QROT 270 engPtr kBase 271 engPtr add kBase div get engKvSlabBytes 0 2 272 engPtr engW EW_OPART 273 engPtr engW EW_MLPART 274 engPtr posVa 275 if not engPktEmitGy 9 uint get engSpec MS_NQ 8 32 276 ret false 277 end 278 kBase = mul mul b 4 mul get engSpec MS_NQ get engSpec MS_HD 279 engPtr engW EW_OPART 280 engPtr engW EW_MLPART 281 engPtr add b8W B8_QKV mul b mul qN 4 282 engPtr add b8W B8_Y kBase 283 engArg 8 284 ret engPktEmitGy 10 uint get engSpec MS_NQ 1 uint get engSpec MS_HD 285 end 286 287 fn q4B8EmitFaLayer L ulong faIdx ulong causal bool to bool 288 H = get engSpec MS_H 289 qN = mul get engSpec MS_NQ get engSpec MS_HD 290 if eq get engSpec MS_ATTN_GATE 1 291 qN = mul qN 2 292 end 293 kvN = mul get engSpec MS_NKV get engSpec MS_HD 294 useNvOmma = and eq get engUseNvfp4 0 1 eq rem H 64 0 295 if useNvOmma 296 if not q4B8QuantActNv4 b8W B8_XN H 297 ret false 298 end 299 if not q4B8EmitNv4GemmCached get engLA1WVa L get engLA1SVa L b8W B8_QKV H qN 300 ret false 301 end 302 if not q4B8EmitNv4GemmCached get engLA2WVa L get engLA2SVa L b8W B8_K H kvN 303 ret false 304 end 305 if not q4B8EmitNv4GemmCached get engLA3WVa L get engLA3SVa L b8W B8_V H kvN 306 ret false 307 end 308 else 309 if not q4B8EmitW4Gemm b8W B8_XN get engLA1WVa L get engLA1SVa L b8W B8_QKV H qN 310 ret false 311 end 312 if not q4B8EmitW4Gemm b8W B8_XN get engLA2WVa L get engLA2SVa L b8W B8_K H kvN 313 ret false 314 end 315 if not q4B8EmitW4Gemm b8W B8_XN get engLA3WVa L get engLA3SVa L b8W B8_V H kvN 316 ret false 317 end 318 end 319 for b ulong Q4_B8 320 if not q4B8EmitFaAttnOne L faIdx b qN kvN causal 321 ret false 322 end 323 end 324 oK = mul get engSpec MS_NQ get engSpec MS_HD 325 ret q4B8EmitW4Gemm b8W B8_Y get engLA4WVa L get engLA4SVa L b8W B8_AOUT oK H 326 end 327 328 fn q4B8EmitGdnCausalOne L ulong gdnIdx ulong b ulong qkvN ulong zN ulong to bool 329 nV = get engSpec MS_GDN_NV 330 qkvVa = add b8W B8_QKV mul b mul qkvN 4 331 engPtr qkvVa 332 engPtr get engLA4SVa L 333 engPtr add get engGdnTailVa 0 mul gdnIdx mul qkvN 12 334 if not engPktEmitGy 13 uint div qkvN 128 1 128 335 ret false 336 end 337 engPtr qkvVa 338 engPtr add b8W B8_Z mul b mul zN 4 339 engPtr add b8W B8_AB mul b mul nV 8 340 engPtr get engLKNormVa L 341 engPtr add get engGdnStateVa 0 mul gdnIdx mul nV 65536 342 engPtr get engLQNormVa L 343 engPtr add b8W B8_Y mul b mul zN 4 344 ret engPktEmitGy 14 uint nV 1 128 345 end 346 347 fn q4B8EmitGdnLayer L ulong gdnIdx ulong causal bool to bool 348 H = get engSpec MS_H 349 nK = get engSpec MS_GDN_NK 350 nV = get engSpec MS_GDN_NV 351 qkvN = add mul nK 256 mul nV 128 352 zN = mul nV 128 353 useNvOmma = and eq get engUseNvfp4 0 1 eq rem H 64 0 354 if useNvOmma 355 if not q4B8QuantActNv4 b8W B8_XN H 356 ret false 357 end 358 if not q4B8EmitNv4GemmCached get engLA1WVa L get engLA1SVa L b8W B8_QKV H qkvN 359 ret false 360 end 361 if not q4B8EmitNv4GemmCached get engLA2WVa L get engLA2SVa L b8W B8_Z H zN 362 ret false 363 end 364 else 365 if not q4B8EmitW4Gemm b8W B8_XN get engLA1WVa L get engLA1SVa L b8W B8_QKV H qkvN 366 ret false 367 end 368 if not q4B8EmitW4Gemm b8W B8_XN get engLA2WVa L get engLA2SVa L b8W B8_Z H zN 369 ret false 370 end 371 end 372 if not q4B8EmitBf16Gemm get engLA4WVa L b8W B8_XN b8W B8_AB H mul nV 2 373 ret false 374 end 375 if causal 376 for b ulong Q4_B8 377 if not q4B8EmitGdnCausalOne L gdnIdx b qkvN zN 378 ret false 379 end 380 end 381 else 382 engPtr b8W B8_QKV 383 engPtr get engLA4SVa L 384 engPtr add get q4B8GdnTailVa 0 mul gdnIdx mul qkvN 12 385 engArg uint div get q4B8GdnTailStride 0 4 386 if not engPktEmitGy 48 uint div qkvN 128 uint Q4_B8 128 387 ret false 388 end 389 engPtr b8W B8_QKV 390 engPtr b8W B8_Z 391 engPtr b8W B8_AB 392 engPtr get engLKNormVa L 393 engPtr add get q4B8GdnStateVa 0 mul gdnIdx mul nV 65536 394 engPtr get engLQNormVa L 395 engPtr b8W B8_Y 396 engArg uint div get q4B8GdnStateStride 0 4 397 if not engPktEmitGy 49 uint nV uint Q4_B8 128 398 ret false 399 end 400 end 401 ret q4B8EmitW4Gemm b8W B8_Y get engLA3WVa L get engLA3SVa L b8W B8_AOUT zN H 402 end 403 404 fn q4B8EmitMoeRouterShared L ulong ids0Va ulong idsStride ulong to bool 405 H = get engSpec MS_H 406 shI = get engSpec MS_SH_I 407 if not q4B8EmitBf16Gemm get q4RouterWVa L b8W B8_XN b8W B8_MOE_LOG H get engSpec MS_NEXP 408 ret false 409 end 410 if not q4B8EmitBf16Gemm get q4ShGateSVa L b8W B8_XN b8W B8_SH_GATE H 1 411 ret false 412 end 413 engPtr b8W B8_MOE_LOG 414 engPtr ids0Va 415 engPtr b8W B8_MOE_W 416 engArg uint idsStride 417 if not engPktEmitGy 47 uint Q4_B8 1 32 418 ret false 419 end 420 if not q4B8EmitBf16Gemm get q4ShGateWVa L b8W B8_XN b8W B8_GU H shI 421 ret false 422 end 423 if not q4B8EmitBf16Gemm get q4ShUpWVa L b8W B8_XN b8W B8_SH_UP H shI 424 ret false 425 end 426 engPtr b8W B8_GU 427 engPtr b8W B8_SH_UP 428 engPtr b8W B8_SW 429 engArg uint shI 430 if not engPktEmitGy 43 uint div add shI 255 256 uint Q4_B8 256 431 ret false 432 end 433 ret q4B8EmitBf16Gemm get q4ShDnWVa L b8W B8_SW b8W B8_SH_OUT shI H 434 end 435 436 fn q4B8EmitMoeLayer L ulong to bool 437 H = get engSpec MS_H 438 moeI = get engSpec MS_MOE_I 439 topK = get engSpec MS_TOPK 440 idsStride = mul get engSpec MS_LAYERS topK 441 ids0Va = add get q4B8ShmVa 0 add B8_SHM_TRACE mul mul L topK 4 442 if not q4B8EmitMoeRouterShared L ids0Va idsStride 443 ret false 444 end 445 guKid = ulong 27 446 dnKid = ulong 28 447 if eq get engUseNvfp4 0 1 448 guKid = ulong 36 449 dnKid = ulong 37 450 end 451 engPtr b8W B8_XN 452 engPtr get q4ExpGuWVa L 453 engPtr get q4ExpGuSVa L 454 engPtr ids0Va 455 engPtr b8W B8_MOE_MID 456 engArg uint topK 457 engArg uint idsStride 458 gxGu = uint div add moeI 31 32 459 gyGu = uint mul Q4_B8 topK 460 if not engPktEmitGy guKid gxGu gyGu 256 461 ret false 462 end 463 engPtr b8W B8_MOE_MID 464 engPtr get q4ExpDnWVa L 465 engPtr get q4ExpDnSVa L 466 engPtr ids0Va 467 engPtr b8W B8_MOE_W 468 engPtr b8W B8_SH_OUT 469 engPtr b8W B8_SH_GATE 470 engPtr b8W B8_AOUT 471 engArg uint idsStride 472 ret engPktEmitGy dnKid uint div add H 31 32 uint Q4_B8 256 473 end 474 475 fn q4B8EmitOneLayer L ulong faIdx ulong gdnIdx ulong causal bool to bool 476 pleL1 = get engSpec MS_PLE_LAYER 477 if and gt pleL1 0 eq add L 1 pleL1 478 if not q4B8EmitPleLayer causal 479 ret false 480 end 481 end 482 if not q4B8EmitHcPrep L false 483 ret false 484 end 485 if eq get engLayerKind L 0 486 if not q4B8EmitFaLayer L faIdx causal 487 ret false 488 end 489 else 490 if not q4B8EmitGdnLayer L gdnIdx causal 491 ret false 492 end 493 end 494 if not q4B8EmitHcInject 495 ret false 496 end 497 if not q4B8EmitHcPrep L true 498 ret false 499 end 500 if not q4B8EmitMoeLayer L 501 ret false 502 end 503 ret q4B8EmitHcInject 504 end 505 506 fn q4B8EmitLayersRange startL ulong endL ulong causal bool to bool 507 nL = get engSpec MS_LAYERS 508 gdnIdx = ulong 0 509 faIdx = ulong 0 510 for L ulong nL bound 128 511 if and ge L startL lt L endL 512 if not q4B8EmitOneLayer L faIdx gdnIdx causal 513 ret false 514 end 515 end 516 if eq get engLayerKind L 0 517 faIdx = add faIdx 1 518 else 519 gdnIdx = add gdnIdx 1 520 end 521 end 522 ret true 523 end 524 525 fn q4B8EmitFinalMixToShm to bool 526 H = get engSpec MS_H 527 hc = get engSpec MS_HC_COUNT 528 hcH = mul hc H 529 lr = get engSpec MS_HC_LOWRANK 530 engPtr b8W B8_HC_X 531 engPtr get q4FinHcNormVa 0 532 engPtr b8W B8_HC_NORM 533 if not engPktEmitGy 44 uint hc uint Q4_B8 256 534 ret false 535 end 536 if not q4B8EmitBf16Gemm get q4FinHcDnVa 0 b8W B8_HC_NORM b8W B8_HC_DOWN hcH lr 537 ret false 538 end 539 for b ulong Q4_B8 540 engPtr add b8W B8_HC_DOWN mul b mul lr 4 541 engArg uint lr 542 if not engPktEmitGy 18 uint div add lr 255 256 1 256 543 ret false 544 end 545 end 546 if not q4B8EmitBf16Gemm get q4FinHcUpVa 0 b8W B8_HC_DOWN b8W B8_HC_UP lr hcH 547 ret false 548 end 549 engPtr b8W B8_HC_UP 550 engPtr b8W B8_HC_NORM 551 engPtr add get q4B8ShmVa 0 B8_SHM_XN 552 if not engPktEmitGy 45 uint div H 256 uint Q4_B8 256 553 ret false 554 end 555 ret true 556 end 557 558 fn q4B8EmitLmHeadGpu to bool 559 H = get engSpec MS_H 560 V = get engSpec MS_VOCAB 561 xnVa = add get q4B8ShmVa 0 B8_SHM_XN 562 note Compute ALL 8 streams' lm_head logits (8 x 248,320) in ONE pass over 318 MB W4 weights! 563 if not q4B8EmitW4Gemm xnVa get engLmW4Va 0 add get engLmW4Va 0 get engLmW4SOff 0 b8W B8_LOGW4 H V 564 ret false 565 end 566 for b ulong Q4_B8 567 logVa = add b8W B8_LOGW4 mul b mul V 4 568 cidVa = add b8W B8_CIDS mul b 512 569 clogVa = add b8W B8_CLOG mul b 512 570 engPtr logVa 571 engPtr cidVa 572 if not engPktEmitGy 12 64 1 256 573 ret false 574 end 575 engPtr mapaddr get engLmBf16 0 576 engPtr add xnVa mul b mul H 4 577 engPtr cidVa 578 engPtr clogVa 579 engArg 128 580 if not engPktEmitGy 5 128 1 32 581 ret false 582 end 583 end 584 ret true 585 end 586 587 fn q4B8RecordPackets to bool 588 nL = get engSpec MS_LAYERS 589 midL = div nL 2 590 note Slot 4: B=8 Layer 0 at 32 MiB (completely disjoint from 0..32 MiB B=1 workspace!) 591 engPktStartAt `33554432` 592 if not q4B8EmitOneLayer 0 0 0 false 593 ret false 594 end 595 if not engPktFinish 4 596 ret false 597 end 598 note Slot 5: B=8 Layers 1..midL-1 at 34 MiB (6 MiB headroom) 599 engPktStartAt `35651584` 600 if not q4B8EmitLayersRange 1 midL false 601 ret false 602 end 603 if not engPktFinish 5 604 ret false 605 end 606 note Slot 7: B=8 Layers midL..nL-1 + final mixer at 40 MiB (6 MiB headroom) 607 engPktStartAt `41943040` 608 if not q4B8EmitLayersRange midL nL false 609 ret false 610 end 611 if not q4B8EmitFinalMixToShm 612 ret false 613 end 614 if not engPktFinish 7 615 ret false 616 end 617 note Slot 6: B=8 Batched lm_head at 46 MiB (4 MiB headroom before 50 MiB) 618 engPktStartAt `48234496` 619 if not q4B8EmitLmHeadGpu 620 ret false 621 end 622 if not engPktFinish 6 623 ret false 624 end 625 note Slot 8..10: B=1 Causal Chunked-8 Prefill (shared KV/GDN/PLE state across b=0..7) 626 engPktStartAt `54525952` 627 if not q4B8EmitOneLayer 0 0 0 true 628 ret false 629 end 630 if not engPktFinish 8 631 ret false 632 end 633 engPktStartAt `56623104` 634 if not q4B8EmitLayersRange 1 midL true 635 ret false 636 end 637 if not engPktFinish 9 638 ret false 639 end 640 engPktStartAt `62914560` 641 if not q4B8EmitLayersRange midL nL true 642 ret false 643 end 644 ret engPktFinish 10 645 end 646 647 fn q4B8AllocStates to bool 648 nGdn = get engSpec MS_N_GDN 649 nK = get engSpec MS_GDN_NK 650 nV = get engSpec MS_GDN_NV 651 qkvN = add mul nK 256 mul nV 128 652 stStride = mul mul nGdn nV 65536 653 tlStride = mul div add mul mul nGdn qkvN 12 4095 4096 4096 654 put q4B8GdnStateStride 0 stStride 655 put q4B8GdnTailStride 0 tlStride 656 sm = nvalloc mul Q4_B8 stStride true 657 tm = nvalloc mul Q4_B8 tlStride true 658 if or le sm 0 le tm 0 659 ret false 660 end 661 put q4B8GdnStateMap 0 sm 662 put q4B8GdnStateVa 0 mapaddr sm 663 put q4B8GdnTailMap 0 tm 664 put q4B8GdnTailVa 0 mapaddr tm 665 ret true 666 end 667 668 fn q4B8Init to bool 669 w = nvalloc Q4_B8_WORK_BYTES true 670 if le w 0 671 ret false 672 end 673 put q4B8Work 0 w 674 put q4B8WorkVa 0 mapaddr w 675 s = nvalloc Q4_B8_SHM_BYTES false 676 if le s 0 677 ret false 678 end 679 put q4B8Shm 0 s 680 put q4B8ShmVa 0 mapaddr s 681 if not q4B8AllocStates 682 ret false 683 end 684 hcH = mul get engSpec MS_HC_COUNT get engSpec MS_H 685 csStride = mul div add mul hcH 36 65535 65536 65536 686 put q4B8PleConvStride 0 csStride 687 cm = nvalloc mul Q4_B8 csStride true 688 if le cm 0 689 ret false 690 end 691 put q4B8PleConvMap 0 cm 692 put q4B8PleConvVa 0 mapaddr cm 693 if not q4B8RecordPackets 694 ret false 695 end 696 put q4B8Ready 0 1 697 ret true 698 end 699 700 fn q4B8ResetStream b ulong to void 701 eos = long get engSpec MS_EOS_ID 702 put q4B8PleHist mul b 2 eos 703 put q4B8PleHist add mul b 2 1 eos 704 put q4B8PleSegPos b 0 705 for i ulong `1048576` 706 put engIoBuf i 0 707 end 708 stStride = get q4B8GdnStateStride 0 709 pos = ulong 0 710 while lt pos stStride bound 1024 711 cnt = sub stStride pos 712 if gt cnt `1048576` 713 cnt = ulong `1048576` 714 end 715 r = mapwrite get q4B8GdnStateMap 0 add mul b stStride pos engIoBuf 0 cnt 716 pos = add pos cnt 717 end 718 tlStride = get q4B8GdnTailStride 0 719 pos = ulong 0 720 while lt pos tlStride bound 256 721 cnt = sub tlStride pos 722 if gt cnt `1048576` 723 cnt = ulong `1048576` 724 end 725 r = mapwrite get q4B8GdnTailMap 0 add mul b tlStride pos engIoBuf 0 cnt 726 pos = add pos cnt 727 end 728 csStride = get q4B8PleConvStride 0 729 pos = ulong 0 730 while lt pos csStride bound 64 731 cnt = sub csStride pos 732 if gt cnt `1048576` 733 cnt = ulong `1048576` 734 end 735 r = mapwrite get q4B8PleConvMap 0 add mul b csStride pos engIoBuf 0 cnt 736 pos = add pos cnt 737 end 738 end 739 740 fn q4B8LoadEmbedOne b ulong tid ulong to bool 741 H = get engSpec MS_H 742 hc = get engSpec MS_HC_COUNT 743 hcH = mul hc H 744 b2 = mul H 2 745 if ne mapread get engEmbBf16 0 mul tid b2 engIoBuf 0 b2 long b2 746 ret false 747 end 748 engWidenBuf H 749 hBytes = mul H 4 750 base = add B8_HC_X mul b mul hcH 4 751 for c ulong hc bound 8 752 if ne mapwrite get q4B8Work 0 add base mul c hBytes engWideBuf 0 hBytes long hBytes 753 ret false 754 end 755 end 756 ret true 757 end 758 759 fn q4B8SetPosOne b ulong pos ulong to void 760 engPutU32Io 0 uint pos 761 r = mapwrite get q4B8Work 0 add B8_POS mul b 4096 engIoBuf 0 4 762 end 763 764 fn q4B8CopyPleToB8 b ulong to void 765 hb = mul get engSpec MS_H 4 766 r0 = mapread get q4ApuShm 0 Q4_SHM_PLE engIoBuf 0 hb 767 r1 = mapwrite get q4B8Shm 0 add B8_SHM_PLE mul b hb engIoBuf 0 hb 768 end 769 770 fn q4B8DispatchPleOne b ulong tok ulong to void 771 if eq get engSpec MS_PLE_LAYER 0 772 ret 773 end 774 eos = long get engSpec MS_EOS_ID 775 s0 = long tok 776 seg = get q4B8PleSegPos b 777 s1 = eos 778 s2 = eos 779 if ge seg 1 780 s1 = get q4B8PleHist add mul b 2 1 781 end 782 if ge seg 2 783 s2 = get q4B8PleHist mul b 2 784 end 785 put q4B8PleHist mul b 2 get q4B8PleHist add mul b 2 1 786 put q4B8PleHist add mul b 2 1 s0 787 if eq s0 eos 788 put q4B8PleSegPos b 0 789 else 790 put q4B8PleSegPos b add seg 1 791 end 792 mix2 = xor mul s0 get q4PleMult 0 mul s1 get q4PleMult 1 793 mix3 = xor mix2 mul s2 get q4PleMult 2 794 hpn = get engSpec MS_PLE_HPN 795 nHeads = mul 2 hpn 796 for h ulong nHeads bound 20 797 if lt h hpn 798 q4SendOnePleHead h mix2 799 else 800 q4SendOnePleHead h mix3 801 end 802 end 803 for h ulong nHeads bound 20 804 q4ApuReadPair get q4ApuResFds h 805 end 806 q4B8CopyPleToB8 b 807 end 808 809 text b8Dbg4 " [B8] S4=" 810 text b8Dbg5 " S5=" 811 text b8Dbg7 " S7=" 812 text b8Dbg6 " S6=" 813 text b8DbgNl "\n" 814 815 fn q4B8StepDecode needLogits bool to bool 816 for b ulong Q4_B8 817 tok = get q4B8InTok b 818 pos = get q4B8InPos b 819 if not q4B8LoadEmbedOne b tok 820 ret false 821 end 822 q4B8SetPosOne b pos 823 end 824 note Stage 1: Kick B=8 Layer 0 on 48 SMs WHILE 16 ARMv9 CPU cores read 8x 51B PLE in parallel! 825 if not q4KickPre 4 826 ret false 827 end 828 for b ulong Q4_B8 829 if ne get q4B8Active b 0 830 q4B8DispatchPleOne b get q4B8InTok b 831 end 832 end 833 if not q4WaitPre 834 engPrint b8Dbg4 835 engPrintU get engSlotLen 4 836 engPrint b8DbgNl 837 ret false 838 end 839 note Stage 2: Run B=8 Layers 1..midL-1 (Slot 5) + Layers midL..47 + final mixer (Slot 7) -> q4B8Shm[B8_SHM_XN] 840 if not engReplayPre 5 841 engPrint b8Dbg5 842 engPrintU get engSlotLen 5 843 engPrint b8DbgNl 844 ret false 845 end 846 if not engReplayPre 7 847 engPrint b8Dbg7 848 engPrintU get engSlotLen 7 849 engPrint b8DbgNl 850 ret false 851 end 852 if not needLogits 853 nL = get engSpec MS_LAYERS 854 topK = get engSpec MS_TOPK 855 for b ulong Q4_B8 856 if ne get q4B8Active b 0 857 q4ArbProcessTrace get q4B8Shm 0 add B8_SHM_TRACE mul b mul mul nL topK 4 858 end 859 end 860 ret true 861 end 862 note Stage 3: Kick B=8 Batched lm_head (1 pass over 318 MB W4 for all 8 streams!) WHILE Live Arboris updates! 863 if not q4KickPre 6 864 ret false 865 end 866 nL = get engSpec MS_LAYERS 867 topK = get engSpec MS_TOPK 868 for b ulong Q4_B8 869 if ne get q4B8Active b 0 870 q4ArbProcessTrace get q4B8Shm 0 add B8_SHM_TRACE mul b mul mul nL topK 4 871 end 872 end 873 if not q4WaitPre 874 engPrint b8Dbg6 875 engPrintU get engSlotLen 6 876 engPrint b8DbgNl 877 ret false 878 end 879 for b ulong Q4_B8 880 if ne get q4B8Active b 0 881 r0 = mapread get q4B8Work 0 add B8_CIDS mul b 512 engIoBuf 0 512 882 for i ulong 128 883 put engCandIds i engGetU32Io mul i 4 884 end 885 r1 = mapread get q4B8Work 0 add B8_CLOG mul b 512 engIoBuf 0 512 886 bestV = f32 -`1000000000`.0f32 887 bestTok = ulong 0 888 for i ulong 128 889 v = bitcast f32 engGetU32Io mul i 4 890 if gt v bestV 891 bestV = v 892 bestTok = ulong get engCandIds i 893 end 894 end 895 put q4B8OutTok b bestTok 896 end 897 end 898 ret true 899 end 900 901 fn q4PrefillChunk8 pos0 ulong to bool 902 for b ulong Q4_B8 903 tok = ulong get engTokSeq add pos0 b 904 if not q4B8LoadEmbedOne b tok 905 ret false 906 end 907 q4B8SetPosOne b add pos0 b 908 end 909 if not q4KickPre 8 910 ret false 911 end 912 hb = mul get engSpec MS_H 4 913 for b ulong Q4_B8 914 tok = ulong get engTokSeq add pos0 b 915 if not q4DispatchPleApu tok 916 ret false 917 end 918 r0 = mapread get q4ApuShm 0 Q4_SHM_PLE engIoBuf 0 hb 919 r1 = mapwrite get q4B8Shm 0 add B8_SHM_PLE mul b hb engIoBuf 0 hb 920 end 921 if not q4WaitPre 922 ret false 923 end 924 if not engReplayPre 9 925 ret false 926 end 927 if not engReplayPre 10 928 ret false 929 end 930 nL = get engSpec MS_LAYERS 931 topK = get engSpec MS_TOPK 932 for b ulong Q4_B8 933 q4ArbProcessTrace get q4B8Shm 0 add B8_SHM_TRACE mul b mul mul nL topK 4 934 end 935 ret true 936 end 937 938 fn q4B8PrefillSeq ntok ulong to ulong 939 q4ResetState 940 pos = ulong 0 941 if and eq get q4B8Ready 0 1 gt ntok 8 942 nCh = div sub ntok 1 8 943 for c ulong nCh bound 2048 944 if not q4PrefillChunk8 pos 945 ret 999999 946 end 947 pos = add pos 8 948 end 949 end 950 tok = ulong 0 951 while lt pos ntok bound 2048 952 t = ulong get engTokSeq pos 953 tok = q4StepToken t pos eq add pos 1 ntok 954 if eq tok 999999 955 ret 999999 956 end 957 pos = add pos 1 958 end 959 put engCurPos 0 ntok 960 ret tok 961 end