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

1006 lines · 21856 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
330ockloop 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 (L: the layer applications og 56, which is the layer count when the model has no loop)
364note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV;
365note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic
366fn gbtolb to f64
367nc = f64 add mul 10 get og 56 3
368ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64
369end
370
371note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss
372fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong
373tb = gbtolb
374fl = ulong 0
375if ne nf 0
376fl = or fl 1
377end
378if not lt ggclp rho tb
379fl = or fl 2
380end
381if not lt gg2rl 2 tb
382fl = or fl 4
383end
384if not lt gbabs sub lg l0 mul tb gbabs l0
385fl = or fl 8
386end
387ret fl
388end
389
390note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p))
391global gbcq f64 3
392fn gbcos p ulong to f64
393for k ulong 3
394put gbcq k 0.0f64
395end
396for q ulong ocn p
397i0 = mul q OCH
398for j ulong sub oce q p i0
399gbcq1 add i0 j p
400end
401end
402d = fsqrt mul get gbcq 1 get gbcq 2
403if gt d 0.0f64
404ret ggclp div get gbcq 0 d
405end
406ret 0.0f64
407end
408
409fn gbcq1 i ulong p ulong to void
410x = omvg i
411y = omvg add p i
412put gbcq 0 add get gbcq 0 mul x y
413put gbcq 1 add get gbcq 1 mul x x
414put gbcq 2 add get gbcq 2 mul y y
415end
416
417note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9` and verdict (exit 49)
418fn gbc2vd lg f64 l0 f64 fl ulong to void
419cs = gbcos get og 8
420oputc 71
421oputc 66
422oputc 50
423oputc 32
424oputu get og 56
425oputc 32
426oputu add mul 10 get og 56 3
427oputc 32
428oputl rmic mul gbtolb 1000.0f64
429oputc 32
430oputl rmic mul cs 1000.0f64
431oputc 32
432oputl rmic mul gg2rl 0 1000.0f64
433oputc 32
434oputl rmic mul gg2rl 2 1000.0f64
435oputc 32
436oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64
437oputc 10
438gbvd 49 eq fl 0
439end
440
441note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd)
442fn gbcmp2 to void
443put gkbf 0 1
444ggcmp2
445end
446
447note gtrainb: gtrainf with gkbf 1 (geval stays f32)
448fn gbtrain to void
449put gkbf 0 1
450ggtrainf
451end
452
453note gprofb: gprof with gkbf 1
454fn gbprof to void
455put gkbf 0 1
456ggprof
457end
458
459note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by
460note element against a reference built from the bf16-rounded operands. One operand holds integers in
461note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9
462note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen
463note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles
464note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the
465note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN
466note 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,
467note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL
468note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi).
469note 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
470global gbcs uint 10
471global gbfb ulong 1
472global gbha f64 98304
473global gbhb f64 147456
474
475fn gbfrag to void
476if ne oargc 3
477exit 2
478end
479ockloop 2
480ggopen
481if lt mul ggr get og 0 4194304
482ggdie 3
483end
484put gkbf 0 1
485put gkbck 0 0
486put gbfb 0 sub gga1 11 gkbase
487bad = ulong 0
488for p ulong 2
489for i ulong 13
490bad = add bad gbf1 i p
491end
492end
493for p ulong 2
494for q uint 4
495bad = add bad gbfl p and q 1 shr q 1
496end
497end
498r = nvstop
499oputc 71
500oputc 70
501oputc 83
502oputc 32
503oputu bad
504oputc 10
505gbvd 50 eq bad 0
506end
507
508note case i of pass p into gbcs
509fn gbcase i ulong p ulong to void
510for j ulong 10
511put gbcs j 0
512end
513put gbcs 8 uint p
514put gbcs 6 128
515put gbcs 7 192
516put gbcs 5 128
517if eq rem i 3 0
518put gbcs 5 32
519end
520if eq rem i 3 1
521put gbcs 5 96
522put gbcs 9 1
523end
524if lt i 8
525put gbcs 0 uint and i 1
526put gbcs 1 uint and shr i 1 1
527put gbcs 2 uint and shr i 2 1
528ret
529end
530put gbcs 9 0
531put gbcs 0 1
532if eq i 8
533put gbcs 3 1
534put gbcs 5 96
535end
536if eq i 9
537put gbcs 1 1
538put gbcs 3 2
539end
540note cz 1 needs k = m
541if eq i 10
542put gbcs 4 1
543put gbcs 5 128
544end
545if eq i 11
546put gbcs 0 0
547put gbcs 2 1
548put gbcs 4 2
549end
550if eq i 12
551put gbcs 1 1
552put gbcs 4 3
553put gbcs 5 32
554put gbcs 7 128
555put gbcs 9 1
556end
557end
558
559note one case: write the data, launch, compare; the mismatch count
560fn gbf1 i ulong p ulong to ulong
561gbcase i p
562gbnan
563gbwop 0
564gbwop 1
565gbwc
566put gkinv 0 1
567if not gbflch
568ggdie 98
569end
570if eq OMUTN 0
571n = ulong get gbcs 7
572gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0
573end
574b = gbfcmp
575ep = ulong get gbcs 3
576id = add 27 ulong get gbcs 0
577id = add id mul 2 ulong get gbcs 1
578id = add id mul 4 ulong get gbcs 2
579if ne ep 0
580id = add 34 ep
581end
582oputc 71
583oputc 70
584oputc 32
585oputu p
586oputc 32
587oputu i
588oputc 32
589oputu id
590oputc 32
591oputu ulong get gbcs 5
592oputc 32
593oputu ulong get gbcs 4
594oputc 32
595oputu ep
596oputc 32
597oputu b
598oputc 10
599ret b
600end
601
602note NaN in every A and B word (2 MiB)
603fn gbnan to void
604b = get gbfb 0
605for i ulong 524288
606gbput add b mul i 4 2143289344
607end
608end
609
610note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B);
611note batch z at (z / 3)(3 zs + 64) + (z % 3) zs
612fn gbzs md uint w ulong kd ulong to ulong
613if eq md 1
614ret mul w add kd 8
615end
616ret mul kd add w 16
617end
618
619fn gbzof z ulong zs ulong to ulong
620ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs
621end
622
623fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong
624if eq md 1
625ret add mul x add kd 8 kk
626end
627ret add x mul kk add w 16
628end
629
630note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z)
631fn gbfr x ulong kk ulong z ulong to f64
632s = add mul x 7 mul kk 3
633s = add s mul z 5
634a = sub f64 rem s 5 2.0f64
635s = add mul x 13 mul kk 11
636s = add s mul z 3
637b = f64 rem s 64
638ret add a div b 512.0f64
639end
640
641fn gbint x ulong kk ulong z ulong to f64
642s = add mul x 5 mul kk 7
643s = add s mul z 3
644ret sub f64 rem s 11 5.0f64
645end
646
647note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded
648note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped.
649fn gbwop ab uint to void
650md = get gbcs ab
651k = ulong get gbcs 5
652w = ulong get gbcs add 6 ab
653zs = gbzs md w k
654base = add get gbfb 0 mul ulong ab 1048576
655fr = eq get gbcs 8 ab
656for z ulong 6
657for x ulong w
658gbwrow ab z x base zs fr
659end
660end
661end
662
663fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void
664zb = add base mul gbzof z zs 4
665t = add 128 mul 64 ulong ab
666hi = mul add mul z t x 128
667k = ulong get gbcs 5
668for kk ulong k
669gbwel ab zb x kk gbwv x kk z fr hi
670end
671end
672
673note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer
674fn gbwv x ulong kk ulong z ulong fr bool to uint
675v = gbint x kk z
676if fr
677v = gbfr x kk z
678end
679ret bitcast uint f32 v
680end
681
682note store bits u of operand ab at (x, kk) of the z block at zb; keep its bf16 value at gbha / gbhb hi + kk
683fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void
684md = get gbcs ab
685w = ulong get gbcs add 6 ab
686o = gbopf md x kk w ulong get gbcs 5
687gbput add zb mul o 4 u
688if eq ab 0
689put gbha add hi kk gbrv u
690else
691put gbhb add hi kk gbrv u
692end
693end
694
695note old C ((3 r + 5 c + z) mod 17) - 8, and the epilogue-2 mask M: NaN when (5 r + c) mod 13 = 0,
696note else ((r + 3 c + z) mod 3) - 1, as f32 bits
697fn gbold3 z ulong r ulong c ulong to f64
698s = add mul r 3 mul c 5
699s = add s z
700ret sub f64 rem s 17 8.0f64
701end
702
703fn gbmv z ulong r ulong c ulong to uint
704if eq rem add mul r 5 c 13 0
705ret 2143289344
706end
707ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64
708end
709
710note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns
711note (and Y's / M's with a fused epilogue) are NaN
712fn gbwc to void
713m = ulong get gbcs 6
714zs = mul m add ulong get gbcs 7 8
715for z ulong 6
716for r ulong m
717gbwcr z r zs
718end
719end
720end
721
722fn gbwcr z ulong r ulong zs ulong to void
723n = ulong get gbcs 7
724ep = get gbcs 3
725o = gbcro z r zs
726for c ulong n
727oc = add o mul c 4
728gbput oc bitcast uint f32 gbold3 z r c
729if eq ep 1
730gbput add oc 1048576 bitcast uint f32 77.0f64
731end
732if eq ep 2
733gbput add oc 1048576 gbmv z r c
734end
735end
736for c ulong 8
737op = add o mul add n c 4
738gbput op 2143289344
739if ne ep 0
740gbput add op 1048576 2143289344
741end
742end
743end
744
745note operand ab's gkopa for the case at device address p
746fn gbfopa ab uint p ulong to void
747md = get gbcs ab
748k = get gbcs 5
749w = get gbcs add 6 ab
750zs = uint gbzs md ulong w ulong k
751so = mul add k 8 4
752sk = uint 4
753if eq md 0
754so = 4
755sk = mul add w 16 4
756end
757gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4
758end
759
760fn gbflch to bool
761d = add gkbase get gbfb 0
762gbfopa 0 d
763gbfopa 1 add d 1048576
764n = get gbcs 7
765zc = mul get gbcs 6 add n 8
766al = 1.0f64
767if eq get gbcs 9 1
768al = 0.5f64
769end
770sm = mul add n 8 4
771s1 = mul add mul zc 3 64 4
772s2 = mul zc 4
773gkopc add d 2097152 sm 4 s1 s2 f32 al
774if ne get gbcs 3 0
775gkope get gbcs 3 add d 3145728
776end
777if ne get gbcs 4 0
778gkopz get gbcs 4
779end
780m = get gbcs 6
781k = get gbcs 5
782bt = eq get gbcs 2 1
783ret gkmm m n k 6 3 bt
784end
785
786note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN
787fn gbfcmp to ulong
788bad = ulong 0
789for z ulong 6
790for r ulong ulong get gbcs 6
791bad = add bad gbfrow z r
792end
793end
794ret bad
795end
796
797note host byte offset of C row r of block z (row stride n + 8 f32)
798fn gbcro z ulong r ulong zs ulong to ulong
799n = ulong get gbcs 7
800b = add get gbfb 0 2097152
801b = add b mul gbzof z zs 4
802ret add b mul mul r add n 8 4
803end
804
805fn gbfrow z ulong r ulong to ulong
806n = ulong get gbcs 7
807zs = mul ulong get gbcs 6 add n 8
808o = gbcro z r zs
809bad = ulong 0
810for c ulong n
811if not gbfel z r c add o mul c 4
812bad = add bad 1
813end
814end
815for c ulong 8
816op = add o mul add n c 4
817if ne gbw op 2143289344
818bad = add bad 1
819end
820if ne get gbcs 3 0
821if ne gbw add op 1048576 2143289344
822bad = add bad 1
823end
824end
825end
826ret bad
827end
828
829note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2
830note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0
831fn gbfel z ulong r ulong c ulong oc ulong to bool
832cb = gbw oc
833old = gbold3 z r c
834if and eq get gbcs 4 3 gt div c 64 div r 64
835ret eq cb bitcast uint f32 old
836end
837ep = get gbcs 3
838if eq ep 2
839if not gt f64 bitcast f32 gbmv z r c 0.0f64
840ret eq cb 0
841end
842end
843s = gbfref z r c
844if eq get gbcs 2 1
845s = add s old
846end
847if ne f64 bitcast f32 cb s
848ret false
849end
850if eq ep 1
851w = uint 0
852if gt s 0.0f64
853w = cb
854end
855ret eq gbw add oc 1048576 w
856end
857ret true
858end
859
860note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands)
861fn gbfref z ulong r ulong c ulong to f64
862k = ulong get gbcs 5
863lo = ulong 0
864hi = mul add div r 64 1 64
865cz = get gbcs 4
866if and eq cz 1 lt hi k
867k = hi
868end
869if eq cz 2
870lo = mul div r 64 64
871end
872ia = mul add mul z 128 r 128
873s = gbfdot ia mul add mul z 192 c 128 lo sub k lo
874if eq get gbcs 9 1
875s = mul s 0.5f64
876end
877ret s
878end
879
880fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64
881s = 0.0f64
882for j ulong kn
883kk = add lo j
884a = get gbha add ia kk
885s = add s mul a get gbhb add ib kk
886end
887ret s
888end
889
890note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0,
891note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile),
892note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples
893note 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,
894note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`.
895fn gbfl p ulong ma uint mb uint to ulong
896for j ulong 10
897put gbcs j 0
898end
899put gbcs 0 ma
900put gbcs 1 mb
901put gbcs 5 16384
902put gbcs 6 64
903put gbcs 7 64
904put gbcs 8 uint p
905b = get gbfb 0
906for i ulong 3162112
907gbput add b mul i 4 2143289344
908end
909gblw 0
910gblw 1
911put gkinv 0 1
912if not gbllch
913ggdie 98
914end
915bad = ulong 0
916for r ulong 64
917bad = add bad gblrow r
918end
919oputc 71
920oputc 70
921oputc 76
922oputc 32
923oputu p
924oputc 32
925oputu ulong ma
926oputc 32
927oputu ulong mb
928oputc 32
929oputu bad
930oputc 10
931ret bad
932end
933
934note operand ab of the long case: nonzero words only at kk = 0 mod 29
935fn gblw ab uint to void
936md = get gbcs ab
937base = add get gbfb 0 mul ulong ab 6291456
938fr = eq get gbcs 8 ab
939for x ulong 64
940gblwr md base x fr
941end
942end
943
944fn gblwr md uint base ulong x ulong fr bool to void
945w = ulong 64
946kd = ulong 16384
947for kk ulong kd
948u = uint 0
949if eq rem kk 29 0
950u = gbwv x kk 0 fr
951end
952o = gbopf md x kk w kd
953gbput add base mul o 4 u
954end
955end
956
957note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats
958fn gbllch to bool
959d = add gkbase get gbfb 0
960gblopa 0 d
961gblopa 1 add d 6291456
962gkopc add d 12582912 288 4 0 0 f32 1.0f64
963gkopz 0
964ret gkmm 64 64 16384 1 1 false
965end
966
967fn gblopa ab uint p ulong to void
968so = uint 65568
969sk = uint 4
970if eq get gbcs ab 0
971so = 4
972sk = 320
973end
974gkopa ab p so sk 0 0
975end
976
977note row r of the long case: 64 exact values, then 8 padding words that must still be NaN
978fn gblrow r ulong to ulong
979o = add add get gbfb 0 12582912 mul r 288
980bad = ulong 0
981for c ulong 64
982cb = gbw add o mul c 4
983if ne f64 bitcast f32 cb gblref r c
984bad = add bad 1
985end
986end
987for c ulong 8
988if ne gbw add o mul add 64 c 4 2143289344
989bad = add bad 1
990end
991end
992ret bad
993end
994
995note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk)
996fn gblref r ulong c ulong to f64
997fa = eq get gbcs 8 0
998fb = eq get gbcs 8 1
999s = 0.0f64
1000for j ulong 565
1001kk = mul j 29
1002a = gbrv gbwv r kk 0 fa
1003s = add s mul a gbrv gbwv c kk 0 fb
1004end
1005ret s
1006end