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

1754 lines · 45753 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note gk.om: general sm_121 launcher and f32 kernel emitters for the Omega LLM.
3note Needs context.om (CUDApp native/nvrm), vector.om and launch.om (CUDApp native/blackwell), vendored
4note unmodified from aien-dev/CUDApp dcba8bb8 (last change 2e242169) into native/omegallm/gpu/.
5note QMD words follow omega_blackwell_qmd.c; control words follow omega_blackwell_codegen_gemm.c.
6note Each kernel id owns a 16 KiB slot of gkcode (64 slots); gkthr/gkgpr/gksm describe its launch shape.
7global gkcode uchar 1048576
8global gkat ulong 1
9global gkpend uint 1
10global gkthr uint 64
11global gkgpr uint 64
12global gksm uint 64
13global gkarg uint 32
14global gkna uint 1
15global gkmd uint 2
16fn gkw v uint to void
17ap = get gkat 0
18put gkcode ap uchar and v 255
19put gkcode add ap 1 uchar and shr v 8 255
20put gkcode add ap 2 uchar and shr v 16 255
21put gkcode add ap 3 uchar shr v 24
22put gkat 0 add ap 4
23end
24fn gkq a uint b uint c uint d uint to void
25gkw a
26gkw b
27gkw c
28gkw d
29end
30fn gkcw st uint wb uint rb uint wt uint to uint
31ret or or or shl st 9 shl wb 14 shl rb 17 shl wt 20
32end
33note Scheduling kinds: fixed, predicate, variable load, more loads, uniform load, store, more stores.
34fn gkfx a uint b uint c uint to void
35gkq a b c gkcw 6 7 7 get gkpend 0
36put gkpend 0 0
37end
38fn gkpr a uint b uint c uint to void
39gkq a b c gkcw 14 7 7 get gkpend 0
40put gkpend 0 0
41end
42fn gkvr a uint b uint c uint to void
43gkq a b c or gkcw 4 0 1 get gkpend 0 8192
44put gkpend 0 3
45end
46fn gkvm a uint b uint c uint to void
47gkq a b c or gkcw 4 0 1 and get gkpend 0 2 8192
48put gkpend 0 or get gkpend 0 3
49end
50fn gkvar a uint b uint c uint more bool to void
51if more
52gkvm a b c
53else
54gkvr a b c
55end
56end
57fn gkst a uint b uint c uint more bool to void
58w = get gkpend 0
59if more
60w = and w 4294967293
61end
62gkq a b c or gkcw 4 7 1 w 8192
63put gkpend 0 or w 2
64end
65fn gksync to void
66gkq 31517 0 65536 gkcw 6 7 7 get gkpend 0
67put gkpend 0 0
68end
69fn gkexit to void
70gkq 31053 0 58720256 or gkcw 5 7 7 get gkpend 0 8192
71put gkpend 0 0
72gkq 16546119 4294967292 58982399 1032192
73end
74note Instruction words, verified against nvdisasm -b SM121.
75fn gkldc d uint off uint more bool to void
76gkvar or 4278221698 shl d 16 shl div off 4 8 2048 more
77end
78fn gkldc64 d uint off uint to void
79gkvr or 4278221698 shl d 16 shl div off 4 8 2560
80end
81fn gks2r d uint sr uint more bool to void
82gkvar or 31001 shl d 16 0 shl sr 8 more
83end
84fn gkimadi d uint a uint imm uint c uint to void
85gkfx or or 30756 shl d 16 shl a 24 imm or 126747136 c
86end
87fn gkimad d uint a uint b uint c uint to void
88gkfx or or 29220 shl d 16 shl a 24 b or 126747136 c
89end
90fn gkimadw d uint a uint imm uint c uint to void
91gkfx or or 30757 shl d 16 shl a 24 imm or 126746624 c
92end
93fn gkimadh d uint a uint b uint c uint to void
94gkfx or or 29223 shl d 16 shl a 24 b or 126746624 c
95end
96fn gkshr d uint a uint n uint to void
97gkfx or 4278220825 shl d 16 n or 71168 a
98end
99fn gkandi d uint a uint imm uint to void
100gkfx or or 30738 shl d 16 shl a 24 imm 126796031
101end
102fn gkmovi d uint imm uint to void
103gkfx or 30722 shl d 16 imm 3840
104end
105fn gkmovz d uint to void
106gkfx or 29186 shl d 16 255 3840
107end
108fn gkiadd d uint a uint b uint to void
109gkfx or or 29200 shl d 16 shl a 24 b 134209791
110end
111note ISETP.GE P p = R a >= imm (signed); the destination predicate is word 2 bit 17, as in gkisetp
112fn gkisge p uint a uint imm uint to void
113gkpr or 30732 shl a 24 imm or 66085488 shl p 17
114end
115note ISETP P p = R a cc R b (signed; cc LT 1, EQ 2, LE 3, GT 4, NE 5, GE 6)
116fn gkisetp p uint a uint b uint cc uint to void
117gkpr or 29196 shl a 24 b or or 66060400 shl cc 12 shl p 17
118end
119note IMAD.WIDE.U32 R d:d+1 = R a * R b + R c:c+1
120fn gkimadwr d uint a uint b uint c uint to void
121gkfx or or 29221 shl d 16 shl a 24 b or 126746624 c
122end
123note @P p EXIT (no trailing self-branch: the block continues when P p is false)
124fn gkexitp p uint to void
125gkq or 2381 shl p 12 0 58720256 or gkcw 5 7 7 get gkpend 0 8192
126put gkpend 0 0
127end
128note @P p LDG/STG: the guard predicate is bits 12-15 of word 0 (p 7 = PT, p + 8 negates), so the
129note unguarded forms below are p 7 and keep their words
130fn gkldgp d uint a uint off uint more bool p uint to void
131gkvar or or or 2433 shl p 12 shl d 16 shl a 24 or 4 shl off 8 203299072 more
132end
133fn gkstgp a uint b uint off uint more bool p uint to void
134gkst or or 2438 shl p 12 shl a 24 or b shl off 8 202381572 more
135end
136fn gkldg d uint a uint off uint more bool to void
137gkldgp d a off more 7
138end
139fn gkstg a uint b uint off uint more bool to void
140gkstgp a b off more 7
141end
142fn gksts a uint off uint b uint more bool to void
143gkst or 31112 shl a 24 or b shl off 8 134220031 more
144end
145fn gklds d uint a uint off uint sb uint wt uint to void
146gkq or or 31108 shl d 16 shl a 24 shl off 8 3072 or gkcw 2 sb 7 wt 8192
147end
148note FFMA d = a b + c; ru is the operand reuse mask (bit 0 Ra, bit 1 Rb, bit 2 Rc; control bits 122-124).
149note A nonzero mask also sets control bit 109 (word-3 bit 13): nvdisasm decodes .reuse only with it set,
150note and ptxas (CUDA 13 sm_121) emits every .reuse instruction with it set. gkcw never sets it.
151fn gkffma d uint a uint b uint c uint wt uint ru uint to void
152y = uint 0
153if ne ru 0
154y = or shl ru 26 8192
155end
156gkq or or 29219 shl d 16 shl a 24 b c or gkcw 1 7 7 wt y
157end
158fn gkfmulf d uint a uint b uint to void
159gkfx or or 29216 shl d 16 shl a 24 b 4194304
160end
161fn gkffmaf d uint a uint b uint c uint to void
162gkfx or or 29219 shl d 16 shl a 24 b c
163end
164fn gkbra p uint tgt ulong to void
165here = div get gkat 0 16
166d = uint and sub sub tgt here 1 4294967295
167w1 = shl shr d 6 2
168c = uint 58720256
169if le tgt here
170w1 = or w1 4026531840
171c = 58982399
172end
173gkq or or 2375 shl p 12 shl and d 63 18 w1 c gkcw 5 7 7 get gkpend 0
174put gkpend 0 0
175end
176note GEMM operand setup. Mode 0 (O): thread loads outer tid&31, depth tid>>5, step depth 2.
177note Mode 1 (K): outer tid>>4, depth tid&15, step outer 4. Args of operand ab start at word 8*ab:
178note ptr lo/hi, outer stride, depth stride, batch stride 1, batch stride 2, per-load delta, tile step.
179fn gkopnd mode uint blk uint ab uint to void
180o = add uint 896 mul ab 32
181if eq mode 0
182gkandi 11 0 31
183gkshr 12 0 5
184else
185gkshr 11 0 4
186gkandi 12 0 15
187end
188gkimadi 13 11 4 255
189gkimadi add 92 ab 12 144 13
190gkimadi 13 blk 32 11
191gkldc 14 add o 8 false
192gkldc 15 add o 12 true
193gkimad 13 13 14 255
194gkimad 13 12 15 13
195gkldc 14 add o 16 false
196gkldc 15 add o 20 true
197gkimad 13 6 14 13
198gkimad 13 7 15 13
199gkldc64 10 o
200gkimadw add 88 mul ab 2 13 1 10
201gkldc add 97 ab add o 24 false
202gkldc add 99 ab add o 28 true
203end
204note One k step of the tile: prefetch k+1 fragments into the other set, then 16 FFMA on set kk&1.
205note Set 0 is a R32-35 b R36-39 on SB3; set 1 is a R40-43 b R44-47 on SB4. Element k accumulates in
206note R16+k; the FFMAs of one row share a and carry gkru.
207fn gkfrag k uint a uint b uint w uint ru uint to void
208c = add 16 k
209gkffma c a b c w ru
210end
211fn gkstep kk uint to void
212s = and kk 1
213if lt kk 15
214o = sub 1 s
215gklds add 32 mul o 8 94 mul add kk 1 144 add 3 o 0
216gklds add 36 mul o 8 95 add 2304 mul add kk 1 144 add 3 o 0
217end
218w = shl uint 1 add 3 s
219ra = add 32 mul s 8
220k = uint 0
221for i uint 4
222for j uint 4
223gkfrag k add ra i add add ra 4 j w gkru j 3
224w = 0
225k = add k 1
226end
227end
228end
229note Causal attention (gkmm cz). gkczx: the block exits when its column tile R2 exceeds R3 + word 28
230note (word 28 is 0 when only C on and below the diagonal is needed, 2^30 otherwise). gkczk: K tile
231note count R kn = R3 word 26 + word 22, and both operand pointers (pa, pa + 2) start R3 word 27 tiles
232note in (tile steps st, st + 1). Dense: word 26 = word 27 = 0. Uses R12, R13 before the main loop.
233fn gkczx to void
234gkldc 12 1008 false
235gkiadd 12 12 3
236gkisetp 0 2 12 4
237gkexitp 0
238end
239fn gkczk kn uint st uint pa uint to void
240gkldc kn 984 false
241gkldc 12 1000 true
242gkldc 13 1004 true
243gkimad kn 3 12 kn
244gkimad 13 3 13 255
245gkimadwr pa 13 st pa
246gkimadwr add pa 2 13 add st 1 add pa 2
247end
248note One 32x32 tile of C per 64-thread block, 4x4 per thread, K tiles of 16 through shared memory.
249note C args: ptr words 16/17, row stride 18, col stride 19, batch strides 20/21; 22 ktiles (base),
250note 23 batch magic, 24 minus level-2 count, 25 alpha bits, 26-28 causal (gkczx, gkczk), 30/31 the
251note fused epilogue's second pointer (gkope). beta true adds the old C; ep is the fused epilogue.
252fn gkgemm ma uint mb uint beta bool ep uint to void
253put gkpend 0 0
254gkldc 1 892 false
255gks2r 0 33 true
256gks2r 2 37 true
257gks2r 3 38 true
258gks2r 5 39 true
259gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
260put gkpend 0 4
261gkldc 8 988 false
262gkldc 9 992 true
263gkimadh 6 5 8 255
264gkimad 7 6 9 5
265gkczx
266gkshr 94 0 3
267gkimadi 94 94 16 255
268gkandi 95 0 7
269gkimadi 95 95 16 255
270gkopnd ma 3 0
271gkopnd mb 2 1
272gkczk 96 99 88
273gkmovi 101 4294967295
274for i uint 16
275gkmovz add 16 i
276end
277da = uint 288
278if eq ma 1
279da = 16
280end
281db = uint 288
282if eq mb 1
283db = 16
284end
285top = div get gkat 0 16
286put gkpend 0 63
287for r uint 8
288gkimadw add 56 mul r 2 97 r 88
289end
290for r uint 8
291gkimadw add 72 mul r 2 98 r 90
292end
293gkimadw 88 99 1 88
294gkimadw 90 100 1 90
295for r uint 8
296gkldg add 40 r add 56 mul r 2 0 ne r 0
297end
298for r uint 8
299gkldg add 48 r add 72 mul r 2 0 true
300end
301gksync
302for r uint 8
303gksts 92 mul r da add 40 r ne r 0
304end
305for r uint 8
306gksts 93 add 2304 mul r db add 48 r true
307end
308gksync
309gklds 32 94 0 3 0
310gklds 36 95 2304 3 0
311for kk uint 16
312gkstep kk
313end
314gkiadd 96 96 101
315gkisge 0 96 1
316gkbra 0 ulong top
317gkepi beta ep
318end
319note GEMM epilogue: C address of the thread's 4x4 block, alpha scale, optional old-C add, store.
320note Fused epilogue ep (gkope, beta false): 1 also stores Y = C > 0 ? C : 0 at the second pointer
321note (words 30/31, base R90), the FFN ReLU forward; 2 loads M at the second pointer's same offsets
322note into R40-55 and stores M > 0 ? C : 0, the FFN ReLU backward mask (NaN M gives 0).
323fn gkepi beta bool ep uint to void
324gkshr 12 0 3
325gkimadi 12 12 4 255
326gkimadi 12 3 32 12
327gkandi 13 0 7
328gkimadi 13 13 4 255
329gkimadi 13 2 32 13
330gkldc 14 968 false
331gkldc 15 972 true
332gkimad 12 12 14 255
333gkimad 12 13 15 12
334gkldc 10 976 false
335gkldc 11 980 true
336gkimad 12 6 10 12
337gkimad 12 7 11 12
338gkldc64 10 960
339gkimadw 88 12 1 10
340if ne ep 0
341gkldc64 10 1016
342gkimadw 90 12 1 10
343end
344gkldc 12 996 false
345if eq ep 2
346gkadr32 90
347for k uint 16
348gkldg add 40 k add 56 mul k 2 0 ne k 0
349end
350end
351gkadr32 88
352if beta
353for k uint 16
354gkldg add 40 k add 56 mul k 2 0 ne k 0
355end
356for k uint 16
357gkffmaf add 16 k add 16 k 12 add 40 k
358end
359else
360for k uint 16
361gkfmulf add 16 k add 16 k 12
362end
363end
364if eq ep 2
365t = gkthm 92
366for k uint 16
367gkepm add 16 k add 40 k t
368end
369end
370for k uint 16
371gkstg add 56 mul k 2 add 16 k 0 ne k 0
372end
373if eq ep 1
374for k uint 16
375gkepm add 16 k add 16 k 255
376end
377gkadr32 90
378for k uint 16
379gkstg add 56 mul k 2 add 16 k 0 ne k 0
380end
381end
382gkexit
383end
384note Address pairs R56-87 of the thread's 4x4 block (row stride R14, column stride R15) from base b.
385fn gkadr32 b uint to void
386for i uint 4
387r0 = add 56 mul i 8
388gkimadw r0 14 i b
389for j uint 3
390gkimadw add r0 mul add j 1 2 15 add j 1 r0
391end
392end
393end
394note Fused epilogue select: P2 = R m > R t, R e = P2 ? R e : 0 (FSETP.GT, FSEL; gr.om's encoders,
395note verified against nvdisasm there). t is RZ (255) except under mutant m.
396fn gkepm e uint m uint t uint to void
397grfsetp 2 m t 4
398grfsel e e 255 2
399end
400note Mutant m: 1 in the product, 0 in the mutant, whose fused backward mask (ep 2) is M > 2^-10 (the
401note forward and its stored FP unchanged). gkthm loads that threshold into R r and returns the
402note mask's compare register: RZ (255) in the product, r in the mutant.
403const OMUTM ulong 1
404fn gkthm r uint to uint
405if eq OMUTM 0
406gkmovi r grf 0.0009765625f64
407ret r
408end
409ret 255
410end
411note 64x64 GEMM: one 64x64 tile of C per 64-thread block, 8x8 per thread at rows ty*4+i and 32+ty*4+i,
412note columns tx*4+j and 32+tx*4+j (ty = tid>>3, tx = tid&7), K tiles of 8 through shared memory
413note (272-byte k rows, B at 2176). Each C element is still one FFMA chain over k in order, so results
414note equal the 32x32 kernel bit for bit. The K loop is software pipelined (k8): two 4352-byte shared
415note buffers; each tile first issues the global loads of the next tile (guarded by P1 = ktiles left
416note >= 2) into staging registers, runs its 64 FFMA steps from the current buffer, then stores the
417note staging into the other buffer behind one barrier. Registers: R16-79 accumulators; R80-111
418note fragments (set 0 a R80-87 b R88-95 on SB3, set 1 a R96-103 b R104-111 on SB4), later epilogue
419note addresses; R112-127 load addresses, later old C; R128/R130 operand pointers, R132/133 STS bases,
420note R134/135 LDS bases, R136 ktiles, R137/138 per-load delta, R139/140 tile step, R141 minus one,
421note later alpha; R142-149 A staging, R150-157 B staging; R158 buffer step (+4352 or -4352).
422note OMUTT: 1 in the product; 0 in mutant t, whose 64x64 kernels drop the k = 7 product of the thread's
423note C element (row 0, column 0).
424const OMUTT ulong 1
425note OKREU: 1 sets the Ra reuse bit (control bit 122, with bit 109) on each GEMM FFMA whose successor
426note reads the same a fragment; 0 (variant r0) leaves both clear as k6 did. It does not change the FFMA
427note chain of any C element, so both builds give the same bits. k7's window also measured a remap of C
428note column j to accumulator column j xor 1, which moves every k-loop b fragment and accumulator to
429note different low two index bits; it made no measured difference on sm_121 and was removed
430note (evidence/OMEGALLM/gpu-k7/).
431const OKREU ulong 1
432note gkru j last: the reuse operand mask of the FFMA at column j of a run that ends at column last.
433fn gkru j uint last uint to uint
434if and eq OKREU 1 lt j last
435ret 1
436end
437ret 0
438end
439note Mode 0 (O): thread loads outer tid, depths 0..7. Mode 1 (K): outer tid>>3 step 8, depth tid&7.
440fn gkopnd64 mode uint blk uint ab uint to void
441o = add uint 896 mul ab 32
442if eq mode 0
443gkandi 11 0 63
444gkmovz 12
445else
446gkshr 11 0 3
447gkandi 12 0 7
448end
449gkimadi 13 11 4 255
450gkimadi add 132 ab 12 272 13
451gkimadi 13 blk 64 11
452gkldc 14 add o 8 false
453gkldc 15 add o 12 true
454gkimad 13 13 14 255
455gkimad 13 12 15 13
456gkldc 14 add o 16 false
457gkldc 15 add o 20 true
458gkimad 13 6 14 13
459gkimad 13 7 15 13
460gkldc64 10 o
461gkimadw add 128 mul ab 2 13 1 10
462gkldc add 137 ab add o 24 false
463gkldc add 139 ab add o 28 true
464end
465fn gkstep64 kk uint to void
466s = and kk 1
467if lt kk 7
468o = sub 1 s
469d = add 80 mul o 16
470ko = mul add kk 1 272
471gklds d 134 ko add 3 o 0
472gklds add d 4 134 add ko 128 add 3 o 0
473gklds add d 8 135 add 2176 ko add 3 o 0
474gklds add d 12 135 add 2304 ko add 3 o 0
475end
476w = shl uint 1 add 3 s
477ra = add 80 mul s 16
478cut = and eq OMUTT 0 eq kk 7
479for i uint 8
480for j uint 8
481k = add mul i 8 j
482if or not cut ne k 0
483c = add 16 k
484u = gkru j 7
485gkffma c add ra i add add ra 8 j c w u
486w = 0
487end
488end
489end
490end
491note Load round q of a K tile into staging under predicate p (7 = PT): four A and four B loads (tile
492note loads 4q..4q+3); round 1 advances the operand pointers after its addresses are formed. Round 1
493note waits only for round 0's address reads.
494fn gkld64 q uint p uint to void
495if gt q 0
496put gkpend 0 2
497end
498l = mul q 4
499for r uint 4
500gkimadw add 112 mul r 2 137 add l r 128
501end
502for r uint 4
503gkimadw add 120 mul r 2 138 add l r 130
504end
505if gt q 0
506gkimadw 128 139 1 128
507gkimadw 130 140 1 130
508end
509for r uint 4
510gkldgp add 142 add l r add 112 mul r 2 0 ne r 0 p
511end
512for r uint 4
513gkldgp add 150 add l r add 120 mul r 2 0 true p
514end
515end
516fn gksts64 ma uint mb uint to void
517da = uint 272
518if eq ma 1
519da = 32
520end
521db = uint 272
522if eq mb 1
523db = 32
524end
525for r uint 8
526gksts 132 mul r da add 142 r ne r 0
527end
528for r uint 8
529gksts 133 add 2176 mul r db add 150 r true
530end
531end
532fn gkgemm64 ma uint mb uint beta bool ep uint to void
533put gkpend 0 0
534gkldc 1 892 false
535gks2r 0 33 true
536gks2r 2 37 true
537gks2r 3 38 true
538gks2r 5 39 true
539gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
540put gkpend 0 4
541gkldc 8 988 false
542gkldc 9 992 true
543gkimadh 6 5 8 255
544gkimad 7 6 9 5
545gkczx
546gkshr 134 0 3
547gkimadi 134 134 16 255
548gkandi 135 0 7
549gkimadi 135 135 16 255
550gkopnd64 ma 3 0
551gkopnd64 mb 2 1
552gkczk 136 139 128
553gkmovi 141 4294967295
554for i uint 64
555gkmovz add 16 i
556end
557put gkpend 0 63
558gkld64 0 7
559gkld64 1 7
560gksts64 ma mb
561gksync
562gkmovi 158 4352
563gkiadd 132 132 158
564gkiadd 133 133 158
565top = div get gkat 0 16
566gkisge 1 136 2
567gklds 80 134 0 3 0
568gklds 84 134 128 3 0
569gklds 88 135 2176 3 0
570gklds 92 135 2304 3 0
571gkld64 0 1
572gkld64 1 1
573for kk uint 8
574gkstep64 kk
575end
576gksts64 ma mb
577gksync
578gkiadd 134 134 158
579gkiadd 135 135 158
580gkimad 158 158 141 255
581gkiadd 132 132 158
582gkiadd 133 133 158
583gkiadd 136 136 141
584gkisge 0 136 1
585gkbra 0 ulong top
586gkepi64 beta ep
587end
588note Accumulator register of element k (row k>>2, column k&3) of quadrant (rh, ch).
589fn gkacc64 rh uint ch uint k uint to uint
590ret add add 16 mul add mul rh 4 shr k 2 8 add mul ch 4 and k 3
591end
592note One 4x4 quadrant of the 64x64 epilogue: address pairs R80-111 from base R128 (+32 rows R10,
593note +32 columns R11), alpha R141, old C in R112-127 when beta, store. Fused epilogue ep as in gkepi,
594note second base R142 (quadrant base R144); ep 2 loads M into R112-127 (beta is false there).
595fn gkquad64 rh uint ch uint beta bool ep uint to void
596if eq ep 2
597gkqadr rh ch 142 144
598for k uint 16
599gkldg add 112 k add 80 mul k 2 0 ne k 0
600end
601end
602gkqadr rh ch 128 130
603if beta
604for k uint 16
605gkldg add 112 k add 80 mul k 2 0 ne k 0
606end
607end
608for k uint 16
609e = gkacc64 rh ch k
610if beta
611gkffmaf e e 141 add 112 k
612else
613gkfmulf e e 141
614end
615end
616if eq ep 2
617t = gkthm 140
618for k uint 16
619gkepm gkacc64 rh ch k add 112 k t
620end
621end
622for k uint 16
623e = gkacc64 rh ch k
624gkstg add 80 mul k 2 e 0 ne k 0
625end
626if eq ep 1
627for k uint 16
628e = gkacc64 rh ch k
629gkepm e e 255
630end
631gkqadr rh ch 142 144
632for k uint 16
633e = gkacc64 rh ch k
634gkstg add 80 mul k 2 e 0 ne k 0
635end
636end
637end
638note Address pairs R80-111 of quadrant (rh, ch) from thread base b (quadrant base in R t when moved).
639fn gkqadr rh uint ch uint b uint t uint to void
640qb = b
641if eq rh 1
642gkimadw t 10 1 b
643qb = t
644end
645if eq ch 1
646gkimadw t 11 1 qb
647qb = t
648end
649for i uint 4
650r0 = add 80 mul i 8
651gkimadw r0 14 i qb
652for j uint 3
653gkimadw add r0 mul add j 1 2 15 add j 1 r0
654end
655end
656end
657note 64x64 epilogue: C base of the thread (R128), second base R142 when fused, quadrant offsets
658note R10/R11, then four quadrants.
659fn gkepi64 beta bool ep uint to void
660gkshr 12 0 3
661gkimadi 12 12 4 255
662gkimadi 12 3 64 12
663gkandi 13 0 7
664gkimadi 13 13 4 255
665gkimadi 13 2 64 13
666gkldc 14 968 false
667gkldc 15 972 true
668gkimad 12 12 14 255
669gkimad 12 13 15 12
670gkldc 10 976 false
671gkldc 11 980 true
672gkimad 12 6 10 12
673gkimad 12 7 11 12
674gkldc64 10 960
675gkimadw 128 12 1 10
676if ne ep 0
677gkldc64 10 1016
678gkimadw 142 12 1 10
679end
680gkimadi 10 14 32 255
681gkimadi 11 15 32 255
682gkldc 141 996 false
683for h uint 4
684gkquad64 shr h 1 and h 1 beta ep
685end
686gkexit
687end
688note BF16 tensor-core GEMM (ids 27..36, docs/omegallm/CONTRACT-BF16.md): one 64x64 tile of C per
689note 128-thread block, K tiles of 32, f32 operands rounded to bf16 (round to nearest even) by F2FP while
690note staging into shared memory, HMMA.16816.F32.BF16 accumulating in f32, the f32 epilogue of gkepi64.
691note Words verified with nvdisasm -b SM121 against ptxas 13.0 sm_121 output for mma.sync.aligned
692note .m16n8k16.row.col.f32.bf16.bf16.f32, cvt.rn.bf16x2.f32 and ld.global.v4.f32.
693note HMMA d a b c: d, c and a 4-aligned, b 2-aligned. ptxas keeps 16 cycles between the HMMAs of a warp
694note (stall 15, then gknop of stall 1); a dependent HMMA comes 128 cycles later here.
695fn gkhmma d uint a uint b uint c uint wt uint to void
696gkq or or 29244 shl d 16 shl a 24 b or c 268288 gkcw 15 7 7 wt
697gknop
698end
699fn gknop to void
700gkq 31000 0 0 gkcw 1 7 7 0
701end
702note F2FP.BF16.F32.PACK_AB d a b: d = bf16(a) << 16 | bf16(b), round to nearest even; rz rounds toward
703note zero (mutant v only). Stall 2 as ptxas.
704const OMUTV ulong 1
705fn gkf2fp d uint a uint b uint wt uint to void
706c = uint 4351
707if eq OMUTV 0
708c = 102655
709end
710gkq or or 29246 shl d 16 shl a 24 b c or gkcw 2 7 7 wt 8192
711end
712note LDG.E.128 R d..d+3 = [R a:a+1] under predicate p (7 = PT)
713fn gkldgq d uint a uint more bool p uint to void
714gkvar or or or 2433 shl p 12 shl d 16 shl a 24 4 203300096 more
715end
716note R d = ((tid >> s) & m) mult (+ R d unless first); R8 is the scratch.
717fn gkbt d uint s uint m uint mult uint first bool to void
718if eq s 0
719gkandi 8 0 m
720else
721gkshr 8 0 s
722gkandi 8 8 m
723end
724c = d
725if first
726c = 255
727end
728gkimadi d 8 mult c
729end
730note Operand ab of the BF16 tile. Mode 0 (O, outer stride 4): thread loads outers 4 (tid & 15) + 0..3,
731note depths 4 (tid >> 4) + j, j < 4 (one LDG.128 per depth, delta = depth stride). Mode 1 (K, depth
732note stride 4): outers (tid >> 3) + 16 i, i < 4 (delta 16 outer strides), depths 4 (tid & 7) + 0..3.
733note Pointer R112 + 2 ab, delta R137 + ab, tile step (32 depths) R139 + ab.
734fn gkopndb mode uint blk uint ab uint to void
735o = add uint 896 mul ab 32
736if eq mode 0
737gkandi 11 0 15
738gkimadi 11 11 4 255
739gkshr 12 0 4
740gkimadi 12 12 4 255
741else
742gkshr 11 0 3
743gkandi 12 0 7
744gkimadi 12 12 4 255
745end
746gkimadi 13 blk 64 11
747gkldc 14 add o 8 false
748gkldc 15 add o 12 true
749gkimad 13 13 14 255
750gkimad 13 12 15 13
751gkldc 14 add o 16 false
752gkldc 15 add o 20 true
753gkimad 13 6 14 13
754gkimad 13 7 15 13
755gkldc64 10 o
756gkimadw add 112 mul ab 2 13 1 10
757gkldc add 137 ab add o 24 false
758gkldc add 139 ab add o 28 true
759end
760note Shared memory, per 8192-byte buffer, k pair p = k >> 1 (0..15), outer r (0..63): A at
761note 2048 (p >> 3) + 512 (r >> 4) + 64 (r & 7) + 16 (p & 3) + 4 ((r >> 3) & 1) + 8 ((p >> 2) & 1),
762note B at 4096 + the same with the weights 4 and 8 exchanged, so that one LDS.128 at 512 j + 16 lane
763note (+ 2048 per k16 step) gives a lane its four A registers (rows g, g + 8; k 2t, 2t + 8) of m16
764note block j, or the two B registers of n8 blocks 2j and 2j + 1 (g = lane >> 2, t = lane & 3).
765note gkstsb: the thread's STS base R132 + ab for operand mode (o = 4 for A, 8 for B is the r-bit-3 weight).
766fn gkstsb mode uint ab uint to void
767d = add 132 ab
768ro = add 4 mul ab 4
769ko = sub 12 ro
770if eq mode 1
771gkbt d 2 1 2048 true
772gkbt d 3 7 64 false
773gkbt d 0 1 32 false
774gkbt d 6 1 ro false
775gkbt d 1 1 ko false
776else
777gkbt d 6 1 2048 true
778gkbt d 2 3 512 false
779gkbt d 0 1 256 false
780gkbt d 4 1 32 false
781gkbt d 1 1 ro false
782gkbt d 5 1 ko false
783end
784end
785note LDS bases: R134 = 1024 wm + 16 lane (A, warp row wm = tid >> 6), R135 = 1024 wn + 16 lane (B, warp
786note column wn = (tid >> 5) & 1). Mutant l (OMUTL 0) swaps the A rows of lanes g and g ^ 1.
787const OMUTL ulong 1
788fn gkldsb to void
789gkbt 134 6 1 1024 true
790gkbt 134 0 31 16 false
791if eq OMUTL 0
792gkbt 134 2 1 4294967168 false
793gkmovi 8 64
794gkiadd 134 134 8
795end
796gkbt 135 5 1 1024 true
797gkbt 135 0 31 16 false
798end
799note Load a K tile into staging under predicate p: A in R80-95, B in R96-111 (addresses R116-131),
800note then advance both operand pointers by their tile steps.
801fn gkldb p uint to void
802for i uint 4
803gkimadw add 116 mul i 2 137 i 112
804end
805for i uint 4
806gkimadw add 124 mul i 2 138 i 114
807end
808gkimadw 112 139 1 112
809gkimadw 114 140 1 114
810for i uint 4
811gkldgq add 80 mul i 4 add 116 mul i 2 ne i 0 p
812end
813for i uint 4
814gkldgq add 96 mul i 4 add 124 mul i 2 true p
815end
816end
817note Packed word x (0..7) of a staged operand at s: mode 1 is (i, e) = (x >> 1, x & 1), the k pair e of
818note outer load i; mode 0 is (jp, c) = (x >> 2, x & 3), depth pair jp of outer c. gkcvd is the packed
819note register (in place), gkcvl the lower-k f32 register (the hi one follows it, + 1 or + 4), gkcvo
820note the STS offset.
821fn gkcvl md uint x uint s uint to uint
822if eq md 1
823ret add add s mul shr x 1 4 mul and x 1 2
824end
825ret add add s mul shr x 2 8 and x 3
826end
827fn gkcvd md uint x uint s uint to uint
828if eq md 1
829ret add add s mul shr x 1 4 and x 1
830end
831ret gkcvl md x s
832end
833fn gkcvo md uint x uint to uint
834if eq md 1
835ret add mul shr x 1 512 mul and x 1 16
836end
837ret add mul and x 3 64 mul shr x 2 16
838end
839note Round the staged tile to bf16 pairs (the first F2FP waits for the loads), then store it.
840fn gkcvsb ma uint mb uint to void
841w = get gkpend 0
842put gkpend 0 0
843for ab uint 2
844md = ma
845if eq ab 1
846md = mb
847end
848s = add 80 mul ab 16
849hd = uint 1
850if eq md 0
851hd = 4
852end
853for x uint 8
854l = gkcvl md x s
855gkf2fp gkcvd md x s add l hd l w
856w = 0
857end
858end
859for ab uint 2
860md = ma
861if eq ab 1
862md = mb
863end
864gkcvst md ab add 80 mul ab 16
865end
866end
867note gkcvsb's stores of operand ab (mode md, staged at register s)
868fn gkcvst md uint ab uint s uint to void
869for x uint 8
870gksts add 132 ab add gkcvo md x mul ab 4096 gkcvd md x s or ne x 0 ne ab 0
871end
872end
873note Mutant o (OMUTO 0) runs one K tile fewer than the tile count.
874const OMUTO ulong 1
875fn gkgemmb ma uint mb uint beta bool ep uint to void
876put gkpend 0 0
877gkldc 1 892 false
878gks2r 0 33 true
879gks2r 2 37 true
880gks2r 3 38 true
881gks2r 5 39 true
882gkq 4278482860 27392 134220288 or gkcw 4 2 7 get gkpend 0 8192
883put gkpend 0 4
884gkldc 8 988 false
885gkldc 9 992 true
886gkimadh 6 5 8 255
887gkimad 7 6 9 5
888gkczx
889gkldsb
890gkstsb ma 0
891gkstsb mb 1
892gkopndb ma 3 0
893gkopndb mb 2 1
894gkczk 136 139 112
895gkmovi 141 4294967295
896if eq OMUTO 0
897gkiadd 136 136 141
898end
899for i uint 32
900gkmovz add 16 i
901end
902put gkpend 0 63
903gkldb 7
904gkcvsb ma mb
905gksync
906gkmovi 142 8192
907gkiadd 132 132 142
908gkiadd 133 133 142
909top = div get gkat 0 16
910gkisge 1 136 2
911for kk uint 2
912s = add 3 kk
913f = add 48 mul kk 16
914ko = mul kk 2048
915gklds f 134 ko s 0
916gklds add f 4 134 add ko 512 s 0
917gklds add f 8 135 add ko 4096 s 0
918gklds add f 12 135 add ko 4608 s 0
919end
920gkldb 1
921for kk uint 2
922gkmmak kk
923end
924gkcvsb ma mb
925gksync
926gkiadd 134 134 142
927gkiadd 135 135 142
928gkimad 142 142 141 255
929gkiadd 132 132 142
930gkiadd 133 133 142
931gkiadd 136 136 141
932gkisge 0 136 1
933gkbra 0 ulong top
934gkepib beta ep
935end
936note gkgemmb's eight HMMAs of k16 step kk (the first waits on barrier 3 + kk of the LDS loads)
937fn gkmmak kk uint to void
938w = shl uint 1 add 3 kk
939f = add 48 mul kk 16
940for mi uint 2
941for ni uint 4
942d = add add 16 mul mi 16 mul ni 4
943b = add add add f 8 mul shr ni 1 4 mul and ni 1 2
944gkhmma d add f mul mi 4 b d w
945w = 0
946end
947end
948end
949note Address pairs R48 + 2 e of the 16 elements e = 4 ni + c of one m16 half (row g + 8 (c >> 1),
950note column 8 ni + 2t + (c & 1)) from half base hb; row stride R14, column stride R15.
951fn gkhadrb hb uint to void
952gkimadw 52 14 8 hb
953gkimadw 48 15 0 hb
954for e uint 16
955if and ne e 0 ne e 2
956src = hb
957if ge and e 3 2
958src = 52
959end
960gkimadw add 48 mul e 2 15 add mul shr e 2 8 and e 1 src
961end
962end
963end
964note One m16 half mi of the thread's accumulators (R16 + 16 mi + e): ep 2 loads M into R80-95, beta
965note the old C; alpha R141; fused epilogue as gkquad64. Half bases: C R112 + 4 mi, second R114 + 4 mi.
966fn gkhalfb mi uint beta bool ep uint to void
967hc = add 112 mul mi 4
968hy = add 114 mul mi 4
969a0 = add 16 mul mi 16
970if eq ep 2
971gkhadrb hy
972for e uint 16
973gkldg add 80 e add 48 mul e 2 0 ne e 0
974end
975end
976gkhadrb hc
977if beta
978for e uint 16
979gkldg add 80 e add 48 mul e 2 0 ne e 0
980end
981end
982for e uint 16
983if beta
984gkffmaf add a0 e add a0 e 141 add 80 e
985else
986gkfmulf add a0 e add a0 e 141
987end
988end
989if eq ep 2
990t = gkthm 140
991for e uint 16
992gkepm add a0 e add 80 e t
993end
994end
995for e uint 16
996gkstg add 48 mul e 2 add a0 e 0 ne e 0
997end
998if eq ep 1
999for e uint 16
1000gkepm add a0 e add a0 e 255
1001end
1002gkhadrb hy
1003for e uint 16
1004gkstg add 48 mul e 2 add a0 e 0 ne e 0
1005end
1006end
1007end
1008note Thread C base R112 (and second base R114 when fused): row 64 R3 + 32 wm + g, column
1009note 64 R2 + 32 wn + 2t; half 1 bases R116 / R118 are 16 rows further.
1010fn gkepib beta bool ep uint to void
1011gkbt 12 6 1 32 true
1012gkbt 12 2 7 1 false
1013gkimadi 12 3 64 12
1014gkbt 13 5 1 32 true
1015gkbt 13 0 3 2 false
1016gkimadi 13 2 64 13
1017gkldc 14 968 false
1018gkldc 15 972 true
1019gkimad 12 12 14 255
1020gkimad 12 13 15 12
1021gkldc 10 976 false
1022gkldc 11 980 true
1023gkimad 12 6 10 12
1024gkimad 12 7 11 12
1025gkldc64 10 960
1026gkimadw 112 12 1 10
1027if ne ep 0
1028gkldc64 10 1016
1029gkimadw 114 12 1 10
1030end
1031gkimadw 116 14 16 112
1032if ne ep 0
1033gkimadw 118 14 16 114
1034end
1035gkldc 141 996 false
1036for mi uint 2
1037gkhalfb mi beta ep
1038end
1039gkexit
1040end
1041note Kernel table: ids 0..7 = 32x32 gemm (one 16 KiB slot each), ids 40, 42, ..., 54 = 64x64 gemm
1042note (40 + 2 (ma + 2 mb + 4 beta), two slots each: the pipelined K loop does not fit in one). Fused
1043note epilogue kernels (gkope, beta false): 25 (32x32) and 56 (64x64) ep 1 with ma 1 mb 0 (the FFN W1
1044note forward, ggxw), 26 and 58 ep 2 with ma 1 mb 1 (the FFN dF backward, ggxwt).
1045note gkgpr must be at least the highest GPR the kernel touches + 3: the hardware burns 2 GPRs of the
1046note QMD count for the program counter (Mesa NAK sm70.rs hw_reserved_gprs), so R(n-2) and R(n-1) are
1047note outside the allocation and a kernel using them hangs (launch wait -110). The 64x64 kernels use
1048note R158 (buffer step), so 160 hung (evidence/OMEGALLM/gpu-k8 attempt 1); 168 is the next multiple of 8.
1049note tests/omegallm/gpu-disasm.sh checks this rule per slot.
1050fn gkslot id uint to void
1051put gkat 0 mul ulong id 16384
1052end
1053fn gkall to bool
1054for g uint 20
1055id = g
1056ma = and g 1
1057mb = and shr g 1 1
1058bt = ne and g 4 0
1059ep = uint 0
1060if ge g 16
1061ep = add and g 1 1
1062ma = 1
1063mb = sub ep 1
1064bt = false
1065end
1066wide = and ge g 8 and ne g 16 ne g 17
1067if ge g 8
1068id = add 40 mul sub g 8 2
1069end
1070if ge g 16
1071id = add 24 ep
1072end
1073if ge g 18
1074id = add 54 mul ep 2
1075end
1076if not gkemit id ma mb bt ep wide
1077ret false
1078end
1079end
1080for g uint 10
1081ma = and g 1
1082mb = and shr g 1 1
1083bt = ne and g 4 0
1084ep = uint 0
1085if ge g 8
1086ep = sub g 7
1087ma = 1
1088mb = sub ep 1
1089bt = false
1090end
1091if not gkemitb add 27 g ma mb bt ep
1092ret false
1093end
1094end
1095ret true
1096end
1097note Emit the BF16 kernel (ma, mb, beta, ep) into slot id: ids 27 + ma + 2 mb + 4 beta, 35 ep 1 (ma 1
1098note mb 0), 36 ep 2 (ma 1 mb 1); highest GPR R142, so 152 (multiple of 8 at least 145).
1099fn gkemitb id uint ma uint mb uint bt bool ep uint to bool
1100gkslot id
1101gkgemmb ma mb bt ep
1102put gkgpr id 152
1103put gksm id 16384
1104if gt get gkat 0 mul ulong add id 1 16384
1105ret false
1106end
1107put gkthr id 128
1108ret true
1109end
1110note Emit one GEMM kernel into slot id (two slots when wide, the 64x64 kernel); false on overflow.
1111fn gkemit id uint ma uint mb uint bt bool ep uint wide bool to bool
1112gkslot id
1113ns = uint 1
1114if wide
1115gkgemm64 ma mb bt ep
1116put gkgpr id 168
1117put gksm id 8704
1118ns = 2
1119else
1120gkgemm ma mb bt ep
1121put gkgpr id 104
1122put gksm id 4608
1123end
1124if gt get gkat 0 mul ulong add id ns 16384
1125ret false
1126end
1127put gkthr id 64
1128ret true
1129end
1130note Launch: QMD for kernel id with grid gx gy gz; args gkarg[0..gkna).
1131fn gkqq to void
1132q = mapaddr get gbmem 2
1133for i ulong 96
1134put gbq0 i 0
1135put gbq1 i 0
1136end
1137put gbq0 4 8388610
1138put gbq0 10 2293760
1139put gbq0 12 gblo shr add q 4096 8
1140put gbq0 14 5243392
1141put gbq0 34 65537
1142put gbq0 35 2049
1143put gbq1 4 20905984
1144put gbq1 9 3
1145put gbq1 10 1638400
1146put gbq1 12 gblo shr q 8
1147put gbq1 14 793772964
1148put gbq1 15 gblo add q 8192
1149put gbq1 16 gbhi add q 8192
1150put gbq1 17 6
1151put gbq1 19 2172715008
1152put gbq1 20 56
1153put gbq1 22 67108864
1154put gbq1 58 271581201
1155end
1156fn gkqcb to void
1157cb = mapaddr get gbmem 1
1158scr = add mapaddr get gbmem 2 16384
1159put gbq1 42 gblo shr cb 6
1160put gbq1 43 or uint and shr cb 38 524287 33554432
1161put gbq1 44 gblo shr scr 6
1162put gbq1 45 or uint and shr scr 38 524287 75497472
1163put gbq1 52 gblo shr add cb 768 6
1164put gbq1 53 or uint and shr add cb 768 38 524287 8388608
1165put gbq1 56 gblo shr scr 6
1166put gbq1 57 or uint and shr scr 38 524287 2147483648
1167gbdriver cb
1168e = add cb add 896 mul ulong get gkna 0 4
1169put gbcb 212 gbhi e
1170put gbcb 213 gblo e
1171end
1172note QMD word 36 (QMDV05_00 bits 1152-1180): SHARED_MEMORY_SIZE_SHIFTED7, then the MIN (32 KiB, hw 9),
1173note MAX (100 KiB, hw 26) and TARGET SM shared-memory configs, hw value = KiB / 4 + 1 from the GB10
1174note carveouts 0 8 16 32 64 100. TARGET is the smallest carveout that holds the kernel's shared memory
1175note times its register-limited blocks per SM, 65536 / (gkgpr * gkthr). Mesa NAK qmd.rs
1176note gv100_get_hw_smem_sizes sizes TARGET the same way from warp-limited workgroups; the register limit is
1177note the tighter bound here. A fixed 32 KiB TARGET under-sized the 32x32 GEMM (9 blocks * 4608 B) and the
1178note pipelined 64x64 GEMM (6 * 8704 B): under it the pipelined kernel was 1.12x slower than the k7b one,
1179note under this rule 0.91x (evidence/OMEGALLM/gpu-k8). Only ids with gkthr set may call this.
1180fn gksmw id uint to uint
1181sm = div add get gksm id 127 128
1182if lt sm 8
1183sm = 8
1184end
1185need = mul mul div 65536 mul get gkgpr id get gkthr id sm 128
1186tg = uint 9
1187if gt need 32768
1188tg = 17
1189end
1190if gt need 65536
1191tg = 26
1192end
1193ret or or 3426304 shl tg 23 sm
1194end
1195fn gkqmd id uint gx uint gy uint gz uint to void
1196gkqq
1197code = add mapaddr get gbmem 4 mul ulong id 16384
1198put gbq1 32 gblo shr code 4
1199put gbq1 33 or uint and shr code 36 2097151 20971520
1200put gbq1 59 gblo shr code 8
1201put gbq1 34 or get gkthr id 65536
1202put gbq1 35 or or 1 shl get gkgpr id 8 131072
1203put gbq1 36 gksmw id
1204put gbq1 39 gx
1205put gbq1 40 gy
1206put gbq1 41 gz
1207gkqcb
1208put gbcb 216 get gkthr id
1209end
1210note set after host writes to the arena; the next launch invalidates GPU L2 sysmem lines first
1211global gkinv uint 1
1212fn gkbuild id uint gx uint gy uint gz uint to void
1213gkbody id gx gy gz
1214gbtail
1215end
1216note gkbody: one launch without the completion tail (ham/ollm-speed): QMD, setup, the optional L2
1217note invalidate, constant bank and argument DMA, QMD semaphore, QMD launch. batch.om (CUDApp
1218note ham/launch-batch 3b4f5807, vendored) stacks bodies under one gbtail.
1219fn gkbody id uint gx uint gy uint gz uint to void
1220gkqmd id gx gy gz
1221put gbused 0 0
1222gbsetup
1223if ne get gkinv 0 0
1224note L2_SYSMEM_NCOH_INVALIDATE (clc96f.h:73, MEM_OP_D operation 0x11 in bits 31:27): drop L2 lines
1225note the host rewrote in the arena; the previous launch ended with L2_FLUSH_DIRTY, so none is dirty
1226gbmth 0 40 4
1227gbemit 0
1228gbemit 0
1229gbemit 0
1230gbemit 2281701376
1231put gkinv 0 0
1232end
1233cb = mapaddr get gbmem 1
1234q = mapaddr get gbmem 2
1235gbdma cb 224
1236for i ulong 224
1237gbemit get gbcb i
1238end
1239na = get gkna 0
1240gbdma add cb 896 na
1241for i uint 32
1242if lt i na
1243gbemit get gkarg i
1244end
1245end
1246gbinline q false
1247gbdma add q 8192 1
1248gbemit 1
1249gbinline add q 4096 true
1250end
1251note gbtail: the completion tail, byte for byte the old end of gkbuild (CUDApp launch.om's gbtail at
1252note ham/launch-batch has the same words): mark 1, L2_FLUSH_DIRTY, mark 2
1253fn gbtail to void
1254mark = mapaddr get gbmem 3
1255gbrel mark 1145324612
1256gbmth 0 40 4
1257gbemit 0
1258gbemit 0
1259gbemit 0
1260gbemit 2147483648
1261gbrel add mark 16 1179010630
1262end
1263note Launch profile (gprof): while gkpf[0] is 1, each launch appends id gx gy gz m n k wait_ns to gkpl
1264note (8 words, at most 1024 launches); gkmm sets gkps to its m n k first, other launches log zeros.
1265global gkpf uint 1
1266global gkpc ulong 1
1267global gkpl ulong 8192
1268global gkps uint 3
1269fn gkplog id uint gx uint gy uint gz uint w long to void
1270c = get gkpc 0
1271if and eq get gkpf 0 1 lt c 1024
1272o = mul c 8
1273put gkpl o ulong id
1274put gkpl add o 1 ulong gx
1275put gkpl add o 2 ulong gy
1276put gkpl add o 3 ulong gz
1277put gkpl add o 4 ulong get gkps 0
1278put gkpl add o 5 ulong get gkps 1
1279put gkpl add o 6 ulong get gkps 2
1280put gkpl add o 7 ulong w
1281put gkpc 0 add c 1
1282end
1283put gkps 0 0
1284put gkps 1 0
1285put gkps 2 0
1286end
1287note launch accounting: 0 launches, 1 build ns, 2 wait ns (send to the QMD release semaphore),
1288note 3 max wait ns
1289global gkt long 4
1290note wait for the release: spin up to 2 ms on the mark (vendored context.om nvwait sleeps 100 us
1291note between polls, which cost each launch about 110-150 us in the rung-6 gprof ZERO round trips),
1292note then hand the rest of the budget to nvwait, which keeps its checks, sleep and timeout
1293fn gkwait m long off ulong value uint budget ulong to bool
1294if eq get nvfault 0 0
1295start = monons
1296while lt sub monons start 2000000
1297if eq mapget m off 4 ulong value
1298if ne get nvput 0 get nvret 0
1299put nvret 0 get nvput 0
1300ret true
1301end
1302end
1303end
1304end
1305ret nvwait m off value budget
1306end
1307note wait for a word nvwait cannot (it refuses once nvret has caught up with nvput): spin 2 ms,
1308note then poll every 100 us up to budget ns; a timeout sets nverr -110 and nvfault as nvwait does
1309fn gkpoll m long off ulong value uint budget ulong to bool
1310if ne get nvfault 0 0
1311put nverr 0 neg long 22
1312ret false
1313end
1314start = monons
1315while true
1316if eq mapget m off 4 ulong value
1317ret true
1318end
1319el = sub monons start
1320if or lt el 0 ge el long budget
1321put nverr 0 neg long 110
1322put nvfault 0 1
1323ret false
1324end
1325if ge el 2000000
1326r = sleepns long 100000
1327if ne r 0
1328put nverr 0 r
1329put nvfault 0 1
1330ret false
1331end
1332end
1333end
1334ret false
1335end
1336note submit the built pushbuffer and wait for the launch to retire; returns the wait in ns, or -1
1337fn gksend to long
1338r = mapset get gbmem 2 8192 4 0
1339r = mapset get gbmem 3 0 4 0
1340r = mapset get gbmem 3 16 4 0
1341n = get gbused 0
1342r = mapwrite get gbmem 0 0 gbcmd 0 n
1343put nvstage 0 41
1344if ne r long n
1345put nverr 0 neg long 5
1346ret neg long 1
1347end
1348t1 = monons
1349if not nvsend get gbmem 0 0 uint div n 4
1350ret neg long 1
1351end
1352put nvstage 0 42
1353if not gkwait get gbmem 3 16 1179010630 5000000000
1354ret neg long 1
1355end
1356put nvstage 0 43
1357if ne mapget get gbmem 3 0 4 1145324612
1358put nverr 0 neg long 5
1359put nvfault 0 1
1360ret neg long 1
1361end
1362note the QMD's own release semaphore is written after the grid retires and may land after the
1363note second mark (omega_blackwell_engine.c be_wait_release_semaphore waits for it): wait, not peek
1364put nvstage 0 44
1365if not gkpoll get gbmem 2 8192 6 1000000000
1366ret neg long 1
1367end
1368w = sub monons t1
1369put gkt 2 add get gkt 2 w
1370if gt w get gkt 3
1371put gkt 3 w
1372end
1373ret w
1374end
1375global gkbat uint 1
1376fn gklaunch id uint gx uint gy uint gz uint to bool
1377if or or or eq gx 0 eq gy 0 eq gz 0 ne get nvfault 0 0
1378put nverr 0 neg long 22
1379ret false
1380end
1381if or eq get gkna 0 0 gt get gkna 0 32
1382put nverr 0 neg long 22
1383ret false
1384end
1385put nvstage 0 40
1386t0 = monons
1387if eq get gkbat 0 1
1388ret gkrec id gx gy gz t0
1389end
1390gkbuild id gx gy gz
1391put gkt 0 add get gkt 0 1
1392put gkt 1 add get gkt 1 sub monons t0
1393w = gksend
1394if lt w 0
1395ret false
1396end
1397gkplog id gx gy gz w
1398ret true
1399end
1400note gkbat[0] = 1 (ham/ollm-speed, gg.om ggdbat only): gklaunch records each launch into the open
1401note batch.om batch (gkbody, then gbappend with the previous ordinal as its dependency: a serial chain)
1402note instead of sending it; gkbsub sends the batch once and waits once. Profile lines log wait 0.
1403fn gkrec id uint gx uint gy uint gz uint t0 long to bool
1404i = get gbst 1
1405gkbody id gx gy gz
1406put gkt 0 add get gkt 0 1
1407put gkt 1 add get gkt 1 sub monons t0
1408if not gbappend ulong id i
1409ret false
1410end
1411gkplog id gx gy gz 0
1412ret true
1413end
1414note submit the recorded batch: one nvsend, one nvwait (batch.om gbbsubmit); the wait goes to gkt 2
1415fn gkbsub to bool
1416t1 = monons
1417ok = gbbsubmit 5000000000
1418w = sub monons t1
1419put gkt 2 add get gkt 2 w
1420ret ok
1421end
1422note Open the chip: slots 0 cmd, 1 cbank, 2 qmd, 3 marks, 4 code, 5 arena of asz bytes.
1423fn gkopen asz ulong to bool
1424if not nvfull
1425ret false
1426end
1427if not nvchan
1428ret false
1429end
1430for i ulong 6
1431size = ulong 4096
1432if or eq i 0 eq i 2
1433size = 65536
1434end
1435if eq i 4
1436size = 1048576
1437end
1438if eq i 5
1439size = asz
1440end
1441note slots 0-4 (pushbuffer, cbank, QMD, marks, code) are CPU-written or host-polled: GPU-uncached
1442note as in omega_blackwell_engine.c alloc_checked; cached, a release sat in L2 about 1.06 s (cortex
1443note d03a999a). The arena (slot 5) stays cached: gkinv invalidates it after host writes and every
1444note launch ends with L2_FLUSH_DIRTY before its second mark.
1445m = nvalloc size eq i 5
1446if le m 0
1447ret false
1448end
1449put gbmem i m
1450end
1451if not gkall
1452put nverr 0 neg long 7
1453ret false
1454end
1455r = mapwrite get gbmem 4 0 gkcode 0 1048576
1456ret eq r long 1048576
1457end
1458fn gkbase to ulong
1459ret mapaddr get gbmem 5
1460end
1461note GEMM host side. Operand ab (0 = A, 1 = B): device address, outer/depth/batch strides in bytes.
1462note Mode 1 (K) when the depth stride is 4; gkmm derives per-load delta and tile step for the tile.
1463fn gkopa ab uint ptr ulong so uint sk uint s1 uint s2 uint to void
1464o = mul ab 8
1465put gkarg o gblo ptr
1466put gkarg add o 1 gbhi ptr
1467put gkarg add o 2 so
1468put gkarg add o 3 sk
1469put gkarg add o 4 s1
1470put gkarg add o 5 s2
1471md = uint 0
1472if eq sk 4
1473md = 1
1474end
1475put gkmd ab md
1476end
1477fn gkopc ptr ulong scm uint scn uint s1 uint s2 uint al f32 to void
1478put gkarg 16 gblo ptr
1479put gkarg 17 gbhi ptr
1480put gkarg 18 scm
1481put gkarg 19 scn
1482put gkarg 20 s1
1483put gkarg 21 s2
1484put gkarg 25 bitcast uint al
1485end
1486note C[z] = alpha A[z] B[z] (+ C[z] if beta), z = z1 h2 + z2, z < nz; m n multiples of 32, k of 16.
1487note m and n both multiples of 64 take the 64x64 kernels (ids 40..54 even), else the 32x32 ones (ids 0..7).
1488fn gkshape m uint n uint k uint nz uint h2 uint to bool
1489if ne rem m 32 0
1490ret false
1491end
1492if ne rem n 32 0
1493ret false
1494end
1495if or ne rem k 16 0 eq k 0
1496ret false
1497end
1498if or eq nz 0 gt nz 65535
1499ret false
1500end
1501ret ne h2 0
1502end
1503note Batch split magic: z1 = umulhi(z, mg) must equal z / h2 for every z < nz; else nverr -33.
1504fn gkmagic nz uint h2 uint to uint
1505mg = uint 0
1506if gt h2 1
1507mg = uint div add 4294967296 ulong sub h2 1 ulong h2
1508end
1509ok = true
1510for z uint nz
1511if ne uint shr mul ulong z ulong mg 32 div z h2
1512ok = false
1513end
1514end
1515if not ok
1516put nverr 0 neg long 33
1517end
1518ret mg
1519end
1520note Per-load delta (word 6) and tile step (word 7) of operand ab for tile width t (32 or 64).
1521fn gkopt ab uint t uint to void
1522o = mul ab 8
1523so = get gkarg add o 2
1524sk = get gkarg add o 3
1525gd = mul sk 2
1526st = mul sk 16
1527if eq t 64
1528gd = sk
1529st = mul sk 8
1530end
1531if eq get gkmd ab 1
1532gd = mul so 4
1533if eq t 64
1534gd = mul so 8
1535end
1536end
1537put gkarg add o 6 gd
1538put gkarg add o 7 st
1539end
1540note Tile width: 64 when m and n are both multiples of 64, else 32.
1541fn gktile m uint n uint to uint
1542if and eq rem m 64 0 eq rem n 64 0
1543ret 64
1544end
1545ret 32
1546end
1547note Causal mode cz, set by gkopz before gkmm (attention over a T x T score matrix, zero above its
1548note diagonal); gkmm consumes it, so a GEMM without gkopz is dense. 0 dense; 1 A is
1549note zero above its diagonal (C = P U, DQ = DS K), so row tile r stops after K column (r + 1) t;
1550note 2 A is zero below its diagonal (C = P^T DO, DK = DS^T Q), so row tile r starts at K row r t;
1551note 3 only C on and below the diagonal is needed (S = Q K^T, DP = DO U^T), so blocks above it exit.
1552note Modes 1 and 2 need k = m, mode 3 needs m = n. Mutant q (OMUTQ 0) stops mode 1 one K tile short.
1553const OMUTQ ulong 1
1554note BF16 path (docs/omegallm/CONTRACT-BF16.md): callers select gkbf 1 to run every
1555note 64x64 gkmm on the tensor-core kernels 27..36; 32x32 GEMMs (n = E) stay f32. gkbck 1 checks each
1556note BF16 launch against gb.om's sampled reference (gbpre before, gbpost after), with gkbp holding
1557note m n k nz h2 beta ep cz. No fallback: a GEMM the kernels cannot take fails with nverr -22.
1558global gkbf uint 1
1559global gkbck uint 1
1560global gkbp uint 8
1561global gkcz uint 1
1562fn gkopz cz uint to void
1563put gkcz 0 cz
1564end
1565fn gkczok m uint n uint k uint cz uint to bool
1566if eq cz 0
1567ret true
1568end
1569if le cz 2
1570ret eq k m
1571end
1572if eq cz 3
1573ret eq m n
1574end
1575ret false
1576end
1577note GEMM K-loop words: 22 K tiles (kd deep), 26 ka, 27 kc, 28 X for tile t and the gkopz mode,
1578note which it consumes
1579fn gkczarg t uint k uint to void
1580cz = get gkcz 0
1581put gkcz 0 0
1582kd = uint 16
1583if eq t 64
1584kd = 8
1585if eq get gkbf 0 1
1586kd = 32
1587end
1588end
1589pt = div t kd
1590put gkarg 22 div k kd
1591put gkarg 26 0
1592put gkarg 27 0
1593put gkarg 28 1073741824
1594if eq cz 1
1595put gkarg 22 pt
1596put gkarg 26 pt
1597if eq OMUTQ 0
1598put gkarg 22 sub pt 1
1599end
1600end
1601if eq cz 2
1602put gkarg 26 neg pt
1603put gkarg 27 pt
1604end
1605if eq cz 3
1606put gkarg 28 0
1607end
1608end
1609note Fused epilogue ep for the next gkmm, which consumes it (see gkepi): 1 also stores
1610note Y = C > 0 ? C : 0 at y, 2 stores M > 0 ? C : 0 with M read at y; y has C's strides. ep 1 needs
1611note operand modes ma 1 mb 0 and ep 2 ma 1 mb 1 (the only fused kernels, gkall), both beta false.
1612note Measured (k9, evidence/OMEGALLM/gpu-k9): replaces gr.om RELUF/RELUB, 16 launches per max-shape step
1613note fewer; summed GPU wait 144.56 -> 133.54 ms/step (-7.62%, three rounds each, non-quiet); t200 checkpoint
1614note bit-identical to the unfused k8 product.
1615global gkep uint 1
1616fn gkope ep uint y ulong to void
1617put gkep 0 ep
1618put gkarg 30 gblo y
1619put gkarg 31 gbhi y
1620end
1621note true when fused epilogue ep may run with the pending operand modes and beta
1622fn gkepok ep uint beta bool to bool
1623if eq ep 0
1624ret true
1625end
1626if or gt ep 2 beta
1627ret false
1628end
1629if ne get gkmd 0 1
1630ret false
1631end
1632ret eq get gkmd 1 sub ep 1
1633end
1634fn gkmm m uint n uint k uint nz uint h2 uint beta bool to bool
1635ep = get gkep 0
1636put gkep 0 0
1637if not gkepok ep beta
1638put gkcz 0 0
1639put nverr 0 neg long 22
1640ret false
1641end
1642if or not gkshape m n k nz h2 not gkczok m n k get gkcz 0
1643put gkcz 0 0
1644put nverr 0 neg long 22
1645ret false
1646end
1647put nverr 0 0
1648put gkarg 23 gkmagic nz h2
1649if ne get nverr 0 0
1650put gkcz 0 0
1651ret false
1652end
1653put gkarg 24 neg h2
1654t = gktile m n
1655bf = and eq get gkbf 0 1 eq t 64
1656if bf
1657if not gkbfok k
1658put gkcz 0 0
1659put nverr 0 neg long 22
1660ret false
1661end
1662else
1663gkopt 0 t
1664gkopt 1 t
1665end
1666put gkbp 7 get gkcz 0
1667gkczarg t k
1668put gkps 0 m
1669put gkps 1 n
1670put gkps 2 k
1671id = gkmmid t beta ep
1672if bf
1673id = gkmmb beta ep
1674end
1675if not and bf eq get gkbck 0 1
1676ret gklaunch id div n t div m t nz
1677end
1678gkbpset m n k nz h2 beta
1679put gkbp 6 ep
1680gbpre
1681if not gklaunch id div n t div m t nz
1682ret false
1683end
1684ret gbpost
1685end
1686note BF16 operands both take the 16-byte tile (gkbfop) and k is a multiple of the K tile 32
1687fn gkbfok k uint to bool
1688ba = gkbfop 0
1689bb = gkbfop 1
1690ret and and ba bb eq rem k 32 0
1691end
1692note gkbp words 0..5 for gb.om's check: m n k nz h2 beta
1693fn gkbpset m uint n uint k uint nz uint h2 uint beta bool to void
1694put gkbp 0 m
1695put gkbp 1 n
1696put gkbp 2 k
1697put gkbp 3 nz
1698put gkbp 4 h2
1699put gkbp 5 0
1700if beta
1701put gkbp 5 1
1702end
1703end
1704note Operand ab for the BF16 tile: 16-byte aligned pointer and batch strides, and 16-byte loads
1705note (mode 1: so a multiple of 16; mode 0: so = 4, sk a multiple of 16); sets delta and tile step.
1706fn gkbfop ab uint to bool
1707o = mul ab 8
1708so = get gkarg add o 2
1709sk = get gkarg add o 3
1710ok = eq rem get gkarg o 16 0
1711ok = and ok eq rem get gkarg add o 4 16 0
1712ok = and ok eq rem get gkarg add o 5 16 0
1713if eq get gkmd ab 1
1714ok = and ok eq rem so 16 0
1715put gkarg add o 6 mul so 16
1716else
1717ok = and ok and eq so 4 eq rem sk 16 0
1718put gkarg add o 6 sk
1719end
1720put gkarg add o 7 mul sk 32
1721ret ok
1722end
1723note BF16 kernel id for beta and fused epilogue ep; sets the arg count
1724fn gkmmb beta bool ep uint to uint
1725if ne ep 0
1726put gkna 0 32
1727ret add 34 ep
1728end
1729put gkna 0 29
1730id = add add 27 get gkmd 0 mul 2 get gkmd 1
1731if beta
1732id = add id 4
1733end
1734ret id
1735end
1736note kernel id of a gkmm (gkall's table) for tile t, beta and fused epilogue ep; sets the arg count
1737fn gkmmid t uint beta bool ep uint to uint
1738if ne ep 0
1739put gkna 0 32
1740if eq t 64
1741ret add 54 mul ep 2
1742end
1743ret add 24 ep
1744end
1745put gkna 0 29
1746id = add get gkmd 0 mul 2 get gkmd 1
1747if beta
1748id = add id 4
1749end
1750if eq t 64
1751id = add 40 mul id 2
1752end
1753ret id
1754end