interplane.Get Omega
Read in your language

omega-helix

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-helix / native / gpu / gb.om

1894 lines · 38761 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 gkmm / gkmmsw sets gklt)
267fn gbid to ulong
268ret ulong get gklt 0
269end
270
271note gkmm's hook after a checked BF16 launch: check the samples, print
272note `GB id m n k nz h2 beta ep cz worst_e6 bad`; true (the step goes on, gbcmp gives the verdict)
273fn gbpost to bool
274bad = ulong 0
275mr = 0.0f64
276for i ulong 66
277q = gbchk i
278if gt q 1.0f64
279bad = add bad 1
280end
281if gt q mr
282mr = q
283end
284end
285gbnote bad mr
286ret true
287end
288
289fn gbnote bad ulong mr f64 to void
290id = gbid
291put gbcst 0 add get gbcst 0 1
292put gbcst 1 add get gbcst 1 bad
293put gbcst 2 or get gbcst 2 shl ulong 1 id
294put gbcst 3 or get gbcst 3 shl ulong 1 ulong get gkbp 7
295put gbcst 4 or get gbcst 4 shl ulong 1 ulong get gkbp 6
296put gbcst 5 or get gbcst 5 shl ulong 1 ulong get gkbp 5
297if gt mr get gbmx 0
298put gbmx 0 mr
299end
300oputc 71
301oputc 66
302oputc 32
303oputu id
304for j ulong 8
305oputc 32
306oputu ulong get gkbp j
307end
308oputc 32
309oputl rmic mr
310oputc 32
311oputu bad
312oputc 10
313end
314
315note gbcmp CK DATA S: one gtrainf step's gradient pass (gggrad, no Adam) with gkbf 1 and gkbck 1, so
316note gk.om checks every BF16 launch (gbpre, gbpost). Prints the GB lines, `GBC gemms bad worst_e6
317note idmask czmask epmask betamask` and GBCMP PASS, or GBCMP FAIL (exit 48): a sample past its bound,
318note no BF16 GEMM, or coverage short of the step's BF16 combinations (epilogues 1 and 2, cz 3, beta
319note false and true).
320fn gbcmp to void
321if ne oargc 5
322exit 2
323end
324ockloop 2
325odopen 3
326obatch oargu 4
327put og 10 0
328put gbcst 6 12345
329put gkbf 0 1
330put gkbck 0 1
331ggopen
332l = gggrad
333put gkbck 0 0
334r = nvstop
335oputc 71
336oputc 66
337oputc 67
338for j ulong 2
339oputc 32
340oputu get gbcst j
341end
342oputc 32
343oputl rmic get gbmx 0
344for j ulong 4
345oputc 32
346oputu get gbcst add j 2
347end
348oputc 10
349ok = and eq get gbcst 1 0 gt get gbcst 0 0
350cv = eq and get gbcst 4 6 6
351cv = and cv gbcz3
352cv = and cv eq get gbcst 5 3
353gbvd 48 and ok cv
354end
355
356note gbcmp's cz 3 coverage: a BF16 causal score GEMM (S = Q K^T or DP = DO U^T, m = n = T) runs only
357note on the 64 x 64 tile, so it is required only when T is a multiple of 64; at T 32 (the small shape
358note of docs/LOOP.md) both stay f32 and the requirement could never be met
359fn gbcz3 to bool
360if ne rem get og 1 64 0
361ret true
362end
363ret ne and get gbcst 3 8 0
364end
365
366note gbcmp2 bound: tolb = n_c (2u + u^2), u = 2^-8, n_c = 10 L + 3 BF16 GEMMs on a step's longest
367note (L: the layer applications og 56, which is the layer count when the model has no loop)
368note dependency chain (per layer forward QKV, S, WO, W1, W2; backward dF, dA of W1, dN, DP, dA of QKV;
369note plus the logits, dlogits to dH and the last weight gradient); a first-order normwise heuristic
370fn gbtolb to f64
371nc = f64 add mul 10 get og 56 3
372ret mul nc add div 1.0f64 128.0f64 div 1.0f64 65536.0f64
373end
374
375note gcmp2's fail bits under gkbf 1: 1 non-finite, 2 rho >= tolb, 4 rel1 >= tolb, 8 |dloss| >= tolb loss,
376note 16 and 32 the per-tensor check (gbtens)
377fn gbc2fl lg f64 l0 f64 rho f64 nf ulong to ulong
378tb = gbtolb
379fl = ulong 0
380if ne nf 0
381fl = or fl 1
382end
383if not lt ggclp rho tb
384fl = or fl 2
385end
386if not lt gg2rl 2 tb
387fl = or fl 4
388end
389if not lt gbabs sub lg l0 mul tb gbabs l0
390fl = or fl 8
391end
392ret or fl gbtens get og 8
393end
394
395note gbcmp2's per-tensor check. gbtolb is a normwise bound over the whole gradient that grows with the
396note applications (0.65 at 8, 1.28 at 16): it cannot catch a wrong gradient. For every parameter
397note tensor k (0 wte, 1 wpe, 2 the final gain, then g1 wq wk wv wo g2 w1 w2 of each parameter layer)
398note the chip gradient x is compared with K0 (CPU f64, its own ReLU mask, omv[P, 2P)) and K1 (CPU f64,
399note the chip's ReLU mask, GRD): cos = x.y / (|x| |y|) and rel = |x - y| / |y|. Fail bit 16: a
400note cosine below GBCOS 0.995; 32: a rel at or above GBREL 0.1 = sqrt(2 (1 - 0.995)), the normwise
401note error at which an error perpendicular to y alone brings the cosine down to 0.995. rel catches
402note what the cosine cannot: a gradient that keeps its direction and loses scale, as in mutant lg,
403note where a core w2 keeps one of its NP passes.
404const GBCOS f64 0.995
405const GBREL f64 0.1
406note gbta: sums over the tensor in flight, 0 x.y0 1 x.x 2 y0.y0 3 |x - y0|^2 4 x.y1 5 y1.y1
407note 6 |x - y1|^2; gbtw: 0 least cos0, 1 largest rel0, 2 least cos1, 3 largest rel1, 4 tensor of
408note rel0, 5 tensor of rel1; gbtf: the fail bits
409global gbta f64 7
410global gbtw f64 6
411global gbtf ulong 1
412fn gbtens p ulong to ulong
413put gbtf 0 0
414put gbtw 0 2.0f64
415put gbtw 1 0.0f64
416put gbtw 2 2.0f64
417put gbtw 3 0.0f64
418put gbtw 4 0.0f64
419put gbtw 5 0.0f64
420for k ulong gbtn
421gbt1 k p
422end
423ret get gbtf 0
424end
425
426fn gbtn to ulong
427ret add 3 mul 8 get og 5
428end
429
430note offset of tensor j inside a parameter layer (model.om opg1 .. opw2); j 8 is the layer's end
431fn gbjo j ulong to ulong
432d = get og 2
433dd = mul d d
434if eq j 0
435ret 0
436end
437if le j 5
438ret add d mul sub j 1 dd
439end
440if eq j 6
441ret add mul 2 d mul 4 dd
442end
443if eq j 7
444ret add add mul 2 d mul 4 dd mul d get og 4
445end
446ret get og 32
447end
448
449note first parameter of tensor k, and (w 1) the one after its last
450fn gbtb k ulong w ulong to ulong
451if eq k 0
452ret mul w get og 30
453end
454if eq k 1
455ret get og add 30 w
456end
457if eq k 2
458ret add get og 33 mul w get og 2
459end
460q = sub k 3
461ret add opg1 div q 8 gbjo add rem q 8 w
462end
463
464fn gbt1 k ulong p ulong to void
465for j ulong 7
466put gbta j 0.0f64
467end
468i = gbtb k 0
469e = gbtb k 1
470while lt i e bound 9223372036854775807
471gbta1 i p
472i = add i 1
473end
474gbtacc k
475end
476
477fn gbta1 i ulong p ulong to void
478x = omvg i
479y = omvg add p i
480z = ocsg i
481put gbta 0 add get gbta 0 mul x y
482put gbta 1 add get gbta 1 mul x x
483put gbta 2 add get gbta 2 mul y y
484y = sub x y
485put gbta 3 add get gbta 3 mul y y
486put gbta 4 add get gbta 4 mul x z
487put gbta 5 add get gbta 5 mul z z
488z = sub x z
489put gbta 6 add get gbta 6 mul z z
490end
491
492note cosine x.y / sqrt(x.x y.y): 1 when both are zero, 0 when one is
493fn gbcsn xy f64 xx f64 yy f64 to f64
494if and eq xx 0.0f64 eq yy 0.0f64
495ret 1.0f64
496end
497d = fsqrt mul xx yy
498if gt d 0.0f64
499ret div xy d
500end
501ret 0.0f64
502end
503
504fn gbtacc k ulong to void
505c0 = gbcsn get gbta 0 get gbta 1 get gbta 2
506c1 = gbcsn get gbta 4 get gbta 1 get gbta 5
507r0 = gbrat fsqrt get gbta 3 fsqrt get gbta 2
508r1 = gbrat fsqrt get gbta 6 fsqrt get gbta 5
509if or not ge c0 GBCOS not ge c1 GBCOS
510put gbtf 0 or get gbtf 0 16
511end
512if or not lt r0 GBREL not lt r1 GBREL
513put gbtf 0 or get gbtf 0 32
514end
515gbtlo 0 c0
516gbthi 1 r0 k
517gbtlo 2 c1
518gbthi 3 r1 k
519end
520
521note gbtw[j] = x when x is below it (j 0, 2: the least cosines)
522fn gbtlo j ulong x f64 to void
523if lt x get gbtw j
524put gbtw j x
525end
526end
527
528note gbtw[j] = x when x is above it (j 1, 3: the largest rels), and its tensor k in gbtw[4 + j / 2]
529fn gbthi j ulong x f64 k ulong to void
530if gt x get gbtw j
531put gbtw j x
532put gbtw add 4 div j 2 f64 k
533end
534end
535
536note `GBT tensors cos0_e9 rel0_e9 k0 cos1_e9 rel1_e9 k1 bits`
537fn gbtpr to void
538oputc 71
539oputc 66
540oputc 84
541oputc 32
542oputu gbtn
543for h ulong 2
544gbtp1 h
545end
546oputc 32
547oputu get gbtf 0
548oputc 10
549end
550
551fn gbtp1 h ulong to void
552o = mul h 2
553gg2pe ggclp get gbtw o
554gg2pe ggclp get gbtw add o 1
555oputc 32
556oputu ulong get gbtw add 4 h
557end
558
559note cosine of the chip gradient (omv[0, p)) and K0 (omv[p, 2p))
560global gbcq f64 3
561fn gbcos p ulong to f64
562for k ulong 3
563put gbcq k 0.0f64
564end
565for q ulong ocn p
566i0 = mul q OCH
567for j ulong sub oce q p i0
568gbcq1 add i0 j p
569end
570end
571d = fsqrt mul get gbcq 1 get gbcq 2
572if gt d 0.0f64
573ret ggclp div get gbcq 0 d
574end
575ret 0.0f64
576end
577
578fn gbcq1 i ulong p ulong to void
579x = omvg i
580y = omvg add p i
581put gbcq 0 add get gbcq 0 mul x y
582put gbcq 1 add get gbcq 1 mul x x
583put gbcq 2 add get gbcq 2 mul y y
584end
585
586note gbcmp2's line `GB2 L n_c tolb_e9 cos_e9 rel0_e9 rel1_e9 dlossrel_e9`, the per-tensor line `GBT`
587note (gbtpr) and verdict (exit 49)
588fn gbc2vd lg f64 l0 f64 fl ulong to void
589cs = gbcos get og 8
590oputc 71
591oputc 66
592oputc 50
593oputc 32
594oputu get og 56
595oputc 32
596oputu add mul 10 get og 56 3
597oputc 32
598oputl rmic mul gbtolb 1000.0f64
599oputc 32
600oputl rmic mul cs 1000.0f64
601oputc 32
602oputl rmic mul gg2rl 0 1000.0f64
603oputc 32
604oputl rmic mul gg2rl 2 1000.0f64
605oputc 32
606oputl rmic mul ggclp div gbabs sub lg l0 gbabs l0 1000.0f64
607oputc 10
608gbtpr
609gbvd 49 eq fl 0
610end
611
612note gbcmp2 CK DATA S: gcmp2 with gkbf 1 (gg.om ggc2fl and ggc2pr branch to gbc2fl and gbc2vd)
613fn gbcmp2 to void
614put gkbf 0 1
615ggcmp2
616end
617
618note gtrainb CK TRAIN UNTIL EVERY [FREEZE_LAYERS]: gtrainf with gkbf 1 (geval stays f32); the frozen
619note core (gg.om ggfz) is the same on BF16: the frozen layers launch no BF16 GEMM at all.
620note When OLLM_NVFP4=1 is set in the environment, routes eligible 128x128x64 GEMMs through NVFP4.
621fn gbtrain to void
622if ggnv4_env
623ggsnv4 1
624end
625put gkbf 0 1
626ggtrainf
627ggsnv4 0
628end
629
630note gtrain4 CK TRAIN UNTIL EVERY [FREEZE_LAYERS]: gtrainb with NVFP4 (OMMA.SF.16864) enabled
631fn gbtrain4 to void
632ggsnv4 1
633put gkbf 0 1
634ggtrainf
635ggsnv4 0
636end
637
638note gprofb: gprof with gkbf 1
639fn gbprof to void
640if ggnv4_env
641ggsnv4 1
642end
643put gkbf 0 1
644ggprof
645ggsnv4 0
646end
647
648note gbfrag CK: every BF16 kernel (ids 27..36) on fixed data with exact f32 sums, compared element by
649note element against a reference built from the bf16-rounded operands. One operand holds integers in
650note [-5, 5], the other values (n - 2) + j 2^-9 (n < 5, j < 64), so every product is a multiple of 2^-9
651note (2^-10 with alpha 1/2) and every sum stays below 2^11: the chip's f32 result is exact. Thirteen
652note cases (eight plain kernels, epilogues 1 and 2, cz 1, 2 and 3), each with the operand roles
653note swapped; nz 6, h2 3, padded strides, NaN in every operand word the kernel must not read and in the
654note eight padding columns of every C row (and of Y / M with a fused epilogue), which must still be NaN
655note 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,
656note then the eight long-K cases (gbfl, `GFL pass ma mb bad`), `GFS bad` and GBFRAG PASS, or GBFRAG FAIL
657note (exit 50). Needs a logits buffer of at least 4 Mi floats (B 8: 8 Mi).
658note 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
659global gbcs uint 10
660global gbfb ulong 1
661global gbha f64 98304
662global gbhb f64 147456
663
664fn gbfrag to void
665if ne oargc 3
666exit 2
667end
668ockloop 2
669ggopen
670if lt mul ggr get og 0 4194304
671ggdie 3
672end
673put gkbf 0 1
674put gkbck 0 0
675put gbfb 0 sub gga1 11 gkbase
676bad = ulong 0
677for p ulong 2
678for i ulong 13
679bad = add bad gbf1 i p
680end
681end
682for i ulong 10
683bad = add bad gbf128 i
684end
685for p ulong 2
686for q uint 4
687bad = add bad gbfl p and q 1 shr q 1
688end
689end
690bad = add bad gbf85
691r = nvstop
692oputc 71
693oputc 70
694oputc 83
695oputc 32
696oputu bad
697oputc 10
698gbvd 50 eq bad 0
699end
700
701note case i of pass p into gbcs
702fn gbcase i ulong p ulong to void
703for j ulong 10
704put gbcs j 0
705end
706put gbcs 8 uint p
707put gbcs 6 128
708put gbcs 7 192
709put gbcs 5 128
710if eq rem i 3 0
711put gbcs 5 32
712end
713if eq rem i 3 1
714put gbcs 5 96
715put gbcs 9 1
716end
717if lt i 8
718put gbcs 0 uint and i 1
719put gbcs 1 uint and shr i 1 1
720put gbcs 2 uint and shr i 2 1
721ret
722end
723put gbcs 9 0
724put gbcs 0 1
725if eq i 8
726put gbcs 3 1
727put gbcs 5 96
728end
729if eq i 9
730put gbcs 1 1
731put gbcs 3 2
732end
733note cz 1 needs k = m
734if eq i 10
735put gbcs 4 1
736put gbcs 5 128
737end
738if eq i 11
739put gbcs 0 0
740put gbcs 2 1
741put gbcs 4 2
742end
743if eq i 12
744put gbcs 1 1
745put gbcs 4 3
746put gbcs 5 32
747put gbcs 7 128
748put gbcs 9 1
749end
750end
751
752note one case: write the data, launch, compare; the mismatch count
753fn gbf1 i ulong p ulong to ulong
754gbcase i p
755gbnan
756gbwop 0
757gbwop 1
758gbwc
759put gkinv 0 1
760if not gbflch
761ggdie 98
762end
763if eq OMUTN 0
764n = ulong get gbcs 7
765gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0
766end
767b = gbfcmp
768ep = ulong get gbcs 3
769id = add 27 ulong get gbcs 0
770id = add id mul 2 ulong get gbcs 1
771id = add id mul 4 ulong get gbcs 2
772if ne ep 0
773id = add 34 ep
774end
775oputc 71
776oputc 70
777oputc 32
778oputu p
779oputc 32
780oputu i
781oputc 32
782oputu id
783oputc 32
784oputu ulong get gbcs 5
785oputc 32
786oputu ulong get gbcs 4
787oputc 32
788oputu ep
789oputc 32
790oputu b
791oputc 10
792ret b
793end
794
795note Exact check of all 10 128x128 BF16 Tensor Core kernels (m=128, n=128, cz=0, nz=6 h2=3, NaN padding)
796fn gbf128 i ulong to ulong
797gbcase i 0
798put gbcs 7 128
799gbnan
800gbwop 0
801gbwop 1
802gbwc
803put gkinv 0 1
804if not gbflch
805ggdie 98
806end
807if eq OMUTN 0
808n = ulong get gbcs 7
809gbput add gbcro 0 0 mul ulong get gbcs 6 add n 8 mul n 4 0
810end
811b = gbfcmp
812oputc 71
813oputc 70
814oputc 32
815oputu 2
816oputc 32
817oputu i
818oputc 32
819oputu ulong get gklt 0
820oputc 32
821oputu ulong get gbcs 5
822oputc 32
823oputu ulong get gbcs 4
824oputc 32
825oputu ulong get gbcs 3
826oputc 32
827oputu b
828oputc 10
829ret b
830end
831
832note NaN in every A and B word (2 MiB)
833fn gbnan to void
834b = get gbfb 0
835for i ulong 524288
836gbput add b mul i 4 2143289344
837end
838end
839
840note operand layout in floats: mode 1 rows of k + 8, mode 0 rows of w + 16 (w = m for A, n for B);
841note batch z at (z / 3)(3 zs + 64) + (z % 3) zs
842fn gbzs md uint w ulong kd ulong to ulong
843if eq md 1
844ret mul w add kd 8
845end
846ret mul kd add w 16
847end
848
849fn gbzof z ulong zs ulong to ulong
850ret add mul div z 3 add mul zs 3 64 mul rem z 3 zs
851end
852
853fn gbopf md uint x ulong kk ulong w ulong kd ulong to ulong
854if eq md 1
855ret add mul x add kd 8 kk
856end
857ret add x mul kk add w 16
858end
859
860note the fractional value (n - 2) + j 2^-9 and the integer value at (x, kk, z)
861fn gbfr x ulong kk ulong z ulong to f64
862s = add mul x 7 mul kk 3
863s = add s mul z 5
864a = sub f64 rem s 5 2.0f64
865s = add mul x 13 mul kk 11
866s = add s mul z 3
867b = f64 rem s 64
868ret add a div b 512.0f64
869end
870
871fn gbint x ulong kk ulong z ulong to f64
872s = add mul x 5 mul kk 7
873s = add s mul z 3
874ret sub f64 rem s 11 5.0f64
875end
876
877note write operand ab (0 A, 1 B) for z < 6, x < m (A) or n (B), kk < k; keep the bf16-rounded
878note values in gbha / gbhb (index (z rows + x) 128 + kk). Pass 0: A fractional, B integer; pass 1 swapped.
879fn gbwop ab uint to void
880md = get gbcs ab
881k = ulong get gbcs 5
882w = ulong get gbcs add 6 ab
883zs = gbzs md w k
884base = add get gbfb 0 mul ulong ab 1048576
885fr = eq get gbcs 8 ab
886for z ulong 6
887for x ulong w
888gbwrow ab z x base zs fr
889end
890end
891end
892
893fn gbwrow ab uint z ulong x ulong base ulong zs ulong fr bool to void
894zb = add base mul gbzof z zs 4
895t = add 128 mul 64 ulong ab
896hi = mul add mul z t x 128
897k = ulong get gbcs 5
898for kk ulong k
899gbwel ab zb x kk gbwv x kk z fr hi
900end
901end
902
903note operand value at (x, kk, z) as f32 bits: fractional when fr, else integer
904fn gbwv x ulong kk ulong z ulong fr bool to uint
905v = gbint x kk z
906if fr
907v = gbfr x kk z
908end
909ret bitcast uint f32 v
910end
911
912note store bits u of operand ab at (x, kk) of the z block at zb; keep its bf16 value at gbha / gbhb hi + kk
913fn gbwel ab uint zb ulong x ulong kk ulong u uint hi ulong to void
914md = get gbcs ab
915w = ulong get gbcs add 6 ab
916o = gbopf md x kk w ulong get gbcs 5
917gbput add zb mul o 4 u
918if eq ab 0
919put gbha add hi kk gbrv u
920else
921put gbhb add hi kk gbrv u
922end
923end
924
925note old C ((3 r + 5 c + z) mod 17) - 8, and the epilogue-2 mask M: NaN when (5 r + c) mod 13 = 0,
926note else ((r + 3 c + z) mod 3) - 1, as f32 bits
927fn gbold3 z ulong r ulong c ulong to f64
928s = add mul r 3 mul c 5
929s = add s z
930ret sub f64 rem s 17 8.0f64
931end
932
933fn gbmv z ulong r ulong c ulong to uint
934if eq rem add mul r 5 c 13 0
935ret 2143289344
936end
937ret bitcast uint f32 sub f64 rem add add r mul c 3 z 3 1.0f64
938end
939
940note C (old values) and Y: 77 for epilogue 1, M for epilogue 2; C rows of n + 8, whose 8 padding columns
941note (and Y's / M's with a fused epilogue) are NaN
942fn gbwc to void
943m = ulong get gbcs 6
944zs = mul m add ulong get gbcs 7 8
945for z ulong 6
946for r ulong m
947gbwcr z r zs
948end
949end
950end
951
952fn gbwcr z ulong r ulong zs ulong to void
953n = ulong get gbcs 7
954ep = get gbcs 3
955o = gbcro z r zs
956for c ulong n
957oc = add o mul c 4
958gbput oc bitcast uint f32 gbold3 z r c
959if eq ep 1
960gbput add oc 1048576 bitcast uint f32 77.0f64
961end
962if eq ep 2
963gbput add oc 1048576 gbmv z r c
964end
965end
966for c ulong 8
967op = add o mul add n c 4
968gbput op 2143289344
969if ne ep 0
970gbput add op 1048576 2143289344
971end
972end
973end
974
975note operand ab's gkopa for the case at device address p
976fn gbfopa ab uint p ulong to void
977md = get gbcs ab
978k = get gbcs 5
979w = get gbcs add 6 ab
980zs = uint gbzs md ulong w ulong k
981so = mul add k 8 4
982sk = uint 4
983if eq md 0
984so = 4
985sk = mul add w 16 4
986end
987gkopa ab p so sk mul add mul zs 3 64 4 mul zs 4
988end
989
990fn gbflch to bool
991d = add gkbase get gbfb 0
992gbfopa 0 d
993gbfopa 1 add d 1048576
994n = get gbcs 7
995zc = mul get gbcs 6 add n 8
996al = 1.0f64
997if eq get gbcs 9 1
998al = 0.5f64
999end
1000sm = mul add n 8 4
1001s1 = mul add mul zc 3 64 4
1002s2 = mul zc 4
1003gkopc add d 2097152 sm 4 s1 s2 f32 al
1004if ne get gbcs 3 0
1005gkope get gbcs 3 add d 3145728
1006end
1007if ne get gbcs 4 0
1008gkopz get gbcs 4
1009end
1010m = get gbcs 6
1011k = get gbcs 5
1012bt = eq get gbcs 2 1
1013ret gkmm m n k 6 3 bt
1014end
1015
1016note mismatches over every C element of the case (and Y for epilogue 1), plus padding words no longer NaN
1017fn gbfcmp to ulong
1018bad = ulong 0
1019for z ulong 6
1020for r ulong ulong get gbcs 6
1021bad = add bad gbfrow z r
1022end
1023end
1024ret bad
1025end
1026
1027note host byte offset of C row r of block z (row stride n + 8 f32)
1028fn gbcro z ulong r ulong zs ulong to ulong
1029n = ulong get gbcs 7
1030b = add get gbfb 0 2097152
1031b = add b mul gbzof z zs 4
1032ret add b mul mul r add n 8 4
1033end
1034
1035fn gbfrow z ulong r ulong to ulong
1036n = ulong get gbcs 7
1037zs = mul ulong get gbcs 6 add n 8
1038o = gbcro z r zs
1039bad = ulong 0
1040for c ulong n
1041if not gbfel z r c add o mul c 4
1042bad = add bad 1
1043end
1044end
1045for c ulong 8
1046op = add o mul add n c 4
1047if ne gbw op 2143289344
1048bad = add bad 1
1049end
1050if ne get gbcs 3 0
1051if ne gbw add op 1048576 2143289344
1052bad = add bad 1
1053end
1054end
1055end
1056ret bad
1057end
1058
1059note element (z, r, c) at host offset oc: cz 3 blocks above the diagonal keep the old C; epilogue 2
1060note gives +0 where M is not > 0; else C equals the exact reference; epilogue 1 Y = C when C > 0, else +0
1061fn gbfel z ulong r ulong c ulong oc ulong to bool
1062cb = gbw oc
1063old = gbold3 z r c
1064if and eq get gbcs 4 3 gt div c 64 div r 64
1065ret eq cb bitcast uint f32 old
1066end
1067ep = get gbcs 3
1068if eq ep 2
1069if not gt f64 bitcast f32 gbmv z r c 0.0f64
1070ret eq cb 0
1071end
1072end
1073s = gbfref z r c
1074if eq get gbcs 2 1
1075s = add s old
1076end
1077if ne f64 bitcast f32 cb s
1078ret false
1079end
1080if eq ep 1
1081w = uint 0
1082if gt s 0.0f64
1083w = cb
1084end
1085ret eq gbw add oc 1048576 w
1086end
1087ret true
1088end
1089
1090note exact reference alpha sum a b over the K range the kernel reads (bf16-rounded operands)
1091fn gbfref z ulong r ulong c ulong to f64
1092k = ulong get gbcs 5
1093lo = ulong 0
1094hi = mul add div r 64 1 64
1095cz = get gbcs 4
1096if and eq cz 1 lt hi k
1097k = hi
1098end
1099if eq cz 2
1100lo = mul div r 64 64
1101end
1102ia = mul add mul z 128 r 128
1103s = gbfdot ia mul add mul z 192 c 128 lo sub k lo
1104if eq get gbcs 9 1
1105s = mul s 0.5f64
1106end
1107ret s
1108end
1109
1110fn gbfdot ia ulong ib ulong lo ulong kn ulong to f64
1111s = 0.0f64
1112for j ulong kn
1113kk = add lo j
1114a = get gbha add ia kk
1115s = add s mul a get gbhb add ib kk
1116end
1117ret s
1118end
1119
1120note long-K case of pass p, operand modes ma, mb: m = n = 64, k = 16384 (512 K tiles), nz 1, beta 0,
1121note alpha 1. Operands are zero except kk = 0 mod 29 (565 terms, at least one in every 32-wide K tile),
1122note so a dropped, repeated or misplaced K tile changes C; sums stay below 565 5 17/8 < 2^13 in multiples
1123note 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,
1124note NaN everywhere first; C's padding columns must stay NaN. Prints `GFL pass ma mb bad`.
1125fn gbfl p ulong ma uint mb uint to ulong
1126for j ulong 10
1127put gbcs j 0
1128end
1129put gbcs 0 ma
1130put gbcs 1 mb
1131put gbcs 5 16384
1132put gbcs 6 64
1133put gbcs 7 64
1134put gbcs 8 uint p
1135b = get gbfb 0
1136for i ulong 3162112
1137gbput add b mul i 4 2143289344
1138end
1139gblw 0
1140gblw 1
1141put gkinv 0 1
1142if not gbllch
1143ggdie 98
1144end
1145bad = ulong 0
1146for r ulong 64
1147bad = add bad gblrow r
1148end
1149oputc 71
1150oputc 70
1151oputc 76
1152oputc 32
1153oputu p
1154oputc 32
1155oputu ulong ma
1156oputc 32
1157oputu ulong mb
1158oputc 32
1159oputu bad
1160oputc 10
1161ret bad
1162end
1163
1164note operand ab of the long case: nonzero words only at kk = 0 mod 29
1165fn gblw ab uint to void
1166md = get gbcs ab
1167base = add get gbfb 0 mul ulong ab 6291456
1168fr = eq get gbcs 8 ab
1169for x ulong 64
1170gblwr md base x fr
1171end
1172end
1173
1174fn gblwr md uint base ulong x ulong fr bool to void
1175w = ulong 64
1176kd = ulong 16384
1177for kk ulong kd
1178u = uint 0
1179if eq rem kk 29 0
1180u = gbwv x kk 0 fr
1181end
1182o = gbopf md x kk w kd
1183gbput add base mul o 4 u
1184end
1185end
1186
1187note launch the long case: mode 1 rows of k + 8 floats, mode 0 K rows of 80 floats
1188fn gbllch to bool
1189d = add gkbase get gbfb 0
1190gblopa 0 d
1191gblopa 1 add d 6291456
1192gkopc add d 12582912 288 4 0 0 f32 1.0f64
1193gkopz 0
1194ret gkmm 64 64 16384 1 1 false
1195end
1196
1197fn gblopa ab uint p ulong to void
1198so = uint 65568
1199sk = uint 4
1200if eq get gbcs ab 0
1201so = 4
1202sk = 320
1203end
1204gkopa ab p so sk 0 0
1205end
1206
1207note row r of the long case: 64 exact values, then 8 padding words that must still be NaN
1208fn gblrow r ulong to ulong
1209o = add add get gbfb 0 12582912 mul r 288
1210bad = ulong 0
1211for c ulong 64
1212cb = gbw add o mul c 4
1213if ne f64 bitcast f32 cb gblref r c
1214bad = add bad 1
1215end
1216end
1217for c ulong 8
1218if ne gbw add o mul add 64 c 4 2143289344
1219bad = add bad 1
1220end
1221end
1222ret bad
1223end
1224
1225note exact reference: sum over kk = 29 j of the bf16-rounded A(r, kk) B(c, kk)
1226fn gblref r ulong c ulong to f64
1227fa = eq get gbcs 8 0
1228fb = eq get gbcs 8 1
1229s = 0.0f64
1230for j ulong 565
1231kk = mul j 29
1232a = gbrv gbwv r kk 0 fa
1233s = add s mul a gbrv gbwv c kk 0 fb
1234end
1235ret s
1236end
1237
1238note Slot 85 (gkgemmh128_3way) exact 0-mismatch verification: M=128 N=128 K=128 across 3 passes.
1239note A0..A2 spaced by s6=65536 (32 KiB NaN gaps), B0..B2 contiguous dd2=32768, NaN padding outside.
1240fn gbf85_a p uint r uint k uint to long
1241v = rem add add mul ulong r 3 mul ulong k 5 mul ulong p 7 7
1242ret sub long v 3
1243end
1244
1245fn gbf85_b p uint c uint k uint to long
1246v = rem add add mul ulong c 5 mul ulong k 7 mul ulong p 11 7
1247ret sub long v 3
1248end
1249
1250fn gbf85_u16 v long to uint
1251u = bitcast uint f32 f64 v
1252ret shr u 16
1253end
1254
1255fn gbf85_wr1 p uint r uint cc uint to void
1256ba = add get gbfb 0 mul ulong p 65536
1257bb = add add get gbfb 0 524288 mul ulong p 32768
1258off = add mul ulong r 256 mul ulong cc 4
1259k = mul cc 2
1260a0 = gbf85_u16 gbf85_a p r k
1261a1 = gbf85_u16 gbf85_a p r add k 1
1262gbput add ba off or a0 shl a1 16
1263a0 = gbf85_u16 gbf85_b p r k
1264a1 = gbf85_u16 gbf85_b p r add k 1
1265gbput add bb off or a0 shl a1 16
1266end
1267
1268fn gbf85_wrp p uint to void
1269for r uint 128
1270for cc uint 64
1271gbf85_wr1 p r cc
1272end
1273end
1274end
1275
1276fn gbf85_chk1 r uint c uint to long
1277s = long 0
1278for p uint 3
1279for k uint 128
1280s = add s mul gbf85_a p r k gbf85_b p c k
1281end
1282end
1283ret s
1284end
1285
1286fn gbf85_chkel r uint c uint g uint to ulong
1287s = gbf85_chk1 r c
1288u = bitcast uint f32 f64 s
1289e = shr gbbf u 16
1290if ne g e
1291ret 1
1292end
1293ret 0
1294end
1295
1296fn gbf85_chkpair r uint cc uint to ulong
1297c = mul cc 2
1298o = add add get gbfb 0 1048576 add mul ulong r 256 mul ulong cc 4
1299w = gbw o
1300bad = gbf85_chkel r c and w 65535
1301ret add bad gbf85_chkel r add c 1 shr w 16
1302end
1303
1304fn gbf85 to ulong
1305b = get gbfb 0
1306for i ulong 524288
1307gbput add b mul i 4 2143322048
1308end
1309for p uint 3
1310gbf85_wrp p
1311end
1312put gkinv 0 1
1313d = add gkbase b
1314gkoph 0 d 256 2 65280 0
1315gkoph 1 add d 524288 256 2 32512 0
1316gkopc add d 1048576 256 2 0 0 f32 1.0f64
1317ggmmh 128 128 128 1 1 8
1318bad = ulong 0
1319for r uint 128
1320for cc uint 64
1321bad = add bad gbf85_chkpair r cc
1322end
1323end
1324for i ulong 16
1325if ne gbw add add b 1081344 mul i 4 2143322048
1326bad = add bad 1
1327end
1328end
1329oputc 71
1330oputc 70
1331oputc 56
1332oputc 53
1333oputc 32
1334oputu 128
1335oputc 32
1336oputu 128
1337oputc 32
1338oputu 128
1339oputc 32
1340oputu bad
1341oputc 10
1342ret bad
1343end
1344
1345note gbcmp2h CK DATA S: full ggbf_ok == true end-to-end packed-BF16 step parity against CPU f64 K0
1346note (exercises slots 67..74, 75, 76, 77..80, 82, 83, 85, 86, 87). Prints `GBH 1` + GBCMP2 verdict.
1347fn gbcmp2h to void
1348if ne oargc 5
1349exit 2
1350end
1351ockloop 2
1352odopen 3
1353obatch oargu 4
1354p = get og 8
1355put gkbf 0 1
1356put gkbck 0 0
1357ggsnofp 1
1358ggopen
1359if not ggbf_ok
1360ggsnofp 0
1361r = nvstop
1362gbvd 49 false
1363end
1364ggupw
1365ggf2b ggp5 gga1 0 gga1 38
1366ggquant_w
1367t0 = monons
1368lg = ggpass false
1369t1 = monons
1370ggdown
1371ggstash p 0
1372ggsnofp 0
1373put og 10 0
1374put ogkm 0 0
1375l0 = ograd
1376ggstash p p
1377ggk2z
1378gg2cmp p
1379oputc 71
1380oputc 66
1381oputc 72
1382oputc 32
1383oputc 49
1384oputc 10
1385oflush
1386ggc2pr lg l0 sub t1 t0
1387end
1388
1389note NVFP4 standalone K-looped 128x128 GEMM parity verifier (gnv4gemm M N K) and full-step verifier (gnv4cmp)
1390global gn4o ulong 12
1391global gn4m f64 10
1392global gn4s f64 4
1393
1394fn gn4align x ulong to ulong
1395ret mul div add x 255 256 256
1396end
1397
1398fn gn4p idx ulong to ulong
1399ret add gkbase get gn4o idx
1400end
1401
1402fn gn4plan m ulong n ulong k ulong to ulong
1403c = ulong 0
1404put gn4o 0 c
1405c = add c gn4align mul mul m k 2
1406put gn4o 1 c
1407c = add c gn4align mul mul n k 2
1408put gn4o 2 c
1409c = add c gn4align div mul m k 2
1410put gn4o 3 c
1411c = add c gn4align div mul m k 16
1412put gn4o 4 c
1413c = add c gn4align div mul n k 2
1414put gn4o 5 c
1415c = add c gn4align div mul n k 16
1416put gn4o 6 c
1417c = add c gn4align mul mul m n 4
1418put gn4o 7 c
1419c = add c gn4align mul mul m n 2
1420put gn4o 8 m
1421put gn4o 9 n
1422put gn4o 10 k
1423ret c
1424end
1425
1426note Deterministic value in {-1.5, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5} (exact in BF16 and E2M1 with scale 0.25; coprime strides so every row/col differs)
1427fn gn4v i ulong j ulong salt ulong to f64
1428s = rem add add mul i 5 mul j 13 salt 7
1429ret mul sub f64 s 3.0f64 0.5f64
1430end
1431
1432fn gn4bf16 v f64 to uint
1433u = bitcast uint f32 v
1434ret shr u 16
1435end
1436
1437fn gn4wr_rm1 ro ulong r ulong c2 ulong salt ulong to void
1438for cc ulong c2
1439c = mul cc 2
1440lo = gn4bf16 gn4v r c salt
1441hi = gn4bf16 gn4v r add c 1 salt
1442gbput add ro mul cc 4 or lo shl hi 16
1443end
1444end
1445
1446fn gn4wr_rm base ulong rows ulong cols ulong salt ulong to void
1447c2 = div cols 2
1448for r ulong rows
1449gn4wr_rm1 add base mul mul r cols 2 r c2 salt
1450end
1451end
1452
1453fn gn4wr_tr1 ro ulong r ulong c2 ulong salt ulong to void
1454for cc ulong c2
1455c = mul cc 2
1456lo = gn4bf16 gn4v c r salt
1457hi = gn4bf16 gn4v add c 1 r salt
1458gbput add ro mul cc 4 or lo shl hi 16
1459end
1460end
1461
1462fn gn4wr_tr base ulong rows ulong cols ulong salt ulong to void
1463c2 = div cols 2
1464for r ulong rows
1465gn4wr_tr1 add base mul mul r cols 2 r c2 salt
1466end
1467end
1468
1469fn gn4ref r ulong c ulong k ulong sc f64 to f64
1470s = 0.0f64
1471for kk ulong k
1472s = add s mul gn4v r kk 1 gn4v c kk 5
1473end
1474ret mul s sc
1475end
1476
1477fn gn4rdf32 base ulong r ulong c ulong n ulong to f64
1478o = add base mul add mul r n c 4
1479ret f64 bitcast f32 gbw o
1480end
1481
1482fn gn4rdbf base ulong r ulong c ulong n ulong to f64
1483idx = add mul r n c
1484w = gbw add base mul div idx 2 4
1485h = and w 65535
1486if eq rem idx 2 1
1487h = shr w 16
1488end
1489ret f64 bitcast f32 shl h 16
1490end
1491
1492fn gn4nf_f32r ro ulong n ulong to ulong
1493nf = ulong 0
1494for c ulong n
1495v = f64 bitcast f32 gbw add ro mul c 4
1496if not ggfin v
1497nf = add nf 1
1498end
1499end
1500ret nf
1501end
1502
1503fn gn4chk_nf_f32 base ulong m ulong n ulong to ulong
1504nf = ulong 0
1505for r ulong m
1506nf = add nf gn4nf_f32r add base mul mul r n 4 n
1507end
1508ret nf
1509end
1510
1511fn gn4nf_bfr ro ulong n2 ulong to ulong
1512nf = ulong 0
1513for cc ulong n2
1514w = gbw add ro mul cc 4
1515v0 = f64 bitcast f32 shl and w 65535 16
1516v1 = f64 bitcast f32 shl shr w 16 16
1517if not ggfin v0
1518nf = add nf 1
1519end
1520if not ggfin v1
1521nf = add nf 1
1522end
1523end
1524ret nf
1525end
1526
1527fn gn4chk_nf_bf base ulong m ulong n ulong to ulong
1528nf = ulong 0
1529n2 = div n 2
1530for r ulong m
1531nf = add nf gn4nf_bfr add base mul mul r n 2 n2
1532end
1533ret nf
1534end
1535
1536fn gn4acc g f64 rf f64 to void
1537put gn4s 0 add get gn4s 0 mul g rf
1538put gn4s 1 add get gn4s 1 mul g g
1539put gn4s 2 add get gn4s 2 mul rf rf
1540df = sub g rf
1541put gn4s 3 add get gn4s 3 mul df df
1542end
1543
1544fn gn4wr_rm1s ro ulong r ulong c2 ulong sc f64 to void
1545for cc ulong c2
1546c = mul cc 2
1547lo = gn4bf16 mul gn4v r c 1 sc
1548hi = gn4bf16 mul gn4v r add c 1 1 sc
1549gbput add ro mul cc 4 or lo shl hi 16
1550end
1551end
1552
1553fn gn4wr_rms base ulong rows ulong cols ulong sc f64 to void
1554c2 = div cols 2
1555for r ulong rows
1556gn4wr_rm1s add base mul mul r cols 2 r c2 sc
1557end
1558end
1559
1560note Non-power-of-2 arbitrary float in [-1.73, +1.73] (exercises non-zero UE4M3 mantissas + E2M1 rounding)
1561fn gn4va i ulong j ulong salt ulong to f64
1562s = rem add add mul i 11 mul j 17 salt 23
1563ret mul sub f64 s 11.0f64 0.157f64
1564end
1565
1566fn gn4wr_rma1 ro ulong r ulong c2 ulong salt ulong sc f64 to void
1567for cc ulong c2
1568c = mul cc 2
1569lo = gn4bf16 mul gn4va r c salt sc
1570hi = gn4bf16 mul gn4va r add c 1 salt sc
1571gbput add ro mul cc 4 or lo shl hi 16
1572end
1573end
1574
1575fn gn4wr_rma base ulong rows ulong cols ulong salt ulong sc f64 to void
1576c2 = div cols 2
1577for r ulong rows
1578gn4wr_rma1 add base mul mul r cols 2 r c2 salt sc
1579end
1580end
1581
1582fn gn4refa r ulong c ulong k ulong sc f64 to f64
1583s = 0.0f64
1584for kk ulong k
1585s = add s mul gn4va r kk 3 mul gn4va c kk 9 sc
1586end
1587ret s
1588end
1589
1590fn gn4stat1 s ulong idx ulong bf bool to void
1591m = get gn4o 8
1592n = get gn4o 9
1593k = get gn4o 10
1594r = rem add mul s 37 13 m
1595c = rem add mul s 53 29 n
1596if eq s 0
1597r = 0
1598c = 0
1599end
1600if eq s 63
1601r = sub m 1
1602c = sub n 1
1603end
1604sc = 1.0f64
1605if eq idx 2
1606sc = 2.0f64
1607end
1608if eq idx 3
1609sc = 0.000244140625f64
1610end
1611g = 0.0f64
1612if bf
1613g = gn4rdbf get gn4o 7 r c n
1614else
1615g = gn4rdf32 get gn4o 6 r c n
1616end
1617rf = 0.0f64
1618if eq idx 4
1619rf = gn4refa r c k 0.05f64
1620else
1621rf = gn4ref r c k sc
1622end
1623gn4acc g rf
1624end
1625
1626fn gn4stat idx ulong bf bool to void
1627for j ulong 4
1628put gn4s j 0.0f64
1629end
1630for s ulong 64
1631gn4stat1 s idx bf
1632end
1633cs = gbcsn get gn4s 0 get gn4s 1 get gn4s 2
1634rl = gbrat fsqrt get gn4s 3 fsqrt get gn4s 2
1635put gn4m mul idx 2 cs
1636put gn4m add mul idx 2 1 rl
1637end
1638
1639fn gn4gemm_pr m ulong n ulong k ulong nf ulong tf long to void
1640ok = eq nf 0
1641ok = and ok ge get gn4m 0 0.98f64
1642ok = and ok ge get gn4m 2 0.98f64
1643ok = and ok ge get gn4m 4 0.98f64
1644ok = and ok ge get gn4m 6 0.98f64
1645ok = and ok ge get gn4m 8 0.95f64
1646oputc 71
1647oputc 78
1648oputc 86
1649oputc 52
1650oputc 71
1651oputc 69
1652oputc 77
1653oputc 77
1654oputc 32
1655oputu m
1656oputc 32
1657oputu n
1658oputc 32
1659oputu k
1660for j ulong 10
1661oputc 32
1662oputl rmic ggclp get gn4m j
1663end
1664oputc 32
1665oputu nf
1666oputc 32
1667oputl tf
1668oputc 32
1669if ok
1670oputc 80
1671oputc 65
1672oputc 83
1673oputc 83
1674oputc 10
1675oflush
1676ret
1677end
1678oputc 70
1679oputc 65
1680oputc 73
1681oputc 76
1682oputc 10
1683oflush
1684exit 52
1685end
1686
1687fn gn4gemm_run m ulong n ulong k ulong fl uint al f64 to void
1688ggmm4_ab gn4p 2 gn4p 3 gn4p 4 gn4p 5 m n
1689if ne and fl 2 0
1690gkopc gn4p 7 uint mul n 2 2 0 0 f32 al
1691else
1692gkopc gn4p 6 uint mul n 4 4 0 0 f32 al
1693end
1694ggmm4_lch m n k fl
1695end
1696
1697fn gn4gemm_bench m ulong n ulong k ulong nf ulong to void
1698it = ulong 1
1699if ge mul mul m n k 67108864
1700it = 5
1701end
1702t0 = monons
1703for q ulong it
1704gn4gemm_run m n k 2 1.0f64
1705end
1706ns = sub monons t0
1707tf = long 0
1708if gt ns 0
1709flops = mul mul 2.0f64 f64 mul m n mul f64 k f64 it
1710tf = long div flops mul f64 ns 10.0f64
1711end
1712r = nvstop
1713gn4gemm_pr m n k nf tf
1714end
1715
1716fn gnv4gemm to void
1717if ne oargc 5
1718exit 2
1719end
1720m = oargu 2
1721n = oargu 3
1722k = oargu 4
1723if or eq m 0 eq n 0
1724exit 2
1725end
1726if eq k 0
1727exit 2
1728end
1729if or gt m 8192 gt n 8192
1730exit 2
1731end
1732if gt k 16384
1733exit 2
1734end
1735if gt mul m n 67108864
1736exit 2
1737end
1738if or ne rem m 128 0 ne rem n 128 0
1739exit 2
1740end
1741if ne rem k 64 0
1742exit 2
1743end
1744asz = gn4plan m n k
1745grset 256 32 64 2 128 2
1746if not gropen asz
1747ggsetupfail
1748end
1749gn4wr_rm get gn4o 0 m k 1
1750gn4wr_tr get gn4o 1 k n 5
1751put gkinv 0 1
1752ggqnv4_p 1.0f64
1753ggqnv4 gn4p 0 gn4p 2 gn4p 3 m k false
1754ggqnv4 gn4p 1 gn4p 4 gn4p 5 n k true
1755gn4gemm_run m n k 0 1.0f64
1756nf = gn4chk_nf_f32 get gn4o 6 m n
1757gn4stat 0 false
1758gn4wr_rm get gn4o 1 n k 5
1759put gkinv 0 1
1760ggqnv4 gn4p 1 gn4p 4 gn4p 5 n k false
1761gn4gemm_run m n k 2 1.0f64
1762nf = add nf gn4chk_nf_bf get gn4o 7 m n
1763gn4stat 1 true
1764gn4gemm_run m n k 3 1.0f64
1765nf = add nf gn4chk_nf_bf get gn4o 7 m n
1766gn4stat 2 true
1767gn4wr_rms get gn4o 0 m k 0.000244140625f64
1768put gkinv 0 1
1769ggqnv4_p 4096.0f64
1770ggqnv4 gn4p 0 gn4p 2 gn4p 3 m k false
1771ggqnv4_p 1.0f64
1772gn4gemm_run m n k 0 0.000244140625f64
1773nf = add nf gn4chk_nf_f32 get gn4o 6 m n
1774gn4stat 3 false
1775gn4wr_rma get gn4o 0 m k 3 1.0f64
1776gn4wr_rma get gn4o 1 n k 9 0.05f64
1777put gkinv 0 1
1778ggqnv4_p 1.0f64
1779ggqnv4 gn4p 0 gn4p 2 gn4p 3 m k false
1780ggqnv4_p 16.0f64
1781ggqnv4 gn4p 1 gn4p 4 gn4p 5 n k false
1782ggqnv4_p 1.0f64
1783gn4gemm_run m n k 0 0.0625f64
1784nf = add nf gn4chk_nf_f32 get gn4o 6 m n
1785gn4stat 4 false
1786gn4gemm_bench m n k nf
1787end
1788
1789fn gn4cmp_nf p ulong to ulong
1790nf = ulong 0
1791for q ulong ocn p
1792i0 = mul q OCH
1793for j ulong sub oce q p i0
1794if not ggfin omvg add i0 j
1795nf = add nf 1
1796end
1797end
1798end
1799ret nf
1800end
1801
1802fn gnv4cmp_pr p ulong nf ulong ns long to void
1803cs = get gn4m 0
1804dl = get gn4m 1
1805l0 = get gn4m 2
1806lg = get gn4m 3
1807ok = and eq nf 0 and ggfin lg and ge cs 0.90f64 lt dl 0.15f64
1808oputc 71
1809oputc 78
1810oputc 86
1811oputc 52
1812oputc 67
1813oputc 77
1814oputc 80
1815oputc 32
1816oputu p
1817oputc 32
1818oputl rmic ggclp cs
1819oputc 32
1820oputl rmic ggclp dl
1821oputc 32
1822oputu nf
1823oputc 32
1824oputl rmic ggclp l0
1825oputc 32
1826oputl rmic ggclp lg
1827oputc 32
1828oputl ns
1829oputc 32
1830if ok
1831oputc 80
1832oputc 65
1833oputc 83
1834oputc 83
1835oputc 10
1836oflush
1837ret
1838end
1839oputc 70
1840oputc 65
1841oputc 73
1842oputc 76
1843oputc 10
1844oflush
1845exit 53
1846end
1847
1848note gnv4cmp CK DATA S: full-step forward+backward NVFP4 vs CPU f64 K0 parity verifier
1849fn gnv4cmp to void
1850if ne oargc 5
1851exit 2
1852end
1853ockloop 2
1854odopen 3
1855obatch oargu 4
1856p = get og 8
1857put gkbf 0 1
1858put gkbck 0 0
1859ggsnv4 1
1860ggsnofp 1
1861ggopen
1862if not ggnv4_ok
1863ggsnv4 0
1864ggsnofp 0
1865r = nvstop
1866put gn4m 0 0.0f64
1867put gn4m 1 1.0f64
1868put gn4m 2 0.0f64
1869put gn4m 3 0.0f64
1870gnv4cmp_pr p 1 0
1871end
1872ggupw
1873ggf2b ggp5 gga1 0 gga1 38
1874t0 = monons
1875lg = ggpass false
1876t1 = monons
1877ggdown
1878ggstash p 0
1879ggsnv4 0
1880ggsnofp 0
1881put og 10 0
1882put ogkm 0 0
1883l0 = ograd
1884ggstash p p
1885nf = gn4cmp_nf p
1886cs = gbcos p
1887dl = gbrat gbabs sub lg l0 gbabs l0
1888r = nvstop
1889put gn4m 0 cs
1890put gn4m 1 dl
1891put gn4m 2 l0
1892put gn4m 3 lg
1893gnv4cmp_pr p nf sub t1 t0
1894end