interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / gpu / nvidia / om / gemmex / num.om

206 lines · 4837 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note The number formats of the general GEMM (src/omega_gpu_gemm_ex.h, CUDApp#144), shared by every om/gemmex unit.
3note Values travel as raw IEEE bits (f32 as uint, f64 as ulong); the f32/f64 arithmetic is the seeded float set:
4note one rounding per operator, round to nearest even, no fused operations.
5note Bit constants (Wolfram FromDigits): 0x7f800000 2139095040, 0x7fc00000 2143289344, 0x7fffff 8388607,
6note 0x7fffffff 2147483647, 0x7fff 32767, 0x8000 32768, 0x7c00 31744, 0x200 512, 0x477ff000 1199566848,
7note 0x38800000 947912704, 0x33000000 855638016, 0x800000 8388608, 0xfff 4095, 0x3ff 1023, 0xffff0000 4294901760,
8note 0x7f80 32640, 0x33800000 864026624 (f32 2^-24), 0x3b000000 989855744 (f32 2^-9).
9
10note finite f32 bits: the exponent field is not all ones
11fn gx_fin u uint to bool
12ret ne and u 2139095040 2139095040
13end
14
15note binary16 bits to f32, exact
16fn gx_h2f h uint to f32
17s = shl shr h 15 31
18e = and shr h 10 31
19m = and h 1023
20if eq e 31
21ret bitcast f32 or or s 2139095040 shl m 13
22end
23if eq e 0
24v = mul f32 m bitcast f32 uint 864026624
25if ne s 0
26ret neg v
27end
28ret v
29end
30ret bitcast f32 or or s shl add e 112 23 shl m 13
31end
32
33note e4m3 byte to f32, exact; 0x7f and 0xff are NaN (0x7fc00000), there is no infinity
34fn gx_e4m3 b uint to f32
35e = and shr b 3 15
36m = and b 7
37if and eq e 15 eq m 7
38ret bitcast f32 uint 2143289344
39end
40v = bitcast f32 or shl add e 120 23 shl m 20
41if eq e 0
42v = mul f32 m bitcast f32 uint 989855744
43end
44if ne and b 128 0
45ret neg v
46end
47ret v
48end
49
50note a stored element (raw bits u of type t: 0 f32, 1 bf16, 2 f16, 3 e4m3, 4 e5m2) to f32; an e5m2 byte is the top
51note byte of an f16
52fn gx_ldf t ulong u uint to f32
53if eq t 0
54ret bitcast f32 u
55end
56if eq t 1
57ret bitcast f32 shl u 16
58end
59if eq t 2
60ret gx_h2f u
61end
62if eq t 3
63ret gx_e4m3 u
64end
65ret gx_h2f shl u 8
66end
67
68note a stored element is non-finite (inf, NaN; e4m3: NaN only) when its exponent field has every bit set
69fn gx_bad t ulong u uint to bool
70if eq t 0
71ret eq and u 2139095040 2139095040
72end
73if eq t 1
74ret eq and u 32640 32640
75end
76if eq t 2
77ret eq and u 31744 31744
78end
79if eq t 3
80ret eq and u 127 127
81end
82ret eq and u 124 124
83end
84
85note f32 bits to bf16 bits, round to nearest even; a NaN stays a NaN (quiet bit set when the payload is cut)
86fn gx_f2bf u uint to uint
87if not gx_fin u
88if ne and u 8388607 0
89ret or shr u 16 64
90end
91ret shr u 16
92end
93ret shr add add u 32767 and shr u 16 1 16
94end
95
96note f32 bits to f16 bits, round to nearest even (subnormals rounded, overflow to infinity, NaN quieted)
97fn gx_f2h u uint to uint
98s = and shr u 16 32768
99a = and u 2147483647
100if ge a 2139095040
101if gt a 2139095040
102ret or or s 31744 512
103end
104ret or s 31744
105end
106if ge a 1199566848
107ret or s 31744
108end
109if lt a 947912704
110if lt a 855638016
111ret s
112end
113mt = or and a 8388607 8388608
114sh = sub 126 shr a 23
115q = shr mt sh
116rm = and mt sub shl uint 1 sh 1
117hf = shl uint 1 sub sh 1
118if gt rm hf
119q = add q 1
120end
121if and eq rm hf ne and q 1 0
122q = add q 1
123end
124ret or s q
125end
126r = add add a 4095 and shr a 13 1
127ret or s or shl sub shr r 23 112 10 and shr r 13 1023
128end
129
130note f64 to f32 rounding to odd: one more rounding to bf16 or f16 is then the correct rounding of v. A NaN or
131note overflowing v gives a non-finite f, returned as is; g, f widened back, serves the exact test and the direction.
132fn gx_d2fo v f64 to f32
133f = f32 v
134u = bitcast uint f
135if not gx_fin u
136ret f
137end
138g = f64 f
139if eq g v
140ret f
141end
142if ne and u 1 0
143ret f
144end
145if gt fabs v fabs g
146ret bitcast f32 add u 1
147end
148ret bitcast f32 sub u 1
149end
150
151note exact split of a finite f32 into three bf16 planes by truncation (hi | mid << 16 | lo << 32); non-finite: 0
152fn gx_split u uint to ulong
153if not gx_fin u
154ret 0
155end
156r1 = sub bitcast f32 u bitcast f32 and u 4294901760
157u1 = bitcast uint r1
158u2 = bitcast uint sub r1 bitcast f32 and u1 4294901760
159p = or ulong shr u 16 shl ulong shr u1 16 16
160ret or p shl ulong shr u2 16 32
161end
162
163note the byte size of a stored element of type t
164fn gx_es t ulong to ulong
165if eq t 0
166ret 4
167end
168if le t 2
169ret 2
170end
171ret 1
172end
173
174note element i (little-endian, es bytes) of a byte window
175fn gx_get s view uchar 65536 i ulong es ulong to uint
176p = mul i es
177v = uint get s p
178if gt es 1
179v = or v shl uint get s add p 1 8
180end
181if gt es 2
182v = or v shl uint get s add p 2 16
183v = or v shl uint get s add p 3 24
184end
185ret v
186end
187
188note element i (es bytes, little-endian) of the words of w
189fn gx_wel w view ulong 8192 i ulong es ulong to uint
190b = mul i es
191v = shr get w shr b 3 mul and b 7 8
192ret uint and v sub shl ulong 1 mul es 8 1
193end
194
195note store element i (little-endian, es bytes) into a byte window
196fn gx_put d edit uchar 65536 i ulong es ulong v uint to void
197p = mul i es
198put d p uchar and v 255
199if gt es 1
200put d add p 1 uchar and shr v 8 255
201end
202if gt es 2
203put d add p 2 uchar and shr v 16 255
204put d add p 3 uchar shr v 24
205end
206end