interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / gpu / gg.om

5704 lines · 108614 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note gg.om: ollm GPU gradient (aien-train#52, rung 4). The step loss and its gradient for the batch in
3note obx/oby, computed on the GB10 by gk.om GEMMs and gr.om row kernels over all R = B T rows at
4note once in f32; the gradient lands in ogr[0..P) as f64 and the loss in olos, so oadam and olsum run
5note unchanged on the CPU. Arena (gbmem 5) byte offsets in gga: 0 W, 1 G, 2 X, 3 Y, 4 CSR start
6note (V + 1), 5 CSR order (R), 6 first layer, 7 layer stride, 8 HF, 9 SF, 10 Z, 11 logits (R V),
7note 12 CE stats (R 3), 13 DH, 14 DA, 15 DQ DK DU (contiguous), 16 DO, 17 DF, 18 DP, 19 WS, 20 CS;
8note layer fields 24 + f: 0 hin 1 a 2 q 3 k 4 u 5 o 6 hm 7 bn 8 s1 9 s2 10 fp 11 rl 12 P;
9note 40 the arena device address, 47 the plan cursor. s1, s2 and SF hold 1/s (the CPU keeps s).
10global gga ulong 56
11global gknv4 uint 1
12global ggw4_ok ulong 1
13global ggn4p ulong 3
14global ggn4s f64 2
15global ggn4q uint 1
16global ggn4ep uint 1
17global ggenv uchar 64
18text ggnv4e "OLLM_NVFP4=1"
19global ggcn ulong 65537
20note byte strides: 0 D, 1 T D, 2 E, 3 T, 4 T T, 5 H T T, 6 R D, 7 D D (each times 4)
21global ggs uint 8
22note OMUTG: 1 in the product; 0 in mutant g (DK = -DS^T Q), which the parity check must catch.
23const OMUTG ulong 1
24note Looped models (docs/LOOP.md): the layer region holds one activation block per layer application
25note (og 56 of them), and application l reads the parameters and adds to the gradient of parameter layer
26note olp l (model.om ovg1..ovw2). Every weight-gradient launch accumulates into G, which ggbwd zeroes
27note once, so a core's gradient is the sum over its passes. Without a loop og 56 = L and olp l = l.
28note OMUTLG: 1 in the product; 0 in mutant lg (the w2 gradient of an application that repeats an
29note earlier parameter layer is not launched, model.om's mutant lp on the chip), which gcmp and gcmp2 on
30note a looped checkpoint must catch. Without a loop the two are the same.
31const OMUTLG ulong 1
32note Frozen-core training on the chip (spec.om trainf's FREEZE_LAYERS F). ggfz: 0 the frozen
33note parameter count fz = og 31 + F og 32 (the embeddings wte and wpe, then layers 0..F-1), 1 F. With
34note fz > 0, ggbwd stops after layer F and launches no gradient into [0, fz) (the tied head's wte
35note term, the WPE column sums, the embedding scatter), so G[0, fz) stays the zero ggbwd wrote; ggadam
36note clips over that G and steps only [fz, P); ggdnst brings back only [fz, P), so the frozen f64
37note weights and moments on the host stay bit for bit (spext checks them by hash). A looped
38note checkpoint with F > 0 is exit 17, as cmdtrainf (ockload) refuses it on the CPU.
39note OMUTFZ: 1 in the product; 0 in mutant fz (the chip ignores the freeze: full backward, Adam and
40note download), which gcmp and gacmp with FREEZE_LAYERS and spext after gtrainf must catch.
41const OMUTFZ ulong 1
42global ggfz ulong 2
43
44note GPU setup failed (ggopen): a clear stage and error on stderr, then exit 75. L1014: a setup
45note that failed before buffers 2 and 3 existed used to reach ggdie's mapget of the unmapped slots, which
46note trapped BOUNDS (exit 103) with no message. stdout stays free for the program's own output.
47note Error 81 is RM status 0x51 NV_ERR_NO_MEMORY (open-gpu-kernel-modules nvstatuscodes.h). Only stage 35
48note (the compute object, context.om) waits on it, 50 x 100 ms, because there it means another job holds
49note the GPU. At any other stage, such as 21 (a memory allocation), there is no wait and no retry, so
50note "try again" is not offered; the cause there is not established by this code, so the text names none.
51text ggsa "omega: GPU setup failed at stage "
52text ggsb ", error "
53text ggsc " (driver status 81, out of memory: seen while another job holds the GPU; it outlasted the bounded wait). Try again.\n"
54text ggse " (driver status 81, out of memory, at a step with no wait and no retry; only stage 35 waits for a busy GPU).\n"
55text ggsd " (positive is driver status, negative is errno or internal; the stage is the nvstage step in gpu/context.om).\n"
56
57fn ggerr1 n ulong to void
58p = ulong 0
59while lt p n bound 4097
60w = write 2 oout p sub n p
61if le w 0
62exit 70
63end
64p = add p ulong w
65end
66put oon 0 0
67end
68
69fn ggsetupfail to void
70oflush
71r = write 2 ggsa 0 len ggsa
72oputu ulong get nvstage 0
73ggerr1 get oon 0
74r = write 2 ggsb 0 len ggsb
75oputl get nverr 0
76ggerr1 get oon 0
77if eq get nverr 0 81
78if eq get nvstage 0 35
79r = write 2 ggsc 0 len ggsc
80else
81r = write 2 ggse 0 len ggse
82end
83else
84r = write 2 ggsd 0 len ggsd
85end
86exit 75
87end
88
89fn ggdie c ulong to void
90oputc 71
91oputc 88
92oputc 32
93oputu c
94oputc 32
95oputl get nverr 0
96oputc 32
97oputu ulong get nvstage 0
98note diagnostics: launches done, mark1, mark2, QMD semaphore; zero while slots 2 and 3 are unmapped
99note (a failed ggopen), where mapget of an unmapped slot trapped BOUNDS (exit 103, no message)
100oputc 32
101oputl get gkt 0
102oputc 32
103if and ne get gbmem 3 0 ne get gbmem 2 0
104oputu mapget get gbmem 3 0 4
105oputc 32
106oputu mapget get gbmem 3 16 4
107oputc 32
108oputu mapget get gbmem 2 8192 4
109oputc 32
110oputu get gbst 4
111oputc 32
112oputu get gbst 5
113else
114oputc 48
115oputc 32
116oputc 48
117oputc 32
118oputc 48
119end
120oputc 10
121oflush
122exit 84
123end
124
125fn ggcd n ulong k ulong to ulong
126ret div add n sub k 1 k
127end
128
129fn ggr to ulong
130ret mul get og 6 get og 1
131end
132
133note P rounded up to the SQN chunk of 512; the gradient buffer is that long and zeroed to it
134fn ggp5 to ulong
135ret mul ggcd get og 8 512 512
136end
137
138fn ggput i ulong n ulong to void
139c = get gga 47
140put gga i c
141b = mul div add mul n 4 255 256 256
142put gga 47 add c b
143end
144
145fn ggplan_nv4 r ulong d ulong f ulong to void
146if eq get gknv4 0 0
147put gga 41 0
148put gga 42 0
149put gga 43 0
150put gga 44 0
151put gga 48 0
152put gga 49 0
153put gga 50 0
154put gga 51 0
155ret
156end
157mx = mul 3 mul r d
158t = mul r f
159if gt t mx
160mx = t
161end
162t = mul d f
163if gt t mx
164mx = t
165end
166t = mul r get og 0
167if gt t mx
168mx = t
169end
170t = mul d get og 0
171if gt t mx
172mx = t
173end
174nnv = ggcd mx 8
175nsx = ggcd mx 64
176ggput 41 nnv
177ggput 42 nsx
178ggput 43 nnv
179ggput 44 nsx
180wlay = add mul 4 mul d d mul 2 mul d f
181wtot = mul get og 5 wlay
182wnv = ggcd wtot 8
183wsx = ggcd wtot 64
184ggput 48 wnv
185ggput 49 wsx
186ggput 50 wnv
187ggput 51 wsx
188end
189
190note arena plan for the dims in og; returns its size in bytes
191fn ggplan to ulong
192tt = get og 1
193d = get og 2
194h = get og 3
195f = get og 4
196bb = get og 6
197r = ggr
198rd = mul r d
199pp = mul mul bb h mul tt tt
200put gga 47 0
201for i ulong 8
202ggput add 24 i rd
203end
204ggput 32 r
205ggput 33 r
206ggput 34 mul r f
207ggput 35 mul r f
208ggput 36 pp
209put gga 7 get gga 47
210put gga 47 0
211ggput 0 get og 8
212ggput 1 ggp5
213ggput 2 r
214ggput 3 r
215ggput 4 add get og 0 1
216ggput 5 r
217put gga 6 get gga 47
218ls = mul get gga 7 get og 56
219put gga 47 add get gga 47 ls
220ggput 8 rd
221ggput 9 r
222ggput 10 rd
223ggput 11 mul r get og 0
224ggput 12 mul r 3
225ggput 13 rd
226ggput 14 rd
227ggput 15 mul rd 3
228ggput 16 rd
229ggput 17 mul r f
230ggput 18 pp
231ggput 19 rd
232ggput 20 mul bb d
233ggput 21 get og 8
234ggput 22 get og 8
235sn = div ggp5 512
236if lt sn 2048
237sn = 2048
238end
239ggput 23 sn
240note 37: split-K partials for ggsk, up to 4M floats (16 MB) so 128x128 dW fills 3.0 waves on 48 SMs
241ggput 37 4194304
242note 38: packed-BF16 weight shadow (P * 2 bytes = ggp5 / 2 u32 words) for native BF16-IO 128x128 GEMMs
243ggput 38 div ggp5 2
244note 39: packed-BF16 dlogits scratch (R * V * 2 bytes) so F32->BF16 after CE is out-of-place & race-free
245ggput 39 div mul r get og 0 2
246note 41..44: NVFP4 quantized scratch buffers (A_nv4, Sa_nv4, B_nv4, Sb_nv4) when gknv4 != 0
247ggplan_nv4 r d f
248ret get gga 47
249end
250
251fn gga1 i ulong to ulong
252ret add get gga 40 get gga i
253end
254
255fn ggl l ulong f ulong to ulong
256o = add get gga 6 mul l get gga 7
257ret add add get gga 40 o get gga add 24 f
258end
259
260note address of the output of layer application l (the next hin, or HF after the last one)
261fn ggout l ulong to ulong
262if lt add l 1 get og 56
263ret ggl add l 1 0
264end
265ret gga1 8
266end
267
268fn ggw o ulong to ulong
269ret add gga1 0 mul o 4
270end
271
272fn ggwh o ulong to ulong
273ret add gga1 38 mul o 2
274end
275
276fn ggg o ulong to ulong
277ret add gga1 1 mul o 4
278end
279
280note GEMM tiles need m, n multiples of 32 and k of 16: V T D F E multiples of 32; R = B T;
281note softmax rows keep T/32 values per lane in R32..R47, so T is at most 512
282fn ggok to bool
283for i ulong 5
284v = get og i
285if eq i 3
286v = get og 7
287end
288if eq i 1
289v = get og 1
290end
291if or eq v 0 ne rem v 32 0
292ret false
293end
294end
295if gt get og 0 65536
296ret false
297end
298if gt get og 1 512
299ret false
300end
301ret le mul get og 6 get og 3 65535
302end
303
304note open the chip and emit the kernels for the dims in og
305fn ggopen to void
306if not ggok
307ggdie 1
308end
309asz = ggplan
310v = get og 0
311tt = get og 1
312d = get og 2
313h = get og 3
314f = get og 4
315bb = get og 6
316grset v tt d h f bb
317if not gropen asz
318ggsetupfail
319end
320if not gbbopen
321ggdie 122
322end
323put gga 40 gkbase
324ggstr
325end
326
327note pointer j (1-based) into arg slots 2j, 2j + 1
328fn ggpt j uint a ulong to void
329put gkarg mul j 2 gblo a
330put gkarg add mul j 2 1 gbhi a
331end
332
333fn ggu s uint v ulong to void
334put gkarg s uint v
335end
336
337fn ggrun id uint na uint gx ulong gy ulong gz ulong to void
338put gkna 0 na
339x = uint gx
340y = uint gy
341z = uint gz
342if not gklaunch id x y z
343ggdie add 100 ulong id
344end
345end
346
347fn ggmm m ulong n ulong k ulong nz ulong h2 ulong bt bool to void
348a = uint m
349b = uint n
350c = uint k
351e = uint nz
352g = uint h2
353if not gkmm a b c e g bt
354ggdie 99
355end
356end
357
358note Packed-BF16 Native-IO GEMM & conversion helpers (slots 67..74, 82..83)
359fn ggmmh m ulong n ulong k ulong nz ulong h2 ulong fl uint to void
360a = uint m
361b = uint n
362c = uint k
363e = uint nz
364g = uint h2
365if not gkmmh a b c e g fl
366ggdie 99
367end
368end
369
370note NVFP4 (OMMA.SF.16864) mode state, env check, quantizer (slots 88/89), and 128x128 GEMM (slots 90..95)
371fn ggsnv4 v uint to void
372put gknv4 0 v
373end
374
375fn ggnv4_eq n long to bool
376if ne n long len ggnv4e
377ret false
378end
379for j ulong len ggnv4e
380if ne get ggenv j get ggnv4e j
381ret false
382end
383end
384ret true
385end
386
387fn ggnv4_env to bool
388ec = ulong envcnt
389for i ulong ec bound 4096
390n = envread i ggenv 0 64
391if ggnv4_eq n
392ret true
393end
394end
395ret false
396end
397
398fn ggnv4_shape m ulong n ulong k ulong to bool
399if or eq m 0 or eq n 0 eq k 0
400ret false
401end
402if or ne rem m 128 0 ne rem n 128 0
403ret false
404end
405ret eq rem k 64 0
406end
407
408fn ggnv4_ok to bool
409if not ggbf_ok
410ret false
411end
412if eq get gknv4 0 0
413ret false
414end
415ret eq rem get og 2 128 0
416end
417
418fn ggdw_nv4_ok m ulong n ulong to bool
419if not ggnv4_ok
420ret false
421end
422if not ggnv4_shape m n ggr
423ret false
424end
425ret ge mul m n 786432
426end
427
428fn ggqnv4_p ps f64 to void
429put ggn4q 0 bitcast uint f32 ps
430end
431
432fn ggmm4_sc sa f64 sb f64 to void
433put ggn4s 0 sa
434put ggn4s 1 sb
435end
436
437note Quantize BF16 operand to NVFP4 E2M1 + UE4M3 block-16 scales:
438note tr=false -> slot 88 (row-major [m,k], stride=k*2, grid=(m,1,1))
439note tr=true -> slot 89 (transposed [k,m]->[m,k], stride=m*2, grid=(m/32,k/64,1))
440note gkarg[9] holds f32 pre_scale bits (defaults to 1.0f = 0x3f800000 when ggn4q[0] == 0)
441fn ggqnv4 src ulong dst ulong sc ulong m ulong k ulong tr bool to void
442ggpt 0 src
443ggpt 1 dst
444ggpt 2 sc
445ggu 6 m
446ggu 7 k
447ps = get ggn4q 0
448if eq ps 0
449ps = 1065353216
450end
451ggu 9 ulong ps
452if tr
453ggu 8 mul m 2
454ggrun 89 10 div m 32 div k 64 1
455ret
456end
457ggu 8 mul k 2
458ggrun 88 10 div m 8 div k 64 1
459end
460
461fn ggmm4_id fl uint ep uint to uint
462if eq ep 1
463ret 94
464end
465if eq ep 2
466ret 95
467end
468bt = ne and fl 1 0
469obf = ne and fl 2 0
470if obf
471if bt
472ret 91
473end
474ret 90
475end
476if bt
477ret 93
478end
479ret 92
480end
481
482fn ggmm4_ab anv ulong asx ulong bnv ulong bsx ulong m ulong n ulong to void
483ggpt 0 anv
484ggpt 1 asx
485ggu 4 mul m 32
486ggu 5 mul m 4
487ggu 6 0
488ggu 7 0
489ggpt 4 bnv
490ggpt 5 bsx
491ggu 12 mul n 32
492ggu 13 mul n 4
493ggu 14 0
494ggu 15 0
495end
496
497fn gggrad_sc to f64
498s = mul 2.0f64 f64 ggr
499if lt s 4096.0f64
500ret 4096.0f64
501end
502ret s
503end
504
505fn ggw4_el lp ulong wi ulong to ulong
506d = get og 2
507f = get og 4
508dd = mul d d
509df = mul d f
510wlay = add mul 4 dd mul 2 df
511base = mul lp wlay
512if lt wi 4
513ret add base mul wi dd
514end
515if eq wi 4
516ret add base mul 4 dd
517end
518ret add base add mul 4 dd df
519end
520
521fn ggw4_dt lp ulong wi ulong tr bool to ulong
522el = ggw4_el lp wi
523if tr
524ret add gga1 48 div el 2
525end
526ret add gga1 50 div el 2
527end
528
529fn ggw4_sc lp ulong wi ulong tr bool to ulong
530el = ggw4_el lp wi
531if tr
532ret add gga1 49 div el 16
533end
534ret add gga1 51 div el 16
535end
536
537fn ggmm4_ab_z m ulong n ulong ks ulong to void
538ggpt 0 gga1 41
539ggpt 1 gga1 42
540ggu 4 mul m 32
541ggu 5 mul m 4
542ggu 6 div mul ks m 2
543ggu 7 div mul ks m 16
544ggpt 4 gga1 43
545ggpt 5 gga1 44
546ggu 12 mul n 32
547ggu 13 mul n 4
548ggu 14 div mul ks n 2
549ggu 15 div mul ks n 16
550end
551
552fn ggmm4_lch_z m ulong n ulong k ulong nz ulong fl uint to void
553ep = get gkep 0
554put gkep 0 0
555put nverr 0 0
556put gkarg 23 gkmagic uint nz uint nz
557if ne get nverr 0 0
558put gkfl 0 0
559ggdie 99
560end
561put gkarg 24 neg uint nz
562put gkarg 22 uint div k 64
563put gkarg 26 0
564put gkarg 27 0
565put gkarg 28 1073741824
566put gkbp 7 0
567put gkps 0 uint m
568put gkps 1 uint n
569put gkps 2 uint k
570if ne ep 0
571ep_sc = get ggn4ep 0
572put ggn4ep 0 0
573put gkarg 6 gblo gga1 43
574put gkarg 7 gbhi gga1 43
575put gkarg 14 gblo gga1 44
576put gkarg 15 gbhi gga1 44
577put gkarg 26 uint m
578put gkarg 27 ep_sc
579put gkna 0 32
580else
581put gkna 0 29
582end
583id = ggmm4_id fl ep
584if not gkmmsw id uint div n 128 uint div m 128 uint nz 2
585ggdie 99
586end
587end
588
589fn ggmm4_lch m ulong n ulong k ulong fl uint to void
590ggmm4_lch_z m n k 1 fl
591end
592
593fn ggks_nv4 m ulong n ulong to ulong
594r = ggr
595b128 = mul div m 128 div n 128
596mn = mul m n
597s = ulong 1
598while lt mul b128 s 144 bound 8
599s2 = mul s 2
600if gt s2 16
601ret s
602end
603if ne rem r mul s2 64 0
604ret s
605end
606if gt mul mn s2 4194304
607ret s
608end
609s = s2
610end
611ret s
612end
613
614fn ggmm4_pt a ulong b ulong c ulong to void
615put ggn4p 0 a
616put ggn4p 1 b
617put ggn4p 2 c
618end
619
620note Cached-weight NVFP4 GEMM: ggn4p[0]=x, ggn4p[2]=c, ggn4s[0]=sa
621fn ggmm4_cwp lp ulong wi ulong tr bool n ulong k ulong fl uint to void
622sa = get ggn4s 0
623if eq sa 0.0f64
624sa = 1.0f64
625end
626put ggn4s 0 1.0f64
627ggmm4_ab gga1 41 gga1 42 ggw4_dt lp wi tr ggw4_sc lp wi tr ggr n
628al = div 1.0f64 mul sa 16.0f64
629gkopc get ggn4p 2 uint mul n 2 2 0 0 f32 al
630ggmm4_lch ggr n k fl
631end
632
633fn ggmm4_cwp2 lp ulong wi ulong tr bool n ulong k ulong fl uint to void
634sa = get ggn4s 0
635if eq sa 0.0f64
636sa = 1.0f64
637end
638put ggn4s 0 1.0f64
639ggmm4_ab gga1 43 gga1 44 ggw4_dt lp wi tr ggw4_sc lp wi tr ggr n
640al = div 1.0f64 mul sa 16.0f64
641gkopc get ggn4p 2 uint mul n 2 2 0 0 f32 al
642ggmm4_lch ggr n k fl
643end
644
645fn ggmm4_cw lp ulong wi ulong tr bool n ulong k ulong fl uint to void
646sa = get ggn4s 0
647if eq sa 0.0f64
648sa = 1.0f64
649end
650ggqnv4_p sa
651ggqnv4 get ggn4p 0 gga1 41 gga1 42 ggr k false
652ggqnv4_p 1.0f64
653ggmm4_cwp lp wi tr n k fl
654end
655
656note On-chip BF16->NVFP4 quantize (with 2-level tensor pre-scale ggn4s[0..1]) + K-looped 128x128 GEMM (slots 88/89 + 90..95)
657fn ggmm4 m ulong n ulong k ulong fl uint ta bool tb bool to void
658sa = get ggn4s 0
659sb = get ggn4s 1
660if eq sa 0.0f64
661sa = 1.0f64
662end
663if eq sb 0.0f64
664sb = 1.0f64
665end
666put ggn4s 0 1.0f64
667put ggn4s 1 1.0f64
668ggqnv4_p sa
669ggqnv4 get ggn4p 0 gga1 41 gga1 42 m k ta
670ggqnv4_p sb
671ggqnv4 get ggn4p 1 gga1 43 gga1 44 n k tb
672ggqnv4_p 1.0f64
673ggmm4_ab gga1 41 gga1 42 gga1 43 gga1 44 m n
674al = div 1.0f64 mul sa sb
675if ne and fl 2 0
676gkopc get ggn4p 2 uint mul n 2 2 0 0 f32 al
677else
678gkopc get ggn4p 2 uint mul n 4 4 0 0 f32 al
679end
680ggmm4_lch m n k fl
681end
682
683fn ggxwh_bf x ulong w ulong c ulong n ulong k ulong bt bool to void
684fl = uint 2
685if bt
686fl = 3
687end
688k2 = uint mul k 2
689n2 = uint mul n 2
690gkoph 0 x k2 2 0 0
691gkoph 1 w 2 n2 0 0
692gkopc c n2 2 0 0 f32 1.0f64
693ggmmh ggr n k 1 1 fl
694end
695
696fn ggxwh x ulong w ulong c ulong n ulong k ulong bt bool to void
697fl = uint 2
698if bt
699fl = 3
700end
701if and ggnv4_ok ggnv4_shape ggr n k
702ggmm4_pt x w c
703ggmm4 ggr n k fl false true
704ret
705end
706ggmm4_sc 1.0f64 1.0f64
707ggxwh_bf x w c n k bt
708end
709
710fn ggxwth x ulong w ulong c ulong n ulong k ulong bt bool to void
711fl = uint 2
712if bt
713fl = 3
714end
715if and ggnv4_ok ggnv4_shape ggr n k
716ggmm4_pt x w c
717ggmm4 ggr n k fl false false
718ret
719end
720ggmm4_sc 1.0f64 1.0f64
721k2 = uint mul k 2
722n2 = uint mul n 2
723gkoph 0 x k2 2 0 0
724gkoph 1 w k2 2 0 0
725gkopc c n2 2 0 0 f32 1.0f64
726ggmmh ggr n k 1 1 fl
727end
728
729fn ggskh m ulong n ulong nz ulong s ulong g ulong gz uint to void
730mn = mul m n
731p = gga1 37
732n4 = uint mul n 4
733c1 = uint mul mul mn s 4
734c2 = uint mul mn 4
735gkopc p n4 4 c1 c2 f32 1.0f64
736k = div ggr s
737z = mul nz s
738ggmmh m n k z s 0
739ggskr mn nz s g gz
740end
741
742fn ggf2b n ulong src ulong dst ulong to void
743n8 = div n 8
744ggu 0 n8
745ggpt 1 src
746ggpt 2 dst
747ggrun 82 6 ggcd n8 256 1 1
748end
749
750fn ggb2f n ulong src ulong dst ulong to void
751n8 = div n 8
752ggu 0 n8
753ggpt 1 src
754ggpt 2 dst
755ggrun 83 6 ggcd n8 256 1 1
756end
757
758note C[R][n] (+)= X[R][k] W[k][n]
759fn ggxw x ulong w ulong c ulong n ulong k ulong bt bool to void
760k4 = uint mul k 4
761n4 = uint mul n 4
762gkopa 0 x k4 4 0 0
763gkopa 1 w 4 n4 0 0
764gkopc c n4 4 0 0 f32 1.0f64
765ggmm ggr n k 1 1 bt
766end
767
768note C[R][n] (+)= X[R][k] W[n][k]^T
769fn ggxwt x ulong w ulong c ulong n ulong k ulong bt bool to void
770k4 = uint mul k 4
771n4 = uint mul n 4
772gkopa 0 x k4 4 0 0
773gkopa 1 w k4 4 0 0
774gkopc c n4 4 0 0 f32 1.0f64
775ggmm ggr n k 1 1 bt
776end
777
778note G[kk][n] += X[R][kk]^T DY[R][n], split over R when the grid is small (ggks)
779fn ggwg x ulong dy ulong g ulong kk ulong n ulong to void
780k4 = uint mul kk 4
781n4 = uint mul n 4
782s = ggks kk n 1
783if eq s 1
784gkopa 0 x 4 k4 0 0
785gkopa 1 dy 4 n4 0 0
786gkopc g n4 4 0 0 f32 1.0f64
787ggmm kk n ggr 1 1 true
788ret
789end
790sl = div ggr s
791gkopa 0 x 4 k4 0 uint mul mul sl kk 4
792gkopa 1 dy 4 n4 0 uint mul mul sl n 4
793ggsk kk n 1 s g 0
794end
795
796note Split-K for the weight gradients, whose depth is R = B T. A grid of (m/32)(n/32)nz blocks
797note leaves most SMs idle (rung-6 gprof: id 4 at 1.7-6 TFLOP/s on 64-256 blocks), so ggks doubles
798note the split s (up to 16, R/s a multiple of 16) until the grid reaches 1024 blocks: then
799note blocks s < 2048, so the m n nz s partials fit the 2^21 floats of slot 37.
800note Under gkbf 1 a slice depth R/s must also be a multiple of the BF16 K tile 32 (gk.om gkbfok):
801note with R = 128 (the small shape B 4, T 32) the f32 rule took s = 8, depth 16, which gkmm refuses
802note with nverr -22, so gbcmp, gbcmp2 and gtrainb ended in ggdie (exit 84). At R = 16384 (B 64, T 256)
803note every s <= 16 leaves a depth that is a multiple of 32: the split there is unchanged.
804fn ggks m ulong n ulong nz ulong to ulong
805r = ggr
806if ggbf_ok
807b128 = mul mul div m 128 div n 128 nz
808if ge b128 96
809ret 1
810end
811mnz = mul mul m n nz
812s = ulong 1
813while lt mul b128 s 288 bound 8
814s2 = mul s 2
815if gt s2 16
816ret s
817end
818if ne rem r mul s2 32 0
819ret s
820end
821if gt mul mnz s2 4194304
822ret s
823end
824s = s2
825end
826ret s
827end
828b = mul mul div m 32 div n 32 nz
829kq = ulong 16
830if eq get gkbf 0 1
831kq = 32
832end
833s = ulong 1
834while lt mul b s 1024 bound 8
835s2 = mul s 2
836if or gt s2 16 ne rem r mul s2 kq 0
837ret s
838end
839s = s2
840end
841ret s
842end
843
844note Operands A and B are set (gkopa) with the slice stride as s2. Batch z = z1 s + z2 (z1 < nz the
845note caller's batch, z2 < s the slice) writes partial P[z1][z2] = A[z1 z2]^T B[z1 z2] into slot 37,
846note then COLSUM 14 adds G[z1] (byte stride gz) += sum over z2 of P[z1][z2], in slice order.
847note Mutant p (OMUTP 0) adds one slice fewer, which gcmp2 must catch.
848const OMUTP ulong 1
849fn ggsk m ulong n ulong nz ulong s ulong g ulong gz uint to void
850mn = mul m n
851p = gga1 37
852n4 = uint mul n 4
853c1 = uint mul mul mn s 4
854c2 = uint mul mn 4
855gkopc p n4 4 c1 c2 f32 1.0f64
856k = div ggr s
857z = mul nz s
858ggmm m n k z s false
859ggskr mn nz s g gz
860end
861
862note COLSUM 14 over slot 37: G[c gz + i] += sum over z2 < s of P[c][z2][i], vectorized 256 thr x 4 floats
863fn ggskr mn ulong nz ulong s ulong g ulong gz uint to void
864kc = s
865if eq OMUTP 0
866kc = sub s 1
867end
868mn4 = div mn 4
869ggu 0 mn4
870ggu 1 kc
871ggpt 1 gga1 37
872ggu 4 mul mn 4
873ggu 5 mul mul mn s 4
874ggpt 3 g
875ggu 8 ulong gz
876cid = uint 14
877if ggow_ok
878cid = 13
879end
880ggrun cid 9 ggcd mn4 256 nz 1
881end
882
883note 19 COPY (x into y), 20 ZERO (x) over n elements (256 threads x 4 floats via LDG.E.128/STG.E.128)
884fn ggew id uint n ulong x ulong y ulong to void
885n4 = div n 4
886ggu 0 n4
887ggpt 1 x
888ggpt 2 y
889ggrun id 6 ggcd n4 256 1 1
890end
891
892fn ggrmsf_h v ulong g ulong y ulong s ulong to void
893r = mul ggr 32
894ggu 0 r
895ggpt 1 v
896ggpt 2 g
897ggpt 3 y
898ggpt 4 s
899ggpt 5 0
900ggpt 6 0
901ggu 14 ggr
902ggu 15 0
903ggrun 75 16 ggcd r 256 1 1
904end
905
906fn ggrmsf_h_nv4 v ulong g ulong y ulong s ulong to void
907r = mul ggr 32
908ggu 0 r
909ggpt 1 v
910ggpt 2 g
911ggpt 3 y
912ggpt 4 s
913ggpt 5 gga1 41
914ggpt 6 gga1 42
915ggu 14 ggr
916ggu 15 1
917ggrun 75 16 ggcd r 256 1 1
918end
919
920global ggdgb ulong 2
921fn ggrms_g to ulong
922g = div ggr 8
923if and le get og 2 512 gt g 144
924g = 144
925end
926ret g
927end
928fn ggow_ok to bool
929if not ggbf_ok
930ret false
931end
932if ne get og 59 1
933ret false
934end
935ret eq ggfzn 0
936end
937
938fn ggsk_nv4 dy ulong g ulong kk ulong n ulong sb f64 sa f64 to void
939s = ggks_nv4 kk n
940ggqnv4_p sb
941ggqnv4 dy gga1 43 gga1 44 n ggr true
942ggqnv4_p 1.0f64
943al = div 1.0f64 mul sa sb
944n4 = uint mul n 4
945if eq s 1
946fl = uint 1
947if ggow_ok
948fl = 0
949end
950ggmm4_ab gga1 41 gga1 42 gga1 43 gga1 44 kk n
951gkopc g n4 4 0 0 f32 al
952ggmm4_lch kk n ggr fl
953ret
954end
955ks = div ggr s
956mn = mul kk n
957ggmm4_ab_z kk n ks
958gkopc gga1 37 n4 4 0 uint mul mn 4 f32 al
959ggmm4_lch_z kk n ks s 0
960ggskr mn 1 s g 0
961end
962
963fn ggwgh_bf x ulong dy ulong g ulong kk ulong n ulong to void
964k2 = uint mul kk 2
965n2 = uint mul n 2
966n4 = uint mul n 4
967s = ggks kk n 1
968if eq s 1
969fl = uint 1
970if ggow_ok
971fl = 0
972end
973gkoph 0 x 2 k2 0 0
974gkoph 1 dy 2 n2 0 0
975gkopc g n4 4 0 0 f32 1.0f64
976ggmmh kk n ggr 1 1 fl
977ret
978end
979sl = div ggr s
980gkoph 0 x 2 k2 0 uint mul mul sl kk 2
981gkoph 1 dy 2 n2 0 uint mul mul sl n 2
982ggskh kk n 1 s g 0
983end
984
985fn ggwgh x ulong dy ulong g ulong kk ulong n ulong to void
986sa = get ggn4s 0
987sb = get ggn4s 1
988if eq sa 0.0f64
989sa = 1.0f64
990end
991if eq sb 0.0f64
992sb = 1.0f64
993end
994put ggn4s 0 1.0f64
995put ggn4s 1 1.0f64
996if ggdw_nv4_ok kk n
997ggqnv4_p sa
998ggqnv4 x gga1 41 gga1 42 kk ggr true
999ggsk_nv4 dy g kk n sb sa
1000ret
1001end
1002ggwgh_bf x dy g kk n
1003end
1004fn ggain_cnt to ulong
1005if and ggbf_ok le ggrms_g 256
1006ret 1
1007end
1008ret 2
1009end
1010fn ggdef_gain to bool
1011if not ggbf_ok
1012ret false
1013end
1014if ne get og 59 1
1015ret false
1016end
1017gx = ggrms_g
1018if gt gx 256
1019ret false
1020end
1021nl = sub get og 56 ggfzl
1022if or eq nl 0 gt nl 16
1023ret false
1024end
1025ret le mul mul 2 nl gx ggr
1026end
1027fn ggrmsb_h_ex v ulong g ulong s ulong ws ulong owdh ulong to void
1028r = mul ggr 32
1029gx = ggrms_g
1030ggu 0 r
1031ggpt 1 v
1032ggpt 2 g
1033ggpt 3 s
1034ggpt 4 gga1 14
1035ggpt 5 gga1 13
1036ggpt 6 ws
1037ggu 14 owdh
1038ggu 15 mul gx 256
1039ggrun 76 16 gx 1 1
1040end
1041fn ggrmsb_h v ulong g ulong s ulong to void
1042ggrmsb_h_ex v g s gga1 19 0
1043end
1044fn ggws_off idx ulong to ulong
1045if eq get ggdgb 1 0
1046ret gga1 19
1047end
1048ret add gga1 19 mul mul idx ggrms_g mul get og 2 4
1049end
1050fn ggrmsb_gain l ulong ffn bool to void
1051idx = sub l get ggdgb 0
1052if not ffn
1053idx = add get ggdgb 1 idx
1054end
1055ws = ggws_off idx
1056v = ggl l 0
1057go = ovg1 l
1058s = ggl l 8
1059if ffn
1060v = ggl l 6
1061go = ovg2 l
1062s = ggl l 9
1063end
1064g = ggw go
1065ggrmsb_h_ex v g s ws 0
1066if eq get ggdgb 1 0
1067ggain_h go
1068end
1069end
1070
1071note RMSF/RMSB run one warp per row: arg 0 and the thread count are R*32
1072fn ggrmsf v ulong g ulong y ulong s ulong to void
1073r = mul ggr 32
1074ggu 0 r
1075ggpt 1 v
1076ggpt 2 g
1077ggpt 3 y
1078ggpt 4 s
1079ggrun 10 10 ggcd r 64 1 1
1080end
1081
1082note dh += RMS backward of da; the gain terms go to WS
1083fn ggrmsb v ulong g ulong s ulong to void
1084r = mul ggr 32
1085ggu 0 r
1086ggpt 1 v
1087ggpt 2 g
1088ggpt 3 s
1089ggpt 4 gga1 14
1090ggpt 5 gga1 13
1091ggpt 6 gga1 19
1092ggrun 11 14 ggcd r 64 1 1
1093end
1094
1095note gain gradient at parameter offset go += column sums of WS (grid-stride <=144xD or 8-warp CTA-reduced R/8)
1096fn ggain_h go ulong to void
1097d = get og 2
1098d4 = div d 4
1099gx = ggrms_g
1100cid = uint 14
1101if ggow_ok
1102cid = 13
1103end
1104if le gx 256
1105ggu 0 d4
1106ggu 1 gx
1107ggpt 1 gga1 19
1108ggu 4 mul d 4
1109ggu 5 0
1110ggpt 3 ggg go
1111ggu 8 0
1112ggrun cid 9 ggcd d4 256 1 1
1113ret
1114end
1115tt8 = div get og 1 8
1116bb = get og 6
1117ggu 0 d4
1118ggu 1 tt8
1119ggpt 1 gga1 19
1120ggu 4 mul d 4
1121ggu 5 mul mul tt8 d 4
1122ggpt 3 gga1 20
1123ggu 8 mul d 4
1124ggrun 13 9 ggcd d4 256 bb 1
1125ggu 0 d4
1126ggu 1 bb
1127ggpt 1 gga1 20
1128ggu 4 mul d 4
1129ggu 5 0
1130ggpt 3 ggg go
1131ggu 8 0
1132ggrun cid 9 ggcd d4 256 1 1
1133end
1134fn ggain_def lo ulong nl ulong to void
1135d = get og 2
1136d4 = div d 4
1137gx = ggrms_g
1138cid = uint 14
1139if ggow_ok
1140cid = 13
1141end
1142gd4 = mul mul gx d 4
1143l4 = mul get og 32 4
1144ggu 0 d4
1145ggu 1 gx
1146ggpt 1 gga1 19
1147ggu 4 mul d 4
1148ggu 5 gd4
1149ggpt 3 ggg ovg2 lo
1150ggu 8 l4
1151ggrun cid 9 ggcd d4 256 nl 1
1152ggu 0 d4
1153ggu 1 gx
1154ggpt 1 add gga1 19 mul nl gd4
1155ggu 4 mul d 4
1156ggu 5 gd4
1157ggpt 3 ggg ovg1 lo
1158ggu 8 l4
1159ggrun cid 9 ggcd d4 256 nl 1
1160end
1161
1162note gain gradient at parameter offset go += column sums of WS: vectorized 256 thr x 4 floats
1163fn ggain go ulong to void
1164d = get og 2
1165d4 = div d 4
1166tt = get og 1
1167bb = get og 6
1168ggu 0 d4
1169ggu 1 tt
1170ggpt 1 gga1 19
1171ggu 4 mul d 4
1172ggu 5 mul mul tt d 4
1173ggpt 3 gga1 20
1174ggu 8 mul d 4
1175ggrun 13 9 ggcd d4 256 bb 1
1176ggu 0 d4
1177ggu 1 bb
1178ggpt 1 gga1 20
1179ggu 4 mul d 4
1180ggu 5 0
1181ggpt 3 ggg go
1182ggu 8 0
1183ggrun 14 9 ggcd d4 256 1 1
1184end
1185
1186note softmax rows: one warp per row, T 32 threads per score block, grid (T/2, B H)
1187fn ggsoft id uint p ulong dp ulong to void
1188tt = get og 1
1189ggu 0 mul tt 32
1190ggpt 1 p
1191ggpt 2 dp
1192ggrun id 6 div tt 2 mul get og 6 get og 3 1
1193end
1194
1195note attention GEMM over the B H (sequence, head) batch; operands set by gghd/ggpl/ggch/ggcp first;
1196note cz: gkopz causal mode (1 A = P or DS, 2 A = P^T or DS^T, 3 C = S or DP)
1197fn ggbh m ulong n ulong k ulong bt bool cz uint to void
1198gkopz cz
1199ggmm m n k mul get og 6 get og 3 get og 3 bt
1200end
1201
1202note head-layout operand (rows of E inside D-wide rows, batch b then head); tr: k runs along rows
1203fn gghd ab uint p ulong tr bool to void
1204s0 = get ggs 0
1205s1 = get ggs 1
1206s2 = get ggs 2
1207if tr
1208gkopa ab p 4 s0 s1 s2
1209else
1210gkopa ab p s0 4 s1 s2
1211end
1212end
1213
1214note T x T score operand (batch b then head); tr: transposed
1215fn ggpl ab uint p ulong tr bool to void
1216s3 = get ggs 3
1217s4 = get ggs 4
1218s5 = get ggs 5
1219if tr
1220gkopa ab p 4 s3 s5 s4
1221else
1222gkopa ab p s3 4 s5 s4
1223end
1224end
1225
1226fn ggch c ulong al f64 to void
1227s0 = get ggs 0
1228s1 = get ggs 1
1229s2 = get ggs 2
1230gkopc c s0 4 s1 s2 f32 al
1231end
1232
1233fn ggcp c ulong al f64 to void
1234s3 = get ggs 3
1235s4 = get ggs 4
1236s5 = get ggs 5
1237gkopc c s3 4 s5 s4 f32 al
1238end
1239
1240note stride table for the dims in og (bytes)
1241fn ggstr to void
1242d = get og 2
1243tt = get og 1
1244put ggs 0 uint mul d 4
1245put ggs 1 uint mul tt mul d 4
1246put ggs 2 uint mul get og 7 4
1247put ggs 3 uint mul tt 4
1248put ggs 4 uint mul tt mul tt 4
1249put ggs 5 uint mul get og 3 mul tt mul tt 4
1250put ggs 6 uint mul ggr mul d 4
1251put ggs 7 uint mul d mul d 4
1252end
1253
1254fn ggqkv_nv4 l ulong to void
1255d = get og 2
1256lp = olp l
1257if ne get ggw4_ok 0 0
1258for p ulong 3
1259ggmm4_ab gga1 41 gga1 42 ggw4_dt lp p true ggw4_sc lp p true ggr d
1260gkopc add ggl l 2 mul p ulong get ggs 6 uint mul d 2 2 0 0 f32 0.0625f64
1261ggmm4_lch ggr d d 2
1262end
1263ret
1264end
1265ggqnv4_p 1.0f64
1266ggqnv4 ggl l 1 gga1 41 gga1 42 ggr d false
1267dd2 = mul mul d d 2
1268w0 = ggwh ovwq l
1269ggqnv4_p 16.0f64
1270for p ulong 3
1271ggqnv4 add w0 mul p dd2 gga1 43 gga1 44 d d true
1272ggmm4_ab gga1 41 gga1 42 gga1 43 gga1 44 ggr d
1273gkopc add ggl l 2 mul p ulong get ggs 6 uint mul d 2 2 0 0 f32 0.0625f64
1274ggmm4_lch ggr d d 2
1275end
1276ggqnv4_p 1.0f64
1277end
1278
1279note q, k, u = a wq, a wk, a wv as one batched GEMM (wq wk wv and q k u are contiguous)
1280fn ggqkv l ulong to void
1281d = get og 2
1282a = ggl l 1
1283c = ggl l 2
1284if and ggnv4_ok ggnv4_shape ggr d d
1285ggqkv_nv4 l
1286ret
1287end
1288if ggbf_ok
1289d2 = uint mul d 2
1290s6 = get ggs 6
1291w2 = ggwh ovwq l
1292gkoph 0 a d2 2 0 0
1293gkoph 1 w2 2 d2 0 uint mul mul d d 2
1294gkopc c d2 2 0 s6 f32 1.0f64
1295ggmmh ggr d d 3 3 2
1296ret
1297end
1298w = ggw ovwq l
1299s0 = get ggs 0
1300s6 = get ggs 6
1301s7 = get ggs 7
1302gkopa 0 a s0 4 0 0
1303gkopa 1 w 4 s0 0 s7
1304gkopc c s0 4 0 s6 f32 1.0f64
1305ggmm ggr d d 3 3 false
1306end
1307
1308note true when SM121 HMMA.16816 Fused Flash-Attention-2 (slots 63..66) is active for this shape
1309fn ggfa_ok to bool
1310if or eq get gkbf 0 0 ne get gkbck 0 0
1311ret false
1312end
1313if eq OMUTG 0
1314ret false
1315end
1316tt = get og 1
1317if or lt tt 64 ne rem tt 64 0
1318ret false
1319end
1320e = get og 7
1321ret or eq e 32 eq e 64
1322end
1323note true when 128x128 fast epilogues (residual-fused beta + single-store ReLU) are active in ggffn/ggbffn
1324note ggnofp[0] = 1 during gtrainf/gtrainb/gprof (skips duplicate FP store; gcmp2/gbcmp2 keeps FP for ogmask rho check)
1325global ggnofp uint 1
1326fn ggsnofp v uint to void
1327put ggnofp 0 v
1328end
1329fn ggfast_ffn to bool
1330if or eq get gkbf 0 0 ne get gkbck 0 0
1331ret false
1332end
1333if or ne rem ggr 128 0 ne rem get og 2 128 0
1334ret false
1335end
1336ret eq rem get og 4 128 0
1337end
1338fn ggskip_fp to bool
1339ret and ggfast_ffn ne get ggnofp 0 0
1340end
1341note true when full Packed-BF16 Native-IO training mode (slots 67..83, 2 B/elem activations + W_bf16) is active
1342fn ggbf_ok to bool
1343if not ggskip_fp
1344ret false
1345end
1346ret ggfa_ok
1347end
1348note P = softmax(q k^T / sqrt(E)), o = P u; uses SM121 HMMA.16816 Flash-Attention-2 (FA2F slot 63) when ggfa_ok
1349fn ggatt l ulong to void
1350tt = get og 1
1351e = get og 7
1352pp = ggl l 12
1353q = ggl l 2
1354k = ggl l 3
1355u = ggl l 4
1356o = ggl l 5
1357if ggfa_ok
1358ggpt 0 q
1359ggpt 1 k
1360ggpt 2 u
1361ggpt 3 o
1362ggpt 4 pp
1363fid = uint 63
1364if ggbf_ok
1365fid = 77
1366end
1367ggrun fid 10 div tt 64 get og 3 get og 6
1368ret
1369end
1370gghd 0 q false
1371gghd 1 k false
1372isq = div 1.0f64 fsqrt f64 e
1373ggcp pp isq
1374ggbh tt tt e false 3
1375ggsoft 15 pp pp
1376ggpl 0 pp false
1377gghd 1 u true
1378ggch o 1.0f64
1379ggbh tt e tt false 1
1380end
1381
1382note hm = hin + o wo; bn = rms(hm) g2; out = hm + relu(bn w1) w2
1383fn ggffn_h l ulong to void
1384d = get og 2
1385f = get og 4
1386hm = ggl l 6
1387lp = olp l
1388cw = and ne get ggw4_ok 0 0 and ggnv4_ok and ggnv4_shape ggr d d ggnv4_shape ggr f d
1389gkopfl 8 ggl l 0
1390if and cw ge d 512
1391ggmm4_pt ggl l 5 0 hm
1392ggmm4_sc 1.0f64 1.0f64
1393ggmm4_cw lp 3 true d d 3
1394else
1395ggxwh_bf ggl l 5 ggwh ovwo l hm d d true
1396end
1397if cw
1398ggrmsf_h_nv4 hm ggw ovg2 l ggl l 7 ggl l 9
1399gkope 1 ggl l 11
1400put ggn4ep 0 1065353216
1401ggmm4_pt ggl l 7 0 ggl l 10
1402ggmm4_sc 1.0f64 1.0f64
1403ggmm4_cwp lp 4 true f d 2
1404else
1405ggrmsf_h hm ggw ovg2 l ggl l 7 ggl l 9
1406gkope 1 ggl l 11
1407ggmm4_sc 1.0f64 16.0f64
1408ggxwh ggl l 7 ggwh ovw1 l ggl l 10 f d false
1409end
1410gkopfl 8 hm
1411if cw
1412ggmm4_pt ggl l 11 0 ggout l
1413ggmm4_sc 1.0f64 1.0f64
1414ggmm4_cwp2 lp 5 true d f 3
1415else
1416ggmm4_sc 1.0f64 16.0f64
1417ggxwh ggl l 11 ggwh ovw2 l ggout l d f true
1418end
1419end
1420
1421fn ggffn l ulong to void
1422if ggbf_ok
1423ggffn_h l
1424ret
1425end
1426d = get og 2
1427f = get og 4
1428rd = mul ggr d
1429hm = ggl l 6
1430x = ggl l 0
1431ff = ggfast_ffn
1432if not ff
1433ggew 19 rd x hm
1434end
1435x5 = ggl l 5
1436w = ggw ovwo l
1437if ff
1438gkopfl 8 x
1439end
1440ggxw x5 w hm d d true
1441x7 = ggl l 7
1442w = ggw ovg2 l
1443s = ggl l 9
1444ggrmsf hm w x7 s
1445fp = ggl l 10
1446w = ggw ovw1 l
1447y = ggl l 11
1448gkope 1 y
1449if ggskip_fp
1450gkopfl 4 0
1451end
1452ggxw x7 w fp f d false
1453o = ggout l
1454if not ff
1455ggew 19 rd hm o
1456end
1457w = ggw ovw2 l
1458if ff
1459gkopfl 8 hm
1460end
1461ggxw y w o d f true
1462end
1463
1464fn ggfl l ulong to void
1465x = ggl l 0
1466w = ggw ovg1 l
1467a = ggl l 1
1468s = ggl l 8
1469if ggbf_ok
1470if and ne get ggw4_ok 0 0 and ggnv4_ok ggnv4_shape ggr get og 2 get og 2
1471ggrmsf_h_nv4 x w a s
1472else
1473ggrmsf_h x w a s
1474end
1475else
1476ggrmsf x w a s
1477end
1478ggqkv l
1479ggatt l
1480ggffn l
1481end
1482
1483note ggnoce 1 (oglast): stop after logits GEMM. ggnoce 2 (gfcache): stop right after the LV layers
1484note (leaving final hidden state HF in gga1 8, skipping final RMS, logits and CE).
1485global ggnoce uint 1
1486fn ggb_ok to bool
1487ret and and eq get gkbck 0 0 eq get gkpf 0 0 and eq get gkbat 0 0 gt get gbpkt 0 0
1488end
1489
1490fn ggquant_w1 lp ulong to void
1491d = get og 2
1492f = get og 4
1493w0 = ggwh opwq lp
1494dd2 = mul mul d d 2
1495for p ulong 3
1496w = add w0 mul p dd2
1497ggqnv4 w ggw4_dt lp p true ggw4_sc lp p true d d true
1498end
1499if ge d 512
1500wo = add w0 mul 3 dd2
1501ggqnv4 wo ggw4_dt lp 3 true ggw4_sc lp 3 true d d true
1502ggqnv4 wo ggw4_dt lp 3 false ggw4_sc lp 3 false d d false
1503end
1504w1 = ggwh opw1 lp
1505ggqnv4 w1 ggw4_dt lp 4 true ggw4_sc lp 4 true f d true
1506ggqnv4 w1 ggw4_dt lp 4 false ggw4_sc lp 4 false d f false
1507w2 = ggwh opw2 lp
1508ggqnv4 w2 ggw4_dt lp 5 true ggw4_sc lp 5 true d f true
1509ggqnv4 w2 ggw4_dt lp 5 false ggw4_sc lp 5 false f d false
1510end
1511
1512fn ggquant_w to void
1513put ggw4_ok 0 0
1514if not ggnv4_ok
1515ret
1516end
1517d = get og 2
1518f = get og 4
1519if or not ggnv4_shape d d d not ggnv4_shape f d d
1520ret
1521end
1522ggqnv4_p 16.0f64
1523nl = get og 5
1524per = ulong 7
1525if ge d 512
1526per = 9
1527end
1528if and ggb_ok le mul nl per 512
1529if not gbbegin mul nl per
1530ggdie 120
1531end
1532put gkbat 0 1
1533for lp ulong nl bound 64
1534ggquant_w1 lp
1535end
1536put gkbat 0 0
1537if not gkbsub
1538ggdie 121
1539end
1540else
1541for lp ulong nl bound 64
1542ggquant_w1 lp
1543end
1544end
1545ggqnv4_p 1.0f64
1546put ggw4_ok 0 1
1547end
1548fn ggfwd_emb to void
1549d = get og 2
1550x = ggl 0 0
1551if ggbf_ok
1552x = gga1 13
1553end
1554ggu 0 d
1555ggpt 1 x
1556x = ggw 0
1557ggpt 2 x
1558x = ggw get og 30
1559ggpt 3 x
1560x = gga1 2
1561ggpt 4 x
1562tt = get og 1
1563bb = get og 6
1564ggrun 9 10 ggcd d 64 tt bb
1565if ggbf_ok
1566ggf2b mul ggr d gga1 13 ggl 0 0
1567end
1568end
1569fn ggfwd_head to void
1570if eq get ggnoce 0 2
1571ret
1572end
1573d = get og 2
1574v = get og 0
1575if ggbf_ok
1576ggrmsf_h gga1 8 ggw get og 33 gga1 10 gga1 9
1577d2 = uint mul d 2
1578v2 = uint mul v 2
1579gkoph 0 gga1 10 d2 2 0 0
1580gkoph 1 ggwh 0 d2 2 0 0
1581gkopc gga1 11 v2 2 0 0 f32 1.0f64
1582ggmmh ggr v d 1 1 2
1583if ne get ggnoce 0 0
1584ret
1585end
1586r = mul ggr 32
1587ggu 0 r
1588ggpt 1 gga1 11
1589ggpt 2 gga1 3
1590ggpt 3 gga1 12
1591ggpt 4 gga1 39
1592ggrun 87 10 ggcd r 256 1 1
1593ret
1594end
1595x = gga1 8
1596w = ggw get og 33
1597z = gga1 10
1598s = gga1 9
1599ggrmsf x w z s
1600x = gga1 11
1601w = ggw 0
1602ggxwt z w x v d false
1603if ne get ggnoce 0 0
1604ret
1605end
1606r = mul ggr 32
1607ggu 0 r
1608ggpt 1 x
1609x = gga1 3
1610ggpt 2 x
1611x = gga1 12
1612ggpt 3 x
1613ggrun 21 8 ggcd r 64 1 1
1614end
1615fn ggfwd_hcnt to ulong
1616nc = get ggnoce 0
1617if eq nc 2
1618ret 0
1619end
1620if eq nc 1
1621ret 2
1622end
1623ret 3
1624end
1625fn ggfwd_ecnt to ulong
1626if ggbf_ok
1627ret 2
1628end
1629ret 1
1630end
1631fn ggfwd_lcnt to ulong
1632c = ulong 11
1633if ggfa_ok
1634c = sub c 2
1635end
1636if ggfast_ffn
1637c = sub c 2
1638end
1639if ggnv4_ok
1640d = get og 2
1641f = get og 4
1642r = ggr
1643cw = and ne get ggw4_ok 0 0 and ggnv4_shape r d d ggnv4_shape r f d
1644if cw
1645note QKV: 3 GEMMs vs 1 BF16 (+2, 0 quant!); W1: 0 quant; W2: 0 quant (fused into slot 94!); O: +1 quant iff d>=512
1646c = add c 2
1647if ge d 512
1648c = add c 1
1649end
1650else
1651if ggnv4_shape r d d
1652c = add c 6
1653end
1654if ggnv4_shape r f d
1655c = add c 2
1656end
1657if ggnv4_shape r d f
1658c = add c 2
1659end
1660end
1661end
1662ret c
1663end
1664fn ggfwd_bat to void
1665lv = get og 56
1666lc = ggfwd_lcnt
1667l0 = ulong 0
1668while lt l0 lv bound 1024
1669l1 = add l0 16
1670if gt l1 lv
1671l1 = lv
1672end
1673cnt = mul sub l1 l0 lc
1674if eq l0 0
1675cnt = add cnt ggfwd_ecnt
1676end
1677if eq l1 lv
1678cnt = add cnt ggfwd_hcnt
1679end
1680if not gbbegin cnt
1681ggdie 120
1682end
1683put gkbat 0 1
1684if eq l0 0
1685ggfwd_emb
1686end
1687l = l0
1688while lt l l1 bound 32
1689ggfl l
1690l = add l 1
1691end
1692if eq l1 lv
1693ggfwd_head
1694end
1695put gkbat 0 0
1696if not gkbsub
1697ggdie 121
1698end
1699l0 = l1
1700end
1701end
1702fn ggfwd to void
1703if ggb_ok
1704ggfwd_bat
1705ret
1706end
1707ggfwd_emb
1708for l ulong get og 56 bound 9223372036854775807
1709ggfl l
1710end
1711ggfwd_head
1712end
1713
1714fn ggbffn_h l ulong to void
1715d = get og 2
1716f = get og 4
1717lp = olp l
1718gs = gggrad_sc
1719cw = and ne get ggw4_ok 0 0 and ggnv4_ok and ggnv4_shape ggr f d ggnv4_shape ggr d f
1720gkope 2 ggl l 11
1721if cw
1722put ggn4ep 0 bitcast uint f32 gs
1723ggmm4_pt gga1 13 0 gga1 17
1724ggmm4_sc gs 1.0f64
1725ggmm4_cw lp 5 false f d 2
1726ggmm4_pt gga1 17 0 gga1 14
1727ggmm4_sc gs 1.0f64
1728ggmm4_cwp2 lp 4 false d f 2
1729else
1730ggmm4_sc gs 16.0f64
1731ggxwth gga1 13 ggwh ovw2 l gga1 17 f d false
1732end
1733if or eq OMUTLG 1 eq lp l
1734ggmm4_sc 1.0f64 gs
1735ggwgh ggl l 11 gga1 13 ggg ovw2 l f d
1736end
1737ggmm4_sc 1.0f64 gs
1738ggwgh ggl l 7 gga1 17 ggg ovw1 l d f
1739if not cw
1740ggmm4_sc gs 16.0f64
1741ggxwth gga1 17 ggwh ovw1 l gga1 14 d f false
1742end
1743ggrmsb_gain l true
1744end
1745
1746note layer l FFN backward: DH holds d out on entry, d hm on exit. The dF GEMM's fused epilogue (gkope 2)
1747note applies the ReLU mask FP > 0; the forward W1 GEMM (ggffn, gkope 1) stores both FP and Y.
1748fn ggbffn l ulong to void
1749if ggbf_ok
1750ggbffn_h l
1751ret
1752end
1753d = get og 2
1754f = get og 4
1755dh = gga1 13
1756df = gga1 17
1757da = gga1 14
1758w = ggw ovw2 l
1759x = ggl l 10
1760if ggskip_fp
1761x = ggl l 11
1762end
1763gkope 2 x
1764ggxwt dh w df f d false
1765x = ggl l 11
1766w = ggg ovw2 l
1767if or eq OMUTLG 1 eq olp l l
1768ggwg x dh w f d
1769end
1770x = ggl l 7
1771w = ggg ovw1 l
1772ggwg x df w d f
1773w = ggw ovw1 l
1774ggxwt df w da d f false
1775x = ggl l 6
1776w = ggw ovg2 l
1777s = ggl l 9
1778ggrmsb x w s
1779ggain ovg2 l
1780end
1781
1782fn ggfa_bwd l ulong to void
1783tt = get og 1
1784h = get og 3
1785bb = get og 6
1786dn = gga1 16
1787pp = ggl l 12
1788dp = gga1 18
1789dq = gga1 15
1790dk = add dq ulong get ggs 6
1791du = add dk ulong get ggs 6
1792id_d = uint 64
1793id_q = uint 65
1794id_kv = uint 66
1795if ggbf_ok
1796id_d = 78
1797id_q = 79
1798id_kv = 80
1799end
1800ggpt 0 ggl l 5
1801ggpt 1 dn
1802ggpt 2 dp
1803ggrun id_d 6 div tt 2 h bb
1804ggpt 0 ggl l 2
1805ggpt 1 ggl l 3
1806ggpt 2 ggl l 4
1807ggpt 3 dn
1808ggpt 4 pp
1809ggpt 5 dp
1810ggpt 6 dq
1811ggrun id_q 14 div tt 64 h bb
1812ggpt 0 ggl l 2
1813ggpt 1 ggl l 3
1814ggpt 2 ggl l 4
1815ggpt 3 dn
1816ggpt 4 pp
1817ggpt 5 dp
1818ggpt 6 dk
1819ggpt 7 du
1820ggrun id_kv 16 div tt 64 h bb
1821end
1822
1823note layer l attention backward: DO, DP, DU, DS (in DP), DQ, DK, then DA. Causal as in ggatt: DP
1824note on and below the diagonal (cz 3, SOFTB reads dp above it as 0), DU and DK start at the diagonal
1825note (cz 2), DQ stops there (cz 1).
1826fn ggbatt l ulong to void
1827d = get og 2
1828tt = get og 1
1829e = get og 7
1830dh = gga1 13
1831dn = gga1 16
1832if ggbf_ok
1833gs = gggrad_sc
1834if and ge d 512 and ne get ggw4_ok 0 0 and ggnv4_ok ggnv4_shape ggr d d
1835ggmm4_pt dh 0 dn
1836ggmm4_sc gs 1.0f64
1837ggmm4_cw olp l 3 false d d 2
1838else
1839k2 = uint mul d 2
1840gkoph 0 dh k2 2 0 0
1841gkoph 1 ggwh ovwo l k2 2 0 0
1842gkopc dn k2 2 0 0 f32 1.0f64
1843ggmmh ggr d d 1 1 2
1844end
1845ggmm4_sc 1.0f64 gs
1846ggwgh ggl l 5 dh ggg ovwo l d d
1847ggfa_bwd l
1848ret
1849end
1850w = ggw ovwo l
1851ggxwt dh w dn d d false
1852x = ggl l 5
1853w = ggg ovwo l
1854ggwg x dh w d d
1855if ggfa_ok
1856ggfa_bwd l
1857ret
1858end
1859dp = gga1 18
1860pp = ggl l 12
1861dq = gga1 15
1862dk = add dq ulong get ggs 6
1863du = add dk ulong get ggs 6
1864x = ggl l 4
1865gghd 0 dn false
1866gghd 1 x false
1867ggcp dp 1.0f64
1868ggbh tt tt e false 3
1869ggpl 0 pp true
1870gghd 1 dn true
1871ggch du 1.0f64
1872ggbh tt e tt false 2
1873ggsoft 16 pp dp
1874x = ggl l 3
1875ggpl 0 dp false
1876gghd 1 x true
1877ggch dq 1.0f64
1878ggbh tt e tt false 1
1879al = 1.0f64
1880if eq OMUTG 0
1881al = neg 1.0f64
1882end
1883x = ggl l 2
1884ggpl 0 dp true
1885gghd 1 x true
1886ggch dk al
1887ggbh tt e tt false 2
1888end
1889
1890fn ggbqkv_h l ulong to void
1891d = get og 2
1892da = gga1 14
1893dq = gga1 15
1894d2 = uint mul d 2
1895s6 = get ggs 6
1896s7 = get ggs 7
1897dd2 = uint mul mul d d 2
1898note Fused 3-way GEMM (slot 85): dA = dQ Wq^T + dK Wk^T + dV Wv^T in ONE launch!
1899gkoph 0 dq d2 2 sub s6 d2 0
1900gkoph 1 ggwh ovwq l d2 2 sub dd2 d2 0
1901gkopc da d2 2 0 0 f32 1.0f64
1902ggmmh ggr d d 1 1 8
1903a = ggl l 1
1904w = ggg ovwq l
1905if ggdw_nv4_ok d d
1906ggqnv4_p 1.0f64
1907ggqnv4 a gga1 41 gga1 42 d ggr true
1908for p ulong 3
1909ggsk_nv4 add dq mul p ulong s6 add w mul p ulong s7 d d gggrad_sc 1.0f64
1910end
1911else
1912k = ggks d d 3
1913if eq k 1
1914gkoph 0 a 2 d2 0 0
1915gkoph 1 dq 2 d2 0 s6
1916gkopc w get ggs 0 4 0 s7 f32 1.0f64
1917ggmmh d d ggr 3 3 1
1918else
1919sl = uint div ggr k
1920gkoph 0 a 2 d2 0 mul sl d2
1921gkoph 1 dq 2 d2 s6 mul sl d2
1922ggskh d d 3 k w s7
1923end
1924end
1925ggrmsb_gain l false
1926end
1927
1928note DA = DQ wq^T + DK wk^T + DU wv^T; G wq wk wv += a^T (DQ DK DU) batched; then the rms backward
1929fn ggbqkv l ulong to void
1930if ggbf_ok
1931ggbqkv_h l
1932ret
1933end
1934d = get og 2
1935da = gga1 14
1936dq = gga1 15
1937dk = add dq ulong get ggs 6
1938du = add dk ulong get ggs 6
1939w = ggw ovwq l
1940ggxwt dq w da d d false
1941w = ggw ovwk l
1942ggxwt dk w da d d true
1943w = ggw ovwv l
1944ggxwt du w da d d true
1945a = ggl l 1
1946w = ggg ovwq l
1947s0 = get ggs 0
1948s6 = get ggs 6
1949s7 = get ggs 7
1950k = ggks d d 3
1951if eq k 1
1952gkopa 0 a 4 s0 0 0
1953gkopa 1 dq 4 s0 0 s6
1954gkopc w s0 4 0 s7 f32 1.0f64
1955ggmm d d ggr 3 3 true
1956else
1957sl = uint div ggr k
1958gkopa 0 a 4 s0 0 mul sl s0
1959gkopa 1 dq 4 s0 s6 mul sl s0
1960ggsk d d 3 k w s7
1961end
1962x = ggl l 0
1963w = ggw ovg1 l
1964s = ggl l 8
1965ggrmsb x w s
1966ggain ovg1 l
1967end
1968
1969fn ggbl l ulong to void
1970ggbffn l
1971ggbatt l
1972ggbqkv l
1973end
1974
1975fn ggks_ex m ulong n ulong nz ulong to ulong
1976if gt ggks m n nz 1
1977ret 1
1978end
1979ret 0
1980end
1981
1982fn ggbwd_head to void
1983d = get og 2
1984v = get og 0
1985r = ggr
1986owdh = ggow_ok
1987if not owdh
1988ggew 20 ggp5 gga1 1 gga1 1
1989ggew 20 mul r d gga1 13 gga1 13
1990end
1991if ggbf_ok
1992ggxwh_bf gga1 39 ggwh 0 gga1 14 d v false
1993if eq ggfzn 0
1994ggwgh_bf gga1 39 gga1 10 ggg 0 v d
1995end
1996owf = ulong 0
1997if owdh
1998owf = 1
1999end
2000ggrmsb_h_ex gga1 8 ggw get og 33 gga1 9 gga1 19 owf
2001ggain_h get og 33
2002ret
2003end
2004ggxw gga1 11 ggw 0 gga1 14 d v false
2005if eq ggfzn 0
2006ggwg gga1 11 gga1 10 ggg 0 v d
2007end
2008ggrmsb gga1 8 ggw get og 33 gga1 9
2009ggain get og 33
2010end
2011
2012fn ggbwd_emb to void
2013if ne ggfzn 0
2014ret
2015end
2016d = get og 2
2017v = get og 0
2018tt = get og 1
2019td = mul tt d
2020td4 = div td 4
2021dh = gga1 13
2022if ggbf_ok
2023ggb2f mul ggr d gga1 13 gga1 16
2024dh = gga1 16
2025end
2026ggu 0 td4
2027ggu 1 get og 6
2028ggpt 1 dh
2029ggu 4 mul td 4
2030ggu 5 0
2031ggpt 3 ggg get og 30
2032ggu 8 0
2033cid = uint 14
2034if ggow_ok
2035cid = 13
2036end
2037ggrun cid 9 ggcd td4 256 1 1
2038ggu 0 d
2039ggpt 1 dh
2040ggpt 2 gga1 4
2041ggpt 3 gga1 5
2042ggpt 4 ggg 0
2043ggrun 22 10 ggcd d 64 v 1
2044end
2045
2046fn ggbwd_hcnt to ulong
2047c = add 4 ggain_cnt
2048if ggow_ok
2049c = sub c 2
2050end
2051if eq ggfzn 0
2052c = add c add 1 ggks_ex get og 0 get og 2 1
2053end
2054note LM-head backward stays in BF16 for NVFP4 convergence parity
2055ret c
2056end
2057
2058fn ggbwd_lcnt l ulong to ulong
2059d = get og 2
2060f = get og 4
2061c = ulong 20
2062if ggfa_ok
2063c = sub c 2
2064end
2065if ggbf_ok
2066c = sub c 2
2067end
2068c = add c ggks_ex d f 1
2069c = add c ggks_ex d d 1
2070c = add c ggks_ex d d 3
2071if or eq OMUTLG 1 eq olp l l
2072c = add c add 1 ggks_ex f d 1
2073end
2074if ggbf_ok
2075if ggdef_gain
2076c = sub c 4
2077else
2078c = sub c mul 2 sub 2 ggain_cnt
2079end
2080end
2081if ggnv4_ok
2082r = ggr
2083cw = and ne get ggw4_ok 0 0 and ggnv4_shape r f d ggnv4_shape r d f
2084if ggnv4_shape r f d
2085if cw
2086c = add c 1
2087else
2088c = add c 2
2089end
2090end
2091if and or eq OMUTLG 1 eq olp l l ggdw_nv4_ok f d
2092c = add c 2
2093if gt ggks_nv4 f d 1
2094c = add c 1
2095end
2096c = sub c ggks_ex f d 1
2097end
2098if ggdw_nv4_ok d f
2099c = add c 2
2100if gt ggks_nv4 d f 1
2101c = add c 1
2102end
2103c = sub c ggks_ex d f 1
2104end
2105if ggnv4_shape r d f
2106if not cw
2107c = add c 2
2108end
2109end
2110if and ge d 512 and ne get ggw4_ok 0 0 ggnv4_shape r d d
2111c = add c 1
2112end
2113if ggdw_nv4_ok d d
2114c = add c 2
2115if gt ggks_nv4 d d 1
2116c = add c 1
2117end
2118c = sub c ggks_ex d d 1
2119q4 = ulong 6
2120if gt ggks_nv4 d d 1
2121q4 = 9
2122end
2123c = sub add c add 1 q4 add 1 ggks_ex d d 3
2124end
2125end
2126ret c
2127end
2128
2129fn ggbwd_ecnt to ulong
2130if eq ggfzn 0
2131if ggbf_ok
2132ret 3
2133end
2134ret 2
2135end
2136ret 0
2137end
2138
2139fn ggbwd_bat to void
2140lv = get og 56
2141fzl = ggfzl
2142if le lv fzl
2143cnt = add ggbwd_hcnt ggbwd_ecnt
2144if not gbbegin cnt
2145ggdie 120
2146end
2147put gkbat 0 1
2148ggbwd_head
2149ggbwd_emb
2150put gkbat 0 0
2151if not gkbsub
2152ggdie 121
2153end
2154ret
2155end
2156hi = lv
2157first = true
2158while gt hi fzl bound 1024
2159lo = fzl
2160if gt sub hi fzl 8
2161lo = sub hi 8
2162end
2163nl = sub hi lo
2164defg = and ggdef_gain gt nl 0
2165cnt = ulong 0
2166if first
2167cnt = add cnt ggbwd_hcnt
2168end
2169l = hi
2170while gt l lo bound 16
2171l = sub l 1
2172cnt = add cnt ggbwd_lcnt l
2173end
2174if defg
2175cnt = add cnt 2
2176end
2177if eq lo fzl
2178cnt = add cnt ggbwd_ecnt
2179end
2180if not gbbegin cnt
2181ggdie 120
2182end
2183put gkbat 0 1
2184if first
2185ggbwd_head
2186end
2187if defg
2188put ggdgb 0 lo
2189put ggdgb 1 nl
2190end
2191l2 = hi
2192while gt l2 lo bound 16
2193l2 = sub l2 1
2194ggbl l2
2195end
2196if defg
2197ggain_def lo nl
2198put ggdgb 1 0
2199end
2200if eq lo fzl
2201ggbwd_emb
2202end
2203put gkbat 0 0
2204if not gkbsub
2205ggdie 121
2206end
2207first = false
2208hi = lo
2209end
2210end
2211
2212fn ggbwd to void
2213if ggb_ok
2214ggbwd_bat
2215ret
2216end
2217ggbwd_head
2218lv = get og 56
2219fzl = ggfzl
2220defg = and ggdef_gain gt lv fzl
2221if defg
2222put ggdgb 0 fzl
2223put ggdgb 1 sub lv fzl
2224end
2225l = lv
2226while gt l fzl bound 9223372036854775807
2227l = sub l 1
2228ggbl l
2229end
2230if defg
2231ggain_def fzl sub lv fzl
2232put ggdgb 1 0
2233end
2234ggbwd_emb
2235end
2236
2237note Mutant f: 1 in the product, 0 in the mutant, which uploads layer 0's W1 times 1 + 2^-10 (a
2238note forward error on the chip that gcmp2's FP check must name).
2239const OMUTF ulong 1
2240
2241note W[i] as f32 for i in chunk q of P (a loop runs at most 999999 passes)
2242fn ggupc q ulong p ulong to void
2243h = get gbmem 5
2244wo = get gga 0
2245i0 = mul q OCH
2246for j ulong sub oce q p i0
2247i = add i0 j
2248x = bitcast uint f32 ggupv i
2249m = mapset h add wo mul i 4 4 ulong x
2250end
2251end
2252
2253note the uploaded value of W[i]: the parameter at virtual index i in the product
2254fn ggupv i ulong to f64
2255w = owg i
2256if eq OMUTF 0
2257a = opw1 0
2258if and ge i a lt i add a mul get og 2 get og 4
2259w = mul w 1.0009765625f64
2260end
2261end
2262ret w
2263end
2264
2265note upload the f32 parameters
2266fn ggupw to void
2267p = get og 8
2268for q ulong ocn p
2269ggupc q p
2270end
2271put gkinv 0 1
2272end
2273
2274note upload the batch tokens and the CSR of x (rows sorted by token)
2275fn ggupb to void
2276ggupt
2277ggcsr
2278put gkinv 0 1
2279end
2280
2281note n values of the Adam pool from s as f32 to arena byte offset o + 4 j, and back (one chunk of an
2282note Adam moment)
2283fn ggupmc s ulong o ulong n ulong to void
2284h = get gbmem 5
2285for j ulong n
2286x = bitcast uint f32 omvg add s j
2287m = mapset h add o mul j 4 4 ulong x
2288end
2289end
2290
2291fn ggdnmc s ulong o ulong n ulong to void
2292h = get gbmem 5
2293for j ulong n
2294x = uint mapget h add o mul j 4 4
2295omvp add s j f64 bitcast f32 x
2296end
2297end
2298
2299note direct OLLMCK05 mmap -> GPU arena load and GPU arena -> OLLMCK05 atomic save
2300fn ggck5_rd off ulong nb ulong to void
2301if ne get ock5_has 0 0
2302ommap_read get ock5_mh 0 off nb
2303ret
2304end
2305if ne oprd get ock5_ai 0 off 0 nb nb
2306exit 70
2307end
2308end
2309
2310fn ggck5_copy_to_gpu src_off ulong dst_off ulong nb ulong to void
2311h = get gbmem 5
2312done = ulong 0
2313while lt done nb bound 999999
2314chunk = sub nb done
2315if gt chunk OPWCAP
2316chunk = OPWCAP
2317end
2318ggck5_rd add src_off done chunk
2319w = mapwrite h add dst_off done opw 0 chunk
2320if ne w long chunk
2321ggdie 85
2322end
2323done = add done chunk
2324end
2325end
2326
2327fn ggck5_bf16_expand cnt ulong to void
2328j = cnt
2329while gt j 0 bound 200000
2330j = sub j 1
2331w16 = ock5_opw2 mul j 2
2332opwput4 mul j 4 shl w16 16
2333end
2334end
2335
2336fn ggck5_bf16_to_gpu src_off ulong dst_off ulong p ulong to void
2337h = get gbmem 5
2338i0 = ulong 0
2339while lt i0 p bound 999999
2340cnt = sub p i0
2341if gt cnt OCK5C
2342cnt = OCK5C
2343end
2344ggck5_rd add src_off mul i0 2 mul cnt 2
2345ggck5_bf16_expand cnt
2346nb4 = mul cnt 4
2347w = mapwrite h add dst_off mul i0 4 opw 0 nb4
2348if ne w long nb4
2349ggdie 85
2350end
2351i0 = add i0 cnt
2352end
2353end
2354
2355fn ggload_ck5_bf fl ulong p ulong to void
2356h = get gbmem 5
2357ggew 20 ggp5 gga1 1 gga1 1
2358has_bf = ne and fl OCK5F_BF16 0
2359if and has_bf ne get ock5_st 7 0
2360ggck5_copy_to_gpu get ock5_st 6 get gga 38 mul p 2
2361z = mul p 2
2362ze = mul ggp5 2
2363if lt z ze
2364if eq rem z 4 2
2365w0 = uint mapget h add get gga 38 sub z 2 4
2366m0 = mapset h add get gga 38 sub z 2 4 ulong and w0 65535
2367z = add z 2
2368end
2369while lt z ze bound 512
2370m1 = mapset h add get gga 38 z 4 0
2371z = add z 4
2372end
2373end
2374put gkinv 0 1
2375else
2376ggf2b ggp5 gga1 0 gga1 38
2377end
2378if ggnv4_ok
2379exp_wnv = sub get gga 47 get gga 48
2380has_nv = ne and fl OCK5F_NV4 0
2381ok_nv = and has_nv eq get ock5_st 9 exp_wnv
2382if ok_nv
2383d = get og 2
2384if and ggnv4_shape d d d ggnv4_shape get og 4 d d
2385ggck5_copy_to_gpu get ock5_st 8 get gga 48 exp_wnv
2386put gkinv 0 1
2387put ggw4_ok 0 1
2388ret
2389end
2390end
2391ggquant_w
2392end
2393end
2394
2395fn ggload_ck5 load_mv bool to void
2396p = get og 8
2397fl = get ock5_st 0
2398if ne and fl OCK5F_F32 0
2399ggck5_copy_to_gpu get ock5_st 2 get gga 0 mul p 4
2400else
2401ggck5_bf16_to_gpu get ock5_st 2 get gga 0 p
2402end
2403put gkinv 0 1
2404if load_mv
2405if ne and fl OCK5F_ZMV 0
2406ggew 20 p gga1 21 gga1 21
2407ggew 20 p gga1 22 gga1 22
2408else
2409mv_off = get ock5_st 4
2410ggck5_copy_to_gpu mv_off get gga 21 mul p 4
2411ggck5_copy_to_gpu add mv_off mul p 4 get gga 22 mul p 4
2412put gkinv 0 1
2413end
2414end
2415if ggbf_ok
2416ggload_ck5_bf fl p
2417end
2418put gkinv 0 1
2419ock5_drop_mh
2420end
2421
2422fn ggsave_ck5_hash gb long off ulong chunk ulong p2 ulong h ulong to ulong
2423nw_m = div chunk 8
2424curh = ommap_fnv64 gb off nw_m h
2425tail_b = sub add chunk p2 mul nw_m 8
2426if gt tail_b 0
2427ret ocfnvw64_off curh mul nw_m 8 div tail_b 8
2428end
2429ret curh
2430end
2431
2432fn ggsave_ck5_sec src_off ulong slen ulong pad ulong h ulong pos ulong to ulong
2433gb = get gbmem 5
2434done = ulong 0
2435curh = h
2436curp = pos
2437while lt done slen bound 999999
2438chunk = sub slen done
2439if gt chunk 1048576
2440chunk = 1048576
2441end
2442r = mapread gb add src_off done opw 0 chunk
2443if ne r long chunk
2444exit 70
2445end
2446p2 = ulong 0
2447if eq add done chunk slen
2448p2 = pad
2449ock5_pad0 chunk p2
2450end
2451curh = ggsave_ck5_hash gb add src_off done chunk p2 curh
2452tb = add chunk p2
2453ocwput_opw curp tb
2454curp = add curp tb
2455done = add done chunk
2456end
2457ret curh
2458end
2459
2460fn ggck5_plan to void
2461p = get og 8
2462fl = OCK5F_F32
2463if ggbf_ok
2464fl = or fl OCK5F_BF16
2465end
2466if ne get ggw4_ok 0 0
2467fl = or fl OCK5F_NV4
2468end
2469if gt get og 59 1
2470fl = or fl OCK5F_LOOP
2471end
2472orig = get ock5_st 1
2473if or eq orig 0 gt orig 3
2474orig = 2
2475if gt get og 59 1
2476orig = 3
2477end
2478end
2479w_off = OCK5HDR
2480w_len = mul p 4
2481mv_off = ock5_align64 add w_off w_len
2482mv_len = mul p 8
2483cur = ock5_align64 add mv_off mv_len
2484wbf_off = ulong 0
2485wbf_len = ulong 0
2486if ne and fl OCK5F_BF16 0
2487wbf_off = cur
2488wbf_len = mul p 2
2489cur = ock5_align64 add wbf_off wbf_len
2490end
2491wnv_off = ulong 0
2492wnv_len = ulong 0
2493if ne and fl OCK5F_NV4 0
2494wnv_off = cur
2495wnv_len = sub get gga 47 get gga 48
2496cur = ock5_align64 add wnv_off wnv_len
2497end
2498put ock5_st 0 fl
2499put ock5_st 1 orig
2500put ock5_st 2 w_off
2501put ock5_st 3 w_len
2502put ock5_st 4 mv_off
2503put ock5_st 5 mv_len
2504put ock5_st 6 wbf_off
2505put ock5_st 7 wbf_len
2506put ock5_st 8 wnv_off
2507put ock5_st 9 wnv_len
2508put ock5_st 10 cur
2509end
2510
2511fn ggsave_ck5_st gga_i ulong off_i ulong len_i ulong nxt ulong h ulong to ulong
2512so = get ock5_st off_i
2513sl = get ock5_st len_i
2514pad = sub nxt add so sl
2515ret ggsave_ck5_sec get gga gga_i sl pad h so
2516end
2517
2518fn ggsave_ck5 ai ulong to void
2519ggck5_plan
2520ocwopen ai
2521h = ulong 14695981039346656037
2522h = ggsave_ck5_st 0 2 3 get ock5_st 4 h
2523m_len = mul get og 8 4
2524mv_off = get ock5_st 4
2525h = ggsave_ck5_sec get gga 21 m_len 0 h mv_off
2526nxt = get ock5_st 10
2527if ne get ock5_st 6 0
2528nxt = get ock5_st 6
2529else
2530if ne get ock5_st 8 0
2531nxt = get ock5_st 8
2532end
2533end
2534pad_v = sub nxt add mv_off get ock5_st 5
2535h = ggsave_ck5_sec get gga 22 m_len pad_v h add mv_off m_len
2536if ne get ock5_st 6 0
2537nxt2 = get ock5_st 10
2538if ne get ock5_st 8 0
2539nxt2 = get ock5_st 8
2540end
2541h = ggsave_ck5_st 38 6 7 nxt2 h
2542end
2543if ne get ock5_st 8 0
2544h = ggsave_ck5_st 48 8 9 get ock5_st 10 h
2545end
2546put ock5_st 11 h
2547ock5_wr_hdr
2548ocwend ai
2549end
2550
2551note optimizer state (parameters, Adam pool) to the chip as f32 (m in gga 21, v in gga 22), and back
2552fn ggupst to void
2553ggupw
2554p = get og 8
2555for q ulong ocn p
2556i0 = mul q OCH
2557n = sub oce q p i0
2558ggupmc add get og 11 i0 add get gga 21 mul i0 4 n
2559ggupmc add get og 12 i0 add get gga 22 mul i0 4 n
2560end
2561put gkinv 0 1
2562end
2563
2564note only [fz, P) comes back (ggfz): the frozen f64 weights and moments stay as loaded
2565fn ggdnst to void
2566p = get og 8
2567fz = ggfzn
2568for q ulong ocn p
2569ggdnq q p fz
2570end
2571end
2572
2573note chunk q of P from its first index at or above fz: parameters, then both moments
2574fn ggdnq q ulong p ulong fz ulong to void
2575a = mul q OCH
2576e = oce q p
2577if le e fz
2578ret
2579end
2580if lt a fz
2581a = fz
2582end
2583n = sub e a
2584ggdnwr a n
2585ggdnmc add get og 11 a add get gga 21 mul a 4 n
2586ggdnmc add get og 12 a add get gga 22 mul a 4 n
2587end
2588
2589note chip parameters [a, a + n) back into the parameter banks as f64 (n at most OCH)
2590fn ggdnwr a ulong n ulong to void
2591h = get gbmem 5
2592o = get gga 0
2593for j ulong n
2594i = add a j
2595x = uint mapget h add o mul i 4 4
2596owp i f64 bitcast f32 x
2597end
2598end
2599
2600global ggub uchar 262144
2601fn ggub_u32 i ulong v ulong to void
2602o = mul i 4
2603put ggub o uchar and v 255
2604put ggub add o 1 uchar and shr v 8 255
2605put ggub add o 2 uchar and shr v 16 255
2606put ggub add o 3 uchar and shr v 24 255
2607end
2608
2609fn ggupt to void
2610h = get gbmem 5
2611xo = get gga 2
2612yo = get gga 3
2613r = ggr
2614if le r 65536
2615for i ulong r
2616ggub_u32 i get obx i
2617end
2618m = mapwrite h xo ggub 0 mul r 4
2619for i ulong r
2620ggub_u32 i get oby i
2621end
2622m = mapwrite h yo ggub 0 mul r 4
2623ret
2624end
2625for i ulong r
2626tk = get obx i
2627m = mapset h add xo mul i 4 4 tk
2628ty = get oby i
2629m = mapset h add yo mul i 4 4 ty
2630end
2631end
2632
2633note counting sort of the R rows by input token: start (V + 1) and order (R) arrays
2634fn ggcsr to void
2635h = get gbmem 5
2636v = get og 0
2637r = ggr
2638for c ulong add v 1
2639put ggcn c 0
2640end
2641for i ulong r
2642c1 = add get obx i 1
2643n = add get ggcn c1 1
2644put ggcn c1 n
2645end
2646for c ulong v
2647c1 = add c 1
2648n = add get ggcn c1 get ggcn c
2649put ggcn c1 n
2650end
2651so = get gga 4
2652v1 = add v 1
2653if le v1 65536
2654for c ulong v1
2655ggub_u32 c get ggcn c
2656end
2657m = mapwrite h so ggub 0 mul v1 4
2658else
2659for c ulong v1
2660n = get ggcn c
2661m = mapset h add so mul c 4 4 n
2662end
2663end
2664so = get gga 5
2665if le r 65536
2666for i ulong r
2667c = get obx i
2668p = get ggcn c
2669ggub_u32 p i
2670put ggcn c add p 1
2671end
2672m = mapwrite h so ggub 0 mul r 4
2673else
2674for i ulong r
2675c = get obx i
2676p = get ggcn c
2677m = mapset h add so mul p 4 4 i
2678put ggcn c add p 1
2679end
2680end
2681end
2682
2683note GRD[i] = G[i] for i in chunk q of P
2684fn ggdnc q ulong p ulong to void
2685h = get gbmem 5
2686go = get gga 1
2687i0 = mul q OCH
2688for j ulong sub oce q p i0
2689i = add i0 j
2690x = uint mapget h add go mul i 4 4
2691ocsp i f64 bitcast f32 x
2692end
2693end
2694
2695fn ggdown to void
2696p = get og 8
2697for q ulong ocn p
2698ggdnc q p
2699end
2700end
2701
2702note one f32 at arena byte offset o, as f64
2703fn ggf o ulong to f64
2704x = uint mapget get gbmem 5 o 4
2705ret f64 bitcast f32 x
2706end
2707
2708note row loss from the CE stats (m, sum, l_y) of row r: log(sum) + m - l_y
2709fn ggrl r ulong to f64
2710o = add get gga 12 mul r 12
2711m = ggf o
2712sm = ggf add o 4
2713ly = ggf add o 8
2714ret sub add flog sm m ly
2715end
2716
2717fn ggloss to f64
2718tt = get og 1
2719for b ulong get og 6
2720s = 0.0f64
2721for t ulong tt
2722s = add s ggrl add mul b tt t
2723end
2724put olos b div s f64 tt
2725end
2726ret olsum
2727end
2728
2729note the step loss and gradient for the batch in obx/oby; ggopen first. full: upload the f32
2730note parameters first and download the gradient into ogr[0..P) after; otherwise both stay on chip.
2731note phase wall times (ns): 0 upload, 1 forward, 2 backward, 3 download, 4 loss, 5 on-chip Adam
2732global ggtm long 6
2733note Skip CPU ggloss on non-save/non-log training steps (saves ~1.04 ms/step of f64 flog on R=16384!)
2734global ggnoloss uint 1
2735global ggsqn_req uint 1
2736global ggsqn_done uint 1
2737fn ggsqn_run to void
2738n4 = div ggp5 4
2739ggu 0 n4
2740ggpt 1 gga1 1
2741ggpt 2 gga1 23
2742ggrun 23 6 256 1 1
2743end
2744fn ggstep_bat_cnt to ulong
2745lv = get og 56
2746fzl = ggfzl
2747cnt = add add ggfwd_ecnt mul lv ggfwd_lcnt ggfwd_hcnt
2748cnt = add cnt ggbwd_hcnt
2749l = fzl
2750while lt l lv bound 32
2751cnt = add cnt ggbwd_lcnt l
2752l = add l 1
2753end
2754if and ggdef_gain gt lv fzl
2755cnt = add cnt 2
2756end
2757cnt = add cnt ggbwd_ecnt
2758ret add cnt 1
2759end
2760fn ggstep_bat_ok to bool
2761if or not ggb_ok not ggbf_ok
2762ret false
2763end
2764if or ne get ggnoce 0 0 gt get og 56 16
2765ret false
2766end
2767if gt sub get og 56 ggfzl 8
2768ret false
2769end
2770ret le ggstep_bat_cnt 512
2771end
2772fn ggpass_bat to void
2773lv = get og 56
2774fzl = ggfzl
2775if not gbbegin ggstep_bat_cnt
2776ggdie 120
2777end
2778put gkbat 0 1
2779ggfwd_emb
2780for l ulong lv bound 32
2781ggfl l
2782end
2783ggfwd_head
2784ggbwd_head
2785defg = and ggdef_gain gt lv fzl
2786if defg
2787put ggdgb 0 fzl
2788put ggdgb 1 sub lv fzl
2789end
2790l2 = lv
2791while gt l2 fzl bound 32
2792l2 = sub l2 1
2793ggbl l2
2794end
2795if defg
2796ggain_def fzl sub lv fzl
2797put ggdgb 1 0
2798end
2799ggbwd_emb
2800ggsqn_run
2801put gkbat 0 0
2802end
2803fn ggpass full bool to f64
2804t = monons
2805if full
2806ggupw
2807end
2808ggupb
2809u = monons
2810put ggtm 0 sub u t
2811if and not full and eq get ggsqn_req 0 1 ggstep_bat_ok
2812ggpass_bat
2813t_mid = monons
2814put ggtm 1 sub t_mid u
2815if not gkbsub
2816ggdie 121
2817end
2818u = monons
2819put ggtm 2 sub u t_mid
2820put ggsqn_done 0 1
2821else
2822ggfwd
2823t = monons
2824put ggtm 1 sub t u
2825ggbwd
2826u = monons
2827put ggtm 2 sub u t
2828end
2829if full
2830ggdown
2831end
2832t = monons
2833put ggtm 3 sub t u
2834l = 0.0f64
2835if eq get ggnoloss 0 0
2836l = ggloss
2837end
2838u = monons
2839put ggtm 4 sub u t
2840ret l
2841end
2842
2843fn gggrad to f64
2844ret ggpass true
2845end
2846
2847note clip and Adam on chip at step og[41], the f32 image of oadam: SQN partial sums of g^2, the
2848note norm, clip factor, schedule and bias corrections on the host in f64, then ADAM over P
2849fn ggadam to void
2850p = get og 8
2851if eq get ggsqn_done 0 0
2852ggsqn_run
2853else
2854put ggsqn_done 0 0
2855end
2856h = get gbmem 5
2857so = get gga 23
2858ss = 0.0f64
2859for c ulong 2048
2860x = uint mapget h add so mul c 4 4
2861ss = add ss f64 bitcast f32 x
2862end
2863nr = fsqrt ss
2864cl = 1.0f64
2865if gt nr 1.0
2866cl = div 1.0 nr
2867end
2868s = get og 41
2869lrs = olrs s bitcast f64 get og 43
2870bc1 = sub 1.0 opowb 0.9 s
2871bc2 = sub 1.0 opowb 0.99 s
2872if eq OMUTC 0
2873bc1 = 1.0f64
2874bc2 = 1.0f64
2875end
2876ggad24 p cl div lrs bc1 div 1.0f64 bc2
2877end
2878
2879note ADAM over [fz, P) (ggfz; all of P without a freeze): the frozen weights and moments are not
2880note touched on the chip. Clip factor cl, lr / bc1 and 1 / bc2 from ggadam.
2881fn ggad24 p ulong cl f64 lb f64 ib f64 to void
2882fz = ggfzn
2883n = sub p fz
2884n4 = div n 4
2885o = mul fz 4
2886ggu 0 n4
2887ggpt 1 add gga1 0 o
2888ggpt 2 add gga1 21 o
2889ggpt 3 add gga1 22 o
2890ggpt 4 add gga1 1 o
2891ggu 10 ulong grf cl
2892ggu 11 ulong grf lb
2893ggu 12 ulong grf ib
2894if ggbf_ok
2895ggpt 7 add gga1 38 mul fz 2
2896ggrun 81 16 ggcd n4 256 1 1
2897ggquant_w
2898ret
2899end
2900ggrun 24 13 ggcd n4 256 1 1
2901end
2902
2903fn ggtms to void
2904oputc 71
2905oputc 84
2906for i ulong 6
2907oputc 32
2908oputl get ggtm i
2909end
2910for i ulong 4
2911oputc 32
2912oputl get gkt i
2913end
2914oputc 10
2915end
2916
2917note keep the CPU gradient: BLK[i] = GRD[i] (chunked: a loop runs at most 999999 passes)
2918fn ggkeep p ulong to void
2919for q ulong ocn p
2920i0 = mul q OCH
2921for j ulong sub oce q p i0
2922i = add i0 j
2923ogrp add p i ocsg i
2924end
2925end
2926end
2927
2928note ggcm[0] = max |GRD[i] - BLK[i]|, ggcm[1] = max |BLK[i]| over i < P
2929global ggcm f64 2
2930fn ggmaxd p ulong to void
2931put ggcm 0 0.0f64
2932put ggcm 1 0.0f64
2933for q ulong ocn p
2934i0 = mul q OCH
2935for j ulong sub oce q p i0
2936i = add i0 j
2937rf = ogrg add p i
2938dd = sub ocsg i rf
2939if lt dd 0.0f64
2940dd = neg dd
2941end
2942if lt rf 0.0f64
2943rf = neg rf
2944end
2945if gt dd get ggcm 0
2946put ggcm 0 dd
2947end
2948if gt rf get ggcm 1
2949put ggcm 1 rf
2950end
2951end
2952end
2953end
2954
2955note the chip's frozen count and layer (ggfz; 0 in mutant fz)
2956fn ggfzn to ulong
2957if eq OMUTFZ 0
2958ret 0
2959end
2960ret get ggfz 0
2961end
2962
2963fn ggfzl to ulong
2964if eq OMUTFZ 0
2965ret 0
2966end
2967ret get ggfz 1
2968end
2969
2970note FREEZE_LAYERS f for the loaded checkpoint, checked as cmdtrainf: f > L is exit 2, f > 0 on a
2971note looped checkpoint exit 17 (ockload's refusal); fz = og 31 + f og 32
2972fn ggfzset f ulong to void
2973if gt f get og 5
2974exit 2
2975end
2976if and gt f 0 gt get og 59 1
2977exit 17
2978end
2979put ggfz 0 0
2980put ggfz 1 f
2981if gt f 0
2982put ggfz 0 add get og 31 mul f get og 32
2983end
2984end
2985
2986note `GFZ F fz`
2987fn ggfzpr to void
2988oputc 71
2989oputc 70
2990oputc 90
2991oputc 32
2992oputu get ggfz 1
2993oputc 32
2994oputu get ggfz 0
2995oputc 10
2996oflush
2997end
2998
2999note gcmp and gacmp arguments CK DATA S [FREEZE_LAYERS]: load CK, open DATA, batch S; FREEZE_LAYERS
3000note sets ggfz for the chip and ofreeze / ofzlay (cmdtrainf's freeze) for the CPU reference
3001fn ggcarg to void
3002na = oargc
3003if and ne na 5 ne na 6
3004exit 2
3005end
3006ockloop 2
3007odopen 3
3008obatch oargu 4
3009if eq na 6
3010ggfzset oargu 5
3011ggfzpr
3012put ofreeze 0 get ggfz 0
3013put ofzlay 0 get ggfz 1
3014end
3015end
3016
3017note GRD[i] = 0 for i < fz: oadam's mask on the CPU reference gradient (the tied head still writes
3018note the frozen wte's gradient on the CPU)
3019fn ggfzz to void
3020fz = get ggfz 0
3021i = ulong 0
3022while lt i fz bound 9223372036854775807
3023ocsp i 0.0f64
3024i = add i 1
3025end
3026end
3027
3028note after gggrad: true without a freeze; else `GCF F fz nonzero` over the chip's GRD[0, fz), which must
3029note all be 0 (no frozen gradient launched)
3030fn ggfzok to bool
3031fz = get ggfz 0
3032if eq fz 0
3033ret true
3034end
3035n = ulong 0
3036i = ulong 0
3037while lt i fz bound 9223372036854775807
3038if ne ocsg i 0.0f64
3039n = add n 1
3040end
3041i = add i 1
3042end
3043oputc 71
3044oputc 67
3045oputc 70
3046ggfzln n
3047ret eq n 0
3048end
3049
3050note ` F fz n` and a newline
3051fn ggfzln n ulong to void
3052oputc 32
3053oputu get ggfz 1
3054oputc 32
3055oputu get ggfz 0
3056oputc 32
3057oputu n
3058oputc 10
3059end
3060
3061note after gacmp's step: true without a freeze; else `GAF F fz changed`, the frozen parameters whose CPU
3062note value is not BLK (the loaded f64) or whose chip value (GRD, ggdw) is not f32 of it
3063fn ggfzad p ulong to bool
3064fz = get ggfz 0
3065if eq fz 0
3066ret true
3067end
3068n = ulong 0
3069i = ulong 0
3070while lt i fz bound 9223372036854775807
3071n = add n ggfzd1 i p
3072i = add i 1
3073end
3074oputc 71
3075oputc 65
3076oputc 70
3077ggfzln n
3078ret eq n 0
3079end
3080
3081fn ggfzd1 i ulong p ulong to ulong
3082w0 = ogrg add p i
3083if ne bitcast ulong owg i bitcast ulong w0
3084ret 1
3085end
3086if ne bitcast ulong ocsg i bitcast ulong f64 f32 w0
3087ret 1
3088end
3089ret 0
3090end
3091
3092note gcmp CK DATA S [FREEZE_LAYERS]: CPU ograd and GPU gradient for batch S; prints
3093note `GC P maxabs_e12 maxref_e12 rel_e9 cpu_loss_micro gpu_loss_micro gpu_ns tol_e9` and GCMP PASS
3094note (rel < tol, |loss difference| < 1e-5) or GCMP FAIL (exit 45). tol = R 2^-24 is the first-order
3095note bound gamma_R on the f32 error of a sum of R = B T rows, the longest reduction of the weight
3096note gradients, relative to the sum of magnitudes; the CPU reference is f64. ReLU units whose f64
3097note pre-activation lies within f32 resolution of zero can take the other side on the GPU (receipt:
3098note evidence/OMEGALLM/gpu/README.md), and both sides are valid subgradients. With FREEZE_LAYERS F
3099note (ggcarg) both sides freeze as trainf: the CPU reference is ograd with ofzlay F and GRD[0, fz)
3100note zeroed (oadam's mask), the chip's G[0, fz) must be exactly 0 (`GCF F fz nonzero`, ggfzok).
3101fn ggcmp to void
3102ggcarg
3103put og 10 0
3104lc = ograd
3105p = get og 8
3106ggfzz
3107ggkeep p
3108ggopen
3109t0 = monons
3110lg = gggrad
3111t1 = monons
3112ggmaxd p
3113mx = get ggcm 0
3114mr = get ggcm 1
3115rel = div mx mr
3116dl = sub lg lc
3117if lt dl 0.0f64
3118dl = neg dl
3119end
3120oputc 71
3121oputc 67
3122oputc 32
3123oputu p
3124oputc 32
3125oputl rmic mul mx 1000000.0f64
3126oputc 32
3127oputl rmic mul mr 1000000.0f64
3128oputc 32
3129oputl rmic mul rel 1000.0f64
3130oputc 32
3131oputl rmic lc
3132oputc 32
3133oputl rmic lg
3134oputc 32
3135oputl sub t1 t0
3136oputc 32
3137tol = div f64 ggr 16777216.0f64
3138oputl rmic mul tol 1000.0f64
3139oputc 10
3140ggtms
3141ok = and and lt rel tol lt dl 0.00001f64 ggfzok
3142r = nvstop
3143oputc 71
3144oputc 67
3145oputc 77
3146oputc 80
3147oputc 32
3148if ok
3149oputc 80
3150oputc 65
3151oputc 83
3152oputc 83
3153oputc 10
3154oflush
3155ret
3156end
3157oputc 70
3158oputc 65
3159oputc 73
3160oputc 76
3161oputc 10
3162oflush
3163exit 45
3164end
3165
3166note true when x is finite (every comparison with NaN is false)
3167fn ggfin x f64 to bool
3168a = x
3169if lt a 0.0f64
3170a = neg a
3171end
3172ret le a bitcast f64 ulong 9218868437227405311
3173end
3174
3175note x for printing: 1e6 when x is not finite or above 1e6 (rmic traps past the long range)
3176fn ggclp x f64 to f64
3177if ggfin x
3178if lt x 1000000.0f64
3179ret x
3180end
3181end
3182ret 1000000.0f64
3183end
3184
3185note gcmp2 state. ggfs[j] = s_j for the row in flight (F <= 8192). ggk2: [0] ReLU
3186note flips (sign of FP_chip differs from FP_ref), [1] non-finite FP values (chip or reference),
3187note [2] layer of the largest rho, [3] non-finite gradient values (chip, K0 or K1).
3188note ggk3: [0] rho max, [1] max |FP_ref| at a flip, [2] max |FP_chip - FP_ref|.
3189global ggfs f64 8192
3190global ggk2 ulong 4
3191global ggk3 f64 3
3192fn ggk2z to void
3193for k ulong 4
3194put ggk2 k 0
3195end
3196for k ulong 3
3197put ggk3 k 0.0f64
3198end
3199end
3200
3201note s_j = sum_k |bn_k| |W1[k F + j]| for row t of layer l (k < D ascending), the magnitude sum of
3202note the FP dot product the chip rounds (layer l is staged: ogmask runs inside olayb)
3203fn ggsj l ulong t ulong to void
3204d = get og 2
3205f = get og 4
3206bn = add oabn l mul t d
3207wv = ovw1 l
3208inb = owinb wv
3209w = ulong 0
3210if inb
3211w = sub wv get og 34
3212else
3213w = opx wv
3214end
3215for j ulong f
3216put ggfs j 0.0f64
3217end
3218for k ulong d
3219a = get oa add bn k
3220if lt a 0.0f64
3221a = neg a
3222end
3223if inb
3224ggsjk_b a add w mul k f f
3225else
3226ggsjk a add w mul k f f
3227end
3228end
3229end
3230
3231fn ggsjk a f64 wo ulong f ulong to void
3232for j ulong f
3233x = get ow add wo j
3234if lt x 0.0f64
3235x = neg x
3236end
3237put ggfs j add get ggfs j mul a x
3238end
3239end
3240
3241fn ggsjk_b a f64 wo ulong f ulong to void
3242for j ulong f
3243x = get oww add wo j
3244if lt x 0.0f64
3245x = neg x
3246end
3247put ggfs j add get ggfs j mul a x
3248end
3249end
3250
3251note model.om's ReLU mask hook (ogkm[0] = 1, gcmp2 only): for row t of layer l of the sequence in
3252note flight, check every unit's reference FP against the chip's FP (left in the arena by gggrad),
3253note then zero dF where the chip's FP is not > 0, the chip's fused dF mask (gkope 2; NaN included).
3254fn ogmask yo ulong l ulong t ulong to void
3255f = get og 4
3256xo = add oafp l mul t f
3257ggsj l t
3258row = add mul get ogkm 1 get og 1 t
3259c0 = add sub ggl l 10 get gga 40 mul mul row f 4
3260for j ulong f
3261c = ggf add c0 mul j 4
3262ggk1 get oa add xo j c j l
3263if not gt c 0.0f64
3264put oa add yo j 0.0f64
3265end
3266end
3267end
3268
3269note one unit: non-finite count, |FP_chip - FP_ref|, rho = that over s_j (inf when s_j = 0 and the
3270note values differ), and the flip count
3271fn ggk1 v f64 c f64 j ulong l ulong to void
3272if not and ggfin v ggfin c
3273put ggk2 1 add get ggk2 1 1
3274ret
3275end
3276dd = sub c v
3277if lt dd 0.0f64
3278dd = neg dd
3279end
3280if gt dd get ggk3 2
3281put ggk3 2 dd
3282end
3283ggkr dd get ggfs j l
3284ggkfl v c
3285end
3286
3287fn ggkr dd f64 s f64 l ulong to void
3288if not gt dd 0.0f64
3289ret
3290end
3291rho = bitcast f64 ulong 9218868437227405312
3292if gt s 0.0f64
3293rho = div dd s
3294end
3295if gt rho get ggk3 0
3296put ggk3 0 rho
3297put ggk2 2 l
3298end
3299end
3300
3301fn ggkfl v f64 c f64 to void
3302a = gt v 0.0f64
3303b = gt c 0.0f64
3304if eq a b
3305ret
3306end
3307put ggk2 0 add get ggk2 0 1
3308if lt v 0.0f64
3309v = neg v
3310end
3311if gt v get ggk3 1
3312put ggk3 1 v
3313end
3314end
3315
3316note Adam pool [o + i] = GRD[i] for i < P (the chip gradient at o = 0, K0 at o = P; gcmp2 has no
3317note Adam step)
3318fn ggstash p ulong o ulong to void
3319for q ulong ocn p
3320ggstq q p o
3321end
3322end
3323
3324fn ggstq q ulong p ulong o ulong to void
3325i0 = mul q OCH
3326for j ulong sub oce q p i0
3327i = add i0 j
3328omvp add o i ocsg i
3329end
3330end
3331
3332note ggc2: [0] max |G_chip - K0|, [1] max |K0|, [2] max |G_chip - K1| over i < P, with the chip
3333note gradient in the Adam pool [0, P), K0 in [P, 2P) and K1 in GRD; non-finite values count in ggk2[3]
3334global ggc2 f64 3
3335fn gg2cmp p ulong to void
3336for k ulong 3
3337put ggc2 k 0.0f64
3338end
3339for q ulong ocn p
3340i0 = mul q OCH
3341for j ulong sub oce q p i0
3342gg2c1 add i0 j p
3343end
3344end
3345end
3346
3347fn gg2c1 i ulong p ulong to void
3348c = omvg i
3349r0 = omvg add p i
3350r1 = ocsg i
3351if not and and ggfin c ggfin r0 ggfin r1
3352put ggk2 3 add get ggk2 3 1
3353ret
3354end
3355gg2mx 0 sub c r0
3356gg2mx 1 r0
3357gg2mx 2 sub c r1
3358end
3359
3360fn gg2mx k ulong x f64 to void
3361if lt x 0.0f64
3362x = neg x
3363end
3364if gt x get ggc2 k
3365put ggc2 k x
3366end
3367end
3368
3369note gcmp2 CK DATA S: branch-conditioned parity (evidence/OMEGALLM/gpu-max/README.md). The chip
3370note gradient runs first and stays in the Adam pool; K0 is the CPU ograd with its own ReLU mask (gcmp's
3371note reference), K1 the CPU ograd with the chip's FP sign as the ReLU mask (ogmask), which also checks
3372note every FFN pre-activation. Prints `GC2 P rel0_e9 rel1_e9 tolr_e9 rho_e9 tolf_e9 rho_layer flips
3373note vflip_e12 fpdiff_e12 nonfinite cpu_loss_micro gpu_loss_micro gpu_ns fails` and GCMP2 PASS, or
3374note GCMP2 FAIL (exit 47). rel0 = max|G_chip - K0| / max|K0| (gcmp's rel), rel1 = max|G_chip - K1| /
3375note max|K0|; rho = max |FP_chip - FP_ref| / s_j. fails is a bit set: 1 a non-finite FP or gradient
3376note value, 2 rho >= tolf = max(D, F, T) 2^-24, 4 rel1 >= tolr = R 2^-24 (gcmp's gamma_R), 8 |chip
3377note loss - K0 loss| >= 1e-5.
3378fn ggcmp2 to void
3379if ne oargc 5
3380exit 2
3381end
3382ockloop 2
3383odopen 3
3384obatch oargu 4
3385p = get og 8
3386ggopen
3387t0 = monons
3388lg = gggrad
3389t1 = monons
3390ggstash p 0
3391put og 10 0
3392put ogkm 0 0
3393l0 = ograd
3394ggstash p p
3395for k ulong 4
3396put ggk2 k 0
3397end
3398for k ulong 3
3399put ggk3 k 0.0f64
3400end
3401put ogkm 0 1
3402put og 10 0
3403l1 = ograd
3404put ogkm 0 0
3405gg2cmp p
3406ggc2pr lg l0 sub t1 t0
3407end
3408
3409note the GC2 line and verdict of gcmp2 (lg chip loss, l0 K0 loss, ns chip gradient time); gbcmp2
3410note (gkbf 1) takes gb.om's verdict
3411fn ggc2pr lg f64 l0 f64 ns long to void
3412fl = ggc2fl lg l0
3413ggc2ln lg l0 ns fl
3414ggtms
3415r = nvstop
3416if eq get gkbf 0 1
3417gbc2vd lg l0 fl
3418ret
3419end
3420ggc2vd eq fl 0
3421end
3422
3423note ggc2[k] / max |K0|, clamped for printing
3424fn gg2rl k ulong to f64
3425ret ggclp div get ggc2 k get ggc2 1
3426end
3427
3428note tolf = max(D, F, T) 2^-24
3429fn gg2tf to f64
3430n = fmax f64 get og 2 fmax f64 get og 4 f64 get og 1
3431ret div n 16777216.0f64
3432end
3433
3434note tolr = R 2^-24 (gcmp's gamma_R)
3435fn gg2tr to f64
3436ret div f64 ggr 16777216.0f64
3437end
3438
3439note the gcmp2 fail bits (see ggcmp2); gb.om's BF16 bits under gkbf 1
3440fn ggc2fl lg f64 l0 f64 to ulong
3441if eq get gkbf 0 1
3442ret gbc2fl lg l0 get ggk3 0 add get ggk2 1 get ggk2 3
3443end
3444dl = sub lg l0
3445if lt dl 0.0f64
3446dl = neg dl
3447end
3448fl = ulong 0
3449if ne add get ggk2 1 get ggk2 3 0
3450fl = or fl 1
3451end
3452if not lt ggclp get ggk3 0 gg2tf
3453fl = or fl 2
3454end
3455if not lt gg2rl 2 gg2tr
3456fl = or fl 4
3457end
3458if not lt dl 0.00001f64
3459fl = or fl 8
3460end
3461ret fl
3462end
3463
3464fn ggc2ln lg f64 l0 f64 ns long fl ulong to void
3465oputc 71
3466oputc 67
3467oputc 50
3468oputc 32
3469oputu get og 8
3470gg2pe gg2rl 0
3471gg2pe gg2rl 2
3472gg2pe gg2tr
3473gg2pe ggclp get ggk3 0
3474gg2pe gg2tf
3475oputc 32
3476oputu get ggk2 2
3477oputc 32
3478oputu get ggk2 0
3479oputc 32
3480oputl rmic mul ggclp get ggk3 1 1000000.0f64
3481oputc 32
3482oputl rmic mul ggclp get ggk3 2 1000000.0f64
3483oputc 32
3484oputu add get ggk2 1 get ggk2 3
3485oputc 32
3486oputl rmic ggclp l0
3487oputc 32
3488oputl rmic ggclp lg
3489oputc 32
3490oputl ns
3491oputc 32
3492oputu fl
3493oputc 10
3494end
3495
3496note " x_e9"
3497fn gg2pe x f64 to void
3498oputc 32
3499oputl rmic mul x 1000.0f64
3500end
3501
3502fn ggc2vd ok bool to void
3503oputc 71
3504oputc 67
3505oputc 77
3506oputc 80
3507oputc 50
3508oputc 32
3509if ok
3510oputc 80
3511oputc 65
3512oputc 83
3513oputc 83
3514oputc 10
3515oflush
3516ret
3517end
3518oputc 70
3519oputc 65
3520oputc 73
3521oputc 76
3522oputc 10
3523oflush
3524exit 47
3525end
3526
3527note gtrain CK TRAIN UNTIL EVERY: cmdtrain with the gradient from the GPU (K = 1, no workers); prints
3528note `T s+1 loss_micro` at save points like train
3529fn ggtrain to void
3530if ne oargc 6
3531exit 2
3532end
3533ockloop 2
3534odopen 3
3535un = oargu 4
3536ev = oargu 5
3537if eq ev 0
3538exit 2
3539end
3540en = get og 42
3541if lt un en
3542en = un
3543end
3544ggopen
3545while lt get og 41 en bound 999999
3546s = get og 41
3547obatch s
3548put og 10 0
3549ls = gggrad
3550oadam
3551s1 = add s 1
3552put og 41 s1
3553if or eq rem s1 ev 0 eq s1 en
3554ocksave 2
3555oputc 84
3556oputc 32
3557oputu s1
3558oputc 32
3559oputl rmic ls
3560oputc 10
3561oflush
3562end
3563end
3564r = nvstop
3565end
3566
3567note BLK[i] = the f64 parameter i before the step
3568fn ggkw q ulong p ulong to void
3569i0 = mul q OCH
3570for j ulong sub oce q p i0
3571i = add i0 j
3572ogrp add p i owg i
3573end
3574end
3575
3576note GRD[i] = chip parameter i as f64
3577fn ggdw q ulong p ulong to void
3578h = get gbmem 5
3579wo = get gga 0
3580i0 = mul q OCH
3581for j ulong sub oce q p i0
3582i = add i0 j
3583x = uint mapget h add wo mul i 4 4
3584ocsp i f64 bitcast f32 x
3585end
3586end
3587
3588note over i < P with w0 = BLK[i], c = parameter i (CPU f64 result), g = GRD[i] (chip f32 result):
3589note u = c - w0, d = (g - f32(w0)) - u; ggam 0 max |d|, 1 max |u|, 2 max (|d| - 2^-24 |c|), the part
3590note of d above the half-ulp rounding of the f32 master weight
3591global ggam f64 3
3592fn ggad1 i ulong p ulong to void
3593w0 = ogrg add p i
3594c = owg i
3595u = sub c w0
3596g = sub ocsg i f64 f32 w0
3597d = fabs sub g u
3598u = fabs u
3599e = sub d mul fabs c 0.000000059604644775390625f64
3600if gt d get ggam 0
3601put ggam 0 d
3602end
3603if gt u get ggam 1
3604put ggam 1 u
3605end
3606if gt e get ggam 2
3607put ggam 2 e
3608end
3609end
3610
3611fn ggadc q ulong p ulong to void
3612i0 = mul q OCH
3613for j ulong sub oce q p i0
3614ggad1 add i0 j p
3615end
3616end
3617
3618note gacmp CK DATA S [FREEZE_LAYERS]: one step from CK on batch S with the chip gradient, Adam on the
3619note CPU (oadam, f64) and on the chip (ggadam, f32); prints `GA P maxd_e12 maxu_e12 rel_e9 resid_e9
3620note tol_e9 adam_ns loss_micro` and GACMP PASS (resid = max(|d| - 2^-24 |c|) / max |u| < tol = 2^-16)
3621note or GACMP FAIL (exit 46). With FREEZE_LAYERS (ggcarg) oadam runs with ofreeze fz and the chip
3622note with ggfz; every frozen chip weight must still be its uploaded f32 and every frozen CPU weight
3623note its loaded f64 (`GAF F fz changed`, ggfzad).
3624fn ggacmp to void
3625ggcarg
3626put og 10 0
3627p = get og 8
3628ggopen
3629ggupst
3630lg = ggpass true
3631for q ulong ocn p
3632ggkw q p
3633end
3634oadam
3635t0 = monons
3636ggadam
3637t1 = monons
3638for q2 ulong ocn p
3639ggdw q2 p
3640end
3641for k ulong 3
3642put ggam k 0.0f64
3643end
3644for q3 ulong ocn p
3645ggadc q3 p
3646end
3647mu = get ggam 1
3648rel = div get ggam 0 mu
3649res = div get ggam 2 mu
3650tol = div 1.0f64 65536.0f64
3651oputc 71
3652oputc 65
3653oputc 32
3654oputu p
3655oputc 32
3656oputl rmic mul get ggam 0 1000000.0f64
3657oputc 32
3658oputl rmic mul mu 1000000.0f64
3659oputc 32
3660oputl rmic mul rel 1000.0f64
3661oputc 32
3662oputl rmic mul res 1000.0f64
3663oputc 32
3664oputl rmic mul tol 1000.0f64
3665oputc 32
3666oputl sub t1 t0
3667oputc 32
3668oputl rmic lg
3669oputc 10
3670r = nvstop
3671ok = and lt res tol ggfzad p
3672oputc 71
3673oputc 65
3674oputc 67
3675oputc 77
3676oputc 80
3677oputc 32
3678if ok
3679oputc 80
3680oputc 65
3681oputc 83
3682oputc 83
3683oputc 10
3684oflush
3685ret
3686end
3687oputc 70
3688oputc 65
3689oputc 73
3690oputc 76
3691oputc 10
3692oflush
3693exit 46
3694end
3695
3696note gtrainf CK TRAIN UNTIL EVERY [FREEZE_LAYERS]: gtrain with the parameters and Adam state resident
3697note on the chip in f32 (ggadam); only the batch goes up each step. At save points the state comes back
3698note to f64 for ocksave and `T s+1 loss_micro` is printed; GT at the end has the last step's phase
3699note times. FREEZE_LAYERS F > 0 is trainf's frozen core on the chip (ggfz): `GFZ F fz` first, then the
3700note embeddings and layers 0..F-1 are skipped in backward, untouched by ADAM and saved as loaded.
3701fn ggtrainf to void
3702na = oargc
3703if and ne na 6 ne na 7
3704exit 2
3705end
3706put og 61 1
3707ockloop 2
3708odopen 3
3709un = oargu 4
3710ev = oargu 5
3711if eq ev 0
3712exit 2
3713end
3714if eq na 7
3715ggfzset oargu 6
3716ggfzpr
3717end
3718en = get og 42
3719if lt un en
3720en = un
3721end
3722ggopen
3723put ggnofp 0 1
3724if eq get og 46 5
3725ggload_ck5 true
3726else
3727ggupst
3728if ggbf_ok
3729ggew 20 ggp5 gga1 1 gga1 1
3730ggf2b ggp5 gga1 0 gga1 38
3731ggquant_w
3732end
3733end
3734while lt get og 41 en bound 999999
3735s = get og 41
3736s1 = add s 1
3737need_l = or eq rem s1 ev 0 eq s1 en
3738if need_l
3739put ggnoloss 0 0
3740else
3741put ggnoloss 0 1
3742end
3743obatch s
3744put og 10 0
3745put ggsqn_req 0 1
3746put ggsqn_done 0 0
3747ls = ggpass false
3748put ggsqn_req 0 0
3749put ggnoloss 0 0
3750t = monons
3751ggadam
3752put ggtm 5 sub monons t
3753put og 41 s1
3754if need_l
3755if eq get og 46 5
3756ggsave_ck5 2
3757else
3758ggdnst
3759ocksave 2
3760end
3761oputc 84
3762oputc 32
3763oputu s1
3764oputc 32
3765oputl rmic ls
3766oputc 10
3767oflush
3768end
3769end
3770ggtms
3771oflush
3772r = nvstop
3773end
3774
3775note gprof CK TRAIN N: N gtrainf steps from CK, nothing saved. The last step runs with the launch
3776note profile on (gk.om gkpf), then 64 ZERO launches of 64 floats measure the bare launch round trip.
3777note Prints `T steps loss_micro`, one `GP id gx gy gz m n k wait_ns` per profiled launch, then GT.
3778fn ggprof to void
3779if ne oargc 5
3780exit 2
3781end
3782put og 61 1
3783ockloop 2
3784odopen 3
3785nn = oargu 4
3786if eq nn 0
3787exit 2
3788end
3789ggopen
3790put ggnofp 0 1
3791if eq get og 46 5
3792ggload_ck5 true
3793else
3794ggupst
3795if ggbf_ok
3796ggew 20 ggp5 gga1 1 gga1 1
3797ggf2b ggp5 gga1 0 gga1 38
3798ggquant_w
3799end
3800end
3801ls = 0.0f64
3802for i ulong nn
3803s = get og 41
3804obatch s
3805put og 10 0
3806if eq add i 1 nn
3807put gkpf 0 1
3808end
3809ls = ggpass false
3810t = monons
3811ggadam
3812put ggtm 5 sub monons t
3813put og 41 add s 1
3814end
3815for i ulong 64
3816ggew 20 64 ggg 0 ggg 0
3817end
3818put gkpf 0 0
3819oputc 84
3820oputc 32
3821oputu get og 41
3822oputc 32
3823oputl rmic ls
3824oputc 10
3825for q ulong get gkpc 0
3826o = mul q 8
3827oputc 71
3828oputc 80
3829for j ulong 8
3830oputc 32
3831oputu get gkpl add o j
3832end
3833oputc 10
3834end
3835ggtms
3836oflush
3837r = nvstop
3838end
3839
3840note geval CK HELD W [MERGES]: eval (cli.om cmdeval) with the forward pass on the chip in f32. Windows
3841note w < W at offsets w T, B per forward (og 6); the last set pads its spare rows with window 0 and
3842note does not count them. Each window adds its mean row loss in window order and, when V > 256, the
3843note decoded length of its T targets, as cmdeval does. Prints `E step W loss_micro bpb_micro` like
3844note eval, then `GE gpu_ns` (upload, forwards and loss reads). Exits 2 and 23 as eval.
3845global ggev f64 1
3846global ggen ulong 1
3847fn ggeval to void
3848ww = ggevw
3849t = monons
3850ggopen
3851if eq get og 46 5
3852ggload_ck5 false
3853else
3854ggupw
3855end
3856put ggev 0 0.0f64
3857put ggen 0 0
3858bb = get og 6
3859nc = div add ww sub bb 1 bb
3860for c ulong nc
3861ggevc c ww
3862end
3863ns = sub monons t
3864ggevpr ww
3865oputc 71
3866oputc 69
3867oputc 32
3868oputl ns
3869oputc 10
3870oflush
3871r = nvstop
3872end
3873
3874note eval's argument checks; loads CK, MERGES and HELD and returns W
3875fn ggevw to ulong
3876na = oargc
3877if and ne na 5 ne na 6
3878exit 2
3879end
3880put og 61 1
3881ockloop 2
3882ww = oargu 4
3883v = get og 0
3884if gt v 256
3885if eq na 5
3886exit 2
3887end
3888obload 5 v
3889end
3890odopen 3
3891tt = get og 1
3892m = get odn 0
3893if eq ww 0
3894exit 23
3895end
3896if gt ww div sub m 1 tt
3897exit 23
3898end
3899ret ww
3900end
3901
3902note window set c: rows b < B hold window c B + b (window 0 past W), one chip forward, then the sums
3903fn ggevc c ulong ww ulong to void
3904bb = get og 6
3905tt = get og 1
3906for b ulong bb
3907w = add mul c bb b
3908if ge w ww
3909w = 0
3910end
3911obrow b mul w tt
3912end
3913ggupb
3914ggfwd
3915for b ulong bb
3916w = add mul c bb b
3917if lt w ww
3918ggevr b
3919end
3920end
3921end
3922
3923note add row b's mean loss (CE stats left by ggfwd) and, when V > 256, its decoded target bytes
3924fn ggevr b ulong to void
3925tt = get og 1
3926o = mul b tt
3927s = 0.0f64
3928for t ulong tt
3929s = add s ggrl add o t
3930end
3931put ggev 0 add get ggev 0 div s f64 tt
3932if gt get og 0 256
3933n = get ggen 0
3934for t ulong tt
3935n = add n obtlen get oby add o t
3936end
3937put ggen 0 n
3938end
3939end
3940
3941note the E line of cmdeval from the sums
3942fn ggevpr ww ulong to void
3943lo = div get ggev 0 f64 ww
3944bp = div lo 0.6931471805599453
3945if gt get og 0 256
3946tt = get og 1
3947bp = div mul bp f64 mul ww tt f64 get ggen 0
3948end
3949oputc 69
3950oputc 32
3951oputu get og 41
3952oputc 32
3953oputu ww
3954oputc 32
3955oputl rmic lo
3956oputc 32
3957oputl rmic bp
3958oputc 10
3959end
3960
3961note gdiag CK DATA S: the CE kernel's per-row stats (max m, sum of exp(l - m), logit l_y) on the chip
3962note in f32 against the CPU's f64 values for the same forward of batch S. Prints R, then the mean
3963note over rows times 1e9 of: d m, d l_y, d log sum, d loss, (sum of the chip's p) - 1, d (l_y - m).
3964note It located the CE sum bias that the Kahan sum in gr.om grce removes.
3965global gdacc f64 6
3966global gdcpu f64 3
3967fn ggdiag to void
3968if ne oargc 5
3969exit 2
3970end
3971ockloop 2
3972odopen 3
3973obatch oargu 4
3974put og 10 0
3975ggopen
3976ggupw
3977ggupb
3978ggfwd
3979for i ulong 6
3980put gdacc i 0.0f64
3981end
3982for b ulong get og 6
3983x = ofwd b
3984for t ulong get og 1
3985ggdrow b t
3986end
3987end
3988n = f64 ggr
3989oputu ggr
3990for i ulong 6
3991oputc 32
3992oputl rmic mul div get gdacc i n 1000.0f64
3993end
3994oputc 10
3995oflush
3996r = nvstop
3997end
3998
3999fn ggdrow b ulong t ulong to void
4000r = add mul b get og 1 t
4001ggdcpu t get oby r
4002ggdacc r
4003put gdacc 4 add get gdacc 4 sub ggdps r 1.0f64
4004end
4005
4006note CPU f64 stats of row t of the last ofwd: max logit, log sum exp(l - max), logit of y
4007fn ggdcpu t ulong y ulong to void
4008d = get og 2
4009v = get og 0
4010pr = add get og 18 mul t v
4011klogit add get og 17 mul t d pr v d
4012m = bitcast f64 ulong 18442240474082181120
4013for j ulong v
4014m = fmax m get oa add pr j
4015end
4016s = 0.0f64
4017for k ulong v
4018s = add s fexp sub get oa add pr k m
4019end
4020put gdcpu 0 m
4021put gdcpu 1 flog s
4022put gdcpu 2 get oa add pr y
4023end
4024
4025fn ggdacc r ulong to void
4026o = add get gga 12 mul r 12
4027mg = sub ggf o get gdcpu 0
4028ls = sub flog ggf add o 4 get gdcpu 1
4029lg = sub ggf add o 8 get gdcpu 2
4030put gdacc 0 add get gdacc 0 mg
4031put gdacc 1 add get gdacc 1 lg
4032put gdacc 2 add get gdacc 2 ls
4033put gdacc 3 add get gdacc 3 sub add ls mg lg
4034put gdacc 5 add get gdacc 5 sub lg mg
4035end
4036
4037note sum of the chip's probabilities of row r, recovered from the CE output L = (p - [j = y]) sc
4038fn ggdps r ulong to f64
4039v = get og 0
4040sc = div 1.0f64 f64 ggr
4041o = add get gga 11 mul mul r v 4
4042s = 0.0f64
4043for j ulong v
4044s = add s div ggf add o mul j 4 sc
4045end
4046ret add s 1.0f64
4047end
4048
4049note gserve and gsample (aien-train DRA-292): serve and sample (serve.om, cli.om) with the per-token
4050note forward on the chip. cli.om's ochipup calls ogchip once the checkpoint loads: B = 1 (one
4051note context row, R = T), open the chip, upload the f32 parameters. olastf then calls oglast per
4052note token: upload the T context tokens of obx row 0 (positions past CT hold earlier tokens or 0,
4053note which the causal forward never reads for position CT - 1), ggdfwd (below: the rows up to the
4054note context's last 64-row tile, no CE, so the logits stay logits), and the logits of position
4055note CT - OMUTZ read back as f64 into TMP[0..V) (oa), where
4056note argmax and draws run as on the CPU.
4057note OMUTZ: 1 in the product; 0 in mutant z (the row past the context), which gscmp must catch.
4058const OMUTZ ulong 1
4059fn ogchip to void
4060put og 6 1
4061ggopen
4062if eq get og 46 5
4063ggload_ck5 false
4064else
4065ggupw
4066end
4067end
4068
4069fn oglast to void
4070ggupt
4071put gkinv 0 1
4072ggdfwd
4073ogrd
4074end
4075
4076note the logits of position CT - OMUTZ (row CT - OMUTZ of slot 11) read back as f64 into TMP[0..V) (oa)
4077fn ogrd to void
4078v = get og 0
4079r = sub get og 9 OMUTZ
4080o = add get gga 11 mul mul r v 4
4081tm = get og 27
4082for c ulong v
4083put oa add tm c ggf add o mul c 4
4084end
4085end
4086
4087note ggdfwd (ham/ollm-speed-kvdec): per-token GPU forward, including mixed-BF16 variants. ggfwd runs all
4088note R = T rows every token; a causal forward never lets a row read a later row, so the logits of
4089note position CT - 1 need rows 0..CT - 1 only. ggdfwd runs rows 0..rr - 1, rr = 64 ceil(CT / 64)
4090note capped at T (whole 64-row tiles, so every GEMM keeps ggfwd's 64x64 kernels and per-element sums),
4091note keeps the residual stream in place in layer 0's input hin (hin += o wo, hin += relu(bn w1) w2: the
4092note same f32 adds as ggffn's COPY then accumulate, without its two COPY launches per layer), and runs
4093note the final RMS and the logits GEMM on the last 64-row tile only (the one holding row CT - 1).
4094note Rows at or past rr keep stale arena data that no computed row reads (causal GEMM tiles; softmax
4095note chunks past a row's own are masked, gr.om k6). No CE launch (ggnoce's logits stay logits).
4096note OMUTJ: 1 in the product; 0 in mutant j (rr = 64 floor(CT / 64), at least 64: row CT - 1 lies past
4097note the computed rows once CT > 64 is not a multiple of 64), which gscmp and gdbench mode 2 must catch.
4098const OMUTJ ulong 1
4099note ggdm[0]: the rows the ggd* GEMMs run over
4100global ggdm ulong 1
4101note bfdec selects BF16 for attention-output and FFN projections only. QKV,
4102note attention scores/value sums, normalization and final logits retain f32.
4103global bfdec uint 1
4104
4105fn ggdrows to ulong
4106ct = get og 9
4107rr = mul div add ct 63 64 64
4108if eq OMUTJ 0
4109rr = mul div ct 64 64
4110if eq rr 0
4111rr = 64
4112end
4113end
4114tt = get og 1
4115if gt rr tt
4116rr = tt
4117end
4118ret rr
4119end
4120
4121note C[m][n] (+)= X[m][k] W[k][n] over the first m = ggdm[0] rows
4122fn ggdxw x ulong w ulong c ulong n ulong k ulong bt bool to void
4123k4 = uint mul k 4
4124n4 = uint mul n 4
4125gkopa 0 x k4 4 0 0
4126gkopa 1 w 4 n4 0 0
4127gkopc c n4 4 0 0 f32 1.0f64
4128ggmm get ggdm 0 n k 1 1 bt
4129end
4130
4131note C[m][n] (+)= X[m][k] W[n][k]^T over the first m = ggdm[0] rows
4132fn ggdxwt x ulong w ulong c ulong n ulong k ulong bt bool to void
4133k4 = uint mul k 4
4134n4 = uint mul n 4
4135gkopa 0 x k4 4 0 0
4136gkopa 1 w k4 4 0 0
4137gkopc c n4 4 0 0 f32 1.0f64
4138ggmm get ggdm 0 n k 1 1 bt
4139end
4140
4141note RMSF (kernel 10) over rows [r0, r0 + m) of the D-wide v and y and of s
4142fn ggdrms v ulong g ulong y ulong s ulong r0 ulong m ulong to void
4143o = mul r0 mul get og 2 4
4144r = mul m 32
4145ggu 0 r
4146ggpt 1 add v o
4147ggpt 2 g
4148ggpt 3 add y o
4149ggpt 4 add s mul r0 4
4150ggrun 10 10 ggcd r 64 1 1
4151end
4152
4153note q, k, u = a wq, a wk, a wv over the first ggdm[0] rows (ggqkv's batched GEMM)
4154fn ggdqkv l ulong to void
4155d = get og 2
4156a = ggl l 1
4157w = ggw ovwq l
4158c = ggl l 2
4159s0 = get ggs 0
4160s6 = get ggs 6
4161s7 = get ggs 7
4162gkopa 0 a s0 4 0 0
4163gkopa 1 w 4 s0 0 s7
4164gkopc c s0 4 0 s6 f32 1.0f64
4165ggmm get ggdm 0 d d 3 3 false
4166end
4167
4168note ggatt over the first rr = ggdm[0] rows: S (causal, rr x rr), SOFTF on rows < rr (grid rr / 2;
4169note arg 0 stays the thread bound T 32), o = P u (causal, k = rr)
4170fn ggdatt l ulong to void
4171rr = get ggdm 0
4172e = get og 7
4173pp = ggl l 12
4174q = ggl l 2
4175k = ggl l 3
4176gghd 0 q false
4177gghd 1 k false
4178isq = div 1.0f64 fsqrt f64 e
4179ggcp pp isq
4180ggbh rr rr e false 3
4181ggu 0 mul get og 1 32
4182ggpt 1 pp
4183ggpt 2 pp
4184ggrun 15 6 div rr 2 mul get og 6 get og 3 1
4185u = ggl l 4
4186o = ggl l 5
4187ggpl 0 pp false
4188gghd 1 u true
4189ggch o 1.0f64
4190ggbh rr e rr false 1
4191end
4192
4193note hin += o wo; bn = rms(hin) g2; hin += relu(bn w1) w2, over the first ggdm[0] rows, hin = layer 0's
4194fn ggdffn l ulong to void
4195savedbf = get gkbf 0
4196d = get og 2
4197f = get og 4
4198h = ggl 0 0
4199x = ggl l 5
4200w = ggw ovwo l
4201if ne get bfdec 0 0
4202put gkbf 0 1
4203end
4204ggdxw x w h d d true
4205put gkbf 0 savedbf
4206x = ggl l 7
4207w = ggw ovg2 l
4208s = ggl l 9
4209ggdrms h w x s 0 get ggdm 0
4210fp = ggl l 10
4211w = ggw ovw1 l
4212y = ggl l 11
4213gkope 1 y
4214if ne get bfdec 0 0
4215put gkbf 0 1
4216end
4217ggdxw x w fp f d false
4218put gkbf 0 savedbf
4219w = ggw ovw2 l
4220if ne get bfdec 0 0
4221put gkbf 0 1
4222end
4223ggdxw y w h d f true
4224put gkbf 0 savedbf
4225end
4226
4227fn ggdfl l ulong to void
4228h = ggl 0 0
4229w = ggw ovg1 l
4230a = ggl l 1
4231s = ggl l 8
4232ggdrms h w a s 0 get ggdm 0
4233ggdqkv l
4234ggdatt l
4235ggdffn l
4236end
4237
4238fn ggdfwd to void
4239rr = ggdrows
4240put ggdm 0 rr
4241d = get og 2
4242x = ggl 0 0
4243ggu 0 d
4244ggpt 1 x
4245x = ggw 0
4246ggpt 2 x
4247x = ggw get og 30
4248ggpt 3 x
4249x = gga1 2
4250ggpt 4 x
4251ggrun 9 10 ggcd d 64 rr get og 6
4252for l ulong get og 56 bound 9223372036854775807
4253ggdfl l
4254end
4255ggdhead rr
4256end
4257
4258note final RMS and logits GEMM over the last 64-row tile of the rr rows (all rr when rr < 64)
4259fn ggdhead rr ulong to void
4260d = get og 2
4261tb = ulong 64
4262if lt rr 64
4263tb = rr
4264end
4265b = sub rr tb
4266h = ggl 0 0
4267w = ggw get og 33
4268z = gga1 10
4269s = gga1 9
4270ggdrms h w z s b tb
4271put ggdm 0 tb
4272x = add z mul mul b d 4
4273w = ggw 0
4274v = get og 0
4275c = add gga1 11 mul mul b v 4
4276ggdxwt x w c v d false
4277put ggdm 0 rr
4278end
4279
4280note the forward of gdbench mode md: 0 ggfwd without CE (the gserve forward at edf39b0), 3 ggdfwd with
4281note every 64x64 GEMM on the BF16 tensor-core kernels (gk.om gkbf 1, docs/omegallm/CONTRACT-BF16.md;
4282note GEMMs on the 32x32 tile stay f32), 5 ggdfwd as one batch.om submission (ggdbat), 7 selective BF16, else ggdfwd
4283note in f32
4284fn ggdfm md ulong to void
4285if eq md 0
4286put ggnoce 0 1
4287ggfwd
4288put ggnoce 0 0
4289ret
4290end
4291if eq md 3
4292put gkbf 0 1
4293ggdfwd
4294put gkbf 0 0
4295ret
4296end
4297if eq md 7
4298put bfdec 0 1
4299ggdfwd
4300put bfdec 0 0
4301ret
4302end
4303if eq md 5
4304ggdbat
4305ret
4306end
4307ggdfwd
4308end
4309
4310note ggdbat: ggdfwd's launches recorded (gk.om gkbat) into one batch.om batch of exactly 3 + 9 L
4311note launches (EMB, nine per layer, final RMS, logits; batch.om refuses a different count before the
4312note doorbell), sent once and waited once (gkbsub). The kernels, their order and arguments are
4313note ggdfwd's; batch.om ends each body with a WFI release and a shader invalidate, so each grid
4314note drains before the next starts. gdbench opens the batch packet (gbbopen) once after the chip.
4315fn ggdbat to void
4316if not gbbegin add 3 mul 9 get og 56
4317ggdie 120
4318end
4319put gkbat 0 1
4320ggdfwd
4321put gkbat 0 0
4322if not gkbsub
4323ggdie 121
4324end
4325end
4326
4327note FNV-1a over the f64 bits of the V logits in TMP (equal hashes: the same logits, bit for bit)
4328fn ggdhash to ulong
4329tm = get og 27
4330h = ulong 14695981039346656037
4331for c ulong get og 0
4332x = bitcast ulong get oa add tm c
4333h = mul xor h x 1099511628211
4334end
4335ret h
4336end
4337
4338note one gdbench step in mode md on the current context: prints `GD step ct tok hash up_ns fwd_ns
4339note rd_ns launches build_ns wait_ns` (up: the token upload, fwd: the forward's launches, rd: logits
4340note read back and argmax; launches, build and wait from gk.om's gkt) and returns the token
4341fn ggdstep s ulong md ulong to ulong
4342k0 = get gkt 0
4343b0 = get gkt 1
4344w0 = get gkt 2
4345t0 = monons
4346ggupt
4347put gkinv 0 1
4348t1 = monons
4349ggdfm md
4350t2 = monons
4351ogrd
4352a = oargmax
4353t3 = monons
4354h = ggdhash
4355oputc 71
4356oputc 68
4357oputc 32
4358oputu s
4359oputc 32
4360oputu get og 9
4361oputc 32
4362oputu a
4363oputc 32
4364oputu h
4365oputc 32
4366oputl sub t1 t0
4367oputc 32
4368oputl sub t2 t1
4369oputc 32
4370oputl sub t3 t2
4371oputc 32
4372oputl sub get gkt 0 k0
4373oputc 32
4374oputl sub get gkt 1 b0
4375oputc 32
4376oputl sub get gkt 2 w0
4377oputc 10
4378oflush
4379ret a
4380end
4381
4382note gdbench comparison step (2, 4, 6 or 8): ggfwd's logits into ggdcl, then ggdfwd's (f32 in mode 2, BF16 in mode 4,
4383note mode 6 batched, mode 8 selective BF16) into TMP on the same context; prints `GQ step ct tok0 tok1 ndiff maxdiff_e9 margin_e9`
4384note (ndiff: logits whose f64 bits differ, read back from f32, so f32 bits; margin: ggfwd's top-1
4385note logit minus its top-2) and returns tok0. Totals: ggdnd ndiff, ggdmx maxdiff, ggdkd the steps whose
4386note tokens differ although the margin exceeds 2 BFTOL. The logits slot is zeroed (ZERO 20) between
4387note the two forwards, so ggdfwd cannot pass on ggfwd's rows.
4388note BFTOL: the BF16 decode tolerance in nats, declared before its first chip run (ham/ollm-speed
4389note 2026-11-10): modes 4 and 8 pass when maxdiff <= BFTOL and no high-margin step diverges.
4390const BFTOL f64 0.25
4391global ggdcl f64 65536
4392global ggdmx f64 1
4393global ggdnd ulong 1
4394global ggdkd ulong 1
4395global ggdst f64 1
4396
4397note ggdcl[a] minus the largest other ggdcl[c], c < V
4398fn ggdmg a ulong to f64
4399b = bitcast f64 ulong 18442240474082181120
4400for c ulong get og 0
4401if ne c a
4402b = fmax b get ggdcl c
4403end
4404end
4405ret sub get ggdcl a b
4406end
4407
4408fn ggdpar s ulong md ulong to ulong
4409ggupt
4410put gkinv 0 1
4411ggdfm 0
4412ogrd
4413ggdkeep
4414a0 = oargmax
4415v = get og 0
4416ggew 20 mul ggr v gga1 11 gga1 11
4417if eq md 4
4418ggdfm 3
4419else
4420ggdfm sub md 1
4421end
4422ogrd
4423a1 = oargmax
4424nd = ggdcmp
4425dm = get ggdst 0
4426put ggdnd 0 add get ggdnd 0 nd
4427put ggdmx 0 fmax get ggdmx 0 dm
4428mg = ggdmg a0
4429if and ne a0 a1 gt mg mul 2.0f64 BFTOL
4430put ggdkd 0 add get ggdkd 0 1
4431end
4432oputc 71
4433oputc 81
4434oputc 32
4435oputu s
4436oputc 32
4437oputu get og 9
4438oputc 32
4439oputu a0
4440oputc 32
4441oputu a1
4442oputc 32
4443oputu nd
4444oputc 32
4445oputl long mul dm 1000000000.0f64
4446oputc 32
4447oputl long mul mg 1000000000.0f64
4448oputc 10
4449oflush
4450ret a0
4451end
4452
4453note ggdcl = the V logits in TMP
4454fn ggdkeep to void
4455tm = get og 27
4456for c ulong get og 0
4457x = get oa add tm c
4458put ggdcl c x
4459end
4460end
4461
4462note the logits in TMP whose bits differ from ggdcl; the largest |difference| into ggdst[0]
4463fn ggdcmp to ulong
4464tm = get og 27
4465nd = ulong 0
4466dm = 0.0f64
4467for c ulong get og 0
4468x = get oa add tm c
4469y = get ggdcl c
4470if ne bitcast ulong x bitcast ulong y
4471nd = add nd 1
4472end
4473dm = fmax dm fabs sub x y
4474end
4475put ggdst 0 dm
4476ret nd
4477end
4478
4479fn ggdsel s ulong md ulong to ulong
4480if or or or eq md 2 eq md 4 eq md 6 eq md 8
4481ret ggdpar s md
4482end
4483ret ggdstep s md
4484end
4485
4486note gdbench CK N PROMPT MERGES MODE (ham/ollm-speed): the per-token chip forward,
4487note Modes 7 and 8 select the BF16 decode policy and its comparison, respectively.
4488note They retain QKV, attention and logits in f32; three projection GEMMs per layer use BF16.
4489note timed, on the contexts of N greedy steps from token 0 then PROMPT's encoding (once the context
4490note holds T tokens it slides, as ocpush does). MODE 0: ggfwd (all R = T rows, the gserve forward at
4491note edf39b0); 1: ggdfwd (oglast's forward); 3: ggdfwd with BF16 GEMMs (ggdfm 3); 5: ggdfwd as one
4492note batched submission (ggdbat); 7: selective BF16. Modes 2, 4, 6 and 8 compare ggfwd and ggdfwd
4493note (2 f32, 4 BF16, 6 batched, 8 selective BF16) on every
4494note step, one GQ line each (ggdpar), then `GDQ N ndiff maxdiff_e9 diverged`; modes 2 and 6 exit 41
4495note when any logit differs in its bits; modes 4 and 8 exit 42 when maxdiff exceeds BFTOL or a high-margin step
4496note diverged. Modes 0, 1, 3, 5 and 7 print one GD line per step (ggdstep), then run one more forward
4497note at the last context with the launch profile on (`GP id gx gy gz m n k wait_ns` per launch, gk.om
4498note gkpf; batched launches log wait 0), 64 ZERO launches of 64 floats (the bare launch round trip,
4499note GP id 20) and `GDT mode N load_ns open_ns` (checkpoint and merges load; chip open and f32
4500note parameter upload).
4501fn ggdbench to void
4502if ne oargc 7
4503exit 2
4504end
4505t0 = monons
4506ockloop 2
4507nn = oargu 3
4508md = oargu 6
4509v = get og 0
4510if or le v 256 gt md 8
4511exit 2
4512end
4513obload 5 v
4514t1 = monons
4515ogchip
4516if or eq md 5 eq md 6
4517if not gbbopen
4518ggdie 122
4519end
4520end
4521t2 = monons
4522put og 9 0
4523ocpush 0
4524np = obprompt 4
4525for i ulong np
4526ocpush get obtk i
4527end
4528put ggdmx 0 0.0f64
4529put ggdnd 0 0
4530put ggdkd 0 0
4531for s ulong nn
4532a = ggdsel s md
4533ocpush a
4534end
4535if or or or eq md 2 eq md 4 eq md 6 eq md 8
4536ggdverd nn md
4537ret
4538end
4539ggdprof md
4540oputc 71
4541oputc 68
4542oputc 84
4543oputc 32
4544oputu md
4545oputc 32
4546oputu nn
4547oputc 32
4548oputl sub t1 t0
4549oputc 32
4550oputl sub t2 t1
4551oputc 10
4552oflush
4553r = nvstop
4554end
4555
4556note the parity modes' last line `GDQ N ndiff maxdiff_e9 diverged`, then their exit codes
4557fn ggdverd nn ulong md ulong to void
4558oputc 71
4559oputc 68
4560oputc 81
4561oputc 32
4562oputu nn
4563oputc 32
4564oputu get ggdnd 0
4565oputc 32
4566oputl long mul get ggdmx 0 1000000000.0f64
4567oputc 32
4568oputu get ggdkd 0
4569oputc 10
4570oflush
4571r = nvstop
4572if and and ne md 4 ne md 8 gt get ggdnd 0 0
4573exit 41
4574end
4575if and or eq md 4 eq md 8 or gt get ggdmx 0 BFTOL gt get ggdkd 0 0
4576exit 42
4577end
4578end
4579
4580note one forward in mode md at the current context with the launch profile on, then 64 ZERO launches
4581note of 64 floats; prints one GP line per profiled launch
4582fn ggdprof md ulong to void
4583put gkpc 0 0
4584put gkpf 0 1
4585ggupt
4586put gkinv 0 1
4587ggdfm md
4588for i ulong 64
4589ggew 20 64 ggg 0 ggg 0
4590end
4591put gkpf 0 0
4592for q ulong get gkpc 0
4593o = mul q 8
4594oputc 71
4595oputc 80
4596for j ulong 8
4597oputc 32
4598oputu get gkpl add o j
4599end
4600oputc 10
4601end
4602end
4603
4604note gscmp CK N PROMPT MERGES (V > 256): the chip forward against the CPU forward on the contexts of
4605note N greedy steps from token 0 then PROMPT's encoding. Each step runs ofwd and olast (f64) into
4606note gscl, then oglast (chip, f32) into TMP, and prints `GS step cpu_tok chip_tok maxdiff_nano
4607note margin_nano`: maxdiff = max over the V logits of |chip - cpu|, margin = the CPU's top-1 logit
4608note minus its top-2. The CPU token is pushed. Last line `GSCMP N diverged maxdiff_nano`: a step
4609note diverges when the tokens differ although the margin exceeds 2 GSTOL. Exit 40 when a step
4610note diverged or maxdiff exceeds GSTOL.
4611const GSTOL f64 0.002
4612global gscl f64 65536
4613note gscmp totals: gsmd[0] the largest maxdiff, gskd[0] the diverged steps
4614global gsmd f64 1
4615global gskd ulong 1
4616fn ggscmp to void
4617if ne oargc 6
4618exit 2
4619end
4620ockloop 2
4621nn = oargu 3
4622v = get og 0
4623if le v 256
4624exit 2
4625end
4626obload 5 v
4627ogchip
4628put og 9 0
4629ocpush 0
4630np = obprompt 4
4631for i ulong np
4632ocpush get obtk i
4633end
4634put gsmd 0 0.0f64
4635put gskd 0 0
4636for s ulong nn
4637a = ggsst s
4638ocpush a
4639end
4640md = get gsmd 0
4641kd = get gskd 0
4642oputc 71
4643oputc 83
4644oputc 67
4645oputc 77
4646oputc 80
4647oputc 32
4648oputu nn
4649oputc 32
4650oputu kd
4651oputc 32
4652oputl long mul md 1000000000.0f64
4653oputc 10
4654oflush
4655r = nvstop
4656if or gt kd 0 gt md GSTOL
4657exit 40
4658end
4659end
4660
4661note one gscmp step s on the current context: prints its GS line, updates the totals, returns the
4662note CPU token
4663fn ggsst s ulong to ulong
4664a1 = ggscpu
4665mg = ggsmg a1
4666oglast
4667a2 = oargmax
4668dm = ggsdf
4669put gsmd 0 fmax get gsmd 0 dm
4670if and ne a1 a2 gt mg mul 2.0f64 GSTOL
4671put gskd 0 add get gskd 0 1
4672end
4673oputc 71
4674oputc 83
4675oputc 32
4676oputu s
4677oputc 32
4678oputu a1
4679oputc 32
4680oputu a2
4681oputc 32
4682oputl long mul dm 1000000000.0f64
4683oputc 32
4684oputl long mul mg 1000000000.0f64
4685oputc 10
4686oflush
4687ret a1
4688end
4689
4690note the CPU forward's last-position logits into gscl; returns its argmax
4691fn ggscpu to ulong
4692v = get og 0
4693tm = get og 27
4694lf = ofwd 0
4695olast
4696for c ulong v
4697put gscl c get oa add tm c
4698end
4699ret oargmax
4700end
4701
4702note max over c < V of |TMP[c] - gscl[c]|
4703fn ggsdf to f64
4704tm = get og 27
4705dm = 0.0f64
4706for c ulong get og 0
4707x = fabs sub get oa add tm c get gscl c
4708dm = fmax dm x
4709end
4710ret dm
4711end
4712
4713note the CPU's top-1 logit gscl[a] minus its largest other logit
4714fn ggsmg a ulong to f64
4715b = bitcast f64 ulong 18442240474082181120
4716for c ulong get og 0
4717if ne c a
4718b = fmax b get gscl c
4719end
4720end
4721ret sub get gscl a b
4722end
4723
4724note gfcache BASE.ck TOKS W OUT.hc [BF16]: batched GB10 frozen-trunk hidden-state dumper writing
4725note the canonical OLLMFH01 format (compatible with trainfc, spforge, and spcryst) at full GPU speed.
4726note Streams B windows per batched GPU forward (ggnoce = 2 skips logits/CE) and supports NP > 1 looped
4727note checkpoints and D <= 2048 with chunked OCKC writes. Optional arg 6 selects BF16 (1) or f32 (0);
4728note default is 1 when T, D, F are multiples of 64, else 0. Prints `GFCACHE W T D L gpu_ns bf16`.
4729fn ggfch_wry pos ulong b ulong to ulong
4730tt = get og 1
4731bo = mul b tt
4732for t ulong tt
4733ocput8 mul t 8 get oby add bo t
4734end
4735nb = mul tt 8
4736ocwput pos nb
4737ret add pos nb
4738end
4739
4740fn ggfch_wr1 base ulong cnt ulong to void
4741h = get gbmem 5
4742for j ulong cnt
4743x = uint mapget h add base mul j 4 4
4744ocput8 mul j 8 bitcast ulong f64 bitcast f32 x
4745end
4746end
4747
4748fn ggfch_wr pos ulong b ulong to ulong
4749pos = ggfch_wry pos b
4750td = mul get og 1 get og 2
4751base = add get gga 8 mul mul b td 4
4752done = ulong 0
4753while lt done td bound 65536
4754cnt = sub td done
4755if gt cnt OCKC
4756cnt = OCKC
4757end
4758ggfch_wr1 add base mul done 4 cnt
4759nb = mul cnt 8
4760ocwput pos nb
4761pos = add pos nb
4762done = add done cnt
4763end
4764ret pos
4765end
4766
4767fn ggfch_defbf to uint
4768if ne rem get og 1 64 0
4769ret 0
4770end
4771if ne rem get og 2 64 0
4772ret 0
4773end
4774if ne rem get og 4 64 0
4775ret 0
4776end
4777ret 1
4778end
4779
4780fn ggfch_step c ulong ww ulong pos ulong to ulong
4781bb = get og 6
4782tt = get og 1
4783for b ulong bb
4784w = add mul c bb b
4785if ge w ww
4786w = 0
4787end
4788obrow b mul w tt
4789end
4790ggupt
4791put gkinv 0 1
4792put ggnoce 0 2
4793ggfwd
4794put ggnoce 0 0
4795for b2 ulong bb
4796w2 = add mul c bb b2
4797if lt w2 ww
4798pos = ggfch_wr pos b2
4799end
4800end
4801ret pos
4802end
4803
4804fn ggfch_pr ww ulong ns long bf uint to void
4805oputc 71
4806oputc 70
4807oputc 67
4808oputc 65
4809oputc 67
4810oputc 72
4811oputc 69
4812oputc 32
4813oputu ww
4814oputc 32
4815oputu get og 1
4816oputc 32
4817oputu get og 2
4818oputc 32
4819oputu get og 5
4820oputc 32
4821oputl ns
4822oputc 32
4823oputu ulong bf
4824oputc 10
4825oflush
4826end
4827
4828fn ggfcache to void
4829na = oargc
4830if and ne na 6 ne na 7
4831exit 2
4832end
4833ockloop 2
4834odopen 3
4835ww = oargu 4
4836if or eq ww 0 gt ww div sub get odn 0 1 get og 1
4837exit 23
4838end
4839bf = ggfch_defbf
4840if eq na 7
4841ba = oargu 6
4842if gt ba 1
4843exit 2
4844end
4845bf = uint ba
4846end
4847bh = osp_basehash get og 5 get og 33
4848ocwopen 5
4849ocput8 0 OFHMAG
4850ocput8 8 ww
4851ocput8 16 get og 1
4852ocput8 24 get og 2
4853ocput8 32 get og 5
4854ocput8 40 bh
4855ocwput 0 48
4856pos = ulong 48
4857t0 = monons
4858ggopen
4859ggupw
4860put gkbf 0 bf
4861bb = get og 6
4862nc = div add ww sub bb 1 bb
4863for c ulong nc
4864pos = ggfch_step c ww pos
4865end
4866put gkbf 0 0
4867ns = sub monons t0
4868ocwend 5
4869r = nvstop
4870ggfch_pr ww ns bf
4871end
4872
4873note ogcmd: the gb10 target's GPU commands. cli.om's ocmd offers every command word here first
4874note (nogpu.om's ogcmd takes none on the CPU-only targets), so the gb10 unit is the aarch64 source
4875note with gpu/ in place of nogpu.om and no renamed main. It runs the GPU commands here and the BF16
4876note training commands in gb.om and returns true; any other word returns false and ocmd goes on to
4877note the CPU commands. The inference interface is documented in docs/omegallm/CONTRACT-BF16.md;
4878note command constants encode little-endian word bytes.
4879note nvfp4 M N K ITERS: live physical GB10 SM121 OMMA.SF.16864.F32.E2M1.E2M1.UE4M3.4X
4880note in-register instruction-rate microbenchmark (slot 84, 500 TF hardware peak).
4881note Loads per-thread operands/scales once before the K-loop, repeats 16 independent
4882note OMMA.SF.16864 ops in registers per ktile, and verifies 64 multi-CTA/warp-0..7
4883note output lanes + 4 NaN sentinels (checked=68). Not a global-memory GEMM. Prints:
4884note `NVFP4 M N K ITERS wall_ns tflops_x100 pct_peak_x100 expected got checked bad INREG_OMMA_MICROBENCH PASS/FAIL`.
4885fn ggnvfp4_lch m ulong n ulong k ulong to void
4886base = gkbase
4887ggpt 0 base
4888ggpt 1 add base 8192
4889ggpt 2 add base 16384
4890ggpt 3 add base 24576
4891ggpt 4 add base 65536
4892ggu 10 div k 64
4893ggu 11 n
4894ggrun 84 12 div n 128 div m 128 1
4895end
4896
4897fn ggnvfp4_cpos r ulong c ulong n ulong to ulong
4898ret add 65536 mul add mul r n c 4
4899end
4900
4901fn ggnvfp4_dr k ulong to ulong
4902if eq k 1
4903ret 7
4904end
4905if eq k 3
4906ret 99
4907end
4908if eq k 4
4909ret 96
4910end
4911if ge k 5
4912ret 103
4913end
4914ret 0
4915end
4916
4917fn ggnvfp4_dc k ulong to ulong
4918if or eq k 1 eq k 3
4919ret 6
4920end
4921if or eq k 2 eq k 4
4922ret 64
4923end
4924if eq k 5
4925ret 66
4926end
4927if eq k 6
4928ret 68
4929end
4930if eq k 7
4931ret 70
4932end
4933ret 0
4934end
4935
4936fn ggnvfp4_p1 h long r ulong c ulong n ulong to void
4937m = mapset h ggnvfp4_cpos r c n 4 2143289344
4938m = mapset h ggnvfp4_cpos r add c 1 n 4 2143289344
4939m = mapset h ggnvfp4_cpos add r 8 c n 4 2143289344
4940m = mapset h ggnvfp4_cpos add r 8 add c 1 n 4 2143289344
4941end
4942
4943fn ggnvfp4_c1 h long r ulong c ulong n ulong exp f64 to ulong
4944bad = ulong 0
4945if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos r c n 4 exp
4946bad = add bad 1
4947end
4948if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos r add c 1 n 4 exp
4949bad = add bad 1
4950end
4951if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos add r 8 c n 4 exp
4952bad = add bad 1
4953end
4954if ne f64 bitcast f32 uint mapget h ggnvfp4_cpos add r 8 add c 1 n 4 exp
4955bad = add bad 1
4956end
4957ret bad
4958end
4959
4960fn ggnvfp4_poison m ulong n ulong to void
4961h = get gbmem 5
4962rb1 = sub m 128
4963cb1 = sub n 128
4964for k ulong 8
4965dr = ggnvfp4_dr k
4966dc = ggnvfp4_dc k
4967ggnvfp4_p1 h dr dc n
4968ggnvfp4_p1 h add rb1 dr add cb1 dc n
4969end
4970r = mapset h ggnvfp4_cpos 0 8 n 4 2143289344
4971r = mapset h ggnvfp4_cpos 0 9 n 4 2143289344
4972r = mapset h ggnvfp4_cpos 96 72 n 4 2143289344
4973r = mapset h ggnvfp4_cpos 96 73 n 4 2143289344
4974put gkinv 0 1
4975end
4976
4977fn ggnvfp4_chk m ulong n ulong exp f64 to ulong
4978h = get gbmem 5
4979rb1 = sub m 128
4980cb1 = sub n 128
4981bad = ulong 0
4982for k ulong 8
4983dr = ggnvfp4_dr k
4984dc = ggnvfp4_dc k
4985bad = add bad ggnvfp4_c1 h dr dc n exp
4986bad = add bad ggnvfp4_c1 h add rb1 dr add cb1 dc n exp
4987end
4988if ne uint mapget h ggnvfp4_cpos 0 8 n 4 2143289344
4989bad = add bad 1
4990end
4991if ne uint mapget h ggnvfp4_cpos 0 9 n 4 2143289344
4992bad = add bad 1
4993end
4994if ne uint mapget h ggnvfp4_cpos 96 72 n 4 2143289344
4995bad = add bad 1
4996end
4997if ne uint mapget h ggnvfp4_cpos 96 73 n 4 2143289344
4998bad = add bad 1
4999end
5000ret bad
5001end
5002
5003fn ggnvfp4_pr m ulong n ulong k ulong it ulong ns long to void
5004got = f64 bitcast f32 uint mapget get gbmem 5 65536 4
5005exp = f64 k
5006bad = ggnvfp4_chk m n exp
5007flops = mul mul 2.0f64 f64 mul m n mul f64 k f64 it
5008tf = div flops mul f64 ns 1000.0f64
5009pct = div mul tf 100.0f64 500.0f64
5010oputc 78
5011oputc 86
5012oputc 70
5013oputc 80
5014oputc 52
5015oputc 32
5016oputu m
5017oputc 32
5018oputu n
5019oputc 32
5020oputu k
5021oputc 32
5022oputu it
5023oputc 32
5024oputl ns
5025oputc 32
5026oputl long mul tf 100.0f64
5027oputc 32
5028oputl long mul pct 100.0f64
5029oputc 32
5030oputl long exp
5031oputc 32
5032oputl long got
5033oputc 32
5034oputu 68
5035oputc 32
5036oputu bad
5037oputc 32
5038oputc 73
5039oputc 78
5040oputc 82
5041oputc 69
5042oputc 71
5043oputc 95
5044oputc 79
5045oputc 77
5046oputc 77
5047oputc 65
5048oputc 95
5049oputc 77
5050oputc 73
5051oputc 67
5052oputc 82
5053oputc 79
5054oputc 66
5055oputc 69
5056oputc 78
5057oputc 67
5058oputc 72
5059oputc 32
5060if eq bad 0
5061oputc 80
5062oputc 65
5063oputc 83
5064oputc 83
5065oputc 10
5066oflush
5067r = nvstop
5068ret
5069end
5070oputc 70
5071oputc 65
5072oputc 73
5073oputc 76
5074oputc 10
5075oflush
5076r = nvstop
5077exit 51
5078end
5079
5080fn ggnvfp4_fill to void
5081h = get gbmem 5
5082for i ulong 2048
5083r = mapset h mul i 4 4 572662306
5084r = mapset h add 8192 mul i 4 4 572662306
5085r = mapset h add 16384 mul i 4 4 943208504
5086r = mapset h add 24576 mul i 4 4 943208504
5087end
5088put gkinv 0 1
5089end
5090
5091fn ggnvfp4 to void
5092if ne oargc 6
5093exit 2
5094end
5095m = oargu 2
5096n = oargu 3
5097k = oargu 4
5098it = oargu 5
5099if or eq m 0 eq n 0
5100exit 2
5101end
5102if or eq k 0 eq it 0
5103exit 2
5104end
5105if or gt m 65536 gt n 65536
5106exit 2
5107end
5108if gt mul m n 1073741824
5109exit 2
5110end
5111if or gt k 16777216 gt it 1000000
5112exit 2
5113end
5114if or ne rem m 128 0 ne rem n 128 0
5115exit 2
5116end
5117if ne rem k 64 0
5118exit 2
5119end
5120grset 256 32 64 2 128 2
5121if not gropen add mul mul m n 4 1048576
5122ggsetupfail
5123end
5124ggnvfp4_fill
5125ggnvfp4_lch m n k
5126ggnvfp4_poison m n
5127t0 = monons
5128for q ulong it bound 1000001
5129ggnvfp4_lch m n k
5130end
5131ns = sub monons t0
5132ggnvfp4_pr m n k it ns
5133end
5134
5135text ggnv4sp_shm "/tmp/omega_vfio_kern.shm"
5136const GGNV4SP_MAGIC ulong 5714301938159472177
5137const GGNV4SP_KSHM ulong 2097152
5138global ggnv4sp_st ulong 6
5139global ggnv4sp_in ulong 9
5140global ggnv4sp_pm ulong 8
5141global ggnv4sp_fp f64 4
5142
5143fn ggnv4sp_open to long
5144fd = int fopen ggnv4sp_shm 0 len ggnv4sp_shm 2 0
5145if lt fd 0
5146ret neg long 1
5147end
5148ksh = mapnew fd 0 GGNV4SP_KSHM 3 1
5149sc = fclose fd
5150if le ksh 0
5151ret neg long 2
5152end
5153if ne mapget ksh 0 8 GGNV4SP_MAGIC
5154md = mapdrop ksh
5155ret neg long 3
5156end
5157ret ksh
5158end
5159
5160fn ggnv4sp_upcode ksh long to ulong
5161grset 256 32 64 2 128 2
5162gkslot 96
5163grnvfp4sp
5164base = mul ulong 96 16384
5165raw = sub get gkat 0 base
5166csz = mul div add raw 15 16 16
5167if lt csz 512
5168csz = 512
5169end
5170nw = div csz 4
5171for i ulong nw bound 8192
5172o = mul i 4
5173w = ulong get gkcode add base o
5174w = or w shl ulong get gkcode add base add o 1 8
5175w = or w shl ulong get gkcode add base add o 2 16
5176w = or w shl ulong get gkcode add base add o 3 24
5177r = mapset ksh add 4096 o 4 w
5178end
5179ret csz
5180end
5181
5182fn ggnv4sp_fill_in ksh long to void
5183rw = get ggnv4sp_in 0
5184khi = get ggnv4sp_in 1
5185klo = get ggnv4sp_in 2
5186aw = get ggnv4sp_in 3
5187bw = get ggnv4sp_in 4
5188ew = get ggnv4sp_in 5
5189r = mapset ksh 65536 4 rw
5190r = mapset ksh 65540 4 khi
5191r = mapset ksh 65544 4 klo
5192for i ulong 128
5193r = mapset ksh add 65600 mul i 4 4 aw
5194r = mapset ksh add 66112 mul i 4 4 bw
5195end
5196for i ulong 32
5197r = mapset ksh add 66624 mul i 4 4 ew
5198r = mapset ksh add 66752 mul i 4 4 943208504
5199r = mapset ksh add 66880 mul i 4 4 943208504
5200end
5201end
5202
5203fn ggnv4sp_fire ksh long csz ulong to long
5204cur = mapget ksh 8 8
5205nxt = add cur 1
5206r = mapset ksh 24 8 csz
5207r = mapset ksh 32 8 6144
5208r = mapset ksh 40 8 128
5209r = mapset ksh 48 8 64
5210r = mapset ksh 56 8 4096
5211r = mapset ksh 64 8 8192
5212cpufence
5213r = mapset ksh 8 8 nxt
5214cpufence
5215t0 = monons
5216done = false
5217for p ulong 200000000
5218ak = mapget ksh 16 8
5219if eq ak nxt
5220done = true
5221break
5222end
5223if gt sub monons t0 long 6000000000
5224break
5225end
5226cpurelax
5227end
5228if not done
5229ret neg long 70
5230end
5231put ggnv4sp_st 0 mapget ksh 72 8
5232put ggnv4sp_st 1 mapget ksh 80 8
5233put ggnv4sp_st 2 mapget ksh 88 8
5234ret long 0
5235end
5236
5237fn ggnv4sp_chk ksh long rw ulong ehi f64 elo f64 to ulong
5238bad = ulong 0
5239for w ulong 192
5240want = elo
5241if lt w rw
5242want = ehi
5243end
5244bo = add 589824 mul w 32
5245for j ulong 8
5246v = f64 bitcast f32 uint mapget ksh add bo mul j 4 4
5247if ne v want
5248bad = add bad 1
5249end
5250end
5251end
5252for s ulong 16
5253if ne mapget ksh add 595968 mul s 4 4 0
5254bad = add bad 1
5255end
5256end
5257ret bad
5258end
5259
5260fn ggnv4sp_ctrls ksh long csz ulong to ulong
5261put ggnv4sp_in 6 get ggnv4sp_in 0
5262put ggnv4sp_in 7 get ggnv4sp_in 1
5263put ggnv4sp_in 8 get ggnv4sp_in 2
5264bad = ulong 0
5265note Control 1: invalid csz=100 MUST be rejected with status=22
5266rc = ggnv4sp_fire ksh 100
5267if or ne rc 0 ne get ggnv4sp_st 1 22
5268bad = add bad 1
5269end
5270note Control 2: zero-operand A=0 MUST produce exact 0.0 across all 192 warps
5271put ggnv4sp_in 0 0
5272put ggnv4sp_in 1 1
5273put ggnv4sp_in 2 1
5274put ggnv4sp_in 3 0
5275put ggnv4sp_in 4 572662306
5276put ggnv4sp_in 5 1145324612
5277ggnv4sp_fill_in ksh
5278rc = ggnv4sp_fire ksh csz
5279if or ne rc 0 or ne get ggnv4sp_st 1 0 ne get ggnv4sp_st 2 0
5280bad = add bad 1
5281else
5282bad = add bad ggnv4sp_chk ksh 0 0.0f64 0.0f64
5283end
5284note Control 3: full-grid E2M1 Tensor Core linearity (B=1.0 -> 64.0 vs B=2.0 -> 128.0 across 1552 lanes)
5285put ggnv4sp_in 3 572662306
5286put ggnv4sp_in 4 572662306
5287put ggnv4sp_in 5 1145324612
5288ggnv4sp_fill_in ksh
5289rc = ggnv4sp_fire ksh csz
5290ma = f64 0.0f64
5291if and eq rc 0 and eq get ggnv4sp_st 1 0 eq get ggnv4sp_st 2 0
5292bad = add bad ggnv4sp_chk ksh 0 64.0f64 64.0f64
5293ma = f64 bitcast f32 uint mapget ksh 589824 4
5294else
5295bad = add bad 1
5296end
5297put ggnv4sp_in 4 1145324612
5298ggnv4sp_fill_in ksh
5299rc = ggnv4sp_fire ksh csz
5300mb = f64 0.0f64
5301if and eq rc 0 and eq get ggnv4sp_st 1 0 eq get ggnv4sp_st 2 0
5302bad = add bad ggnv4sp_chk ksh 0 128.0f64 128.0f64
5303mb = f64 bitcast f32 uint mapget ksh 589824 4
5304else
5305bad = add bad 1
5306end
5307if or ne ma 64.0f64 ne mb 128.0f64
5308bad = add bad 1
5309end
5310put ggnv4sp_in 0 get ggnv4sp_in 6
5311put ggnv4sp_in 1 get ggnv4sp_in 7
5312put ggnv4sp_in 2 get ggnv4sp_in 8
5313put ggnv4sp_st 3 ulong long ma
5314put ggnv4sp_st 4 ulong long mb
5315ret bad
5316end
5317
5318fn ggnv4sp_pr_tag to void
5319oputc 78
5320oputc 86
5321oputc 70
5322oputc 80
5323oputc 52
5324oputc 83
5325oputc 80
5326oputc 32
5327end
5328
5329fn ggnv4sp_pr_scope to void
5330oputc 73
5331oputc 78
5332oputc 82
5333oputc 69
5334oputc 71
5335oputc 95
5336oputc 79
5337oputc 77
5338oputc 77
5339oputc 65
5340oputc 95
5341oputc 83
5342oputc 80
5343oputc 95
5344oputc 49
5345oputc 54
5346oputc 56
5347oputc 49
5348oputc 50
5349oputc 56
5350oputc 32
5351end
5352
5353fn ggnv4sp_pr to void
5354flops = mul mul 2.0f64 f64 mul get ggnv4sp_pm 0 get ggnv4sp_pm 1 mul f64 get ggnv4sp_pm 2 f64 get ggnv4sp_pm 3
5355tf = div flops mul f64 get ggnv4sp_pm 4 1000.0f64
5356pct = div mul tf 100.0f64 1000.0f64
5357ggnv4sp_pr_tag
5358oputu get ggnv4sp_pm 0
5359oputc 32
5360oputu get ggnv4sp_pm 1
5361oputc 32
5362oputu get ggnv4sp_pm 2
5363oputc 32
5364oputu get ggnv4sp_pm 3
5365oputc 32
5366oputu get ggnv4sp_pm 4
5367oputc 32
5368oputu get ggnv4sp_pm 5
5369oputc 32
5370oputl long mul tf 100.0f64
5371oputc 32
5372oputl long mul pct 100.0f64
5373oputc 32
5374oputl long get ggnv4sp_fp 0
5375oputc 32
5376oputl long get ggnv4sp_fp 1
5377oputc 32
5378oputl long get ggnv4sp_fp 2
5379oputc 32
5380oputl long get ggnv4sp_fp 3
5381oputc 32
5382oputu 1552
5383oputc 32
5384oputu get ggnv4sp_pm 6
5385oputc 32
5386oputu get ggnv4sp_st 3
5387oputc 32
5388oputu get ggnv4sp_st 4
5389oputc 32
5390ggnv4sp_pr_scope
5391if eq get ggnv4sp_pm 6 0
5392oputc 80
5393oputc 65
5394oputc 83
5395oputc 83
5396oputc 10
5397oflush
5398ret
5399end
5400oputc 70
5401oputc 65
5402oputc 73
5403oputc 76
5404oputc 10
5405oflush
5406exit 51
5407end
5408
5409fn ggnv4sp_ok rc long to ulong
5410if ne rc 0
5411ret 1
5412end
5413if ne get ggnv4sp_st 1 0
5414ret 1
5415end
5416if ne get ggnv4sp_st 2 0
5417ret 1
5418end
5419ret 0
5420end
5421
5422fn ggnv4sp_vfio ksh long to void
5423csz = ggnv4sp_upcode ksh
5424bad = ggnv4sp_ctrls ksh csz
5425if ne bad 0
5426md = mapdrop ksh
5427put ggnv4sp_pm 4 1
5428put ggnv4sp_pm 5 1
5429put ggnv4sp_pm 6 bad
5430ggnv4sp_pr
5431ret
5432end
5433put ggnv4sp_in 3 572662306
5434put ggnv4sp_in 4 572662306
5435put ggnv4sp_in 5 1145324612
5436ggnv4sp_fill_in ksh
5437bad = add bad ggnv4sp_ok ggnv4sp_fire ksh csz
5438if ne bad 0
5439md = mapdrop ksh
5440put ggnv4sp_pm 4 1
5441put ggnv4sp_pm 5 1
5442put ggnv4sp_pm 6 bad
5443ggnv4sp_pr
5444ret
5445end
5446cns = ulong 0
5447t0 = monons
5448for q ulong get ggnv4sp_pm 3 bound 1000001
5449bad = add bad ggnv4sp_ok ggnv4sp_fire ksh csz
5450cns = add cns get ggnv4sp_st 0
5451end
5452put ggnv4sp_pm 5 ulong sub monons t0
5453put ggnv4sp_pm 4 cns
5454rw = get ggnv4sp_in 0
5455bad = add bad ggnv4sp_chk ksh rw get ggnv4sp_fp 0 get ggnv4sp_fp 2
5456put ggnv4sp_pm 6 bad
5457put ggnv4sp_fp 1 f64 bitcast f32 uint mapget ksh 589824 4
5458put ggnv4sp_fp 3 f64 bitcast f32 uint mapget ksh add 589824 mul rw 32 4
5459md = mapdrop ksh
5460ggnv4sp_pr
5461end
5462
5463fn ggnv4sp_rm to void
5464grset 256 32 64 2 128 2
5465if not gropen 65536
5466ggsetupfail
5467end
5468h = get gbmem 5
5469base = gkbase
5470r = mapset h 0 4 get ggnv4sp_in 0
5471r = mapset h 4 4 get ggnv4sp_in 1
5472r = mapset h 8 4 get ggnv4sp_in 2
5473for i ulong 128
5474r = mapset h add 64 mul i 4 4 572662306
5475r = mapset h add 576 mul i 4 4 572662306
5476end
5477for i ulong 32
5478r = mapset h add 1088 mul i 4 4 1145324612
5479r = mapset h add 1216 mul i 4 4 943208504
5480r = mapset h add 1344 mul i 4 4 943208504
5481end
5482put gkinv 0 1
5483ggpt 0 base
5484ggpt 1 add base 8192
5485ggrun 96 4 48 1 1
5486t0 = monons
5487for q ulong get ggnv4sp_pm 3 bound 1000001
5488ggpt 0 base
5489ggpt 1 add base 8192
5490ggrun 96 4 48 1 1
5491end
5492wns = ulong sub monons t0
5493rw = get ggnv4sp_in 0
5494put ggnv4sp_fp 1 f64 bitcast f32 uint mapget h 8192 4
5495put ggnv4sp_fp 3 f64 bitcast f32 uint mapget h add 8192 mul rw 32 4
5496bad = ulong 0
5497for w ulong 192
5498want = get ggnv4sp_fp 2
5499if lt w rw
5500want = get ggnv4sp_fp 0
5501end
5502bo = add 8192 mul w 32
5503for j ulong 8
5504if ne f64 bitcast f32 uint mapget h add bo mul j 4 4 want
5505bad = add bad 1
5506end
5507end
5508end
5509put ggnv4sp_st 3 0
5510put ggnv4sp_st 4 0
5511put ggnv4sp_pm 4 wns
5512put ggnv4sp_pm 5 wns
5513put ggnv4sp_pm 6 bad
5514rv = nvstop
5515ggnv4sp_pr
5516end
5517
5518fn ggnvfp4sp to void
5519if ne oargc 6
5520exit 2
5521end
5522m = oargu 2
5523n = oargu 3
5524k = oargu 4
5525it = oargu 5
5526if or eq m 0 eq n 0
5527exit 2
5528end
5529if or eq k 0 eq it 0
5530exit 2
5531end
5532if or gt m 65536 gt n 65536
5533exit 2
5534end
5535if or gt k 16777216 gt it 1000000
5536exit 2
5537end
5538if ne rem m 128 0
5539exit 2
5540end
5541if ne rem n 128 0
5542exit 2
5543end
5544if ne rem k 128 0
5545exit 2
5546end
5547nstr = mul div m 16 div n 64
5548bstr = div nstr 192
5549rw = rem nstr 192
5550kt = div k 128
5551khi = mul add bstr 1 kt
5552klo = mul bstr kt
5553put ggnv4sp_pm 0 m
5554put ggnv4sp_pm 1 n
5555put ggnv4sp_pm 2 k
5556put ggnv4sp_pm 3 it
5557put ggnv4sp_in 0 rw
5558put ggnv4sp_in 1 khi
5559put ggnv4sp_in 2 klo
5560put ggnv4sp_fp 0 mul f64 khi 64.0f64
5561put ggnv4sp_fp 2 mul f64 klo 64.0f64
5562ksh = ggnv4sp_open
5563if gt ksh 0
5564ggnv4sp_vfio ksh
5565ret
5566end
5567ggnv4sp_rm
5568end
5569
5570fn ogcmd w ulong to bool
5571if eq w 27696107117441895
5572put bfdec 0 1
5573put ogpu 0 1
5574put og 61 1
5575cmdserve
5576r = nvstop
5577ret true
5578end
5579if eq w 108235010962279
5580put bfdec 0 1
5581put ogpu 0 1
5582put og 61 1
5583cmdsamp
5584r = nvstop
5585ret true
5586end
5587if eq w 111559399797607
5588put ogpu 0 1
5589put og 61 1
5590cmdserve
5591r = nvstop
5592ret true
5593end
5594if eq w 28548202775016295
5595put ogpu 0 1
5596put og 61 1
5597cmdsamp
5598r = nvstop
5599ret true
5600end
5601if eq w 482871571303
5602ggscmp
5603ret true
5604end
5605if eq w 29382723376407655
5606ggdbench
5607ret true
5608end
5609if eq w 1886217063
5610ggcmp
5611ret true
5612end
5613if eq w 216634581863
5614ggcmp2
5615ret true
5616end
5617if eq w 121398885512295
5618ggtrain
5619ret true
5620end
5621if eq w 28831846509999207
5622ggtrainf
5623ret true
5624end
5625if eq w 482871566695
5626ggacmp
5627ret true
5628end
5629if eq w 465491617127
5630ggeval
5631ret true
5632end
5633if eq w 444015928423
5634ggdiag
5635ret true
5636end
5637if eq w 439956435047
5638ggprof
5639ret true
5640end
5641if eq w 27705946603156583
5642gbtrain
5643ret true
5644end
5645if eq w 108192095957095
5646gbprof
5647ret true
5648end
5649if eq w 482871566951
5650gbcmp
5651ret true
5652end
5653if eq w 55458452955751
5654gbcmp2
5655ret true
5656end
5657if eq w 29328856030863975
5658gbcmp2h
5659ret true
5660end
5661if eq w 113668228801127
5662gbfrag
5663ret true
5664end
5665if eq w 418346528103
5666gm_r = gm_selftest
5667if ne gm_r 0
5668exit gm_r
5669end
5670ret true
5671end
5672if eq w 474365654119
5673gt_r = gt_selftest
5674if ne gt_r 0
5675exit gt_r
5676end
5677ret true
5678end
5679if eq w 28543748692731495
5680ggfcache
5681ret true
5682end
5683if eq w 225224062574
5684ggnvfp4
5685ret true
5686end
5687if eq w 31651866452850286
5688ggnvfp4sp
5689ret true
5690end
5691if eq w 14758097674466407
5692gbtrain4
5693ret true
5694end
5695if eq w 7885070016531820135
5696gnv4gemm
5697ret true
5698end
5699if eq w 31645470240960103
5700gnv4cmp
5701ret true
5702end
5703ret false
5704end