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 / decode.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note 100% Pure Seeded-Omega Real-Weight End-to-End Inkling-2-Mini Decoder on GB10. |
| 3 | note ZERO libcuda.so.1, ZERO libc, ZERO Python, ZERO C. |
| 4 | note Composes: context.om + vector.om + launch.om + batch.om + bw_enc_sm121.om + emit_fast.om |
| 5 | note + arboris/prefetch.om + safetensors.om + tokenizer.om + decode.om. |
| 6 | |
| 7 | const IK_TMAX ulong 32768 |
| 8 | const IK_KV_LAYER_BYTES ulong 134217728 |
| 9 | const IK_KV_HALF_BYTES ulong 67108864 |
| 10 | const IK_SCONV_LAYER_BYTES ulong 79872 |
| 11 | const IK_KV_SLOT_BYTES ulong 6442450944 |
| 12 | const IK_SCONV_SLOT_BYTES ulong 3833856 |
| 13 | |
| 14 | global ikDSlot ulong 1 |
| 15 | global ikDKCode long 1 |
| 16 | global ikDKCodePos ulong 1 |
| 17 | global ikDKCodeFail uint 1 |
| 18 | global ikDKOff ulong 32 |
| 19 | global ikDKSmem uint 32 |
| 20 | global ikDKLut long 1 |
| 21 | global ikDWork long 1 |
| 22 | global ikDWorkVa ulong 1 |
| 23 | global ikDKv long 8 |
| 24 | global ikDKvVa ulong 8 |
| 25 | global ikDArg uint 32 |
| 26 | global ikDArgC ulong 1 |
| 27 | global ikDPkt long 1 |
| 28 | global ikDPktPos ulong 1 |
| 29 | global ikDSetupDone ulong 1 |
| 30 | global ikDOutBuf uchar 65536 |
| 31 | global ikDProjTmp f32 16384 |
| 32 | global ikDRLatTmp f32 384 |
| 33 | global ikDPromptIds uint 1024 |
| 34 | global ikDPromptLen ulong 1 |
| 35 | global ikDBlockCnt ulong 1 |
| 36 | global ikDPreBlockCnt ulong 1 |
| 37 | global ikDPrevBlockCnt ulong 1 |
| 38 | |
| 39 | note Live Arboris online hit-rate counters across real Inkling-2-Mini routing traces: |
| 40 | global ikDArbRes long 3072 |
| 41 | global ikDArbScores ulong 128 |
| 42 | global ikDArbPred8 uint 16 |
| 43 | global ikDArbPred16 uint 32 |
| 44 | global ikDArbHasPred ulong 1 |
| 45 | global ikDArbStats ulong 4 |
| 46 | |
| 47 | text ikDMsgBanner "=== INKLING-2-MINI 100% PURE SEEDED-OMEGA REAL-WEIGHT DECODER (ZERO CUDA) ===\n" |
| 48 | text ikDMsgLoad "Loading real checkpoint (~18.3 GB) + tokenizer into GB10 UVM...\n" |
| 49 | text ikDMsgLoaded "Loaded checkpoint in ms: " |
| 50 | text ikDMsgPrompt "Prompt tokens: " |
| 51 | text ikDMsgGen "Generated: " |
| 52 | text ikDMsgStats "\nDecode summary: tokens=" |
| 53 | text ikDMsgMs " avg_ms_per_tok=" |
| 54 | text ikDMsgArb "\nLive Arboris (real routing): transitions=" |
| 55 | text ikDMsgHit8 " top8_hits=" |
| 56 | text ikDMsgHit16 " top16_hits=" |
| 57 | text ikDMsgFail "FAIL: decoder error stage=" |
| 58 | text ikDMsgNl "\n" |
| 59 | text ikDDefDir "/home/drakestapleton/models/inkling-2-mini-ed40e7da" |
| 60 | text ikDSufSt "/model-00001-of-00001.safetensors" |
| 61 | text ikDSufTok "/tokenizer.json" |
| 62 | text ikDDefPrompt "What is the capital of France? Answer in one sentence." |
| 63 | |
| 64 | fn ikDPrint s view uchar * to void |
| 65 | w = write 1 s 0 len s |
| 66 | end |
| 67 | |
| 68 | fn ikDPrintU v ulong to void |
| 69 | b = new uchar 32 0 |
| 70 | if eq v 0 |
| 71 | put b 0 48 |
| 72 | w = write 1 b 0 1 |
| 73 | ret |
| 74 | end |
| 75 | n = ulong 0 |
| 76 | x = v |
| 77 | while gt x 0 bound 32 |
| 78 | put b n uchar add 48 rem x 10 |
| 79 | x = div x 10 |
| 80 | n = add n 1 |
| 81 | end |
| 82 | for i ulong n bound 32 |
| 83 | p = sub sub n 1 i |
| 84 | w = write 1 b p 1 |
| 85 | end |
| 86 | end |
| 87 | |
| 88 | fn ikDW off ulong to ulong |
| 89 | ret add get ikDWorkVa 0 off |
| 90 | end |
| 91 | |
| 92 | fn ikDKvVaStep step ulong to ulong |
| 93 | s = get ikDSlot 0 |
| 94 | lOff = mul step IK_KV_LAYER_BYTES |
| 95 | ret add get ikDKvVa s lOff |
| 96 | end |
| 97 | |
| 98 | fn ikDScBaseStep step ulong to ulong |
| 99 | sOff = mul get ikDSlot 0 IK_SCONV_SLOT_BYTES |
| 100 | lOff = mul step IK_SCONV_LAYER_BYTES |
| 101 | ret add 0x1800000 add sOff lOff |
| 102 | end |
| 103 | |
| 104 | fn ikDF32Bits f f32 to uint |
| 105 | ret bitcast uint f |
| 106 | end |
| 107 | |
| 108 | fn ikDBuildLut to bool |
| 109 | m = nvalloc 4096 true |
| 110 | if le m 0 |
| 111 | ret false |
| 112 | end |
| 113 | put ikDKLut 0 m |
| 114 | for i ulong 256 |
| 115 | s = and shr i 7 1 |
| 116 | e = and shr i 3 15 |
| 117 | mn = and i 7 |
| 118 | v = f32 0.0f32 |
| 119 | if eq e 15 |
| 120 | if eq mn 7 |
| 121 | v = bitcast f32 uint 0x7fc00000 |
| 122 | else |
| 123 | v = mul f32 256.0f32 add f32 1.0f32 div f32 mn f32 8.0f32 |
| 124 | end |
| 125 | else |
| 126 | if eq e 0 |
| 127 | v = mul f32 0.015625f32 div f32 mn f32 8.0f32 |
| 128 | else |
| 129 | pw = f32 1.0f32 |
| 130 | if ge e 7 |
| 131 | for k ulong sub e 7 bound 16 |
| 132 | pw = mul pw f32 2.0f32 |
| 133 | end |
| 134 | else |
| 135 | for k ulong sub 7 e bound 16 |
| 136 | pw = mul pw f32 0.5f32 |
| 137 | end |
| 138 | end |
| 139 | v = mul pw add f32 1.0f32 div f32 mn f32 8.0f32 |
| 140 | end |
| 141 | end |
| 142 | if eq s 1 |
| 143 | v = sub f32 0.0f32 v |
| 144 | end |
| 145 | r = mapset m mul i 4 4 ulong ikDF32Bits v |
| 146 | end |
| 147 | e2 = new f32 16 0.0f32 |
| 148 | put e2 0 0.0f32 |
| 149 | put e2 1 0.5f32 |
| 150 | put e2 2 1.0f32 |
| 151 | put e2 3 1.5f32 |
| 152 | put e2 4 2.0f32 |
| 153 | put e2 5 3.0f32 |
| 154 | put e2 6 4.0f32 |
| 155 | put e2 7 6.0f32 |
| 156 | put e2 8 -0.0f32 |
| 157 | put e2 9 -0.5f32 |
| 158 | put e2 10 -1.0f32 |
| 159 | put e2 11 -1.5f32 |
| 160 | put e2 12 -2.0f32 |
| 161 | put e2 13 -3.0f32 |
| 162 | put e2 14 -4.0f32 |
| 163 | put e2 15 -6.0f32 |
| 164 | for i ulong 16 |
| 165 | r = mapset m mul add 256 i 4 4 ulong ikDF32Bits get e2 i |
| 166 | end |
| 167 | ret true |
| 168 | end |
| 169 | |
| 170 | fn ikfWrite w view ulong 4 to void |
| 171 | p = get ikDKCodePos 0 |
| 172 | b = new uchar 16 0 |
| 173 | if gt get ikDKCode 0 0 |
| 174 | for i ulong 16 |
| 175 | put b i uchar and shr get w div i 4 mul rem i 4 8 255 |
| 176 | end |
| 177 | if ne mapwrite get ikDKCode 0 p b 0 16 16 |
| 178 | put ikDKCodeFail 0 1 |
| 179 | end |
| 180 | end |
| 181 | put ikDKCodePos 0 add p 16 |
| 182 | end |
| 183 | |
| 184 | fn ikDInitBatchKernels to bool |
| 185 | smB = new uint 3 0 |
| 186 | put smB 0 8192 |
| 187 | put smB 1 9280 |
| 188 | put smB 2 9280 |
| 189 | for j ulong 3 |
| 190 | idx = add 26 j |
| 191 | p = mul div add get ikDKCodePos 0 255 256 256 |
| 192 | put ikDKOff idx p |
| 193 | put ikDKCodePos 0 p |
| 194 | put ikDKSmem idx get smB j |
| 195 | put ikfPc 0 0 |
| 196 | put ikfGuard 0 0 |
| 197 | ok = ikbKernel uchar add 65 j |
| 198 | if not ok |
| 199 | ret false |
| 200 | end |
| 201 | ikfDone |
| 202 | if ne get ikDKCodeFail 0 0 |
| 203 | ret false |
| 204 | end |
| 205 | end |
| 206 | ret true |
| 207 | end |
| 208 | |
| 209 | fn ikDInitKernels to bool |
| 210 | cm = nvalloc 262144 false |
| 211 | if le cm 0 |
| 212 | ret false |
| 213 | end |
| 214 | put ikDKCode 0 cm |
| 215 | put ikDKCodePos 0 0 |
| 216 | put ikDKCodeFail 0 0 |
| 217 | sm = new uint 26 0 |
| 218 | put sm 0 18432 |
| 219 | put sm 1 12288 |
| 220 | put sm 2 12288 |
| 221 | put sm 3 6144 |
| 222 | put sm 4 512 |
| 223 | put sm 5 256 |
| 224 | put sm 6 10752 |
| 225 | put sm 7 0 |
| 226 | put sm 8 1088 |
| 227 | put sm 9 1088 |
| 228 | put sm 10 0 |
| 229 | put sm 11 0 |
| 230 | put sm 12 2048 |
| 231 | put sm 13 6144 |
| 232 | put sm 14 0 |
| 233 | put sm 15 0 |
| 234 | put sm 16 256 |
| 235 | put sm 17 9280 |
| 236 | put sm 18 0 |
| 237 | put sm 19 0 |
| 238 | put sm 20 0 |
| 239 | put sm 21 256 |
| 240 | put sm 22 64 |
| 241 | put sm 23 0 |
| 242 | put sm 24 0 |
| 243 | put sm 25 0 |
| 244 | for k ulong 26 |
| 245 | p = mul div add get ikDKCodePos 0 255 256 256 |
| 246 | put ikDKOff k p |
| 247 | put ikDKCodePos 0 p |
| 248 | put ikDKSmem k get sm k |
| 249 | put ikfPc 0 0 |
| 250 | put ikfGuard 0 0 |
| 251 | ok = ikfKernel uchar add 97 k |
| 252 | if not ok |
| 253 | ret false |
| 254 | end |
| 255 | ikfDone |
| 256 | if ne get ikDKCodeFail 0 0 |
| 257 | ret false |
| 258 | end |
| 259 | end |
| 260 | ret ikDInitBatchKernels |
| 261 | end |
| 262 | |
| 263 | fn ikDArgReset to void |
| 264 | put ikDArgC 0 0 |
| 265 | end |
| 266 | |
| 267 | fn ikDPtr va ulong to void |
| 268 | c = get ikDArgC 0 |
| 269 | put ikDArg c gblo va |
| 270 | put ikDArg add c 1 gbhi va |
| 271 | put ikDArgC 0 add c 2 |
| 272 | end |
| 273 | |
| 274 | fn ikDArgU v uint to void |
| 275 | c = get ikDArgC 0 |
| 276 | put ikDArg c v |
| 277 | put ikDArgC 0 add c 1 |
| 278 | end |
| 279 | |
| 280 | fn ikDQmd k ulong gx uint gy uint gz uint bx uint to void |
| 281 | gbqmd 1 |
| 282 | code = add mapaddr get ikDKCode 0 get ikDKOff k |
| 283 | put gbq1 32 gblo shr code 4 |
| 284 | put gbq1 33 or uint and shr code 36 2097151 20971520 |
| 285 | put gbq1 34 or shl uint 1 16 bx |
| 286 | put gbq1 35 or uint 131073 shl uint 64 8 |
| 287 | sm = get ikDKSmem k |
| 288 | if eq sm 0 |
| 289 | put gbq1 36 78923784 |
| 290 | else |
| 291 | put gbq1 36 or uint 78923776 div add sm 127 128 |
| 292 | end |
| 293 | put gbq1 37 0 |
| 294 | put gbq1 39 gx |
| 295 | put gbq1 40 gy |
| 296 | put gbq1 41 gz |
| 297 | put gbq1 59 gblo shr code 8 |
| 298 | end |
| 299 | |
| 300 | note Fast single-batch GPFIFO builder: uploads driver cb template ONCE per session, |
| 301 | note and ALWAYS emits the 20-byte GPU L2 invalidate (0x88000000) so GPU sees fresh CPU UVM writes (e.g. ikDEmbed). |
| 302 | fn ikDBatchStart to bool |
| 303 | put gbused 0 0 |
| 304 | if eq get ikDSetupDone 0 0 |
| 305 | gbsetup |
| 306 | cb = mapaddr get gbmem 1 |
| 307 | gbdriver cb |
| 308 | gbdma cb 224 |
| 309 | for i ulong 224 |
| 310 | gbemit get gbcb i |
| 311 | end |
| 312 | put ikDSetupDone 0 1 |
| 313 | end |
| 314 | gbmth 0 40 4 |
| 315 | gbemit 0 |
| 316 | gbemit 0 |
| 317 | gbemit 0 |
| 318 | gbemit 2281701376 |
| 319 | n = get gbused 0 |
| 320 | r = mapwrite get ikDPkt 0 0 gbcmd 0 n |
| 321 | if ne r long n |
| 322 | ret false |
| 323 | end |
| 324 | put ikDPktPos 0 n |
| 325 | ret true |
| 326 | end |
| 327 | |
| 328 | fn ikDBatchAdd k ulong gx uint gy uint gz uint bx uint to bool |
| 329 | ikDQmd k gx gy gz bx |
| 330 | put gbused 0 0 |
| 331 | cb = mapaddr get gbmem 1 |
| 332 | q = mapaddr get gbmem 2 |
| 333 | argc = uint get ikDArgC 0 |
| 334 | gbdma add cb 896 argc |
| 335 | for i ulong get ikDArgC 0 bound 32 |
| 336 | gbemit get ikDArg i |
| 337 | end |
| 338 | gbinline q false |
| 339 | gbdma add q 8192 1 |
| 340 | gbemit 1 |
| 341 | gbinline add q 4096 true |
| 342 | va = mapaddr get gbmem 3 |
| 343 | gbrel add va 32 6 |
| 344 | gbmth 1 540 1 |
| 345 | gbemit 4113 |
| 346 | n = get gbused 0 |
| 347 | pos = get ikDPktPos 0 |
| 348 | if gt add pos n 4192000 |
| 349 | ret false |
| 350 | end |
| 351 | r = mapwrite get ikDPkt 0 pos gbcmd 0 n |
| 352 | if ne r long n |
| 353 | ret false |
| 354 | end |
| 355 | put ikDPktPos 0 add pos n |
| 356 | ikDArgReset |
| 357 | ret true |
| 358 | end |
| 359 | |
| 360 | fn ikDBatchRun to bool |
| 361 | put gbused 0 0 |
| 362 | gbtail |
| 363 | n = get gbused 0 |
| 364 | pos = get ikDPktPos 0 |
| 365 | r = mapwrite get ikDPkt 0 pos gbcmd 0 n |
| 366 | if ne r long n |
| 367 | ret false |
| 368 | end |
| 369 | tot = add pos n |
| 370 | r = mapset get gbmem 2 8192 4 0 |
| 371 | r = mapset get gbmem 3 0 4 0 |
| 372 | r = mapset get gbmem 3 16 4 0 |
| 373 | if not nvsend get ikDPkt 0 0 uint div tot 4 |
| 374 | ret false |
| 375 | end |
| 376 | if not nvwait get gbmem 3 16 1179010630 15000000000 |
| 377 | ret false |
| 378 | end |
| 379 | ret eq mapget get gbmem 3 0 4 1145324612 |
| 380 | end |
| 381 | |
| 382 | fn ikDRunOne k ulong gx uint gy uint gz uint bx uint to bool |
| 383 | if not ikDBatchStart |
| 384 | ret false |
| 385 | end |
| 386 | if not ikDBatchAdd k gx gy gz bx |
| 387 | ret false |
| 388 | end |
| 389 | ret ikDBatchRun |
| 390 | end |
| 391 | |
| 392 | fn ikDReadF32 m long off ulong to f32 |
| 393 | ret bitcast f32 uint mapget m off 4 |
| 394 | end |
| 395 | |
| 396 | fn ikDWriteF32 m long off ulong v f32 to void |
| 397 | r = mapset m off 4 ulong bitcast uint v |
| 398 | end |
| 399 | |
| 400 | fn ikDReadBf16Buf idx ulong to f32 |
| 401 | o = mul idx 2 |
| 402 | lo = uint get ikDOutBuf o |
| 403 | hi = shl uint get ikDOutBuf add o 1 8 |
| 404 | ret bitcast f32 shl or lo hi 16 |
| 405 | end |
| 406 | |
| 407 | fn ikDEmbedWrite invRms f32 to void |
| 408 | ar = get ikStArena 0 |
| 409 | normBase = sub get ikGW 1 get ikStArenaVa 0 |
| 410 | wk = get ikDWork 0 |
| 411 | r0 = mapread ar normBase ikDOutBuf 0 9216 |
| 412 | for i ulong 2304 |
| 413 | o = mul i 4 |
| 414 | g = ikDBufF32 o |
| 415 | v = mul mul get ikDProjTmp i invRms g |
| 416 | ikDBufPutF32 o v |
| 417 | end |
| 418 | r1 = mapwrite wk 0 ikDOutBuf 0 9216 |
| 419 | end |
| 420 | |
| 421 | note CPU helper: embed lookup (BF16 -> FP32) + embed_norm (RMSNorm) into work[0x000000]. |
| 422 | fn ikDEmbed tok ulong to void |
| 423 | ar = get ikStArena 0 |
| 424 | embBase = add sub get ikGW 0 get ikStArenaVa 0 mul tok 4608 |
| 425 | r = mapread ar embBase ikDOutBuf 0 4608 |
| 426 | sq = f32 0.0f32 |
| 427 | for i ulong 2304 |
| 428 | v = ikDReadBf16Buf i |
| 429 | put ikDProjTmp i v |
| 430 | sq = add sq mul v v |
| 431 | end |
| 432 | invRms = div f32 1.0f32 fsqrt add div sq f32 2304.0f32 f32 0.000001f32 |
| 433 | ikDEmbedWrite invRms |
| 434 | end |
| 435 | |
| 436 | note Copy 2304 FP32 values inside ikDWork (9216 bytes). |
| 437 | fn ikDCopy2304 dstOff ulong srcOff ulong to void |
| 438 | wk = get ikDWork 0 |
| 439 | r = mapread wk srcOff ikDOutBuf 0 9216 |
| 440 | r = mapwrite wk dstOff ikDOutBuf 0 9216 |
| 441 | end |
| 442 | |
| 443 | fn ikDCopyBlocks dstOff ulong srcOff ulong nBlocks ulong to void |
| 444 | wk = get ikDWork 0 |
| 445 | for b ulong nBlocks bound 16 |
| 446 | r = mapread wk add srcOff mul b 9216 ikDOutBuf 0 9216 |
| 447 | r = mapwrite wk add dstOff mul b 9216 ikDOutBuf 0 9216 |
| 448 | end |
| 449 | end |
| 450 | |
| 451 | note Append GPU copy of 2304 FP32s into open GPFIFO batch (zero CPU sync). |
| 452 | fn ikDBatchCopy2304 dstOff ulong srcOff ulong to bool |
| 453 | ikDArgReset |
| 454 | ikDPtr ikDW srcOff |
| 455 | ikDPtr 0 |
| 456 | ikDPtr ikDW dstOff |
| 457 | ikDArgU 2304 |
| 458 | ret ikDBatchAdd 23 18 1 1 128 |
| 459 | end |
| 460 | |
| 461 | note Append GPU copy of nBlocks x 2304 FP32s into open GPFIFO batch. |
| 462 | fn ikDBatchCopyBlocks dstOff ulong srcOff ulong nBlocks ulong to bool |
| 463 | if eq nBlocks 0 |
| 464 | ret true |
| 465 | end |
| 466 | nFloats = mul nBlocks 2304 |
| 467 | tiles = mul nBlocks 18 |
| 468 | ikDArgReset |
| 469 | ikDPtr ikDW srcOff |
| 470 | ikDPtr 0 |
| 471 | ikDPtr ikDW dstOff |
| 472 | ikDArgU uint nFloats |
| 473 | ret ikDBatchAdd 23 uint tiles 1 1 128 |
| 474 | end |
| 475 | |
| 476 | note Append GPU vector add of 2304 FP32s (dst = a + b) into open GPFIFO batch. |
| 477 | fn ikDBatchAdd2304 dstOff ulong aOff ulong bOff ulong to bool |
| 478 | ikDArgReset |
| 479 | ikDPtr ikDW aOff |
| 480 | ikDPtr ikDW bOff |
| 481 | ikDPtr ikDW dstOff |
| 482 | ikDArgU 2304 |
| 483 | ret ikDBatchAdd 23 18 1 1 128 |
| 484 | end |
| 485 | |
| 486 | note Append GPU BF16 KV cache pack+write into open GPFIFO batch (zero CPU sync). |
| 487 | fn ikDBatchKvAppend step ulong posQ ulong to bool |
| 488 | kva = ikDKvVaStep step |
| 489 | ikDArgReset |
| 490 | ikDPtr ikDW 0x014000 |
| 491 | ikDPtr ikDW 0x016000 |
| 492 | ikDPtr kva |
| 493 | ikDPtr add kva IK_KV_HALF_BYTES |
| 494 | ikDArgU uint IK_TMAX |
| 495 | ikDArgU uint posQ |
| 496 | ret ikDBatchAdd 24 8 1 1 64 |
| 497 | end |
| 498 | |
| 499 | note Append BlockAttentionResidual into open GPFIFO batch (zero CPU sync). |
| 500 | fn ikDBatchAttnRes blkOff ulong nBlk ulong partialOff ulong qVa ulong qCnt uint outOff ulong to bool |
| 501 | if not ikDBatchCopy2304 add blkOff mul nBlk 9216 partialOff |
| 502 | ret false |
| 503 | end |
| 504 | kTot = add nBlk 1 |
| 505 | ikDArgReset |
| 506 | ikDPtr ikDW blkOff |
| 507 | ikDPtr 0 |
| 508 | ikDPtr ikDW 0x0f0000 |
| 509 | ikDArgU 2304 |
| 510 | if not ikDBatchAdd 5 uint kTot 1 1 256 |
| 511 | ret false |
| 512 | end |
| 513 | ikDPtr ikDW 0x0f0000 |
| 514 | ikDPtr qVa |
| 515 | ikDPtr ikDW outOff |
| 516 | ikDArgU uint kTot |
| 517 | ikDArgU qCnt |
| 518 | ikDArgU 2304 |
| 519 | ret ikDBatchAdd 4 qCnt 1 1 256 |
| 520 | end |
| 521 | |
| 522 | note Run BlockAttentionResidual standalone. |
| 523 | fn ikDAttnRes blkOff ulong nBlk ulong partialOff ulong qVa ulong qCnt uint outOff ulong to bool |
| 524 | if not ikDBatchStart |
| 525 | ret false |
| 526 | end |
| 527 | if not ikDBatchAttnRes blkOff nBlk partialOff qVa qCnt outOff |
| 528 | ret false |
| 529 | end |
| 530 | ret ikDBatchRun |
| 531 | end |
| 532 | |
| 533 | fn ikDReadArenaChunk va ulong nbytes ulong to void |
| 534 | for a ulong 4 |
| 535 | base = get ikStArenaVa a |
| 536 | if and ge va base lt va add base IK_ARENA_SZ |
| 537 | r = mapread get ikStArena a sub va base ikDOutBuf 0 nbytes |
| 538 | ret |
| 539 | end |
| 540 | end |
| 541 | end |
| 542 | |
| 543 | fn ikDMgUpProj wVa ulong outOff ulong to void |
| 544 | wk = get ikDWork 0 |
| 545 | for i ulong 64 |
| 546 | put ikDRLatTmp i ikDReadF32 wk add 0x1d5000 mul i 4 |
| 547 | end |
| 548 | for blk ulong 9 |
| 549 | ikDReadArenaChunk add wVa mul blk 32768 32768 |
| 550 | for r ulong 256 |
| 551 | acc = f32 0.0f32 |
| 552 | rowIdx = add mul blk 256 r |
| 553 | for c ulong 64 |
| 554 | w = ikDReadBf16Buf add mul r 64 c |
| 555 | acc = add acc mul w get ikDRLatTmp c |
| 556 | end |
| 557 | ikDWriteF32 wk add outOff mul rowIdx 4 acc |
| 558 | end |
| 559 | end |
| 560 | end |
| 561 | |
| 562 | note Append Inkling2LoopMessageGate into an open GPFIFO batch (100% GPU via bf16_gemv_k64_fast + msg_gate_fast). |
| 563 | fn ikDBatchMsgGate l ulong ctrlIdx ulong hOff ulong msgOff ulong outOff ulong to bool |
| 564 | slotBase = add 28 mul ctrlIdx 3 |
| 565 | ikDArgReset |
| 566 | ikDPtr ikDW hOff |
| 567 | ikDPtr 0 |
| 568 | ikDPtr ikDW 0x1d0000 |
| 569 | ikDArgU 2304 |
| 570 | if not ikDBatchAdd 5 1 1 1 256 |
| 571 | ret false |
| 572 | end |
| 573 | ikDPtr ikDW msgOff |
| 574 | ikDPtr 0 |
| 575 | ikDPtr ikDW 0x1d2400 |
| 576 | ikDArgU 2304 |
| 577 | if not ikDBatchAdd 5 1 1 1 256 |
| 578 | ret false |
| 579 | end |
| 580 | ikDPtr ikLva l slotBase |
| 581 | ikDPtr ikDW 0x1d0000 |
| 582 | ikDPtr ikDW 0x1d5000 |
| 583 | ikDArgU 4608 |
| 584 | ikDArgU 64 |
| 585 | if not ikDBatchAdd 0 2 1 1 256 |
| 586 | ret false |
| 587 | end |
| 588 | ikDPtr ikLva l add slotBase 1 |
| 589 | ikDPtr ikDW 0x1d5000 |
| 590 | ikDPtr ikDW 0x1d6000 |
| 591 | ikDArgU 2304 |
| 592 | if not ikDBatchAdd 21 72 1 1 256 |
| 593 | ret false |
| 594 | end |
| 595 | ikDPtr ikLva l add slotBase 2 |
| 596 | ikDPtr ikDW 0x1d5000 |
| 597 | ikDPtr ikDW 0x1d9000 |
| 598 | ikDArgU 2304 |
| 599 | if not ikDBatchAdd 21 72 1 1 256 |
| 600 | ret false |
| 601 | end |
| 602 | ikDPtr ikDW hOff |
| 603 | ikDPtr ikDW msgOff |
| 604 | ikDPtr ikDW 0x1d6000 |
| 605 | ikDPtr ikDW 0x1d9000 |
| 606 | ikDPtr ikDW outOff |
| 607 | ikDArgU 2304 |
| 608 | ret ikDBatchAdd 19 18 1 1 128 |
| 609 | end |
| 610 | |
| 611 | note Run Inkling2LoopMessageGate standalone if needed. |
| 612 | fn ikDMsgGate l ulong ctrlIdx ulong hOff ulong msgOff ulong outOff ulong to bool |
| 613 | if not ikDBatchStart |
| 614 | ret false |
| 615 | end |
| 616 | if not ikDBatchMsgGate l ctrlIdx hOff msgOff outOff |
| 617 | ret false |
| 618 | end |
| 619 | ret ikDBatchRun |
| 620 | end |
| 621 | |
| 622 | note Append cross-pass 4 messages [q, k, v, mlp] into open GPFIFO batch (zero CPU sync). |
| 623 | fn ikDBatchCrossPassMsgs loopIdx ulong to bool |
| 624 | nPrev = get ikDPrevBlockCnt 0 |
| 625 | vaQ = add get ikGW 5 mul mul loopIdx 2 9216 |
| 626 | vaKV = add get ikGW 6 mul mul loopIdx 2 9216 |
| 627 | ikDArgReset |
| 628 | ikDPtr ikDW 0x160000 |
| 629 | ikDPtr 0 |
| 630 | ikDPtr ikDW 0x190000 |
| 631 | ikDArgU 2304 |
| 632 | if not ikDBatchAdd 5 uint nPrev 1 1 256 |
| 633 | ret false |
| 634 | end |
| 635 | ikDPtr ikDW 0x190000 |
| 636 | ikDPtr vaQ |
| 637 | ikDPtr ikDW 0x1c0000 |
| 638 | ikDArgU uint nPrev |
| 639 | ikDArgU 1 |
| 640 | ikDArgU 2304 |
| 641 | if not ikDBatchAdd 4 1 1 1 256 |
| 642 | ret false |
| 643 | end |
| 644 | ikDPtr ikDW 0x190000 |
| 645 | ikDPtr add vaQ 9216 |
| 646 | ikDPtr ikDW 0x1c6c00 |
| 647 | ikDArgU uint nPrev |
| 648 | ikDArgU 1 |
| 649 | ikDArgU 2304 |
| 650 | if not ikDBatchAdd 4 1 1 1 256 |
| 651 | ret false |
| 652 | end |
| 653 | ikDPtr ikDW 0x190000 |
| 654 | ikDPtr vaKV |
| 655 | ikDPtr ikDW 0x1c2400 |
| 656 | ikDArgU uint nPrev |
| 657 | ikDArgU 2 |
| 658 | ikDArgU 2304 |
| 659 | ret ikDBatchAdd 4 2 1 1 256 |
| 660 | end |
| 661 | |
| 662 | fn ikDRelDot h ulong dMax ulong to void |
| 663 | wk = get ikDWork 0 |
| 664 | hOff = add 0x020000 mul mul h 1024 4 |
| 665 | for d ulong dMax bound 1024 |
| 666 | acc = f32 0.0f32 |
| 667 | for rr ulong 16 |
| 668 | a = get ikDRLatTmp add mul h 16 rr |
| 669 | b = get ikDProjTmp add mul rr 1024 d |
| 670 | acc = add acc mul a b |
| 671 | end |
| 672 | ikDWriteF32 wk add hOff mul d 4 acc |
| 673 | end |
| 674 | end |
| 675 | |
| 676 | note Compute rel_bias[24, 1024] on CPU from R_lat[24, 16] (FP32) @ rel_logits_proj[16, 1024] (BF16). |
| 677 | fn ikDComputeRelBias l ulong posQ ulong to void |
| 678 | wk = get ikDWork 0 |
| 679 | projBase = sub get ikLW add mul l 40 7 get ikStArenaVa 0 |
| 680 | r = mapread get ikStArena 0 projBase ikDOutBuf 0 32768 |
| 681 | for i ulong 384 |
| 682 | put ikDRLatTmp i ikDReadF32 wk add 0x01c000 mul i 4 |
| 683 | end |
| 684 | dMax = add posQ 1 |
| 685 | if gt dMax 1024 |
| 686 | dMax = ulong 1024 |
| 687 | end |
| 688 | for rr ulong 16 |
| 689 | for d ulong dMax bound 1024 |
| 690 | idx = add mul rr 1024 d |
| 691 | put ikDProjTmp idx ikDReadBf16Buf idx |
| 692 | end |
| 693 | end |
| 694 | for h ulong 24 |
| 695 | ikDRelDot h dMax |
| 696 | end |
| 697 | end |
| 698 | |
| 699 | fn ikDBufF32 off ulong to f32 |
| 700 | b0 = uint get ikDOutBuf off |
| 701 | b1 = shl uint get ikDOutBuf add off 1 8 |
| 702 | b2 = shl uint get ikDOutBuf add off 2 16 |
| 703 | b3 = shl uint get ikDOutBuf add off 3 24 |
| 704 | ret bitcast f32 or or b0 b1 or b2 b3 |
| 705 | end |
| 706 | |
| 707 | fn ikDBufPutF32 off ulong v f32 to void |
| 708 | u = bitcast uint v |
| 709 | put ikDOutBuf off uchar and u 255 |
| 710 | put ikDOutBuf add off 1 uchar and shr u 8 255 |
| 711 | put ikDOutBuf add off 2 uchar and shr u 16 255 |
| 712 | put ikDOutBuf add off 3 uchar and shr u 24 255 |
| 713 | end |
| 714 | |
| 715 | note Bulk vector add of 2304 FP32s inside ikDWork: dst = a + b (2 mapread + 1 mapwrite vs 6912 mapget/mapset). |
| 716 | fn ikDVecAdd2304 dstOff ulong aOff ulong bOff ulong to void |
| 717 | wk = get ikDWork 0 |
| 718 | r0 = mapread wk aOff ikDOutBuf 0 9216 |
| 719 | r1 = mapread wk bOff ikDOutBuf 9216 9216 |
| 720 | for i ulong 2304 |
| 721 | o = mul i 4 |
| 722 | v = add ikDBufF32 o ikDBufF32 add 9216 o |
| 723 | ikDBufPutF32 o v |
| 724 | end |
| 725 | r2 = mapwrite wk dstOff ikDOutBuf 0 9216 |
| 726 | end |
| 727 | |
| 728 | fn ikDPackKvHeadFast h ulong to void |
| 729 | hBase = mul h 512 |
| 730 | for d ulong 128 |
| 731 | srcK = add hBase mul d 4 |
| 732 | srcV = add 4096 srcK |
| 733 | o = add 8192 mul d 2 |
| 734 | put ikDOutBuf o get ikDOutBuf add srcK 2 |
| 735 | put ikDOutBuf add o 1 get ikDOutBuf add srcK 3 |
| 736 | put ikDOutBuf add o 256 get ikDOutBuf add srcV 2 |
| 737 | put ikDOutBuf add o 257 get ikDOutBuf add srcV 3 |
| 738 | end |
| 739 | end |
| 740 | |
| 741 | fn ikDWriteKvHead kv long kBase ulong posQ ulong h ulong to void |
| 742 | ikDPackKvHeadFast h |
| 743 | hOff = add mul h mul IK_TMAX 256 mul posQ 256 |
| 744 | kOff = add kBase hOff |
| 745 | vOff = add kOff IK_KV_HALF_BYTES |
| 746 | r = mapwrite kv kOff ikDOutBuf 8192 256 |
| 747 | r = mapwrite kv vOff ikDOutBuf 8448 256 |
| 748 | end |
| 749 | |
| 750 | note Bulk KV cache pack & append for all 8 heads (2 mapread + 16 mapwrite vs 2048 mapget). |
| 751 | fn ikDAppendKv step ulong posQ ulong to void |
| 752 | wk = get ikDWork 0 |
| 753 | r = mapread wk 0x014000 ikDOutBuf 0 4096 |
| 754 | r = mapread wk 0x016000 ikDOutBuf 4096 4096 |
| 755 | kv = get ikDKv get ikDSlot 0 |
| 756 | kBase = mul step IK_KV_LAYER_BYTES |
| 757 | for h ulong 8 |
| 758 | ikDWriteKvHead kv kBase posQ h |
| 759 | end |
| 760 | end |
| 761 | |
| 762 | note Live Arboris hook: score next transition from bulk-read ikDOutBuf[step*32 .. +32]. |
| 763 | fn ikDArbStepBuf step ulong l ulong to void |
| 764 | ids = new uint 8 0 |
| 765 | scores = new ulong 128 0 |
| 766 | base = mul step 32 |
| 767 | for i ulong 8 |
| 768 | o = add base mul i 4 |
| 769 | b0 = uint get ikDOutBuf o |
| 770 | b1 = shl uint get ikDOutBuf add o 1 8 |
| 771 | b2 = shl uint get ikDOutBuf add o 2 16 |
| 772 | b3 = shl uint get ikDOutBuf add o 3 24 |
| 773 | put ids i or or b0 b1 or b2 b3 |
| 774 | end |
| 775 | if eq get ikDArbHasPred 0 1 |
| 776 | put ikDArbStats 0 add get ikDArbStats 0 1 |
| 777 | for i ulong 8 |
| 778 | eid = get ids i |
| 779 | for j ulong 8 |
| 780 | if eq get ikDArbPred8 j eid |
| 781 | put ikDArbStats 1 add get ikDArbStats 1 1 |
| 782 | break |
| 783 | end |
| 784 | end |
| 785 | for j ulong 16 |
| 786 | if eq get ikDArbPred16 j eid |
| 787 | put ikDArbStats 2 add get ikDArbStats 2 1 |
| 788 | break |
| 789 | end |
| 790 | end |
| 791 | end |
| 792 | end |
| 793 | ikArbUpdate l ids |
| 794 | trans = l |
| 795 | nextL = add l 1 |
| 796 | if and eq l 17 lt step 40 |
| 797 | trans = ulong 23 |
| 798 | nextL = ulong 6 |
| 799 | end |
| 800 | if lt nextL 24 |
| 801 | ikArbScoreNext trans ids scores |
| 802 | for k ulong 16 |
| 803 | best = ulong 128 |
| 804 | bestSc = ulong 0 |
| 805 | for e ulong 128 |
| 806 | sc = get scores e |
| 807 | if gt sc bestSc |
| 808 | bestSc = sc |
| 809 | best = e |
| 810 | end |
| 811 | end |
| 812 | if lt best 128 |
| 813 | put ikDArbPred16 k uint best |
| 814 | if lt k 8 |
| 815 | put ikDArbPred8 k uint best |
| 816 | end |
| 817 | put scores best 0 |
| 818 | else |
| 819 | put ikDArbPred16 k 0xffffffff |
| 820 | if lt k 8 |
| 821 | put ikDArbPred8 k 0xffffffff |
| 822 | end |
| 823 | end |
| 824 | end |
| 825 | put ikDArbHasPred 0 1 |
| 826 | else |
| 827 | put ikDArbHasPred 0 0 |
| 828 | end |
| 829 | end |
| 830 | |
| 831 | fn ikDArbStepAll to void |
| 832 | wk = get ikDWork 0 |
| 833 | r = mapread wk 0x058800 ikDOutBuf 0 1536 |
| 834 | ikArbResetToken |
| 835 | put ikDArbHasPred 0 0 |
| 836 | for l ulong 6 |
| 837 | ikDArbStepBuf l l |
| 838 | end |
| 839 | for j ulong 12 |
| 840 | ikDArbStepBuf add 6 j add 6 j |
| 841 | end |
| 842 | for pass ulong 2 |
| 843 | for j ulong 12 |
| 844 | ikDArbStepBuf add add 18 mul pass 12 j add 6 j |
| 845 | end |
| 846 | end |
| 847 | for j ulong 6 |
| 848 | ikDArbStepBuf add 42 j add 18 j |
| 849 | end |
| 850 | end |
| 851 | |
| 852 | fn ikDSig x f32 to f32 |
| 853 | y = sub f32 0.0f32 x |
| 854 | if gt y f32 20.0f32 |
| 855 | ret f32 0.0f32 |
| 856 | end |
| 857 | if lt y f32 -20.0f32 |
| 858 | ret f32 1.0f32 |
| 859 | end |
| 860 | z = mul y f32 1.4426950408889634f32 |
| 861 | k = int z |
| 862 | if lt z f32 0.0f32 |
| 863 | k = sub k 1 |
| 864 | end |
| 865 | f = sub z f32 k |
| 866 | p = add f32 1.0f32 mul f add f32 0.69314718f32 mul f add f32 0.24022651f32 mul f add f32 0.05550411f32 mul f f32 0.00961813f32 |
| 867 | bits = shl uint add k 127 23 |
| 868 | e = mul bitcast f32 bits p |
| 869 | ret div f32 1.0f32 add f32 1.0f32 e |
| 870 | end |
| 871 | |
| 872 | fn ikDApplyGate to void |
| 873 | wk = get ikDWork 0 |
| 874 | for i ulong 3072 |
| 875 | o = mul i 4 |
| 876 | ao = ikDReadF32 wk add 0x040000 o |
| 877 | ga = ikDReadF32 wk add 0x018000 o |
| 878 | ikDWriteF32 wk add 0x040000 o mul ao ikDSig ga |
| 879 | end |
| 880 | end |
| 881 | |
| 882 | fn ikDRunLayerAttn step ulong l ulong posQ ulong hasMsgs bool to bool |
| 883 | isFull = or or eq l 5 eq l 11 or eq l 17 eq l 23 |
| 884 | if not ikDBatchCopy2304 0x04c000 0x000000 |
| 885 | ret false |
| 886 | end |
| 887 | if gt l 0 |
| 888 | qCnt = uint 1 |
| 889 | if isFull |
| 890 | qCnt = uint 3 |
| 891 | end |
| 892 | qVa = get ikAttnQOff sub l 1 |
| 893 | if not ikDBatchAttnRes 0x0c0000 get ikDBlockCnt 0 0x000000 qVa qCnt 0x0b0000 |
| 894 | ret false |
| 895 | end |
| 896 | else |
| 897 | if not ikDBatchCopy2304 0x0b0000 0x000000 |
| 898 | ret false |
| 899 | end |
| 900 | end |
| 901 | if not hasMsgs |
| 902 | if not ikDBatchCopy2304 0x000000 0x0b0000 |
| 903 | ret false |
| 904 | end |
| 905 | if isFull |
| 906 | if not ikDBatchCopy2304 0x008000 0x0b2400 |
| 907 | ret false |
| 908 | end |
| 909 | if not ikDBatchCopy2304 0x00c000 0x0b4800 |
| 910 | ret false |
| 911 | end |
| 912 | else |
| 913 | if not ikDBatchCopy2304 0x008000 0x0b0000 |
| 914 | ret false |
| 915 | end |
| 916 | if not ikDBatchCopy2304 0x00c000 0x0b0000 |
| 917 | ret false |
| 918 | end |
| 919 | end |
| 920 | else |
| 921 | if not ikDBatchMsgGate l 0 0x0b0000 0x1c0000 0x000000 |
| 922 | ret false |
| 923 | end |
| 924 | if isFull |
| 925 | if not ikDBatchMsgGate l 1 0x0b2400 0x1c2400 0x008000 |
| 926 | ret false |
| 927 | end |
| 928 | if not ikDBatchMsgGate l 2 0x0b4800 0x1c4800 0x00c000 |
| 929 | ret false |
| 930 | end |
| 931 | else |
| 932 | if not ikDBatchMsgGate l 1 0x0b0000 0x1c2400 0x008000 |
| 933 | ret false |
| 934 | end |
| 935 | if not ikDBatchMsgGate l 2 0x0b0000 0x1c4800 0x00c000 |
| 936 | ret false |
| 937 | end |
| 938 | end |
| 939 | end |
| 940 | ikDArgReset |
| 941 | ikDPtr ikDW 0x000000 |
| 942 | ikDPtr ikLva l 16 |
| 943 | ikDPtr ikDW 0x004000 |
| 944 | ikDArgU 2304 |
| 945 | if not ikDBatchAdd 5 1 1 1 256 |
| 946 | ret false |
| 947 | end |
| 948 | if isFull |
| 949 | ikDPtr ikDW 0x008000 |
| 950 | ikDPtr ikLva l 17 |
| 951 | ikDPtr ikDW 0x008000 |
| 952 | ikDArgU 2304 |
| 953 | if not ikDBatchAdd 5 1 1 1 256 |
| 954 | ret false |
| 955 | end |
| 956 | ikDPtr ikDW 0x00c000 |
| 957 | ikDPtr ikLva l 18 |
| 958 | ikDPtr ikDW 0x00c000 |
| 959 | ikDArgU 2304 |
| 960 | if not ikDBatchAdd 5 1 1 1 256 |
| 961 | ret false |
| 962 | end |
| 963 | else |
| 964 | ikDPtr ikDW 0x008000 |
| 965 | ikDPtr ikLva l 16 |
| 966 | ikDPtr ikDW 0x008000 |
| 967 | ikDArgU 2304 |
| 968 | if not ikDBatchAdd 5 1 1 1 256 |
| 969 | ret false |
| 970 | end |
| 971 | ikDPtr ikDW 0x00c000 |
| 972 | ikDPtr ikLva l 16 |
| 973 | ikDPtr ikDW 0x00c000 |
| 974 | ikDArgU 2304 |
| 975 | if not ikDBatchAdd 5 1 1 1 256 |
| 976 | ret false |
| 977 | end |
| 978 | end |
| 979 | ikDPtr ikLva l 0 |
| 980 | ikDPtr ikDW 0x004000 |
| 981 | ikDPtr ikDW 0x010000 |
| 982 | ikDArgU 2304 |
| 983 | ikDArgU 3072 |
| 984 | if not ikDBatchAdd 0 96 1 1 256 |
| 985 | ret false |
| 986 | end |
| 987 | ikDPtr ikLva l 1 |
| 988 | ikDPtr ikDW 0x008000 |
| 989 | ikDPtr ikDW 0x014000 |
| 990 | ikDArgU 2304 |
| 991 | ikDArgU 1024 |
| 992 | if not ikDBatchAdd 0 32 1 1 256 |
| 993 | ret false |
| 994 | end |
| 995 | ikDPtr ikLva l 2 |
| 996 | ikDPtr ikDW 0x00c000 |
| 997 | ikDPtr ikDW 0x016000 |
| 998 | ikDArgU 2304 |
| 999 | ikDArgU 1024 |
| 1000 | if not ikDBatchAdd 0 32 1 1 256 |
| 1001 | ret false |
| 1002 | end |
| 1003 | ikDPtr ikLva l 3 |
| 1004 | ikDPtr ikDW 0x004000 |
| 1005 | ikDPtr ikDW 0x01c000 |
| 1006 | ikDArgU 2304 |
| 1007 | ikDArgU 384 |
| 1008 | if not ikDBatchAdd 0 12 1 1 256 |
| 1009 | ret false |
| 1010 | end |
| 1011 | ikDPtr ikLva l 5 |
| 1012 | ikDPtr ikDW 0x004000 |
| 1013 | ikDPtr ikDW 0x018000 |
| 1014 | ikDArgU 2304 |
| 1015 | ikDArgU 3072 |
| 1016 | if not ikDBatchAdd 0 96 1 1 256 |
| 1017 | ret false |
| 1018 | end |
| 1019 | ikDPtr ikLva l 6 |
| 1020 | ikDPtr ikDW 0x004000 |
| 1021 | ikDPtr ikDW 0x01d000 |
| 1022 | ikDArgU 2304 |
| 1023 | ikDArgU 24 |
| 1024 | if not ikDBatchAdd 0 1 1 1 256 |
| 1025 | ret false |
| 1026 | end |
| 1027 | scBase = ikDScBaseStep step |
| 1028 | if isFull |
| 1029 | ikDPtr ikLva l 12 |
| 1030 | ikDPtr ikDW 0x008000 |
| 1031 | ikDPtr ikDW 0x01e000 |
| 1032 | ikDArgU 2304 |
| 1033 | ikDArgU 32 |
| 1034 | if not ikDBatchAdd 0 1 1 1 256 |
| 1035 | ret false |
| 1036 | end |
| 1037 | ikDPtr ikLva l 13 |
| 1038 | ikDPtr ikDW 0x00c000 |
| 1039 | ikDPtr ikDW 0x01e100 |
| 1040 | ikDArgU 2304 |
| 1041 | ikDArgU 32 |
| 1042 | if not ikDBatchAdd 0 1 1 1 256 |
| 1043 | ret false |
| 1044 | end |
| 1045 | ikDPtr ikDW 0x014000 |
| 1046 | ikDPtr ikDW scBase |
| 1047 | ikDPtr ikLva l 10 |
| 1048 | ikDPtr ikDW 0x01e000 |
| 1049 | ikDPtr ikDW 0x014000 |
| 1050 | ikDArgU 1024 |
| 1051 | ikDArgU 7 |
| 1052 | if not ikDBatchAdd 18 8 1 1 128 |
| 1053 | ret false |
| 1054 | end |
| 1055 | ikDPtr ikDW 0x016000 |
| 1056 | ikDPtr ikDW add scBase 12288 |
| 1057 | ikDPtr ikLva l 11 |
| 1058 | ikDPtr ikDW 0x01e100 |
| 1059 | ikDPtr ikDW 0x016000 |
| 1060 | ikDArgU 1024 |
| 1061 | ikDArgU 7 |
| 1062 | if not ikDBatchAdd 18 8 1 1 128 |
| 1063 | ret false |
| 1064 | end |
| 1065 | else |
| 1066 | ikDPtr ikDW 0x014000 |
| 1067 | ikDPtr ikDW scBase |
| 1068 | ikDPtr ikLva l 10 |
| 1069 | ikDPtr ikDW 0x014000 |
| 1070 | ikDArgU 1024 |
| 1071 | if not ikDBatchAdd 11 8 1 1 128 |
| 1072 | ret false |
| 1073 | end |
| 1074 | ikDPtr ikDW 0x016000 |
| 1075 | ikDPtr ikDW add scBase 12288 |
| 1076 | ikDPtr ikLva l 11 |
| 1077 | ikDPtr ikDW 0x016000 |
| 1078 | ikDArgU 1024 |
| 1079 | if not ikDBatchAdd 11 8 1 1 128 |
| 1080 | ret false |
| 1081 | end |
| 1082 | end |
| 1083 | ikDPtr ikDW 0x010000 |
| 1084 | ikDPtr ikLva l 8 |
| 1085 | ikDPtr ikDW 0x1e0000 |
| 1086 | ikDPtr ikDW 0x010000 |
| 1087 | ikDArgU 24 |
| 1088 | ikDArgU 128 |
| 1089 | ikDArgU ikDF32Bits 22.627417f32 |
| 1090 | if isFull |
| 1091 | ikDArgU 3 |
| 1092 | else |
| 1093 | ikDArgU 1 |
| 1094 | end |
| 1095 | if not ikDBatchAdd 7 24 1 1 32 |
| 1096 | ret false |
| 1097 | end |
| 1098 | ikDPtr ikDW 0x014000 |
| 1099 | ikDPtr 0 |
| 1100 | ikDPtr 0 |
| 1101 | ikDPtr ikDW 0x014000 |
| 1102 | ikDArgU 8 |
| 1103 | ikDArgU 128 |
| 1104 | ikDArgU ikDF32Bits 22.627417f32 |
| 1105 | ikDArgU 0 |
| 1106 | if not ikDBatchAdd 7 8 1 1 32 |
| 1107 | ret false |
| 1108 | end |
| 1109 | ikDPtr ikDW 0x016000 |
| 1110 | ikDPtr 0 |
| 1111 | ikDPtr ikDW 0x016000 |
| 1112 | ikDArgU 128 |
| 1113 | if not ikDBatchAdd 5 8 1 1 256 |
| 1114 | ret false |
| 1115 | end |
| 1116 | ikDPtr ikDW 0x01c000 |
| 1117 | ikDPtr ikLva l 9 |
| 1118 | ikDPtr ikDW 0x1e0000 |
| 1119 | ikDPtr ikDW 0x01c000 |
| 1120 | ikDArgU 24 |
| 1121 | ikDArgU 16 |
| 1122 | ikDArgU ikDF32Bits 8.0f32 |
| 1123 | if isFull |
| 1124 | ikDArgU 3 |
| 1125 | else |
| 1126 | ikDArgU 1 |
| 1127 | end |
| 1128 | if not ikDBatchAdd 7 24 1 1 32 |
| 1129 | ret false |
| 1130 | end |
| 1131 | dMaxRel = add posQ 1 |
| 1132 | if gt dMaxRel 1024 |
| 1133 | dMaxRel = ulong 1024 |
| 1134 | end |
| 1135 | relTiles = div add dMaxRel 127 128 |
| 1136 | ikDPtr ikDW 0x01c000 |
| 1137 | ikDPtr ikLva l 7 |
| 1138 | ikDPtr ikDW 0x020000 |
| 1139 | ikDArgU uint dMaxRel |
| 1140 | if not ikDBatchAdd 22 uint relTiles 24 1 128 |
| 1141 | ret false |
| 1142 | end |
| 1143 | if not ikDBatchKvAppend step posQ |
| 1144 | ret false |
| 1145 | end |
| 1146 | kva = ikDKvVaStep step |
| 1147 | splitS = uint 1 |
| 1148 | if ge posQ 64 |
| 1149 | splitS = uint 4 |
| 1150 | end |
| 1151 | if ge posQ 512 |
| 1152 | splitS = uint 8 |
| 1153 | end |
| 1154 | ikDArgReset |
| 1155 | ikDPtr ikDW 0x010000 |
| 1156 | ikDPtr kva |
| 1157 | ikDPtr add kva IK_KV_HALF_BYTES |
| 1158 | ikDPtr ikDW 0x020000 |
| 1159 | ikDPtr ikDW 0x1e2000 |
| 1160 | ikDPtr ikDW 0x1e4000 |
| 1161 | ikDArgU 24 |
| 1162 | ikDArgU 8 |
| 1163 | ikDArgU uint IK_TMAX |
| 1164 | ikDArgU 1024 |
| 1165 | ikDArgU uint posQ |
| 1166 | if isFull |
| 1167 | ikDArgU 0 |
| 1168 | else |
| 1169 | ikDArgU 1024 |
| 1170 | end |
| 1171 | ikDArgU splitS |
| 1172 | if not ikDBatchAdd 13 24 splitS 1 256 |
| 1173 | ret false |
| 1174 | end |
| 1175 | ikDPtr ikDW 0x1e2000 |
| 1176 | ikDPtr ikDW 0x1e4000 |
| 1177 | ikDPtr ikDW 0x01d000 |
| 1178 | ikDPtr ikDW 0x018000 |
| 1179 | ikDPtr ikDW 0x040000 |
| 1180 | ikDArgU 24 |
| 1181 | ikDArgU splitS |
| 1182 | if not ikDBatchAdd 14 24 1 1 32 |
| 1183 | ret false |
| 1184 | end |
| 1185 | ikDPtr ikLva l 4 |
| 1186 | ikDPtr ikDW 0x040000 |
| 1187 | ikDPtr ikDW 0x044000 |
| 1188 | ikDArgU 3072 |
| 1189 | ikDArgU 2304 |
| 1190 | if not ikDBatchAdd 0 72 1 1 256 |
| 1191 | ret false |
| 1192 | end |
| 1193 | ikDPtr ikDW 0x044000 |
| 1194 | ikDPtr ikDW add scBase 24576 |
| 1195 | ikDPtr ikLva l 14 |
| 1196 | ikDPtr ikDW 0x048000 |
| 1197 | ikDArgU 2304 |
| 1198 | ret ikDBatchAdd 11 18 1 1 128 |
| 1199 | end |
| 1200 | |
| 1201 | fn ikDRunLayerMlp step ulong l ulong hasMsgs bool to bool |
| 1202 | isBoundary = eq rem l 6 0 |
| 1203 | if isBoundary |
| 1204 | bc = get ikDBlockCnt 0 |
| 1205 | if not ikDBatchCopy2304 add 0x0c0000 mul bc 9216 0x04c000 |
| 1206 | ret false |
| 1207 | end |
| 1208 | put ikDBlockCnt 0 add bc 1 |
| 1209 | if not ikDBatchCopy2304 0x04c000 0x048000 |
| 1210 | ret false |
| 1211 | end |
| 1212 | else |
| 1213 | if not ikDBatchAdd2304 0x04c000 0x04c000 0x048000 |
| 1214 | ret false |
| 1215 | end |
| 1216 | end |
| 1217 | mqVa = get ikMlpQOff l |
| 1218 | if not ikDBatchAttnRes 0x0c0000 get ikDBlockCnt 0 0x04c000 mqVa 1 0x050000 |
| 1219 | ret false |
| 1220 | end |
| 1221 | lutva = mapaddr get ikDKLut 0 |
| 1222 | idVa = ikDW add 0x058800 mul step 32 |
| 1223 | if hasMsgs |
| 1224 | if not ikDBatchMsgGate l 3 0x050000 0x1c6c00 0x050000 |
| 1225 | ret false |
| 1226 | end |
| 1227 | end |
| 1228 | ikDArgReset |
| 1229 | ikDPtr ikDW 0x050000 |
| 1230 | ikDPtr ikLva l 19 |
| 1231 | ikDPtr ikDW 0x054000 |
| 1232 | ikDArgU 2304 |
| 1233 | if not ikDBatchAdd 5 1 1 1 256 |
| 1234 | ret false |
| 1235 | end |
| 1236 | ikDPtr ikLva l 20 |
| 1237 | ikDPtr ikDW 0x054000 |
| 1238 | ikDPtr ikDW 0x058000 |
| 1239 | ikDArgU 2304 |
| 1240 | ikDArgU 128 |
| 1241 | if not ikDBatchAdd 0 4 1 1 256 |
| 1242 | ret false |
| 1243 | end |
| 1244 | ikDPtr ikDW 0x058000 |
| 1245 | ikDPtr ikLva l 21 |
| 1246 | ikDPtr idVa |
| 1247 | ikDPtr ikDW 0x058500 |
| 1248 | if not ikDBatchAdd 12 1 1 1 32 |
| 1249 | ret false |
| 1250 | end |
| 1251 | if lt l 20 |
| 1252 | ikDPtr ikDW 0x054000 |
| 1253 | ikDPtr lutva |
| 1254 | ikDPtr ikDW 0x059000 |
| 1255 | ikDPtr ikDW 0x05a000 |
| 1256 | ikDPtr ikDW 0x05e000 |
| 1257 | ikDPtr ikDW 0x060000 |
| 1258 | ikDArgU 144 |
| 1259 | if not ikDBatchAdd 8 2 1 1 128 |
| 1260 | ret false |
| 1261 | end |
| 1262 | ikDPtr ikDW 0x060000 |
| 1263 | ikDPtr ikLva l 22 |
| 1264 | ikDPtr ikLva l 23 |
| 1265 | ikDPtr idVa |
| 1266 | ikDPtr ikLva l 24 |
| 1267 | ikDPtr lutva |
| 1268 | ikDPtr ikDW 0x070000 |
| 1269 | ikDArgU 2304 |
| 1270 | ikDArgU 2304 |
| 1271 | ikDArgU 0 |
| 1272 | if not ikDBatchAdd 1 72 8 1 256 |
| 1273 | ret false |
| 1274 | end |
| 1275 | ikDPtr ikDW 0x070000 |
| 1276 | ikDPtr ikDW 0x058500 |
| 1277 | ikDPtr lutva |
| 1278 | ikDPtr ikDW 0x059000 |
| 1279 | ikDPtr ikDW 0x05a000 |
| 1280 | ikDPtr ikDW 0x05e000 |
| 1281 | ikDPtr ikDW 0x060000 |
| 1282 | ikDArgU 1152 |
| 1283 | if not ikDBatchAdd 17 1 8 1 128 |
| 1284 | ret false |
| 1285 | end |
| 1286 | ikDPtr ikDW 0x060000 |
| 1287 | ikDPtr ikLva l 25 |
| 1288 | ikDPtr ikLva l 26 |
| 1289 | ikDPtr idVa |
| 1290 | ikDPtr ikLva l 27 |
| 1291 | ikDPtr lutva |
| 1292 | ikDPtr ikDW 0x090000 |
| 1293 | ikDArgU 1152 |
| 1294 | ikDArgU 2304 |
| 1295 | ikDArgU 1152 |
| 1296 | if not ikDBatchAdd 1 72 8 1 256 |
| 1297 | ret false |
| 1298 | end |
| 1299 | else |
| 1300 | ikDPtr ikDW 0x054000 |
| 1301 | ikDPtr lutva |
| 1302 | ikDPtr ikDW 0x05a000 |
| 1303 | ikDPtr ikDW 0x05e000 |
| 1304 | ikDPtr ikDW 0x060000 |
| 1305 | ikDArgU 72 |
| 1306 | if not ikDBatchAdd 9 1 1 1 128 |
| 1307 | ret false |
| 1308 | end |
| 1309 | ikDPtr ikDW 0x060000 |
| 1310 | ikDPtr ikLva l 22 |
| 1311 | ikDPtr ikLva l 23 |
| 1312 | ikDPtr idVa |
| 1313 | ikDPtr lutva |
| 1314 | ikDPtr ikDW 0x070000 |
| 1315 | ikDArgU 2304 |
| 1316 | ikDArgU 2304 |
| 1317 | ikDArgU 0 |
| 1318 | if not ikDBatchAdd 2 72 8 1 256 |
| 1319 | ret false |
| 1320 | end |
| 1321 | ikDPtr ikDW 0x070000 |
| 1322 | ikDPtr ikDW 0x058500 |
| 1323 | ikDPtr ikDW 0x084000 |
| 1324 | ikDPtr 0 |
| 1325 | ikDArgU 1152 |
| 1326 | if not ikDBatchAdd 10 9 8 1 128 |
| 1327 | ret false |
| 1328 | end |
| 1329 | ikDPtr ikDW 0x084000 |
| 1330 | ikDPtr lutva |
| 1331 | ikDPtr ikDW 0x05a000 |
| 1332 | ikDPtr ikDW 0x05e000 |
| 1333 | ikDPtr ikDW 0x060000 |
| 1334 | ikDArgU 288 |
| 1335 | if not ikDBatchAdd 9 3 1 1 128 |
| 1336 | ret false |
| 1337 | end |
| 1338 | ikDPtr ikDW 0x060000 |
| 1339 | ikDPtr ikLva l 25 |
| 1340 | ikDPtr ikLva l 26 |
| 1341 | ikDPtr idVa |
| 1342 | ikDPtr lutva |
| 1343 | ikDPtr ikDW 0x090000 |
| 1344 | ikDArgU 1152 |
| 1345 | ikDArgU 2304 |
| 1346 | ikDArgU 1152 |
| 1347 | if not ikDBatchAdd 2 72 8 1 256 |
| 1348 | ret false |
| 1349 | end |
| 1350 | end |
| 1351 | scBase = ikDScBaseStep step |
| 1352 | ikDPtr ikDW 0x090000 |
| 1353 | ikDPtr ikDW add scBase 52224 |
| 1354 | ikDPtr ikLva l 15 |
| 1355 | ikDPtr ikDW 0x0a4000 |
| 1356 | ikDArgU 2304 |
| 1357 | if not ikDBatchAdd 15 18 1 1 128 |
| 1358 | ret false |
| 1359 | end |
| 1360 | ret ikDBatchAdd2304 0x000000 0x04c000 0x0a4000 |
| 1361 | end |
| 1362 | |
| 1363 | fn ikDArgmax200k to long |
| 1364 | wk = get ikDWork 0 |
| 1365 | bestId = ulong 0 |
| 1366 | bestVal = f32 -1e30f32 |
| 1367 | vBase = ulong 0 |
| 1368 | while lt vBase 200058 bound 16 |
| 1369 | cnt = sub 200058 vBase |
| 1370 | if gt cnt 16384 |
| 1371 | cnt = ulong 16384 |
| 1372 | end |
| 1373 | off = add 0x300000 mul vBase 4 |
| 1374 | r = mapread wk off ikDOutBuf 0 mul cnt 4 |
| 1375 | for i ulong cnt bound 16384 |
| 1376 | val = ikDBufF32 mul i 4 |
| 1377 | if gt val bestVal |
| 1378 | bestVal = val |
| 1379 | bestId = add vBase i |
| 1380 | end |
| 1381 | end |
| 1382 | vBase = add vBase cnt |
| 1383 | end |
| 1384 | ret long bestId |
| 1385 | end |
| 1386 | |
| 1387 | note Run all 48 logical layers + optional 201k unembed in ONE SINGLE-SUBMIT GPFIFO (zero mid-forward CPU syncs!). |
| 1388 | fn ikDForwardToken tok ulong posQ ulong needLogits bool to long |
| 1389 | ikDEmbed tok |
| 1390 | put ikDBlockCnt 0 0 |
| 1391 | if not ikDBatchStart |
| 1392 | ret -1 |
| 1393 | end |
| 1394 | for l ulong 6 |
| 1395 | if not ikDRunLayerAttn l l posQ false |
| 1396 | ret -1 |
| 1397 | end |
| 1398 | if not ikDRunLayerMlp l l false |
| 1399 | ret -2 |
| 1400 | end |
| 1401 | end |
| 1402 | if not ikDBatchCopy2304 0x150000 0x000000 |
| 1403 | ret -3 |
| 1404 | end |
| 1405 | if not ikDBatchCopyBlocks 0x120000 0x0c0000 get ikDBlockCnt 0 |
| 1406 | ret -3 |
| 1407 | end |
| 1408 | put ikDPreBlockCnt 0 get ikDBlockCnt 0 |
| 1409 | for j ulong 12 |
| 1410 | l = add 6 j |
| 1411 | if not ikDRunLayerAttn l l posQ false |
| 1412 | ret -3 |
| 1413 | end |
| 1414 | if not ikDRunLayerMlp l l false |
| 1415 | ret -4 |
| 1416 | end |
| 1417 | end |
| 1418 | for pass ulong 2 |
| 1419 | preN = get ikDPreBlockCnt 0 |
| 1420 | curN = get ikDBlockCnt 0 |
| 1421 | nPrev = sub curN add preN 1 |
| 1422 | if not ikDBatchCopyBlocks 0x160000 add 0x0c0000 mul add preN 1 9216 nPrev |
| 1423 | ret -5 |
| 1424 | end |
| 1425 | if not ikDBatchCopy2304 add 0x160000 mul nPrev 9216 0x000000 |
| 1426 | ret -5 |
| 1427 | end |
| 1428 | put ikDPrevBlockCnt 0 add nPrev 1 |
| 1429 | if not ikDBatchCopy2304 0x000000 0x150000 |
| 1430 | ret -5 |
| 1431 | end |
| 1432 | if not ikDBatchCopyBlocks 0x0c0000 0x120000 preN |
| 1433 | ret -5 |
| 1434 | end |
| 1435 | put ikDBlockCnt 0 preN |
| 1436 | for j ulong 12 |
| 1437 | step = add add 18 mul pass 12 j |
| 1438 | l = add 6 j |
| 1439 | if not ikDBatchCrossPassMsgs j |
| 1440 | ret -5 |
| 1441 | end |
| 1442 | if not ikDRunLayerAttn step l posQ true |
| 1443 | ret -6 |
| 1444 | end |
| 1445 | if not ikDRunLayerMlp step l true |
| 1446 | ret -7 |
| 1447 | end |
| 1448 | end |
| 1449 | end |
| 1450 | for j ulong 6 |
| 1451 | step = add 42 j |
| 1452 | l = add 18 j |
| 1453 | if not ikDRunLayerAttn step l posQ false |
| 1454 | ret -8 |
| 1455 | end |
| 1456 | if not ikDRunLayerMlp step l false |
| 1457 | ret -9 |
| 1458 | end |
| 1459 | end |
| 1460 | if needLogits |
| 1461 | if not ikDBatchAttnRes 0x0c0000 get ikDBlockCnt 0 0x000000 ikGva 4 1 0x0b0000 |
| 1462 | ret -10 |
| 1463 | end |
| 1464 | ikDArgReset |
| 1465 | ikDPtr ikDW 0x0b0000 |
| 1466 | ikDPtr ikGva 2 |
| 1467 | ikDPtr ikDW 0x004000 |
| 1468 | ikDArgU 2304 |
| 1469 | if not ikDBatchAdd 5 1 1 1 256 |
| 1470 | ret -11 |
| 1471 | end |
| 1472 | ikDPtr ikGva 3 |
| 1473 | ikDPtr ikDW 0x004000 |
| 1474 | ikDPtr ikDW 0x200000 |
| 1475 | ikDArgU 2304 |
| 1476 | ikDArgU 201024 |
| 1477 | if not ikDBatchAdd 0 6282 1 1 256 |
| 1478 | ret -12 |
| 1479 | end |
| 1480 | ikDPtr ikDW 0x200000 |
| 1481 | ikDPtr ikDW 0x300000 |
| 1482 | ikDArgU 201024 |
| 1483 | ikDArgU ikDF32Bits 30.0f32 |
| 1484 | if not ikDBatchAdd 20 1571 1 1 128 |
| 1485 | ret -13 |
| 1486 | end |
| 1487 | end |
| 1488 | if not ikDBatchRun |
| 1489 | ret -14 |
| 1490 | end |
| 1491 | ikDArbStepAll |
| 1492 | if not needLogits |
| 1493 | ret 0 |
| 1494 | end |
| 1495 | ret ikDArgmax200k |
| 1496 | end |
| 1497 | |
| 1498 | fn ikDJoinPath dir view uchar * dlen ulong suf view uchar * dst edit uchar 512 to ulong |
| 1499 | p = ulong 0 |
| 1500 | for i ulong dlen bound 256 |
| 1501 | put dst p get dir i |
| 1502 | p = add p 1 |
| 1503 | end |
| 1504 | for i ulong len suf bound 128 |
| 1505 | put dst p get suf i |
| 1506 | p = add p 1 |
| 1507 | end |
| 1508 | ret p |
| 1509 | end |
| 1510 | |
| 1511 | fn ikDResetSlot slot ulong to void |
| 1512 | wk = get ikDWork 0 |
| 1513 | for i ulong 65536 |
| 1514 | put ikDOutBuf i 0 |
| 1515 | end |
| 1516 | base = add 0x1800000 mul slot IK_SCONV_SLOT_BYTES |
| 1517 | done = ulong 0 |
| 1518 | while lt done IK_SCONV_SLOT_BYTES bound 128 |
| 1519 | chunk = sub IK_SCONV_SLOT_BYTES done |
| 1520 | if gt chunk 65536 |
| 1521 | chunk = ulong 65536 |
| 1522 | end |
| 1523 | r = mapwrite wk add base done ikDOutBuf 0 chunk |
| 1524 | done = add done chunk |
| 1525 | end |
| 1526 | end |
| 1527 | |
| 1528 | fn ikDInitGpu nSlots ulong to int |
| 1529 | if not gbopen 1 |
| 1530 | ret 10 |
| 1531 | end |
| 1532 | if not gbbopen |
| 1533 | ret 11 |
| 1534 | end |
| 1535 | pkt = nvalloc 4194304 false |
| 1536 | if le pkt 0 |
| 1537 | ret 12 |
| 1538 | end |
| 1539 | put ikDPkt 0 pkt |
| 1540 | put ikDSetupDone 0 0 |
| 1541 | if not ikDBuildLut |
| 1542 | ret 13 |
| 1543 | end |
| 1544 | if not ikDInitKernels |
| 1545 | ret 14 |
| 1546 | end |
| 1547 | wk = nvalloc add 25165824 mul nSlots IK_SCONV_SLOT_BYTES true |
| 1548 | if le wk 0 |
| 1549 | ret 15 |
| 1550 | end |
| 1551 | put ikDWork 0 wk |
| 1552 | put ikDWorkVa 0 mapaddr wk |
| 1553 | ikDWriteF32 wk 0x059000 0.05f32 |
| 1554 | for b ulong 8 |
| 1555 | ikDWriteF32 wk add 0x1e0000 mul b 4 1.0f32 |
| 1556 | end |
| 1557 | for s ulong nSlots bound 8 |
| 1558 | kv = nvalloc IK_KV_SLOT_BYTES true |
| 1559 | if le kv 0 |
| 1560 | ret 15 |
| 1561 | end |
| 1562 | put ikDKv s kv |
| 1563 | put ikDKvVa s mapaddr kv |
| 1564 | end |
| 1565 | put ikDSlot 0 0 |
| 1566 | ret 0 |
| 1567 | end |
| 1568 | |
| 1569 | fn ikDInitModel nSlots ulong to int |
| 1570 | dir = new uchar 256 0 |
| 1571 | arg1 = new uchar 256 0 |
| 1572 | pSt = new uchar 512 0 |
| 1573 | pTok = new uchar 512 0 |
| 1574 | dlen = len ikDDefDir |
| 1575 | for i ulong dlen bound 256 |
| 1576 | put dir i get ikDDefDir i |
| 1577 | end |
| 1578 | if ge argcnt 2 |
| 1579 | n = argread 1 arg1 0 256 |
| 1580 | if gt n 0 |
| 1581 | if ne get arg1 0 45 |
| 1582 | dlen = ulong n |
| 1583 | for i ulong dlen bound 256 |
| 1584 | put dir i get arg1 i |
| 1585 | end |
| 1586 | end |
| 1587 | end |
| 1588 | end |
| 1589 | lSt = ikDJoinPath dir dlen ikDSufSt pSt |
| 1590 | lTok = ikDJoinPath dir dlen ikDSufTok pTok |
| 1591 | rc = ikDInitGpu nSlots |
| 1592 | if ne rc 0 |
| 1593 | ret rc |
| 1594 | end |
| 1595 | if not ikStLoadAll pSt lSt |
| 1596 | ret 16 |
| 1597 | end |
| 1598 | if not ikTokLoad pTok lTok |
| 1599 | ret 17 |
| 1600 | end |
| 1601 | ret 0 |
| 1602 | end |
| 1603 | |
| 1604 | fn ikDInitAll to int |
| 1605 | ret ikDInitModel 1 |
| 1606 | end |
| 1607 | |
| 1608 | fn ikDForwardSlot slot ulong tok ulong posQ ulong needLogits bool to long |
| 1609 | put ikDSlot 0 slot |
| 1610 | ret ikDForwardToken tok posQ needLogits |
| 1611 | end |
| 1612 | |
| 1613 | fn ikDRunPrompt to int |
| 1614 | pBuf = new uchar 1024 0 |
| 1615 | pIds = new uint 1024 0 |
| 1616 | pLen = len ikDDefPrompt |
| 1617 | for i ulong pLen bound 1024 |
| 1618 | put pBuf i get ikDDefPrompt i |
| 1619 | end |
| 1620 | if ge argcnt 3 |
| 1621 | n = argread 2 pBuf 0 1024 |
| 1622 | if gt n 0 |
| 1623 | pLen = ulong n |
| 1624 | end |
| 1625 | end |
| 1626 | nTok = ikTokEncodeChat pBuf pLen pIds |
| 1627 | put ikDPromptLen 0 nTok |
| 1628 | ikDPrint ikDMsgPrompt |
| 1629 | ikDPrintU nTok |
| 1630 | ikDPrint ikDMsgNl |
| 1631 | posQ = ulong 0 |
| 1632 | nextTok = long 0 |
| 1633 | for i ulong nTok bound 1024 |
| 1634 | needL = eq add i 1 nTok |
| 1635 | nextTok = ikDForwardToken ulong get pIds i posQ needL |
| 1636 | if lt nextTok 0 |
| 1637 | ikDPrint ikDMsgFail |
| 1638 | ikDPrintU ulong sub 0 nextTok |
| 1639 | ikDPrint ikDMsgNl |
| 1640 | ret 20 |
| 1641 | end |
| 1642 | posQ = add posQ 1 |
| 1643 | end |
| 1644 | ikDPrint ikDMsgGen |
| 1645 | ikTokPrint ulong nextTok |
| 1646 | tGen0 = monons |
| 1647 | genCnt = ulong 1 |
| 1648 | for g ulong 31 bound 64 |
| 1649 | if or eq nextTok 200006 eq nextTok 200010 |
| 1650 | break |
| 1651 | end |
| 1652 | nextTok = ikDForwardToken ulong nextTok posQ true |
| 1653 | if lt nextTok 0 |
| 1654 | ikDPrint ikDMsgFail |
| 1655 | ikDPrintU ulong sub 0 nextTok |
| 1656 | ikDPrint ikDMsgNl |
| 1657 | ret 21 |
| 1658 | end |
| 1659 | ikTokPrint ulong nextTok |
| 1660 | posQ = add posQ 1 |
| 1661 | genCnt = add genCnt 1 |
| 1662 | end |
| 1663 | tGen1 = monons |
| 1664 | ikDPrint ikDMsgStats |
| 1665 | ikDPrintU genCnt |
| 1666 | ikDPrint ikDMsgMs |
| 1667 | if gt genCnt 1 |
| 1668 | ikDPrintU div div ulong sub tGen1 tGen0 sub genCnt 1 1000000 |
| 1669 | else |
| 1670 | ikDPrintU 0 |
| 1671 | end |
| 1672 | ret 0 |
| 1673 | end |