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 / q36 / decode / decode_run.om

2809 lines · 57826 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note 100% Pure Seeded-Omega Zero-CUDA Real-Weight End-to-End Qwen 3.6 35B-A3B FP8 Decoder (q36-decode).
3note ZERO libcuda.so.1, ZERO libc, ZERO Python, ZERO C, ZERO Rust.
4note Loads ALL 40 real layers (layers-0..39.safetensors, ~33.6 GB) + outside.safetensors (lm_head, embed, norm)
5note directly into GB10 UVM, quantizes lm_head on-GPU to W4 block-32 (1.26 ms two-pass exact),
6note runs real 40-layer GDN + Full-Attn + 256E top-8 MoE + Arboris prefill & greedy decode on GB10 silicon!
7note Compose after:
8note gpu/nvidia/native/nvrm/context.om
9note gpu/nvidia/native/blackwell/vector.om
10note gpu/nvidia/native/blackwell/launch.om
11note gpu/nvidia/native/blackwell/batch.om
12note om/encoder/bw_enc_sm121.om
13note om/gpu/q36/emit_fast.om
14note om/gpu/q36/head/emit_head.om
15note om/gpu/q36/arboris/prefetch.om
16
17const QD_H ulong 2048
18const QD_VOCAB ulong 248320
19const QD_LAYERS ulong 40
20const QD_MAX_T ulong 131072
21const QD_OUT_DATA_OFF ulong 38080
22const QD_LM_BYTES ulong 1017118720
23const QD_EMB_OFF ulong 1017156800
24const QD_NORM_OFF ulong 2034275520
25const QD_LAYER_STRIDE ulong 844103680
26const QD_SLAB_BYTES ulong 3376414720
27const QD_SCALE_STRIDE ulong 3000000
28const QD_SCALE_BYTES ulong 120000000
29const QD_EXP_WSTRIDE ulong 3145728
30const QD_EXP_SSTRIDE ulong 768
31const QD_W4_LSTRIDE ulong 478150656
32const QD_W4_SLAB ulong 3347054592
33const W4_GU_W ulong 0
34const W4_GU_S ulong 269484032
35const W4_DN_W ulong 303169536
36const W4_DN_S ulong 437911552
37const W4_DENSE ulong 454754304
38
39note Workspace offsets inside qdWork (32 MB UVM):
40const QDW_X ulong 0
41const QDW_XN ulong 8192
42const QDW_QKV ulong 16384
43const QDW_Z ulong 49152
44const QDW_Y ulong 65536
45const QDW_AOUT ulong 81920
46const QDW_K ulong 90112
47const QDW_V ulong 98304
48const QDW_ROUT ulong 106496
49const QDW_AB ulong 110592
50const QDW_DB ulong 111616
51const QDW_WTS ulong 112640
52const QDW_SHG ulong 113664
53const QDW_IDS ulong 114176
54const QDW_EMID ulong 114688
55const QDW_EOUT ulong 163840
56const QDW_SHMID ulong 147456
57const QDW_SHOUT ulong 229376
58const QDW_ESW ulong 286720
59const QDW_ALOG ulong 327680
60const QDW_FNORM ulong 344064
61const QDW_LEXMAP ulong 352256
62const QDW_POS ulong 393216
63const QDW_TRACE ulong 401408
64const QDW_ROPE ulong 524288
65const QDW_LOGW4 ulong 1048576
66const QDW_LOGBF ulong 2097152
67const QDW_CIDS ulong 3145728
68const QDW_CLOG ulong 3162112
69const QDW_PKT ulong 4194304
70
71note GDN layer offsets inside raw layers-L.safetensors payload:
72const GDN_IN_NORM ulong 0
73const GDN_ALOG ulong 4096
74const GDN_CONV ulong 4160
75const GDN_DTBIAS ulong 69696
76const GDN_IN_A ulong 69760
77const GDN_IN_B ulong 200832
78const GDN_QKV_S ulong 331904
79const GDN_Z_S ulong 333952
80const GDN_LIN_NORM ulong 334976
81const GDN_OUT_S ulong 335232
82const GDN_EXP_S0 ulong 336256
83const GDN_GATE ulong 434560
84const GDN_SH_DN_S ulong 1483136
85const GDN_SH_GU_S ulong 1483264
86const GDN_SH_GATE ulong 1483520
87const GDN_POST_NORM ulong 1487616
88const GDN_QKV_W ulong 1491712
89const GDN_Z_W ulong 18268928
90const GDN_OUT_W ulong 26657536
91const GDN_EXP_W0 ulong 35046144
92const GDN_SH_DN_W ulong 840352512
93const GDN_SH_GU_W ulong 841401088
94const GDN_PAYLOAD ulong 843498240
95
96note Full-Attention layer offsets inside raw layers-L.safetensors payload:
97const FA_IN_NORM ulong 0
98const FA_EXP_S0 ulong 4096
99const FA_GATE ulong 102400
100const FA_SH_DN_S ulong 1150976
101const FA_SH_GU_S ulong 1151104
102const FA_SH_GATE ulong 1151360
103const FA_POST_NORM ulong 1155456
104const FA_KNORM ulong 1159552
105const FA_K_S ulong 1160064
106const FA_O_S ulong 1160192
107const FA_QNORM ulong 1161216
108const FA_Q_S ulong 1161728
109const FA_V_S ulong 1163776
110const FA_EXP_W0 ulong 1163904
111const FA_SH_DN_W ulong 806470272
112const FA_SH_GU_W ulong 807518848
113const FA_K_W ulong 809616000
114const FA_O_W ulong 810664576
115const FA_Q_W ulong 819053184
116const FA_V_W ulong 835830400
117const FA_PAYLOAD ulong 836878976
118
119global qdCodeMap long 1
120global qdCodePos ulong 1
121global qdCodeFail uint 1
122global qdEntry ulong 32
123global qdSmem ulong 32
124global qdUseW4 ulong 1
125global qdW4Map long 6
126global qdW4Va ulong 6
127global qdLut long 1
128global qdWork long 1
129global qdWorkVa ulong 1
130global qdLmBf16 long 1
131global qdLmW4 long 1
132global qdLmW4Va ulong 1
133global qdScaleMap long 1
134global qdScaleVa ulong 1
135global qdGdnStateMap long 1
136global qdGdnStateVa ulong 1
137global qdGdnTailMap long 1
138global qdGdnTailVa ulong 1
139global qdKvMap long 10
140global qdKvVa ulong 10
141global qdRopeMap long 1
142global qdRopeVa ulong 1
143global qdSlabMap long 10
144global qdSlabVa ulong 10
145global qdBind uint 32
146global qdBindN ulong 1
147global qdPktPos ulong 1
148global qdPktOff ulong 1
149global qdPreOff ulong 16
150global qdPreBytes ulong 16
151global qdDirty uint 1
152
153global qdLexRank ulong 256
154global qdIoBuf uchar 1048576
155global qdScaleOut uchar 524288
156global qdPathBuf uchar 512
157global qdDirLen ulong 1
158global qdDirBuf uchar 256
159global qdOutFd int 1
160
161global qdFinalNorm f32 2048
162global qdX f32 2048
163global qdXn f32 2048
164global qdQkv f32 8192
165global qdZ f32 4096
166global qdY f32 4096
167global qdAout f32 2048
168global qdK f32 512
169global qdV f32 512
170global qdRout f32 256
171global qdIds ulong 8
172global qdWts f32 9
173global qdEmid f32 8192
174global qdEout f32 16384
175global qdShMid f32 1024
176global qdShOut f32 2048
177global qdLogW4 f32 248320
178global qdLogBf f32 248320
179global qdCandIds uint 128
180global qdCandLog f32 128
181global qdTmpVec f32 4096
182
183note GPU-resident GDN state (60 MB UVM) + CPU-cached tiny norms/conv/scalars (0 mapread in decode!)
184global qdInNormC f32 81920
185global qdPostNormC f32 81920
186global qdShGateC f32 81920
187global qdConvC f32 983040
188global qdAlogC f32 960
189global qdDtC f32 960
190global qdQnC f32 2560
191global qdKnC f32 2560
192global qdAbTmp f32 64
193global qdDbTmp f32 64
194note GDN conv1d tail: 30 layers x 8192 x 3 f32 = 737,280 f32 (2.8 MB)
195global qdGdnTail f32 737280
196note Full-Attn KV cache: 10 layers x 128 pos x 2 kv x 256 f32 = 655,360 f32 each (5 MB total)
197global qdKCache f32 655360
198global qdVCache f32 655360
199global qdRopeFreq f32 32
200global qdScores f32 128
201
202note Live Arboris online hit counters across real Qwen 3.6 routing traces
203global qdArbHits ulong 1
204global qdArbTot ulong 1
205global qdArbPred uint 8
206global qdArbHasPred ulong 1
207global qdArbIds uint 8
208
209text qdBanner "=== QWEN 3.6 35B-A3B FP8 100% PURE SEEDED-OMEGA REAL-WEIGHT DECODER (ZERO CUDA) ===\n"
210text qdUsage "usage: q36-decode /path/to/qwen3.6-35b-a3b-fp8 [num_gen_tokens]\n"
211text qdErr "FAIL: stage="
212text qdMsgLoad "Loading real checkpoint (~34.7 GB: outside + 40 layers) into GB10 UVM...\n"
213text qdMsgLoaded "Loaded all 40 real layers + quantized W4 lm_head in ms="
214text qdMsgPrefill "Prefilling prompt tokens (len="
215text qdMsgParOk ") -> W4 Two-Pass Exact lm_head PARITY=EXACT vs Full BF16 (tok="
216text qdMsgParBad ") -> W4 Two-Pass Exact lm_head PARITY=MISMATCH (bf16="
217text qdMsgGen "Generated token IDs: "
218text qdMsgSummary "\nREAL_Q36_35B_ZERO_CUDA_DECODE: gen_tokens="
219text qdMsgAvgMs " avg_us_per_tok="
220text qdMsgTps " tok_s_x100="
221text qdMsgArb " arboris_real_hit_pct="
222text qdMsgSp " "
223text qdMsgEq " w4="
224text qdMsgRp ")\n"
225text qdMsgNl "\n"
226global qdRefOut ulong 64
227global qdSoakStats ulong 8
228text qdMsgSoak "SOAK_TURN: turn="
229text qdMsgSoakEnd "SOAK_SUMMARY: turns="
230text qdMsgTotTok " total_dec_toks="
231text qdMsgExact " exact_turns="
232text qdMsgMinUs " min_us="
233text qdMsgMaxUs " max_us="
234text qdDbgEmb "DBG pos=0 emb0="
235text qdDbgQkv " qkv0="
236text qdDbgY " y0="
237text qdDbgAout " aout0="
238text qdDbgRout " rout0="
239text qdDbgX0 " xL0="
240text qdDbgX39 " xL39="
241text qdDbgLog " log0="
242text qdSufOut "/outside.safetensors"
243text qdSufLay "/layers-"
244text qdSufSt ".safetensors"
245
246fn qdPrint s view uchar * to void
247r = write 1 s 0 len s
248end
249
250fn qdPrintU x ulong to void
251b = new uchar 24 0
252pos = ulong 24
253v = x
254while true
255pos = sub pos 1
256put b pos uchar add 48 rem v 10
257v = div v 10
258if eq v 0
259break
260end
261end
262r = write 1 b pos sub 24 pos
263end
264
265fn qdPrintHex x uint to void
266b = new uchar 10 0
267put b 0 48
268put b 1 120
269for i ulong 8
270nib = and shr x mul sub 7 i 4 15
271c = uchar add 48 nib
272if ge nib 10
273c = uchar add 87 nib
274end
275put b add 2 i c
276end
277r = write 1 b 0 10
278end
279
280fn q36Write w view ulong 4 to void
281p = get qdCodePos 0
282b = new uchar 16 0
283if gt get qdCodeMap 0 0
284for i ulong 16
285put b i uchar and shr get w div i 4 mul rem i 4 8 255
286end
287if ne mapwrite get qdCodeMap 0 p b 0 16 16
288put qdCodeFail 0 1
289end
290end
291put qdCodePos 0 add p 16
292end
293
294fn qhWrite w view ulong 4 to void
295q36Write w
296end
297
298fn qdEmitAll to void
299put qdCodePos 0 0
300note 0: bf16_gemv_fast
301p = ulong 0
302put qdEntry 0 p
303put q36Pc 0 0
304put q36Guard 0 0
305ok = q36Kernel uchar 97
306if not ok
307exit 4
308end
309q36Done
310note 1: fp8_b128_gemv_fast
311p = mul div add get qdCodePos 0 255 256 256
312put qdEntry 1 p
313put qdCodePos 0 p
314put q36Pc 0 0
315put q36Guard 0 0
316ok = q36Kernel uchar 98
317if not ok
318exit 4
319end
320q36Done
321note 2: bf16_to_w4_quant
322p = mul div add get qdCodePos 0 255 256 256
323put qdEntry 2 p
324put qdCodePos 0 p
325put qhPc 0 0
326put qhGuard 0 0
327ok = qhKernel uchar 98
328if not ok
329exit 4
330end
331qhDone
332note 3: w4_lmhead_screen_fast
333p = mul div add get qdCodePos 0 255 256 256
334put qdEntry 3 p
335put qdCodePos 0 p
336put qhPc 0 0
337put qhGuard 0 0
338ok = qhKernel uchar 99
339if not ok
340exit 4
341end
342qhDone
343note 4: bf16_rescore_cands
344p = mul div add get qdCodePos 0 255 256 256
345put qdEntry 4 p
346put qdCodePos 0 p
347put qhPc 0 0
348put qhGuard 0 0
349ok = qhKernel uchar 100
350if not ok
351exit 4
352end
353qhDone
354note 5: gdn_step_real_f32
355p = mul div add get qdCodePos 0 255 256 256
356put qdEntry 5 p
357put qdCodePos 0 p
358put q36Pc 0 0
359put q36Guard 0 0
360ok = q36Kernel uchar 110
361if not ok
362exit 4
363end
364q36Done
365note 6: swiglu9_q36_fast
366p = mul div add get qdCodePos 0 255 256 256
367put qdEntry 6 p
368put qdCodePos 0 p
369put q36Pc 0 0
370put q36Guard 0 0
371ok = q36Kernel uchar 111
372if not ok
373exit 4
374end
375q36Done
376note 7: vocab_chunk_top64
377p = mul div add get qdCodePos 0 255 256 256
378put qdEntry 7 p
379put qdCodePos 0 p
380put q36Pc 0 0
381put q36Guard 0 0
382ok = q36Kernel uchar 112
383if not ok
384exit 4
385end
386q36Done
387note 8: moe_combine9_add
388p = mul div add get qdCodePos 0 255 256 256
389put qdEntry 8 p
390put qdCodePos 0 p
391put q36Pc 0 0
392put q36Guard 0 0
393ok = q36Kernel uchar 113
394if not ok
395exit 4
396end
397q36Done
398note 9: gdn_conv_l2_fast
399p = mul div add get qdCodePos 0 255 256 256
400put qdEntry 9 p
401put qdCodePos 0 p
402put q36Pc 0 0
403put q36Guard 0 0
404ok = q36Kernel uchar 114
405if not ok
406exit 4
407end
408q36Done
409note 10: rmsnorm_add_fast
410p = mul div add get qdCodePos 0 255 256 256
411put qdEntry 10 p
412put qdCodePos 0 p
413put q36Pc 0 0
414put q36Guard 0 0
415ok = q36Kernel uchar 115
416if not ok
417exit 4
418end
419q36Done
420note 11: gdn_decay_beta_fast
421p = mul div add get qdCodePos 0 255 256 256
422put qdEntry 11 p
423put qdCodePos 0 p
424put q36Pc 0 0
425put q36Guard 0 0
426ok = q36Kernel uchar 116
427if not ok
428exit 4
429end
430q36Done
431note 12: fp8_b128_grouped8_strided
432p = mul div add get qdCodePos 0 255 256 256
433put qdEntry 12 p
434put qdCodePos 0 p
435put q36Pc 0 0
436put q36Guard 0 0
437ok = q36Kernel uchar 117
438if not ok
439exit 4
440end
441q36Done
442note 13: router_top8_lex
443p = mul div add get qdCodePos 0 255 256 256
444put qdEntry 13 p
445put qdCodePos 0 p
446put q36Pc 0 0
447put q36Guard 0 0
448ok = q36Kernel uchar 118
449if not ok
450exit 4
451end
452q36Done
453note 14: fa_prep_kv_gpu
454p = mul div add get qdCodePos 0 255 256 256
455put qdEntry 14 p
456put qdCodePos 0 p
457put q36Pc 0 0
458put q36Guard 0 0
459ok = q36Kernel uchar 119
460if not ok
461exit 4
462end
463q36Done
464note 15: fa_attend_gpu
465p = mul div add get qdCodePos 0 255 256 256
466put qdEntry 15 p
467put qdCodePos 0 p
468put q36Pc 0 0
469put q36Guard 0 0
470ok = q36Kernel uchar 120
471if not ok
472exit 4
473end
474q36Done
475note 16: w4_b32_grouped8_fast
476p = mul div add get qdCodePos 0 255 256 256
477put qdEntry 16 p
478put qdCodePos 0 p
479put q36Pc 0 0
480put q36Guard 0 0
481ok = q36Kernel uchar 108
482if not ok
483exit 4
484end
485q36Done
486note 17: w4_b32_gemv_fast
487p = mul div add get qdCodePos 0 255 256 256
488put qdEntry 17 p
489put qdCodePos 0 p
490put q36Pc 0 0
491put q36Guard 0 0
492ok = q36Kernel uchar 109
493if not ok
494exit 4
495end
496q36Done
497note 18: fp8_to_w4_b32_quant
498p = mul div add get qdCodePos 0 255 256 256
499put qdEntry 18 p
500put qdCodePos 0 p
501put q36Pc 0 0
502put q36Guard 0 0
503ok = q36Kernel uchar 121
504if not ok
505exit 4
506end
507q36Done
508note 19: w4_b32_gemv_4r
509p = mul div add get qdCodePos 0 255 256 256
510put qdEntry 19 p
511put qdCodePos 0 p
512put q36Pc 0 0
513put q36Guard 0 0
514ok = q36Kernel uchar 122
515if not ok
516exit 4
517end
518q36Done
519note 20: w4_b32_grouped9_4r
520p = mul div add get qdCodePos 0 255 256 256
521put qdEntry 20 p
522put qdCodePos 0 p
523put q36Pc 0 0
524put q36Guard 0 0
525ok = q36Kernel uchar 123
526if not ok
527exit 4
528end
529q36Done
530note 21: gdn_step_real_bf16
531p = mul div add get qdCodePos 0 255 256 256
532put qdEntry 21 p
533put qdCodePos 0 p
534put q36Pc 0 0
535put q36Guard 0 0
536ok = q36Kernel uchar 124
537if not ok
538exit 4
539end
540q36Done
541note 22: w4_b32_grouped9_dn_4r
542p = mul div add get qdCodePos 0 255 256 256
543put qdEntry 22 p
544put qdCodePos 0 p
545put q36Pc 0 0
546put q36Guard 0 0
547ok = q36Kernel uchar 125
548if not ok
549exit 4
550end
551q36Done
552note 23: moe_combine9_rms_fast
553p = mul div add get qdCodePos 0 255 256 256
554put qdEntry 23 p
555put qdCodePos 0 p
556put q36Pc 0 0
557put q36Guard 0 0
558ok = q36Kernel uchar 126
559if not ok
560exit 4
561end
562q36Done
563note 24: bf16_gemv_1r
564p = mul div add get qdCodePos 0 255 256 256
565put qdEntry 24 p
566put qdCodePos 0 p
567put q36Pc 0 0
568put q36Guard 0 0
569ok = q36Kernel uchar 127
570if not ok
571exit 4
572end
573q36Done
574put qdSmem 0 8192
575put qdSmem 1 17472
576put qdSmem 2 0
577put qdSmem 3 8192
578put qdSmem 4 0
579put qdSmem 5 1088
580put qdSmem 6 0
581put qdSmem 7 2048
582put qdSmem 8 0
583put qdSmem 9 64
584put qdSmem 10 64
585put qdSmem 11 0
586put qdSmem 12 17472
587put qdSmem 13 2048
588put qdSmem 14 1088
589put qdSmem 15 1088
590put qdSmem 16 8192
591put qdSmem 17 16384
592put qdSmem 18 1024
593put qdSmem 19 16384
594put qdSmem 20 8192
595put qdSmem 21 1088
596put qdSmem 22 2048
597put qdSmem 23 64
598put qdSmem 24 8192
599end
600
601note =========================================================================
602note Pure OM-2 FP32 math helpers (sqrt, exp, log, cos, sin, bf16)
603note =========================================================================
604fn qdSqrt x f32 to f32
605if le x 0.0f32
606ret 0.0f32
607end
608bits = bitcast uint x
609gbits = add 0x1fc00000 shr bits 1
610y = bitcast f32 gbits
611for i ulong 5
612y = mul 0.5f32 add y div x y
613end
614ret y
615end
616
617fn qdExp x f32 to f32
618v = x
619if gt v 80.0f32
620v = f32 80.0f32
621end
622if lt v neg 80.0f32
623ret 0.0f32
624end
625note x = k * ln(2) + r, |r| <= 0.35
626kf = mul v 1.44269504f32
627ki = int 0
628if ge kf 0.0f32
629ki = int add kf 0.5f32
630else
631ki = int sub kf 0.5f32
632end
633r = sub v mul f32 ki 0.69314718f32
634p = f32 1.0f32
635p = add 1.0f32 mul r mul p 0.16666667f32
636p = add 1.0f32 mul r mul p 0.2f32
637p = add 1.0f32 mul r mul p 0.25f32
638p = add 1.0f32 mul r mul p 0.33333333f32
639p = add 1.0f32 mul r mul p 0.5f32
640p = add 1.0f32 mul r p
641eb = shl uint add ki 127 23
642ret mul p bitcast f32 eb
643end
644
645fn qdLog x f32 to f32
646if le x 0.0f32
647ret neg 80.0f32
648end
649bits = bitcast uint x
650e = sub int and shr bits 23 255 127
651mbits = or and bits 0x007fffff 0x3f800000
652m = bitcast f32 mbits
653if gt m 1.41421356f32
654m = mul m 0.5f32
655e = add e 1
656end
657t = div sub m 1.0f32 add m 1.0f32
658t2 = mul t t
659s = add 0.14285714f32 mul t2 0.11111111f32
660s = add 0.2f32 mul t2 s
661s = add 0.33333333f32 mul t2 s
662s = add 1.0f32 mul t2 s
663ret add mul f32 e 0.69314718f32 mul 2.0f32 mul t s
664end
665
666fn qdSigmoid x f32 to f32
667if ge x 0.0f32
668e = qdExp neg x
669ret div 1.0f32 add 1.0f32 e
670end
671e = qdExp x
672ret div e add 1.0f32 e
673end
674
675fn qdSilu x f32 to f32
676ret mul x qdSigmoid x
677end
678
679fn qdSoftplus x f32 to f32
680if gt x 20.0f32
681ret x
682end
683if lt x neg 20.0f32
684ret qdExp x
685end
686ret qdLog add 1.0f32 qdExp x
687end
688
689fn qdSinCos theta f32 isCos bool to f32
690note Reduce theta to [-pi, pi]
691inv2pi = f32 0.15915494f32
692twopi = f32 6.28318531f32
693pi = f32 3.14159265f32
694hp = f32 1.57079633f32
695k = mul theta inv2pi
696ki = int 0
697if ge k 0.0f32
698ki = int add k 0.5f32
699else
700ki = int sub k 0.5f32
701end
702x = sub theta mul f32 ki twopi
703if isCos
704x = add x hp
705if gt x pi
706x = sub x twopi
707end
708end
709sgn = f32 1.0f32
710if gt x hp
711x = sub pi x
712end
713if lt x neg hp
714x = sub neg pi x
715end
716x2 = mul x x
717r = sub 1.0f32 mul x2 0.0001984127f32
718r = sub 1.0f32 mul x2 mul 0.0083333333f32 r
719r = sub 1.0f32 mul x2 mul 0.0416666667f32 r
720r = sub 1.0f32 mul x2 mul 0.1666666667f32 r
721ret mul sgn mul x r
722end
723
724fn qdInitRopeFreq to void
725note inv_freq[j] = exp(-2*j/64 * ln(10000000.0))
726lnTheta = f32 16.11809565f32
727for j ulong 32
728e = div mul f32 mul j 2 lnTheta 64.0f32
729put qdRopeFreq j qdExp neg e
730end
731end
732
733note Compute lexicographic rank in 0..255 for expert id e in 0..255
734fn qdLexLess a ulong b ulong to bool
735sa = new uchar 4 0
736sb = new uchar 4 0
737la = ulong 0
738lb = ulong 0
739if lt a 10
740put sa 0 uchar add 48 a
741la = ulong 1
742else
743if lt a 100
744put sa 0 uchar add 48 div a 10
745put sa 1 uchar add 48 rem a 10
746la = ulong 2
747else
748put sa 0 uchar add 48 div a 100
749put sa 1 uchar add 48 rem div a 10 10
750put sa 2 uchar add 48 rem a 10
751la = ulong 3
752end
753end
754if lt b 10
755put sb 0 uchar add 48 b
756lb = ulong 1
757else
758if lt b 100
759put sb 0 uchar add 48 div b 10
760put sb 1 uchar add 48 rem b 10
761lb = ulong 2
762else
763put sb 0 uchar add 48 div b 100
764put sb 1 uchar add 48 rem div b 10 10
765put sb 2 uchar add 48 rem b 10
766lb = ulong 3
767end
768end
769m = la
770if lt lb m
771m = lb
772end
773for i ulong m bound 4
774ca = get sa i
775cb = get sb i
776if ne ca cb
777ret lt ca cb
778end
779end
780ret lt la lb
781end
782
783fn qdInitLexRank to void
784for e ulong 256
785r = ulong 0
786for o ulong 256
787if qdLexLess o e
788r = add r 1
789end
790end
791put qdLexRank e r
792end
793end
794
795note =========================================================================
796note GPU dispatch & UVM memory helpers
797note =========================================================================
798fn qdArg u uint to void
799i = get qdBindN 0
800put qdBind i u
801put qdBindN 0 add i 1
802end
803
804fn qdPtr va ulong to void
805qdArg gblo va
806qdArg gbhi va
807end
808
809fn qdPktStartAt off ulong to void
810put qdPktOff 0 off
811put qdPktPos 0 0
812put gbused 0 0
813gbsetup
814gbmth 0 40 4
815gbemit 0
816gbemit 0
817gbemit 0
818gbemit 2281701376
819cb = mapaddr get gbmem 1
820gbdriver cb
821gbdma cb 224
822for j ulong 224
823gbemit get gbcb j
824end
825n = get gbused 0
826r = mapwrite get qdWork 0 off gbcmd 0 n
827put qdPktPos 0 n
828end
829
830fn qdPktStart to void
831qdPktStartAt add QDW_PKT 6291456
832end
833
834fn qdPrepQmdGy kid ulong gx uint gy uint bx uint to void
835gbqmd 1
836a = add mapaddr get qdCodeMap 0 get qdEntry kid
837put gbq1 32 gblo shr a 4
838put gbq1 33 or uint and shr a 36 2097151 20971520
839put gbq1 59 gblo shr a 8
840put gbq1 34 or uint 65536 bx
841put gbq1 35 or uint 131073 shl uint 64 8
842sm = get qdSmem kid
843s7 = ulong 8
844if ne sm 0
845s7 = div add sm 127 128
846end
847put gbq1 36 uint or 78923776 s7
848put gbq1 37 0
849put gbq1 39 gx
850put gbq1 40 gy
851put gbq1 41 1
852put gbcb 216 bx
853put gbcb 220 gx
854put gbcb 221 gy
855put gbcb 222 1
856end
857
858fn qdPktEmitGy kid ulong gx uint gy uint bx uint to bool
859qdPrepQmdGy kid gx gy bx
860cb = mapaddr get gbmem 1
861words = get qdBindN 0
862endva = add cb add 896 mul words 4
863put gbcb 212 gbhi endva
864put gbcb 213 gblo endva
865put gbused 0 0
866gbdma add cb 896 uint words
867for j ulong words
868gbemit get qdBind j
869end
870q = mapaddr get gbmem 2
871gbinline q false
872gbdma add q 8192 1
873gbemit 1
874gbinline add q 4096 true
875gbrel add mapaddr get gbmem 3 32 6
876put qdBindN 0 0
877n = get gbused 0
878pos = get qdPktPos 0
879if gt add pos n 2000000
880ret false
881end
882dstOff = add get qdPktOff 0 pos
883if ne mapwrite get qdWork 0 dstOff gbcmd 0 n long n
884ret false
885end
886put qdPktPos 0 add pos n
887ret true
888end
889
890fn qdPktEmitBx kid ulong gx uint bx uint to bool
891ret qdPktEmitGy kid gx 1 bx
892end
893
894fn qdPktEmit kid ulong gx uint words ulong to bool
895ret qdPktEmitGy kid gx 1 256
896end
897
898fn qdPktFinish slot ulong to bool
899put gbused 0 0
900gbtail
901n = get gbused 0
902pos = get qdPktPos 0
903dstOff = add get qdPktOff 0 pos
904if ne mapwrite get qdWork 0 dstOff gbcmd 0 n long n
905ret false
906end
907put qdPreOff slot get qdPktOff 0
908put qdPreBytes slot add pos n
909ret true
910end
911
912fn qdReplayPre slot ulong to bool
913r = mapset get gbmem 2 8192 4 0
914r = mapset get gbmem 3 0 4 0
915r = mapset get gbmem 3 16 4 0
916off = get qdPreOff slot
917nb = get qdPreBytes slot
918if not nvsend get qdWork 0 off uint div nb 4
919ret false
920end
921if not qdWaitFast 15000000000
922ret false
923end
924ret eq mapget get gbmem 3 0 4 1145324612
925end
926
927fn qdWaitFast timeout ulong to bool
928for sp ulong 50000
929if eq mapget get gbmem 3 16 4 1179010630
930put nvret 0 get nvput 0
931ret true
932end
933end
934t0 = monons
935while lt ulong sub monons t0 timeout bound 5000000
936if eq mapget get gbmem 3 16 4 1179010630
937put nvret 0 get nvput 0
938ret true
939end
940r = sleepns long 5000
941end
942ret false
943end
944
945fn qdPktRun to bool
946put gbused 0 0
947gbtail
948n = get gbused 0
949pos = get qdPktPos 0
950dstOff = add get qdPktOff 0 pos
951if ne mapwrite get qdWork 0 dstOff gbcmd 0 n long n
952ret false
953end
954tot = add pos n
955r = mapset get gbmem 2 8192 4 0
956r = mapset get gbmem 3 0 4 0
957r = mapset get gbmem 3 16 4 0
958if not nvsend get qdWork 0 get qdPktOff 0 uint div tot 4
959ret false
960end
961if not qdWaitFast 15000000000
962ret false
963end
964ret eq mapget get gbmem 3 0 4 1145324612
965end
966
967fn qdGetF32Io p ulong to f32
968b0 = uint get qdIoBuf p
969b1 = shl uint get qdIoBuf add p 1 8
970b2 = shl uint get qdIoBuf add p 2 16
971b3 = shl uint get qdIoBuf add p 3 24
972w = or or b0 b1 or b2 b3
973ret bitcast f32 w
974end
975
976fn qdStoreF32Sel dstSel ulong idx ulong f f32 to void
977if eq dstSel 0
978put qdQkv idx f
979ret
980end
981if eq dstSel 1
982put qdZ idx f
983ret
984end
985if eq dstSel 2
986put qdAout idx f
987ret
988end
989if eq dstSel 3
990put qdK idx f
991ret
992end
993if eq dstSel 4
994put qdV idx f
995ret
996end
997if eq dstSel 5
998put qdRout idx f
999ret
1000end
1001if eq dstSel 6
1002put qdEmid idx f
1003ret
1004end
1005if eq dstSel 7
1006put qdEout idx f
1007ret
1008end
1009if eq dstSel 8
1010put qdShMid idx f
1011ret
1012end
1013if eq dstSel 9
1014put qdShOut idx f
1015ret
1016end
1017if eq dstSel 10
1018put qdLogW4 idx f
1019ret
1020end
1021if eq dstSel 11
1022put qdLogBf idx f
1023ret
1024end
1025if eq dstSel 12
1026put qdCandLog idx f
1027ret
1028end
1029if eq dstSel 14
1030put qdAbTmp idx f
1031ret
1032end
1033if eq dstSel 15
1034put qdX idx f
1035ret
1036end
1037put qdTmpVec idx f
1038end
1039
1040fn qdReadF32 m long off ulong dstSel ulong n ulong to bool
1041bytes = mul n 4
1042pos = ulong 0
1043while lt pos bytes bound 1000
1044want = sub bytes pos
1045if gt want 65536
1046want = ulong 65536
1047end
1048if ne mapread m add off pos qdIoBuf 0 want long want
1049ret false
1050end
1051cnt = div want 4
1052base = div pos 4
1053for i ulong cnt bound 16384
1054f = qdGetF32Io mul i 4
1055qdStoreF32Sel dstSel add base i f
1056end
1057pos = add pos want
1058end
1059ret true
1060end
1061
1062fn qdPutF32Io p ulong f f32 to void
1063w = bitcast uint f
1064put qdIoBuf p uchar and w 255
1065put qdIoBuf add p 1 uchar and shr w 8 255
1066put qdIoBuf add p 2 uchar and shr w 16 255
1067put qdIoBuf add p 3 uchar and shr w 24 255
1068end
1069
1070fn qdLoadF32Sel srcSel ulong idx ulong to f32
1071if eq srcSel 0
1072ret get qdXn idx
1073end
1074if eq srcSel 1
1075ret get qdY idx
1076end
1077if eq srcSel 2
1078ret get qdEmid idx
1079end
1080if eq srcSel 4
1081ret get qdQkv idx
1082end
1083if eq srcSel 5
1084ret get qdDbTmp idx
1085end
1086if eq srcSel 6
1087ret get qdX idx
1088end
1089if eq srcSel 7
1090ret get qdWts idx
1091end
1092ret get qdShMid idx
1093end
1094
1095fn qdWriteF32 m long off ulong srcSel ulong n ulong to bool
1096bytes = mul n 4
1097pos = ulong 0
1098while lt pos bytes bound 1000
1099want = sub bytes pos
1100if gt want 65536
1101want = ulong 65536
1102end
1103cnt = div want 4
1104base = div pos 4
1105for i ulong cnt bound 16384
1106f = qdLoadF32Sel srcSel add base i
1107qdPutF32Io mul i 4 f
1108end
1109if ne mapwrite m add off pos qdIoBuf 0 want long want
1110ret false
1111end
1112pos = add pos want
1113end
1114ret true
1115end
1116
1117fn qdBuildFp8Lut to bool
1118m = nvalloc 4096 true
1119if le m 0
1120ret false
1121end
1122put qdLut 0 m
1123for i ulong 256
1124s = and shr i 7 1
1125e = and shr i 3 15
1126mn = and i 7
1127v = f32 0.0f32
1128if eq e 15
1129if eq mn 7
1130v = f32 0.0f32
1131else
1132v = mul f32 256.0f32 add f32 1.0f32 div f32 mn f32 8.0f32
1133end
1134else
1135if eq e 0
1136v = mul f32 0.015625f32 div f32 mn f32 8.0f32
1137else
1138pw = f32 1.0f32
1139if ge e 7
1140for k ulong sub e 7 bound 16
1141pw = mul pw f32 2.0f32
1142end
1143else
1144for k ulong sub 7 e bound 16
1145pw = mul pw f32 0.5f32
1146end
1147end
1148v = mul pw add f32 1.0f32 div f32 mn f32 8.0f32
1149end
1150end
1151if eq s 1
1152v = neg v
1153end
1154w = bitcast uint v
1155p = mul i 4
1156put qdIoBuf p uchar and w 255
1157put qdIoBuf add p 1 uchar and shr w 8 255
1158put qdIoBuf add p 2 uchar and shr w 16 255
1159put qdIoBuf add p 3 uchar and shr w 24 255
1160end
1161ret eq mapwrite m 0 qdIoBuf 0 1024 1024
1162end
1163
1164note =========================================================================
1165note Checkpoint file path builder & streaming loader
1166note =========================================================================
1167fn qdBuildPath suf view uchar * layIdx long to ulong
1168dlen = get qdDirLen 0
1169for i ulong dlen bound 256
1170put qdPathBuf i get qdDirBuf i
1171end
1172pos = dlen
1173for i ulong len suf bound 64
1174put qdPathBuf pos get suf i
1175pos = add pos 1
1176end
1177if ge layIdx 0
1178if ge layIdx 10
1179put qdPathBuf pos uchar add 48 div ulong layIdx 10
1180pos = add pos 1
1181put qdPathBuf pos uchar add 48 rem ulong layIdx 10
1182pos = add pos 1
1183else
1184put qdPathBuf pos uchar add 48 ulong layIdx
1185pos = add pos 1
1186end
1187for i ulong len qdSufSt bound 32
1188put qdPathBuf pos get qdSufSt i
1189pos = add pos 1
1190end
1191end
1192put qdPathBuf pos 0
1193ret pos
1194end
1195
1196fn qdStreamToMap fd int foff ulong m long moff ulong bytes ulong to bool
1197pos = ulong 0
1198while lt pos bytes bound 2000
1199want = sub bytes pos
1200if gt want 1048576
1201want = ulong 1048576
1202end
1203got = pread fd qdIoBuf 0 want long add foff pos
1204if le got 0
1205ret false
1206end
1207if ne mapwrite m add moff pos qdIoBuf 0 ulong got got
1208ret false
1209end
1210pos = add pos ulong got
1211end
1212ret true
1213end
1214
1215fn qdWidenBuf cnt ulong to void
1216for i ulong cnt bound 131072
1217p = mul i 2
1218q = mul i 4
1219put qdScaleOut q 0
1220put qdScaleOut add q 1 0
1221put qdScaleOut add q 2 get qdIoBuf p
1222put qdScaleOut add q 3 get qdIoBuf add p 1
1223end
1224end
1225
1226note Widen first bf16Bytes of layer L from BF16 -> F32 into qdScaleMap at L * QD_SCALE_STRIDE
1227fn qdWidenLayerScales fd int foff ulong L ulong bf16Bytes ulong to bool
1228pos = ulong 0
1229sbase = mul L QD_SCALE_STRIDE
1230while lt pos bf16Bytes bound 64
1231want = sub bf16Bytes pos
1232if gt want 262144
1233want = ulong 262144
1234end
1235got = pread fd qdIoBuf 0 want long add foff pos
1236if ne got long want
1237ret false
1238end
1239qdWidenBuf div want 2
1240outBytes = mul want 2
1241dstOff = add sbase mul pos 2
1242sm = get qdScaleMap 0
1243if ne mapwrite sm dstOff qdScaleOut 0 outBytes long outBytes
1244ret false
1245end
1246pos = add pos want
1247end
1248ret true
1249end
1250
1251fn qdLoadOutside to bool
1252n = qdBuildPath qdSufOut neg 1
1253fd = int fopen qdPathBuf 0 n 0 0
1254if lt fd 0
1255ret false
1256end
1257put qdOutFd 0 fd
1258note Read final norm.weight BF16 [2048] and upload raw F32 to QDW_FNORM (GPU adds 1+w)
1259if ne pread fd qdIoBuf 0 4096 long QD_NORM_OFF 4096
1260ret false
1261end
1262for i ulong QD_H
1263p = mul i 2
1264bits = or shl uint get qdIoBuf add p 1 24 shl uint get qdIoBuf p 16
1265put qdFinalNorm i bitcast f32 bits
1266end
1267for i ulong QD_H
1268qdPutF32Io mul i 4 get qdFinalNorm i
1269end
1270if ne mapwrite get qdWork 0 QDW_FNORM qdIoBuf 0 8192 8192
1271ret false
1272end
1273note Stream lm_head.weight BF16 (1.017 GB) into qdLmBf16
1274if not qdStreamToMap fd QD_OUT_DATA_OFF get qdLmBf16 0 0 QD_LM_BYTES
1275ret false
1276end
1277ret true
1278end
1279
1280fn qdCacheLayerFa L ulong to bool
1281faIdx = div L 4
1282b256 = mul faIdx 256
1283if not qdReadF32 get qdScaleMap 0 qdLayerSOff L FA_QNORM 13 256
1284ret false
1285end
1286for d ulong 256
1287put qdQnC add b256 d add 1.0f32 get qdTmpVec d
1288end
1289if not qdReadF32 get qdScaleMap 0 qdLayerSOff L FA_KNORM 13 256
1290ret false
1291end
1292for d ulong 256
1293put qdKnC add b256 d add 1.0f32 get qdTmpVec d
1294end
1295ret true
1296end
1297
1298fn qdCacheLayerGdn L ulong to bool
1299gdnIdx = sub L div L 4
1300b32 = mul gdnIdx 32
1301if not qdReadF32 get qdScaleMap 0 qdLayerSOff L GDN_ALOG 13 32
1302ret false
1303end
1304for h ulong 32
1305v = neg qdExp get qdTmpVec h
1306put qdAlogC add b32 h v
1307qdPutF32Io mul h 4 v
1308end
1309if ne mapwrite get qdWork 0 add QDW_ALOG mul gdnIdx 256 qdIoBuf 0 128 128
1310ret false
1311end
1312if not qdReadF32 get qdScaleMap 0 qdLayerSOff L GDN_DTBIAS 13 32
1313ret false
1314end
1315for h ulong 32
1316qdPutF32Io mul h 4 get qdTmpVec h
1317end
1318if ne mapwrite get qdWork 0 add add QDW_ALOG mul gdnIdx 256 128 qdIoBuf 0 128 128
1319ret false
1320end
1321ret true
1322end
1323
1324fn qdCacheLayerCpu L ulong isFa bool to bool
1325b2048 = mul L QD_H
1326if not qdReadF32 get qdScaleMap 0 qdLayerSOff L 0 13 QD_H
1327ret false
1328end
1329for d ulong QD_H
1330put qdInNormC add b2048 d add 1.0f32 get qdTmpVec d
1331end
1332pnOff = GDN_POST_NORM
1333sgOff = GDN_SH_GATE
1334if isFa
1335pnOff = FA_POST_NORM
1336sgOff = FA_SH_GATE
1337end
1338if not qdReadF32 get qdScaleMap 0 qdLayerSOff L pnOff 13 QD_H
1339ret false
1340end
1341for d ulong QD_H
1342put qdPostNormC add b2048 d add 1.0f32 get qdTmpVec d
1343end
1344if not qdReadF32 get qdScaleMap 0 qdLayerSOff L sgOff 13 QD_H
1345ret false
1346end
1347for d ulong QD_H
1348put qdShGateC add b2048 d get qdTmpVec d
1349end
1350if isFa
1351ret qdCacheLayerFa L
1352end
1353ret qdCacheLayerGdn L
1354end
1355
1356fn qdLayerW4Va L ulong to ulong
1357ret add get qdW4Va div L 7 mul rem L 7 QD_W4_LSTRIDE
1358end
1359
1360fn qdEmitQuantW4 wFp8Va ulong sFp8Va ulong wW4Va ulong sW4Va ulong K uint N uint to bool
1361qdPtr wFp8Va
1362qdPtr sFp8Va
1363qdPtr mapaddr get qdLut 0
1364qdPtr wW4Va
1365qdPtr sW4Va
1366qdArg K
1367qdArg N
1368gx = div add N 3 4
1369ret qdPktEmitGy 18 gx 1 128
1370end
1371
1372fn qdCopyMapBytes srcMap long srcOff ulong dstMap long dstOff ulong bytes ulong to bool
1373pos = ulong 0
1374while lt pos bytes bound 64
1375want = sub bytes pos
1376if gt want 262144
1377want = ulong 262144
1378end
1379if ne mapread srcMap add srcOff pos qdIoBuf 0 want long want
1380ret false
1381end
1382if ne mapwrite dstMap add dstOff pos qdIoBuf 0 want long want
1383ret false
1384end
1385pos = add pos want
1386end
1387ret true
1388end
1389
1390fn qdQuantOneGu L ulong expW0 ulong expS0 ulong rk ulong to bool
1391fw = add get qdSlabVa 0 add expW0 add mul rk QD_EXP_WSTRIDE 1048576
1392fs = add qdLayerSVa L add mul expS0 2 add mul rk QD_EXP_SSTRIDE 256
1393dw = add qdLayerW4Va L add W4_GU_W mul rk 1048576
1394ds = add qdLayerW4Va L add W4_GU_S mul rk 131072
1395ret qdEmitQuantW4 fw fs dw ds 2048 1024
1396end
1397
1398fn qdQuantExpGuW4 L ulong expW0 ulong expS0 ulong to bool
1399for b ulong 2
1400qdPktStart
1401for k ulong 128
1402if not qdQuantOneGu L expW0 expS0 add mul b 128 k
1403ret false
1404end
1405end
1406if not qdPktRun
1407ret false
1408end
1409end
1410ret true
1411end
1412
1413fn qdQuantOneDn L ulong expW0 ulong expS0 ulong rk ulong to bool
1414fw = add get qdSlabVa 0 add expW0 mul rk QD_EXP_WSTRIDE
1415fs = add qdLayerSVa L add mul expS0 2 mul rk QD_EXP_SSTRIDE
1416dw = add qdLayerW4Va L add W4_DN_W mul rk 524288
1417ds = add qdLayerW4Va L add W4_DN_S mul rk 65536
1418ret qdEmitQuantW4 fw fs dw ds 512 2048
1419end
1420
1421fn qdQuantExpDnW4 L ulong expW0 ulong expS0 ulong to bool
1422for b ulong 2
1423qdPktStart
1424for k ulong 128
1425if not qdQuantOneDn L expW0 expS0 add mul b 128 k
1426ret false
1427end
1428end
1429if not qdPktRun
1430ret false
1431end
1432end
1433ret true
1434end
1435
1436fn qdQuantLayerExpW4 L ulong isFa bool to bool
1437expW0 = GDN_EXP_W0
1438expS0 = GDN_EXP_S0
1439if isFa
1440expW0 = FA_EXP_W0
1441expS0 = FA_EXP_S0
1442end
1443if not qdQuantExpGuW4 L expW0 expS0
1444ret false
1445end
1446ret qdQuantExpDnW4 L expW0 expS0
1447end
1448
1449fn qdQuantShGuW4 L ulong isFa bool to bool
1450guW = GDN_SH_GU_W
1451guS = GDN_SH_GU_S
1452if isFa
1453guW = FA_SH_GU_W
1454guS = FA_SH_GU_S
1455end
1456fw = add get qdSlabVa 0 guW
1457fs = add qdLayerSVa L mul guS 2
1458w4 = qdLayerW4Va L
1459dw = add w4 268435456
1460ds = add w4 add W4_GU_S 33554432
1461ret qdEmitQuantW4 fw fs dw ds 2048 1024
1462end
1463
1464fn qdQuantShDnW4 L ulong isFa bool to bool
1465dnW = GDN_SH_DN_W
1466dnS = GDN_SH_DN_S
1467if isFa
1468dnW = FA_SH_DN_W
1469dnS = FA_SH_DN_S
1470end
1471fw = add get qdSlabVa 0 dnW
1472fs = add qdLayerSVa L mul dnS 2
1473w4 = qdLayerW4Va L
1474dw = add w4 add W4_DN_W 134217728
1475ds = add w4 add W4_DN_S 16777216
1476ret qdEmitQuantW4 fw fs dw ds 512 2048
1477end
1478
1479fn qdQuantShW4 L ulong isFa bool to bool
1480if not qdQuantShGuW4 L isFa
1481ret false
1482end
1483ret qdQuantShDnW4 L isFa
1484end
1485
1486fn qdQuantDenseFaW4 L ulong to bool
1487wbase = get qdSlabVa 0
1488sbase = qdLayerSVa L
1489d4 = add qdLayerW4Va L W4_DENSE
1490dm = get qdW4Map div L 7
1491doff = add mul rem L 7 QD_W4_LSTRIDE W4_DENSE
1492qdPktStart
1493note Fused FA Q|K|V W at d4+0..9437184, S at d4+9437184..10616832
1494if not qdEmitQuantW4 add wbase FA_Q_W add sbase mul FA_Q_S 2 d4 add d4 9437184 2048 8192
1495ret false
1496end
1497if not qdEmitQuantW4 add wbase FA_K_W add sbase mul FA_K_S 2 add d4 8388608 add d4 10485760 2048 512
1498ret false
1499end
1500if not qdEmitQuantW4 add wbase FA_V_W add sbase mul FA_V_S 2 add d4 8912896 add d4 10551296 2048 512
1501ret false
1502end
1503if not qdEmitQuantW4 add wbase FA_O_W add sbase mul FA_O_S 2 add d4 10616832 add d4 14811136 4096 2048
1504ret false
1505end
1506if not qdQuantShW4 L true
1507ret false
1508end
1509if not qdPktRun
1510ret false
1511end
1512note Contiguous GATE [256,2048] + SH_GATE [1,2048] at d4+15335424
1513if not qdCopyMapBytes get qdSlabMap 0 FA_GATE dm add doff 15335424 1048576
1514ret false
1515end
1516ret qdCopyMapBytes get qdSlabMap 0 FA_SH_GATE dm add doff 16384000 4096
1517end
1518
1519fn qdQuantDenseGdnW4 L ulong to bool
1520wbase = get qdSlabVa 0
1521sbase = qdLayerSVa L
1522d4 = add qdLayerW4Va L W4_DENSE
1523dm = get qdW4Map div L 7
1524doff = add mul rem L 7 QD_W4_LSTRIDE W4_DENSE
1525qdPktStart
1526if not qdEmitQuantW4 add wbase GDN_QKV_W add sbase mul GDN_QKV_S 2 d4 add d4 12582912 2048 12288
1527ret false
1528end
1529if not qdEmitQuantW4 add wbase GDN_OUT_W add sbase mul GDN_OUT_S 2 add d4 14155776 add d4 18350080 4096 2048
1530ret false
1531end
1532if not qdQuantShW4 L false
1533ret false
1534end
1535if not qdPktRun
1536ret false
1537end
1538note Contiguous GATE [256,2048] + SH_GATE [1,2048] at d4+18874368, IN_AB at d4+19927040
1539if not qdCopyMapBytes get qdSlabMap 0 GDN_GATE dm add doff 18874368 1048576
1540ret false
1541end
1542if not qdCopyMapBytes get qdSlabMap 0 GDN_SH_GATE dm add doff 19922944 4096
1543ret false
1544end
1545ret qdCopyMapBytes get qdSlabMap 0 GDN_IN_A dm add doff 19927040 262144
1546end
1547
1548fn qdQuantLayerDenseW4 L ulong isFa bool to bool
1549if isFa
1550ret qdQuantDenseFaW4 L
1551end
1552ret qdQuantDenseGdnW4 L
1553end
1554
1555fn qdLoadLayer L ulong to bool
1556n = qdBuildPath qdSufLay long L
1557fd = int fopen qdPathBuf 0 n 0 0
1558if lt fd 0
1559ret false
1560end
1561if ne pread fd qdIoBuf 0 8 0 8
1562r = fclose fd
1563ret false
1564end
1565hdrLen = ulong 0
1566for i ulong 8
1567hdrLen = or hdrLen shl ulong get qdIoBuf i mul i 8
1568end
1569dataOff = add 8 hdrLen
1570slabIdx = div L 4
1571inSlab = rem L 4
1572moff = mul inSlab QD_LAYER_STRIDE
1573if eq get qdUseW4 0 1
1574slabIdx = ulong 0
1575moff = ulong 0
1576end
1577isFa = eq rem L 4 3
1578payload = GDN_PAYLOAD
1579bf16Top = ulong 1491712
1580if isFa
1581payload = FA_PAYLOAD
1582bf16Top = ulong 1163904
1583end
1584ok = qdStreamToMap fd dataOff get qdSlabMap slabIdx moff payload
1585if not ok
1586r = fclose fd
1587ret false
1588end
1589ok = qdWidenLayerScales fd dataOff L bf16Top
1590r = fclose fd
1591if not ok
1592ret false
1593end
1594if not qdCacheLayerCpu L isFa
1595ret false
1596end
1597if eq get qdUseW4 0 1
1598if not qdQuantLayerExpW4 L isFa
1599ret false
1600end
1601ret qdQuantLayerDenseW4 L isFa
1602end
1603ret true
1604end
1605
1606fn qdLayerWVa L ulong to ulong
1607ret add get qdSlabVa div L 4 mul rem L 4 QD_LAYER_STRIDE
1608end
1609
1610fn qdLayerSVa L ulong to ulong
1611ret add get qdScaleVa 0 mul L QD_SCALE_STRIDE
1612end
1613
1614fn qdLayerSOff L ulong bf16Off ulong to ulong
1615ret add mul L QD_SCALE_STRIDE mul bf16Off 2
1616end
1617
1618note =========================================================================
1619note GPU GEMV helpers (reusing verified bf16_gemv_fast & fp8_b128_gemv_fast)
1620note =========================================================================
1621fn qdEmitW4Gemv4R xVa ulong wVa ulong sVa ulong outVa ulong K uint N uint to bool
1622qdPtr xVa
1623qdPtr wVa
1624qdPtr sVa
1625qdPtr outVa
1626qdArg K
1627qdArg N
1628gx = div add N 31 32
1629ret qdPktEmitGy 19 gx 1 256
1630end
1631
1632fn qdEmitW4Gemv xVa ulong wVa ulong sVa ulong outVa ulong K uint N uint to bool
1633qdPtr xVa
1634qdPtr wVa
1635qdPtr sVa
1636qdPtr outVa
1637qdArg K
1638qdArg N
1639gx = div add N 31 32
1640ret qdPktEmitGy 17 gx 1 256
1641end
1642
1643fn qdEmitFp8 xVa ulong wVa ulong sVa ulong outVa ulong K uint N uint to bool
1644qdPtr xVa
1645qdPtr wVa
1646qdPtr sVa
1647qdPtr mapaddr get qdLut 0
1648qdPtr outVa
1649qdArg K
1650qdArg N
1651gx = div add N 31 32
1652ret qdPktEmit 1 gx 12
1653end
1654
1655fn qdEmitBf161R xVa ulong wVa ulong outVa ulong N uint to bool
1656qdPtr wVa
1657qdPtr xVa
1658qdPtr outVa
1659qdArg N
1660gx = div add N 7 8
1661ret qdPktEmitGy 24 gx 1 256
1662end
1663
1664fn qdEmitBf16 xVa ulong wVa ulong outVa ulong K uint N uint to bool
1665qdPtr wVa
1666qdPtr xVa
1667qdPtr outVa
1668qdArg K
1669qdArg N
1670gx = div add N 31 32
1671ret qdPktEmit 0 gx 8
1672end
1673
1674fn qdQuantLmHeadOnGpu to bool
1675qdPktStart
1676qdPtr mapaddr get qdLmBf16 0
1677qdPtr get qdLmW4Va 0
1678qdPtr add get qdLmW4Va 0 254279680
1679qdPtr add get qdLmW4Va 0 286064640
1680qdArg uint QD_VOCAB
1681if not qdPktEmitBx 2 62080 128
1682ret false
1683end
1684ret qdPktRun
1685end
1686
1687note =========================================================================
1688note CPU exact FP32 helpers (RMSNorm, GDN, Full-Attn, Router, SwiGLU)
1689note =========================================================================
1690fn qdLoadEmbed tid ulong to bool
1691off = add QD_EMB_OFF mul tid 4096
1692if ne pread get qdOutFd 0 qdIoBuf 0 4096 long off 4096
1693ret false
1694end
1695for i ulong QD_H
1696p = mul i 2
1697bits = or shl uint get qdIoBuf add p 1 24 shl uint get qdIoBuf p 16
1698put qdX i bitcast f32 bits
1699end
1700ret qdWriteF32 get qdWork 0 QDW_X 6 QD_H
1701end
1702
1703fn qdEmitRmsNormAdd normVa ulong doAdd uint to bool
1704wva = get qdWorkVa 0
1705qdPtr add wva QDW_X
1706qdPtr add wva QDW_AOUT
1707qdPtr normVa
1708qdPtr add wva QDW_XN
1709qdArg doAdd
1710ret qdPktEmitBx 10 1 256
1711end
1712
1713fn qdEmitGdnTail L ulong gdnIdx ulong to bool
1714wbase = qdLayerWVa L
1715sbase = qdLayerSVa L
1716wva = get qdWorkVa 0
1717qdPtr add wva QDW_AB
1718qdPtr add wva add QDW_ALOG mul gdnIdx 256
1719qdPtr add sbase mul GDN_DTBIAS 2
1720qdPtr add wva QDW_DB
1721if not qdPktEmitBx 11 1 32
1722ret false
1723end
1724qdPtr add wva QDW_QKV
1725qdPtr add wva QDW_Z
1726qdPtr add wva QDW_DB
1727qdPtr add get qdGdnStateVa 0 mul gdnIdx 2097152
1728qdPtr add sbase mul GDN_LIN_NORM 2
1729qdPtr add wva QDW_Y
1730if not qdPktEmitBx 5 32 128
1731ret false
1732end
1733if not qdEmitFp8 add wva QDW_Y add wbase GDN_OUT_W add sbase mul GDN_OUT_S 2 add wva QDW_AOUT 4096 2048
1734ret false
1735end
1736if not qdEmitRmsNormAdd add sbase mul GDN_POST_NORM 2 1
1737ret false
1738end
1739xnVa = add wva QDW_XN
1740if not qdEmitBf16 xnVa add wbase GDN_GATE add wva QDW_ROUT 2048 256
1741ret false
1742end
1743ret qdEmitBf16 xnVa add wbase GDN_SH_GATE add wva QDW_SHG 2048 1
1744end
1745
1746fn qdMoeEmitShGu L ulong isFa bool to bool
1747shGuW = GDN_SH_GU_W
1748shGuS = GDN_SH_GU_S
1749if isFa
1750shGuW = FA_SH_GU_W
1751shGuS = FA_SH_GU_S
1752end
1753ew = add qdLayerWVa L shGuW
1754es = add qdLayerSVa L mul shGuS 2
1755eo = add get qdWorkVa 0 QDW_SHMID
1756ret qdEmitFp8 add get qdWorkVa 0 QDW_XN ew es eo 2048 1024
1757end
1758
1759fn qdMoeEmitShDn L ulong isFa bool to bool
1760shDnW = GDN_SH_DN_W
1761shDnS = GDN_SH_DN_S
1762if isFa
1763shDnW = FA_SH_DN_W
1764shDnS = FA_SH_DN_S
1765end
1766ew = add qdLayerWVa L shDnW
1767es = add qdLayerSVa L mul shDnS 2
1768ei = add get qdWorkVa 0 add QDW_ESW 16384
1769eo = add get qdWorkVa 0 QDW_SHOUT
1770ret qdEmitFp8 ei ew es eo 512 2048
1771end
1772
1773fn qdLmCheckParity bestId ulong to bool
1774qdPktStart
1775if not qdEmitBf16 add get qdWorkVa 0 QDW_XN mapaddr get qdLmBf16 0 add get qdWorkVa 0 QDW_LOGBF uint QD_H uint QD_VOCAB
1776ret false
1777end
1778if not qdPktRun
1779ret false
1780end
1781if not qdReadF32 get qdWork 0 QDW_LOGBF 11 QD_VOCAB
1782ret false
1783end
1784bfId = ulong 0
1785bfMax = get qdLogBf 0
1786for v ulong QD_VOCAB
1787if gt get qdLogBf v bfMax
1788bfMax = get qdLogBf v
1789bfId = v
1790end
1791end
1792if eq bfId bestId
1793qdPrint qdMsgParOk
1794qdPrintU bestId
1795qdPrint qdMsgRp
1796else
1797qdPrint qdMsgParBad
1798qdPrintU bfId
1799qdPrint qdMsgEq
1800qdPrintU bestId
1801qdPrint qdMsgRp
1802end
1803ret true
1804end
1805
1806fn qdReadCandIds to bool
1807if ne mapread get qdWork 0 QDW_CIDS qdIoBuf 0 512 512
1808ret false
1809end
1810for j ulong 128
1811p = mul j 4
1812b0 = uint get qdIoBuf p
1813b1 = shl uint get qdIoBuf add p 1 8
1814b2 = shl uint get qdIoBuf add p 2 16
1815b3 = shl uint get qdIoBuf add p 3 24
1816put qdCandIds j or or b0 b1 or b2 b3
1817end
1818ret true
1819end
1820
1821fn qdEmitGpuMoe L ulong isFa bool to bool
1822wbase = qdLayerWVa L
1823sbase = qdLayerSVa L
1824wva = get qdWorkVa 0
1825expW0 = GDN_EXP_W0
1826expS0 = GDN_EXP_S0
1827if isFa
1828expW0 = FA_EXP_W0
1829expS0 = FA_EXP_S0
1830end
1831note 1. GPU router_top8_lex: writes per-layer QDW_TRACE[L] for live Arboris + QDW_WTS[9]
1832idsVa = add wva add QDW_TRACE mul L 64
1833qdPtr add wva QDW_ROUT
1834qdPtr idsVa
1835qdPtr add wva QDW_WTS
1836qdPtr add wva QDW_SHG
1837qdPtr add wva QDW_LEXMAP
1838if not qdPktEmitGy 13 1 1 256
1839ret false
1840end
1841note 2. 1-launch 8-expert FP8 grouped gate_up (Grid=(32,8,1) = 256 CTAs!)
1842qdPtr add wva QDW_XN
1843qdPtr add wbase add expW0 1048576
1844qdPtr add sbase add mul expS0 2 256
1845qdPtr idsVa
1846qdPtr mapaddr get qdLut 0
1847qdPtr add wva QDW_EMID
1848qdArg 2048
1849qdArg 1024
1850qdArg 0
1851if not qdPktEmitGy 12 32 8 256
1852ret false
1853end
1854note 3. Shared expert gate_up + fused SwiGLU9
1855if not qdMoeEmitShGu L isFa
1856ret false
1857end
1858qdPtr add wva QDW_EMID
1859qdPtr add wva QDW_ESW
1860if not qdPktEmitGy 6 18 1 256
1861ret false
1862end
1863note 4. 1-launch 8-expert FP8 grouped down (Grid=(64,8,1) = 512 CTAs!)
1864qdPtr add wva QDW_ESW
1865qdPtr add wbase expW0
1866qdPtr add sbase mul expS0 2
1867qdPtr idsVa
1868qdPtr mapaddr get qdLut 0
1869qdPtr add wva QDW_EOUT
1870qdArg 512
1871qdArg 2048
1872qdArg 512
1873if not qdPktEmitGy 12 64 8 256
1874ret false
1875end
1876note 5. Shared expert down + fused 9-expert combine + residual add into QDW_X
1877if not qdMoeEmitShDn L isFa
1878ret false
1879end
1880qdPtr add wva QDW_X
1881qdPtr add wva QDW_EOUT
1882qdPtr add wva QDW_WTS
1883ret qdPktEmitGy 8 8 1 256
1884end
1885
1886fn qdEmitGpuMoeW4 L ulong isFa bool to bool
1887w4 = qdLayerW4Va L
1888wva = get qdWorkVa 0
1889idsVa = add wva add QDW_TRACE mul L 64
1890qdPtr add wva QDW_ROUT
1891qdPtr idsVa
1892qdPtr add wva QDW_WTS
1893qdPtr add wva add QDW_ROUT 1024
1894qdPtr add wva QDW_LEXMAP
1895if not qdPktEmitGy 13 1 1 256
1896ret false
1897end
1898note 1-launch 9-expert 4-row/warp W4 gate_up (8 routed + slot 256 shared in parallel!)
1899qdPtr add wva QDW_XN
1900qdPtr add w4 W4_GU_W
1901qdPtr add w4 W4_GU_S
1902qdPtr idsVa
1903qdPtr add wva QDW_EMID
1904qdArg 2048
1905qdArg 1024
1906qdArg 0
1907if not qdPktEmitGy 20 32 9 256
1908ret false
1909end
1910note FUSED SwiGLU + zero-loop 4-row/half-warp W4 down_proj (-40 swiglu9 launches!)
1911qdPtr add wva QDW_EMID
1912qdPtr add w4 W4_DN_W
1913qdPtr add w4 W4_DN_S
1914qdPtr idsVa
1915qdPtr add wva QDW_EOUT
1916if not qdPktEmitGy 22 32 9 256
1917ret false
1918end
1919note FUSED 9-expert combine + residual add + NEXT-LAYER RMSNorm (-40 rmsnorm launches!)
1920nextNorm = add wva QDW_FNORM
1921if lt add L 1 QD_LAYERS
1922nextNorm = qdLayerSVa add L 1
1923end
1924qdPtr add wva QDW_X
1925qdPtr add wva QDW_EOUT
1926qdPtr add wva QDW_WTS
1927qdPtr nextNorm
1928qdPtr add wva QDW_XN
1929qdArg 1
1930ret qdPktEmitGy 23 1 1 256
1931end
1932
1933fn qdEmitGdnLayerW4 L ulong gdnIdx ulong to bool
1934d4 = add qdLayerW4Va L W4_DENSE
1935sbase = qdLayerSVa L
1936wva = get qdWorkVa 0
1937xnVa = add wva QDW_XN
1938if eq L 0
1939if not qdEmitRmsNormAdd sbase 0
1940ret false
1941end
1942end
1943if not qdEmitW4Gemv4R xnVa d4 add d4 12582912 add wva QDW_QKV 2048 12288
1944ret false
1945end
1946if not qdEmitBf161R xnVa add d4 19927040 add wva QDW_AB 64
1947ret false
1948end
1949qdPtr add wva QDW_QKV
1950qdPtr add sbase mul GDN_CONV 2
1951qdPtr add get qdGdnTailVa 0 mul gdnIdx 98304
1952if not qdPktEmitGy 9 64 1 128
1953ret false
1954end
1955note Fused decay/beta + BF16 GDN state (64 iters, -63 MB/tok, -30 launches!) + Gated RMSNorm
1956qdPtr add wva QDW_QKV
1957qdPtr add wva QDW_Z
1958qdPtr add wva QDW_AB
1959qdPtr add wva add QDW_ALOG mul gdnIdx 256
1960qdPtr add get qdGdnStateVa 0 mul gdnIdx 2097152
1961qdPtr add sbase mul GDN_LIN_NORM 2
1962qdPtr add wva QDW_Y
1963if not qdPktEmitGy 21 32 1 128
1964ret false
1965end
1966if not qdEmitW4Gemv4R add wva QDW_Y add d4 14155776 add d4 18350080 add wva QDW_AOUT 4096 2048
1967ret false
1968end
1969if not qdEmitRmsNormAdd add sbase mul GDN_POST_NORM 2 1
1970ret false
1971end
1972note Fused high-occupancy 33-CTA router [256,2048] + sh_gate [1,2048] in ONE launch!
1973if not qdEmitBf161R xnVa add d4 18874368 add wva QDW_ROUT 257
1974ret false
1975end
1976ret qdEmitGpuMoeW4 L false
1977end
1978
1979fn qdEmitFaLayerW4 L ulong faIdx ulong to bool
1980d4 = add qdLayerW4Va L W4_DENSE
1981sbase = qdLayerSVa L
1982wva = get qdWorkVa 0
1983xnVa = add wva QDW_XN
1984if eq L 0
1985if not qdEmitRmsNormAdd sbase 0
1986ret false
1987end
1988end
1989note Fused FA Q[8192] | K[512] | V[512] in ONE 4-row/warp W4 launch!
1990if not qdEmitW4Gemv4R xnVa d4 add d4 9437184 add wva QDW_QKV 2048 9216
1991ret false
1992end
1993kBase = get qdKvVa faIdx
1994vBase = add kBase 268435456
1995qdPtr add wva add QDW_QKV 32768
1996qdPtr add wva add QDW_QKV 34816
1997qdPtr add sbase mul FA_KNORM 2
1998qdPtr get qdRopeVa 0
1999qdPtr kBase
2000qdPtr vBase
2001qdPtr add wva QDW_POS
2002if not qdPktEmitGy 14 2 1 256
2003ret false
2004end
2005qdPtr add wva QDW_QKV
2006qdPtr add sbase mul FA_QNORM 2
2007qdPtr get qdRopeVa 0
2008qdPtr kBase
2009qdPtr vBase
2010qdPtr add wva QDW_Y
2011qdPtr add wva QDW_POS
2012if not qdPktEmitGy 15 16 1 256
2013ret false
2014end
2015if not qdEmitW4Gemv4R add wva QDW_Y add d4 10616832 add d4 14811136 add wva QDW_AOUT 4096 2048
2016ret false
2017end
2018if not qdEmitRmsNormAdd add sbase mul FA_POST_NORM 2 1
2019ret false
2020end
2021note Fused high-occupancy 33-CTA router [256,2048] + sh_gate [1,2048] in ONE launch!
2022if not qdEmitBf161R xnVa add d4 15335424 add wva QDW_ROUT 257
2023ret false
2024end
2025ret qdEmitGpuMoeW4 L true
2026end
2027
2028fn qdEmitGdnLayerFull L ulong gdnIdx ulong to bool
2029if eq get qdUseW4 0 1
2030ret qdEmitGdnLayerW4 L gdnIdx
2031end
2032wbase = qdLayerWVa L
2033sbase = qdLayerSVa L
2034wva = get qdWorkVa 0
2035xnVa = add wva QDW_XN
2036if not qdEmitRmsNormAdd sbase 0
2037ret false
2038end
2039note Fused in_proj_qkv + in_proj_z [12288, 2048] in ONE launch!
2040if not qdEmitFp8 xnVa add wbase GDN_QKV_W add sbase mul GDN_QKV_S 2 add wva QDW_QKV 2048 12288
2041ret false
2042end
2043if not qdEmitBf16 xnVa add wbase GDN_IN_A add wva QDW_AB 2048 64
2044ret false
2045end
2046qdPtr add wva QDW_QKV
2047qdPtr add sbase mul GDN_CONV 2
2048qdPtr add get qdGdnTailVa 0 mul gdnIdx 98304
2049if not qdPktEmitGy 9 64 1 128
2050ret false
2051end
2052if not qdEmitGdnTail L gdnIdx
2053ret false
2054end
2055ret qdEmitGpuMoe L false
2056end
2057
2058fn qdEmitFaHead L ulong to bool
2059wbase = qdLayerWVa L
2060sbase = qdLayerSVa L
2061wva = get qdWorkVa 0
2062xnVa = add wva QDW_XN
2063if not qdEmitRmsNormAdd sbase 0
2064ret false
2065end
2066if not qdEmitFp8 xnVa add wbase FA_Q_W add sbase mul FA_Q_S 2 add wva QDW_QKV 2048 8192
2067ret false
2068end
2069if not qdEmitFp8 xnVa add wbase FA_K_W add sbase mul FA_K_S 2 add wva QDW_K 2048 512
2070ret false
2071end
2072ret qdEmitFp8 xnVa add wbase FA_V_W add sbase mul FA_V_S 2 add wva QDW_V 2048 512
2073end
2074
2075fn qdEmitFaTailAndMoe L ulong to bool
2076wbase = qdLayerWVa L
2077sbase = qdLayerSVa L
2078wva = get qdWorkVa 0
2079xnVa = add wva QDW_XN
2080if not qdEmitFp8 add wva QDW_Y add wbase FA_O_W add sbase mul FA_O_S 2 add wva QDW_AOUT 4096 2048
2081ret false
2082end
2083if not qdEmitRmsNormAdd add sbase mul FA_POST_NORM 2 1
2084ret false
2085end
2086if not qdEmitBf16 xnVa add wbase FA_GATE add wva QDW_ROUT 2048 256
2087ret false
2088end
2089if not qdEmitBf16 xnVa add wbase FA_SH_GATE add wva QDW_SHG 2048 1
2090ret false
2091end
2092ret qdEmitGpuMoe L true
2093end
2094
2095fn qdEmitFaLayerFull L ulong faIdx ulong to bool
2096if eq get qdUseW4 0 1
2097ret qdEmitFaLayerW4 L faIdx
2098end
2099if not qdEmitFaHead L
2100ret false
2101end
2102sbase = qdLayerSVa L
2103wva = get qdWorkVa 0
2104kBase = get qdKvVa faIdx
2105vBase = add kBase 268435456
2106note GPU k_norm + partial RoPE + KV cache append
2107qdPtr add wva QDW_K
2108qdPtr add wva QDW_V
2109qdPtr add sbase mul FA_KNORM 2
2110qdPtr get qdRopeVa 0
2111qdPtr kBase
2112qdPtr vBase
2113qdPtr add wva QDW_POS
2114if not qdPktEmitGy 14 2 1 256
2115ret false
2116end
2117note GPU q_norm + partial RoPE + GQA + sigmoid(gate) -> QDW_Y
2118qdPtr add wva QDW_QKV
2119qdPtr add sbase mul FA_QNORM 2
2120qdPtr get qdRopeVa 0
2121qdPtr kBase
2122qdPtr vBase
2123qdPtr add wva QDW_Y
2124qdPtr add wva QDW_POS
2125if not qdPktEmitGy 15 16 1 256
2126ret false
2127end
2128ret qdEmitFaTailAndMoe L
2129end
2130
2131fn qdEmitFinalHead to bool
2132wva = get qdWorkVa 0
2133if eq get qdUseW4 0 0
2134if not qdEmitRmsNormAdd add wva QDW_FNORM 0
2135ret false
2136end
2137end
2138qdPtr get qdLmW4Va 0
2139qdPtr add get qdLmW4Va 0 254279680
2140qdPtr add wva QDW_XN
2141qdPtr add wva QDW_LOGW4
2142qdArg uint QD_VOCAB
2143if not qdPktEmitGy 3 7760 1 256
2144ret false
2145end
2146qdPtr add wva QDW_LOGW4
2147qdPtr add wva QDW_CIDS
2148if not qdPktEmitGy 7 64 1 256
2149ret false
2150end
2151qdPtr mapaddr get qdLmBf16 0
2152qdPtr add wva QDW_XN
2153qdPtr add wva QDW_CIDS
2154qdPtr add wva QDW_CLOG
2155qdArg 128
2156ret qdPktEmitGy 4 128 1 32
2157end
2158
2159fn qdEmitAll40Layers to bool
2160gdnIdx = ulong 0
2161faIdx = ulong 0
2162for L ulong QD_LAYERS
2163if eq rem L 4 3
2164if not qdEmitFaLayerFull L faIdx
2165ret false
2166end
2167faIdx = add faIdx 1
2168else
2169if not qdEmitGdnLayerFull L gdnIdx
2170ret false
2171end
2172gdnIdx = add gdnIdx 1
2173end
2174end
2175ret true
2176end
2177
2178fn qdRecordAllPackets to bool
2179note Slot 0: ALL 40 REAL LAYERS + FINAL NORM + W4 TWO-PASS EXACT LM_HEAD IN ONE SINGLE GPFIFO SUBMIT!!
2180qdPktStartAt QDW_PKT
2181if not qdEmitAll40Layers
2182ret false
2183end
2184if not qdEmitFinalHead
2185ret false
2186end
2187if not qdPktFinish 0
2188ret false
2189end
2190note Slot 1: ALL 40 REAL LAYERS ONLY (for prefill tokens)
2191qdPktStartAt add QDW_PKT 2097152
2192if not qdEmitAll40Layers
2193ret false
2194end
2195ret qdPktFinish 1
2196end
2197
2198fn qdUpdateArboris to void
2199if ne mapread get qdWork 0 QDW_TRACE qdIoBuf 0 2560 2560
2200ret
2201end
2202q36ArbResetToken
2203idsLocal = new uint 8 0
2204prLocal = new uint 8 0
2205hasPred = ulong 0
2206np = ulong 0
2207for L ulong QD_LAYERS
2208base = mul L 64
2209for k ulong 8
2210p = add base mul k 4
2211b0 = uint get qdIoBuf p
2212b1 = shl uint get qdIoBuf add p 1 8
2213b2 = shl uint get qdIoBuf add p 2 16
2214b3 = shl uint get qdIoBuf add p 3 24
2215put idsLocal k or or b0 b1 or b2 b3
2216end
2217if eq hasPred 1
2218h = q36ArbScorePred prLocal np idsLocal
2219put qdArbHits 0 add get qdArbHits 0 h
2220put qdArbTot 0 add get qdArbTot 0 8
2221end
2222q36ArbObserve L idsLocal
2223if lt add L 1 QD_LAYERS
2224np = q36ArbPredict L idsLocal 8 prLocal
2225hasPred = ulong 1
2226if eq np 0
2227hasPred = ulong 0
2228end
2229end
2230end
2231end
2232
2233fn qdForwardToken tid ulong pos ulong runHead bool checkParity bool to ulong
2234if not qdLoadEmbed tid
2235ret 999999
2236end
2237w = uint pos
2238put qdIoBuf 0 uchar and w 255
2239put qdIoBuf 1 uchar and shr w 8 255
2240put qdIoBuf 2 uchar and shr w 16 255
2241put qdIoBuf 3 uchar and shr w 24 255
2242if ne mapwrite get qdWork 0 QDW_POS qdIoBuf 0 4 4
2243ret 999999
2244end
2245if not runHead
2246if not qdReplayPre 1
2247ret 999999
2248end
2249qdUpdateArboris
2250ret 0
2251end
2252if not qdReplayPre 0
2253ret 999999
2254end
2255qdUpdateArboris
2256if not qdReadCandIds
2257ret 999999
2258end
2259if not qdReadF32 get qdWork 0 QDW_CLOG 12 128
2260ret 999999
2261end
2262bestId = ulong get qdCandIds 0
2263bestLog = get qdCandLog 0
2264for j ulong 128
2265if gt get qdCandLog j bestLog
2266bestLog = get qdCandLog j
2267bestId = ulong get qdCandIds j
2268end
2269end
2270if checkParity
2271if not qdLmCheckParity bestId
2272ret 999999
2273end
2274end
2275ret bestId
2276end
2277
2278fn qdInitRopeRow subP ulong curC edit f32 32 curS edit f32 32 stepC view f32 32 stepS view f32 32 to void
2279base = mul subP 256
2280for j ulong 32
2281c = get curC j
2282s = get curS j
2283qdPutF32Io add base mul j 4 c
2284qdPutF32Io add base add 128 mul j 4 s
2285dc = get stepC j
2286ds = get stepS j
2287put curC j sub mul c dc mul s ds
2288put curS j add mul s dc mul c ds
2289end
2290end
2291
2292fn qdInitRopeBlk mr long blk ulong stepC view f32 32 stepS view f32 32 to void
2293curC = new f32 32 0.0f32
2294curS = new f32 32 0.0f32
2295p0 = f32 mul blk 256
2296for j ulong 32
2297th = mul p0 get qdRopeFreq j
2298put curC j qdSinCos th true
2299put curS j qdSinCos th false
2300end
2301for subP ulong 256
2302qdInitRopeRow subP curC curS stepC stepS
2303end
2304r = mapwrite mr mul blk 65536 qdIoBuf 0 65536
2305end
2306
2307fn qdInitKvRope128k to bool
2308note Allocate 128K GPU KV cache (10 layers x 131072 pos x 2 kv x 256 f32 x 2 = 5.37 GB in 10 x 512 MB slabs)
2309for fk ulong 10
2310mk = nvalloc 536870912 true
2311if le mk 0
2312ret false
2313end
2314put qdKvMap fk mk
2315put qdKvVa fk mapaddr mk
2316end
2317note Allocate and precompute 128K RoPE cos/sin table [131072, 64] = 32 MB UVM (<150 ms via recurrence!)
2318mr = nvalloc 33554432 true
2319if le mr 0
2320ret false
2321end
2322put qdRopeMap 0 mr
2323put qdRopeVa 0 mapaddr mr
2324stepC = new f32 32 0.0f32
2325stepS = new f32 32 0.0f32
2326for j ulong 32
2327f = get qdRopeFreq j
2328put stepC j qdSinCos f true
2329put stepS j qdSinCos f false
2330end
2331for blk ulong 512
2332qdInitRopeBlk mr blk stepC stepS
2333end
2334ret true
2335end
2336
2337fn qdGpuInit to int
2338if not gbopen 1
2339ret 2
2340end
2341if not gbbopen
2342ret 3
2343end
2344put qdCodeMap 0 0
2345qdEmitAll
2346csize = mul div add get qdCodePos 0 4095 4096 4096
2347cm = nvalloc csize false
2348if le cm 0
2349ret 4
2350end
2351put qdCodeMap 0 cm
2352put qdCodeFail 0 0
2353qdEmitAll
2354if ne get qdCodeFail 0 0
2355ret 5
2356end
2357if not qdBuildFp8Lut
2358ret 6
2359end
2360put qdWork 0 nvalloc 33554432 true
2361if le get qdWork 0 0
2362ret 7
2363end
2364put qdWorkVa 0 mapaddr get qdWork 0
2365put qdLmBf16 0 nvalloc QD_LM_BYTES true
2366if le get qdLmBf16 0 0
2367ret 8
2368end
2369put qdLmW4 0 nvalloc 300000000 true
2370if le get qdLmW4 0 0
2371ret 9
2372end
2373put qdLmW4Va 0 mapaddr get qdLmW4 0
2374put qdScaleMap 0 nvalloc QD_SCALE_BYTES true
2375if le get qdScaleMap 0 0
2376ret 10
2377end
2378put qdScaleVa 0 mapaddr get qdScaleMap 0
2379put qdGdnStateMap 0 nvalloc 62914560 true
2380if le get qdGdnStateMap 0 0
2381ret 10
2382end
2383put qdGdnStateVa 0 mapaddr get qdGdnStateMap 0
2384for i ulong 65536
2385put qdIoBuf i 0
2386end
2387for z ulong 960
2388r = mapwrite get qdGdnStateMap 0 mul z 65536 qdIoBuf 0 65536
2389end
2390put qdGdnTailMap 0 nvalloc 2949120 true
2391if le get qdGdnTailMap 0 0
2392ret 10
2393end
2394put qdGdnTailVa 0 mapaddr get qdGdnTailMap 0
2395for z ulong 45
2396r = mapwrite get qdGdnTailMap 0 mul z 65536 qdIoBuf 0 65536
2397end
2398note Upload 256-entry lexRank table to QDW_LEXMAP
2399for e ulong 256
2400w = uint get qdLexRank e
2401p = mul e 4
2402put qdIoBuf p uchar and w 255
2403put qdIoBuf add p 1 uchar and shr w 8 255
2404put qdIoBuf add p 2 uchar and shr w 16 255
2405put qdIoBuf add p 3 uchar and shr w 24 255
2406end
2407r = mapwrite get qdWork 0 QDW_LEXMAP qdIoBuf 0 1024
2408if not qdInitKvRope128k
2409ret 10
2410end
2411if eq get qdUseW4 0 1
2412m0 = nvalloc QD_LAYER_STRIDE true
2413if le m0 0
2414ret 11
2415end
2416put qdSlabMap 0 m0
2417put qdSlabVa 0 mapaddr m0
2418for s ulong 6
2419mw = nvalloc QD_W4_SLAB true
2420if le mw 0
2421ret 11
2422end
2423put qdW4Map s mw
2424put qdW4Va s mapaddr mw
2425end
2426else
2427for s ulong 10
2428m = nvalloc QD_SLAB_BYTES true
2429if le m 0
2430ret 11
2431end
2432put qdSlabMap s m
2433put qdSlabVa s mapaddr m
2434end
2435end
2436ret 0
2437end
2438
2439fn qdRunPrompt genN ulong to int
2440prompt = new ulong 17 0
2441put prompt 0 248045
2442put prompt 1 846
2443put prompt 2 198
2444put prompt 3 3710
2445put prompt 4 369
2446put prompt 5 279
2447put prompt 6 6511
2448put prompt 7 314
2449put prompt 8 9338
2450put prompt 9 30
2451put prompt 10 248046
2452put prompt 11 198
2453put prompt 12 248045
2454put prompt 13 74455
2455put prompt 14 198
2456put prompt 15 248068
2457put prompt 16 248069
2458qdPrint qdMsgPrefill
2459qdPrintU 17
2460nextTok = ulong 0
2461for p ulong 17
2462isLast = eq p 16
2463nextTok = qdForwardToken get prompt p p isLast isLast
2464if eq nextTok 999999
2465qdPrint qdErr
2466qdPrintU 50
2467qdPrint qdMsgNl
2468ret 15
2469end
2470end
2471genOut = new ulong 64 0
2472put genOut 0 nextTok
2473tDec0 = monons
2474pos = ulong 17
2475for g ulong sub genN 1 bound 64
2476nextTok = qdForwardToken nextTok pos true false
2477if eq nextTok 999999
2478qdPrint qdErr
2479qdPrintU 51
2480qdPrint qdMsgNl
2481ret 16
2482end
2483put genOut add g 1 nextTok
2484pos = add pos 1
2485end
2486tDecUs = div ulong sub monons tDec0 1000
2487qdPrint qdMsgGen
2488for g ulong genN bound 64
2489qdPrintU get genOut g
2490qdPrint qdMsgSp
2491end
2492qdPrint qdMsgSummary
2493qdPrintU genN
2494qdPrint qdMsgAvgMs
2495avgUs = ulong 0
2496tpsX100 = ulong 0
2497if gt genN 1
2498avgUs = div tDecUs sub genN 1
2499if gt avgUs 0
2500tpsX100 = div 100000000 avgUs
2501end
2502end
2503qdPrintU avgUs
2504qdPrint qdMsgTps
2505qdPrintU tpsX100
2506qdPrint qdMsgArb
2507hitPct = ulong 0
2508if gt get qdArbTot 0 0
2509hitPct = div mul get qdArbHits 0 100 get qdArbTot 0
2510end
2511qdPrintU hitPct
2512qdPrint qdMsgNl
2513ret 0
2514end
2515
2516fn qdLoadAll dbgOnly bool to int
2517qdPrint qdMsgLoad
2518tLoad0 = monons
2519if not qdLoadOutside
2520qdPrint qdErr
2521qdPrintU 30
2522qdPrint qdMsgNl
2523ret 12
2524end
2525nLay = QD_LAYERS
2526if dbgOnly
2527nLay = ulong 1
2528end
2529for L ulong nLay bound 40
2530if not qdLoadLayer L
2531qdPrint qdErr
2532qdPrintU add 100 L
2533qdPrint qdMsgNl
2534ret 13
2535end
2536end
2537if not dbgOnly
2538if not qdQuantLmHeadOnGpu
2539qdPrint qdErr
2540qdPrintU 40
2541qdPrint qdMsgNl
2542ret 14
2543end
2544if not qdRecordAllPackets
2545qdPrint qdErr
2546qdPrintU 41
2547qdPrint qdMsgNl
2548ret 14
2549end
2550end
2551tLoadMs = div ulong sub monons tLoad0 1000000
2552qdPrint qdMsgLoaded
2553qdPrintU tLoadMs
2554qdPrint qdMsgNl
2555ret 0
2556end
2557
2558fn qdPrintHex4 f f32 to void
2559w = bitcast uint f
2560qdPrintU ulong w
2561qdPrint qdMsgSp
2562end
2563
2564fn qdDbgDumpIo off ulong to void
2565r = mapread get qdW4Map 0 off qdIoBuf 0 16
2566for i ulong 4
2567qdPrintU ulong bitcast uint qdGetF32Io mul i 4
2568qdPrint qdMsgSp
2569end
2570qdPrint qdMsgNl
2571end
2572
2573fn qdRunDbg to void
2574r = qdLoadEmbed 248045
2575wva = get qdWorkVa 0
2576sbase = qdLayerSVa 0
2577qdPktStart
2578r = qdEmitRmsNormAdd sbase 0
2579r = qdEmitFp8 add wva QDW_XN add get qdSlabVa 0 GDN_QKV_W add sbase mul GDN_QKV_S 2 add wva QDW_QKV 2048 12288
2580r = qdPktRun
2581r = qdReadF32 get qdWork 0 QDW_QKV 4 8
2582for i ulong 4
2583qdPrintHex4 get qdQkv i
2584end
2585qdPrint qdMsgNl
2586if eq get qdUseW4 0 1
2587d4 = add qdLayerW4Va 0 W4_DENSE
2588qdPktStart
2589r = qdEmitW4Gemv add wva QDW_XN d4 add d4 12582912 add wva QDW_QKV 2048 12288
2590r = qdPktRun
2591r = qdReadF32 get qdWork 0 QDW_QKV 4 8
2592for i ulong 4
2593qdPrintHex4 get qdQkv i
2594end
2595qdPrint qdMsgNl
2596qdDbgDumpIo W4_DENSE
2597qdDbgDumpIo add W4_DENSE 12582912
2598end
2599end
2600
2601fn qdResetState to void
2602for i ulong 65536
2603put qdIoBuf i 0
2604end
2605for z ulong 960
2606r = mapwrite get qdGdnStateMap 0 mul z 65536 qdIoBuf 0 65536
2607end
2608for z ulong 45
2609r = mapwrite get qdGdnTailMap 0 mul z 65536 qdIoBuf 0 65536
2610end
2611end
2612
2613fn qdRunOneTurn genN ulong turnIdx ulong to int
2614prompt = new ulong 17 0
2615put prompt 0 248045
2616put prompt 1 846
2617put prompt 2 198
2618put prompt 3 3710
2619put prompt 4 369
2620put prompt 5 279
2621put prompt 6 6511
2622put prompt 7 314
2623put prompt 8 9338
2624put prompt 9 30
2625put prompt 10 248046
2626put prompt 11 198
2627put prompt 12 248045
2628put prompt 13 74455
2629put prompt 14 198
2630put prompt 15 248068
2631put prompt 16 248069
2632if gt turnIdx 0
2633qdResetState
2634end
2635nextTok = ulong 0
2636for p ulong 17
2637isLast = eq p 16
2638nextTok = qdForwardToken get prompt p p isLast false
2639if eq nextTok 999999
2640ret 15
2641end
2642end
2643exact = ulong 1
2644if eq turnIdx 0
2645put qdRefOut 0 nextTok
2646else
2647if ne nextTok get qdRefOut 0
2648exact = ulong 0
2649end
2650end
2651pos = ulong 17
2652tTurn0 = monons
2653for g ulong sub genN 1 bound 64
2654t0 = monons
2655nextTok = qdForwardToken nextTok pos true false
2656if eq nextTok 999999
2657ret 16
2658end
2659dtUs = div ulong sub monons t0 1000
2660if lt dtUs get qdSoakStats 1
2661put qdSoakStats 1 dtUs
2662end
2663if gt dtUs get qdSoakStats 2
2664put qdSoakStats 2 dtUs
2665end
2666put qdSoakStats 3 add get qdSoakStats 3 dtUs
2667put qdSoakStats 4 add get qdSoakStats 4 1
2668if eq turnIdx 0
2669put qdRefOut add g 1 nextTok
2670else
2671if ne nextTok get qdRefOut add g 1
2672exact = ulong 0
2673end
2674end
2675pos = add pos 1
2676end
2677tTurnUs = div ulong sub monons tTurn0 1000
2678put qdSoakStats 0 add get qdSoakStats 0 exact
2679qdPrint qdMsgSoak
2680qdPrintU turnIdx
2681qdPrint qdMsgAvgMs
2682qdPrintU div tTurnUs sub genN 1
2683qdPrint qdMsgExact
2684qdPrintU exact
2685qdPrint qdMsgNl
2686ret 0
2687end
2688
2689fn qdRunSoak genN ulong repeats ulong to int
2690put qdSoakStats 0 0
2691put qdSoakStats 1 999999999
2692put qdSoakStats 2 0
2693put qdSoakStats 3 0
2694put qdSoakStats 4 0
2695for t ulong repeats bound 200
2696rc = qdRunOneTurn genN t
2697if ne rc 0
2698ret rc
2699end
2700end
2701totTok = get qdSoakStats 4
2702totUs = get qdSoakStats 3
2703avgUs = ulong 0
2704tps = ulong 0
2705if gt totTok 0
2706avgUs = div totUs totTok
2707if gt avgUs 0
2708tps = div 100000000 avgUs
2709end
2710end
2711qdPrint qdMsgSoakEnd
2712qdPrintU repeats
2713qdPrint qdMsgTotTok
2714qdPrintU totTok
2715qdPrint qdMsgExact
2716qdPrintU get qdSoakStats 0
2717qdPrint qdMsgMinUs
2718qdPrintU get qdSoakStats 1
2719qdPrint qdMsgAvgMs
2720qdPrintU avgUs
2721qdPrint qdMsgMaxUs
2722qdPrintU get qdSoakStats 2
2723qdPrint qdMsgTps
2724qdPrintU tps
2725qdPrint qdMsgNl
2726ret 0
2727end
2728
2729fn qdParseArgU idx ulong to ulong
2730nb = new uchar 16 0
2731nl = argread idx nb 0 15
2732if le nl 0
2733ret 0
2734end
2735v = ulong 0
2736for i ulong ulong nl bound 16
2737c = get nb i
2738if and ge c 48 le c 57
2739v = add mul v 10 ulong sub c 48
2740end
2741end
2742ret v
2743end
2744
2745fn main to int
2746qdPrint qdBanner
2747if lt argcnt 2
2748qdPrint qdUsage
2749ret 1
2750end
2751dlen = argread 1 qdDirBuf 0 250
2752if le dlen 0
2753ret 1
2754end
2755put qdDirLen 0 ulong dlen
2756genN = ulong 8
2757if ge argcnt 3
2758v = qdParseArgU 2
2759if and gt v 0 le v 99
2760genN = v
2761end
2762end
2763put qdUseW4 0 0
2764wb = new uchar 16 0
2765if ge argcnt 4
2766wl = argread 3 wb 0 15
2767if and ge wl 2 eq get wb 0 119
2768put qdUseW4 0 1
2769end
2770end
2771repeats = ulong 1
2772if ge argcnt 5
2773rv = qdParseArgU 4
2774if and gt rv 1 le rv 200
2775repeats = rv
2776end
2777end
2778qdInitRopeFreq
2779qdInitLexRank
2780q36ArbResetAll
2781irc = qdGpuInit
2782if ne irc 0
2783qdPrint qdErr
2784qdPrintU ulong irc
2785qdPrint qdMsgNl
2786ret irc
2787end
2788dbgOnly = eq genN 99
2789lrc = qdLoadAll dbgOnly
2790if ne lrc 0
2791rc = nvstop
2792ret lrc
2793end
2794if dbgOnly
2795qdRunDbg
2796r = fclose get qdOutFd 0
2797rc = nvstop
2798ret 0
2799end
2800prc = int 0
2801if gt repeats 1
2802prc = qdRunSoak genN repeats
2803else
2804prc = qdRunPrompt genN
2805end
2806r = fclose get qdOutFd 0
2807rc = nvstop
2808ret prc
2809end