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 / bonsai.om

1607 lines · 37870 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note om/gpu/engine/bonsai.om: Pure Seeded-OM-2 Zero-CUDA Full-APU Ternary Bonsai 2.5 27B Engine!
3note Native 1.750000 bpw 896-Byte 4-Row Tiled PTQ1_0 + 64-Row/CTA Overlapped Double-Stream GEMV
4note + 1024-Block Walsh-Hadamard + BF16-State Grouped GDN + Uncached Host-Coherent APU Shm
5note + Full GB10 APU (20 ARMv9 Cores + 48 Blackwell SMs = 6,164 Cores Simultaneously)!
6
7const BW_XN_ROT ulong 98304
8const BW_Y_ROT ulong 950272
9const BW_SW_ROT ulong 1785856
10const BW_PARTS ulong 2039808
11const BW_SIGN5120 ulong 2113536
12const BW_SIGN6144 ulong 2146304
13const BW_SIGN17408 ulong 2179072
14
15note Uncached host-coherent bonApuShm (64 KiB) layout:
16note 0..20479 : xn_rot[5120] F32 (GPU -> 20 CPU workers)
17note 32768..32775 : gpu_best (val f32, idx u32)
18note 32784..32787 : pos u32 (CPU -> GPU FA kernels, uncached coherent!)
19note 40960..61439 : x_init[5120] F32 (CPU embed -> GPU EW_X, uncached coherent!)
20const APU_SHM_XROT ulong 0
21const APU_SHM_BEST ulong 32768
22const APU_SHM_POS ulong 32800
23const APU_SHM_MTP_POS ulong 32832
24const APU_SHM_XROT_M4 ulong 131072
25const APU_SHM_XINIT ulong 40960
26
27note Per-layer single-VA 896-B tiled PTQ1_0 pointers (100% 1.750000 bpw!)
28global bonA1T ulong 128
29global bonOT ulong 128
30global bonGuT ulong 128
31global bonDnT ulong 128
32
33global bonLmT ulong 1
34global bonLmNAll ulong 1
35global bonLmNGpu ulong 1
36global bonLmNCpu ulong 1
37global bonUseApu ulong 1
38
39global bonApuShm long 1
40global bonApuShmVa ulong 1
41
42global bonEmbRawOff ulong 1
43global bonLmRawOff ulong 1
44global bonEmbRowBuf uchar 1280
45global bonEmbF32 f32 5120
46
47note 4-row-group tiled loader buffers (up to 64 row-groups = 256 rows per I/O batch -> ~1 MB!)
48global bonRaw4Buf uchar 1048576
49global bonTileOut uchar 1048576
50
51note 20-core persistent Full-APU CPU worker pool (10x X925 + 10x A725)
52global bonApuReady ulong 1
53global bonPids long 20
54global bonGoFds int 20
55global bonResFds int 20
56global bonPipeMsg uchar 32
57global bonCpuAcc4 f32 4
58global bonCpuBestV f32 4
59global bonCpuBestI ulong 4
60global bonCpuScaleBuf f32 262144
61
62text gNmEmb "token_embd.weight"
63text gNmOut "output.weight"
64text gNmFNorm "output_norm.weight"
65text gSufInNorm "attn_norm.weight"
66text gSufPostNorm "post_attention_norm.weight"
67text gSufQ "attn_q.weight"
68text gSufK "attn_k.weight"
69text gSufV "attn_v.weight"
70text gSufO "attn_output.weight"
71text gSufQNorm "attn_q_norm.weight"
72text gSufKNorm "attn_k_norm.weight"
73text gSufQkv "attn_qkv.weight"
74text gSufZ "attn_gate.weight"
75text gSufSsmOut "ssm_out.weight"
76text gSufConv "ssm_conv1d.weight"
77text gSufSsmA "ssm_a"
78text gSufSsmDt "ssm_dt.bias"
79text gSufSsmNorm "ssm_norm.weight"
80text gSufAlpha "ssm_alpha.weight"
81text gSufBeta "ssm_beta.weight"
82text gSufGate "ffn_gate.weight"
83text gSufUp "ffn_up.weight"
84text gSufDn "ffn_down.weight"
85
86text bonMsgMode "quant_format : PTQ1_0 896B-Tiled (1.75 bpw) + Hadamard-1024 + BF16-GDN + Full-APU (20 CPU + 48 SM)\n"
87text bonMsgApu "Full-APU split : GPU_48SM_rows="
88text bonMsgApuCpu " CPU_20CORE_rows="
89text bonMsgMtp "mtp_draft_layer : "
90text bonDbgS "DBG "
91text bonDbgEq "="
92text bonDbgSp " "
93text bonDbgNl "\n"
94
95note bonCurMap/bonCurOff/bonCurVa/bonSlabAllocMap defined in bonsai_nvfp4.om
96
97fn bonLoadF32 tid ulong to ulong
98n = mul get ggDim0 tid get ggDim1 tid
99nb = mul n 4
100if not bonSlabAllocMap nb
101ret 0
102end
103dm = get bonCurMap 0
104doff = get bonCurOff 0
105va = get bonCurVa 0
106foff = add get ggDataStart 0 get ggTOff tid
107fd = get ggFd 0
108done = ulong 0
109while lt done nb bound 4096
110ch = sub nb done
111if gt ch 1048576
112ch = ulong 1048576
113end
114if ne pread fd engIoBuf 0 ch long add foff done long ch
115ret 0
116end
117if ne mapwrite dm add doff done engIoBuf 0 ch long ch
118ret 0
119end
120done = add done ch
121end
122ret va
123end
124
125fn bonPackF32ToBf16 elems ulong to void
126for i ulong elems bound 524288
127u = engGetU32Io mul i 4
128r = add u add 32767 and shr u 16 1
129bf = shr r 16
130put engWideBuf mul i 2 uchar and bf 255
131put engWideBuf add mul i 2 1 uchar and shr bf 8 255
132end
133end
134
135fn bonLoadGdnAbPart tid ulong dm long dstOff ulong elems ulong to bool
136f32B = mul elems 4
137bfB = mul elems 2
138foff = add get ggDataStart 0 get ggTOff tid
139if ne pread get ggFd 0 engIoBuf 0 f32B long foff long f32B
140ret false
141end
142bonPackF32ToBf16 elems
143ret eq mapwrite dm dstOff engWideBuf 0 bfB long bfB
144end
145
146fn bonLoadGdnAbBf16 L ulong to ulong
147tA = ggFindLayer L gSufAlpha
148tB = ggFindLayer L gSufBeta
149if or lt tA 0 lt tB 0
150ret 0
151end
152elems = mul get engSpec MS_GDN_NV get engSpec MS_H
153bfB = mul elems 2
154if not bonSlabAllocMap mul bfB 2
155ret 0
156end
157dm = get bonCurMap 0
158doff = get bonCurOff 0
159va = get bonCurVa 0
160if not bonLoadGdnAbPart ulong tA dm doff elems
161ret 0
162end
163if not bonLoadGdnAbPart ulong tB dm add doff bfB elems
164ret 0
165end
166ret va
167end
168
169fn bonCopySmall tid ulong dm long dstOff ulong nb ulong to bool
170foff = add get ggDataStart 0 get ggTOff tid
171got = pread get ggFd 0 engIoBuf 0 nb long foff
172if ne got long nb
173ret false
174end
175ret eq mapwrite dm dstOff engIoBuf 0 nb long nb
176end
177
178fn bonLoadGdnAlogDt L ulong to ulong
179tA = ggFindLayer L gSufSsmA
180tD = ggFindLayer L gSufSsmDt
181if or lt tA 0 lt tD 0
182ret 0
183end
184nb = mul get engSpec MS_GDN_NV 4
185if not bonSlabAllocMap mul nb 2
186ret 0
187end
188dm = get bonCurMap 0
189doff = get bonCurOff 0
190va = get bonCurVa 0
191if not bonCopySmall ulong tA dm doff nb
192ret 0
193end
194if not bonCopySmall ulong tD dm add doff nb nb
195ret 0
196end
197ret va
198end
199
200note =========================================================================
201note 896-Byte 4-Row Contiguous Tile PTQ1_0 Loader!
202note Packs every 4 rows x 8 blocks (4 x 1024 w = 896 B) into:
203note [0..511] : 32 lanes x 16 B (W_a -> 512 B coalesced LDG.E.128!)
204note [512..767] : 32 lanes x 8 B (W_b -> 256 B coalesced LDG.E.64!)
205note [768..895] : 32 lanes x 4 B (W_c -> 128 B coalesced LDG.E.32!)
206note =========================================================================
207global bonTDstMap long 1
208global bonTDstOff ulong 1
209
210fn bonCopyBlk28 src ulong tBase ulong lane ulong to void
211da = add tBase mul lane 16
212db = add add tBase 512 mul lane 8
213dc = add add tBase 768 mul lane 4
214for j ulong 16
215put bonTileOut add da j get bonRaw4Buf add src j
216end
217for j ulong 8
218put bonTileOut add db j get bonRaw4Buf add add src 16 j
219end
220for j ulong 4
221put bonTileOut add dc j get bonRaw4Buf add add src 24 j
222end
223end
224
225fn bonPackChunk4 rgRawOff ulong tBase ulong cByte ulong rowBytes ulong to void
226for sr ulong 4
227rOff = add add rgRawOff mul sr rowBytes cByte
228for blk ulong 8
229bonCopyBlk28 add rOff mul blk 28 tBase add mul sr 8 blk
230end
231end
232end
233
234fn bonPackOneTile4 rgRawOff ulong rgTileOff ulong nChunks ulong to void
235rowBytes = mul nChunks 224
236for c ulong nChunks bound 64
237bonPackChunk4 rgRawOff add rgTileOff mul c 896 mul c 224 rowBytes
238end
239end
240
241fn bonTileBatch foff ulong dstOff ulong cnt ulong nChunks ulong to bool
242rgBytes = mul nChunks 896
243nb = mul cnt rgBytes
244if ne pread get ggFd 0 bonRaw4Buf 0 nb long foff long nb
245ret false
246end
247for g ulong cnt bound 256
248bonPackOneTile4 mul g rgBytes mul g rgBytes nChunks
249end
250ret eq mapwrite get bonTDstMap 0 dstOff bonTileOut 0 nb long nb
251end
252
253fn bonStreamTiledRange tid ulong nRows ulong byteOff ulong to bool
254nChunks = div get ggDim0 tid 1024
255rgBytes = mul nChunks 896
256nRg = div nRows 4
257batchRg = div 1048576 rgBytes
258if eq batchRg 0
259batchRg = ulong 1
260end
261foff = add get ggDataStart 0 get ggTOff tid
262dstBase = add get bonTDstOff 0 byteOff
263done = ulong 0
264while lt done nRg bound 100000
265cnt = sub nRg done
266if gt cnt batchRg
267cnt = batchRg
268end
269if not bonTileBatch add foff mul done rgBytes add dstBase mul done rgBytes cnt nChunks
270ret false
271end
272done = add done cnt
273end
274ret true
275end
276
277fn bonAllocTiled totBytes ulong to bool
278if not bonSlabAllocMap totBytes
279ret false
280end
281put bonTDstMap 0 get bonCurMap 0
282put bonTDstOff 0 get bonCurOff 0
283put bonTResVa 0 get bonCurVa 0
284ret true
285end
286
287fn bonLoadTiled1 tid ulong to bool
288if eq get bonUseNvfp4 0 1
289ret bonLoadNvfp4_1 tid
290end
291nb = mul div mul get ggDim0 tid get ggDim1 tid 128 28
292if not bonAllocTiled nb
293ret false
294end
295ret bonStreamTiledRange tid get ggDim1 tid 0
296end
297
298fn bonLoadTiled2 t0 ulong t1 ulong to bool
299if eq get bonUseNvfp4 0 1
300ret bonLoadNvfp4_2 t0 t1
301end
302b0 = mul div mul get ggDim0 t0 get ggDim1 t0 128 28
303b1 = mul div mul get ggDim0 t1 get ggDim1 t1 128 28
304if not bonAllocTiled add b0 b1
305ret false
306end
307if not bonStreamTiledRange t0 get ggDim1 t0 0
308ret false
309end
310ret bonStreamTiledRange t1 get ggDim1 t1 b0
311end
312
313fn bonLoadTiled3 t0 ulong t1 ulong t2 ulong to bool
314if eq get bonUseNvfp4 0 1
315ret bonLoadNvfp4_3 t0 t1 t2
316end
317b0 = mul div mul get ggDim0 t0 get ggDim1 t0 128 28
318b1 = mul div mul get ggDim0 t1 get ggDim1 t1 128 28
319b2 = mul div mul get ggDim0 t2 get ggDim1 t2 128 28
320if not bonAllocTiled add add b0 b1 b2
321ret false
322end
323if not bonStreamTiledRange t0 get ggDim1 t0 0
324ret false
325end
326if not bonStreamTiledRange t1 get ggDim1 t1 b0
327ret false
328end
329ret bonStreamTiledRange t2 get ggDim1 t2 add b0 b1
330end
331
332fn bonLoadTiledSlice tid ulong nRows ulong to bool
333if eq get bonUseNvfp4 0 1
334ret bonLoadNvfp4_Slice tid nRows
335end
336nb = mul div mul get ggDim0 tid nRows 128 28
337if not bonAllocTiled nb
338ret false
339end
340ret bonStreamTiledRange tid nRows 0
341end
342
343note =========================================================================
344note CPU-side PTQ1_0 embed dequant + inverse 1024-block FWHT -> uncached bonApuShm!
345note =========================================================================
346fn bonBuildCpuLut to void
347for bv ulong 256
348v = bv
349for tr ulong 5
350t = mul v 3
351xi = shr t 8
352v = and t 255
353put bonCpuLut add mul bv 5 tr uchar xi
354put bonCpuLutF add mul bv 5 tr sub f32 xi f32 1.0f32
355end
356end
357end
358
359fn bonFwht1024Cpu base ulong to void
360h = ulong 1
361while lt h 1024 bound 12
362step = shl h 1
363i = ulong 0
364while lt i 1024 bound 1024
365for j ulong h bound 512
366p0 = add base add i j
367p1 = add p0 h
368a = get bonEmbF32 p0
369b = get bonEmbF32 p1
370put bonEmbF32 p0 add a b
371put bonEmbF32 p1 sub a b
372end
373i = add i step
374end
375h = step
376end
377for j ulong 1024
378put bonEmbF32 add base j mul get bonEmbF32 add base j f32 0.03125f32
379end
380end
381
382fn bonEmbByte bv ulong dstOff ulong stride ulong cnt ulong d f32 to void
383lb = mul bv 5
384for tr ulong cnt bound 5
385w = get bonCpuLutF add lb tr
386put bonEmbF32 add dstOff mul tr stride mul w d
387end
388end
389
390fn bonDequantEmbBlk b ulong to void
391src = mul b 28
392h = or ulong get bonEmbRowBuf add src 26 shl ulong get bonEmbRowBuf add src 27 8
393d = bitcast f32 ggFp16ToF32Bits h
394base = mul b 128
395for m ulong 16
396bonEmbByte ulong get bonEmbRowBuf add src m add base m 16 5 d
397end
398for m ulong 8
399bonEmbByte ulong get bonEmbRowBuf add add src 16 m add add base 80 m 8 5 d
400end
401for j ulong 2
402bonEmbByte ulong get bonEmbRowBuf add add src 24 j add add base 120 j 2 4 d
403end
404end
405
406fn bonLoadEmbedCpu tok ulong to bool
407H = get engSpec MS_H
408nb = div H 128
409rowBytes = mul nb 28
410foff = add get bonEmbRawOff 0 mul tok rowBytes
411if ne pread get ggFd 0 bonEmbRowBuf 0 rowBytes long foff long rowBytes
412ret false
413end
414for b ulong nb bound 64
415bonDequantEmbBlk b
416end
417nblks = div H 1024
418for kb ulong nblks bound 8
419bonFwht1024Cpu mul kb 1024
420end
421for i ulong H bound 8192
422v = mul get bonEmbF32 i get ggSign5120 i
423engPutU32Io mul i 4 bitcast uint v
424end
425note Write to UNCACHED host-coherent bonApuShm + APU_SHM_XINIT (zero stale GPU L2!)
426ret eq mapwrite get bonApuShm 0 APU_SHM_XINIT engIoBuf 0 mul H 4 long mul H 4
427end
428
429fn bonSetPos pos ulong to bool
430engPutU32Io 0 uint pos
431note Write to UNCACHED host-coherent bonApuShm + APU_SHM_POS (zero stale GPU L2!)
432ret eq mapwrite get bonApuShm 0 APU_SHM_POS engIoBuf 0 4 4
433end
434
435note =========================================================================
436note Full-APU 20-Core CPU Pool (10x X925 @ 3.9 GHz + 10x A725 @ 2.8 GHz)
437note =========================================================================
438fn bonPinCore cpu ulong to void
439m = new uchar 8 0
440put m div cpu 8 uchar shl ulong 1 rem cpu 8
441r = cpuset 0 m 0 8
442end
443
444fn bonPutU32Msg off ulong v uint to void
445for j ulong 4
446put bonPipeMsg add off j uchar and shr v mul j 8 255
447end
448end
449
450fn bonGetU32Msg off ulong to uint
451b0 = uint get bonPipeMsg off
452b1 = shl uint get bonPipeMsg add off 1 8
453b2 = shl uint get bonPipeMsg add off 2 16
454b3 = shl uint get bonPipeMsg add off 3 24
455ret or or b0 b1 or b2 b3
456end
457
458fn bonGetI32 b view uchar * off ulong to int
459b0 = uint get b off
460b1 = shl uint get b add off 1 8
461b2 = shl uint get b add off 2 16
462b3 = shl uint get b add off 3 24
463ret int or or b0 b1 or b2 b3
464end
465
466fn bonWorkerLoadSlice r0 ulong rCnt ulong to void
467rowBytes = ulong 1120
468totB = mul rCnt rowBytes
469foff = add get bonLmRawOff 0 mul r0 rowBytes
470done = ulong 0
471while lt done totB bound 256
472ch = sub totB done
473if gt ch 1048576
474ch = ulong 1048576
475end
476got = pread get ggFd 0 bonCpuRawBuf done ch long add foff done
477if le got 0
478break
479end
480done = add done ulong got
481end
482nBlks = mul rCnt 40
483for b ulong nBlks bound 262144
484src = mul b 28
485dlo = ulong get bonCpuRawBuf add src 26
486dhi = ulong get bonCpuRawBuf add src 27
487put bonCpuScaleBuf b bitcast f32 ggFp16ToF32Bits or dlo shl dhi 8
488end
489put bonCpuRowStart 0 r0
490put bonCpuRowCnt 0 rCnt
491if eq get bonUseNvfp4 0 1
492bonWorkerConvertNvfp4
493end
494end
495
496fn bonCpuDotBlk blkIdx ulong xb ulong to f32
497src = mul blkIdx 28
498bsum = f32 0.0f32
499for m ulong 16
500lb = mul ulong get bonCpuRawBuf add src m 5
501xm = add xb m
502bsum = add bsum mul get bonCpuLutF lb get bonCpuX xm
503bsum = add bsum mul get bonCpuLutF add lb 1 get bonCpuX add xm 16
504bsum = add bsum mul get bonCpuLutF add lb 2 get bonCpuX add xm 32
505bsum = add bsum mul get bonCpuLutF add lb 3 get bonCpuX add xm 48
506bsum = add bsum mul get bonCpuLutF add lb 4 get bonCpuX add xm 64
507end
508for m ulong 8
509lb = mul ulong get bonCpuRawBuf add add src 16 m 5
510xm = add add xb 80 m
511bsum = add bsum mul get bonCpuLutF lb get bonCpuX xm
512bsum = add bsum mul get bonCpuLutF add lb 1 get bonCpuX add xm 8
513bsum = add bsum mul get bonCpuLutF add lb 2 get bonCpuX add xm 16
514bsum = add bsum mul get bonCpuLutF add lb 3 get bonCpuX add xm 24
515bsum = add bsum mul get bonCpuLutF add lb 4 get bonCpuX add xm 32
516end
517for j ulong 2
518lb = mul ulong get bonCpuRawBuf add add src 24 j 5
519xm = add add xb 120 j
520bsum = add bsum mul get bonCpuLutF lb get bonCpuX xm
521bsum = add bsum mul get bonCpuLutF add lb 1 get bonCpuX add xm 2
522bsum = add bsum mul get bonCpuLutF add lb 2 get bonCpuX add xm 4
523bsum = add bsum mul get bonCpuLutF add lb 3 get bonCpuX add xm 6
524end
525ret mul bsum get bonCpuScaleBuf blkIdx
526end
527
528fn bonCpuDotRow rIdx ulong to f32
529acc = f32 0.0f32
530blkBase = mul rIdx 40
531for b ulong 40
532acc = add acc bonCpuDotBlk add blkBase b mul b 128
533end
534ret acc
535end
536
537fn bonWorkerExecAt shmOff ulong resFd int to void
538rr = mapread get bonApuShm 0 shmOff engIoBuf 0 20480
539for i ulong 5120
540put bonCpuX i bitcast f32 engGetU32Io mul i 4
541end
542bestVal = f32 -1000000000.0f32
543bestIdx = ulong 0
544rc = get bonCpuRowCnt 0
545rs = get bonCpuRowStart 0
546for ri ulong rc bound 32768
547v = f32 0.0f32
548if eq get bonUseNvfp4 0 1
549v = bonCpuDotRowNvfp4 ri
550else
551v = bonCpuDotRow ri
552end
553if gt v bestVal
554bestVal = v
555bestIdx = add rs ri
556end
557end
558bonPutU32Msg 0 bitcast uint bestVal
559bonPutU32Msg 4 uint bestIdx
560wr = write resFd bonPipeMsg 0 8
561end
562
563fn bonCpuDotBlkM4A src ulong xb ulong to void
564s0 = f32 0.0f32
565s1 = f32 0.0f32
566s2 = f32 0.0f32
567s3 = f32 0.0f32
568for m ulong 16
569lb = mul ulong get bonCpuRawBuf add src m 5
570xm = add xb m
571w = get bonCpuLutF lb
572s0 = add s0 mul w get bonCpuX4 xm
573s1 = add s1 mul w get bonCpuX4 add xm 5120
574s2 = add s2 mul w get bonCpuX4 add xm 10240
575s3 = add s3 mul w get bonCpuX4 add xm 15360
576w = get bonCpuLutF add lb 1
577s0 = add s0 mul w get bonCpuX4 add xm 16
578s1 = add s1 mul w get bonCpuX4 add xm 5136
579s2 = add s2 mul w get bonCpuX4 add xm 10256
580s3 = add s3 mul w get bonCpuX4 add xm 15376
581w = get bonCpuLutF add lb 2
582s0 = add s0 mul w get bonCpuX4 add xm 32
583s1 = add s1 mul w get bonCpuX4 add xm 5152
584s2 = add s2 mul w get bonCpuX4 add xm 10272
585s3 = add s3 mul w get bonCpuX4 add xm 15392
586w = get bonCpuLutF add lb 3
587s0 = add s0 mul w get bonCpuX4 add xm 48
588s1 = add s1 mul w get bonCpuX4 add xm 5168
589s2 = add s2 mul w get bonCpuX4 add xm 10288
590s3 = add s3 mul w get bonCpuX4 add xm 15408
591w = get bonCpuLutF add lb 4
592s0 = add s0 mul w get bonCpuX4 add xm 64
593s1 = add s1 mul w get bonCpuX4 add xm 5184
594s2 = add s2 mul w get bonCpuX4 add xm 10304
595s3 = add s3 mul w get bonCpuX4 add xm 15424
596end
597put bonCpuAcc4 0 s0
598put bonCpuAcc4 1 s1
599put bonCpuAcc4 2 s2
600put bonCpuAcc4 3 s3
601end
602
603fn bonCpuDotBlkM4B src ulong xb ulong to void
604s0 = get bonCpuAcc4 0
605s1 = get bonCpuAcc4 1
606s2 = get bonCpuAcc4 2
607s3 = get bonCpuAcc4 3
608for m ulong 8
609lb = mul ulong get bonCpuRawBuf add add src 16 m 5
610xm = add add xb 80 m
611w = get bonCpuLutF lb
612s0 = add s0 mul w get bonCpuX4 xm
613s1 = add s1 mul w get bonCpuX4 add xm 5120
614s2 = add s2 mul w get bonCpuX4 add xm 10240
615s3 = add s3 mul w get bonCpuX4 add xm 15360
616w = get bonCpuLutF add lb 1
617s0 = add s0 mul w get bonCpuX4 add xm 8
618s1 = add s1 mul w get bonCpuX4 add xm 5128
619s2 = add s2 mul w get bonCpuX4 add xm 10248
620s3 = add s3 mul w get bonCpuX4 add xm 15368
621w = get bonCpuLutF add lb 2
622s0 = add s0 mul w get bonCpuX4 add xm 16
623s1 = add s1 mul w get bonCpuX4 add xm 5136
624s2 = add s2 mul w get bonCpuX4 add xm 10256
625s3 = add s3 mul w get bonCpuX4 add xm 15376
626w = get bonCpuLutF add lb 3
627s0 = add s0 mul w get bonCpuX4 add xm 24
628s1 = add s1 mul w get bonCpuX4 add xm 5144
629s2 = add s2 mul w get bonCpuX4 add xm 10264
630s3 = add s3 mul w get bonCpuX4 add xm 15384
631w = get bonCpuLutF add lb 4
632s0 = add s0 mul w get bonCpuX4 add xm 32
633s1 = add s1 mul w get bonCpuX4 add xm 5152
634s2 = add s2 mul w get bonCpuX4 add xm 10272
635s3 = add s3 mul w get bonCpuX4 add xm 15392
636end
637put bonCpuAcc4 0 s0
638put bonCpuAcc4 1 s1
639put bonCpuAcc4 2 s2
640put bonCpuAcc4 3 s3
641end
642
643fn bonCpuDotBlkM4C src ulong xb ulong to void
644s0 = get bonCpuAcc4 0
645s1 = get bonCpuAcc4 1
646s2 = get bonCpuAcc4 2
647s3 = get bonCpuAcc4 3
648for j ulong 2
649lb = mul ulong get bonCpuRawBuf add add src 24 j 5
650xm = add add xb 120 j
651w = get bonCpuLutF lb
652s0 = add s0 mul w get bonCpuX4 xm
653s1 = add s1 mul w get bonCpuX4 add xm 5120
654s2 = add s2 mul w get bonCpuX4 add xm 10240
655s3 = add s3 mul w get bonCpuX4 add xm 15360
656w = get bonCpuLutF add lb 1
657s0 = add s0 mul w get bonCpuX4 add xm 2
658s1 = add s1 mul w get bonCpuX4 add xm 5122
659s2 = add s2 mul w get bonCpuX4 add xm 10242
660s3 = add s3 mul w get bonCpuX4 add xm 15362
661w = get bonCpuLutF add lb 2
662s0 = add s0 mul w get bonCpuX4 add xm 4
663s1 = add s1 mul w get bonCpuX4 add xm 5124
664s2 = add s2 mul w get bonCpuX4 add xm 10244
665s3 = add s3 mul w get bonCpuX4 add xm 15364
666w = get bonCpuLutF add lb 3
667s0 = add s0 mul w get bonCpuX4 add xm 6
668s1 = add s1 mul w get bonCpuX4 add xm 5126
669s2 = add s2 mul w get bonCpuX4 add xm 10246
670s3 = add s3 mul w get bonCpuX4 add xm 15366
671end
672put bonCpuAcc4 0 s0
673put bonCpuAcc4 1 s1
674put bonCpuAcc4 2 s2
675put bonCpuAcc4 3 s3
676end
677
678fn bonCpuDotBlkM4 blkIdx ulong xb ulong to void
679src = mul blkIdx 28
680bonCpuDotBlkM4A src xb
681bonCpuDotBlkM4B src xb
682bonCpuDotBlkM4C src xb
683sc = get bonCpuScaleBuf blkIdx
684put bonCpuRowAcc 0 add get bonCpuRowAcc 0 mul get bonCpuAcc4 0 sc
685put bonCpuRowAcc 1 add get bonCpuRowAcc 1 mul get bonCpuAcc4 1 sc
686put bonCpuRowAcc 2 add get bonCpuRowAcc 2 mul get bonCpuAcc4 2 sc
687put bonCpuRowAcc 3 add get bonCpuRowAcc 3 mul get bonCpuAcc4 3 sc
688end
689
690fn bonWorkerExecM4 resFd int to void
691rr = mapread get bonApuShm 0 APU_SHM_XROT_M4 engIoBuf 0 81920
692for i ulong 20480
693put bonCpuX4 i bitcast f32 engGetU32Io mul i 4
694end
695put bonCpuBestV 0 f32 -1000000000.0f32
696put bonCpuBestV 1 f32 -1000000000.0f32
697put bonCpuBestV 2 f32 -1000000000.0f32
698put bonCpuBestV 3 f32 -1000000000.0f32
699put bonCpuBestI 0 0
700put bonCpuBestI 1 0
701put bonCpuBestI 2 0
702put bonCpuBestI 3 0
703rc = get bonCpuRowCnt 0
704rs = get bonCpuRowStart 0
705for ri ulong rc bound 32768
706if eq get bonUseNvfp4 0 1
707bonCpuDotRowM4Nvfp4 ri
708else
709put bonCpuRowAcc 0 f32 0.0f32
710put bonCpuRowAcc 1 f32 0.0f32
711put bonCpuRowAcc 2 f32 0.0f32
712put bonCpuRowAcc 3 f32 0.0f32
713for b ulong 40
714bonCpuDotBlkM4 add mul ri 40 b mul b 128
715end
716end
717for m ulong 4
718if gt get bonCpuRowAcc m get bonCpuBestV m
719put bonCpuBestV m get bonCpuRowAcc m
720put bonCpuBestI m add rs ri
721end
722end
723end
724for m2 ulong 4
725bonPutU32Msg mul m2 8 bitcast uint get bonCpuBestV m2
726bonPutU32Msg add mul m2 8 4 uint get bonCpuBestI m2
727end
728wr = write resFd bonPipeMsg 0 32
729end
730
731fn bonWorkerLoop w ulong goFd int resFd int r0 ulong rCnt ulong to void
732bonPinCore w
733bonBuildCpuLut
734bonWorkerLoadSlice r0 rCnt
735put bonPipeMsg 0 1
736wr0 = write resFd bonPipeMsg 0 1
737for iter ulong 2000000
738rd = read goFd bonPipeMsg 0 1
739if or le rd 0 eq get bonPipeMsg 0 0
740exit 0
741end
742tag = ulong get bonPipeMsg 0
743if eq tag 2
744bonWorkerExecM4 resFd
745else
746bonWorkerExecAt APU_SHM_XROT resFd
747end
748end
749exit 0
750end
751
752fn bonClosePrevPipes w ulong to void
753for pw ulong w bound 20
754cp1 = fclose get bonGoFds pw
755cp2 = fclose get bonResFds pw
756end
757end
758
759fn bonSpawnOne w ulong cur ulong cnt ulong to void
760pGo = new uchar 8 0
761pRes = new uchar 8 0
762r1 = pipefds pGo 0 8 0
763r2 = pipefds pRes 0 8 0
764goRd = bonGetI32 pGo 0
765goWr = bonGetI32 pGo 4
766resRd = bonGetI32 pRes 0
767resWr = bonGetI32 pRes 4
768pid = fork
769if eq pid 0
770c1 = fclose goWr
771c2 = fclose resRd
772bonClosePrevPipes w
773bonWorkerLoop w goRd resWr cur cnt
774end
775c3 = fclose goRd
776c4 = fclose resWr
777put bonPids w pid
778put bonGoFds w goWr
779put bonResFds w resRd
780end
781
782fn bonApuInit to void
783if or eq get bonUseApu 0 0 eq get bonLmNCpu 0 0
784put bonApuReady 0 0
785ret
786end
787cur = get bonLmNGpu 0
788remW = ulong 30
789remR = get bonLmNCpu 0
790for w ulong 20
791wt = ulong 2
792if lt rem w 10 5
793wt = ulong 1
794end
795cnt = div mul remR wt remW
796if eq w 19
797cnt = remR
798end
799remR = sub remR cnt
800remW = sub remW wt
801bonSpawnOne w cur cnt
802cur = add cur cnt
803end
804for w ulong 20
805ack = read get bonResFds w bonPipeMsg 0 1
806end
807put bonApuReady 0 1
808end
809
810fn bonApuStop to void
811if eq get bonApuReady 0 0
812ret
813end
814st = new uchar 4 0
815put bonPipeMsg 0 0
816for w ulong 20
817wr = write get bonGoFds w bonPipeMsg 0 1
818c1 = fclose get bonGoFds w
819c2 = fclose get bonResFds w
820wp = waitpid int get bonPids w st 0 4 0
821end
822put bonApuReady 0 0
823end
824
825note =========================================================================
826note Kernel registration & command buffer emission
827note =========================================================================
828fn bonRegTernary kid ulong mode ulong smem ulong to void
829p = mul div add get engCodePos 0 255 256 256
830put engEntry kid p
831put engSmem kid smem
832put engCodePos 0 p
833put q36Pc 0 0
834put q36Guard 0 0
835if not tTernaryKernel mode
836exit 4
837end
838q36Done
839end
840
841fn bonRegNvfp4 kid ulong mode ulong smem ulong to void
842p = mul div add get engCodePos 0 255 256 256
843put engEntry kid p
844put engSmem kid smem
845put engCodePos 0 p
846put q36Pc 0 0
847put q36Guard 0 0
848if not n4Nvfp4Kernel mode
849exit 4
850end
851q36Done
852end
853
854fn bonEmitAllKernels to void
855put engCodePos 0 0
856engRegKernel 0 1 200 4128
857engRegKernel 1 1 300 64
858engRegKernel 2 1 301 0
859engRegKernel 4 1 303 0
860engRegKernel 7 1 306 1088
861engRegKernel 8 1 307 1088
862engRegKernel 9 1 308 0
863engRegKernel 10 1 309 0
864engRegKernel 13 0 114 64
865bonRegTernary 15 400 4352
866bonRegTernary 16 401 4096
867bonRegTernary 17 402 31744
868bonRegTernary 18 403 4352
869bonRegTernary 19 404 2048
870bonRegTernary 20 405 0
871bonRegTernary 21 406 16960
872bonRegTernary 22 407 16960
873bonRegTernary 23 408 64
874bonRegTernary 24 409 64
875bonRegTernary 25 410 0
876bonRegTernary 26 411 0
877bonRegNvfp4 27 420 0
878bonRegNvfp4 28 421 2304
879bonRegNvfp4 29 422 2304
880bonRegNvfp4 30 423 5760
881bonRegNvfp4 31 424 5760
882end
883
884fn bonEmitPtq1 xVa ulong wVa ulong outVa ulong K ulong N ulong to bool
885if eq get bonUseNvfp4 0 1
886engPtr xVa
887engPtr wVa
888engPtr add wVa div mul N K 2
889engPtr get bonNvLutVa 0
890engPtr outVa
891engArg uint K
892engArg uint N
893ret engPktEmitGy 28 uint div add N 31 32 1 256
894end
895engPtr xVa
896engPtr wVa
897engPtr outVa
898engArg uint K
899engArg uint N
900ret engPktEmitGy 15 uint div add N 127 128 1 256
901end
902
903fn bonEmitFwht inVa ulong signVa ulong outVa ulong K ulong to bool
904engPtr inVa
905engPtr signVa
906engPtr outVa
907engArg uint K
908engArg 0
909ret engPktEmitGy 16 uint div K 1024 1 256
910end
911
912fn bonEmitFaAttnSub L ulong faIdx ulong to bool
913nQ = get engSpec MS_NQ
914nKV = get engSpec MS_NKV
915hd = get engSpec MS_HD
916kBase = get engKvVa faIdx
917vBase = add kBase div get engKvSlabBytes 0 2
918kInVa = add engW EW_QKV mul mul nQ mul hd 2 4
919vInVa = add kInVa mul mul nKV hd 4
920posVa = engW EW_POS
921engPtr kInVa
922engPtr vInVa
923engPtr get engLKNormVa L
924engPtr get engRopeVa 0
925engPtr kBase
926engPtr vBase
927engPtr posVa
928if not engPktEmitGy 7 uint nKV 1 uint hd
929ret false
930end
931engPtr engW EW_QKV
932engPtr get engLQNormVa L
933engPtr get engRopeVa 0
934engPtr engW EW_QROT
935engPtr posVa
936if not engPktEmitGy 8 uint nQ 1 uint hd
937ret false
938end
939engPtr engW EW_QROT
940engPtr kBase
941engPtr vBase
942engPtr engW EW_OPART
943engPtr engW EW_MLPART
944engPtr posVa
945if not engPktEmitGy 9 uint nQ 8 32
946ret false
947end
948engPtr engW EW_OPART
949engPtr engW EW_MLPART
950engPtr engW EW_QKV
951engPtr engW EW_Y
952engArg 8
953ret engPktEmitGy 10 uint nQ 1 uint hd
954end
955
956fn bonEmitFaLayer L ulong faIdx ulong to bool
957H = get engSpec MS_H
958oK = mul get engSpec MS_NQ get engSpec MS_HD
959qkvN = add mul oK 2 mul mul get engSpec MS_NKV get engSpec MS_HD 2
960if not bonEmitFwht engW EW_XN engW BW_SIGN5120 engW BW_XN_ROT H
961ret false
962end
963if not bonEmitPtq1 engW BW_XN_ROT get bonA1T L engW EW_QKV H qkvN
964ret false
965end
966if not bonEmitFaAttnSub L faIdx
967ret false
968end
969if not bonEmitFwht engW EW_Y engW BW_SIGN6144 engW BW_Y_ROT oK
970ret false
971end
972ret bonEmitPtq1 engW BW_Y_ROT get bonOT L engW EW_AOUT oK H
973end
974
975fn bonEmitGdnCore L ulong gdnIdx ulong to bool
976H = get engSpec MS_H
977nV = get engSpec MS_GDN_NV
978qkvN = add mul get engSpec MS_GDN_NK 256 mul nV 128
979engPtr get engLA4WVa L
980engPtr engW EW_XN
981engPtr engW EW_AB
982engArg uint H
983engArg uint mul nV 2
984if not engPktEmitGy 4 uint div add mul nV 2 7 8 1 256
985ret false
986end
987engPtr engW EW_QKV
988engPtr get engLA4SVa L
989engPtr add get engGdnTailVa 0 mul gdnIdx mul qkvN 12
990if not engPktEmitGy 13 uint div qkvN 128 1 128
991ret false
992end
993engPtr engW EW_QKV
994engPtr add engW EW_QKV mul qkvN 4
995engPtr engW EW_AB
996engPtr get engLKNormVa L
997engPtr add get engGdnStateVa 0 mul gdnIdx mul nV 32768
998engPtr get engLQNormVa L
999engPtr engW EW_Y
1000ret engPktEmitGy 17 uint nV 1 128
1001end
1002
1003fn bonEmitGdnLayer L ulong gdnIdx ulong to bool
1004H = get engSpec MS_H
1005zN = mul get engSpec MS_GDN_NV 128
1006qkvzN = add add mul get engSpec MS_GDN_NK 256 zN zN
1007if not bonEmitFwht engW EW_XN engW BW_SIGN5120 engW BW_XN_ROT H
1008ret false
1009end
1010if not bonEmitPtq1 engW BW_XN_ROT get bonA1T L engW EW_QKV H qkvzN
1011ret false
1012end
1013if not bonEmitGdnCore L gdnIdx
1014ret false
1015end
1016if not bonEmitFwht engW EW_Y engW BW_SIGN6144 engW BW_Y_ROT zN
1017ret false
1018end
1019ret bonEmitPtq1 engW BW_Y_ROT get bonOT L engW EW_AOUT zN H
1020end
1021
1022fn bonEmitMlpLayer L ulong to bool
1023H = get engSpec MS_H
1024I = get engSpec MS_I
1025if not engEmitRmsNormAdd get engLPostNormVa L 1
1026ret false
1027end
1028if not bonEmitFwht engW EW_XN engW BW_SIGN5120 engW BW_XN_ROT H
1029ret false
1030end
1031if not bonEmitPtq1 engW BW_XN_ROT get bonGuT L engW EW_GU H mul I 2
1032ret false
1033end
1034engPtr engW EW_GU
1035engPtr engW EW_SW
1036engArg uint I
1037if not engPktEmitGy 2 uint div I 256 1 256
1038ret false
1039end
1040if not bonEmitFwht engW EW_SW engW BW_SIGN17408 engW BW_SW_ROT I
1041ret false
1042end
1043ret bonEmitPtq1 engW BW_SW_ROT get bonDnT L engW EW_AOUT I H
1044end
1045
1046fn bonEmitTrunk to bool
1047H = get engSpec MS_H
1048note Copy uncached host-coherent bonApuShm -> GPU-cached EW_X & EW_POS (0.5 us, ZERO stale L2!)
1049engPtr add get bonApuShmVa 0 APU_SHM_XINIT
1050engPtr engW EW_X
1051engArg uint H
1052if not engPktEmitGy 20 uint div H 256 1 256
1053ret false
1054end
1055engPtr add get bonApuShmVa 0 APU_SHM_POS
1056engPtr engW EW_POS
1057engArg 1
1058if not engPktEmitGy 20 1 1 256
1059ret false
1060end
1061nL = get engSpec MS_LAYERS
1062faIdx = ulong 0
1063gdnIdx = ulong 0
1064for L ulong nL bound 128
1065doAdd = uint 1
1066if eq L 0
1067doAdd = 0
1068end
1069if not engEmitRmsNormAdd get engLInNormVa L doAdd
1070ret false
1071end
1072if eq get engLayerKind L 0
1073if not bonEmitFaLayer L faIdx
1074ret false
1075end
1076faIdx = add faIdx 1
1077else
1078if not bonEmitGdnLayer L gdnIdx
1079ret false
1080end
1081gdnIdx = add gdnIdx 1
1082end
1083if not bonEmitMlpLayer L
1084ret false
1085end
1086end
1087ret true
1088end
1089
1090fn bonEmitLmHead nRows ulong to bool
1091H = get engSpec MS_H
1092if eq get bonUseNvfp4 0 1
1093nParts = div add nRows 31 32
1094engPtr engW BW_XN_ROT
1095engPtr get bonLmT 0
1096engPtr add get bonLmT 0 div mul get bonLmNAll 0 H 2
1097engPtr get bonNvLutVa 0
1098engPtr engW BW_PARTS
1099engArg uint H
1100engArg uint nRows
1101if not engPktEmitGy 29 uint nParts 1 256
1102ret false
1103end
1104engPtr engW BW_PARTS
1105engPtr add get bonApuShmVa 0 APU_SHM_BEST
1106engArg uint nParts
1107ret engPktEmitGy 19 1 1 256
1108end
1109nParts = div add nRows 127 128
1110engPtr engW BW_XN_ROT
1111engPtr get bonLmT 0
1112engPtr engW BW_PARTS
1113engArg uint H
1114engArg uint nRows
1115if not engPktEmitGy 18 uint nParts 1 256
1116ret false
1117end
1118engPtr engW BW_PARTS
1119engPtr add get bonApuShmVa 0 APU_SHM_BEST
1120engArg uint nParts
1121ret engPktEmitGy 19 1 1 256
1122end
1123
1124fn bonRecordPackets to bool
1125H = get engSpec MS_H
1126engPktStartAt EW_PKT
1127if not bonEmitTrunk
1128ret false
1129end
1130if not engEmitRmsNormAdd engW EW_FNORM 1
1131ret false
1132end
1133if not bonEmitFwht engW EW_XN engW BW_SIGN5120 engW BW_XN_ROT H
1134ret false
1135end
1136engPtr engW BW_XN_ROT
1137engPtr add get bonApuShmVa 0 APU_SHM_XROT
1138engArg uint H
1139if not engPktEmitGy 20 uint div H 256 1 256
1140ret false
1141end
1142if not engPktFinish 0
1143ret false
1144end
1145engPktStartAt add EW_PKT 4194304
1146if not bonEmitTrunk
1147ret false
1148end
1149if not engEmitRmsNormAdd engW EW_FNORM 1
1150ret false
1151end
1152if not engPktFinish 1
1153ret false
1154end
1155engPktStartAt add EW_PKT 8388608
1156if not bonEmitLmHead get bonLmNGpu 0
1157ret false
1158end
1159if not engPktFinish 2
1160ret false
1161end
1162engPktStartAt add EW_PKT 12582912
1163if not bonEmitTrunk
1164ret false
1165end
1166if not engEmitRmsNormAdd engW EW_FNORM 1
1167ret false
1168end
1169if not bonEmitFwht engW EW_XN engW BW_SIGN5120 engW BW_XN_ROT H
1170ret false
1171end
1172if not bonEmitLmHead get bonLmNGpu 0
1173ret false
1174end
1175ret engPktFinish 3
1176end
1177
1178fn bonGpuInitKvGdn to int
1179ropeBytes = mul get engSpec MS_MAX_CTX mul get engSpec MS_ROPE_DIMS 4
1180put engRopeMap 0 nvalloc ropeBytes true
1181if le get engRopeMap 0 0
1182ret 10
1183end
1184put engRopeVa 0 mapaddr get engRopeMap 0
1185nGdn = get engSpec MS_N_GDN
1186nK = get engSpec MS_GDN_NK
1187nV = get engSpec MS_GDN_NV
1188qkvN = add mul nK 256 mul nV 128
1189put engGdnStateMap 0 nvalloc mul nGdn mul nV 32768 true
1190if le get engGdnStateMap 0 0
1191ret 10
1192end
1193put engGdnStateVa 0 mapaddr get engGdnStateMap 0
1194put engGdnTailMap 0 nvalloc mul div add mul nGdn mul qkvN 12 65535 65536 65536 true
1195if le get engGdnTailMap 0 0
1196ret 10
1197end
1198put engGdnTailVa 0 mapaddr get engGdnTailMap 0
1199kvBytes = mul get engSpec MS_MAX_CTX mul get engSpec MS_NKV mul get engSpec MS_HD 8
1200put engKvSlabBytes 0 kvBytes
1201nFa = add get engSpec MS_N_FA get ggHasMtp 0
1202for fi ulong nFa bound 64
1203km = nvalloc kvBytes true
1204if le km 0
1205ret 11
1206end
1207put engKvMap fi km
1208put engKvVa fi mapaddr km
1209end
1210put engSlabCnt 0 0
1211if not bmAllocGpu
1212ret 12
1213end
1214ret 0
1215end
1216
1217fn bonGpuInit to int
1218put engUseStrictSync 0 1
1219if not gbopen 1
1220ret 2
1221end
1222if not gbbopen
1223ret 3
1224end
1225put engCodeMap 0 0
1226bonEmitAllKernels
1227csize = mul div add get engCodePos 0 4095 4096 4096
1228cm = nvalloc csize false
1229if le cm 0
1230ret 4
1231end
1232put engCodeMap 0 cm
1233put engCodeFail 0 0
1234bonEmitAllKernels
1235if ne get engCodeFail 0 0
1236ret 5
1237end
1238put engWork 0 nvalloc ENG_WORK_BYTES true
1239if le get engWork 0 0
1240ret 7
1241end
1242put engWorkVa 0 mapaddr get engWork 0
1243put bonApuShm 0 nvalloc 262144 false
1244if le get bonApuShm 0 0
1245ret 7
1246end
1247put bonApuShmVa 0 mapaddr get bonApuShm 0
1248if eq get bonUseNvfp4 0 1
1249if not bonNvInitGpu
1250ret 7
1251end
1252end
1253ret bonGpuInitKvGdn
1254end
1255
1256fn bonUploadSigns to bool
1257for i ulong 5120
1258engPutU32Io mul i 4 bitcast uint get ggSign5120 i
1259end
1260if ne mapwrite get engWork 0 BW_SIGN5120 engIoBuf 0 20480 20480
1261ret false
1262end
1263for i ulong 6144
1264engPutU32Io mul i 4 bitcast uint get ggSign6144 i
1265end
1266if ne mapwrite get engWork 0 BW_SIGN6144 engIoBuf 0 24576 24576
1267ret false
1268end
1269for i ulong 17408
1270engPutU32Io mul i 4 bitcast uint get ggSign17408 i
1271end
1272if ne mapwrite get engWork 0 BW_SIGN17408 engIoBuf 0 69632 69632
1273ret false
1274end
1275ret true
1276end
1277
1278fn bonLoadFaSub L ulong to bool
1279tQ = ggFindLayer L gSufQ
1280tK = ggFindLayer L gSufK
1281tV = ggFindLayer L gSufV
1282if or lt tQ 0 or lt tK 0 lt tV 0
1283ret false
1284end
1285if not bonLoadTiled3 ulong tQ ulong tK ulong tV
1286ret false
1287end
1288put bonA1T L get bonTResVa 0
1289tO = ggFindLayer L gSufO
1290if lt tO 0
1291ret false
1292end
1293if not bonLoadTiled1 ulong tO
1294ret false
1295end
1296put bonOT L get bonTResVa 0
1297tQn = ggFindLayer L gSufQNorm
1298tKn = ggFindLayer L gSufKNorm
1299if or lt tQn 0 lt tKn 0
1300ret false
1301end
1302vaQn = bonLoadF32 ulong tQn
1303vaKn = bonLoadF32 ulong tKn
1304if or eq vaQn 0 eq vaKn 0
1305ret false
1306end
1307put engLQNormVa L vaQn
1308put engLKNormVa L vaKn
1309ret true
1310end
1311
1312fn bonLoadGdnSub L ulong to bool
1313tQkv = ggFindLayer L gSufQkv
1314tZ = ggFindLayer L gSufZ
1315if or lt tQkv 0 lt tZ 0
1316ret false
1317end
1318if not bonLoadTiled2 ulong tQkv ulong tZ
1319ret false
1320end
1321put bonA1T L get bonTResVa 0
1322tO = ggFindLayer L gSufSsmOut
1323if lt tO 0
1324ret false
1325end
1326if not bonLoadTiled1 ulong tO
1327ret false
1328end
1329put bonOT L get bonTResVa 0
1330tConv = ggFindLayer L gSufConv
1331tSn = ggFindLayer L gSufSsmNorm
1332if or lt tConv 0 lt tSn 0
1333ret false
1334end
1335va0 = bonLoadF32 ulong tConv
1336if eq va0 0
1337ret false
1338end
1339put engLA4SVa L va0
1340va0 = bonLoadF32 ulong tSn
1341if eq va0 0
1342ret false
1343end
1344put engLQNormVa L va0
1345va0 = bonLoadGdnAbBf16 L
1346if eq va0 0
1347ret false
1348end
1349put engLA4WVa L va0
1350va0 = bonLoadGdnAlogDt L
1351if eq va0 0
1352ret false
1353end
1354put engLKNormVa L va0
1355ret true
1356end
1357
1358fn bonLoadMlpSub L ulong to bool
1359tG = ggFindLayer L gSufGate
1360tU = ggFindLayer L gSufUp
1361if or lt tG 0 lt tU 0
1362ret false
1363end
1364if not bonLoadTiled2 ulong tG ulong tU
1365ret false
1366end
1367put bonGuT L get bonTResVa 0
1368tD = ggFindLayer L gSufDn
1369if lt tD 0
1370ret false
1371end
1372if not bonLoadTiled1 ulong tD
1373ret false
1374end
1375put bonDnT L get bonTResVa 0
1376ret true
1377end
1378
1379fn bonLoadLayer L ulong to bool
1380tIn = ggFindLayer L gSufInNorm
1381tPost = ggFindLayer L gSufPostNorm
1382if or lt tIn 0 lt tPost 0
1383ret false
1384end
1385vaIn = bonLoadF32 ulong tIn
1386vaPost = bonLoadF32 ulong tPost
1387if or eq vaIn 0 eq vaPost 0
1388ret false
1389end
1390put engLInNormVa L vaIn
1391put engLPostNormVa L vaPost
1392if eq get engLayerKind L 0
1393if not bonLoadFaSub L
1394ret false
1395end
1396else
1397if not bonLoadGdnSub L
1398ret false
1399end
1400end
1401ret bonLoadMlpSub L
1402end
1403
1404fn bonLoadAll to int
1405bonBuildCpuLut
1406if not engUploadRope
1407ret 13
1408end
1409if not bonUploadSigns
1410ret 13
1411end
1412tEmb = ggFindExact gNmEmb len gNmEmb
1413tOut = ggFindExact gNmOut len gNmOut
1414tFn = ggFindExact gNmFNorm len gNmFNorm
1415if or lt tEmb 0 or lt tOut 0 lt tFn 0
1416ret 14
1417end
1418put bonEmbRawOff 0 add get ggDataStart 0 get ggTOff ulong tEmb
1419put bonLmRawOff 0 add get ggDataStart 0 get ggTOff ulong tOut
1420H = get engSpec MS_H
1421V = get engSpec MS_VOCAB
1422put bonLmNAll 0 V
1423if eq get bonUseApu 0 1
1424if eq get bonLmNCpu 0 0
1425put bonLmNCpu 0 512
1426end
1427if ge get bonLmNCpu 0 V
1428put bonLmNCpu 0 0
1429end
1430note Keep bonLmNGpu a multiple of 4 for 4-row tiled PTQ1_0!
1431ng = mul div sub V get bonLmNCpu 0 4 4
1432put bonLmNGpu 0 ng
1433put bonLmNCpu 0 sub V ng
1434else
1435put bonLmNCpu 0 0
1436put bonLmNGpu 0 V
1437end
1438if not bonCopySmall ulong tFn get engWork 0 EW_FNORM mul H 4
1439ret 14
1440end
1441if not bonLoadTiledSlice ulong tOut get bonLmNAll 0
1442ret 14
1443end
1444put bonLmT 0 get bonTResVa 0
1445nL = get engSpec MS_LAYERS
1446for L ulong nL bound 128
1447if not bonLoadLayer L
1448ret 15
1449end
1450end
1451if not bonRecordPackets
1452ret 16
1453end
1454if not bmLoadAndRecord
1455ret 17
1456end
1457bonApuInit
1458ret 0
1459end
1460
1461fn bonResetState to void
1462if gt get engSlotLen 16 0
1463okZ = engReplayPre 16
1464put engCurPos 0 0
1465ret
1466end
1467for i ulong 65536
1468put engIoBuf i 0
1469end
1470nGdn = get engSpec MS_N_GDN
1471nV = get engSpec MS_GDN_NV
1472nK = get engSpec MS_GDN_NK
1473sTot = mul nGdn mul nV 32768
1474engZeroMap get engGdnStateMap 0 div add sTot 65535 65536
1475qkvN = add mul nK 256 mul nV 128
1476tTot = mul nGdn mul qkvN 12
1477engZeroMap get engGdnTailMap 0 div add tTot 65535 65536
1478put engCurPos 0 0
1479end
1480
1481fn bonStepDecode to ulong
1482if eq get bonApuReady 0 0
1483if not engReplayPre 3
1484ret 999999
1485end
1486if ne mapread get bonApuShm 0 APU_SHM_BEST engIoBuf 0 8 8
1487ret 999999
1488end
1489ret ulong engGetU32Io 4
1490end
1491if not engReplayPre 0
1492ret 999999
1493end
1494r0 = mapset get gbmem 2 8192 4 0
1495r1 = mapset get gbmem 3 0 4 0
1496r2 = mapset get gbmem 3 16 4 0
1497if not nvsend get engWork 0 get engSlotOff 2 uint div get engSlotLen 2 4
1498ret 999999
1499end
1500put bonPipeMsg 0 1
1501for w ulong 20
1502wr = write get bonGoFds w bonPipeMsg 0 1
1503end
1504if not engWaitFast 10000000000
1505ret 999999
1506end
1507if ne mapread get bonApuShm 0 APU_SHM_BEST engIoBuf 0 8 8
1508ret 999999
1509end
1510bestVal = bitcast f32 engGetU32Io 0
1511bestIdx = ulong engGetU32Io 4
1512for w ulong 20
1513rd = read get bonResFds w bonPipeMsg 0 8
1514fv = bitcast f32 bonGetU32Msg 0
1515idx = ulong bonGetU32Msg 4
1516if gt fv bestVal
1517bestVal = fv
1518bestIdx = idx
1519end
1520end
1521ret bestIdx
1522end
1523
1524fn bonPrefillAndFirst ntok ulong to ulong
1525bonResetState
1526for i ulong ntok bound 4096
1527if not bonLoadEmbedCpu ulong get engTokSeq i
1528ret 999999
1529end
1530if not bonSetPos i
1531ret 999999
1532end
1533if eq add i 1 ntok
1534tok = bonStepDecode
1535put engCurPos 0 ntok
1536ret tok
1537else
1538if not engReplayPre 1
1539ret 999999
1540end
1541end
1542end
1543ret 999999
1544end
1545
1546global bonProfUs ulong 8
1547text bonDbgM4 "M4_PROBE (copy rms fwht ptq1M4_ok ptq1M4_us gdnM4 faM4 mlpM4 s6_ok s6_us mtp4_ok mtp4_us): "
1548text bonP1 "PROF_1TOK: trunk_us="
1549text bonP2 " lm_gpu_us="
1550text bonP3 " ptq1_34816x5120_us="
1551text bonP4 " fwht5120_us="
1552text bonP5 " gdn_layer_us="
1553text bonP6 " fa_layer_us="
1554text bonP7 " mlp_layer_us="
1555
1556fn bonBenchSlot slot ulong reps ulong idx ulong to void
1557ok = engReplayPre slot
1558t0 = ulong monons
1559for r ulong reps bound 128
1560ok = engReplayPre slot
1561end
1562t1 = ulong monons
1563put bonProfUs idx div sub t1 t0 mul reps 1000
1564end
1565
1566fn bonProfileSteps to void
1567bonResetState
1568ok = bonLoadEmbedCpu 760
1569ok = bonSetPos 0
1570bonBenchSlot 0 2 0
1571bonBenchSlot 2 2 1
1572engPktStartAt add EW_PKT 54525952
1573ok = bonEmitPtq1 engW BW_XN_ROT get bonGuT 0 engW EW_GU 5120 34816
1574ok = engPktFinish 0
1575bonBenchSlot 0 10 2
1576engPktStartAt add EW_PKT 54525952
1577ok = bonEmitFwht engW EW_XN engW BW_SIGN5120 engW BW_XN_ROT 5120
1578ok = engPktFinish 0
1579bonBenchSlot 0 50 3
1580engPktStartAt add EW_PKT 54525952
1581ok = bonEmitGdnLayer 0 0
1582ok = engPktFinish 0
1583bonBenchSlot 0 10 4
1584engPktStartAt add EW_PKT 54525952
1585ok = bonEmitFaLayer 3 0
1586ok = engPktFinish 0
1587bonBenchSlot 0 10 5
1588engPktStartAt add EW_PKT 54525952
1589ok = bonEmitMlpLayer 0
1590ok = engPktFinish 0
1591bonBenchSlot 0 10 6
1592engPrint bonP1
1593engPrintU get bonProfUs 0
1594engPrint bonP2
1595engPrintU get bonProfUs 1
1596engPrint bonP3
1597engPrintU get bonProfUs 2
1598engPrint bonP4
1599engPrintU get bonProfUs 3
1600engPrint bonP5
1601engPrintU get bonProfUs 4
1602engPrint bonP6
1603engPrintU get bonProfUs 5
1604engPrint bonP7
1605engPrintU get bonProfUs 6
1606engPrint bonDbgNl
1607end