interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / gpu / engine / runtime.om

1672 lines · 36421 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note om/gpu/engine/runtime.om: Universal Pure Seeded-OM-2 Zero-CUDA GPU Runtime for ANY Dense/Hybrid Model.
3note Dynamically sizes UVM workspaces, loads tensors by canonical name from any safetensors layout,
4note quantizes BF16 or FP8 weights to W4 block-32 on GPU, specializes SM121 SASS kernels via emit_generic.om,
5note and records GPFIFO packets for 128K-context decode & OpenAI HTTP serving.
6
7const ENG_MAX_LAYERS ulong 128
8const ENG_WORK_BYTES ulong 92274688
9const ENG_SCRATCH_BYTES ulong 402653184
10
11note Per-layer device pointers (VA in UVM) for up to 128 layers
12global engLInNormVa ulong 128
13global engLPostNormVa ulong 128
14global engLA1WVa ulong 128
15global engLA1SVa ulong 128
16global engLA2WVa ulong 128
17global engLA2SVa ulong 128
18global engLA3WVa ulong 128
19global engLA3SVa ulong 128
20global engLA4WVa ulong 128
21global engLA4SVa ulong 128
22global engLQNormVa ulong 128
23global engLKNormVa ulong 128
24global engLGateWVa ulong 128
25global engLGateSVa ulong 128
26global engLUpWVa ulong 128
27global engLUpSVa ulong 128
28global engLDnWVa ulong 128
29global engLDnSVa ulong 128
30
31note Dynamic UVM slab allocator (up to 48 slabs of 2 GiB each = 96 GiB W4 capacity!)
32const ENG_SLAB_SZ ulong 2147483648
33global engSlabMap long 48
34global engSlabVa ulong 48
35global engSlabUsed ulong 48
36global engSlabCnt ulong 1
37
38note Decode workspace slot offsets inside engWork (32 MB)
39const EW_X ulong 0
40const EW_XN ulong 65536
41const EW_QKV ulong 131072
42const EW_K ulong 393216
43const EW_V ulong 524288
44const EW_Z ulong 655360
45const EW_Y ulong 917504
46const EW_AOUT ulong 1048576
47const EW_AB ulong 1114112
48const EW_GU ulong 1179648
49const EW_SW ulong 1703936
50const EW_FNORM ulong 1966080
51const EW_POS ulong 2031616
52const EW_OUT_TOK ulong 2035712
53const EW_LOGW4 ulong 2039808
54const EW_CIDS ulong 3145728
55const EW_CLOG ulong 3162112
56const EW_QROT ulong 3178496
57const EW_OPART ulong 3309568
58const EW_MLPART ulong 3833856
59const EW_PKT ulong 4194304
60
61global engCodePos ulong 1
62global engCodeMap long 1
63global engCodeFail uint 1
64global engEntry ulong 64
65global engSmem ulong 64
66global engBind uint 32
67global engBindN ulong 1
68global engPktOff ulong 1
69global engPktPos ulong 1
70global engSlotOff ulong 18
71global engSlotLen ulong 18
72global engUseStrictSync ulong 1
73global engUseNvfp4 ulong 1
74
75global engWork long 1
76global engWorkVa ulong 1
77global engScratch long 1
78global engScratchVa ulong 1
79global engLut long 1
80global engLmBf16 long 1
81global engEmbBf16 long 1
82global engLmW4 long 1
83global engLmW4Va ulong 1
84global engLmW4SOff ulong 1
85global engRopeMap long 1
86global engRopeVa ulong 1
87global engGdnStateMap long 1
88global engGdnStateVa ulong 1
89global engGdnTailMap long 1
90global engGdnTailVa ulong 1
91global engKvMap long 64
92global engKvVa ulong 64
93global engKvSlabBytes ulong 1
94
95global engIoBuf uchar 1048576
96global engWideBuf uchar 524288
97global engRopeFreq f64 128
98global engCandIds uint 128
99global engDig uchar 32
100global engTokSeq uint 131072
101global engCurPos ulong 1
102
103fn engPrint s view uchar * to void
104r = write 1 s 0 len s
105end
106
107fn engPrintU x ulong to void
108if eq x 0
109put engDig 0 48
110r = write 1 engDig 0 1
111ret
112end
113v = x
114p = ulong 32
115while gt v 0 bound 24
116p = sub p 1
117put engDig p uchar add 48 rem v 10
118v = div v 10
119end
120r = write 1 engDig p sub 32 p
121end
122
123fn q36Write w view ulong 4 to void
124p = get engCodePos 0
125b = new uchar 16 0
126if gt get engCodeMap 0 0
127for i ulong 16
128ww = get w div i 4
129sh = mul rem i 4 8
130put b i uchar and shr ww sh 255
131end
132if ne mapwrite get engCodeMap 0 p b 0 16 16
133put engCodeFail 0 1
134end
135end
136put engCodePos 0 add p 16
137end
138
139fn engRegKernel kid ulong src ulong mode ulong smem ulong to void
140p = mul div add get engCodePos 0 255 256 256
141put engEntry kid p
142put engSmem kid smem
143put engCodePos 0 p
144put q36Pc 0 0
145put q36Guard 0 0
146if eq src 0
147if not q36Kernel uchar mode
148exit 4
149end
150else
151if not engKernel mode
152exit 4
153end
154end
155q36Done
156end
157
158fn engEmitAll to void
159put engCodePos 0 0
160note 0: q38W4Gemv4R (4 KiB streaming SMEM W4 GEMV for ANY K % 1024 == 0, ANY N)
161engRegKernel 0 1 200 4128
162note 1: uRmsNormAdd (specialized for H, addOne, eps)
163engRegKernel 1 1 300 64
164note 2: uSwiGlu (any I % 256 == 0)
165engRegKernel 2 1 301 0
166note 3: uBf16ToW4Quant (on-GPU BF16->W4 quant for ANY K % 64 == 0, ANY N)
167engRegKernel 3 1 302 0
168note 4: uBf16Gemv1R (universal BF16 GEMV for ANY K % 256 == 0, ANY N)
169engRegKernel 4 1 303 0
170note 5: uBf16RescoreCands (specialized for H)
171engRegKernel 5 1 304 0
172note 6: uArgmaxEmbedStep (specialized for H)
173engRegKernel 6 1 305 512
174note 7: uFaPrepKv (specialized for nKV, hd, ropeDims, qkNorm, addOne)
175engRegKernel 7 1 306 1088
176note 8: uFaPrepQ (specialized for nQ, hd, ropeDims, qkNorm, addOne, attnGate)
177engRegKernel 8 1 307 1088
178note 9: uFaAttendSplitF32 (specialized for nQ, nKV, hd)
179engRegKernel 9 1 308 0
180note 10: uFaReduceGate (specialized for hd, attnGate)
181engRegKernel 10 1 309 0
182note 11: qFp8ToW4B32Quant (on-GPU FP8->W4 quant, SMEM=1024)
183engRegKernel 11 0 121 1024
184note 12: vocab_chunk_top64 (64 chunks -> 128 cands)
185engRegKernel 12 0 112 2048
186note 13: qGdnConvL2Fast (causal conv1d + SiLU + L2-norm)
187engRegKernel 13 0 114 64
188if gt get engSpec MS_N_GDN 0
189note 14: uGdnStepReal (specialized for nK, nV, gdnSigmoid)
190engRegKernel 14 1 310 31744
191end
192note 15: uW4GemvAnyK (universal W4 GEMV for any K % 128 == 0, any N)
193engRegKernel 15 1 400 0
194note 16: uBf16GemvAnyK (universal BF16 GEMV for any K % 64 == 0, any N)
195engRegKernel 16 1 401 0
196if eq get engSpec MS_ARCH 3
197engRegKernel 17 1 402 64
198engRegKernel 18 1 403 0
199engRegKernel 19 1 404 0
200engRegKernel 20 1 405 0
201engRegKernel 21 1 406 4096
202engRegKernel 22 1 407 0
203engRegKernel 23 1 408 0
204engRegKernel 24 1 409 64
205engRegKernel 25 1 410 0
206engRegKernel 27 1 412 2080
207engRegKernel 28 1 413 2624
208end
209note 26: uW4Gemv4R512 (4-row/warp LDG.E.64 + 2 KiB SMEM W4 GEMV for any K % 512 == 0)
210engRegKernel 26 1 411 2080
211note 29: uBf16Gemv4R (4-row/warp LDG.E.64 + 512 B SMEM BF16 GEMV for any K % 128 == 0)
212engRegKernel 29 1 414 528
213note 30: uW4GemmB8 (Batched B=8 W4 GEMM for any K % 128 == 0, any N)
214engRegKernel 30 1 415 4128
215note 31: uBf16GemmB8 (Batched B=8 BF16 GEMM for any K % 64 == 0, any N)
216engRegKernel 31 1 416 2080
217note 32..38: NVFP4 (FP4 E2M1 + FP8 E4M3 block-16) quantizers, GEMV, B=8 GEMM & MoE
218engRegKernel 32 1 420 1024
219engRegKernel 33 1 421 0
220engRegKernel 34 1 422 2144
221engRegKernel 35 1 423 4192
222if eq get engSpec MS_ARCH 3
223engRegKernel 36 1 424 2144
224engRegKernel 37 1 425 2688
225end
226engRegKernel 38 1 426 64
227note 40..41: NVFP4 activation quant + hardware OMMA.SF.16864 B=8 GEMM
228engRegKernel 40 1 440 1024
229engRegKernel 41 1 441 2688
230note 43..49: Batched B=8 elementwise/HC/router/GDN kernels
231engRegKernel 43 1 417 0
232engRegKernel 44 1 418 64
233engRegKernel 45 1 419 0
234engRegKernel 46 1 430 0
235engRegKernel 47 1 431 4096
236engRegKernel 48 1 432 64
237engRegKernel 49 1 311 31744
238end
239
240fn engW off ulong to ulong
241ret add get engWorkVa 0 off
242end
243
244fn engArg u uint to void
245n = get engBindN 0
246put engBind n u
247put engBindN 0 add n 1
248end
249
250fn engPtr va ulong to void
251engArg gblo va
252engArg gbhi va
253end
254
255fn engPktStartAt off ulong to void
256put engPktOff 0 off
257put engPktPos 0 0
258put engBindN 0 0
259put gbused 0 0
260gbsetup
261gbmth 0 40 4
262gbemit 0
263gbemit 0
264gbemit 0
265gbemit 2281701376
266cb = mapaddr get gbmem 1
267gbdriver cb
268gbdma cb 224
269for j ulong 224
270gbemit get gbcb j
271end
272n = get gbused 0
273r = mapwrite get engWork 0 off gbcmd 0 n
274put engPktPos 0 n
275end
276
277fn engPktStart to void
278engPktStartAt add EW_PKT 16777216
279end
280
281fn engPrepQmdGy kid ulong gx uint gy uint bx uint to void
282gbqmd 1
283a = add mapaddr get engCodeMap 0 get engEntry kid
284put gbq1 32 gblo shr a 4
285put gbq1 33 or uint and shr a 36 2097151 20971520
286put gbq1 59 gblo shr a 8
287put gbq1 34 or uint 65536 bx
288put gbq1 35 or uint 131073 shl uint 64 8
289sm = get engSmem kid
290s7 = ulong 8
291if ne sm 0
292s7 = div add sm 127 128
293end
294sc = ulong 9
295if gt sm 32768
296sc = ulong 17
297end
298if gt sm 65536
299sc = ulong 26
300end
301w36 = or or or shl sc 23 shl ulong 26 17 shl sc 11 s7
302put gbq1 36 uint w36
303put gbq1 37 0
304put gbq1 39 gx
305put gbq1 40 gy
306put gbq1 41 1
307put gbcb 216 bx
308put gbcb 220 gx
309put gbcb 221 gy
310put gbcb 222 1
311end
312
313fn engPktEmitGy kid ulong gx uint gy uint bx uint to bool
314engPrepQmdGy kid gx gy bx
315cb = mapaddr get gbmem 1
316words = get engBindN 0
317endva = add cb add 896 mul words 4
318put gbcb 212 gbhi endva
319put gbcb 213 gblo endva
320put gbused 0 0
321gbdma add cb 896 uint words
322for j ulong words
323gbemit get engBind j
324end
325q = mapaddr get gbmem 2
326gbinline q false
327gbdma add q 8192 1
328gbemit 1
329gbinline add q 4096 true
330if eq get engUseStrictSync 0 1
331gbmth 1 272 1
332gbemit 0
333end
334gbrel add mapaddr get gbmem 3 32 6
335if eq get engUseStrictSync 0 1
336gbmth 1 540 1
337gbemit 4113
338end
339put engBindN 0 0
340n = get gbused 0
341pos = get engPktPos 0
342if gt add pos n 4000000
343ret false
344end
345dstOff = add get engPktOff 0 pos
346if ne mapwrite get engWork 0 dstOff gbcmd 0 n long n
347ret false
348end
349put engPktPos 0 add pos n
350ret true
351end
352
353fn engPktFinish slot ulong to bool
354put gbused 0 0
355gbtail
356n = get gbused 0
357pos = get engPktPos 0
358dstOff = add get engPktOff 0 pos
359if ne mapwrite get engWork 0 dstOff gbcmd 0 n long n
360ret false
361end
362put engSlotOff slot get engPktOff 0
363put engSlotLen slot add pos n
364ret true
365end
366
367fn engWaitFast timeout ulong to bool
368for sp ulong 50000
369if eq mapget get gbmem 3 16 4 1179010630
370put nvret 0 get nvput 0
371ret true
372end
373end
374t0 = monons
375while lt ulong sub monons t0 timeout bound 5000000
376if eq mapget get gbmem 3 16 4 1179010630
377put nvret 0 get nvput 0
378ret true
379end
380r = sleepns long 5000
381end
382ret false
383end
384
385fn engReplayPre slot ulong to bool
386r0 = mapset get gbmem 2 8192 4 0
387r1 = mapset get gbmem 3 0 4 0
388r2 = mapset get gbmem 3 16 4 0
389off = get engSlotOff slot
390ln = get engSlotLen slot
391if not nvsend get engWork 0 off uint div ln 4
392ret false
393end
394if not engWaitFast 30000000000
395engPrintU ulong mapget get gbmem 3 0 4
396ret false
397end
398ret eq mapget get gbmem 3 0 4 1145324612
399end
400
401fn engPktRun to bool
402put gbused 0 0
403gbtail
404n = get gbused 0
405pos = get engPktPos 0
406dstOff = add get engPktOff 0 pos
407if ne mapwrite get engWork 0 dstOff gbcmd 0 n long n
408ret false
409end
410tot = add pos n
411r0 = mapset get gbmem 2 8192 4 0
412r1 = mapset get gbmem 3 0 4 0
413r2 = mapset get gbmem 3 16 4 0
414if not nvsend get engWork 0 get engPktOff 0 uint div tot 4
415ret false
416end
417if not engWaitFast 30000000000
418engPrintU ulong mapget get gbmem 3 0 4
419ret false
420end
421ret eq mapget get gbmem 3 0 4 1145324612
422end
423
424note Dynamic UVM slab bump allocator (returns 256-byte aligned VA, writes outMap/outOff)
425global engAllocMap long 1
426global engAllocOff ulong 1
427
428fn engSlabAlloc bytes ulong to ulong
429al = mul div add bytes 255 256 256
430sc = get engSlabCnt 0
431for si ulong sc bound 48
432used = get engSlabUsed si
433if le add used al ENG_SLAB_SZ
434put engSlabUsed si add used al
435put engAllocMap 0 get engSlabMap si
436put engAllocOff 0 used
437ret add get engSlabVa si used
438end
439end
440if ge sc 48
441ret 0
442end
443m = nvalloc ENG_SLAB_SZ true
444if le m 0
445ret 0
446end
447va = mapaddr m
448put engSlabMap sc m
449put engSlabVa sc va
450put engSlabUsed sc al
451put engSlabCnt 0 add sc 1
452put engAllocMap 0 m
453put engAllocOff 0 0
454ret va
455end
456
457fn engMathExp64 x f64 to f64
458k = long mul x f64 1.4426950408889634f64
459r = sub x mul f64 k f64 0.6931471805599453f64
460p = f64 1.0f64
461term = f64 1.0f64
462for i ulong 14
463term = div mul term r f64 add i 1
464p = add p term
465end
466if ge k 0
467for i ulong ulong k bound 1024
468p = mul p f64 2.0f64
469end
470else
471for i ulong ulong neg k bound 1024
472p = mul p f64 0.5f64
473end
474end
475ret p
476end
477
478fn engMathLn64 x f64 to f64
479note Natural log via range reduction x = m * 2^e, m in [0.75, 1.5], series for ln((1+u)/(1-u))
480m = x
481e = long 0
482while gt m f64 1.5f64 bound 64
483m = mul m f64 0.5f64
484e = add e 1
485end
486while lt m f64 0.75f64 bound 64
487m = mul m f64 2.0f64
488e = sub e 1
489end
490u = div sub m f64 1.0f64 add m f64 1.0f64
491u2 = mul u u
492s = u
493t = u
494for k ulong 12
495t = mul t u2
496s = add s div t f64 add mul add k 1 2 1
497end
498ret add mul f64 2.0f64 s mul f64 e f64 0.6931471805599453f64
499end
500
501fn engSinCore x f64 to f64
502x2 = mul x x
503s = x
504t = x
505for i ulong 7
506k0 = add mul i 2 2
507t = div mul neg t x2 mul f64 k0 f64 add k0 1
508s = add s t
509end
510ret s
511end
512
513fn engMathSinCos64 theta f64 isCos bool to f64
514tp = f64 6.283185307179586f64
515pi = f64 3.141592653589793f64
516hp = f64 1.570796326794896f64
517x = sub theta mul f64 long div theta tp tp
518if gt x pi
519x = sub x tp
520end
521if lt x neg pi
522x = add x tp
523end
524if isCos
525x = add x hp
526if gt x pi
527x = sub x tp
528end
529end
530sgn = f64 1.0f64
531if gt x hp
532x = sub pi x
533end
534if lt x neg hp
535x = sub neg pi x
536end
537if lt x f64 0.0f64
538x = neg x
539sgn = f64 -1.0f64
540end
541ret mul sgn engSinCore x
542end
543
544fn engPutU32Io off ulong w uint to void
545put engIoBuf off uchar and w 255
546put engIoBuf add off 1 uchar and shr w 8 255
547put engIoBuf add off 2 uchar and shr w 16 255
548put engIoBuf add off 3 uchar and shr w 24 255
549end
550
551fn engGetU32Io p ulong to uint
552b0 = uint get engIoBuf p
553b1 = shl uint get engIoBuf add p 1 8
554b2 = shl uint get engIoBuf add p 2 16
555b3 = shl uint get engIoBuf add p 3 24
556ret or or b0 b1 or b2 b3
557end
558
559fn engFillRopeRow base ulong p ulong halfR ulong to void
560for d ulong halfR bound 128
561th = mul f64 p get engRopeFreq d
562cw = bitcast uint f32 engMathSinCos64 th true
563sw = bitcast uint f32 engMathSinCos64 th false
564engPutU32Io add base mul d 4 cw
565engPutU32Io add base add mul halfR 4 mul d 4 sw
566end
567end
568
569fn engUploadRope to bool
570ropeD = get engSpec MS_ROPE_DIMS
571halfR = div ropeD 2
572maxT = get engSpec MS_MAX_CTX
573lnTh = engMathLn64 f64 get engSpec MS_ROPE_THETA
574for i ulong halfR bound 128
575e = div mul neg f64 mul i 2 lnTh f64 ropeD
576put engRopeFreq i engMathExp64 e
577end
578rowBytes = mul ropeD 4
579chunkT = div 262144 rowBytes
580pos = ulong 0
581while lt pos maxT bound 1024
582cnt = sub maxT pos
583if gt cnt chunkT
584cnt = chunkT
585end
586for t ulong cnt bound 1024
587engFillRopeRow mul t rowBytes add pos t halfR
588end
589wbytes = mul cnt rowBytes
590if ne mapwrite get engRopeMap 0 mul pos rowBytes engIoBuf 0 wbytes long wbytes
591ret false
592end
593pos = add pos cnt
594end
595ret true
596end
597
598fn engBuildFp8Lut to bool
599m = nvalloc 4096 true
600if le m 0
601ret false
602end
603put engLut 0 m
604for i ulong 256
605s = and shr i 7 1
606e = and shr i 3 15
607mn = and i 7
608v = f32 0.0f32
609if eq e 15
610if eq mn 7
611v = f32 0.0f32
612else
613v = mul f32 256.0f32 add f32 1.0f32 div f32 mn f32 8.0f32
614end
615else
616if eq e 0
617v = mul f32 0.015625f32 div f32 mn f32 8.0f32
618else
619pw = f32 1.0f32
620if ge e 7
621for k ulong sub e 7 bound 16
622pw = mul pw f32 2.0f32
623end
624else
625for k ulong sub 7 e bound 16
626pw = mul pw f32 0.5f32
627end
628end
629v = mul pw add f32 1.0f32 div f32 mn f32 8.0f32
630end
631end
632if eq s 1
633v = neg v
634end
635engPutU32Io mul i 4 bitcast uint v
636end
637ret eq mapwrite m 0 engIoBuf 0 1024 1024
638end
639
640fn engWidenBuf cnt ulong to void
641for i ulong cnt bound 131072
642p = mul i 2
643q = mul i 4
644put engWideBuf q 0
645put engWideBuf add q 1 0
646put engWideBuf add q 2 get engIoBuf p
647put engWideBuf add q 3 get engIoBuf add p 1
648end
649end
650
651fn engStreamTensorRaw tid ulong dstMap long dstOff ulong to bool
652fid = get stFileId tid
653foff = get stByteOff tid
654bytes = get stByteLen tid
655fd = stOpenFile fid
656if lt fd 0
657ret false
658end
659pos = ulong 0
660while lt pos bytes bound 4000
661want = sub bytes pos
662if gt want 1048576
663want = ulong 1048576
664end
665got = pread fd engIoBuf 0 want long add foff pos
666if le got 0
667ret false
668end
669if ne mapwrite dstMap add dstOff pos engIoBuf 0 ulong got got
670ret false
671end
672pos = add pos ulong got
673end
674ret true
675end
676
677fn engStreamWidenToMap fd int foff ulong bytes ulong dm long doff ulong to bool
678pos = ulong 0
679while lt pos bytes bound 64
680want = sub bytes pos
681if gt want 262144
682want = ulong 262144
683end
684got = pread fd engIoBuf 0 want long add foff pos
685if ne got long want
686ret false
687end
688engWidenBuf div want 2
689ob = mul want 2
690if ne mapwrite dm add doff mul pos 2 engWideBuf 0 ob long ob
691ret false
692end
693pos = add pos want
694end
695ret true
696end
697
698fn engLoadTensorF32 tid ulong to ulong
699dt = get stDtype tid
700bytes = get stByteLen tid
701outBytes = bytes
702if eq dt DT_BF16
703outBytes = mul bytes 2
704end
705va = engSlabAlloc outBytes
706if eq va 0
707ret 0
708end
709dm = get engAllocMap 0
710doff = get engAllocOff 0
711if eq dt DT_F32
712if not engStreamTensorRaw tid dm doff
713ret 0
714end
715ret va
716end
717fd = stOpenFile get stFileId tid
718if lt fd int 0
719ret 0
720end
721if not engStreamWidenToMap fd get stByteOff tid bytes dm doff
722ret 0
723end
724ret va
725end
726
727global engMatWVa ulong 1
728global engMatSVa ulong 1
729global engFuseTidW long 4
730global engFuseTidS long 4
731global engFuseN ulong 4
732global engFuseOff ulong 4
733global engFuseSVa ulong 4
734
735fn engEmitQuantOne idx ulong wVa ulong sVa ulong K ulong to bool
736tidW = ulong get engFuseTidW idx
737dt = get stDtype tidW
738N = get engFuseN idx
739srcVa = add get engScratchVa 0 get engFuseOff idx
740if eq dt DT_F8_E4M3
741engPtr srcVa
742engPtr get engFuseSVa idx
743engPtr mapaddr get engLut 0
744engPtr wVa
745engPtr sVa
746engArg uint K
747engArg uint N
748if eq get engUseNvfp4 0 1
749ret engPktEmitGy 32 uint div add N 3 4 1 128
750end
751ret engPktEmitGy 11 uint div add N 3 4 1 128
752end
753engPtr srcVa
754engPtr wVa
755engPtr sVa
756engArg uint K
757engArg uint N
758if eq get engUseNvfp4 0 1
759ret engPktEmitGy 33 uint div add N 7 8 1 256
760end
761ret engPktEmitGy 3 uint div add N 7 8 1 256
762end
763
764fn engStageFusedOne i ulong sOff ulong to bool
765tidW = ulong get engFuseTidW i
766if not engStreamTensorRaw tidW get engScratch 0 sOff
767ret false
768end
769put engFuseOff i sOff
770if eq get stDtype tidW DT_F8_E4M3
771tidS = get engFuseTidS i
772if lt tidS 0
773ret false
774end
775sv = engLoadTensorF32 ulong tidS
776if eq sv 0
777ret false
778end
779put engFuseSVa i sv
780end
781ret true
782end
783
784fn engQuantFusedBatched cnt ulong K ulong wVa ulong sVa ulong to bool
785sOff = ulong 0
786for i ulong cnt bound 4
787if not engStageFusedOne i sOff
788ret false
789end
790bl = get stByteLen ulong get engFuseTidW i
791sOff = add sOff mul div add bl 255 256 256
792end
793engPktStart
794curW = wVa
795curS = sVa
796for i ulong cnt bound 4
797if not engEmitQuantOne i curW curS K
798ret false
799end
800Ni = get engFuseN i
801curW = add curW div mul Ni K 2
802curS = add curS mul Ni div K 16
803end
804ret engPktRun
805end
806
807fn engQuantFusedSerial cnt ulong K ulong wVa ulong sVa ulong to bool
808curW = wVa
809curS = sVa
810for i ulong cnt bound 4
811if not engStageFusedOne i 0
812ret false
813end
814engPktStart
815if not engEmitQuantOne i curW curS K
816ret false
817end
818if not engPktRun
819ret false
820end
821Ni = get engFuseN i
822curW = add curW div mul Ni K 2
823curS = add curS mul Ni div K 16
824end
825ret true
826end
827
828fn engLoadFusedMatsW4 cnt ulong K ulong to bool
829totN = ulong 0
830totBytes = ulong 0
831for i ulong cnt bound 4
832if lt get engFuseTidW i 0
833ret false
834end
835totN = add totN get engFuseN i
836bl0 = get stByteLen ulong get engFuseTidW i
837totBytes = add totBytes mul div add bl0 255 256 256
838end
839wVa = engSlabAlloc div mul totN K 2
840if eq wVa 0
841ret false
842end
843sVa = engSlabAlloc mul totN div K 16
844if eq sVa 0
845ret false
846end
847put engMatWVa 0 wVa
848put engMatSVa 0 sVa
849if le totBytes ENG_SCRATCH_BYTES
850ret engQuantFusedBatched cnt K wVa sVa
851end
852ret engQuantFusedSerial cnt K wVa sVa
853end
854
855fn engLoadMatW4 tidW ulong tidS long K ulong N ulong to bool
856wBytes = div mul N K 2
857sBytes = mul N div K 16
858wVa = engSlabAlloc wBytes
859if eq wVa 0
860ret false
861end
862sVa = engSlabAlloc sBytes
863if eq sVa 0
864ret false
865end
866put engMatWVa 0 wVa
867put engMatSVa 0 sVa
868dt = get stDtype tidW
869if not engStreamTensorRaw tidW get engScratch 0 0
870ret false
871end
872if eq dt DT_F8_E4M3
873if lt tidS 0
874ret false
875end
876sSrcVa = engLoadTensorF32 ulong tidS
877if eq sSrcVa 0
878ret false
879end
880engPktStart
881engPtr get engScratchVa 0
882engPtr sSrcVa
883engPtr mapaddr get engLut 0
884engPtr wVa
885engPtr sVa
886engArg uint K
887engArg uint N
888qKid = ulong 11
889if eq get engUseNvfp4 0 1
890qKid = ulong 32
891end
892if not engPktEmitGy qKid uint div add N 3 4 1 128
893ret false
894end
895ret engPktRun
896end
897engPktStart
898engPtr get engScratchVa 0
899engPtr wVa
900engPtr sVa
901engArg uint K
902engArg uint N
903qKidB = ulong 3
904if eq get engUseNvfp4 0 1
905qKidB = ulong 33
906end
907if not engPktEmitGy qKidB uint div add N 7 8 1 256
908ret false
909end
910ret engPktRun
911end
912
913text tNmEmb "embed_tokens.weight"
914text tNmLm "lm_head.weight"
915text tNmFNorm "norm.weight"
916text tNmInNorm "input_layernorm.weight"
917text tNmPostNorm "post_attention_layernorm.weight"
918text tNmQW "self_attn.q_proj.weight"
919text tNmQS "self_attn.q_proj.weight_scale_inv"
920text tNmKW "self_attn.k_proj.weight"
921text tNmKS "self_attn.k_proj.weight_scale_inv"
922text tNmVW "self_attn.v_proj.weight"
923text tNmVS "self_attn.v_proj.weight_scale_inv"
924text tNmOW "self_attn.o_proj.weight"
925text tNmOS "self_attn.o_proj.weight_scale_inv"
926text tNmQNorm "self_attn.q_norm.weight"
927text tNmKNorm "self_attn.k_norm.weight"
928text tNmGdnQkvW "linear_attn.in_proj_qkv.weight"
929text tNmGdnQkvS "linear_attn.in_proj_qkv.weight_scale_inv"
930text tNmGdnZW "linear_attn.in_proj_z.weight"
931text tNmGdnZS "linear_attn.in_proj_z.weight_scale_inv"
932text tNmGdnOutW "linear_attn.out_proj.weight"
933text tNmGdnOutS "linear_attn.out_proj.weight_scale_inv"
934text tNmGdnInA "linear_attn.in_proj_a.weight"
935text tNmGdnInB "linear_attn.in_proj_b.weight"
936text tNmGdnConv "linear_attn.conv1d.weight"
937text tNmGdnAlog "linear_attn.A_log"
938text tNmGdnDt "linear_attn.dt_bias"
939text tNmGdnNorm "linear_attn.norm.weight"
940text tNmGateW "mlp.gate_proj.weight"
941text tNmGateS "mlp.gate_proj.weight_scale_inv"
942text tNmUpW "mlp.up_proj.weight"
943text tNmUpS "mlp.up_proj.weight_scale_inv"
944text tNmDnW "mlp.down_proj.weight"
945text tNmDnS "mlp.down_proj.weight_scale_inv"
946
947fn engGetU32Wide p ulong to uint
948b0 = uint get engWideBuf p
949b1 = shl uint get engWideBuf add p 1 8
950b2 = shl uint get engWideBuf add p 2 16
951b3 = shl uint get engWideBuf add p 3 24
952ret or or b0 b1 or b2 b3
953end
954
955fn engNegExpAlog nV ulong isBf16 bool to void
956if isBf16
957engWidenBuf nV
958for h ulong nV bound 64
959p = mul h 4
960f = f32 engMathExp64 f64 bitcast f32 engGetU32Wide p
961engPutU32Io p bitcast uint neg f
962end
963else
964for h ulong nV bound 64
965p = mul h 4
966f = f32 engMathExp64 f64 bitcast f32 engGetU32Io p
967engPutU32Io p bitcast uint neg f
968end
969end
970end
971
972fn engLoadDtPart tid ulong dm long off ulong nV ulong to bool
973fdD = stOpenFile get stFileId tid
974if lt fdD int 0
975ret false
976end
977bD = get stByteLen tid
978if ne pread fdD engIoBuf 0 bD long get stByteOff tid long bD
979ret false
980end
981if eq get stDtype tid DT_BF16
982engWidenBuf nV
983ret eq mapwrite dm off engWideBuf 0 mul nV 4 long mul nV 4
984end
985ret eq mapwrite dm off engIoBuf 0 mul nV 4 long mul nV 4
986end
987
988fn engLoadGdnAlogDt L ulong to ulong
989nV = get engSpec MS_GDN_NV
990tA = stFindLayer L tNmGdnAlog
991tD = stFindLayer L tNmGdnDt
992if or lt tA 0 lt tD 0
993ret 0
994end
995va = engSlabAlloc mul nV 8
996if eq va 0
997ret 0
998end
999dm = get engAllocMap 0
1000doff = get engAllocOff 0
1001fdA = stOpenFile get stFileId ulong tA
1002if lt fdA int 0
1003ret 0
1004end
1005bA = get stByteLen ulong tA
1006if ne pread fdA engIoBuf 0 bA long get stByteOff ulong tA long bA
1007ret 0
1008end
1009engNegExpAlog nV eq get stDtype ulong tA DT_BF16
1010if ne mapwrite dm doff engIoBuf 0 mul nV 4 long mul nV 4
1011ret 0
1012end
1013if not engLoadDtPart ulong tD dm add doff mul nV 4 nV
1014ret 0
1015end
1016ret va
1017end
1018
1019fn engLoadGdnInAb L ulong to ulong
1020H = get engSpec MS_H
1021nV = get engSpec MS_GDN_NV
1022tA = stFindLayer L tNmGdnInA
1023tB = stFindLayer L tNmGdnInB
1024if or lt tA 0 lt tB 0
1025ret 0
1026end
1027oneBytes = mul nV mul H 2
1028va = engSlabAlloc mul oneBytes 2
1029if eq va 0
1030ret 0
1031end
1032dm = get engAllocMap 0
1033doff = get engAllocOff 0
1034if not engStreamTensorRaw ulong tA dm doff
1035ret 0
1036end
1037if not engStreamTensorRaw ulong tB dm add doff oneBytes
1038ret 0
1039end
1040ret va
1041end
1042
1043fn engLoadLayerMat tW long tS long K ulong N ulong to bool
1044if lt tW 0
1045ret false
1046end
1047ret engLoadMatW4 ulong tW tS K N
1048end
1049
1050fn engLoadFaLayer L ulong to bool
1051H = get engSpec MS_H
1052qN = mul get engSpec MS_NQ get engSpec MS_HD
1053if eq get engSpec MS_ATTN_GATE 1
1054qN = mul qN 2
1055end
1056kvN = mul get engSpec MS_NKV get engSpec MS_HD
1057if not engLoadLayerMat stFindLayer L tNmQW stFindLayer L tNmQS H qN
1058ret false
1059end
1060put engLA1WVa L get engMatWVa 0
1061put engLA1SVa L get engMatSVa 0
1062if not engLoadLayerMat stFindLayer L tNmKW stFindLayer L tNmKS H kvN
1063ret false
1064end
1065put engLA2WVa L get engMatWVa 0
1066put engLA2SVa L get engMatSVa 0
1067if not engLoadLayerMat stFindLayer L tNmVW stFindLayer L tNmVS H kvN
1068ret false
1069end
1070put engLA3WVa L get engMatWVa 0
1071put engLA3SVa L get engMatSVa 0
1072if not engLoadLayerMat stFindLayer L tNmOW stFindLayer L tNmOS mul get engSpec MS_NQ get engSpec MS_HD H
1073ret false
1074end
1075put engLA4WVa L get engMatWVa 0
1076put engLA4SVa L get engMatSVa 0
1077if eq get engSpec MS_QK_NORM 1
1078tQN = stFindLayer L tNmQNorm
1079tKN = stFindLayer L tNmKNorm
1080if or lt tQN 0 lt tKN 0
1081ret false
1082end
1083put engLQNormVa L engLoadTensorF32 ulong tQN
1084put engLKNormVa L engLoadTensorF32 ulong tKN
1085end
1086ret true
1087end
1088
1089fn engLoadGdnLayer L ulong to bool
1090H = get engSpec MS_H
1091qkvN = add mul get engSpec MS_GDN_NK 256 mul get engSpec MS_GDN_NV 128
1092zN = mul get engSpec MS_GDN_NV 128
1093if not engLoadLayerMat stFindLayer L tNmGdnQkvW stFindLayer L tNmGdnQkvS H qkvN
1094ret false
1095end
1096put engLA1WVa L get engMatWVa 0
1097put engLA1SVa L get engMatSVa 0
1098if not engLoadLayerMat stFindLayer L tNmGdnZW stFindLayer L tNmGdnZS H zN
1099ret false
1100end
1101put engLA2WVa L get engMatWVa 0
1102put engLA2SVa L get engMatSVa 0
1103if not engLoadLayerMat stFindLayer L tNmGdnOutW stFindLayer L tNmGdnOutS zN H
1104ret false
1105end
1106put engLA3WVa L get engMatWVa 0
1107put engLA3SVa L get engMatSVa 0
1108tConv = stFindLayer L tNmGdnConv
1109tNorm = stFindLayer L tNmGdnNorm
1110if or lt tConv 0 lt tNorm 0
1111ret false
1112end
1113put engLA4WVa L engLoadGdnInAb L
1114put engLA4SVa L engLoadTensorF32 ulong tConv
1115put engLQNormVa L engLoadTensorF32 ulong tNorm
1116put engLKNormVa L engLoadGdnAlogDt L
1117if or eq get engLA4WVa L 0 eq get engLA4SVa L 0
1118ret false
1119end
1120ret and ne get engLQNormVa L 0 ne get engLKNormVa L 0
1121end
1122
1123fn engLoadMlpLayer L ulong to bool
1124H = get engSpec MS_H
1125I = get engSpec MS_I
1126note Fuse gate_proj|up_proj into one contiguous [2*I, H] W4 slab (saves 64 WFI barriers/tok!)
1127put engFuseTidW 0 stFindLayer L tNmGateW
1128put engFuseTidS 0 stFindLayer L tNmGateS
1129put engFuseN 0 I
1130put engFuseTidW 1 stFindLayer L tNmUpW
1131put engFuseTidS 1 stFindLayer L tNmUpS
1132put engFuseN 1 I
1133if not engLoadFusedMatsW4 2 H
1134ret false
1135end
1136put engLGateWVa L get engMatWVa 0
1137put engLGateSVa L get engMatSVa 0
1138if not engLoadLayerMat stFindLayer L tNmDnW stFindLayer L tNmDnS I H
1139ret false
1140end
1141put engLDnWVa L get engMatWVa 0
1142put engLDnSVa L get engMatSVa 0
1143ret true
1144end
1145
1146fn engLoadLayer L ulong to bool
1147tIn = stFindLayer L tNmInNorm
1148tPost = stFindLayer L tNmPostNorm
1149if or lt tIn 0 lt tPost 0
1150ret false
1151end
1152vaIn = engLoadTensorF32 ulong tIn
1153vaPost = engLoadTensorF32 ulong tPost
1154if or eq vaIn 0 eq vaPost 0
1155ret false
1156end
1157put engLInNormVa L vaIn
1158put engLPostNormVa L vaPost
1159if eq get engLayerKind L 0
1160if not engLoadFaLayer L
1161ret false
1162end
1163else
1164if not engLoadGdnLayer L
1165ret false
1166end
1167end
1168ret engLoadMlpLayer L
1169end
1170
1171fn engEmitW4Gemv xVa ulong wVa ulong sVa ulong outVa ulong K ulong N ulong to bool
1172engPtr xVa
1173engPtr wVa
1174engPtr sVa
1175engPtr outVa
1176engArg uint K
1177engArg uint N
1178if eq get engUseNvfp4 0 1
1179if eq rem K 512 0
1180ret engPktEmitGy 34 uint div add N 31 32 1 256
1181end
1182ret engPktEmitGy 38 uint div add N 7 8 1 256
1183end
1184if eq rem K 1024 0
1185ret engPktEmitGy 0 uint div add N 31 32 1 256
1186end
1187if eq rem K 512 0
1188ret engPktEmitGy 26 uint div add N 31 32 1 256
1189end
1190ret engPktEmitGy 15 uint div add N 7 8 1 256
1191end
1192
1193fn engEmitRmsNormAdd normVa ulong doAdd uint to bool
1194engPtr engW EW_X
1195engPtr engW EW_AOUT
1196engPtr normVa
1197engPtr engW EW_XN
1198engArg doAdd
1199ret engPktEmitGy 1 1 1 256
1200end
1201
1202fn engEmitFaAttn L ulong faIdx ulong to bool
1203nQ = get engSpec MS_NQ
1204nKV = get engSpec MS_NKV
1205hd = get engSpec MS_HD
1206kBase = get engKvVa faIdx
1207vBase = add kBase div get engKvSlabBytes 0 2
1208engPtr engW EW_K
1209engPtr engW EW_V
1210engPtr get engLKNormVa L
1211engPtr get engRopeVa 0
1212engPtr kBase
1213engPtr vBase
1214engPtr engW EW_POS
1215if not engPktEmitGy 7 uint nKV 1 uint hd
1216ret false
1217end
1218engPtr engW EW_QKV
1219engPtr get engLQNormVa L
1220engPtr get engRopeVa 0
1221engPtr engW EW_QROT
1222engPtr engW EW_POS
1223if not engPktEmitGy 8 uint nQ 1 uint hd
1224ret false
1225end
1226engPtr engW EW_QROT
1227engPtr kBase
1228engPtr vBase
1229engPtr engW EW_OPART
1230engPtr engW EW_MLPART
1231engPtr engW EW_POS
1232if not engPktEmitGy 9 uint nQ 8 32
1233ret false
1234end
1235engPtr engW EW_OPART
1236engPtr engW EW_MLPART
1237engPtr engW EW_QKV
1238engPtr engW EW_Y
1239engArg 8
1240ret engPktEmitGy 10 uint nQ 1 uint hd
1241end
1242
1243fn engEmitFaLayer L ulong faIdx ulong to bool
1244H = get engSpec MS_H
1245qN = mul get engSpec MS_NQ get engSpec MS_HD
1246if eq get engSpec MS_ATTN_GATE 1
1247qN = mul qN 2
1248end
1249kvN = mul get engSpec MS_NKV get engSpec MS_HD
1250if not engEmitW4Gemv engW EW_XN get engLA1WVa L get engLA1SVa L engW EW_QKV H qN
1251ret false
1252end
1253if not engEmitW4Gemv engW EW_XN get engLA2WVa L get engLA2SVa L engW EW_K H kvN
1254ret false
1255end
1256if not engEmitW4Gemv engW EW_XN get engLA3WVa L get engLA3SVa L engW EW_V H kvN
1257ret false
1258end
1259if not engEmitFaAttn L faIdx
1260ret false
1261end
1262oK = mul get engSpec MS_NQ get engSpec MS_HD
1263ret engEmitW4Gemv engW EW_Y get engLA4WVa L get engLA4SVa L engW EW_AOUT oK H
1264end
1265
1266fn engEmitGdnLayer L ulong gdnIdx ulong to bool
1267H = get engSpec MS_H
1268nK = get engSpec MS_GDN_NK
1269nV = get engSpec MS_GDN_NV
1270qkvN = add mul nK 256 mul nV 128
1271zN = mul nV 128
1272if not engEmitW4Gemv engW EW_XN get engLA1WVa L get engLA1SVa L engW EW_QKV H qkvN
1273ret false
1274end
1275if not engEmitW4Gemv engW EW_XN get engLA2WVa L get engLA2SVa L engW EW_Z H zN
1276ret false
1277end
1278engPtr get engLA4WVa L
1279engPtr engW EW_XN
1280engPtr engW EW_AB
1281engArg uint H
1282engArg uint mul nV 2
1283if not engPktEmitGy 4 uint div add mul nV 2 7 8 1 256
1284ret false
1285end
1286engPtr engW EW_QKV
1287engPtr get engLA4SVa L
1288engPtr add get engGdnTailVa 0 mul gdnIdx mul qkvN 12
1289if not engPktEmitGy 13 uint div qkvN 128 1 128
1290ret false
1291end
1292engPtr engW EW_QKV
1293engPtr engW EW_Z
1294engPtr engW EW_AB
1295engPtr get engLKNormVa L
1296engPtr add get engGdnStateVa 0 mul gdnIdx mul nV 65536
1297engPtr get engLQNormVa L
1298engPtr engW EW_Y
1299if not engPktEmitGy 14 uint nV 1 128
1300ret false
1301end
1302ret engEmitW4Gemv engW EW_Y get engLA3WVa L get engLA3SVa L engW EW_AOUT zN H
1303end
1304
1305fn engEmitMlpLayer L ulong to bool
1306H = get engSpec MS_H
1307I = get engSpec MS_I
1308if not engEmitRmsNormAdd get engLPostNormVa L 1
1309ret false
1310end
1311if not engEmitW4Gemv engW EW_XN get engLGateWVa L get engLGateSVa L engW EW_GU H mul I 2
1312ret false
1313end
1314engPtr engW EW_GU
1315engPtr engW EW_SW
1316engArg uint I
1317if not engPktEmitGy 2 uint div I 256 1 256
1318ret false
1319end
1320ret engEmitW4Gemv engW EW_SW get engLDnWVa L get engLDnSVa L engW EW_AOUT I H
1321end
1322
1323fn engEmitAllLayers to bool
1324nL = get engSpec MS_LAYERS
1325gdnIdx = ulong 0
1326faIdx = ulong 0
1327for L ulong nL bound 128
1328doAdd = uint 1
1329if eq L 0
1330doAdd = uint 0
1331end
1332if not engEmitRmsNormAdd get engLInNormVa L doAdd
1333ret false
1334end
1335if eq get engLayerKind L 0
1336if not engEmitFaLayer L faIdx
1337ret false
1338end
1339faIdx = add faIdx 1
1340else
1341if not engEmitGdnLayer L gdnIdx
1342ret false
1343end
1344gdnIdx = add gdnIdx 1
1345end
1346if not engEmitMlpLayer L
1347ret false
1348end
1349end
1350ret true
1351end
1352
1353fn engEmitFinalHead to bool
1354H = get engSpec MS_H
1355V = get engSpec MS_VOCAB
1356if not engEmitRmsNormAdd engW EW_FNORM 1
1357ret false
1358end
1359if not engEmitW4Gemv engW EW_XN get engLmW4Va 0 add get engLmW4Va 0 get engLmW4SOff 0 engW EW_LOGW4 H V
1360ret false
1361end
1362engPtr engW EW_LOGW4
1363engPtr engW EW_CIDS
1364if not engPktEmitGy 12 64 1 256
1365ret false
1366end
1367engPtr mapaddr get engLmBf16 0
1368engPtr engW EW_XN
1369engPtr engW EW_CIDS
1370engPtr engW EW_CLOG
1371engArg 128
1372ret engPktEmitGy 5 128 1 32
1373end
1374
1375fn engEmitArgmaxEmbed to bool
1376engPtr engW EW_CIDS
1377engPtr engW EW_CLOG
1378engPtr mapaddr get engEmbBf16 0
1379engPtr engW EW_X
1380engPtr engW EW_POS
1381engPtr engW EW_OUT_TOK
1382ret engPktEmitGy 6 1 1 128
1383end
1384
1385fn engRecordPackets to bool
1386engPktStartAt EW_PKT
1387if not engEmitAllLayers
1388ret false
1389end
1390if not engEmitFinalHead
1391ret false
1392end
1393if not engPktFinish 0
1394ret false
1395end
1396engPktStartAt add EW_PKT 4194304
1397if not engEmitAllLayers
1398ret false
1399end
1400if not engPktFinish 1
1401ret false
1402end
1403engPktStartAt add EW_PKT 8388608
1404if not engEmitAllLayers
1405ret false
1406end
1407if not engEmitFinalHead
1408ret false
1409end
1410if not engEmitArgmaxEmbed
1411ret false
1412end
1413ret engPktFinish 2
1414end
1415
1416fn engSetPos pos ulong to bool
1417engPutU32Io 0 uint pos
1418ret eq mapwrite get engWork 0 EW_POS engIoBuf 0 4 4
1419end
1420
1421fn engLoadEmbed tid ulong to bool
1422H = get engSpec MS_H
1423b = mul H 2
1424if ne mapread get engEmbBf16 0 mul tid b engIoBuf 0 b long b
1425ret false
1426end
1427engWidenBuf H
1428ret eq mapwrite get engWork 0 EW_X engWideBuf 0 mul H 4 long mul H 4
1429end
1430
1431fn engReadOutTok to ulong
1432if ne mapread get engWork 0 EW_OUT_TOK engIoBuf 0 4 4
1433ret 999999
1434end
1435ret ulong engGetU32Io 0
1436end
1437
1438fn engReadBestFromCands to ulong
1439if ne mapread get engWork 0 EW_CIDS engIoBuf 0 512 512
1440ret 999999
1441end
1442for i ulong 128
1443put engCandIds i engGetU32Io mul i 4
1444end
1445if ne mapread get engWork 0 EW_CLOG engIoBuf 0 512 512
1446ret 999999
1447end
1448bestIdx = ulong 0
1449bestVal = f32 -1000000000.0f32
1450for i ulong 128
1451v = bitcast f32 engGetU32Io mul i 4
1452if gt v bestVal
1453bestVal = v
1454bestIdx = i
1455end
1456end
1457ret ulong get engCandIds bestIdx
1458end
1459
1460fn engZeroMap m long blks ulong to void
1461for z ulong blks bound 8192
1462r = mapwrite m mul z 65536 engIoBuf 0 65536
1463end
1464end
1465
1466fn engResetState to void
1467nGdn = get engSpec MS_N_GDN
1468if gt nGdn 0
1469for i ulong 65536
1470put engIoBuf i 0
1471end
1472sTot = mul nGdn mul get engSpec MS_GDN_NV 65536
1473engZeroMap get engGdnStateMap 0 div add sTot 65535 65536
1474qkvN = add mul get engSpec MS_GDN_NK 256 mul get engSpec MS_GDN_NV 128
1475tTot = mul nGdn mul qkvN 12
1476engZeroMap get engGdnTailMap 0 div add tTot 65535 65536
1477end
1478put engCurPos 0 0
1479end
1480
1481fn engGpuInitKvAndGdn to int
1482ropeBytes = mul get engSpec MS_MAX_CTX mul get engSpec MS_ROPE_DIMS 4
1483put engRopeMap 0 nvalloc ropeBytes true
1484if le get engRopeMap 0 0
1485ret 10
1486end
1487put engRopeVa 0 mapaddr get engRopeMap 0
1488nGdn = get engSpec MS_N_GDN
1489if gt nGdn 0
1490nK = get engSpec MS_GDN_NK
1491nV = get engSpec MS_GDN_NV
1492qkvN = add mul nK 256 mul nV 128
1493put engGdnStateMap 0 nvalloc mul nGdn mul nV 65536 true
1494if le get engGdnStateMap 0 0
1495ret 10
1496end
1497put engGdnStateVa 0 mapaddr get engGdnStateMap 0
1498put engGdnTailMap 0 nvalloc mul div add mul nGdn mul qkvN 12 65535 65536 65536 true
1499if le get engGdnTailMap 0 0
1500ret 10
1501end
1502put engGdnTailVa 0 mapaddr get engGdnTailMap 0
1503end
1504kvBytes = mul get engSpec MS_MAX_CTX mul get engSpec MS_NKV mul get engSpec MS_HD 8
1505put engKvSlabBytes 0 kvBytes
1506nFa = get engSpec MS_N_FA
1507for fi ulong nFa bound 64
1508km = nvalloc kvBytes true
1509if le km 0
1510ret 11
1511end
1512put engKvMap fi km
1513put engKvVa fi mapaddr km
1514end
1515put engSlabCnt 0 0
1516ret 0
1517end
1518
1519fn engGpuInit to int
1520if not gbopen 1
1521ret 2
1522end
1523if not gbbopen
1524ret 3
1525end
1526put engCodeMap 0 0
1527engEmitAll
1528csize = mul div add get engCodePos 0 4095 4096 4096
1529cm = nvalloc csize false
1530if le cm 0
1531ret 4
1532end
1533put engCodeMap 0 cm
1534put engCodeFail 0 0
1535engEmitAll
1536if ne get engCodeFail 0 0
1537ret 5
1538end
1539if not engBuildFp8Lut
1540ret 6
1541end
1542put engWork 0 nvalloc ENG_WORK_BYTES true
1543if le get engWork 0 0
1544ret 7
1545end
1546put engWorkVa 0 mapaddr get engWork 0
1547put engScratch 0 nvalloc ENG_SCRATCH_BYTES true
1548if le get engScratch 0 0
1549ret 7
1550end
1551put engScratchVa 0 mapaddr get engScratch 0
1552H = get engSpec MS_H
1553V = get engSpec MS_VOCAB
1554lmBytes = mul V mul H 2
1555put engEmbBf16 0 nvalloc lmBytes true
1556if le get engEmbBf16 0 0
1557ret 8
1558end
1559if eq get engSpec MS_TIE_EMB 1
1560put engLmBf16 0 get engEmbBf16 0
1561else
1562put engLmBf16 0 nvalloc lmBytes true
1563if le get engLmBf16 0 0
1564ret 8
1565end
1566end
1567w4LmW = div mul V H 2
1568w4LmS = mul V div H 16
1569put engLmW4SOff 0 w4LmW
1570put engLmW4 0 nvalloc mul div add add w4LmW w4LmS 4095 4096 4096 true
1571if le get engLmW4 0 0
1572ret 9
1573end
1574put engLmW4Va 0 mapaddr get engLmW4 0
1575ret engGpuInitKvAndGdn
1576end
1577
1578fn engLoadAll to int
1579H = get engSpec MS_H
1580V = get engSpec MS_VOCAB
1581if not engUploadRope
1582ret 13
1583end
1584tEmb = stFindCanon tNmEmb len tNmEmb
1585tFNorm = stFindCanon tNmFNorm len tNmFNorm
1586if or lt tEmb 0 lt tFNorm 0
1587ret 14
1588end
1589if not engStreamTensorRaw ulong tEmb get engEmbBf16 0 0
1590ret 14
1591end
1592if eq get engSpec MS_TIE_EMB 0
1593tLm = stFindCanon tNmLm len tNmLm
1594if lt tLm 0
1595ret 14
1596end
1597if not engStreamTensorRaw ulong tLm get engLmBf16 0 0
1598ret 14
1599end
1600end
1601note Load final norm into EW_FNORM
1602fdFn = stOpenFile get stFileId ulong tFNorm
1603if lt fdFn 0
1604ret 14
1605end
1606fnBytes = get stByteLen ulong tFNorm
1607if ne pread fdFn engIoBuf 0 fnBytes long get stByteOff ulong tFNorm long fnBytes
1608ret 14
1609end
1610if eq get stDtype ulong tFNorm DT_BF16
1611engWidenBuf H
1612if ne mapwrite get engWork 0 EW_FNORM engWideBuf 0 mul H 4 long mul H 4
1613ret 14
1614end
1615else
1616if ne mapwrite get engWork 0 EW_FNORM engIoBuf 0 mul H 4 long mul H 4
1617ret 14
1618end
1619end
1620note Quantize lm_head BF16 -> W4/NVFP4 on GPU
1621engPktStart
1622engPtr mapaddr get engLmBf16 0
1623engPtr get engLmW4Va 0
1624engPtr add get engLmW4Va 0 get engLmW4SOff 0
1625engArg uint H
1626engArg uint V
1627lmKid = ulong 3
1628if eq get engUseNvfp4 0 1
1629lmKid = ulong 33
1630end
1631if not engPktEmitGy lmKid uint div add V 7 8 1 256
1632ret 14
1633end
1634if not engPktRun
1635ret 14
1636end
1637nL = get engSpec MS_LAYERS
1638for L ulong nL bound 128
1639if not engLoadLayer L
1640stCloseCached
1641ret 15
1642end
1643end
1644stCloseCached
1645if not engRecordPackets
1646ret 16
1647end
1648ret 0
1649end
1650
1651fn engPrefillSeq ntok ulong to ulong
1652engResetState
1653for i ulong ntok bound 2048
1654if not engLoadEmbed ulong get engTokSeq i
1655ret 999999
1656end
1657if not engSetPos i
1658ret 999999
1659end
1660if eq add i 1 ntok
1661if not engReplayPre 0
1662ret 999999
1663end
1664else
1665if not engReplayPre 1
1666ret 999999
1667end
1668end
1669end
1670put engCurPos 0 ntok
1671ret engReadBestFromCands
1672end