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

196 lines · 5702 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note One gathered run of the general GEMM (CUDApp#144, src/omega_gpu_gemm_ex.c run_part, scan_bad): n stored elements
3note of type t, packed little-endian in src by the C shell, become core-type elements in dst, or are scanned.
4note ops 0 1 2 (put_core f32 f16 bf16, non-finite as 0), 3 (e4m3 copy, NaN as 0), 4 (FAST_BF16: RNE bf16) give the dst
5note elements written (ops 5 6, the bf16x3 planes, are the x3 unit); 7 gives 1 when any element is non-finite; 8 sets
6note dst[j] = 1 for every non-finite j. Ops 0 1 2 4 7 read the run as words from w (the C shell gives the same bytes as
7note src and w, word aligned), op 3 and the fp8 table runs read it as bytes from src; 9 (t = any | rowbad << 1, src
8note the column flags) sets dst[j] = 1 exactly where the epilogue recomputes.
9note Word constants (Wolfram FromDigits of the hex, base 16): the exponent masks 7f8000007f800000
10note 9187343241974906880, 7f807f807f807f80 9187483429707480960, 7c007c007c007c00 8935278002225314816,
11note 7f7f7f7f7f7f7f7f 9187201950435737471, 7c7c7c7c7c7c7c7c 8970181431921507452; the lane high bits 8000000080000000
12note 9223372039002259456, 8000800080008000 9223512776490647552, 8080808080808080 9259542123273814144, and shifted
13note right by 8 es - 1 the lane ones 4294967297, 281479271743489, 72340172838076673 (Wolfram BitShiftRight).
14note The op and the type come as ot = op | t << 4. tab caches the 256 results of ops 0 1 2 4 for an fp8 type (t 3 4):
15note tab[256] = ot + 1 of the table it holds (0 when none; the C shell gives it zeroed and keeps it for the call).
16
17note the exponent mask of type t, repeated over a word
18fn gx_wm t ulong to ulong
19if eq t 0
20ret ulong 9187343241974906880
21end
22if eq t 1
23ret ulong 9187483429707480960
24end
25if eq t 2
26ret ulong 8935278002225314816
27end
28if eq t 3
29ret ulong 9187201950435737471
30end
31ret ulong 8970181431921507452
32end
33
34note the high bit of every es-byte lane of a word
35fn gx_wh es ulong to ulong
36if eq es 4
37ret ulong 9223372039002259456
38end
39if eq es 2
40ret ulong 9223512776490647552
41end
42ret ulong 9259542123273814144
43end
44
45note the elements e0 .. n - 1 of a last partial word of w, one at a time: 1 when one is non-finite
46fn gx_wtail t ulong n ulong e0 ulong w view ulong 8192 to ulong
47es = gx_es t
48for j ulong sub n e0
49if gx_bad t gx_wel view w add e0 j es
50ret 1
51end
52end
53ret 0
54end
55
56note op 7, the last nw & 7 whole words one at a time after the ones that go eight at a time (a, the lane bits so far)
57fn gx_wrest w view ulong 8192 nw ulong m ulong one ulong a ulong to ulong
58for i ulong and nw 7
59x = xor and get w sub nw add i 1 m m
60a = or a sub x one
61end
62ret a
63end
64
65note op 7: 1 when any of the n elements is non-finite (src/omega_gpu_gemm_ex.c run_bad). A lane x = (v & m) ^ m is 0
66note exactly when its exponent bits are all set, and below the lane high bit always (m leaves the sign bit out). x - one
67note (one = 1 in every lane) sets a lane's high bit only when that lane or a lower one is 0 (a borrow), so the OR over
68note the words has a lane high bit set exactly when an element is non-finite. The whole words go eight at a time with
69note no call per word, then the last nw & 7 one at a time, then the elements of a last partial word. A row past the
70note window traps up front on the read of word nw, as the scan would at word 8192; with that bound known the C compiler
71note drops the per-word bounds checks of the eight-word loop.
72fn gx_wbad t ulong n ulong w view ulong 8192 to ulong
73m = gx_wm t
74one = shr gx_wh gx_es t sub shl gx_es t 3 1
75nw = shr mul n gx_es t 3
76if gt nw 8192
77ret get w nw
78end
79a = ulong 0
80for i ulong shr nw 3
81j = shl i 3
82x = xor and get w j m m
83a = or a sub x one
84x = xor and get w add j 1 m m
85a = or a sub x one
86x = xor and get w add j 2 m m
87a = or a sub x one
88x = xor and get w add j 3 m m
89a = or a sub x one
90x = xor and get w add j 4 m m
91a = or a sub x one
92x = xor and get w add j 5 m m
93a = or a sub x one
94x = xor and get w add j 6 m m
95a = or a sub x one
96x = xor and get w add j 7 m m
97a = or a sub x one
98end
99a = gx_wrest view w nw m one a
100es = gx_es t
101if ne and a gx_wh es 0
102ret 1
103end
104ret gx_wtail t n div shl nw 3 es view w
105end
106
107note the core bits of one element (ops 0 1 2 4)
108fn gx_cv op ulong t ulong u uint to uint
109b = bitcast uint gx_ldf t u
110if not gx_fin b
111ret 0
112end
113if eq op 0
114ret b
115end
116if eq op 1
117ret gx_f2h b
118end
119if eq op 2
120ret shr b 16
121end
122ret gx_f2bf b
123end
124
125note runs of 256 or more fp8 elements (ops 0 1 2 4, t 3 4; ot = op | t << 4) look each byte up in tab, built on first use
126fn gx_tcv ot ulong n ulong src view uchar 65536 tab edit ulong 257 dst edit uchar 65536 ce ulong to ulong
127op = and ot 15
128t = shr ot 4
129k = add ot 1
130if ne get tab 256 k
131for b ulong 256
132put tab b ulong gx_cv op t uint b
133end
134put tab 256 k
135end
136for i ulong n
137gx_put edit dst i ce uint get tab ulong get src i
138end
139ret n
140end
141
142note ops 8 9
143fn gx_scan op ulong t ulong n ulong src view uchar 65536 dst edit uchar 65536 to ulong
144if eq op 9
145for j ulong n
146v = uchar 0
147if ne and t 1 0
148if or ne and t 2 0 ne get src j 0
149v = uchar 1
150end
151end
152put dst j v
153end
154ret n
155end
156es = gx_es t
157for i ulong n
158if gx_bad t gx_get view src i es
159put dst i uchar 1
160end
161end
162ret 0
163end
164
165fn gemm_ex_row ot ulong n ulong src view uchar 65536 w view ulong 8192 tab edit ulong 257 dst edit uchar 65536 to ulong
166op = and ot 15
167t = shr ot 4
168if eq op 7
169ret gx_wbad t n view w
170end
171if ge op 8
172ret gx_scan op t n view src edit dst
173end
174if eq op 3
175for i ulong n
176b = get src i
177if eq and b 127 127
178b = uchar 0
179end
180put dst i b
181end
182ret n
183end
184es = gx_es t
185ce = ulong 2
186if eq op 0
187ce = ulong 4
188end
189if and ge t 3 ge n 256
190ret gx_tcv ot n view src edit tab edit dst ce
191end
192for i ulong n
193gx_put edit dst i ce gx_cv op t gx_wel view w i es
194end
195ret n
196end