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

1005 lines · 21761 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note gb.om: ollm BF16 tensor-core path, commands and gates (docs/omegallm/CONTRACT-BF16.md). With gkbf 1,
3note gk.om runs every 64x64 gkmm on kernels 27..36 (HMMA.16816.F32.BF16; the kernel rounds both operands
4note to bf16 RN with F2FP and accumulates in f32). Commands: gtrainb and gprofb (gtrainf and gprof with
5note gkbf 1), gbcmp (sampled check of every BF16 GEMM of one step, exit 48), gbcmp2 (gcmp2's step parity
6note with the BF16 bound, exit 49) and gbfrag (exact check of all ten kernels on fixed data, exit 50).
7note Concatenated before gg.om: it calls gg.om functions but uses none of gg.om's globals.
8
9note OMUTU: 1 in the product; 0 in mutant u, whose reference skips the bf16 rounding (gbfrag must then
10note fail; gbcmp only reports it, CONTRACT-BF16 section 5).
11const OMUTU ulong 1
12
13note OMUTN: 1 in the product; 0 in mutant n, the negative control of gbfrag's padding check: after each
14note short case's launch the host writes 0.0 into C's first padding word (block 0, row 0, column n), so
15note only that check can fail (CONTRACT-BF16 section 4.1).
16const OMUTN ulong 1
17
18note bf16 round to nearest even of the f32 bits u, as f32 bits (NaN unchanged)
19fn gbbf u uint to uint
20if eq OMUTU 0
21ret u
22end
23if and eq and shr u 23 255 255 ne and u 8388607 0
24ret u
25end
26v = add add u 32767 and shr u 16 1
27ret and v 4294901760
28end
29
30note f32 bits x rounded to bf16, as f64
31fn gbrv x uint to f64
32ret f64 bitcast f32 gbbf x
33end
34
35fn gbabs x f64 to f64
36if lt x 0.0f64
37ret neg x
38end
39ret x
40end
41
42note arena word at host byte offset o (offset from gkbase)
43fn gbw o ulong to uint
44ret uint mapget get gbmem 5 o 4
45end
46
47fn gbput o ulong x uint to void
48m = mapset get gbmem 5 o 4 ulong x
49end
50
51note host byte offset of the device address held in arg words w, w + 1
52fn gbhp w uint to ulong
53lo = ulong get gkarg w
54hi = ulong get gkarg add w 1
55ret sub or lo shl hi 32 gkbase
56end
57
58note batch offset z1 s1 + z2 s2 (z = z1 h2 + z2) with s1, s2 in arg words w, w + 1
59fn gbzo z uint h2 uint w uint to ulong
60a = mul ulong div z h2 ulong get gkarg w
61ret add a mul ulong rem z h2 ulong get gkarg add w 1
62end
63
64note verdict line GBCMP (c 48), GBCMP2 (49) or GBFRAG (50): PASS, or FAIL and exit c
65fn gbvd c ulong ok bool to void
66oputc 71
67oputc 66
68if eq c 50
69oputc 70
70oputc 82
71oputc 65
72oputc 71
73else
74oputc 67
75oputc 77
76oputc 80
77if eq c 49
78oputc 50
79end
80end
81oputc 32
82if ok
83oputc 80
84oputc 65
85oputc 83
86oputc 83
87oputc 10
88oflush
89ret
90end
91oputc 70
92oputc 65
93oputc 73
94oputc 76
95oputc 10
96oflush
97if eq c 48
98exit 48
99end
100if eq c 49
101exit 49
102end
103exit 50
104end
105
106note gbcmp state. gbcst (gbst until ham/ollm-speed: batch.om owns that name): 0 BF16 GEMMs checked, 1 samples past their bound, 2 kernel id mask, 3 cz mask,
107note 4 ep mask, 5 beta mask, 6 the sample generator. gbmx[0]: the worst |chip - ref| / bound. gbsp: the
108note 66 sampled (z, r, c) of the launch in flight, gbold the old C there (beta), gbacc the reference sums.
109global gbcst ulong 8
110global gbmx f64 1
111global gbsp uint 198
112global gbold f64 66
113global gbacc f64 3
114
115note next value in [0, n) of the sample generator (31-bit LCG)
116fn gbrnd n uint to uint
117s = rem add mul get gbcst 6 1103515245 12345 2147483648
118put gbcst 6 s
119ret uint rem shr s 8 ulong n
120end
121
122note host byte offset at sample i in C (w 16) or in the epilogue's Y / M (w 30, C's strides)
123fn gbco i ulong w uint to ulong
124o = mul i 3
125z = get gbsp o
126b = add gbhp w gbzo z get gkbp 4 20
127b = add b mul ulong get gbsp add o 1 ulong get gkarg 18
128ret add b mul ulong get gbsp add o 2 ulong get gkarg 19
129end
130
131note gkmm's hook before a checked BF16 launch (gkbp = m n k nz h2 beta ep cz): 64 random (z, r, c)
132note and the corners (0, 0, 0), (nz - 1, m - 1, n - 1); with cz 3 a position in a block above the
133note diagonal (not computed) is mirrored below it. Keeps the old C when beta.
134fn gbpre to void
135for i ulong 66
136gbpre1 i
137end
138end
139
140note one sample i of gbpre
141fn gbpre1 i ulong to void
142z = gbrnd get gkbp 3
143r = gbrnd get gkbp 0
144c = gbrnd get gkbp 1
145if eq i 64
146z = 0
147r = 0
148c = 0
149end
150if eq i 65
151z = sub get gkbp 3 1
152r = sub get gkbp 0 1
153c = sub get gkbp 1 1
154end
155if eq get gkbp 7 3
156if gt div c 64 div r 64
157t = r
158r = c
159c = t
160end
161end
162o = mul i 3
163put gbsp o z
164put gbsp add o 1 r
165put gbsp add o 2 c
166put gbold i 0.0f64
167if eq get gkbp 5 1
168put gbold i f64 bitcast f32 gbw gbco i 16
169end
170end
171
172note reference sums of sample i over the K range the kernel reads (cz 1: k < (r / 64 + 1) 64,
173note cz 2: k >= (r / 64) 64): gbacc 0 sum a b, 1 sum |a b|, 2 the term count; a, b rounded to bf16
174fn gbref i ulong to void
175r = ulong get gbsp add mul i 3 1
176k = ulong get gkbp 2
177lo = ulong 0
178hi = mul add div r 64 1 64
179cz = get gkbp 7
180if eq cz 1
181if lt hi k
182k = hi
183end
184end
185if eq cz 2
186lo = mul div r 64 64
187end
188gbdot gbsa i 0 gbsa i 1 lo sub k lo
189end
190
191note host address of sample i's A row (w 0) or B column (w 1) at k = 0
192fn gbsa i ulong w uint to ulong
193o = mul i 3
194z = get gbsp o
195h2 = get gkbp 4
196g = mul w 8
197x = ulong get gbsp add o ulong add w 1
198p = add gbhp g gbzo z h2 add g 4
199ret add p mul x ulong get gkarg add g 2
200end
201
202fn gbdot pa ulong pb ulong lo ulong kn ulong to void
203ka = ulong get gkarg 3
204kb = ulong get gkarg 11
205s = 0.0f64
206sa = 0.0f64
207for j ulong kn
208kk = add lo j
209p = mul gbrv gbw add pa mul kk ka gbrv gbw add pb mul kk kb
210s = add s p
211sa = add sa gbabs p
212end
213put gbacc 0 s
214put gbacc 1 sa
215put gbacc 2 f64 kn
216end
217
218note d / b, 0 when both are 0; 1e9 when d > b with b = 0 or the ratio non-finite or past 1e9
219fn gbrat d f64 b f64 to f64
220if le d b
221if gt b 0.0f64
222ret div d b
223end
224ret 0.0f64
225end
226if gt b 0.0f64
227q = div d b
228if lt q 1000000000.0f64
229ret q
230end
231end
232ret 1000000000.0f64
233end
234
235note |chip - ref| / bound at sample i, bound (kn 2^-23 + 2^-22)(|alpha| sum |a b| + |old C|); 1e9
236note for a wrong epilogue (ep 2: C must be +0 where M is not > 0; ep 1: Y = C when C > 0, else +0)
237fn gbchk i ulong to f64
238co = gbco i 16
239cb = gbw co
240ep = get gkbp 6
241if eq ep 2
242if not gt f64 bitcast f32 gbw gbco i 30 0.0f64
243if eq cb 0
244ret 0.0f64
245end
246ret 1000000000.0f64
247end
248end
249if eq ep 1
250w = uint 0
251if gt f64 bitcast f32 cb 0.0f64
252w = cb
253end
254if ne gbw gbco i 30 w
255ret 1000000000.0f64
256end
257end
258gbref i
259al = f64 bitcast f32 get gkarg 25
260old = get gbold i
261d = gbabs sub f64 bitcast f32 cb add mul al get gbacc 0 old
262e = add div get gbacc 2 8388608.0f64 div 1.0f64 4194304.0f64
263ret gbrat d mul e add mul gbabs al get gbacc 1 gbabs old
264end
265
266note kernel id of the launch in flight (gk.om gkmmb)
267fn gbid to ulong
268ep = ulong get gkbp 6
269if ne ep 0
270ret add 34 ep
271end
272id = add 27 ulong get gkmd 0
273id = add id mul 2 ulong get gkmd 1
274ret add id mul 4 ulong get gkbp 5
275end
276
277note gkmm's hook after a checked BF16 launch: check the samples, print
278note `GB id m n k nz h2 beta ep cz worst_e6 bad`; true (the step goes on, gbcmp gives the verdict)
279fn gbpost to bool
280bad = ulong 0
281mr = 0.0f64
282for i ulong 66
283q = gbchk i
284if gt q 1.0f64
285bad = add bad 1
286end
287if gt q mr
288mr = q
289end
290end
291gbnote bad mr
292ret true
293end
294
295fn gbnote bad ulong mr f64 to void
296id = gbid
297put gbcst 0 add get gbcst 0 1
298put gbcst 1 add get gbcst 1 bad
299put gbcst 2 or get gbcst 2 shl ulong 1 id
300put gbcst 3 or get gbcst 3 shl ulong 1 ulong get gkbp 7
301put gbcst 4 or get gbcst 4 shl ulong 1 ulong get gkbp 6
302put gbcst 5 or get gbcst 5 shl ulong 1 ulong get gkbp 5
303if gt mr get gbmx 0
304put gbmx 0 mr
305end
306oputc 71
307oputc 66
308oputc 32
309oputu id
310for j ulong 8
311oputc 32
312oputu ulong get gkbp j
313end
314oputc 32
315oputl rmic mr
316oputc 32
317oputu bad
318oputc 10
319end
320
321note gbcmp CK DATA S: one gtrainf step's gradient pass (gggrad, no Adam) with gkbf 1 and gkbck 1, so
322note gk.om checks every BF16 launch (gbpre, gbpost). Prints the GB lines, `GBC gemms bad worst_e6
323note idmask czmask epmask betamask` and GBCMP PASS, or GBCMP FAIL (exit 48): a sample past its bound,
324note no BF16 GEMM, or coverage short of the step's BF16 combinations (epilogues 1 and 2, cz 3, beta
325note false and true).
326fn gbcmp to void
327if ne oargc 5
328exit 2
329end
330ockload 2
331odopen 3
332obatch oargu 4
333put og 10 0
334put gbcst 6 12345
335put gkbf 0 1
336put gkbck 0 1
337ggopen
338l = gggrad
339put gkbck 0 0
340r = nvstop
341oputc 71
342oputc 66
343oputc 67
344for j ulong 2
345oputc 32
346oputu get gbcst j
347end
348oputc 32
349oputl rmic get gbmx 0
350for j ulong 4
351oputc 32
352oputu get gbcst add j 2
353end
354oputc 10
355ok = and eq get gbcst 1 0 gt get gbcst 0 0
356cv = eq and get gbcst 4 6 6
357cv = and cv ne and get gbcst 3 8 0
358cv = and cv eq get gbcst 5 3
359gbvd 48 and ok cv
360end
361
362note gbcmp2 bound: tolb = n_c (2u + u^2), u = 2^-8, n_c = 10 L + 3 BF16 GEMMs on a step's longest
363note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV;
364note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic
365fn gbtolb to f64
366nc = f64 add mul 10 get og 5 3
367ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64
368end
369
370note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss
371fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong
372tb = gbtolb
373fl = ulong 0
374if ne nf 0
375fl = or fl 1
376end
377if not lt ggclp rho tb
378fl = or fl 2
379end
380if not lt gg2rl 2 tb
381fl = or fl 4
382end
383if not lt gbabs sub lg l0 mul tb gbabs l0
384fl = or fl 8
385end
386ret fl
387end
388
389note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p))
390global gbcq f64 3
391fn gbcos p ulong to f64
392for k ulong 3
393put gbcq k 0.0f64
394end
395for q ulong ocn p
396i0 = mul q OCH
397for j ulong sub oce q p i0
398gbcq1 add i0 j p
399end
400end
401d = fsqrt mul get gbcq 1 get gbcq 2
402if gt d 0.0f64
403ret ggclp div get gbcq 0 d
404end
405ret 0.0f64
406end
407
408fn gbcq1 i ulong p ulong to void
409x = omvg i
410y = omvg add p i
411put gbcq 0 add get gbcq 0 mul x y
412put gbcq 1 add get gbcq 1 mul x x
413put gbcq 2 add get gbcq 2 mul y y
414end
415
416note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9` and verdict (exit 49)
417fn gbc2vd lg f64 l0 f64 fl ulong to void
418cs = gbcos get og 8
419oputc 71
420oputc 66
421oputc 50
422oputc 32
423oputu get og 5
424oputc 32
425oputu add mul 10 get og 5 3
426oputc 32
427oputl rmic mul gbtolb 1000.0f64
428oputc 32
429oputl rmic mul cs 1000.0f64
430oputc 32
431oputl rmic mul gg2rl 0 1000.0f64
432oputc 32
433oputl rmic mul gg2rl 2 1000.0f64
434oputc 32
435oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64
436oputc 10
437gbvd 49 eq fl 0
438end
439
440note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd)
441fn gbcmp2 to void
442put gkbf 0 1
443ggcmp2
444end
445
446note gtrainb: gtrainf with gkbf 1 (geval stays f32)
447fn gbtrain to void
448put gkbf 0 1
449ggtrainf
450end
451
452note gprofb: gprof with gkbf 1
453fn gbprof to void
454put gkbf 0 1
455ggprof
456end
457
458note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by
459note element against a reference built from the bf16-rounded operands. One operand holds integers in
460note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9
461note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen
462note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles
463note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the
464note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN
465note afterwards. Data in the logits buffer (A, B, C, Y at 0, 1, 2, 3 MiB). Prints `GF pass case id k cz ep bad` per case,
466note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL
467note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi).
468note gbcs: 0 ma, 1 mb (1 = K-contiguous), 2 beta, 3 ep, 4 cz, 5 k, 6 m, 7 n, 8 pass, 9 alpha 1/2
469global gbcs uint 10
470global gbfb ulong 1
471global gbha f64 98304
472global gbhb f64 147456
473
474fn gbfrag to void
475if ne oargc 3
476exit 2
477end
478ockload 2
479ggopen
480if lt mul ggr get og 0 4194304
481ggdie 3
482end
483put gkbf 0 1
484put gkbck 0 0
485put gbfb 0 sub gga1 11 gkbase
486bad = ulong 0
487for p ulong 2
488for i ulong 13
489bad = add bad gbf1 i p
490end
491end
492for p ulong 2
493for q uint 4
494bad = add bad gbfl p and q 1 shr q 1
495end
496end
497r = nvstop
498oputc 71
499oputc 70
500oputc 83
501oputc 32
502oputu bad
503oputc 10
504gbvd 50 eq bad 0
505end
506
507note case i of pass p into gbcs
508fn gbcase i ulong p ulong to void
509for j ulong 10
510put gbcs j 0
511end
512put gbcs 8 uint p
513put gbcs 6 128
514put gbcs 7 192
515put gbcs 5 128
516if eq rem i 3 0
517put gbcs 5 32
518end
519if eq rem i 3 1
520put gbcs 5 96
521put gbcs 9 1
522end
523if lt i 8
524put gbcs 0 uint and i 1
525put gbcs 1 uint and shr i 1 1
526put gbcs 2 uint and shr i 2 1
527ret
528end
529put gbcs 9 0
530put gbcs 0 1
531if eq i 8
532put gbcs 3 1
533put gbcs 5 96
534end
535if eq i 9
536put gbcs 1 1
537put gbcs 3 2
538end
539note cz 1 needs k = m
540if eq i 10
541put gbcs 4 1
542put gbcs 5 128
543end
544if eq i 11
545put gbcs 0 0
546put gbcs 2 1
547put gbcs 4 2
548end
549if eq i 12
550put gbcs 1 1
551put gbcs 4 3
552put gbcs 5 32
553put gbcs 7 128
554put gbcs 9 1
555end
556end
557
558note one case: write the data, launch, compare; the mismatch count
559fn gbf1 i ulong p ulong to ulong
560gbcase i p
561gbnan
562gbwop 0
563gbwop 1
564gbwc
565put gkinv 0 1
566if not gbflch
567ggdie 98
568end
569if eq OMUTN 0
570n = ulong get gbcs 7
571gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0
572end
573b = gbfcmp
574ep = ulong get gbcs 3
575id = add 27 ulong get gbcs 0
576id = add id mul 2 ulong get gbcs 1
577id = add id mul 4 ulong get gbcs 2
578if ne ep 0
579id = add 34 ep
580end
581oputc 71
582oputc 70
583oputc 32
584oputu p
585oputc 32
586oputu i
587oputc 32
588oputu id
589oputc 32
590oputu ulong get gbcs 5
591oputc 32
592oputu ulong get gbcs 4
593oputc 32
594oputu ep
595oputc 32
596oputu b
597oputc 10
598ret b
599end
600
601note NaN in every A and B word (2 MiB)
602fn gbnan to void
603b = get gbfb 0
604for i ulong 524288
605gbput add b mul i 4 2143289344
606end
607end
608
609note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B);
610note batch z at (z / 3)(3 zs + 64) + (z % 3) zs
611fn gbzs md uint w ulong kd ulong to ulong
612if eq md 1
613ret mul w add kd 8
614end
615ret mul kd add w 16
616end
617
618fn gbzof z ulong zs ulong to ulong
619ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs
620end
621
622fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong
623if eq md 1
624ret add mul x add kd 8 kk
625end
626ret add x mul kk add w 16
627end
628
629note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z)
630fn gbfr x ulong kk ulong z ulong to f64
631s = add mul x 7 mul kk 3
632s = add s mul z 5
633a = sub f64 rem s 5 2.0f64
634s = add mul x 13 mul kk 11
635s = add s mul z 3
636b = f64 rem s 64
637ret add a div b 512.0f64
638end
639
640fn gbint x ulong kk ulong z ulong to f64
641s = add mul x 5 mul kk 7
642s = add s mul z 3
643ret sub f64 rem s 11 5.0f64
644end
645
646note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded
647note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped.
648fn gbwop ab uint to void
649md = get gbcs ab
650k = ulong get gbcs 5
651w = ulong get gbcs add 6 ab
652zs = gbzs md w k
653base = add get gbfb 0 mul ulong ab 1048576
654fr = eq get gbcs 8 ab
655for z ulong 6
656for x ulong w
657gbwrow ab z x base zs fr
658end
659end
660end
661
662fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void
663zb = add base mul gbzof z zs 4
664t = add 128 mul 64 ulong ab
665hi = mul add mul z t x 128
666k = ulong get gbcs 5
667for kk ulong k
668gbwel ab zb x kk gbwv x kk z fr hi
669end
670end
671
672note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer
673fn gbwv x ulong kk ulong z ulong fr bool to uint
674v = gbint x kk z
675if fr
676v = gbfr x kk z
677end
678ret bitcast uint f32 v
679end
680
681note store bits u of operand ab at (x, kk) of the z block at zb; keep its bf16 value at gbha / gbhb hi + kk
682fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void
683md = get gbcs ab
684w = ulong get gbcs add 6 ab
685o = gbopf md x kk w ulong get gbcs 5
686gbput add zb mul o 4 u
687if eq ab 0
688put gbha add hi kk gbrv u
689else
690put gbhb add hi kk gbrv u
691end
692end
693
694note old C ((3 r + 5 c + z) mod 17) - 8, and the epilogue-2 mask M: NaN when (5 r + c) mod 13 = 0,
695note else ((r + 3 c + z) mod 3) - 1, as f32 bits
696fn gbold3 z ulong r ulong c ulong to f64
697s = add mul r 3 mul c 5
698s = add s z
699ret sub f64 rem s 17 8.0f64
700end
701
702fn gbmv z ulong r ulong c ulong to uint
703if eq rem add mul r 5 c 13 0
704ret 2143289344
705end
706ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64
707end
708
709note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns
710note (and Y's / M's with a fused epilogue) are NaN
711fn gbwc to void
712m = ulong get gbcs 6
713zs = mul m add ulong get gbcs 7 8
714for z ulong 6
715for r ulong m
716gbwcr z r zs
717end
718end
719end
720
721fn gbwcr z ulong r ulong zs ulong to void
722n = ulong get gbcs 7
723ep = get gbcs 3
724o = gbcro z r zs
725for c ulong n
726oc = add o mul c 4
727gbput oc bitcast uint f32 gbold3 z r c
728if eq ep 1
729gbput add oc 1048576 bitcast uint f32 77.0f64
730end
731if eq ep 2
732gbput add oc 1048576 gbmv z r c
733end
734end
735for c ulong 8
736op = add o mul add n c 4
737gbput op 2143289344
738if ne ep 0
739gbput add op 1048576 2143289344
740end
741end
742end
743
744note operand ab's gkopa for the case at device address p
745fn gbfopa ab uint p ulong to void
746md = get gbcs ab
747k = get gbcs 5
748w = get gbcs add 6 ab
749zs = uint gbzs md ulong w ulong k
750so = mul add k 8 4
751sk = uint 4
752if eq md 0
753so = 4
754sk = mul add w 16 4
755end
756gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4
757end
758
759fn gbflch to bool
760d = add gkbase get gbfb 0
761gbfopa 0 d
762gbfopa 1 add d 1048576
763n = get gbcs 7
764zc = mul get gbcs 6 add n 8
765al = 1.0f64
766if eq get gbcs 9 1
767al = 0.5f64
768end
769sm = mul add n 8 4
770s1 = mul add mul zc 3 64 4
771s2 = mul zc 4
772gkopc add d 2097152 sm 4 s1 s2 f32 al
773if ne get gbcs 3 0
774gkope get gbcs 3 add d 3145728
775end
776if ne get gbcs 4 0
777gkopz get gbcs 4
778end
779m = get gbcs 6
780k = get gbcs 5
781bt = eq get gbcs 2 1
782ret gkmm m n k 6 3 bt
783end
784
785note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN
786fn gbfcmp to ulong
787bad = ulong 0
788for z ulong 6
789for r ulong ulong get gbcs 6
790bad = add bad gbfrow z r
791end
792end
793ret bad
794end
795
796note host byte offset of C row r of block z (row stride n + 8 f32)
797fn gbcro z ulong r ulong zs ulong to ulong
798n = ulong get gbcs 7
799b = add get gbfb 0 2097152
800b = add b mul gbzof z zs 4
801ret add b mul mul r add n 8 4
802end
803
804fn gbfrow z ulong r ulong to ulong
805n = ulong get gbcs 7
806zs = mul ulong get gbcs 6 add n 8
807o = gbcro z r zs
808bad = ulong 0
809for c ulong n
810if not gbfel z r c add o mul c 4
811bad = add bad 1
812end
813end
814for c ulong 8
815op = add o mul add n c 4
816if ne gbw op 2143289344
817bad = add bad 1
818end
819if ne get gbcs 3 0
820if ne gbw add op 1048576 2143289344
821bad = add bad 1
822end
823end
824end
825ret bad
826end
827
828note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2
829note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0
830fn gbfel z ulong r ulong c ulong oc ulong to bool
831cb = gbw oc
832old = gbold3 z r c
833if and eq get gbcs 4 3 gt div c 64 div r 64
834ret eq cb bitcast uint f32 old
835end
836ep = get gbcs 3
837if eq ep 2
838if not gt f64 bitcast f32 gbmv z r c 0.0f64
839ret eq cb 0
840end
841end
842s = gbfref z r c
843if eq get gbcs 2 1
844s = add s old
845end
846if ne f64 bitcast f32 cb s
847ret false
848end
849if eq ep 1
850w = uint 0
851if gt s 0.0f64
852w = cb
853end
854ret eq gbw add oc 1048576 w
855end
856ret true
857end
858
859note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands)
860fn gbfref z ulong r ulong c ulong to f64
861k = ulong get gbcs 5
862lo = ulong 0
863hi = mul add div r 64 1 64
864cz = get gbcs 4
865if and eq cz 1 lt hi k
866k = hi
867end
868if eq cz 2
869lo = mul div r 64 64
870end
871ia = mul add mul z 128 r 128
872s = gbfdot ia mul add mul z 192 c 128 lo sub k lo
873if eq get gbcs 9 1
874s = mul s 0.5f64
875end
876ret s
877end
878
879fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64
880s = 0.0f64
881for j ulong kn
882kk = add lo j
883a = get gbha add ia kk
884s = add s mul a get gbhb add ib kk
885end
886ret s
887end
888
889note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0,
890note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile),
891note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples
892note of 2^-9, so the f32 result is exact. Logits buffer: A at 0, B at 6 MiB, C (rows of 72) at 12 MiB,
893note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`.
894fn gbfl p ulong ma uint mb uint to ulong
895for j ulong 10
896put gbcs j 0
897end
898put gbcs 0 ma
899put gbcs 1 mb
900put gbcs 5 16384
901put gbcs 6 64
902put gbcs 7 64
903put gbcs 8 uint p
904b = get gbfb 0
905for i ulong 3162112
906gbput add b mul i 4 2143289344
907end
908gblw 0
909gblw 1
910put gkinv 0 1
911if not gbllch
912ggdie 98
913end
914bad = ulong 0
915for r ulong 64
916bad = add bad gblrow r
917end
918oputc 71
919oputc 70
920oputc 76
921oputc 32
922oputu p
923oputc 32
924oputu ulong ma
925oputc 32
926oputu ulong mb
927oputc 32
928oputu bad
929oputc 10
930ret bad
931end
932
933note operand ab of the long case: nonzero words only at kk = 0 mod 29
934fn gblw ab uint to void
935md = get gbcs ab
936base = add get gbfb 0 mul ulong ab 6291456
937fr = eq get gbcs 8 ab
938for x ulong 64
939gblwr md base x fr
940end
941end
942
943fn gblwr md uint base ulong x ulong fr bool to void
944w = ulong 64
945kd = ulong 16384
946for kk ulong kd
947u = uint 0
948if eq rem kk 29 0
949u = gbwv x kk 0 fr
950end
951o = gbopf md x kk w kd
952gbput add base mul o 4 u
953end
954end
955
956note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats
957fn gbllch to bool
958d = add gkbase get gbfb 0
959gblopa 0 d
960gblopa 1 add d 6291456
961gkopc add d 12582912 288 4 0 0 f32 1.0f64
962gkopz 0
963ret gkmm 64 64 16384 1 1 false
964end
965
966fn gblopa ab uint p ulong to void
967so = uint 65568
968sk = uint 4
969if eq get gbcs ab 0
970so = 4
971sk = 320
972end
973gkopa ab p so sk 0 0
974end
975
976note row r of the long case: 64 exact values, then 8 padding words that must still be NaN
977fn gblrow r ulong to ulong
978o = add add get gbfb 0 12582912 mul r 288
979bad = ulong 0
980for c ulong 64
981cb = gbw add o mul c 4
982if ne f64 bitcast f32 cb gblref r c
983bad = add bad 1
984end
985end
986for c ulong 8
987if ne gbw add o mul add 64 c 4 2143289344
988bad = add bad 1
989end
990end
991ret bad
992end
993
994note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk)
995fn gblref r ulong c ulong to f64
996fa = eq get gbcs 8 0
997fb = eq get gbcs 8 1
998s = 0.0f64
999for j ulong 565
1000kk = mul j 29
1001a = gbrv gbwv r kk 0 fa
1002s = add s mul a gbrv gbwv c kk 0 fb
1003end
1004ret s
1005end