|
1 |
| -/*************************************************************************************************** |
2 |
| - * Copyright (c) 2025 - 2025 Codeplay Software Ltd. All rights reserved. |
3 |
| - * SPDX-License-Identifier: BSD-3-Clause |
4 |
| - * |
5 |
| - * Redistribution and use in source and binary forms, with or without |
6 |
| - * modification, are permitted provided that the following conditions are met: |
7 |
| - * |
8 |
| - * 1. Redistributions of source code must retain the above copyright notice, this |
9 |
| - * list of conditions and the following disclaimer. |
10 |
| - * |
11 |
| - * 2. Redistributions in binary form must reproduce the above copyright notice, |
12 |
| - * this list of conditions and the following disclaimer in the documentation |
13 |
| - * and/or other materials provided with the distribution. |
14 |
| - * |
15 |
| - * 3. Neither the name of the copyright holder nor the names of its |
16 |
| - * contributors may be used to endorse or promote products derived from |
17 |
| - * this software without specific prior written permission. |
18 |
| - * |
19 |
| - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 |
| - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
| - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 |
| - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
23 |
| - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
| - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
25 |
| - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
26 |
| - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
27 |
| - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 |
| - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 |
| - * |
30 |
| - **************************************************************************************************/ |
31 |
| - |
32 |
| - #pragma once |
33 |
| - |
34 |
| -#include <cutlass/half.h> |
35 |
| -#include <cute/util/sycl_vec.hpp> |
36 |
| - |
37 |
| -using half_t = cutlass::half_t; |
38 |
| -using uchar16 = cute::intel::uchar16; |
39 |
| -using ushort16 = cute::intel::ushort16; |
40 |
| - |
41 |
| -static inline ushort16 convert_ushort16(uchar16 x) { |
42 |
| - ushort16 result; |
43 |
| - #pragma unroll |
44 |
| - for (int i = 0; i < 16; ++i) { |
45 |
| - result[i] = static_cast<uint16_t>(x[i]); |
46 |
| - } |
47 |
| - return result; |
48 |
| -} |
49 |
| - |
50 |
| -static inline ushort16 E4M3_to_FP16_vec16(uchar16 xin) { |
51 |
| - uchar16 xa = xin & 0x7F; |
52 |
| - uchar16 sgn_x = xin ^ xa; |
53 |
| - |
54 |
| - uchar16 zero_mask; |
55 |
| - #pragma unroll |
56 |
| - for (int i = 0; i < 16; ++i) { |
57 |
| - zero_mask[i] = (xa[i] == 0) ? 1 : 0; |
58 |
| - } |
59 |
| - uchar16 nan_mask = (0x7E - xa) & 0x80; |
60 |
| - uchar16 den_mask = ((xa - 8) >> 7) & 0x01; |
61 |
| - |
62 |
| - xa += (nan_mask >> 1); |
63 |
| - xa |= (den_mask & 8); |
64 |
| - den_mask &= 0x48; |
65 |
| - xa += 0x40 & ~(zero_mask * 0x40); |
66 |
| - |
67 |
| - ushort16 x16 = convert_ushort16(xa) << 7; |
68 |
| - ushort16 den_corr = convert_ushort16(den_mask & ~zero_mask) << 7; |
69 |
| - |
70 |
| - ushort16 result = x16 - den_corr; |
71 |
| - result &= ~(convert_ushort16(zero_mask) << 7); |
72 |
| - |
73 |
| - ushort16 sign_ext = convert_ushort16(sgn_x) << 8; |
74 |
| - result ^= sign_ext; |
75 |
| - |
76 |
| - return result; |
77 |
| -} |
78 |
| - |
79 |
| -static inline unsigned short E4M3_to_FP16(unsigned char xin) { |
80 |
| - unsigned char xa, sgn_x, nan_mask, den_mask; |
81 |
| - |
82 |
| - union { |
83 |
| - signed short i; |
84 |
| - _Float16 f; |
85 |
| - } x16, den_corr; |
86 |
| - |
87 |
| - xa = xin & 0x7f; |
88 |
| - sgn_x = xin ^ xa; |
89 |
| - |
90 |
| - // mask for NaN input |
91 |
| - nan_mask = (0x7e - xa) & 0x80; |
92 |
| - // mask for denormal / zero input |
93 |
| - den_mask = (((signed char)(xa - 8)) >> 7); |
94 |
| - |
95 |
| - // apply Nan correction |
96 |
| - xa += (nan_mask >> 1); |
97 |
| - // first denormal correction |
98 |
| - xa |= (den_mask & 8); |
99 |
| - den_mask &= 0x48; |
100 |
| - // exponent bias correction |
101 |
| - xa += 0x40; |
102 |
| - |
103 |
| - // zero-extend to 16 bits |
104 |
| - x16.i = xa; |
105 |
| - den_corr.i = den_mask; |
106 |
| - // FP16 format |
107 |
| - x16.i <<= 7; |
108 |
| - den_corr.i <<= 7; |
109 |
| - |
110 |
| - // apply correction for denormals/zero |
111 |
| - x16.f -= den_corr.f; |
112 |
| - |
113 |
| - // finally, apply the sign |
114 |
| - x16.i ^= (((signed short)sgn_x) << 8); |
115 |
| - |
116 |
| - return (unsigned short)x16.i; |
117 |
| -} |
| 1 | +/*************************************************************************************************** |
| 2 | + * Copyright (c) 2025 - 2025 Codeplay Software Ltd. All rights reserved. |
| 3 | + * SPDX-License-Identifier: BSD-3-Clause |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * |
| 30 | + **************************************************************************************************/ |
| 31 | + |
| 32 | + #pragma once |
| 33 | + |
| 34 | +#include <cutlass/half.h> |
| 35 | +#include <cute/util/sycl_vec.hpp> |
| 36 | + |
| 37 | +using uchar16 = cute::intel::uchar16; |
| 38 | +using ushort16 = cute::intel::ushort16; |
| 39 | + |
| 40 | +static inline ushort16 convert_ushort16(uchar16 x) { |
| 41 | + ushort16 result; |
| 42 | + #pragma unroll |
| 43 | + for (int i = 0; i < 16; ++i) { |
| 44 | + result[i] = static_cast<uint16_t>(x[i]); |
| 45 | + } |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +static inline unsigned short E4M3_to_FP16(unsigned char xin) { |
| 50 | + unsigned char xa, sgn_x, nan_mask, den_mask; |
| 51 | + |
| 52 | + union { |
| 53 | + signed short i; |
| 54 | + _Float16 f; |
| 55 | + } x16, den_corr; |
| 56 | + |
| 57 | + xa = xin & 0x7f; |
| 58 | + sgn_x = xin ^ xa; |
| 59 | + |
| 60 | + // mask for NaN input |
| 61 | + nan_mask = (0x7e - xa) & 0x80; |
| 62 | + // mask for denormal / zero input |
| 63 | + den_mask = (((signed char)(xa - 8)) >> 7); |
| 64 | + |
| 65 | + // apply Nan correction |
| 66 | + xa += (nan_mask >> 1); |
| 67 | + // first denormal correction |
| 68 | + xa |= (den_mask & 8); |
| 69 | + den_mask &= 0x48; |
| 70 | + // exponent bias correction |
| 71 | + xa += 0x40; |
| 72 | + |
| 73 | + // zero-extend to 16 bits |
| 74 | + x16.i = xa; |
| 75 | + den_corr.i = den_mask; |
| 76 | + // FP16 format |
| 77 | + x16.i <<= 7; |
| 78 | + den_corr.i <<= 7; |
| 79 | + |
| 80 | + // apply correction for denormals/zero |
| 81 | + x16.f -= den_corr.f; |
| 82 | + |
| 83 | + // finally, apply the sign |
| 84 | + x16.i ^= (((signed short)sgn_x) << 8); |
| 85 | + |
| 86 | + return (unsigned short)x16.i; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +static inline ushort16 E4M3_to_FP16_chunk16(uchar16 xin) { |
| 92 | + uchar16 xa = xin & 0x7F; |
| 93 | + uchar16 sgn_x = xin ^ xa; |
| 94 | + |
| 95 | + uchar16 zero_mask; |
| 96 | + #pragma unroll |
| 97 | + for (int i = 0; i < 16; ++i) { |
| 98 | + zero_mask[i] = (xa[i] == 0) ? 1 : 0; |
| 99 | + } |
| 100 | + uchar16 nan_mask = (0x7E - xa) & 0x80; |
| 101 | + uchar16 den_mask = ((xa - 8) >> 7) & 0x01; |
| 102 | + |
| 103 | + xa += (nan_mask >> 1); |
| 104 | + xa |= (den_mask & 8); |
| 105 | + den_mask &= 0x48; |
| 106 | + xa += 0x40 & ~(zero_mask * 0x40); |
| 107 | + |
| 108 | + ushort16 x16 = convert_ushort16(xa) << 7; |
| 109 | + ushort16 den_corr = convert_ushort16(den_mask & ~zero_mask) << 7; |
| 110 | + |
| 111 | + ushort16 result = x16 - den_corr; |
| 112 | + result &= ~(convert_ushort16(zero_mask) << 7); |
| 113 | + |
| 114 | + ushort16 sign_ext = convert_ushort16(sgn_x) << 8; |
| 115 | + result ^= sign_ext; |
| 116 | + |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +template<int N> |
| 122 | +static inline void E5M2_to_FP16(cutlass::Array<uint8_t, N> const &xin, cutlass::Array<uint16_t, N> &xout) { |
| 123 | + // Adapted from https://github.com/pytorch/pytorch/blob/dfcfad2112933cc34247421ac0a4d3f19a1806c1/c10/util/Float8_e5m2.h#L30-L43 |
| 124 | + CUTLASS_PRAGMA_UNROLL |
| 125 | + for (int i = 0; i < N; i++) { |
| 126 | + xout[i] = (static_cast<uint16_t>(xin[i])) << 8; |
| 127 | + } |
| 128 | +} |
0 commit comments