interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / omegallm / gpu / gg.om

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