omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / gpu / inkling2 / decoder / prefill_b8.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Pure Seeded-Omega B=8 Batched Prefill Engine for Inkling-2-Mini on GB10 (ZERO CUDA, ZERO C). |
| 3 | note Uses bf16_gemm_batch_fast (kernel 26, 8x fewer BF16 weight loads) and |
| 4 | note nvfp4_gemm_dedup_fast / mxfp8_gemm_dedup_fast (kernels 27/28, deduplicated MoE experts across 8 tokens). |
| 5 | note Composes after: emit_fast.om + emit_batch.om + arboris + safetensors + tokenizer + decode.om + batcher.om. |
| 6 | |
| 7 | const B8_H ulong 0x600000 |
| 8 | const B8_HNORM ulong 0x620000 |
| 9 | const B8_HK ulong 0x640000 |
| 10 | const B8_HV ulong 0x660000 |
| 11 | const B8_HRES ulong 0x680000 |
| 12 | const B8_Q ulong 0x6a0000 |
| 13 | const B8_K ulong 0x6c0000 |
| 14 | const B8_V ulong 0x6d0000 |
| 15 | const B8_GATE ulong 0x6e0000 |
| 16 | const B8_RLAT ulong 0x700000 |
| 17 | const B8_SINK ulong 0x708000 |
| 18 | const B8_KSCP ulong 0x710000 |
| 19 | const B8_VSCP ulong 0x711000 |
| 20 | const B8_AOUT ulong 0x720000 |
| 21 | const B8_OPROJ ulong 0x740000 |
| 22 | const B8_OSCONV ulong 0x760000 |
| 23 | const B8_BLOCKS ulong 0x780000 |
| 24 | const B8_PREBLK ulong 0x840000 |
| 25 | const B8_PREH ulong 0x890000 |
| 26 | const B8_PREVBLK ulong 0x8b0000 |
| 27 | const B8_MSGS ulong 0x900000 |
| 28 | const B8_AROUT ulong 0x950000 |
| 29 | const B8_MLPIN ulong 0x990000 |
| 30 | const B8_MLPNORM ulong 0x9b0000 |
| 31 | const B8_RLOGITS ulong 0x9d0000 |
| 32 | const B8_RIDS ulong 0x9d2000 |
| 33 | const B8_RWTS ulong 0x9d3000 |
| 34 | const B8_META13 ulong 0x9e0000 |
| 35 | const B8_META2 ulong 0x9e2000 |
| 36 | const B8_XQ ulong 0x9f0000 |
| 37 | const B8_XREC ulong 0xa20000 |
| 38 | const B8_C13 ulong 0xb00000 |
| 39 | const B8_SWI ulong 0xc00000 |
| 40 | const B8_C2 ulong 0xc80000 |
| 41 | const B8_MLPOUT ulong 0xd40000 |
| 42 | const B8_MGCAT ulong 0xd60000 |
| 43 | const B8_MGLAT ulong 0xd90000 |
| 44 | const B8_MGFG ulong 0xda0000 |
| 45 | const B8_MGGN ulong 0xdc0000 |
| 46 | const B8_QSCR ulong 0xde0000 |
| 47 | |
| 48 | global ikB8Toks uint 8 |
| 49 | global ikB8MetaBuf uchar 3072 |
| 50 | global ikB8ArbIds uint 3072 |
| 51 | |
| 52 | fn ikB8PutU32Meta off ulong v uint to void |
| 53 | put ikB8MetaBuf off uchar and v 255 |
| 54 | put ikB8MetaBuf add off 1 uchar and shr v 8 255 |
| 55 | put ikB8MetaBuf add off 2 uchar and shr v 16 255 |
| 56 | put ikB8MetaBuf add off 3 uchar and shr v 24 255 |
| 57 | end |
| 58 | |
| 59 | fn ikB8EmbedWrite b ulong invRms f32 to void |
| 60 | ar = get ikStArena 0 |
| 61 | normBase = sub get ikGW 1 get ikStArenaVa 0 |
| 62 | wk = get ikDWork 0 |
| 63 | r0 = mapread ar normBase ikDOutBuf 0 9216 |
| 64 | for i ulong 2304 |
| 65 | o = mul i 4 |
| 66 | g = ikDBufF32 o |
| 67 | v = mul mul get ikDProjTmp i invRms g |
| 68 | ikDBufPutF32 o v |
| 69 | end |
| 70 | r1 = mapwrite wk add B8_H mul b 9216 ikDOutBuf 0 9216 |
| 71 | end |
| 72 | |
| 73 | fn ikB8EmbedOne b ulong tok ulong to void |
| 74 | ar = get ikStArena 0 |
| 75 | embBase = add sub get ikGW 0 get ikStArenaVa 0 mul tok 4608 |
| 76 | r = mapread ar embBase ikDOutBuf 0 4608 |
| 77 | sq = f32 0.0f32 |
| 78 | for i ulong 2304 |
| 79 | v = ikDReadBf16Buf i |
| 80 | put ikDProjTmp i v |
| 81 | sq = add sq mul v v |
| 82 | end |
| 83 | invRms = div f32 1.0f32 fsqrt add div sq f32 2304.0f32 f32 0.000001f32 |
| 84 | ikB8EmbedWrite b invRms |
| 85 | end |
| 86 | |
| 87 | fn ikB8Copy8 dstOff ulong srcOff ulong to bool |
| 88 | ikDArgReset |
| 89 | ikDPtr ikDW srcOff |
| 90 | ikDPtr 0 |
| 91 | ikDPtr ikDW dstOff |
| 92 | ikDArgU 18432 |
| 93 | ret ikDBatchAdd 23 144 1 1 128 |
| 94 | end |
| 95 | |
| 96 | fn ikB8Add8 dstOff ulong aOff ulong bOff ulong to bool |
| 97 | ikDArgReset |
| 98 | ikDPtr ikDW aOff |
| 99 | ikDPtr ikDW bOff |
| 100 | ikDPtr ikDW dstOff |
| 101 | ikDArgU 18432 |
| 102 | ret ikDBatchAdd 23 144 1 1 128 |
| 103 | end |
| 104 | |
| 105 | fn ikB8Bf16 wVa ulong xOff ulong yOff ulong k uint n uint to bool |
| 106 | ikDArgReset |
| 107 | ikDPtr wVa |
| 108 | ikDPtr ikDW xOff |
| 109 | ikDPtr ikDW yOff |
| 110 | ikDArgU k |
| 111 | ikDArgU n |
| 112 | ikDArgU 8 |
| 113 | gx = div add n 7 8 |
| 114 | ret ikDBatchAdd 26 gx 1 1 256 |
| 115 | end |
| 116 | |
| 117 | fn ikB8CrossPassMsgs loopIdx ulong to bool |
| 118 | nPrev = get ikDPrevBlockCnt 0 |
| 119 | vaQ = add get ikGW 5 mul mul loopIdx 2 9216 |
| 120 | vaKV = add get ikGW 6 mul mul loopIdx 2 9216 |
| 121 | for b ulong 8 |
| 122 | pBlk = add B8_PREVBLK mul b 0x9000 |
| 123 | mBase = add B8_MSGS mul b 0x9000 |
| 124 | ikDArgReset |
| 125 | ikDPtr ikDW pBlk |
| 126 | ikDPtr 0 |
| 127 | ikDPtr ikDW 0x190000 |
| 128 | ikDArgU 2304 |
| 129 | if not ikDBatchAdd 5 uint nPrev 1 1 256 |
| 130 | ret false |
| 131 | end |
| 132 | ikDPtr ikDW 0x190000 |
| 133 | ikDPtr vaQ |
| 134 | ikDPtr ikDW mBase |
| 135 | ikDArgU uint nPrev |
| 136 | ikDArgU 1 |
| 137 | ikDArgU 2304 |
| 138 | if not ikDBatchAdd 4 1 1 1 256 |
| 139 | ret false |
| 140 | end |
| 141 | ikDPtr ikDW 0x190000 |
| 142 | ikDPtr add vaQ 9216 |
| 143 | ikDPtr ikDW add mBase 27648 |
| 144 | ikDArgU uint nPrev |
| 145 | ikDArgU 1 |
| 146 | ikDArgU 2304 |
| 147 | if not ikDBatchAdd 4 1 1 1 256 |
| 148 | ret false |
| 149 | end |
| 150 | ikDPtr ikDW 0x190000 |
| 151 | ikDPtr vaKV |
| 152 | ikDPtr ikDW add mBase 9216 |
| 153 | ikDArgU uint nPrev |
| 154 | ikDArgU 2 |
| 155 | ikDArgU 2304 |
| 156 | if not ikDBatchAdd 4 2 1 1 256 |
| 157 | ret false |
| 158 | end |
| 159 | end |
| 160 | ret true |
| 161 | end |
| 162 | |
| 163 | fn ikB8MsgGateApplyOne l ulong slotBase ulong hOff ulong mOff ulong b ulong outBase ulong to bool |
| 164 | lOff = add B8_MGLAT mul b 256 |
| 165 | fgOff = add B8_MGFG mul b 9216 |
| 166 | gnOff = add B8_MGGN mul b 9216 |
| 167 | oOff = add outBase mul b 9216 |
| 168 | ikDArgReset |
| 169 | ikDPtr ikLva l add slotBase 1 |
| 170 | ikDPtr ikDW lOff |
| 171 | ikDPtr ikDW fgOff |
| 172 | ikDArgU 2304 |
| 173 | if not ikDBatchAdd 21 72 1 1 256 |
| 174 | ret false |
| 175 | end |
| 176 | ikDPtr ikLva l add slotBase 2 |
| 177 | ikDPtr ikDW lOff |
| 178 | ikDPtr ikDW gnOff |
| 179 | ikDArgU 2304 |
| 180 | if not ikDBatchAdd 21 72 1 1 256 |
| 181 | ret false |
| 182 | end |
| 183 | ikDPtr ikDW hOff |
| 184 | ikDPtr ikDW mOff |
| 185 | ikDPtr ikDW fgOff |
| 186 | ikDPtr ikDW gnOff |
| 187 | ikDPtr ikDW oOff |
| 188 | ikDArgU 2304 |
| 189 | ret ikDBatchAdd 19 18 1 1 128 |
| 190 | end |
| 191 | |
| 192 | fn ikB8MsgGate l ulong ctrlIdx ulong hBase ulong hStride ulong msgRel ulong outBase ulong to bool |
| 193 | slotBase = add 28 mul ctrlIdx 3 |
| 194 | for b ulong 8 |
| 195 | hOff = add hBase mul b hStride |
| 196 | mOff = add add B8_MSGS mul b 0x9000 msgRel |
| 197 | cOff = add B8_MGCAT mul b 18432 |
| 198 | ikDArgReset |
| 199 | ikDPtr ikDW hOff |
| 200 | ikDPtr 0 |
| 201 | ikDPtr ikDW cOff |
| 202 | ikDArgU 2304 |
| 203 | if not ikDBatchAdd 5 1 1 1 256 |
| 204 | ret false |
| 205 | end |
| 206 | ikDPtr ikDW mOff |
| 207 | ikDPtr 0 |
| 208 | ikDPtr ikDW add cOff 9216 |
| 209 | ikDArgU 2304 |
| 210 | if not ikDBatchAdd 5 1 1 1 256 |
| 211 | ret false |
| 212 | end |
| 213 | end |
| 214 | if not ikB8Bf16 ikLva l slotBase B8_MGCAT B8_MGLAT 4608 64 |
| 215 | ret false |
| 216 | end |
| 217 | for b ulong 8 |
| 218 | hOff = add hBase mul b hStride |
| 219 | mOff = add add B8_MSGS mul b 0x9000 msgRel |
| 220 | if not ikB8MsgGateApplyOne l slotBase hOff mOff b outBase |
| 221 | ret false |
| 222 | end |
| 223 | end |
| 224 | ret true |
| 225 | end |
| 226 | |
| 227 | fn ikB8AttnResOne b ulong bc ulong qVa ulong qCnt uint to bool |
| 228 | blk = add B8_BLOCKS mul b 0x18000 |
| 229 | h = add B8_H mul b 9216 |
| 230 | ao = add B8_AROUT mul b 0x7000 |
| 231 | ret ikDBatchAttnRes blk bc h qVa qCnt ao |
| 232 | end |
| 233 | |
| 234 | fn ikB8PrepAttnIn l ulong isFull bool hasMsgs bool to bool |
| 235 | if not ikB8Copy8 B8_HRES B8_H |
| 236 | ret false |
| 237 | end |
| 238 | bc = get ikDBlockCnt 0 |
| 239 | if gt l 0 |
| 240 | qCnt = uint 1 |
| 241 | if isFull |
| 242 | qCnt = uint 3 |
| 243 | end |
| 244 | qVa = get ikAttnQOff sub l 1 |
| 245 | for b ulong 8 |
| 246 | if not ikB8AttnResOne b bc qVa qCnt |
| 247 | ret false |
| 248 | end |
| 249 | end |
| 250 | else |
| 251 | for b ulong 8 |
| 252 | if not ikDBatchCopy2304 add B8_AROUT mul b 0x7000 add B8_H mul b 9216 |
| 253 | ret false |
| 254 | end |
| 255 | end |
| 256 | end |
| 257 | if not hasMsgs |
| 258 | for b ulong 8 |
| 259 | arBase = add B8_AROUT mul b 0x7000 |
| 260 | if not ikDBatchCopy2304 add B8_H mul b 9216 arBase |
| 261 | ret false |
| 262 | end |
| 263 | if isFull |
| 264 | if not ikDBatchCopy2304 add B8_HK mul b 9216 add arBase 9216 |
| 265 | ret false |
| 266 | end |
| 267 | if not ikDBatchCopy2304 add B8_HV mul b 9216 add arBase 18432 |
| 268 | ret false |
| 269 | end |
| 270 | else |
| 271 | if not ikDBatchCopy2304 add B8_HK mul b 9216 arBase |
| 272 | ret false |
| 273 | end |
| 274 | if not ikDBatchCopy2304 add B8_HV mul b 9216 arBase |
| 275 | ret false |
| 276 | end |
| 277 | end |
| 278 | end |
| 279 | else |
| 280 | if not ikB8MsgGate l 0 B8_AROUT 0x7000 0 B8_H |
| 281 | ret false |
| 282 | end |
| 283 | if isFull |
| 284 | if not ikB8MsgGate l 1 add B8_AROUT 9216 0x7000 9216 B8_HK |
| 285 | ret false |
| 286 | end |
| 287 | if not ikB8MsgGate l 2 add B8_AROUT 18432 0x7000 18432 B8_HV |
| 288 | ret false |
| 289 | end |
| 290 | else |
| 291 | if not ikB8MsgGate l 1 B8_AROUT 0x7000 9216 B8_HK |
| 292 | ret false |
| 293 | end |
| 294 | if not ikB8MsgGate l 2 B8_AROUT 0x7000 18432 B8_HV |
| 295 | ret false |
| 296 | end |
| 297 | end |
| 298 | end |
| 299 | ret true |
| 300 | end |
| 301 | |
| 302 | fn ikB8AttnKvSconv step ulong l ulong isFull bool to bool |
| 303 | scBase = ikDScBaseStep step |
| 304 | if isFull |
| 305 | if not ikB8Bf16 ikLva l 12 B8_HK B8_KSCP 2304 32 |
| 306 | ret false |
| 307 | end |
| 308 | if not ikB8Bf16 ikLva l 13 B8_HV B8_VSCP 2304 32 |
| 309 | ret false |
| 310 | end |
| 311 | for b ulong 8 |
| 312 | kOff = add B8_K mul b 4096 |
| 313 | vOff = add B8_V mul b 4096 |
| 314 | ikDArgReset |
| 315 | ikDPtr ikDW kOff |
| 316 | ikDPtr ikDW scBase |
| 317 | ikDPtr ikLva l 10 |
| 318 | ikDPtr ikDW add B8_KSCP mul b 128 |
| 319 | ikDPtr ikDW kOff |
| 320 | ikDArgU 1024 |
| 321 | ikDArgU 7 |
| 322 | if not ikDBatchAdd 18 8 1 1 128 |
| 323 | ret false |
| 324 | end |
| 325 | ikDPtr ikDW vOff |
| 326 | ikDPtr ikDW add scBase 12288 |
| 327 | ikDPtr ikLva l 11 |
| 328 | ikDPtr ikDW add B8_VSCP mul b 128 |
| 329 | ikDPtr ikDW vOff |
| 330 | ikDArgU 1024 |
| 331 | ikDArgU 7 |
| 332 | if not ikDBatchAdd 18 8 1 1 128 |
| 333 | ret false |
| 334 | end |
| 335 | end |
| 336 | else |
| 337 | for b ulong 8 |
| 338 | kOff = add B8_K mul b 4096 |
| 339 | vOff = add B8_V mul b 4096 |
| 340 | ikDArgReset |
| 341 | ikDPtr ikDW kOff |
| 342 | ikDPtr ikDW scBase |
| 343 | ikDPtr ikLva l 10 |
| 344 | ikDPtr ikDW kOff |
| 345 | ikDArgU 1024 |
| 346 | if not ikDBatchAdd 11 8 1 1 128 |
| 347 | ret false |
| 348 | end |
| 349 | ikDPtr ikDW vOff |
| 350 | ikDPtr ikDW add scBase 12288 |
| 351 | ikDPtr ikLva l 11 |
| 352 | ikDPtr ikDW vOff |
| 353 | ikDArgU 1024 |
| 354 | if not ikDBatchAdd 11 8 1 1 128 |
| 355 | ret false |
| 356 | end |
| 357 | end |
| 358 | end |
| 359 | ret true |
| 360 | end |
| 361 | |
| 362 | fn ikB8AttnCausalOne step ulong l ulong isFull bool b ulong posQ ulong to bool |
| 363 | dMaxRel = add posQ 1 |
| 364 | if gt dMaxRel 1024 |
| 365 | dMaxRel = ulong 1024 |
| 366 | end |
| 367 | relTiles = div add dMaxRel 127 128 |
| 368 | ikDArgReset |
| 369 | ikDPtr ikDW add B8_RLAT mul b 1536 |
| 370 | ikDPtr ikLva l 7 |
| 371 | ikDPtr ikDW 0x020000 |
| 372 | ikDArgU uint dMaxRel |
| 373 | if not ikDBatchAdd 22 uint relTiles 24 1 128 |
| 374 | ret false |
| 375 | end |
| 376 | kva = ikDKvVaStep step |
| 377 | ikDPtr ikDW add B8_K mul b 4096 |
| 378 | ikDPtr ikDW add B8_V mul b 4096 |
| 379 | ikDPtr kva |
| 380 | ikDPtr add kva IK_KV_HALF_BYTES |
| 381 | ikDArgU uint IK_TMAX |
| 382 | ikDArgU uint posQ |
| 383 | if not ikDBatchAdd 24 8 1 1 64 |
| 384 | ret false |
| 385 | end |
| 386 | splitS = uint 1 |
| 387 | if ge posQ 64 |
| 388 | splitS = uint 4 |
| 389 | end |
| 390 | if ge posQ 512 |
| 391 | splitS = uint 8 |
| 392 | end |
| 393 | ikDPtr ikDW add B8_Q mul b 12288 |
| 394 | ikDPtr kva |
| 395 | ikDPtr add kva IK_KV_HALF_BYTES |
| 396 | ikDPtr ikDW 0x020000 |
| 397 | ikDPtr ikDW 0x1e2000 |
| 398 | ikDPtr ikDW 0x1e4000 |
| 399 | ikDArgU 24 |
| 400 | ikDArgU 8 |
| 401 | ikDArgU uint IK_TMAX |
| 402 | ikDArgU 1024 |
| 403 | ikDArgU uint posQ |
| 404 | if isFull |
| 405 | ikDArgU 0 |
| 406 | else |
| 407 | ikDArgU 1024 |
| 408 | end |
| 409 | ikDArgU splitS |
| 410 | if not ikDBatchAdd 13 24 splitS 1 256 |
| 411 | ret false |
| 412 | end |
| 413 | ikDPtr ikDW 0x1e2000 |
| 414 | ikDPtr ikDW 0x1e4000 |
| 415 | ikDPtr ikDW add B8_SINK mul b 96 |
| 416 | ikDPtr ikDW add B8_GATE mul b 12288 |
| 417 | ikDPtr ikDW add B8_AOUT mul b 12288 |
| 418 | ikDArgU 24 |
| 419 | ikDArgU splitS |
| 420 | ret ikDBatchAdd 14 24 1 1 32 |
| 421 | end |
| 422 | |
| 423 | fn ikB8RunLayerAttn step ulong l ulong startPosQ ulong hasMsgs bool to bool |
| 424 | isFull = or or eq l 5 eq l 11 or eq l 17 eq l 23 |
| 425 | if not ikB8PrepAttnIn l isFull hasMsgs |
| 426 | ret false |
| 427 | end |
| 428 | ikDArgReset |
| 429 | ikDPtr ikDW B8_H |
| 430 | ikDPtr ikLva l 16 |
| 431 | ikDPtr ikDW B8_HNORM |
| 432 | ikDArgU 2304 |
| 433 | if not ikDBatchAdd 5 8 1 1 256 |
| 434 | ret false |
| 435 | end |
| 436 | ikDPtr ikDW B8_HK |
| 437 | if isFull |
| 438 | ikDPtr ikLva l 17 |
| 439 | else |
| 440 | ikDPtr ikLva l 16 |
| 441 | end |
| 442 | ikDPtr ikDW B8_HK |
| 443 | ikDArgU 2304 |
| 444 | if not ikDBatchAdd 5 8 1 1 256 |
| 445 | ret false |
| 446 | end |
| 447 | ikDPtr ikDW B8_HV |
| 448 | if isFull |
| 449 | ikDPtr ikLva l 18 |
| 450 | else |
| 451 | ikDPtr ikLva l 16 |
| 452 | end |
| 453 | ikDPtr ikDW B8_HV |
| 454 | ikDArgU 2304 |
| 455 | if not ikDBatchAdd 5 8 1 1 256 |
| 456 | ret false |
| 457 | end |
| 458 | if not ikB8Bf16 ikLva l 0 B8_HNORM B8_Q 2304 3072 |
| 459 | ret false |
| 460 | end |
| 461 | if not ikB8Bf16 ikLva l 1 B8_HK B8_K 2304 1024 |
| 462 | ret false |
| 463 | end |
| 464 | if not ikB8Bf16 ikLva l 2 B8_HV B8_V 2304 1024 |
| 465 | ret false |
| 466 | end |
| 467 | if not ikB8Bf16 ikLva l 3 B8_HNORM B8_RLAT 2304 384 |
| 468 | ret false |
| 469 | end |
| 470 | if not ikB8Bf16 ikLva l 5 B8_HNORM B8_GATE 2304 3072 |
| 471 | ret false |
| 472 | end |
| 473 | if not ikB8Bf16 ikLva l 6 B8_HNORM B8_SINK 2304 24 |
| 474 | ret false |
| 475 | end |
| 476 | if not ikB8AttnKvSconv step l isFull |
| 477 | ret false |
| 478 | end |
| 479 | ikDArgReset |
| 480 | ikDPtr ikDW B8_Q |
| 481 | ikDPtr ikLva l 8 |
| 482 | ikDPtr ikDW 0x1e0000 |
| 483 | ikDPtr ikDW B8_Q |
| 484 | ikDArgU 24 |
| 485 | ikDArgU 128 |
| 486 | ikDArgU ikDF32Bits 22.627417f32 |
| 487 | if isFull |
| 488 | ikDArgU 3 |
| 489 | else |
| 490 | ikDArgU 1 |
| 491 | end |
| 492 | if not ikDBatchAdd 7 24 8 1 32 |
| 493 | ret false |
| 494 | end |
| 495 | ikDPtr ikDW B8_K |
| 496 | ikDPtr 0 |
| 497 | ikDPtr 0 |
| 498 | ikDPtr ikDW B8_K |
| 499 | ikDArgU 8 |
| 500 | ikDArgU 128 |
| 501 | ikDArgU ikDF32Bits 22.627417f32 |
| 502 | ikDArgU 0 |
| 503 | if not ikDBatchAdd 7 8 8 1 32 |
| 504 | ret false |
| 505 | end |
| 506 | ikDPtr ikDW B8_V |
| 507 | ikDPtr 0 |
| 508 | ikDPtr ikDW B8_V |
| 509 | ikDArgU 128 |
| 510 | if not ikDBatchAdd 5 64 1 1 256 |
| 511 | ret false |
| 512 | end |
| 513 | ikDPtr ikDW B8_RLAT |
| 514 | ikDPtr ikLva l 9 |
| 515 | ikDPtr ikDW 0x1e0000 |
| 516 | ikDPtr ikDW B8_RLAT |
| 517 | ikDArgU 24 |
| 518 | ikDArgU 16 |
| 519 | ikDArgU ikDF32Bits 8.0f32 |
| 520 | if isFull |
| 521 | ikDArgU 3 |
| 522 | else |
| 523 | ikDArgU 1 |
| 524 | end |
| 525 | if not ikDBatchAdd 7 24 8 1 32 |
| 526 | ret false |
| 527 | end |
| 528 | for b ulong 8 |
| 529 | if not ikB8AttnCausalOne step l isFull b add startPosQ b |
| 530 | ret false |
| 531 | end |
| 532 | end |
| 533 | if not ikB8Bf16 ikLva l 4 B8_AOUT B8_OPROJ 3072 2304 |
| 534 | ret false |
| 535 | end |
| 536 | scBase = ikDScBaseStep step |
| 537 | for b ulong 8 |
| 538 | ikDArgReset |
| 539 | ikDPtr ikDW add B8_OPROJ mul b 9216 |
| 540 | ikDPtr ikDW add scBase 24576 |
| 541 | ikDPtr ikLva l 14 |
| 542 | ikDPtr ikDW add B8_OSCONV mul b 9216 |
| 543 | ikDArgU 2304 |
| 544 | if not ikDBatchAdd 11 18 1 1 128 |
| 545 | ret false |
| 546 | end |
| 547 | end |
| 548 | ret true |
| 549 | end |
| 550 | |
| 551 | fn ikB8UploadMeta l ulong to ulong |
| 552 | wk = get ikDWork 0 |
| 553 | r = mapread wk B8_RIDS ikDOutBuf 0 256 |
| 554 | ids = new uint 64 0 |
| 555 | for i ulong 64 |
| 556 | o = mul i 4 |
| 557 | b0 = uint get ikDOutBuf o |
| 558 | b1 = shl uint get ikDOutBuf add o 1 8 |
| 559 | b2 = shl uint get ikDOutBuf add o 2 16 |
| 560 | b3 = shl uint get ikDOutBuf add o 3 24 |
| 561 | put ids i or or b0 b1 or b2 b3 |
| 562 | end |
| 563 | u = msDedupExperts ids 8 |
| 564 | for i ulong 64 |
| 565 | ikB8PutU32Meta mul i 4 get msUniqEids i |
| 566 | ikB8PutU32Meta add 256 mul i 4 get msUniqCnt i |
| 567 | end |
| 568 | for i ulong 512 |
| 569 | ikB8PutU32Meta add 512 mul i 4 get msSlotMap i |
| 570 | end |
| 571 | if lt l 20 |
| 572 | ikDReadArenaChunk ikLva l 24 512 |
| 573 | for i ulong 512 |
| 574 | put ikB8MetaBuf add 2560 i get ikDOutBuf i |
| 575 | end |
| 576 | rw1 = mapwrite wk B8_META13 ikB8MetaBuf 0 3072 |
| 577 | ikDReadArenaChunk ikLva l 27 512 |
| 578 | for i ulong 512 |
| 579 | put ikB8MetaBuf add 2560 i get ikDOutBuf i |
| 580 | end |
| 581 | rw2 = mapwrite wk B8_META2 ikB8MetaBuf 0 3072 |
| 582 | else |
| 583 | rw3 = mapwrite wk B8_META13 ikB8MetaBuf 0 2560 |
| 584 | end |
| 585 | ret u |
| 586 | end |
| 587 | |
| 588 | fn ikB8RecordArb step ulong to void |
| 589 | wk = get ikDWork 0 |
| 590 | r = mapread wk B8_RIDS ikDOutBuf 0 256 |
| 591 | for b ulong 8 |
| 592 | dstBase = add mul mul b 48 8 mul step 8 |
| 593 | for k ulong 8 |
| 594 | o = mul add mul b 8 k 4 |
| 595 | b0 = uint get ikDOutBuf o |
| 596 | b1 = shl uint get ikDOutBuf add o 1 8 |
| 597 | b2 = shl uint get ikDOutBuf add o 2 16 |
| 598 | b3 = shl uint get ikDOutBuf add o 3 24 |
| 599 | put ikB8ArbIds add dstBase k or or b0 b1 or b2 b3 |
| 600 | end |
| 601 | end |
| 602 | end |
| 603 | |
| 604 | fn ikB8MlpAttnResOne b ulong bc2 ulong mqVa ulong to bool |
| 605 | blk = add B8_BLOCKS mul b 0x18000 |
| 606 | hr = add B8_HRES mul b 9216 |
| 607 | mi = add B8_MLPIN mul b 9216 |
| 608 | ret ikDBatchAttnRes blk bc2 hr mqVa 1 mi |
| 609 | end |
| 610 | |
| 611 | fn ikB8RunLayerMlpPre step ulong l ulong hasMsgs bool to bool |
| 612 | isBoundary = eq rem l 6 0 |
| 613 | if isBoundary |
| 614 | bc = get ikDBlockCnt 0 |
| 615 | for b ulong 8 |
| 616 | dstB = add add B8_BLOCKS mul b 0x18000 mul bc 9216 |
| 617 | srcB = add B8_HRES mul b 9216 |
| 618 | if not ikDBatchCopy2304 dstB srcB |
| 619 | ret false |
| 620 | end |
| 621 | end |
| 622 | put ikDBlockCnt 0 add bc 1 |
| 623 | if not ikB8Copy8 B8_HRES B8_OSCONV |
| 624 | ret false |
| 625 | end |
| 626 | else |
| 627 | if not ikB8Add8 B8_HRES B8_HRES B8_OSCONV |
| 628 | ret false |
| 629 | end |
| 630 | end |
| 631 | mqVa = get ikMlpQOff l |
| 632 | bc2 = get ikDBlockCnt 0 |
| 633 | for b ulong 8 |
| 634 | if not ikB8MlpAttnResOne b bc2 mqVa |
| 635 | ret false |
| 636 | end |
| 637 | end |
| 638 | if hasMsgs |
| 639 | if not ikB8MsgGate l 3 B8_MLPIN 9216 27648 B8_MLPIN |
| 640 | ret false |
| 641 | end |
| 642 | end |
| 643 | ikDArgReset |
| 644 | ikDPtr ikDW B8_MLPIN |
| 645 | ikDPtr ikLva l 19 |
| 646 | ikDPtr ikDW B8_MLPNORM |
| 647 | ikDArgU 2304 |
| 648 | if not ikDBatchAdd 5 8 1 1 256 |
| 649 | ret false |
| 650 | end |
| 651 | if not ikB8Bf16 ikLva l 20 B8_MLPNORM B8_RLOGITS 2304 128 |
| 652 | ret false |
| 653 | end |
| 654 | for b ulong 8 |
| 655 | ikDArgReset |
| 656 | ikDPtr ikDW add B8_RLOGITS mul b 512 |
| 657 | ikDPtr ikLva l 21 |
| 658 | ikDPtr ikDW add B8_RIDS mul b 32 |
| 659 | ikDPtr ikDW add B8_RWTS mul b 32 |
| 660 | if not ikDBatchAdd 12 1 1 1 32 |
| 661 | ret false |
| 662 | end |
| 663 | end |
| 664 | lutva = mapaddr get ikDKLut 0 |
| 665 | ikDArgReset |
| 666 | if lt l 20 |
| 667 | ikDPtr ikDW B8_MLPNORM |
| 668 | ikDPtr lutva |
| 669 | ikDPtr ikDW 0x059000 |
| 670 | ikDPtr ikDW B8_QSCR |
| 671 | ikDPtr ikDW add B8_QSCR 0x18000 |
| 672 | ikDPtr ikDW B8_XQ |
| 673 | ikDArgU 1152 |
| 674 | if not ikDBatchAdd 8 9 1 1 128 |
| 675 | ret false |
| 676 | end |
| 677 | else |
| 678 | ikDPtr ikDW B8_MLPNORM |
| 679 | ikDPtr lutva |
| 680 | ikDPtr ikDW B8_QSCR |
| 681 | ikDPtr ikDW add B8_QSCR 0x18000 |
| 682 | ikDPtr ikDW B8_XQ |
| 683 | ikDArgU 576 |
| 684 | if not ikDBatchAdd 9 5 1 1 128 |
| 685 | ret false |
| 686 | end |
| 687 | end |
| 688 | ikDPtr ikDW B8_XQ |
| 689 | ikDPtr ikDW B8_XREC |
| 690 | ikDArgU 2304 |
| 691 | if not ikDBatchAdd 25 18 8 1 128 |
| 692 | ret false |
| 693 | end |
| 694 | ret ikDBatchRun |
| 695 | end |
| 696 | |
| 697 | fn ikB8RunLayerMlpPost step ulong l ulong u ulong to bool |
| 698 | if not ikDBatchStart |
| 699 | ret false |
| 700 | end |
| 701 | lutva = mapaddr get ikDKLut 0 |
| 702 | ikDArgReset |
| 703 | if lt l 20 |
| 704 | ikDPtr ikDW B8_XREC |
| 705 | ikDPtr ikLva l 22 |
| 706 | ikDPtr ikLva l 23 |
| 707 | ikDPtr ikDW B8_META13 |
| 708 | ikDPtr lutva |
| 709 | ikDPtr ikDW B8_C13 |
| 710 | ikDArgU 2304 |
| 711 | ikDArgU 2304 |
| 712 | if not ikDBatchAdd 27 288 uint u 1 256 |
| 713 | ret false |
| 714 | end |
| 715 | ikDPtr ikDW B8_C13 |
| 716 | ikDPtr ikDW B8_RWTS |
| 717 | ikDPtr lutva |
| 718 | ikDPtr ikDW 0x059000 |
| 719 | ikDPtr ikDW B8_QSCR |
| 720 | ikDPtr ikDW add B8_QSCR 0x18000 |
| 721 | ikDPtr ikDW B8_SWI |
| 722 | ikDArgU 1152 |
| 723 | if not ikDBatchAdd 17 1 64 1 128 |
| 724 | ret false |
| 725 | end |
| 726 | ikDPtr ikDW B8_SWI |
| 727 | ikDPtr ikLva l 25 |
| 728 | ikDPtr ikLva l 26 |
| 729 | ikDPtr ikDW B8_META2 |
| 730 | ikDPtr lutva |
| 731 | ikDPtr ikDW B8_C2 |
| 732 | ikDArgU 1152 |
| 733 | ikDArgU 2304 |
| 734 | if not ikDBatchAdd 27 288 uint u 1 256 |
| 735 | ret false |
| 736 | end |
| 737 | else |
| 738 | ikDPtr ikDW B8_XREC |
| 739 | ikDPtr ikLva l 22 |
| 740 | ikDPtr ikLva l 23 |
| 741 | ikDPtr ikDW B8_META13 |
| 742 | ikDPtr lutva |
| 743 | ikDPtr ikDW B8_C13 |
| 744 | ikDArgU 2304 |
| 745 | ikDArgU 2304 |
| 746 | if not ikDBatchAdd 28 288 uint u 1 256 |
| 747 | ret false |
| 748 | end |
| 749 | ikDPtr ikDW B8_C13 |
| 750 | ikDPtr ikDW B8_RWTS |
| 751 | ikDPtr ikDW B8_SWI |
| 752 | ikDPtr 0 |
| 753 | ikDArgU 1152 |
| 754 | if not ikDBatchAdd 10 9 64 1 128 |
| 755 | ret false |
| 756 | end |
| 757 | ikDPtr ikDW B8_SWI |
| 758 | ikDPtr lutva |
| 759 | ikDPtr ikDW B8_QSCR |
| 760 | ikDPtr ikDW add B8_QSCR 0x18000 |
| 761 | ikDPtr ikDW B8_XREC |
| 762 | ikDArgU 2304 |
| 763 | if not ikDBatchAdd 9 18 1 1 128 |
| 764 | ret false |
| 765 | end |
| 766 | ikDPtr ikDW B8_XREC |
| 767 | ikDPtr ikLva l 25 |
| 768 | ikDPtr ikLva l 26 |
| 769 | ikDPtr ikDW B8_META13 |
| 770 | ikDPtr lutva |
| 771 | ikDPtr ikDW B8_C2 |
| 772 | ikDArgU 1152 |
| 773 | ikDArgU 2304 |
| 774 | if not ikDBatchAdd 28 288 uint u 1 256 |
| 775 | ret false |
| 776 | end |
| 777 | end |
| 778 | scBase = ikDScBaseStep step |
| 779 | for b ulong 8 |
| 780 | ikDArgReset |
| 781 | ikDPtr ikDW add B8_C2 mul b 73728 |
| 782 | ikDPtr ikDW add scBase 52224 |
| 783 | ikDPtr ikLva l 15 |
| 784 | ikDPtr ikDW add B8_MLPOUT mul b 9216 |
| 785 | ikDArgU 2304 |
| 786 | if not ikDBatchAdd 15 18 1 1 128 |
| 787 | ret false |
| 788 | end |
| 789 | end |
| 790 | ret ikB8Add8 B8_H B8_HRES B8_MLPOUT |
| 791 | end |
| 792 | |
| 793 | fn ikB8RunLayer step ulong l ulong startPosQ ulong hasMsgs bool to bool |
| 794 | if not ikB8RunLayerAttn step l startPosQ hasMsgs |
| 795 | ret false |
| 796 | end |
| 797 | if not ikB8RunLayerMlpPre step l hasMsgs |
| 798 | ret false |
| 799 | end |
| 800 | ikB8RecordArb step |
| 801 | u = ikB8UploadMeta l |
| 802 | ret ikB8RunLayerMlpPost step l u |
| 803 | end |
| 804 | |
| 805 | fn ikB8ArbFlushOne b ulong to void |
| 806 | ikArbResetToken |
| 807 | put ikDArbHasPred 0 0 |
| 808 | tokBase = mul mul b 48 8 |
| 809 | for s ulong 48 |
| 810 | for k ulong 8 |
| 811 | id = get ikB8ArbIds add tokBase add mul s 8 k |
| 812 | o = add mul s 32 mul k 4 |
| 813 | put ikDOutBuf o uchar and id 255 |
| 814 | put ikDOutBuf add o 1 uchar and shr id 8 255 |
| 815 | put ikDOutBuf add o 2 uchar and shr id 16 255 |
| 816 | put ikDOutBuf add o 3 uchar and shr id 24 255 |
| 817 | end |
| 818 | end |
| 819 | for l ulong 6 |
| 820 | ikDArbStepBuf l l |
| 821 | end |
| 822 | for j ulong 12 |
| 823 | ikDArbStepBuf add 6 j add 6 j |
| 824 | end |
| 825 | for pass ulong 2 |
| 826 | for j ulong 12 |
| 827 | ikDArbStepBuf add add 18 mul pass 12 j add 6 j |
| 828 | end |
| 829 | end |
| 830 | for j ulong 6 |
| 831 | ikDArbStepBuf add 42 j add 18 j |
| 832 | end |
| 833 | end |
| 834 | |
| 835 | fn ikB8SavePreLoop to bool |
| 836 | if not ikB8Copy8 B8_PREH B8_H |
| 837 | ret false |
| 838 | end |
| 839 | bc = get ikDBlockCnt 0 |
| 840 | for b ulong 8 |
| 841 | if not ikDBatchCopyBlocks add B8_PREBLK mul b 0x9000 add B8_BLOCKS mul b 0x18000 bc |
| 842 | ret false |
| 843 | end |
| 844 | end |
| 845 | put ikDPreBlockCnt 0 bc |
| 846 | ret true |
| 847 | end |
| 848 | |
| 849 | fn ikB8StartLoopPass to bool |
| 850 | preN = get ikDPreBlockCnt 0 |
| 851 | curN = get ikDBlockCnt 0 |
| 852 | nPrev = sub curN add preN 1 |
| 853 | for b ulong 8 |
| 854 | srcB = add add B8_BLOCKS mul b 0x18000 mul add preN 1 9216 |
| 855 | dstP = add B8_PREVBLK mul b 0x9000 |
| 856 | if not ikDBatchCopyBlocks dstP srcB nPrev |
| 857 | ret false |
| 858 | end |
| 859 | if not ikDBatchCopy2304 add dstP mul nPrev 9216 add B8_H mul b 9216 |
| 860 | ret false |
| 861 | end |
| 862 | end |
| 863 | put ikDPrevBlockCnt 0 add nPrev 1 |
| 864 | if not ikB8Copy8 B8_H B8_PREH |
| 865 | ret false |
| 866 | end |
| 867 | for b ulong 8 |
| 868 | if not ikDBatchCopyBlocks add B8_BLOCKS mul b 0x18000 add B8_PREBLK mul b 0x9000 preN |
| 869 | ret false |
| 870 | end |
| 871 | end |
| 872 | put ikDBlockCnt 0 preN |
| 873 | ret true |
| 874 | end |
| 875 | |
| 876 | note Prefill 8 prompt tokens (loaded in global ikB8Toks[0..7]) starting at startPosQ in slot. |
| 877 | fn ikDPrefillBatch8 slot ulong startPosQ ulong to bool |
| 878 | put ikDSlot 0 slot |
| 879 | for b ulong 8 |
| 880 | ikB8EmbedOne b ulong get ikB8Toks b |
| 881 | end |
| 882 | put ikDBlockCnt 0 0 |
| 883 | if not ikDBatchStart |
| 884 | ret false |
| 885 | end |
| 886 | for l ulong 6 |
| 887 | if not ikB8RunLayer l l startPosQ false |
| 888 | ret false |
| 889 | end |
| 890 | end |
| 891 | if not ikB8SavePreLoop |
| 892 | ret false |
| 893 | end |
| 894 | for j ulong 12 |
| 895 | l = add 6 j |
| 896 | if not ikB8RunLayer l l startPosQ false |
| 897 | ret false |
| 898 | end |
| 899 | end |
| 900 | for pass ulong 2 |
| 901 | if not ikB8StartLoopPass |
| 902 | ret false |
| 903 | end |
| 904 | for j ulong 12 |
| 905 | step = add add 18 mul pass 12 j |
| 906 | l = add 6 j |
| 907 | if not ikB8CrossPassMsgs j |
| 908 | ret false |
| 909 | end |
| 910 | if not ikB8RunLayer step l startPosQ true |
| 911 | ret false |
| 912 | end |
| 913 | end |
| 914 | end |
| 915 | for j ulong 6 |
| 916 | step = add 42 j |
| 917 | l = add 18 j |
| 918 | if not ikB8RunLayer step l startPosQ false |
| 919 | ret false |
| 920 | end |
| 921 | end |
| 922 | if not ikDBatchRun |
| 923 | ret false |
| 924 | end |
| 925 | for b ulong 8 |
| 926 | ikB8ArbFlushOne b |
| 927 | end |
| 928 | ret true |
| 929 | end |