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 / apiviews / fp8q.om

95 lines · 1995 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note f32 to e4m3, round to nearest, ties to even, saturate at 448 (code 0x7e).
3note Subnormals are units of 2^-9. 448.0f32 is 1138753536, 512.0f32 is 1140850688,
4note 0.5f32 is 1056964608. WolframLanguageEvaluator: FromDigits of those Real32
5note bit lists -> 1138753536, 1140850688, 1056964608.
6note Truncation of a*512 (always in [0, 8)) is a bit extract, not a cast.
7note The row unit quantizes n bf16 values (window 128) by the f32 scale whose
8note bits are sc shifted left 16. The caller owns the block loop.
9fn av_u2f n uint to f32
10if eq n 0
11ret 0.0f32
12end
13p = uint 0
14if ge n 2
15p = uint 1
16end
17if ge n 4
18p = uint 2
19end
20if ge n 8
21p = uint 3
22end
23mant = and shl n sub 23 p 8388607
24ret bitcast f32 or shl add 127 p 23 mant
25end
26fn av_trunc8 x f32 to uint
27u = bitcast uint x
28e = shr u 23
29if lt e 127
30ret 0
31end
32m = or and u 8388607 8388608
33ret shr m sub 150 e
34end
35fn av_qsub a f32 to uint
36x = mul f32 a bitcast f32 uint 1140850688
37xi = av_trunc8 x
38fr = sub x av_u2f xi
39hf = bitcast f32 uint 1056964608
40if gt fr hf
41ret add xi 1
42end
43if and eq fr hf ne and xi 1 0
44ret add xi 1
45end
46ret xi
47end
48fn av_qnorm ab uint to uint
49m = and ab 8388607
50rb = and shr m 19 1
51st = and m 524287
52q = shr m 20
53e = sub add shr ab 23 7 127
54if and ne rb 0 or ne st 0 ne and q 1 0
55q = add q 1
56end
57if eq q 8
58q = uint 0
59e = add e 1
60end
61code = or shl e 3 q
62if gt code 126
63ret uint 126
64end
65ret code
66end
67fn qe4m3 v f32 to uint
68u = bitcast uint v
69s = and shr u 24 128
70ab = and u 2147483647
71a = bitcast f32 ab
72if not lt a bitcast f32 uint 1138753536
73ret or s 126
74end
75if lt shr ab 23 121
76ret or s av_qsub a
77end
78ret or s av_qnorm ab
79end
80fn av_e4m3 bits ulong to ulong
81ret ulong qe4m3 bitcast f32 uint and bits 4294967295
82end
83fn av_fp8q n ulong sc ulong w view ushort 128 out edit uchar 128 to ulong
84if gt n 128
85ret 1
86end
87sf = bitcast f32 shl uint sc 16
88for i ulong 128
89if lt i n
90f = bitcast f32 shl uint get w i 16
91put out i uchar and qe4m3 div f sf 255
92end
93end
94ret 0
95end