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_qwen4_b8.om

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