interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / om / gemmex / plan.om

485 lines · 10223 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note The plan of the general GEMM (CUDApp#144): validation, route, tiling, k parts and the per-batch alpha, over two
3note word windows. x holds the call (written by the C shell, src/omega_gpu_gemm_ex.c), p the plan (read back by it).
4note Codes: 0 OK, 1 BAD_ARGS, 2 TOO_LARGE, 3 UNSUPPORTED (the shell maps them to OMEGA_GPU_GEMM_*).
5note x: 0 op_a, 1 op_b, 2 m, 3 n, 4 k, 5 batch, 6 a_type, 7 b_type, 8 c_type, 9 lda, 10 stride_a, 11 ldb, 12 stride_b,
6note 13 ldc, 14 stride_c, 15 alpha f32 bits, 16 beta f32 bits, 17 compute, 18 scale, 19 scale_a ptr, 20 scale_b ptr,
7note 21 scale_stride_a, 22 scale_stride_b, 23 block_m, 24 block_n, 25 block_k, 26 a ptr, 27 b ptr, 28 c ptr, 29 mutant;
8note op 1: 30 k0, 31 mc, 32 nc, 33 any_bad, 34 m0, 35 n0; op 2: 30 sa[0] bits, 31 sb[0] bits, 32 any_bad;
9note op 3 (fp8 rows): 30 w, 31 scale, 32 y, 33 x32, 34 x16 (ptrs), 35 n, 36 k, 37 m, 38 block, 39 ldy.
10note p (op 0): 0 code, 1 batch, 2 lda, 3 ldb, 4 ldc, 5 op_b in effect, 6 route, 7 core type, 8 kmax, 9 one part,
11note 10 compute (alpha != 0), 11 beta f64 bits, 12 sa_r, 13 sa_c, 14 sb_c, 15 16 scale counts a b, 17 18 scale batches
12note a b, 19 direct shape, 20 dm, 21 tm, 22 tn, 23 kcore, 24 layout, 25 core esize, 26 27 28 esize a b c, 30 31 row op
13note of A' and B', 32 B' row-major (FFMA), 33 core layout, 34 na, 35 nb, 44 45 46 extents a b c;
14note op 1: 36 kc (advance), 37 ku (k the core runs), 38 kb, 39 zero copy, 40 41 byte offsets of A and B, 47 core K;
15note op 2: 42 alpha f64 bits, 43 direct now; op 3: 0 code, 1 ldy, 2 kb, 3 slab rows, 4 a_type.
16note Constants (Wolfram): 2^64-1 18446744073709551615, 2^56 72057594037927936, 2^58 288230376151711744,
17note 2^31-1 2147483647, 2^32-1 4294967295, 32 MiB 33554432, f64 1.0 bits 4607182418800017408.
18
19note stored rows x cols with leading dimension ld: the span, 0 when ld is short or the span passes 2^56
20fn gx_span rows ulong cols ulong ld ulong to ulong
21if lt ld cols
22ret 0
23end
24if gt sub rows 1 div 72057594037927936 ld
25ret 0
26end
27ret add mul sub rows 1 ld cols
28end
29
30note the elements batch copies of a span reach: 0 when the product or sum overflows or passes 2^58
31fn gx_ext span ulong stride ulong batch ulong to ulong
32b1 = sub batch 1
33if ne b1 0
34if gt stride div 18446744073709551615 b1
35ret 0
36end
37end
38t = mul b1 stride
39e = add t span
40if lt e t
41ret 0
42end
43if gt e 288230376151711744
44ret 0
45end
46ret e
47end
48
49note 1 when byte ranges [a, a + pa) and [b, b + qb) overlap (address arithmetic modulo 2^64)
50fn gx_ovl a ulong pa ulong b ulong qb ulong to ulong
51if and lt a add b qb lt b add a pa
52ret 1
53end
54ret 0
55end
56
57note the C3 leading-dimension check of one operand (omega_gpu_gemm.c ld_span): 1 when the span fits 32-bit indexing
58fn gx_lds col ulong r ulong kk ulong ld ulong to ulong
59ou = r
60iu = kk
61if eq col 1
62ou = kk
63iu = r
64end
65l = ld
66if eq l 0
67l = iu
68end
69if or lt l iu gt l 4294967295
70ret 0
71end
72if gt add mul sub ou 1 l iu 4294967295
73ret 0
74end
75ret 1
76end
77
78note omega_gpu_gemm_span: 1 when C3 can address the operands in place
79fn gx_gspan lay ulong mm ulong nn ulong kk ulong lda ulong ldb ulong to ulong
80if or or eq mm 0 eq nn 0 eq kk 0
81ret 0
82end
83if eq gx_lds and lay 1 mm kk lda 0
84ret 0
85end
86ret gx_lds xor shr and lay 2 1 1 nn kk ldb
87end
88
89note the route (low byte) and the core type (next byte): C l.228-239
90fn gx_route comp ulong ta ulong tb ulong to ulong
91if eq comp 2
92ret 773
93end
94if and eq ta tb eq ta 1
95ret 1
96end
97if and eq ta tb eq ta 2
98ret 513
99end
100if and eq ta tb eq ta 3
101ret 257
102end
103if and and ne ta 0 ne ta 2 and ne tb 0 ne tb 2
104ret 2
105end
106if and and ne ta 0 ne ta 1 and ne tb 0 ne tb 1
107ret 514
108end
109if eq comp 1
110ret 4
111end
112ret 3
113end
114
115note op 0, part 1b (C l.202-211): default leading dimensions, spans, the C batch stride, extents
116fn gx_valb x view ulong 48 p edit ulong 64 to ulong
117r = get x 2
118c = get x 4
119if eq get x 0 1
120r = get x 4
121c = get x 2
122end
123put p 2 get x 9
124if eq get x 9 0
125put p 2 c
126end
127put p 48 gx_span r c get p 2
128r = get x 4
129c = get x 3
130if eq get x 1 1
131r = get x 3
132c = get x 4
133end
134put p 3 get x 11
135if eq get x 11 0
136put p 3 c
137end
138put p 49 gx_span r c get p 3
139put p 4 get x 13
140if eq get x 13 0
141put p 4 get x 3
142end
143put p 50 gx_span get x 2 get x 3 get p 4
144if or eq get p 48 0 eq get p 49 0
145ret 1
146end
147if eq get p 50 0
148ret 1
149end
150if gt get p 1 1
151if lt get x 14 get p 50
152ret 1
153end
154end
155put p 44 gx_ext get p 48 get x 10 get p 1
156put p 45 gx_ext get p 49 get x 12 get p 1
157put p 46 gx_ext get p 50 get x 14 get p 1
158if or eq get p 44 0 eq get p 45 0
159ret 1
160end
161if eq get p 46 0
162ret 1
163end
164ret 0
165end
166
167note op 0, part 1 (C l.194-211): defaults, ranges, sizes, spans and extents
168fn gx_val x view ulong 48 p edit ulong 64 to ulong
169b = get x 5
170if eq b 0
171b = 1
172end
173put p 1 b
174if or gt get x 0 1 gt get x 1 1
175ret 1
176end
177if or gt get x 6 4 gt get x 7 4
178ret 1
179end
180if or gt get x 8 4 gt get x 17 2
181ret 1
182end
183if or gt get x 18 2 eq get x 28 0
184ret 1
185end
186m = get x 2
187n = get x 3
188k = get x 4
189if or or eq m 0 eq n 0 eq k 0
190ret 1
191end
192if or gt m 2147483647 gt n 2147483647
193ret 2
194end
195if or gt k 2147483647 gt b 2147483647
196ret 2
197end
198if gt get x 8 2
199ret 3
200end
201ret gx_valb view x edit p
202end
203
204note op 0, part 2 (C l.212-226): operand pointers, overlap with C, the scale grid
205fn gx_val2 x view ulong 48 p edit ulong 64 to ulong
206put p 26 gx_es get x 6
207put p 27 gx_es get x 7
208put p 28 gx_es get x 8
209cb = mul get p 46 get p 28
210put p 10 0
211if ne bitcast f32 uint get x 15 bitcast f32 uint 0
212put p 10 1
213if or eq get x 26 0 eq get x 27 0
214ret 1
215end
216if eq gx_ovl get x 28 cb get x 26 mul get p 44 get p 26 1
217ret 1
218end
219if eq gx_ovl get x 28 cb get x 27 mul get p 45 get p 27 1
220ret 1
221end
222end
223sc = get x 18
224put p 12 0
225put p 13 0
226put p 14 0
227put p 15 0
228put p 16 0
229if ne sc 0
230if or eq get x 19 0 eq get x 20 0
231ret 1
232end
233put p 15 1
234put p 16 1
235end
236if eq sc 2
237if or or eq get x 23 0 eq get x 24 0 eq get x 25 0
238ret 1
239end
240put p 12 div sub add get x 2 get x 23 1 get x 23
241put p 13 div sub add get x 4 get x 25 1 get x 25
242put p 14 div sub add get x 3 get x 24 1 get x 24
243put p 15 mul get p 12 get p 13
244put p 16 mul get p 13 get p 14
245end
246put p 17 1
247if ne get x 21 0
248put p 17 get p 1
249end
250put p 18 1
251if ne get x 22 0
252put p 18 get p 1
253end
254ret 0
255end
256
257note op 0, part 4: the core k of one part, buffer sizes, the core esize and the row ops of A' and B' (row.om ops)
258fn gx_val4 x view ulong 48 p edit ulong 64 to ulong
259r = get p 6
260kc = get x 4
261if gt kc get p 8
262kc = get p 8
263end
264kcore = kc
265if eq r 3
266kcore = mul 9 mul shr add kc 15 4 16
267end
268put p 23 kcore
269put p 34 add mul get p 21 kcore 16
270put p 35 add mul get p 22 kcore 16
271ct = get p 7
272put p 25 2
273if eq ct 1
274put p 25 1
275end
276if eq ct 3
277put p 25 4
278end
279put p 32 0
280put p 33 2
281if eq r 5
282put p 30 0
283put p 31 0
284put p 32 1
285put p 33 0
286ret 0
287end
288if eq r 3
289put p 30 5
290put p 31 6
291ret 0
292end
293if eq r 4
294put p 30 4
295put p 31 4
296ret 0
297end
298put p 30 2
299if eq ct 1
300put p 30 3
301end
302if eq ct 2
303put p 30 1
304end
305put p 31 get p 30
306ret 0
307end
308
309note op 0, part 3 (C l.227-240 and the plan of omega_gpu_gemm_ex l.351-370): route, k part size, tiles, row ops
310fn gx_val3 x view ulong 48 p edit ulong 64 to ulong
311r = gx_route get x 17 get x 6 get x 7
312put p 7 shr r 8
313r = and r 255
314put p 6 r
315put p 8 16384
316if eq r 3
317put p 8 1808
318end
319put p 5 get x 1
320if eq get x 29 2
321put p 5 0
322end
323put p 9 0
324if and le get x 4 get p 8 ne get x 18 2
325put p 9 1
326end
327bt = f64 bitcast f32 uint get x 16
328if eq get x 29 4
329bt = bitcast f64 ulong 0
330end
331put p 11 bitcast ulong bt
332put p 24 or get x 0 shl get p 5 1
333n = get x 2
334put p 20 n
335if gt n 16384
336put p 20 16384
337end
338put p 21 n
339if gt n 4096
340put p 21 4096
341end
342n = get x 3
343put p 22 n
344if gt n 4096
345put p 22 4096
346end
347put p 19 0
348if and eq get p 10 1 eq get p 9 1
349if and eq r 1 eq bt bitcast f64 ulong 0
350if and eq get x 8 0 eq get p 4 n
351if le n 16384
352put p 19 gx_gspan get p 24 get p 20 n get x 4 get p 2 get p 3
353end
354end
355end
356end
357ret gx_val4 view x edit p
358end
359
360note op 1 (C l.388, l.411-420, run_part l.264-270): one k part from k0 of the tile at (m0, n0), mc x nc
361fn gx_kpart x view ulong 48 p edit ulong 64 to ulong
362k0 = get x 30
363kc = sub get x 4 k0
364if gt kc get p 8
365kc = get p 8
366end
367put p 38 0
368if eq get x 18 2
369bk = get x 25
370if gt kc sub bk rem k0 bk
371kc = sub bk rem k0 bk
372end
373put p 38 div k0 bk
374if eq get x 29 3
375put p 38 rem add get p 38 1 get p 13
376end
377end
378ku = kc
379if and eq get x 29 1 eq add k0 kc get x 4
380ku = sub kc 1
381end
382put p 36 kc
383put p 37 ku
384put p 47 ku
385if eq get p 6 3
386put p 47 mul 9 mul shr add ku 15 4 16
387end
388put p 39 0
389if and eq get p 6 1 eq get x 33 0
390put p 39 gx_gspan get p 24 get x 31 get x 32 ku get p 2 get p 3
391end
392put p 40 add mul get x 34 get p 2 k0
393if eq get x 0 1
394put p 40 add mul k0 get p 2 get x 34
395end
396put p 41 add mul k0 get p 3 get x 35
397if eq get p 5 1
398put p 41 add mul get x 35 get p 3 k0
399end
400put p 40 mul get p 40 get p 26
401put p 41 mul get p 41 get p 26
402ret 0
403end
404
405note op 2 (C l.377-389): this batch's alpha (times the tensor scales) and whether C3 writes C directly
406fn gx_batch x view ulong 48 p edit ulong 64 to ulong
407al = f64 bitcast f32 uint get x 15
408if eq get x 18 1
409al = mul al mul f64 bitcast f32 uint get x 30 f64 bitcast f32 uint get x 31
410end
411put p 42 bitcast ulong al
412put p 43 0
413if and and eq get p 19 1 eq get x 32 0 eq al bitcast f64 ulong 4607182418800017408
414put p 43 1
415end
416ret 0
417end
418
419note op 3 (C l.455-461): the fp8 block-row fallback's checks, ldy, k blocks and dequantized rows per slab
420fn gx_fp8v x view ulong 48 p edit ulong 64 to ulong
421n = get x 35
422k = get x 36
423bl = get x 38
424ly = get x 39
425put p 0 1
426if or eq get x 30 0 eq get x 31 0
427ret 1
428end
429if eq get x 32 0
430ret 1
431end
432if eq eq get x 33 0 eq get x 34 0
433ret 1
434end
435if or or eq n 0 eq k 0 or eq get x 37 0 eq bl 0
436ret 1
437end
438if and ne ly 0 lt ly n
439ret 1
440end
441if eq ly 0
442ly = n
443end
444put p 0 0
445put p 1 ly
446put p 2 div sub add k bl 1 bl
447rows = div 33554432 mul k 4
448if eq rows 0
449rows = 1
450end
451if gt rows n
452rows = n
453end
454put p 3 rows
455put p 4 1
456if ne get x 33 0
457put p 4 0
458end
459ret 0
460end
461
462note the entry: op 0 plans a call (validation through the row ops), 1 one k part, 2 one batch, 3 the fp8 rows checks
463fn gemm_ex_plan op ulong x view ulong 48 p edit ulong 64 to ulong
464if eq op 0
465r = gx_val view x edit p
466if ne r 0
467ret r
468end
469r = gx_val2 view x edit p
470if ne r 0
471ret r
472end
473ret gx_val3 view x edit p
474end
475if eq op 1
476ret gx_kpart view x edit p
477end
478if eq op 2
479ret gx_batch view x edit p
480end
481if eq op 3
482ret gx_fp8v view x edit p
483end
484ret 1
485end