interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / train / omega-llm / native / gpu / gg.om

2715 lines · 56136 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 48
11global ggcn ulong 65537
12note 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)
13global ggs uint 8
14note OMUTG: 1 in the product; 0 in mutant g (DK = -DS^T Q), which the parity check must catch.
15const OMUTG ulong 1
16note Looped models (docs/LOOP.md): the layer region holds one activation block per layer application
17note (og 56 of them), and application l reads the parameters and adds to the gradient of parameter layer
18note olp l (model.om ovg1..ovw2). Every weight-gradient launch accumulates into G, which ggbwd zeroes
19note once, so a core's gradient is the sum over its passes. Without a loop og 56 = L and olp l = l.
20note OMUTLG: 1 in the product; 0 in mutant lg (the w2 gradient of an application that repeats an
21note earlier parameter layer is not launched, model.om's mutant lp on the chip), which gcmp and gcmp2 on
22note a looped checkpoint must catch. Without a loop the two are the same.
23const OMUTLG ulong 1
24
25note GPU setup failed (ggopen): a clear stage and error on stderr, then exit 75. L1014: a setup
26note that failed before buffers 2 and 3 existed used to reach ggdie's mapget of the unmapped slots, which
27note trapped BOUNDS (exit 103) with no message. stdout stays free for the program's own output.
28note Error 81 is RM status 0x51 NV_ERR_NO_MEMORY (open-gpu-kernel-modules nvstatuscodes.h). Only stage 35
29note (the compute object, context.om) waits on it, 50 x 100 ms, because there it means another job holds
30note the GPU. At any other stage, such as 21 (a memory allocation), there is no wait and no retry, so
31note "try again" is not offered; the cause there is not established by this code, so the text names none.
32text ggsa "omega: GPU setup failed at stage "
33text ggsb ", error "
34text ggsc " (driver status 81, out of memory: seen while another job holds the GPU; it outlasted the bounded wait). Try again.\n"
35text 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"
36text ggsd " (positive is driver status, negative is errno or internal; the stage is the nvstage step in gpu/context.om).\n"
37
38fn ggerr1 n ulong to void
39p = ulong 0
40while lt p n bound 4097
41w = write 2 oout p sub n p
42if le w 0
43exit 70
44end
45p = add p ulong w
46end
47put oon 0 0
48end
49
50fn ggsetupfail to void
51oflush
52r = write 2 ggsa 0 len ggsa
53oputu ulong get nvstage 0
54ggerr1 get oon 0
55r = write 2 ggsb 0 len ggsb
56oputl get nverr 0
57ggerr1 get oon 0
58if eq get nverr 0 81
59if eq get nvstage 0 35
60r = write 2 ggsc 0 len ggsc
61else
62r = write 2 ggse 0 len ggse
63end
64else
65r = write 2 ggsd 0 len ggsd
66end
67exit 75
68end
69
70fn ggdie c ulong to void
71oputc 71
72oputc 88
73oputc 32
74oputu c
75oputc 32
76oputl get nverr 0
77oputc 32
78oputu ulong get nvstage 0
79note diagnostics: launches done, mark1, mark2, QMD semaphore; zero while slots 2 and 3 are unmapped
80note (a failed ggopen), where mapget of an unmapped slot trapped BOUNDS (exit 103, no message)
81oputc 32
82oputl get gkt 0
83oputc 32
84if and ne get gbmem 3 0 ne get gbmem 2 0
85oputu mapget get gbmem 3 0 4
86oputc 32
87oputu mapget get gbmem 3 16 4
88oputc 32
89oputu mapget get gbmem 2 8192 4
90else
91oputc 48
92oputc 32
93oputc 48
94oputc 32
95oputc 48
96end
97oputc 10
98oflush
99exit 84
100end
101
102fn ggcd n ulong k ulong to ulong
103ret div add n sub k 1 k
104end
105
106fn ggr to ulong
107ret mul get og 6 get og 1
108end
109
110note P rounded up to the SQN chunk of 512; the gradient buffer is that long and zeroed to it
111fn ggp5 to ulong
112ret mul ggcd get og 8 512 512
113end
114
115fn ggput i ulong n ulong to void
116c = get gga 47
117put gga i c
118b = mul div add mul n 4 255 256 256
119put gga 47 add c b
120end
121
122note arena plan for the dims in og; returns its size in bytes
123fn ggplan to ulong
124tt = get og 1
125d = get og 2
126h = get og 3
127f = get og 4
128bb = get og 6
129r = ggr
130rd = mul r d
131pp = mul mul bb h mul tt tt
132put gga 47 0
133for i ulong 8
134ggput add 24 i rd
135end
136ggput 32 r
137ggput 33 r
138ggput 34 mul r f
139ggput 35 mul r f
140ggput 36 pp
141put gga 7 get gga 47
142put gga 47 0
143ggput 0 get og 8
144ggput 1 ggp5
145ggput 2 r
146ggput 3 r
147ggput 4 add get og 0 1
148ggput 5 r
149put gga 6 get gga 47
150ls = mul get gga 7 get og 56
151put gga 47 add get gga 47 ls
152ggput 8 rd
153ggput 9 r
154ggput 10 rd
155ggput 11 mul r get og 0
156ggput 12 mul r 3
157ggput 13 rd
158ggput 14 rd
159ggput 15 mul rd 3
160ggput 16 rd
161ggput 17 mul r f
162ggput 18 pp
163ggput 19 rd
164ggput 20 mul bb d
165ggput 21 get og 8
166ggput 22 get og 8
167ggput 23 div ggp5 512
168note 37: split-K partials for ggsk, at most 2^21 floats (ggks stops doubling once a grid has 1024 blocks)
169ggput 37 2097152
170ret get gga 47
171end
172
173fn gga1 i ulong to ulong
174ret add get gga 40 get gga i
175end
176
177fn ggl l ulong f ulong to ulong
178o = add get gga 6 mul l get gga 7
179ret add add get gga 40 o get gga add 24 f
180end
181
182note address of the output of layer application l (the next hin, or HF after the last one)
183fn ggout l ulong to ulong
184if lt add l 1 get og 56
185ret ggl add l 1 0
186end
187ret gga1 8
188end
189
190fn ggw o ulong to ulong
191ret add gga1 0 mul o 4
192end
193
194fn ggg o ulong to ulong
195ret add gga1 1 mul o 4
196end
197
198note GEMM tiles need m, n multiples of 32 and k of 16: V T D F E multiples of 32; R = B T;
199note softmax rows keep T/32 values per lane in R32..R47, so T is at most 512
200fn ggok to bool
201for i ulong 5
202v = get og i
203if eq i 3
204v = get og 7
205end
206if eq i 1
207v = get og 1
208end
209if or eq v 0 ne rem v 32 0
210ret false
211end
212end
213if gt get og 0 65536
214ret false
215end
216if gt get og 1 512
217ret false
218end
219ret le mul get og 6 get og 3 65535
220end
221
222note open the chip and emit the kernels for the dims in og
223fn ggopen to void
224if not ggok
225ggdie 1
226end
227asz = ggplan
228v = get og 0
229tt = get og 1
230d = get og 2
231h = get og 3
232f = get og 4
233bb = get og 6
234grset v tt d h f bb
235if not gropen asz
236ggsetupfail
237end
238put gga 40 gkbase
239ggstr
240end
241
242note pointer j (1-based) into arg slots 2j, 2j + 1
243fn ggpt j uint a ulong to void
244put gkarg mul j 2 gblo a
245put gkarg add mul j 2 1 gbhi a
246end
247
248fn ggu s uint v ulong to void
249put gkarg s uint v
250end
251
252fn ggrun id uint na uint gx ulong gy ulong gz ulong to void
253put gkna 0 na
254x = uint gx
255y = uint gy
256z = uint gz
257if not gklaunch id x y z
258ggdie add 100 ulong id
259end
260end
261
262fn ggmm m ulong n ulong k ulong nz ulong h2 ulong bt bool to void
263a = uint m
264b = uint n
265c = uint k
266e = uint nz
267g = uint h2
268if not gkmm a b c e g bt
269ggdie 99
270end
271end
272
273note C[R][n] (+)= X[R][k] W[k][n]
274fn ggxw x ulong w ulong c ulong n ulong k ulong bt bool to void
275k4 = uint mul k 4
276n4 = uint mul n 4
277gkopa 0 x k4 4 0 0
278gkopa 1 w 4 n4 0 0
279gkopc c n4 4 0 0 f32 1.0f64
280ggmm ggr n k 1 1 bt
281end
282
283note C[R][n] (+)= X[R][k] W[n][k]^T
284fn ggxwt x ulong w ulong c ulong n ulong k ulong bt bool to void
285k4 = uint mul k 4
286n4 = uint mul n 4
287gkopa 0 x k4 4 0 0
288gkopa 1 w k4 4 0 0
289gkopc c n4 4 0 0 f32 1.0f64
290ggmm ggr n k 1 1 bt
291end
292
293note G[kk][n] += X[R][kk]^T DY[R][n], split over R when the grid is small (ggks)
294fn ggwg x ulong dy ulong g ulong kk ulong n ulong to void
295k4 = uint mul kk 4
296n4 = uint mul n 4
297s = ggks kk n 1
298if eq s 1
299gkopa 0 x 4 k4 0 0
300gkopa 1 dy 4 n4 0 0
301gkopc g n4 4 0 0 f32 1.0f64
302ggmm kk n ggr 1 1 true
303ret
304end
305sl = div ggr s
306gkopa 0 x 4 k4 0 uint mul mul sl kk 4
307gkopa 1 dy 4 n4 0 uint mul mul sl n 4
308ggsk kk n 1 s g 0
309end
310
311note Split-K for the weight gradients, whose depth is R = B T. A grid of (m/32)(n/32)nz blocks
312note leaves most SMs idle (rung-6 gprof: id 4 at 1.7-6 TFLOP/s on 64-256 blocks), so ggks doubles
313note the split s (up to 16, R/s a multiple of 16) until the grid reaches 1024 blocks: then
314note blocks s < 2048, so the m n nz s partials fit the 2^21 floats of slot 37.
315fn ggks m ulong n ulong nz ulong to ulong
316b = mul mul div m 32 div n 32 nz
317r = ggr
318s = ulong 1
319while lt mul b s 1024 bound 8
320s2 = mul s 2
321if or gt s2 16 ne rem r mul s2 16 0
322ret s
323end
324s = s2
325end
326ret s
327end
328
329note Operands A and B are set (gkopa) with the slice stride as s2. Batch z = z1 s + z2 (z1 < nz the
330note caller's batch, z2 < s the slice) writes partial P[z1][z2] = A[z1 z2]^T B[z1 z2] into slot 37,
331note then COLSUM 14 adds G[z1] (byte stride gz) += sum over z2 of P[z1][z2], in slice order.
332note Mutant p (OMUTP 0) adds one slice fewer, which gcmp2 must catch.
333const OMUTP ulong 1
334fn ggsk m ulong n ulong nz ulong s ulong g ulong gz uint to void
335mn = mul m n
336p = gga1 37
337n4 = uint mul n 4
338c1 = uint mul mul mn s 4
339c2 = uint mul mn 4
340gkopc p n4 4 c1 c2 f32 1.0f64
341k = div ggr s
342z = mul nz s
343ggmm m n k z s false
344ggskr mn nz s g gz
345end
346
347note COLSUM 14 over slot 37: G[c gz + i] += sum over z2 < s of P[c][z2][i], i < mn, c < nz
348fn ggskr mn ulong nz ulong s ulong g ulong gz uint to void
349kc = s
350if eq OMUTP 0
351kc = sub s 1
352end
353ggu 0 mn
354ggu 1 kc
355ggpt 1 gga1 37
356ggu 4 mul mn 4
357ggu 5 mul mul mn s 4
358ggpt 3 g
359ggu 8 ulong gz
360ggrun 14 9 ggcd mn 64 nz 1
361end
362
363note 19 COPY (x into y), 20 ZERO (x) over n elements (the FFN ReLU is fused into the GEMMs, gkope)
364fn ggew id uint n ulong x ulong y ulong to void
365ggu 0 n
366ggpt 1 x
367ggpt 2 y
368ggrun id 6 ggcd n 64 1 1
369end
370
371note RMSF/RMSB run one warp per row: arg 0 and the thread count are R*32
372fn ggrmsf v ulong g ulong y ulong s ulong to void
373r = mul ggr 32
374ggu 0 r
375ggpt 1 v
376ggpt 2 g
377ggpt 3 y
378ggpt 4 s
379ggrun 10 10 ggcd r 64 1 1
380end
381
382note dh += RMS backward of da; the gain terms go to WS
383fn ggrmsb v ulong g ulong s ulong to void
384r = mul ggr 32
385ggu 0 r
386ggpt 1 v
387ggpt 2 g
388ggpt 3 s
389ggpt 4 gga1 14
390ggpt 5 gga1 13
391ggpt 6 gga1 19
392ggrun 11 14 ggcd r 64 1 1
393end
394
395note gain gradient at parameter offset go += column sums of WS: per sequence (slot 13), then over B
396fn ggain go ulong to void
397d = get og 2
398tt = get og 1
399bb = get og 6
400ggu 0 d
401ggu 1 tt
402ggpt 1 gga1 19
403ggu 4 mul d 4
404ggu 5 mul mul tt d 4
405ggpt 3 gga1 20
406ggu 8 mul d 4
407ggrun 13 9 ggcd d 64 bb 1
408ggu 0 d
409ggu 1 bb
410ggpt 1 gga1 20
411ggu 4 mul d 4
412ggu 5 0
413ggpt 3 ggg go
414ggu 8 0
415ggrun 14 9 ggcd d 64 1 1
416end
417
418note softmax rows: one warp per row, T 32 threads per score block, grid (T/2, B H)
419fn ggsoft id uint p ulong dp ulong to void
420tt = get og 1
421ggu 0 mul tt 32
422ggpt 1 p
423ggpt 2 dp
424ggrun id 6 div tt 2 mul get og 6 get og 3 1
425end
426
427note attention GEMM over the B H (sequence, head) batch; operands set by gghd/ggpl/ggch/ggcp first;
428note cz: gkopz causal mode (1 A = P or DS, 2 A = P^T or DS^T, 3 C = S or DP)
429fn ggbh m ulong n ulong k ulong bt bool cz uint to void
430gkopz cz
431ggmm m n k mul get og 6 get og 3 get og 3 bt
432end
433
434note head-layout operand (rows of E inside D-wide rows, batch b then head); tr: k runs along rows
435fn gghd ab uint p ulong tr bool to void
436s0 = get ggs 0
437s1 = get ggs 1
438s2 = get ggs 2
439if tr
440gkopa ab p 4 s0 s1 s2
441else
442gkopa ab p s0 4 s1 s2
443end
444end
445
446note T x T score operand (batch b then head); tr: transposed
447fn ggpl ab uint p ulong tr bool to void
448s3 = get ggs 3
449s4 = get ggs 4
450s5 = get ggs 5
451if tr
452gkopa ab p 4 s3 s5 s4
453else
454gkopa ab p s3 4 s5 s4
455end
456end
457
458fn ggch c ulong al f64 to void
459s0 = get ggs 0
460s1 = get ggs 1
461s2 = get ggs 2
462gkopc c s0 4 s1 s2 f32 al
463end
464
465fn ggcp c ulong al f64 to void
466s3 = get ggs 3
467s4 = get ggs 4
468s5 = get ggs 5
469gkopc c s3 4 s5 s4 f32 al
470end
471
472note stride table for the dims in og (bytes)
473fn ggstr to void
474d = get og 2
475tt = get og 1
476put ggs 0 uint mul d 4
477put ggs 1 uint mul tt mul d 4
478put ggs 2 uint mul get og 7 4
479put ggs 3 uint mul tt 4
480put ggs 4 uint mul tt mul tt 4
481put ggs 5 uint mul get og 3 mul tt mul tt 4
482put ggs 6 uint mul ggr mul d 4
483put ggs 7 uint mul d mul d 4
484end
485
486note q, k, u = a wq, a wk, a wv as one batched GEMM (wq wk wv and q k u are contiguous)
487fn ggqkv l ulong to void
488d = get og 2
489a = ggl l 1
490w = ggw ovwq l
491c = ggl l 2
492s0 = get ggs 0
493s6 = get ggs 6
494s7 = get ggs 7
495gkopa 0 a s0 4 0 0
496gkopa 1 w 4 s0 0 s7
497gkopc c s0 4 0 s6 f32 1.0f64
498ggmm ggr d d 3 3 false
499end
500
501note P = softmax(q k^T / sqrt(E)), o = P u; causal: only S on and below the diagonal is formed
502note (gkmm cz 3) and o stops at the diagonal (cz 1), since P is exactly 0 above it
503fn ggatt l ulong to void
504tt = get og 1
505e = get og 7
506pp = ggl l 12
507q = ggl l 2
508k = ggl l 3
509gghd 0 q false
510gghd 1 k false
511isq = div 1.0f64 fsqrt f64 e
512ggcp pp isq
513ggbh tt tt e false 3
514ggsoft 15 pp pp
515u = ggl l 4
516o = ggl l 5
517ggpl 0 pp false
518gghd 1 u true
519ggch o 1.0f64
520ggbh tt e tt false 1
521end
522
523note hm = hin + o wo; bn = rms(hm) g2; out = hm + relu(bn w1) w2
524fn ggffn l ulong to void
525d = get og 2
526f = get og 4
527rd = mul ggr d
528hm = ggl l 6
529x = ggl l 0
530ggew 19 rd x hm
531x = ggl l 5
532w = ggw ovwo l
533ggxw x w hm d d true
534x = ggl l 7
535w = ggw ovg2 l
536s = ggl l 9
537ggrmsf hm w x s
538fp = ggl l 10
539w = ggw ovw1 l
540y = ggl l 11
541gkope 1 y
542ggxw x w fp f d false
543x = y
544o = ggout l
545ggew 19 rd hm o
546w = ggw ovw2 l
547ggxw x w o d f true
548end
549
550fn ggfl l ulong to void
551x = ggl l 0
552w = ggw ovg1 l
553a = ggl l 1
554s = ggl l 8
555ggrmsf x w a s
556ggqkv l
557ggatt l
558ggffn l
559end
560
561note ggnoce 1 (set by oglast only): ggfwd stops after the logits GEMM. CE (kernel 21) rewrites the
562note logits in place to (softmax - [c = y]) / (T B) for training, which keeps their argmax but is not a
563note logit (DRA-292 window gserve-w1: greedy matched, maxdiff 21.6, temperature draws drifted).
564global ggnoce uint 1
565fn ggfwd to void
566d = get og 2
567x = ggl 0 0
568ggu 0 d
569ggpt 1 x
570x = ggw 0
571ggpt 2 x
572x = ggw get og 30
573ggpt 3 x
574x = gga1 2
575ggpt 4 x
576tt = get og 1
577bb = get og 6
578ggrun 9 10 ggcd d 64 tt bb
579for l ulong get og 56 bound 9223372036854775807
580ggfl l
581end
582x = gga1 8
583w = ggw get og 33
584z = gga1 10
585s = gga1 9
586ggrmsf x w z s
587x = gga1 11
588w = ggw 0
589v = get og 0
590ggxwt z w x v d false
591if ne get ggnoce 0 0
592ret
593end
594r = mul ggr 32
595ggu 0 r
596ggpt 1 x
597x = gga1 3
598ggpt 2 x
599x = gga1 12
600ggpt 3 x
601ggrun 21 8 ggcd r 64 1 1
602end
603
604note layer l FFN backward: DH holds d out on entry, d hm on exit. The dF GEMM's fused epilogue (gkope 2)
605note applies the ReLU mask FP > 0; the forward W1 GEMM (ggffn, gkope 1) stores both FP and Y.
606fn ggbffn l ulong to void
607d = get og 2
608f = get og 4
609dh = gga1 13
610df = gga1 17
611da = gga1 14
612w = ggw ovw2 l
613x = ggl l 10
614gkope 2 x
615ggxwt dh w df f d false
616x = ggl l 11
617w = ggg ovw2 l
618if or eq OMUTLG 1 eq olp l l
619ggwg x dh w f d
620end
621x = ggl l 7
622w = ggg ovw1 l
623ggwg x df w d f
624w = ggw ovw1 l
625ggxwt df w da d f false
626x = ggl l 6
627w = ggw ovg2 l
628s = ggl l 9
629ggrmsb x w s
630ggain ovg2 l
631end
632
633note layer l attention backward: DO, DP, DU, DS (in DP), DQ, DK, then DA. Causal as in ggatt: DP
634note on and below the diagonal (cz 3, SOFTB reads dp above it as 0), DU and DK start at the diagonal
635note (cz 2), DQ stops there (cz 1).
636fn ggbatt l ulong to void
637d = get og 2
638tt = get og 1
639e = get og 7
640dh = gga1 13
641dn = gga1 16
642w = ggw ovwo l
643ggxwt dh w dn d d false
644x = ggl l 5
645w = ggg ovwo l
646ggwg x dh w d d
647dp = gga1 18
648x = ggl l 4
649gghd 0 dn false
650gghd 1 x false
651ggcp dp 1.0f64
652ggbh tt tt e false 3
653pp = ggl l 12
654dq = gga1 15
655dk = add dq ulong get ggs 6
656du = add dk ulong get ggs 6
657ggpl 0 pp true
658gghd 1 dn true
659ggch du 1.0f64
660ggbh tt e tt false 2
661ggsoft 16 pp dp
662x = ggl l 3
663ggpl 0 dp false
664gghd 1 x true
665ggch dq 1.0f64
666ggbh tt e tt false 1
667al = 1.0f64
668if eq OMUTG 0
669al = neg 1.0f64
670end
671x = ggl l 2
672ggpl 0 dp true
673gghd 1 x true
674ggch dk al
675ggbh tt e tt false 2
676end
677
678note DA = DQ wq^T + DK wk^T + DU wv^T; G wq wk wv += a^T (DQ DK DU) batched; then the rms backward
679fn ggbqkv l ulong to void
680d = get og 2
681da = gga1 14
682dq = gga1 15
683dk = add dq ulong get ggs 6
684du = add dk ulong get ggs 6
685w = ggw ovwq l
686ggxwt dq w da d d false
687w = ggw ovwk l
688ggxwt dk w da d d true
689w = ggw ovwv l
690ggxwt du w da d d true
691a = ggl l 1
692w = ggg ovwq l
693s0 = get ggs 0
694s6 = get ggs 6
695s7 = get ggs 7
696k = ggks d d 3
697if eq k 1
698gkopa 0 a 4 s0 0 0
699gkopa 1 dq 4 s0 0 s6
700gkopc w s0 4 0 s7 f32 1.0f64
701ggmm d d ggr 3 3 true
702else
703sl = uint div ggr k
704gkopa 0 a 4 s0 0 mul sl s0
705gkopa 1 dq 4 s0 s6 mul sl s0
706ggsk d d 3 k w s7
707end
708x = ggl l 0
709w = ggw ovg1 l
710s = ggl l 8
711ggrmsb x w s
712ggain ovg1 l
713end
714
715fn ggbl l ulong to void
716ggbffn l
717ggbatt l
718ggbqkv l
719end
720
721fn ggbwd to void
722d = get og 2
723v = get og 0
724tt = get og 1
725r = ggr
726ggew 20 ggp5 gga1 1 gga1 1
727ggew 20 mul r d gga1 13 gga1 13
728ggxw gga1 11 ggw 0 gga1 14 d v false
729ggwg gga1 11 gga1 10 ggg 0 v d
730ggrmsb gga1 8 ggw get og 33 gga1 9
731ggain get og 33
732l = get og 56
733while gt l 0 bound 9223372036854775807
734l = sub l 1
735ggbl l
736end
737td = mul tt d
738ggu 0 td
739ggu 1 get og 6
740ggpt 1 gga1 13
741ggu 4 mul td 4
742ggu 5 0
743ggpt 3 ggg get og 30
744ggu 8 0
745ggrun 14 9 ggcd td 64 1 1
746ggu 0 d
747ggpt 1 gga1 13
748ggpt 2 gga1 4
749ggpt 3 gga1 5
750ggpt 4 ggg 0
751ggrun 22 10 ggcd d 64 v 1
752end
753
754note Mutant f: 1 in the product, 0 in the mutant, which uploads layer 0's W1 times 1 + 2^-10 (a
755note forward error on the chip that gcmp2's FP check must name).
756const OMUTF ulong 1
757
758note W[i] as f32 for i in chunk q of P (a loop runs at most 999999 passes)
759fn ggupc q ulong p ulong to void
760h = get gbmem 5
761wo = get gga 0
762i0 = mul q OCH
763for j ulong sub oce q p i0
764i = add i0 j
765x = bitcast uint f32 ggupv i
766m = mapset h add wo mul i 4 4 ulong x
767end
768end
769
770note the uploaded value of W[i]: the parameter at virtual index i in the product
771fn ggupv i ulong to f64
772w = owg i
773if eq OMUTF 0
774a = opw1 0
775if and ge i a lt i add a mul get og 2 get og 4
776w = mul w 1.0009765625f64
777end
778end
779ret w
780end
781
782note upload the f32 parameters
783fn ggupw to void
784p = get og 8
785for q ulong ocn p
786ggupc q p
787end
788put gkinv 0 1
789end
790
791note upload the batch tokens and the CSR of x (rows sorted by token)
792fn ggupb to void
793ggupt
794ggcsr
795put gkinv 0 1
796end
797
798note n values of the Adam pool from s as f32 to arena byte offset o + 4 j, and back (one chunk of an
799note Adam moment)
800fn ggupmc s ulong o ulong n ulong to void
801h = get gbmem 5
802for j ulong n
803x = bitcast uint f32 omvg add s j
804m = mapset h add o mul j 4 4 ulong x
805end
806end
807
808fn ggdnmc s ulong o ulong n ulong to void
809h = get gbmem 5
810for j ulong n
811x = uint mapget h add o mul j 4 4
812omvp add s j f64 bitcast f32 x
813end
814end
815
816note chip parameters of chunk q back into the parameter banks as f64
817fn ggdnwc q ulong p ulong to void
818h = get gbmem 5
819o = get gga 0
820i0 = mul q OCH
821for j ulong sub oce q p i0
822i = add i0 j
823x = uint mapget h add o mul i 4 4
824owp i f64 bitcast f32 x
825end
826end
827
828note optimizer state (parameters, Adam pool) to the chip as f32 (m in gga 21, v in gga 22), and back
829fn ggupst to void
830ggupw
831p = get og 8
832for q ulong ocn p
833i0 = mul q OCH
834n = sub oce q p i0
835ggupmc add get og 11 i0 add get gga 21 mul i0 4 n
836ggupmc add get og 12 i0 add get gga 22 mul i0 4 n
837end
838put gkinv 0 1
839end
840
841fn ggdnst to void
842p = get og 8
843for q ulong ocn p
844ggdnwc q p
845i0 = mul q OCH
846n = sub oce q p i0
847ggdnmc add get og 11 i0 add get gga 21 mul i0 4 n
848ggdnmc add get og 12 i0 add get gga 22 mul i0 4 n
849end
850end
851
852fn ggupt to void
853h = get gbmem 5
854xo = get gga 2
855yo = get gga 3
856for i ulong ggr
857tk = get obx i
858m = mapset h add xo mul i 4 4 tk
859ty = get oby i
860m = mapset h add yo mul i 4 4 ty
861end
862end
863
864note counting sort of the R rows by input token: start (V + 1) and order (R) arrays
865fn ggcsr to void
866h = get gbmem 5
867v = get og 0
868for c ulong add v 1
869put ggcn c 0
870end
871for i ulong ggr
872c1 = add get obx i 1
873n = add get ggcn c1 1
874put ggcn c1 n
875end
876for c ulong v
877c1 = add c 1
878n = add get ggcn c1 get ggcn c
879put ggcn c1 n
880end
881so = get gga 4
882for c ulong add v 1
883n = get ggcn c
884m = mapset h add so mul c 4 4 n
885end
886so = get gga 5
887for i ulong ggr
888c = get obx i
889p = get ggcn c
890m = mapset h add so mul p 4 4 i
891put ggcn c add p 1
892end
893end
894
895note GRD[i] = G[i] for i in chunk q of P
896fn ggdnc q ulong p ulong to void
897h = get gbmem 5
898go = get gga 1
899i0 = mul q OCH
900for j ulong sub oce q p i0
901i = add i0 j
902x = uint mapget h add go mul i 4 4
903ocsp i f64 bitcast f32 x
904end
905end
906
907fn ggdown to void
908p = get og 8
909for q ulong ocn p
910ggdnc q p
911end
912end
913
914note one f32 at arena byte offset o, as f64
915fn ggf o ulong to f64
916x = uint mapget get gbmem 5 o 4
917ret f64 bitcast f32 x
918end
919
920note row loss from the CE stats (m, sum, l_y) of row r: log(sum) + m - l_y
921fn ggrl r ulong to f64
922o = add get gga 12 mul r 12
923m = ggf o
924sm = ggf add o 4
925ly = ggf add o 8
926ret sub add flog sm m ly
927end
928
929fn ggloss to f64
930tt = get og 1
931for b ulong get og 6
932s = 0.0f64
933for t ulong tt
934s = add s ggrl add mul b tt t
935end
936put olos b div s f64 tt
937end
938ret olsum
939end
940
941note the step loss and gradient for the batch in obx/oby; ggopen first. full: upload the f32
942note parameters first and download the gradient into ogr[0..P) after; otherwise both stay on chip.
943note phase wall times (ns): 0 upload, 1 forward, 2 backward, 3 download, 4 loss, 5 on-chip Adam
944global ggtm long 6
945fn ggpass full bool to f64
946t = monons
947if full
948ggupw
949end
950ggupb
951u = monons
952put ggtm 0 sub u t
953ggfwd
954t = monons
955put ggtm 1 sub t u
956ggbwd
957u = monons
958put ggtm 2 sub u t
959if full
960ggdown
961end
962t = monons
963put ggtm 3 sub t u
964l = ggloss
965u = monons
966put ggtm 4 sub u t
967ret l
968end
969
970fn gggrad to f64
971ret ggpass true
972end
973
974note clip and Adam on chip at step og[41], the f32 image of oadam: SQN partial sums of g^2, the
975note norm, clip factor, schedule and bias corrections on the host in f64, then ADAM over P
976fn ggadam to void
977p = get og 8
978nc = div ggp5 512
979ggu 0 nc
980ggpt 1 gga1 1
981ggpt 2 gga1 23
982ggrun 23 6 ggcd nc 64 1 1
983h = get gbmem 5
984so = get gga 23
985ss = 0.0f64
986for c ulong nc
987x = uint mapget h add so mul c 4 4
988ss = add ss f64 bitcast f32 x
989end
990nr = fsqrt ss
991cl = 1.0f64
992if gt nr 1.0
993cl = div 1.0 nr
994end
995s = get og 41
996lrs = olrs s bitcast f64 get og 43
997bc1 = sub 1.0 opowb 0.9 s
998bc2 = sub 1.0 opowb 0.99 s
999if eq OMUTC 0
1000bc1 = 1.0f64
1001bc2 = 1.0f64
1002end
1003ggu 0 p
1004ggpt 1 gga1 0
1005ggpt 2 gga1 21
1006ggpt 3 gga1 22
1007ggpt 4 gga1 1
1008ggu 10 ulong grf cl
1009ggu 11 ulong grf div lrs bc1
1010ggu 12 ulong grf div 1.0f64 bc2
1011ggrun 24 13 ggcd p 64 1 1
1012end
1013
1014fn ggtms to void
1015oputc 71
1016oputc 84
1017for i ulong 6
1018oputc 32
1019oputl get ggtm i
1020end
1021for i ulong 4
1022oputc 32
1023oputl get gkt i
1024end
1025oputc 10
1026end
1027
1028note keep the CPU gradient: BLK[i] = GRD[i] (chunked: a loop runs at most 999999 passes)
1029fn ggkeep p ulong to void
1030for q ulong ocn p
1031i0 = mul q OCH
1032for j ulong sub oce q p i0
1033i = add i0 j
1034ogrp add p i ocsg i
1035end
1036end
1037end
1038
1039note ggcm[0] = max |GRD[i] - BLK[i]|, ggcm[1] = max |BLK[i]| over i < P
1040global ggcm f64 2
1041fn ggmaxd p ulong to void
1042put ggcm 0 0.0f64
1043put ggcm 1 0.0f64
1044for q ulong ocn p
1045i0 = mul q OCH
1046for j ulong sub oce q p i0
1047i = add i0 j
1048rf = ogrg add p i
1049dd = sub ocsg i rf
1050if lt dd 0.0f64
1051dd = neg dd
1052end
1053if lt rf 0.0f64
1054rf = neg rf
1055end
1056if gt dd get ggcm 0
1057put ggcm 0 dd
1058end
1059if gt rf get ggcm 1
1060put ggcm 1 rf
1061end
1062end
1063end
1064end
1065
1066note gcmp CK DATA S: CPU ograd and GPU gradient for batch S; prints
1067note `GC P maxabs_e12 maxref_e12 rel_e9 cpu_loss_micro gpu_loss_micro gpu_ns tol_e9` and GCMP PASS
1068note (rel < tol, |loss difference| < 1e-5) or GCMP FAIL (exit 45). tol = R 2^-24 is the first-order
1069note bound gamma_R on the f32 error of a sum of R = B T rows, the longest reduction of the weight
1070note gradients, relative to the sum of magnitudes; the CPU reference is f64. ReLU units whose f64
1071note pre-activation lies within f32 resolution of zero can take the other side on the GPU (receipt:
1072note evidence/OMEGALLM/gpu/README.md), and both sides are valid subgradients.
1073fn ggcmp to void
1074if ne oargc 5
1075exit 2
1076end
1077ockloop 2
1078odopen 3
1079obatch oargu 4
1080put og 10 0
1081lc = ograd
1082p = get og 8
1083ggkeep p
1084ggopen
1085t0 = monons
1086lg = gggrad
1087t1 = monons
1088ggmaxd p
1089mx = get ggcm 0
1090mr = get ggcm 1
1091rel = div mx mr
1092dl = sub lg lc
1093if lt dl 0.0f64
1094dl = neg dl
1095end
1096oputc 71
1097oputc 67
1098oputc 32
1099oputu p
1100oputc 32
1101oputl rmic mul mx 1000000.0f64
1102oputc 32
1103oputl rmic mul mr 1000000.0f64
1104oputc 32
1105oputl rmic mul rel 1000.0f64
1106oputc 32
1107oputl rmic lc
1108oputc 32
1109oputl rmic lg
1110oputc 32
1111oputl sub t1 t0
1112oputc 32
1113tol = div f64 ggr 16777216.0f64
1114oputl rmic mul tol 1000.0f64
1115oputc 10
1116ggtms
1117ok = and lt rel tol lt dl 0.00001f64
1118r = nvstop
1119oputc 71
1120oputc 67
1121oputc 77
1122oputc 80
1123oputc 32
1124if ok
1125oputc 80
1126oputc 65
1127oputc 83
1128oputc 83
1129oputc 10
1130oflush
1131ret
1132end
1133oputc 70
1134oputc 65
1135oputc 73
1136oputc 76
1137oputc 10
1138oflush
1139exit 45
1140end
1141
1142note true when x is finite (every comparison with NaN is false)
1143fn ggfin x f64 to bool
1144a = x
1145if lt a 0.0f64
1146a = neg a
1147end
1148ret le a bitcast f64 ulong 9218868437227405311
1149end
1150
1151note x for printing: 1e6 when x is not finite or above 1e6 (rmic traps past the long range)
1152fn ggclp x f64 to f64
1153if ggfin x
1154if lt x 1000000.0f64
1155ret x
1156end
1157end
1158ret 1000000.0f64
1159end
1160
1161note gcmp2 state. ggfs[j] = s_j for the row in flight (F <= 1536, CONTRACT-V2). ggk2: [0] ReLU
1162note flips (sign of FP_chip differs from FP_ref), [1] non-finite FP values (chip or reference),
1163note [2] layer of the largest rho, [3] non-finite gradient values (chip, K0 or K1).
1164note ggk3: [0] rho max, [1] max |FP_ref| at a flip, [2] max |FP_chip - FP_ref|.
1165global ggfs f64 1536
1166global ggk2 ulong 4
1167global ggk3 f64 3
1168
1169note s_j = sum_k |bn_k| |W1[k F + j]| for row t of layer l (k < D ascending), the magnitude sum of
1170note the FP dot product the chip rounds (layer l is staged: ogmask runs inside olayb)
1171fn ggsj l ulong t ulong to void
1172d = get og 2
1173f = get og 4
1174bn = add oabn l mul t d
1175w = opx ovw1 l
1176for j ulong f
1177put ggfs j 0.0f64
1178end
1179for k ulong d
1180a = get oa add bn k
1181if lt a 0.0f64
1182a = neg a
1183end
1184ggsjk a add w mul k f f
1185end
1186end
1187
1188fn ggsjk a f64 wo ulong f ulong to void
1189for j ulong f
1190x = get ow add wo j
1191if lt x 0.0f64
1192x = neg x
1193end
1194put ggfs j add get ggfs j mul a x
1195end
1196end
1197
1198note model.om's ReLU mask hook (ogkm[0] = 1, gcmp2 only): for row t of layer l of the sequence in
1199note flight, check every unit's reference FP against the chip's FP (left in the arena by gggrad),
1200note then zero dF where the chip's FP is not > 0, the chip's fused dF mask (gkope 2; NaN included).
1201fn ogmask yo ulong l ulong t ulong to void
1202f = get og 4
1203xo = add oafp l mul t f
1204ggsj l t
1205row = add mul get ogkm 1 get og 1 t
1206c0 = add sub ggl l 10 get gga 40 mul mul row f 4
1207for j ulong f
1208c = ggf add c0 mul j 4
1209ggk1 get oa add xo j c j l
1210if not gt c 0.0f64
1211put oa add yo j 0.0f64
1212end
1213end
1214end
1215
1216note one unit: non-finite count, |FP_chip - FP_ref|, rho = that over s_j (inf when s_j = 0 and the
1217note values differ), and the flip count
1218fn ggk1 v f64 c f64 j ulong l ulong to void
1219if not and ggfin v ggfin c
1220put ggk2 1 add get ggk2 1 1
1221ret
1222end
1223dd = sub c v
1224if lt dd 0.0f64
1225dd = neg dd
1226end
1227if gt dd get ggk3 2
1228put ggk3 2 dd
1229end
1230ggkr dd get ggfs j l
1231ggkfl v c
1232end
1233
1234fn ggkr dd f64 s f64 l ulong to void
1235if not gt dd 0.0f64
1236ret
1237end
1238rho = bitcast f64 ulong 9218868437227405312
1239if gt s 0.0f64
1240rho = div dd s
1241end
1242if gt rho get ggk3 0
1243put ggk3 0 rho
1244put ggk2 2 l
1245end
1246end
1247
1248fn ggkfl v f64 c f64 to void
1249a = gt v 0.0f64
1250b = gt c 0.0f64
1251if eq a b
1252ret
1253end
1254put ggk2 0 add get ggk2 0 1
1255if lt v 0.0f64
1256v = neg v
1257end
1258if gt v get ggk3 1
1259put ggk3 1 v
1260end
1261end
1262
1263note Adam pool [o + i] = GRD[i] for i < P (the chip gradient at o = 0, K0 at o = P; gcmp2 has no
1264note Adam step)
1265fn ggstash p ulong o ulong to void
1266for q ulong ocn p
1267ggstq q p o
1268end
1269end
1270
1271fn ggstq q ulong p ulong o ulong to void
1272i0 = mul q OCH
1273for j ulong sub oce q p i0
1274i = add i0 j
1275omvp add o i ocsg i
1276end
1277end
1278
1279note ggc2: [0] max |G_chip - K0|, [1] max |K0|, [2] max |G_chip - K1| over i < P, with the chip
1280note gradient in the Adam pool [0, P), K0 in [P, 2P) and K1 in GRD; non-finite values count in ggk2[3]
1281global ggc2 f64 3
1282fn gg2cmp p ulong to void
1283for k ulong 3
1284put ggc2 k 0.0f64
1285end
1286for q ulong ocn p
1287i0 = mul q OCH
1288for j ulong sub oce q p i0
1289gg2c1 add i0 j p
1290end
1291end
1292end
1293
1294fn gg2c1 i ulong p ulong to void
1295c = omvg i
1296r0 = omvg add p i
1297r1 = ocsg i
1298if not and and ggfin c ggfin r0 ggfin r1
1299put ggk2 3 add get ggk2 3 1
1300ret
1301end
1302gg2mx 0 sub c r0
1303gg2mx 1 r0
1304gg2mx 2 sub c r1
1305end
1306
1307fn gg2mx k ulong x f64 to void
1308if lt x 0.0f64
1309x = neg x
1310end
1311if gt x get ggc2 k
1312put ggc2 k x
1313end
1314end
1315
1316note gcmp2 CK DATA S: branch-conditioned parity (evidence/OMEGALLM/gpu-max/README.md). The chip
1317note gradient runs first and stays in the Adam pool; K0 is the CPU ograd with its own ReLU mask (gcmp's
1318note reference), K1 the CPU ograd with the chip's FP sign as the ReLU mask (ogmask), which also checks
1319note every FFN pre-activation. Prints `GC2 P rel0_e9 rel1_e9 tolr_e9 rho_e9 tolf_e9 rho_layer flips
1320note vflip_e12 fpdiff_e12 nonfinite cpu_loss_micro gpu_loss_micro gpu_ns fails` and GCMP2 PASS, or
1321note GCMP2 FAIL (exit 47). rel0 = max|G_chip - K0| / max|K0| (gcmp's rel), rel1 = max|G_chip - K1| /
1322note max|K0|; rho = max |FP_chip - FP_ref| / s_j. fails is a bit set: 1 a non-finite FP or gradient
1323note value, 2 rho >= tolf = max(D, F, T) 2^-24, 4 rel1 >= tolr = R 2^-24 (gcmp's gamma_R), 8 |chip
1324note loss - K0 loss| >= 1e-5.
1325fn ggcmp2 to void
1326if ne oargc 5
1327exit 2
1328end
1329ockloop 2
1330odopen 3
1331obatch oargu 4
1332p = get og 8
1333ggopen
1334t0 = monons
1335lg = gggrad
1336t1 = monons
1337ggstash p 0
1338put og 10 0
1339put ogkm 0 0
1340l0 = ograd
1341ggstash p p
1342for k ulong 4
1343put ggk2 k 0
1344end
1345for k ulong 3
1346put ggk3 k 0.0f64
1347end
1348put ogkm 0 1
1349put og 10 0
1350l1 = ograd
1351put ogkm 0 0
1352gg2cmp p
1353ggc2pr lg l0 sub t1 t0
1354end
1355
1356note the GC2 line and verdict of gcmp2 (lg chip loss, l0 K0 loss, ns chip gradient time); gbcmp2
1357note (gkbf 1) takes gb.om's verdict
1358fn ggc2pr lg f64 l0 f64 ns long to void
1359fl = ggc2fl lg l0
1360ggc2ln lg l0 ns fl
1361ggtms
1362r = nvstop
1363if eq get gkbf 0 1
1364gbc2vd lg l0 fl
1365ret
1366end
1367ggc2vd eq fl 0
1368end
1369
1370note ggc2[k] / max |K0|, clamped for printing
1371fn gg2rl k ulong to f64
1372ret ggclp div get ggc2 k get ggc2 1
1373end
1374
1375note tolf = max(D, F, T) 2^-24
1376fn gg2tf to f64
1377n = fmax f64 get og 2 fmax f64 get og 4 f64 get og 1
1378ret div n 16777216.0f64
1379end
1380
1381note tolr = R 2^-24 (gcmp's gamma_R)
1382fn gg2tr to f64
1383ret div f64 ggr 16777216.0f64
1384end
1385
1386note the gcmp2 fail bits (see ggcmp2); gb.om's BF16 bits under gkbf 1
1387fn ggc2fl lg f64 l0 f64 to ulong
1388if eq get gkbf 0 1
1389ret gbc2fl lg l0 get ggk3 0 add get ggk2 1 get ggk2 3
1390end
1391dl = sub lg l0
1392if lt dl 0.0f64
1393dl = neg dl
1394end
1395fl = ulong 0
1396if ne add get ggk2 1 get ggk2 3 0
1397fl = or fl 1
1398end
1399if not lt ggclp get ggk3 0 gg2tf
1400fl = or fl 2
1401end
1402if not lt gg2rl 2 gg2tr
1403fl = or fl 4
1404end
1405if not lt dl 0.00001f64
1406fl = or fl 8
1407end
1408ret fl
1409end
1410
1411fn ggc2ln lg f64 l0 f64 ns long fl ulong to void
1412oputc 71
1413oputc 67
1414oputc 50
1415oputc 32
1416oputu get og 8
1417gg2pe gg2rl 0
1418gg2pe gg2rl 2
1419gg2pe gg2tr
1420gg2pe ggclp get ggk3 0
1421gg2pe gg2tf
1422oputc 32
1423oputu get ggk2 2
1424oputc 32
1425oputu get ggk2 0
1426oputc 32
1427oputl rmic mul ggclp get ggk3 1 1000000.0f64
1428oputc 32
1429oputl rmic mul ggclp get ggk3 2 1000000.0f64
1430oputc 32
1431oputu add get ggk2 1 get ggk2 3
1432oputc 32
1433oputl rmic ggclp l0
1434oputc 32
1435oputl rmic ggclp lg
1436oputc 32
1437oputl ns
1438oputc 32
1439oputu fl
1440oputc 10
1441end
1442
1443note " x_e9"
1444fn gg2pe x f64 to void
1445oputc 32
1446oputl rmic mul x 1000.0f64
1447end
1448
1449fn ggc2vd ok bool to void
1450oputc 71
1451oputc 67
1452oputc 77
1453oputc 80
1454oputc 50
1455oputc 32
1456if ok
1457oputc 80
1458oputc 65
1459oputc 83
1460oputc 83
1461oputc 10
1462oflush
1463ret
1464end
1465oputc 70
1466oputc 65
1467oputc 73
1468oputc 76
1469oputc 10
1470oflush
1471exit 47
1472end
1473
1474note gtrain CK TRAIN UNTIL EVERY: cmdtrain with the gradient from the GPU (K = 1, no workers); prints
1475note `T s+1 loss_micro` at save points like train
1476fn ggtrain to void
1477if ne oargc 6
1478exit 2
1479end
1480ockloop 2
1481odopen 3
1482un = oargu 4
1483ev = oargu 5
1484if eq ev 0
1485exit 2
1486end
1487en = get og 42
1488if lt un en
1489en = un
1490end
1491ggopen
1492while lt get og 41 en bound 999999
1493s = get og 41
1494obatch s
1495put og 10 0
1496ls = gggrad
1497oadam
1498s1 = add s 1
1499put og 41 s1
1500if or eq rem s1 ev 0 eq s1 en
1501ocksave 2
1502oputc 84
1503oputc 32
1504oputu s1
1505oputc 32
1506oputl rmic ls
1507oputc 10
1508oflush
1509end
1510end
1511r = nvstop
1512end
1513
1514note BLK[i] = the f64 parameter i before the step
1515fn ggkw q ulong p ulong to void
1516i0 = mul q OCH
1517for j ulong sub oce q p i0
1518i = add i0 j
1519ogrp add p i owg i
1520end
1521end
1522
1523note GRD[i] = chip parameter i as f64
1524fn ggdw q ulong p ulong to void
1525h = get gbmem 5
1526wo = get gga 0
1527i0 = mul q OCH
1528for j ulong sub oce q p i0
1529i = add i0 j
1530x = uint mapget h add wo mul i 4 4
1531ocsp i f64 bitcast f32 x
1532end
1533end
1534
1535note over i < P with w0 = BLK[i], c = parameter i (CPU f64 result), g = GRD[i] (chip f32 result):
1536note u = c - w0, d = (g - f32(w0)) - u; ggam 0 max |d|, 1 max |u|, 2 max (|d| - 2^-24 |c|), the part
1537note of d above the half-ulp rounding of the f32 master weight
1538global ggam f64 3
1539fn ggad1 i ulong p ulong to void
1540w0 = ogrg add p i
1541c = owg i
1542u = sub c w0
1543g = sub ocsg i f64 f32 w0
1544d = fabs sub g u
1545u = fabs u
1546e = sub d mul fabs c 0.000000059604644775390625f64
1547if gt d get ggam 0
1548put ggam 0 d
1549end
1550if gt u get ggam 1
1551put ggam 1 u
1552end
1553if gt e get ggam 2
1554put ggam 2 e
1555end
1556end
1557
1558fn ggadc q ulong p ulong to void
1559i0 = mul q OCH
1560for j ulong sub oce q p i0
1561ggad1 add i0 j p
1562end
1563end
1564
1565note gacmp CK DATA S: one step from CK on batch S with the chip gradient, Adam on the CPU (oadam, f64)
1566note and on the chip (ggadam, f32); prints `GA P maxd_e12 maxu_e12 rel_e9 resid_e9 tol_e9 adam_ns
1567note loss_micro` and GACMP PASS (resid = max(|d| - 2^-24 |c|) / max |u| < tol = 2^-16) or GACMP FAIL
1568note (exit 46)
1569fn ggacmp to void
1570if ne oargc 5
1571exit 2
1572end
1573ockloop 2
1574odopen 3
1575obatch oargu 4
1576put og 10 0
1577p = get og 8
1578ggopen
1579ggupst
1580lg = ggpass true
1581for q ulong ocn p
1582ggkw q p
1583end
1584oadam
1585t0 = monons
1586ggadam
1587t1 = monons
1588for q2 ulong ocn p
1589ggdw q2 p
1590end
1591for k ulong 3
1592put ggam k 0.0f64
1593end
1594for q3 ulong ocn p
1595ggadc q3 p
1596end
1597mu = get ggam 1
1598rel = div get ggam 0 mu
1599res = div get ggam 2 mu
1600tol = div 1.0f64 65536.0f64
1601oputc 71
1602oputc 65
1603oputc 32
1604oputu p
1605oputc 32
1606oputl rmic mul get ggam 0 1000000.0f64
1607oputc 32
1608oputl rmic mul mu 1000000.0f64
1609oputc 32
1610oputl rmic mul rel 1000.0f64
1611oputc 32
1612oputl rmic mul res 1000.0f64
1613oputc 32
1614oputl rmic mul tol 1000.0f64
1615oputc 32
1616oputl sub t1 t0
1617oputc 32
1618oputl rmic lg
1619oputc 10
1620r = nvstop
1621oputc 71
1622oputc 65
1623oputc 67
1624oputc 77
1625oputc 80
1626oputc 32
1627if lt res tol
1628oputc 80
1629oputc 65
1630oputc 83
1631oputc 83
1632oputc 10
1633oflush
1634ret
1635end
1636oputc 70
1637oputc 65
1638oputc 73
1639oputc 76
1640oputc 10
1641oflush
1642exit 46
1643end
1644
1645note gtrainf CK TRAIN UNTIL EVERY: gtrain with the parameters and Adam state resident on the chip in
1646note f32 (ggadam); only the batch goes up each step. At save points the state comes back to f64 for
1647note ocksave and `T s+1 loss_micro` is printed; GT at the end has the last step's phase times.
1648fn ggtrainf to void
1649if ne oargc 6
1650exit 2
1651end
1652ockloop 2
1653odopen 3
1654un = oargu 4
1655ev = oargu 5
1656if eq ev 0
1657exit 2
1658end
1659en = get og 42
1660if lt un en
1661en = un
1662end
1663ggopen
1664ggupst
1665while lt get og 41 en bound 999999
1666s = get og 41
1667obatch s
1668put og 10 0
1669ls = ggpass false
1670t = monons
1671ggadam
1672put ggtm 5 sub monons t
1673s1 = add s 1
1674put og 41 s1
1675if or eq rem s1 ev 0 eq s1 en
1676ggdnst
1677ocksave 2
1678oputc 84
1679oputc 32
1680oputu s1
1681oputc 32
1682oputl rmic ls
1683oputc 10
1684oflush
1685end
1686end
1687ggtms
1688oflush
1689r = nvstop
1690end
1691
1692note gprof CK TRAIN N: N gtrainf steps from CK, nothing saved. The last step runs with the launch
1693note profile on (gk.om gkpf), then 64 ZERO launches of 64 floats measure the bare launch round trip.
1694note Prints `T steps loss_micro`, one `GP id gx gy gz m n k wait_ns` per profiled launch, then GT.
1695fn ggprof to void
1696if ne oargc 5
1697exit 2
1698end
1699ockloop 2
1700odopen 3
1701nn = oargu 4
1702if eq nn 0
1703exit 2
1704end
1705ggopen
1706ggupst
1707ls = 0.0f64
1708for i ulong nn
1709s = get og 41
1710obatch s
1711put og 10 0
1712if eq add i 1 nn
1713put gkpf 0 1
1714end
1715ls = ggpass false
1716t = monons
1717ggadam
1718put ggtm 5 sub monons t
1719put og 41 add s 1
1720end
1721for i ulong 64
1722ggew 20 64 ggg 0 ggg 0
1723end
1724put gkpf 0 0
1725oputc 84
1726oputc 32
1727oputu get og 41
1728oputc 32
1729oputl rmic ls
1730oputc 10
1731for q ulong get gkpc 0
1732o = mul q 8
1733oputc 71
1734oputc 80
1735for j ulong 8
1736oputc 32
1737oputu get gkpl add o j
1738end
1739oputc 10
1740end
1741ggtms
1742oflush
1743r = nvstop
1744end
1745
1746note geval CK HELD W [MERGES]: eval (cli.om cmdeval) with the forward pass on the chip in f32. Windows
1747note w < W at offsets w T, B per forward (og 6); the last set pads its spare rows with window 0 and
1748note does not count them. Each window adds its mean row loss in window order and, when V > 256, the
1749note decoded length of its T targets, as cmdeval does. Prints `E step W loss_micro bpb_micro` like
1750note eval, then `GE gpu_ns` (upload, forwards and loss reads). Exits 2 and 23 as eval.
1751global ggev f64 1
1752global ggen ulong 1
1753fn ggeval to void
1754ww = ggevw
1755t = monons
1756ggopen
1757ggupw
1758put ggev 0 0.0f64
1759put ggen 0 0
1760bb = get og 6
1761nc = div add ww sub bb 1 bb
1762for c ulong nc
1763ggevc c ww
1764end
1765ns = sub monons t
1766ggevpr ww
1767oputc 71
1768oputc 69
1769oputc 32
1770oputl ns
1771oputc 10
1772oflush
1773r = nvstop
1774end
1775
1776note eval's argument checks; loads CK, MERGES and HELD and returns W
1777fn ggevw to ulong
1778na = oargc
1779if and ne na 5 ne na 6
1780exit 2
1781end
1782ockloop 2
1783ww = oargu 4
1784v = get og 0
1785if gt v 256
1786if eq na 5
1787exit 2
1788end
1789obload 5 v
1790end
1791odopen 3
1792tt = get og 1
1793m = get odn 0
1794if eq ww 0
1795exit 23
1796end
1797if gt ww div sub m 1 tt
1798exit 23
1799end
1800ret ww
1801end
1802
1803note window set c: rows b < B hold window c B + b (window 0 past W), one chip forward, then the sums
1804fn ggevc c ulong ww ulong to void
1805bb = get og 6
1806tt = get og 1
1807for b ulong bb
1808w = add mul c bb b
1809if ge w ww
1810w = 0
1811end
1812obrow b mul w tt
1813end
1814ggupb
1815ggfwd
1816for b ulong bb
1817w = add mul c bb b
1818if lt w ww
1819ggevr b
1820end
1821end
1822end
1823
1824note add row b's mean loss (CE stats left by ggfwd) and, when V > 256, its decoded target bytes
1825fn ggevr b ulong to void
1826tt = get og 1
1827o = mul b tt
1828s = 0.0f64
1829for t ulong tt
1830s = add s ggrl add o t
1831end
1832put ggev 0 add get ggev 0 div s f64 tt
1833if gt get og 0 256
1834n = get ggen 0
1835for t ulong tt
1836n = add n obtlen get oby add o t
1837end
1838put ggen 0 n
1839end
1840end
1841
1842note the E line of cmdeval from the sums
1843fn ggevpr ww ulong to void
1844lo = div get ggev 0 f64 ww
1845bp = div lo 0.6931471805599453
1846if gt get og 0 256
1847tt = get og 1
1848bp = div mul bp f64 mul ww tt f64 get ggen 0
1849end
1850oputc 69
1851oputc 32
1852oputu get og 41
1853oputc 32
1854oputu ww
1855oputc 32
1856oputl rmic lo
1857oputc 32
1858oputl rmic bp
1859oputc 10
1860end
1861
1862note gdiag CK DATA S: the CE kernel's per-row stats (max m, sum of exp(l - m), logit l_y) on the chip
1863note in f32 against the CPU's f64 values for the same forward of batch S. Prints R, then the mean
1864note 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).
1865note It located the CE sum bias that the Kahan sum in gr.om grce removes.
1866global gdacc f64 6
1867global gdcpu f64 3
1868fn ggdiag to void
1869if ne oargc 5
1870exit 2
1871end
1872ockloop 2
1873odopen 3
1874obatch oargu 4
1875put og 10 0
1876ggopen
1877ggupw
1878ggupb
1879ggfwd
1880for i ulong 6
1881put gdacc i 0.0f64
1882end
1883for b ulong get og 6
1884x = ofwd b
1885for t ulong get og 1
1886ggdrow b t
1887end
1888end
1889n = f64 ggr
1890oputu ggr
1891for i ulong 6
1892oputc 32
1893oputl rmic mul div get gdacc i n 1000.0f64
1894end
1895oputc 10
1896oflush
1897r = nvstop
1898end
1899
1900fn ggdrow b ulong t ulong to void
1901r = add mul b get og 1 t
1902ggdcpu t get oby r
1903ggdacc r
1904put gdacc 4 add get gdacc 4 sub ggdps r 1.0f64
1905end
1906
1907note CPU f64 stats of row t of the last ofwd: max logit, log sum exp(l - max), logit of y
1908fn ggdcpu t ulong y ulong to void
1909d = get og 2
1910v = get og 0
1911pr = add get og 18 mul t v
1912klogit add get og 17 mul t d pr v d
1913m = bitcast f64 ulong 18442240474082181120
1914for j ulong v
1915m = fmax m get oa add pr j
1916end
1917s = 0.0f64
1918for k ulong v
1919s = add s fexp sub get oa add pr k m
1920end
1921put gdcpu 0 m
1922put gdcpu 1 flog s
1923put gdcpu 2 get oa add pr y
1924end
1925
1926fn ggdacc r ulong to void
1927o = add get gga 12 mul r 12
1928mg = sub ggf o get gdcpu 0
1929ls = sub flog ggf add o 4 get gdcpu 1
1930lg = sub ggf add o 8 get gdcpu 2
1931put gdacc 0 add get gdacc 0 mg
1932put gdacc 1 add get gdacc 1 lg
1933put gdacc 2 add get gdacc 2 ls
1934put gdacc 3 add get gdacc 3 sub add ls mg lg
1935put gdacc 5 add get gdacc 5 sub lg mg
1936end
1937
1938note sum of the chip's probabilities of row r, recovered from the CE output L = (p - [j = y]) sc
1939fn ggdps r ulong to f64
1940v = get og 0
1941sc = div 1.0f64 f64 ggr
1942o = add get gga 11 mul mul r v 4
1943s = 0.0f64
1944for j ulong v
1945s = add s div ggf add o mul j 4 sc
1946end
1947ret add s 1.0f64
1948end
1949
1950note gserve and gsample (aien-train DRA-292): serve and sample (serve.om, cli.om) with the per-token
1951note forward on the chip. cli.om's ochipup calls ogchip once the checkpoint loads: B = 1 (one
1952note context row, R = T), open the chip, upload the f32 parameters. olastf then calls oglast per
1953note token: upload the T context tokens of obx row 0 (positions past CT hold earlier tokens or 0,
1954note which the causal forward never reads for position CT - 1), ggdfwd (below: the rows up to the
1955note context's last 64-row tile, no CE, so the logits stay logits), and the logits of position
1956note CT - OMUTZ read back as f64 into TMP[0..V) (oa), where
1957note argmax and draws run as on the CPU.
1958note OMUTZ: 1 in the product; 0 in mutant z (the row past the context), which gscmp must catch.
1959const OMUTZ ulong 1
1960fn ogchip to void
1961put og 6 1
1962ggopen
1963ggupw
1964end
1965
1966fn oglast to void
1967ggupt
1968put gkinv 0 1
1969ggdfwd
1970ogrd
1971end
1972
1973note the logits of position CT - OMUTZ (row CT - OMUTZ of slot 11) read back as f64 into TMP[0..V) (oa)
1974fn ogrd to void
1975v = get og 0
1976r = sub get og 9 OMUTZ
1977o = add get gga 11 mul mul r v 4
1978tm = get og 27
1979for c ulong v
1980put oa add tm c ggf add o mul c 4
1981end
1982end
1983
1984note ggdfwd (ham/ollm-speed-kvdec): per-token GPU forward, including mixed-BF16 variants. ggfwd runs all
1985note R = T rows every token; a causal forward never lets a row read a later row, so the logits of
1986note position CT - 1 need rows 0..CT - 1 only. ggdfwd runs rows 0..rr - 1, rr = 64 ceil(CT / 64)
1987note capped at T (whole 64-row tiles, so every GEMM keeps ggfwd's 64x64 kernels and per-element sums),
1988note keeps the residual stream in place in layer 0's input hin (hin += o wo, hin += relu(bn w1) w2: the
1989note same f32 adds as ggffn's COPY then accumulate, without its two COPY launches per layer), and runs
1990note the final RMS and the logits GEMM on the last 64-row tile only (the one holding row CT - 1).
1991note Rows at or past rr keep stale arena data that no computed row reads (causal GEMM tiles; softmax
1992note chunks past a row's own are masked, gr.om k6). No CE launch (ggnoce's logits stay logits).
1993note OMUTJ: 1 in the product; 0 in mutant j (rr = 64 floor(CT / 64), at least 64: row CT - 1 lies past
1994note the computed rows once CT > 64 is not a multiple of 64), which gscmp and gdbench mode 2 must catch.
1995const OMUTJ ulong 1
1996note ggdm[0]: the rows the ggd* GEMMs run over
1997global ggdm ulong 1
1998note bfdec selects BF16 for attention-output and FFN projections only. QKV,
1999note attention scores/value sums, normalization and final logits retain f32.
2000global bfdec uint 1
2001
2002fn ggdrows to ulong
2003ct = get og 9
2004rr = mul div add ct 63 64 64
2005if eq OMUTJ 0
2006rr = mul div ct 64 64
2007if eq rr 0
2008rr = 64
2009end
2010end
2011tt = get og 1
2012if gt rr tt
2013rr = tt
2014end
2015ret rr
2016end
2017
2018note C[m][n] (+)= X[m][k] W[k][n] over the first m = ggdm[0] rows
2019fn ggdxw x ulong w ulong c ulong n ulong k ulong bt bool to void
2020k4 = uint mul k 4
2021n4 = uint mul n 4
2022gkopa 0 x k4 4 0 0
2023gkopa 1 w 4 n4 0 0
2024gkopc c n4 4 0 0 f32 1.0f64
2025ggmm get ggdm 0 n k 1 1 bt
2026end
2027
2028note C[m][n] (+)= X[m][k] W[n][k]^T over the first m = ggdm[0] rows
2029fn ggdxwt x ulong w ulong c ulong n ulong k ulong bt bool to void
2030k4 = uint mul k 4
2031n4 = uint mul n 4
2032gkopa 0 x k4 4 0 0
2033gkopa 1 w k4 4 0 0
2034gkopc c n4 4 0 0 f32 1.0f64
2035ggmm get ggdm 0 n k 1 1 bt
2036end
2037
2038note RMSF (kernel 10) over rows [r0, r0 + m) of the D-wide v and y and of s
2039fn ggdrms v ulong g ulong y ulong s ulong r0 ulong m ulong to void
2040o = mul r0 mul get og 2 4
2041r = mul m 32
2042ggu 0 r
2043ggpt 1 add v o
2044ggpt 2 g
2045ggpt 3 add y o
2046ggpt 4 add s mul r0 4
2047ggrun 10 10 ggcd r 64 1 1
2048end
2049
2050note q, k, u = a wq, a wk, a wv over the first ggdm[0] rows (ggqkv's batched GEMM)
2051fn ggdqkv l ulong to void
2052d = get og 2
2053a = ggl l 1
2054w = ggw ovwq l
2055c = ggl l 2
2056s0 = get ggs 0
2057s6 = get ggs 6
2058s7 = get ggs 7
2059gkopa 0 a s0 4 0 0
2060gkopa 1 w 4 s0 0 s7
2061gkopc c s0 4 0 s6 f32 1.0f64
2062ggmm get ggdm 0 d d 3 3 false
2063end
2064
2065note ggatt over the first rr = ggdm[0] rows: S (causal, rr x rr), SOFTF on rows < rr (grid rr / 2;
2066note arg 0 stays the thread bound T 32), o = P u (causal, k = rr)
2067fn ggdatt l ulong to void
2068rr = get ggdm 0
2069e = get og 7
2070pp = ggl l 12
2071q = ggl l 2
2072k = ggl l 3
2073gghd 0 q false
2074gghd 1 k false
2075isq = div 1.0f64 fsqrt f64 e
2076ggcp pp isq
2077ggbh rr rr e false 3
2078ggu 0 mul get og 1 32
2079ggpt 1 pp
2080ggpt 2 pp
2081ggrun 15 6 div rr 2 mul get og 6 get og 3 1
2082u = ggl l 4
2083o = ggl l 5
2084ggpl 0 pp false
2085gghd 1 u true
2086ggch o 1.0f64
2087ggbh rr e rr false 1
2088end
2089
2090note hin += o wo; bn = rms(hin) g2; hin += relu(bn w1) w2, over the first ggdm[0] rows, hin = layer 0's
2091fn ggdffn l ulong to void
2092savedbf = get gkbf 0
2093d = get og 2
2094f = get og 4
2095h = ggl 0 0
2096x = ggl l 5
2097w = ggw ovwo l
2098if ne get bfdec 0 0
2099put gkbf 0 1
2100end
2101ggdxw x w h d d true
2102put gkbf 0 savedbf
2103x = ggl l 7
2104w = ggw ovg2 l
2105s = ggl l 9
2106ggdrms h w x s 0 get ggdm 0
2107fp = ggl l 10
2108w = ggw ovw1 l
2109y = ggl l 11
2110gkope 1 y
2111if ne get bfdec 0 0
2112put gkbf 0 1
2113end
2114ggdxw x w fp f d false
2115put gkbf 0 savedbf
2116w = ggw ovw2 l
2117if ne get bfdec 0 0
2118put gkbf 0 1
2119end
2120ggdxw y w h d f true
2121put gkbf 0 savedbf
2122end
2123
2124fn ggdfl l ulong to void
2125h = ggl 0 0
2126w = ggw ovg1 l
2127a = ggl l 1
2128s = ggl l 8
2129ggdrms h w a s 0 get ggdm 0
2130ggdqkv l
2131ggdatt l
2132ggdffn l
2133end
2134
2135fn ggdfwd to void
2136rr = ggdrows
2137put ggdm 0 rr
2138d = get og 2
2139x = ggl 0 0
2140ggu 0 d
2141ggpt 1 x
2142x = ggw 0
2143ggpt 2 x
2144x = ggw get og 30
2145ggpt 3 x
2146x = gga1 2
2147ggpt 4 x
2148ggrun 9 10 ggcd d 64 rr get og 6
2149for l ulong get og 56 bound 9223372036854775807
2150ggdfl l
2151end
2152ggdhead rr
2153end
2154
2155note final RMS and logits GEMM over the last 64-row tile of the rr rows (all rr when rr < 64)
2156fn ggdhead rr ulong to void
2157d = get og 2
2158tb = ulong 64
2159if lt rr 64
2160tb = rr
2161end
2162b = sub rr tb
2163h = ggl 0 0
2164w = ggw get og 33
2165z = gga1 10
2166s = gga1 9
2167ggdrms h w z s b tb
2168put ggdm 0 tb
2169x = add z mul mul b d 4
2170w = ggw 0
2171v = get og 0
2172c = add gga1 11 mul mul b v 4
2173ggdxwt x w c v d false
2174put ggdm 0 rr
2175end
2176
2177note the forward of gdbench mode md: 0 ggfwd without CE (the gserve forward at edf39b0), 3 ggdfwd with
2178note every 64x64 GEMM on the BF16 tensor-core kernels (gk.om gkbf 1, docs/omegallm/CONTRACT-BF16.md;
2179note GEMMs on the 32x32 tile stay f32), 5 ggdfwd as one batch.om submission (ggdbat), 7 selective BF16, else ggdfwd
2180note in f32
2181fn ggdfm md ulong to void
2182if eq md 0
2183put ggnoce 0 1
2184ggfwd
2185put ggnoce 0 0
2186ret
2187end
2188if eq md 3
2189put gkbf 0 1
2190ggdfwd
2191put gkbf 0 0
2192ret
2193end
2194if eq md 7
2195put bfdec 0 1
2196ggdfwd
2197put bfdec 0 0
2198ret
2199end
2200if eq md 5
2201ggdbat
2202ret
2203end
2204ggdfwd
2205end
2206
2207note ggdbat: ggdfwd's launches recorded (gk.om gkbat) into one batch.om batch of exactly 3 + 9 L
2208note launches (EMB, nine per layer, final RMS, logits; batch.om refuses a different count before the
2209note doorbell), sent once and waited once (gkbsub). The kernels, their order and arguments are
2210note ggdfwd's; batch.om ends each body with a WFI release and a shader invalidate, so each grid
2211note drains before the next starts. gdbench opens the batch packet (gbbopen) once after the chip.
2212fn ggdbat to void
2213if not gbbegin add 3 mul 9 get og 56
2214ggdie 120
2215end
2216put gkbat 0 1
2217ggdfwd
2218put gkbat 0 0
2219if not gkbsub
2220ggdie 121
2221end
2222end
2223
2224note FNV-1a over the f64 bits of the V logits in TMP (equal hashes: the same logits, bit for bit)
2225fn ggdhash to ulong
2226tm = get og 27
2227h = ulong 14695981039346656037
2228for c ulong get og 0
2229x = bitcast ulong get oa add tm c
2230h = mul xor h x 1099511628211
2231end
2232ret h
2233end
2234
2235note one gdbench step in mode md on the current context: prints `GD step ct tok hash up_ns fwd_ns
2236note rd_ns launches build_ns wait_ns` (up: the token upload, fwd: the forward's launches, rd: logits
2237note read back and argmax; launches, build and wait from gk.om's gkt) and returns the token
2238fn ggdstep s ulong md ulong to ulong
2239k0 = get gkt 0
2240b0 = get gkt 1
2241w0 = get gkt 2
2242t0 = monons
2243ggupt
2244put gkinv 0 1
2245t1 = monons
2246ggdfm md
2247t2 = monons
2248ogrd
2249a = oargmax
2250t3 = monons
2251h = ggdhash
2252oputc 71
2253oputc 68
2254oputc 32
2255oputu s
2256oputc 32
2257oputu get og 9
2258oputc 32
2259oputu a
2260oputc 32
2261oputu h
2262oputc 32
2263oputl sub t1 t0
2264oputc 32
2265oputl sub t2 t1
2266oputc 32
2267oputl sub t3 t2
2268oputc 32
2269oputl sub get gkt 0 k0
2270oputc 32
2271oputl sub get gkt 1 b0
2272oputc 32
2273oputl sub get gkt 2 w0
2274oputc 10
2275oflush
2276ret a
2277end
2278
2279note gdbench comparison step (2, 4, 6 or 8): ggfwd's logits into ggdcl, then ggdfwd's (f32 in mode 2, BF16 in mode 4,
2280note mode 6 batched, mode 8 selective BF16) into TMP on the same context; prints `GQ step ct tok0 tok1 ndiff maxdiff_e9 margin_e9`
2281note (ndiff: logits whose f64 bits differ, read back from f32, so f32 bits; margin: ggfwd's top-1
2282note logit minus its top-2) and returns tok0. Totals: ggdnd ndiff, ggdmx maxdiff, ggdkd the steps whose
2283note tokens differ although the margin exceeds 2 BFTOL. The logits slot is zeroed (ZERO 20) between
2284note the two forwards, so ggdfwd cannot pass on ggfwd's rows.
2285note BFTOL: the BF16 decode tolerance in nats, declared before its first chip run (ham/ollm-speed
2286note 2026-11-10): modes 4 and 8 pass when maxdiff <= BFTOL and no high-margin step diverges.
2287const BFTOL f64 0.25
2288global ggdcl f64 65536
2289global ggdmx f64 1
2290global ggdnd ulong 1
2291global ggdkd ulong 1
2292global ggdst f64 1
2293
2294note ggdcl[a] minus the largest other ggdcl[c], c < V
2295fn ggdmg a ulong to f64
2296b = bitcast f64 ulong 18442240474082181120
2297for c ulong get og 0
2298if ne c a
2299b = fmax b get ggdcl c
2300end
2301end
2302ret sub get ggdcl a b
2303end
2304
2305fn ggdpar s ulong md ulong to ulong
2306ggupt
2307put gkinv 0 1
2308ggdfm 0
2309ogrd
2310ggdkeep
2311a0 = oargmax
2312v = get og 0
2313ggew 20 mul ggr v gga1 11 gga1 11
2314if eq md 4
2315ggdfm 3
2316else
2317ggdfm sub md 1
2318end
2319ogrd
2320a1 = oargmax
2321nd = ggdcmp
2322dm = get ggdst 0
2323put ggdnd 0 add get ggdnd 0 nd
2324put ggdmx 0 fmax get ggdmx 0 dm
2325mg = ggdmg a0
2326if and ne a0 a1 gt mg mul 2.0f64 BFTOL
2327put ggdkd 0 add get ggdkd 0 1
2328end
2329oputc 71
2330oputc 81
2331oputc 32
2332oputu s
2333oputc 32
2334oputu get og 9
2335oputc 32
2336oputu a0
2337oputc 32
2338oputu a1
2339oputc 32
2340oputu nd
2341oputc 32
2342oputl long mul dm 1000000000.0f64
2343oputc 32
2344oputl long mul mg 1000000000.0f64
2345oputc 10
2346oflush
2347ret a0
2348end
2349
2350note ggdcl = the V logits in TMP
2351fn ggdkeep to void
2352tm = get og 27
2353for c ulong get og 0
2354x = get oa add tm c
2355put ggdcl c x
2356end
2357end
2358
2359note the logits in TMP whose bits differ from ggdcl; the largest |difference| into ggdst[0]
2360fn ggdcmp to ulong
2361tm = get og 27
2362nd = ulong 0
2363dm = 0.0f64
2364for c ulong get og 0
2365x = get oa add tm c
2366y = get ggdcl c
2367if ne bitcast ulong x bitcast ulong y
2368nd = add nd 1
2369end
2370dm = fmax dm fabs sub x y
2371end
2372put ggdst 0 dm
2373ret nd
2374end
2375
2376fn ggdsel s ulong md ulong to ulong
2377if or or or eq md 2 eq md 4 eq md 6 eq md 8
2378ret ggdpar s md
2379end
2380ret ggdstep s md
2381end
2382
2383note gdbench CK N PROMPT MERGES MODE (ham/ollm-speed): the per-token chip forward,
2384note Modes 7 and 8 select the BF16 decode policy and its comparison, respectively.
2385note They retain QKV, attention and logits in f32; three projection GEMMs per layer use BF16.
2386note timed, on the contexts of N greedy steps from token 0 then PROMPT's encoding (once the context
2387note holds T tokens it slides, as ocpush does). MODE 0: ggfwd (all R = T rows, the gserve forward at
2388note edf39b0); 1: ggdfwd (oglast's forward); 3: ggdfwd with BF16 GEMMs (ggdfm 3); 5: ggdfwd as one
2389note batched submission (ggdbat); 7: selective BF16. Modes 2, 4, 6 and 8 compare ggfwd and ggdfwd
2390note (2 f32, 4 BF16, 6 batched, 8 selective BF16) on every
2391note step, one GQ line each (ggdpar), then `GDQ N ndiff maxdiff_e9 diverged`; modes 2 and 6 exit 41
2392note when any logit differs in its bits; modes 4 and 8 exit 42 when maxdiff exceeds BFTOL or a high-margin step
2393note diverged. Modes 0, 1, 3, 5 and 7 print one GD line per step (ggdstep), then run one more forward
2394note at the last context with the launch profile on (`GP id gx gy gz m n k wait_ns` per launch, gk.om
2395note gkpf; batched launches log wait 0), 64 ZERO launches of 64 floats (the bare launch round trip,
2396note GP id 20) and `GDT mode N load_ns open_ns` (checkpoint and merges load; chip open and f32
2397note parameter upload).
2398fn ggdbench to void
2399if ne oargc 7
2400exit 2
2401end
2402t0 = monons
2403ockloop 2
2404nn = oargu 3
2405md = oargu 6
2406v = get og 0
2407if or le v 256 gt md 8
2408exit 2
2409end
2410obload 5 v
2411t1 = monons
2412ogchip
2413if or eq md 5 eq md 6
2414if not gbbopen
2415ggdie 122
2416end
2417end
2418t2 = monons
2419put og 9 0
2420ocpush 0
2421np = obprompt 4
2422for i ulong np
2423ocpush get obtk i
2424end
2425put ggdmx 0 0.0f64
2426put ggdnd 0 0
2427put ggdkd 0 0
2428for s ulong nn
2429a = ggdsel s md
2430ocpush a
2431end
2432if or or or eq md 2 eq md 4 eq md 6 eq md 8
2433ggdverd nn md
2434ret
2435end
2436ggdprof md
2437oputc 71
2438oputc 68
2439oputc 84
2440oputc 32
2441oputu md
2442oputc 32
2443oputu nn
2444oputc 32
2445oputl sub t1 t0
2446oputc 32
2447oputl sub t2 t1
2448oputc 10
2449oflush
2450r = nvstop
2451end
2452
2453note the parity modes' last line `GDQ N ndiff maxdiff_e9 diverged`, then their exit codes
2454fn ggdverd nn ulong md ulong to void
2455oputc 71
2456oputc 68
2457oputc 81
2458oputc 32
2459oputu nn
2460oputc 32
2461oputu get ggdnd 0
2462oputc 32
2463oputl long mul get ggdmx 0 1000000000.0f64
2464oputc 32
2465oputu get ggdkd 0
2466oputc 10
2467oflush
2468r = nvstop
2469if and and ne md 4 ne md 8 gt get ggdnd 0 0
2470exit 41
2471end
2472if and or eq md 4 eq md 8 or gt get ggdmx 0 BFTOL gt get ggdkd 0 0
2473exit 42
2474end
2475end
2476
2477note one forward in mode md at the current context with the launch profile on, then 64 ZERO launches
2478note of 64 floats; prints one GP line per profiled launch
2479fn ggdprof md ulong to void
2480put gkpc 0 0
2481put gkpf 0 1
2482ggupt
2483put gkinv 0 1
2484ggdfm md
2485for i ulong 64
2486ggew 20 64 ggg 0 ggg 0
2487end
2488put gkpf 0 0
2489for q ulong get gkpc 0
2490o = mul q 8
2491oputc 71
2492oputc 80
2493for j ulong 8
2494oputc 32
2495oputu get gkpl add o j
2496end
2497oputc 10
2498end
2499end
2500
2501note gscmp CK N PROMPT MERGES (V > 256): the chip forward against the CPU forward on the contexts of
2502note N greedy steps from token 0 then PROMPT's encoding. Each step runs ofwd and olast (f64) into
2503note gscl, then oglast (chip, f32) into TMP, and prints `GS step cpu_tok chip_tok maxdiff_nano
2504note margin_nano`: maxdiff = max over the V logits of |chip - cpu|, margin = the CPU's top-1 logit
2505note minus its top-2. The CPU token is pushed. Last line `GSCMP N diverged maxdiff_nano`: a step
2506note diverges when the tokens differ although the margin exceeds 2 GSTOL. Exit 40 when a step
2507note diverged or maxdiff exceeds GSTOL.
2508const GSTOL f64 0.002
2509global gscl f64 65536
2510note gscmp totals: gsmd[0] the largest maxdiff, gskd[0] the diverged steps
2511global gsmd f64 1
2512global gskd ulong 1
2513fn ggscmp to void
2514if ne oargc 6
2515exit 2
2516end
2517ockloop 2
2518nn = oargu 3
2519v = get og 0
2520if le v 256
2521exit 2
2522end
2523obload 5 v
2524ogchip
2525put og 9 0
2526ocpush 0
2527np = obprompt 4
2528for i ulong np
2529ocpush get obtk i
2530end
2531put gsmd 0 0.0f64
2532put gskd 0 0
2533for s ulong nn
2534a = ggsst s
2535ocpush a
2536end
2537md = get gsmd 0
2538kd = get gskd 0
2539oputc 71
2540oputc 83
2541oputc 67
2542oputc 77
2543oputc 80
2544oputc 32
2545oputu nn
2546oputc 32
2547oputu kd
2548oputc 32
2549oputl long mul md 1000000000.0f64
2550oputc 10
2551oflush
2552r = nvstop
2553if or gt kd 0 gt md GSTOL
2554exit 40
2555end
2556end
2557
2558note one gscmp step s on the current context: prints its GS line, updates the totals, returns the
2559note CPU token
2560fn ggsst s ulong to ulong
2561a1 = ggscpu
2562mg = ggsmg a1
2563oglast
2564a2 = oargmax
2565dm = ggsdf
2566put gsmd 0 fmax get gsmd 0 dm
2567if and ne a1 a2 gt mg mul 2.0f64 GSTOL
2568put gskd 0 add get gskd 0 1
2569end
2570oputc 71
2571oputc 83
2572oputc 32
2573oputu s
2574oputc 32
2575oputu a1
2576oputc 32
2577oputu a2
2578oputc 32
2579oputl long mul dm 1000000000.0f64
2580oputc 32
2581oputl long mul mg 1000000000.0f64
2582oputc 10
2583oflush
2584ret a1
2585end
2586
2587note the CPU forward's last-position logits into gscl; returns its argmax
2588fn ggscpu to ulong
2589v = get og 0
2590tm = get og 27
2591lf = ofwd 0
2592olast
2593for c ulong v
2594put gscl c get oa add tm c
2595end
2596ret oargmax
2597end
2598
2599note max over c < V of |TMP[c] - gscl[c]|
2600fn ggsdf to f64
2601tm = get og 27
2602dm = 0.0f64
2603for c ulong get og 0
2604x = fabs sub get oa add tm c get gscl c
2605dm = fmax dm x
2606end
2607ret dm
2608end
2609
2610note the CPU's top-1 logit gscl[a] minus its largest other logit
2611fn ggsmg a ulong to f64
2612b = bitcast f64 ulong 18442240474082181120
2613for c ulong get og 0
2614if ne c a
2615b = fmax b get gscl c
2616end
2617end
2618ret sub get gscl a b
2619end
2620
2621note ogcmd: the gb10 target's GPU commands. cli.om's ocmd offers every command word here first
2622note (nogpu.om's ogcmd takes none on the CPU-only targets), so the gb10 unit is the aarch64 source
2623note with gpu/ in place of nogpu.om and no renamed main. It runs the GPU commands here and the BF16
2624note training commands in gb.om and returns true; any other word returns false and ocmd goes on to
2625note the CPU commands. The inference interface is documented in docs/omegallm/CONTRACT-BF16.md;
2626note command constants encode little-endian word bytes.
2627fn ogcmd w ulong to bool
2628if eq w 27696107117441895
2629put bfdec 0 1
2630put ogpu 0 1
2631cmdserve
2632r = nvstop
2633ret true
2634end
2635if eq w 108235010962279
2636put bfdec 0 1
2637put ogpu 0 1
2638cmdsamp
2639r = nvstop
2640ret true
2641end
2642if eq w 111559399797607
2643put ogpu 0 1
2644cmdserve
2645r = nvstop
2646ret true
2647end
2648if eq w 28548202775016295
2649put ogpu 0 1
2650cmdsamp
2651r = nvstop
2652ret true
2653end
2654if eq w 482871571303
2655ggscmp
2656ret true
2657end
2658if eq w 29382723376407655
2659ggdbench
2660ret true
2661end
2662if eq w 1886217063
2663ggcmp
2664ret true
2665end
2666if eq w 216634581863
2667ggcmp2
2668ret true
2669end
2670if eq w 121398885512295
2671ggtrain
2672ret true
2673end
2674if eq w 28831846509999207
2675ggtrainf
2676ret true
2677end
2678if eq w 482871566695
2679ggacmp
2680ret true
2681end
2682if eq w 465491617127
2683ggeval
2684ret true
2685end
2686if eq w 444015928423
2687ggdiag
2688ret true
2689end
2690if eq w 439956435047
2691ggprof
2692ret true
2693end
2694if eq w 27705946603156583
2695gbtrain
2696ret true
2697end
2698if eq w 108192095957095
2699gbprof
2700ret true
2701end
2702if eq w 482871566951
2703gbcmp
2704ret true
2705end
2706if eq w 55458452955751
2707gbcmp2
2708ret true
2709end
2710if eq w 113668228801127
2711gbfrag
2712ret true
2713end
2714ret false
2715end