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