Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Jerry Ge | d6a0461 | 2023-12-15 22:45:39 +0000 | [diff] [blame] | 2 | // Copyright (c) 2020-2024, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include "type_conversion.h" |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 17 | #include "arith_util.h" |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 18 | #include "float_utils.h" |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 19 | #include "half.hpp" |
| 20 | #include "quant_util.h" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 21 | #include "template_types.h" |
| 22 | #include <cmath> |
| 23 | |
| 24 | using namespace TosaReference; |
| 25 | using namespace Eigen; |
| 26 | using namespace tosa; |
| 27 | |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 28 | using fp16 = tosa::reference::internal::float_t<int16_t, 5, true, true, true>; |
| 29 | using bf16 = tosa::reference::internal::float_t<int16_t, 8, true, true, true>; |
| 30 | using fp32 = tosa::reference::internal::float_t<int32_t, 8, true, true, true>; |
| 31 | using fp8e4m3 = tosa::reference::internal::float_t<int8_t, 4, true, true, false>; |
| 32 | using fp8e5m2 = tosa::reference::internal::float_t<int8_t, 5, true, true, true>; |
| 33 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 34 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 35 | OpRescale<Rank, InDtype, OutDtype>::OpRescale(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 36 | : GraphNode(sgt_, Op_RESCALE, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 37 | { |
Tai Ly | 6e1e2bc | 2024-03-01 20:59:32 +0000 | [diff] [blame] | 38 | setRequiredOperands(3, 1); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 39 | INIT_ATTRIBUTE(Rescale); |
TatWai Chong | 0dac6c9 | 2024-04-02 15:45:29 -0700 | [diff] [blame^] | 40 | |
| 41 | QMax_s = getSignedMaximum<OutDtype>(); |
| 42 | QMin_s = getSignedMinimum<OutDtype>(); |
| 43 | QMax_u = getUnsignedMaximum<OutDtype>(); |
| 44 | QMin_u = getUnsignedMinimum<OutDtype>(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 47 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 48 | OpRescale<Rank, InDtype, OutDtype>::~OpRescale() |
| 49 | { |
| 50 | if (attribute) |
| 51 | delete attribute; |
| 52 | } |
| 53 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 54 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 55 | int OpRescale<Rank, InDtype, OutDtype>::checkTensorAttributes() |
| 56 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 57 | // Check Tosa Level |
| 58 | auto tosa_level = g_func_config.tosa_level; |
| 59 | LEVEL_CHECK(Rank <= tosa_level.MAX_RANK, "Rank should be smaller than or equal to MAX_RANK"); |
| 60 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 61 | if (validateRequiredOperands()) |
| 62 | return 1; |
| 63 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 64 | // output and input must be the same rank and size |
| 65 | if (inputs[0]->matchRankSize(*outputs[0])) |
| 66 | { |
| 67 | printNodeValidationError("OpRescale: input and output rank/size must match"); |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 72 | out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 73 | |
| 74 | ASSERT_MEM(in && out); |
| 75 | |
Tai Ly | 6e1e2bc | 2024-03-01 20:59:32 +0000 | [diff] [blame] | 76 | multiplierI32 = dynamic_cast<TosaReference::TensorTemplate<TMultiplierI32>*>(inputs[1]); |
| 77 | multiplierI16 = dynamic_cast<TosaReference::TensorTemplate<TMultiplierI16>*>(inputs[1]); |
| 78 | shift = dynamic_cast<TosaReference::TensorTemplate<TShift>*>(inputs[2]); |
| 79 | ASSERT_MEM(shift); |
| 80 | |
| 81 | if (attribute->scale32()) |
| 82 | { |
| 83 | ASSERT_MEM(multiplierI32); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | ASSERT_MEM(multiplierI16); |
| 88 | } |
| 89 | |
Eric Kunze | 9c0a507 | 2024-03-19 21:14:07 +0000 | [diff] [blame] | 90 | auto input_zp = attribute->input_zp(); |
| 91 | auto output_zp = attribute->output_zp(); |
| 92 | auto input_unsigned = attribute->input_unsigned(); |
| 93 | auto output_unsigned = attribute->output_unsigned(); |
| 94 | |
| 95 | // Note that how rescale op interprets signedness of the tensor depends on |
| 96 | // the value of input_unsigned and output_unsigned attributes, and doesn't |
| 97 | // care about the type of tensor itself. |
| 98 | |
| 99 | if (!isI8(InDtype) && (!isI16(InDtype) || input_unsigned == false) && (input_zp != 0)) |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 100 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 101 | printNodeValidationError("OpRescale: Input TOSA_REF_TYPE not INT8/UINT8/UINT16 and zero point not 0"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 102 | return 1; |
| 103 | } |
| 104 | |
Eric Kunze | 9c0a507 | 2024-03-19 21:14:07 +0000 | [diff] [blame] | 105 | if (!isI8(OutDtype) && (!isI16(OutDtype) || output_unsigned == false) && (output_zp != 0)) |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 106 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 107 | printNodeValidationError("OpRescale: Output TOSA_REF_TYPE not INT8/UINT8/UINT16 and zero point not 0"); |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 108 | return 1; |
| 109 | } |
| 110 | |
Eric Kunze | 9c0a507 | 2024-03-19 21:14:07 +0000 | [diff] [blame] | 111 | if (isI16(InDtype) && (input_unsigned == true) && (input_zp != 0) && (input_zp != 32768)) |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 112 | { |
Eric Kunze | 9c0a507 | 2024-03-19 21:14:07 +0000 | [diff] [blame] | 113 | printNodeValidationError("OpRescale: Input unsigned int16 and zero point not 0 or 32768"); |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 114 | return 1; |
| 115 | } |
| 116 | |
Eric Kunze | 9c0a507 | 2024-03-19 21:14:07 +0000 | [diff] [blame] | 117 | if (isI16(OutDtype) && (output_unsigned == true) && (output_zp != 0) && (output_zp != 32768)) |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 118 | { |
Eric Kunze | 9c0a507 | 2024-03-19 21:14:07 +0000 | [diff] [blame] | 119 | printNodeValidationError("OpRescale: Output unsigned int16 and zero point not 0 or 32768"); |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 120 | return 1; |
| 121 | } |
| 122 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 123 | if (attribute->scale32() && (InDtype == TOSA_REF_TYPE_INT48)) |
Kevin Cheng | cc61be3 | 2021-10-14 17:09:57 -0700 | [diff] [blame] | 124 | { |
| 125 | printNodeValidationError("OpRescale: Scale set to true but input type is INT48"); |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | if ((!attribute->scale32()) && attribute->double_round()) |
| 130 | { |
| 131 | printNodeValidationError("OpRescale: Scale set to false but double round set to true"); |
| 132 | return 1; |
| 133 | } |
| 134 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 138 | // helpers to convert types |
| 139 | static int64_t zero_extend(int8_t val) |
| 140 | { |
| 141 | uint8_t* rval = reinterpret_cast<uint8_t*>(&val); |
| 142 | return static_cast<int64_t>(*rval); |
| 143 | } |
| 144 | static int64_t zero_extend(int16_t val) |
| 145 | { |
| 146 | uint16_t* rval = reinterpret_cast<uint16_t*>(&val); |
| 147 | return static_cast<int64_t>(*rval); |
| 148 | } |
| 149 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 150 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 151 | int OpRescale<Rank, InDtype, OutDtype>::eval() |
| 152 | { |
Tai Ly | 6e1e2bc | 2024-03-01 20:59:32 +0000 | [diff] [blame] | 153 | int32_t input_zp = attribute->input_zp(); |
| 154 | int32_t output_zp = attribute->output_zp(); |
| 155 | std::vector<int32_t> multiplier; |
| 156 | std::vector<int32_t> shift; |
| 157 | bool scale32 = attribute->scale32(); |
| 158 | bool double_round = attribute->double_round(); |
| 159 | bool per_channel = attribute->per_channel(); |
| 160 | bool input_unsigned = attribute->input_unsigned(); |
| 161 | bool output_unsigned = attribute->output_unsigned(); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 162 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 163 | // reshape [d0, d1, ..., dn] into [d0 * d1 ..., dn] |
| 164 | Eigen::array<Eigen::Index, 2> shape_2d; |
| 165 | shape_2d[0] = 1; |
| 166 | if (Rank > 0) |
| 167 | { |
| 168 | for (int i = 0; i < Rank - 1; i++) |
| 169 | { |
| 170 | shape_2d[0] *= this->in->getShape()[i]; |
| 171 | } |
| 172 | shape_2d[1] = this->in->getShape()[Rank - 1]; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | shape_2d[1] = 1; |
| 177 | } |
| 178 | ETensor2<InEigenType> input_reshaped = this->in->getTensor().reshape(shape_2d); |
| 179 | |
| 180 | ETensor2<OutEigenType> output_2d(shape_2d); |
| 181 | |
Tai Ly | 6e1e2bc | 2024-03-01 20:59:32 +0000 | [diff] [blame] | 182 | if (scale32) |
| 183 | { |
| 184 | auto multiplier_val = this->multiplierI32->getTensor(); |
| 185 | for (int i = 0; i < multiplier_val.size(); i++) |
| 186 | { |
| 187 | multiplier.push_back(static_cast<int32_t>(multiplier_val(i))); |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | auto multiplier_val = this->multiplierI16->getTensor(); |
| 193 | for (int i = 0; i < multiplier_val.size(); i++) |
| 194 | { |
| 195 | multiplier.push_back(static_cast<int32_t>(multiplier_val(i))); |
| 196 | } |
| 197 | } |
| 198 | auto shift_val = this->shift->getTensor(); |
| 199 | for (int i = 0; i < shift_val.size(); i++) |
| 200 | { |
| 201 | shift.push_back(static_cast<int32_t>(shift_val(i))); |
| 202 | } |
| 203 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 204 | if (per_channel) |
| 205 | { |
| 206 | ETensor2<InEigenType> curr_channel_slice_prescaled; |
| 207 | ETensor2<OutEigenType> curr_channel_slice_postscaled; |
| 208 | int32_t channel_multiplier, channel_shift; |
| 209 | Eigen::array<Eigen::Index, 2> begin, size; |
| 210 | size = Eigen::array<Eigen::Index, 2>({ shape_2d[0], 1 }); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 211 | try |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 212 | { |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 213 | for (int32_t i = 0; i < shape_2d[1]; i++) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 214 | { |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 215 | begin = Eigen::array<Eigen::Index, 2>({ 0, i }); |
| 216 | curr_channel_slice_prescaled = input_reshaped.slice(begin, size); |
| 217 | channel_multiplier = multiplier[i]; |
| 218 | channel_shift = shift[i]; |
| 219 | curr_channel_slice_postscaled = |
| 220 | curr_channel_slice_prescaled.unaryExpr([=](InEigenType in_val) -> OutEigenType { |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 221 | int64_t input_zp_shifted; |
| 222 | if (input_unsigned) |
| 223 | { |
| 224 | int64_t in_val64; |
| 225 | int64_t in_zp64; |
| 226 | switch (GetNumBits<InDtype>::value) |
| 227 | { |
| 228 | case 8: |
| 229 | in_val64 = zero_extend(static_cast<int8_t>(in_val)); |
| 230 | in_zp64 = zero_extend(static_cast<int8_t>(input_zp)); |
| 231 | break; |
| 232 | case 16: |
| 233 | in_val64 = zero_extend(static_cast<int16_t>(in_val)); |
| 234 | in_zp64 = zero_extend(static_cast<int16_t>(input_zp)); |
| 235 | break; |
| 236 | default: |
| 237 | in_val64 = static_cast<int64_t>(in_val); |
| 238 | in_zp64 = static_cast<int64_t>(input_zp); |
| 239 | break; |
| 240 | } |
| 241 | input_zp_shifted = in_val64 - in_zp64; |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | input_zp_shifted = in_val - input_zp; |
| 246 | } |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 247 | int32_t scaled; |
| 248 | if (scale32) |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 249 | scaled = TosaReference::QuantUtil::apply_scale_32(static_cast<int32_t>(input_zp_shifted), |
| 250 | channel_multiplier, channel_shift, |
| 251 | double_round); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 252 | else |
| 253 | scaled = TosaReference::QuantUtil::apply_scale_16(input_zp_shifted, channel_multiplier, |
| 254 | channel_shift); |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 255 | int64_t output_zp_extended; |
| 256 | if (output_unsigned) |
| 257 | { |
| 258 | switch (GetNumBits<OutDtype>::value) |
| 259 | { |
| 260 | case 8: |
| 261 | output_zp_extended = zero_extend(static_cast<int8_t>(output_zp)); |
| 262 | break; |
| 263 | case 16: |
| 264 | output_zp_extended = zero_extend(static_cast<int16_t>(output_zp)); |
| 265 | break; |
| 266 | default: |
| 267 | output_zp_extended = static_cast<int64_t>(output_zp); |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | output_zp_extended = static_cast<int64_t>(output_zp); |
| 274 | } |
TatWai Chong | 0dac6c9 | 2024-04-02 15:45:29 -0700 | [diff] [blame^] | 275 | |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 276 | int64_t res_in_64 = static_cast<int64_t>(scaled) + output_zp_extended; |
Jeremy Johnson | df628d4 | 2023-01-10 14:40:54 +0000 | [diff] [blame] | 277 | int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::max()); |
| 278 | int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::min()); |
TatWai Chong | 0dac6c9 | 2024-04-02 15:45:29 -0700 | [diff] [blame^] | 279 | |
Jeremy Johnson | df628d4 | 2023-01-10 14:40:54 +0000 | [diff] [blame] | 280 | if (res_in_64 > i32_max_in_64 || res_in_64 < i32_min_in_64) |
| 281 | { |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 282 | std::string desc = "scaling result [" + std::to_string(scaled) + "] plus output_zp [" + |
| 283 | std::to_string(output_zp) + "] not in i32 range"; |
Jeremy Johnson | df628d4 | 2023-01-10 14:40:54 +0000 | [diff] [blame] | 284 | throw desc; |
| 285 | } |
TatWai Chong | 0dac6c9 | 2024-04-02 15:45:29 -0700 | [diff] [blame^] | 286 | |
| 287 | // Treat the output values as unsigned if `output_unsigned` is true. |
| 288 | int32_t clipped_val = (output_unsigned) |
| 289 | ? applyClip<int32_t, uint32_t>(res_in_64, QMin_u, QMax_u) |
| 290 | : applyClip<int32_t, int32_t>(res_in_64, QMin_s, QMax_s); |
| 291 | |
| 292 | OutEigenType out_val = static_cast<OutEigenType>(clipped_val); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 293 | return out_val; |
| 294 | }); |
| 295 | |
| 296 | for (int32_t j = 0; j < shape_2d[0]; j++) |
| 297 | { |
| 298 | output_2d(j, i) = curr_channel_slice_postscaled(j, 0); |
| 299 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 302 | catch (std::string desc) |
| 303 | { |
Jeremy Johnson | df628d4 | 2023-01-10 14:40:54 +0000 | [diff] [blame] | 304 | REQUIRE(false, "OpRescale failure: %s.", desc.c_str()); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 305 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 306 | } |
| 307 | else |
| 308 | { |
| 309 | int32_t tensor_multiplier = multiplier[0]; |
| 310 | int32_t tensor_shift = shift[0]; |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 311 | try |
| 312 | { |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 313 | output_2d = input_reshaped.unaryExpr([=](InEigenType in_val) -> OutEigenType { |
| 314 | int64_t input_zp_shifted; |
| 315 | if (input_unsigned) |
| 316 | { |
| 317 | int64_t in_val64; |
| 318 | int64_t in_zp64; |
| 319 | switch (GetNumBits<InDtype>::value) |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 320 | { |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 321 | case 8: |
| 322 | in_val64 = zero_extend(static_cast<int8_t>(in_val)); |
| 323 | in_zp64 = zero_extend(static_cast<int8_t>(input_zp)); |
| 324 | break; |
| 325 | case 16: |
| 326 | in_val64 = zero_extend(static_cast<int16_t>(in_val)); |
| 327 | in_zp64 = zero_extend(static_cast<int16_t>(input_zp)); |
| 328 | break; |
| 329 | default: |
| 330 | in_val64 = static_cast<int64_t>(in_val); |
| 331 | in_zp64 = static_cast<int64_t>(input_zp); |
| 332 | break; |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 333 | } |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 334 | input_zp_shifted = in_val64 - in_zp64; |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | input_zp_shifted = in_val - input_zp; |
| 339 | } |
| 340 | int32_t scaled; |
| 341 | if (scale32) |
| 342 | scaled = TosaReference::QuantUtil::apply_scale_32(input_zp_shifted, tensor_multiplier, tensor_shift, |
| 343 | double_round); |
| 344 | else |
| 345 | scaled = |
| 346 | TosaReference::QuantUtil::apply_scale_16(input_zp_shifted, tensor_multiplier, tensor_shift); |
Jeremy Johnson | df628d4 | 2023-01-10 14:40:54 +0000 | [diff] [blame] | 347 | |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 348 | int64_t output_zp_extended; |
| 349 | if (output_unsigned) |
| 350 | { |
| 351 | switch (GetNumBits<OutDtype>::value) |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 352 | { |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 353 | case 8: |
| 354 | output_zp_extended = zero_extend(static_cast<int8_t>(output_zp)); |
| 355 | break; |
| 356 | case 16: |
| 357 | output_zp_extended = zero_extend(static_cast<int16_t>(output_zp)); |
| 358 | break; |
| 359 | default: |
| 360 | output_zp_extended = static_cast<int64_t>(output_zp); |
| 361 | break; |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 362 | } |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 363 | } |
| 364 | else |
| 365 | { |
| 366 | output_zp_extended = static_cast<int64_t>(output_zp); |
| 367 | } |
| 368 | int64_t res_in_64 = static_cast<int64_t>(scaled) + output_zp_extended; |
| 369 | int64_t i32_max_in_64 = IsSignedInt<OutDtype>() |
| 370 | ? static_cast<int64_t>(std::numeric_limits<int32_t>::max()) |
| 371 | : static_cast<int64_t>(std::numeric_limits<uint32_t>::max()); |
| 372 | int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::min()); |
Eric Kunze | a8098c0 | 2023-09-07 00:31:54 +0000 | [diff] [blame] | 373 | |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 374 | if (res_in_64 > i32_max_in_64 || res_in_64 < i32_min_in_64) |
| 375 | { |
| 376 | std::string desc = "scaling result [" + std::to_string(scaled) + "] plus output_zp [" + |
| 377 | std::to_string(output_zp) + "] not in i32 range"; |
| 378 | throw desc; |
| 379 | } |
| 380 | |
TatWai Chong | 0dac6c9 | 2024-04-02 15:45:29 -0700 | [diff] [blame^] | 381 | // Treat the output values as unsigned if `output_unsigned` is true. |
| 382 | int32_t clipped_val = (output_unsigned) ? applyClip<int32_t, uint32_t>(res_in_64, QMin_u, QMax_u) |
| 383 | : applyClip<int32_t, int32_t>(res_in_64, QMin_s, QMax_s); |
| 384 | |
| 385 | OutEigenType out_val = static_cast<OutEigenType>(clipped_val); |
TatWai Chong | 08fe7a5 | 2024-03-21 14:34:33 -0700 | [diff] [blame] | 386 | return out_val; |
| 387 | }); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 388 | } |
| 389 | catch (std::string desc) |
| 390 | { |
Jeremy Johnson | df628d4 | 2023-01-10 14:40:54 +0000 | [diff] [blame] | 391 | REQUIRE(false, "OpRescale failure: %s.", desc.c_str()); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 392 | } |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // reshape [d0 * d1 ..., dn] back to [d0, d1, ..., dn] |
| 396 | Eigen::array<Eigen::Index, Rank> output_shape; |
| 397 | for (int i = 0; i < Rank; i++) |
| 398 | { |
| 399 | output_shape[i] = this->out->getShape()[i]; |
| 400 | } |
| 401 | this->out->getTensor() = output_2d.reshape(output_shape); |
| 402 | |
| 403 | return GraphNode::eval(); |
| 404 | } |
| 405 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 406 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 407 | OpCast<Rank, InDtype, OutDtype>::OpCast(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 408 | : GraphNode(sgt_, Op_CAST, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 409 | { |
| 410 | setRequiredOperands(1, 1); |
| 411 | setRequiredRank(0, 6); |
| 412 | } |
| 413 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 414 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 415 | OpCast<Rank, InDtype, OutDtype>::~OpCast() |
| 416 | {} |
| 417 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 418 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 419 | int OpCast<Rank, InDtype, OutDtype>::checkTensorAttributes() |
| 420 | { |
Jerry Ge | a793f46 | 2023-04-11 00:05:02 +0000 | [diff] [blame] | 421 | // Check Tosa Level |
| 422 | auto tosa_level = g_func_config.tosa_level; |
| 423 | LEVEL_CHECK(Rank <= tosa_level.MAX_RANK, "Rank should be smaller than or equal to MAX_RANK"); |
| 424 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 425 | if (validateRequiredOperands()) |
| 426 | return 1; |
| 427 | |
| 428 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0])) |
| 429 | { |
| 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | // output and input must be the same rank and size |
| 434 | if (inputs[0]->matchRankSize(*outputs[0])) |
| 435 | { |
| 436 | printNodeValidationError("OpCast: input and output rank/size must match"); |
| 437 | return 1; |
| 438 | } |
| 439 | |
| 440 | in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 441 | out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 442 | |
| 443 | ASSERT_MEM(in && out); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 448 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 449 | int OpCast<Rank, InDtype, OutDtype>::eval() |
| 450 | { |
| 451 | this->out->getTensor() = this->in->getTensor().unaryExpr(cast_helper.get_fcn()); |
| 452 | |
| 453 | return GraphNode::eval(); |
| 454 | } |
| 455 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 456 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 457 | CastHelper<InDtype, OutDtype>::CastHelper() |
| 458 | { |
| 459 | fcn = [](InEigenType in) -> OutEigenType { |
| 460 | OutEigenType out = (OutEigenType)in; // implicit sign_extend() if sizeof(out_t) >= sizeof(in_t) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 461 | return out; |
| 462 | }; |
| 463 | } |
| 464 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 465 | template <TOSA_REF_TYPE InDtype> |
| 466 | CastHelper<InDtype, TOSA_REF_TYPE_BOOL>::CastHelper() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 467 | { |
| 468 | fcn = [](InEigenType in) -> bool { return (in != 0) ? true : false; }; |
| 469 | } |
| 470 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 471 | template <TOSA_REF_TYPE OutDtype> |
| 472 | CastHelper<TOSA_REF_TYPE_BOOL, OutDtype>::CastHelper() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 473 | { |
| 474 | fcn = [](bool in) -> OutEigenType { |
| 475 | OutEigenType out = in ? (OutEigenType)1 : (OutEigenType)0; |
| 476 | return out; |
| 477 | }; |
| 478 | } |
| 479 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 480 | template <TOSA_REF_TYPE InDtype> |
| 481 | CastHelper<InDtype, TOSA_REF_TYPE_FP16>::CastHelper() |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 482 | { |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 483 | // Integer data converted to fp16 (stored as fp32) |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 484 | fcn = [](InEigenType in) -> float { |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 485 | half_float::half h = half_float::half(in); |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 486 | float out = half_float::half_cast<float, half_float::half>(h); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 487 | return out; |
| 488 | }; |
| 489 | } |
| 490 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 491 | CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_FP16>::CastHelper() |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 492 | { |
| 493 | // fp32 data converted to fp16 (stored as fp32) |
| 494 | fcn = [](float in) -> float { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 495 | float out = fpTrunc<TOSA_REF_TYPE_FP16>(in); // truncate required for conversion from higher precision |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 496 | return out; |
| 497 | }; |
| 498 | } |
| 499 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 500 | template <TOSA_REF_TYPE InDtype> |
| 501 | CastHelper<InDtype, TOSA_REF_TYPE_BF16>::CastHelper() |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 502 | { |
| 503 | // Integer data converted to bf16 (stored as fp32) |
| 504 | fcn = [](InEigenType in) -> float { |
| 505 | float out = (float)in; // default cast to float is round_to_nearest_float() |
| 506 | return out; |
| 507 | }; |
| 508 | } |
| 509 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 510 | CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_BF16>::CastHelper() |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 511 | { |
| 512 | // fp32 data converted to bf16 (stored as fp32) |
| 513 | fcn = [](float in) -> float { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 514 | return fpTrunc<TOSA_REF_TYPE_BF16>(in); // truncate required for conversions from higher precision |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 515 | }; |
| 516 | } |
| 517 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 518 | template <TOSA_REF_TYPE OutDtype> |
| 519 | CastHelper<TOSA_REF_TYPE_FP16, OutDtype>::CastHelper() |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 520 | { |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 521 | // fp16 data (stored as fp32) converted to integer |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 522 | fcn = [](float in) -> OutEigenType { |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 523 | // Cast from float representation back to half_float before rounding |
| 524 | half_float::half h = half_float::half(in); |
Jerry Ge | d6a0461 | 2023-12-15 22:45:39 +0000 | [diff] [blame] | 525 | if (h >= half_float::half(float(OutMax))) |
| 526 | return OutMax; |
| 527 | |
| 528 | if (h <= half_float::half(float(OutMin))) |
| 529 | return OutMin; |
| 530 | |
| 531 | h = std::rint(h); |
| 532 | OutEigenType out = half_float::half_cast<OutEigenType, half_float::half>(h); |
| 533 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 534 | return out; |
| 535 | }; |
| 536 | } |
| 537 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 538 | CastHelper<TOSA_REF_TYPE_FP16, TOSA_REF_TYPE_FP32>::CastHelper() |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 539 | { |
| 540 | // No-op since fp16 values treated internally as their fp32 representation |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 541 | fcn = [](float in) -> OutEigenType { return in; }; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 544 | template <TOSA_REF_TYPE OutDtype> |
| 545 | CastHelper<TOSA_REF_TYPE_BF16, OutDtype>::CastHelper() |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 546 | { |
| 547 | // bf16 data (stored as fp32) converted to integer |
| 548 | fcn = [](float in) -> OutEigenType { |
Jerry Ge | d6a0461 | 2023-12-15 22:45:39 +0000 | [diff] [blame] | 549 | if (in >= float(OutMax)) |
| 550 | return OutMax; |
| 551 | |
| 552 | if (in <= float(OutMin)) |
| 553 | return OutMin; |
| 554 | |
| 555 | OutEigenType out = std::rint(in); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 556 | return out; |
| 557 | }; |
| 558 | } |
| 559 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 560 | CastHelper<TOSA_REF_TYPE_BF16, TOSA_REF_TYPE_FP32>::CastHelper() |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 561 | { |
| 562 | // No-op since bf16 values treated as truncated fp32 internally |
Jerry Ge | 9c9c8da | 2023-07-19 23:08:16 +0000 | [diff] [blame] | 563 | fcn = [](InEigenType in) -> OutEigenType { return in; }; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 566 | template <TOSA_REF_TYPE InDtype> |
| 567 | CastHelper<InDtype, TOSA_REF_TYPE_FP32>::CastHelper() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 568 | { |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 569 | // Integer data converted to fp32 |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 570 | fcn = [](InEigenType in) -> float { |
| 571 | float out = (OutEigenType)in; // default cast to float is round_to_nearest_float() |
| 572 | return out; |
| 573 | }; |
| 574 | } |
| 575 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 576 | template <TOSA_REF_TYPE OutDtype> |
| 577 | CastHelper<TOSA_REF_TYPE_FP32, OutDtype>::CastHelper() |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 578 | { |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 579 | // fp32 data converted to integer |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 580 | fcn = [](float in) -> OutEigenType { |
Jerry Ge | 44827be | 2023-12-15 00:05:37 +0000 | [diff] [blame] | 581 | if (in >= float(OutMax)) |
| 582 | return OutMax; |
| 583 | |
| 584 | if (in <= float(OutMin)) |
| 585 | return OutMin; |
| 586 | |
Eric Kunze | 57bc079 | 2023-01-25 10:05:51 -0800 | [diff] [blame] | 587 | OutEigenType out = std::rint(in); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 588 | return out; |
| 589 | }; |
| 590 | } |
| 591 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 592 | template <TOSA_REF_TYPE OutDtype> |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 593 | CastHelper<TOSA_REF_TYPE_FP8E4M3, OutDtype>::CastHelper() |
| 594 | { |
| 595 | // fp8e4m3 data (stored as fp32) converted to integer |
| 596 | fcn = [](float in) -> OutEigenType { |
| 597 | if (in >= float(OutMax)) |
| 598 | return OutMax; |
| 599 | if (in <= float(OutMin)) |
| 600 | return OutMin; |
| 601 | |
| 602 | OutEigenType out = std::rint(in); |
| 603 | return out; |
| 604 | }; |
| 605 | } |
| 606 | |
| 607 | CastHelper<TOSA_REF_TYPE_FP8E4M3, TOSA_REF_TYPE_FP16>::CastHelper() |
| 608 | { |
| 609 | // fp8e4m3 data (stored as fp32) converted to fp16 (stored as fp32) |
| 610 | fcn = [](float in) -> float { |
| 611 | half_float::half h = half_float::half(in); |
| 612 | float out = half_float::half_cast<half_float::half, float>(h); |
| 613 | return out; |
| 614 | }; |
| 615 | } |
| 616 | |
| 617 | CastHelper<TOSA_REF_TYPE_FP8E4M3, TOSA_REF_TYPE_BF16>::CastHelper() |
| 618 | { |
| 619 | // fp8e4m3 data (stored as fp32) converted to bf16 (stored as fp32) |
| 620 | fcn = [](float in) -> float { return (float)in; }; |
| 621 | } |
| 622 | |
| 623 | CastHelper<TOSA_REF_TYPE_FP8E4M3, TOSA_REF_TYPE_FP32>::CastHelper() |
| 624 | { |
| 625 | // fp8e4m3 data (stored as fp32) converted to fp32 |
| 626 | fcn = [](InEigenType in) -> OutEigenType { return in; }; |
| 627 | } |
| 628 | |
| 629 | template <TOSA_REF_TYPE OutDtype> |
| 630 | CastHelper<TOSA_REF_TYPE_FP8E5M2, OutDtype>::CastHelper() |
| 631 | { |
| 632 | // fp8e5m2 data (stored as fp32) converted to integer |
| 633 | fcn = [](float in) -> OutEigenType { |
| 634 | if (in >= float(OutMax)) |
| 635 | return OutMax; |
| 636 | if (in <= float(OutMin)) |
| 637 | return OutMin; |
| 638 | |
| 639 | OutEigenType out = std::rint(in); |
| 640 | return out; |
| 641 | }; |
| 642 | } |
| 643 | |
| 644 | CastHelper<TOSA_REF_TYPE_FP8E5M2, TOSA_REF_TYPE_FP16>::CastHelper() |
| 645 | { |
| 646 | // fp8e5m2 data (stored as fp32) converted to fp16 (stored as fp32) |
| 647 | fcn = [](float in) -> float { |
| 648 | half_float::half h = half_float::half(in); |
| 649 | float out = half_float::half_cast<half_float::half, float>(h); |
| 650 | return out; |
| 651 | }; |
| 652 | } |
| 653 | |
| 654 | CastHelper<TOSA_REF_TYPE_FP8E5M2, TOSA_REF_TYPE_BF16>::CastHelper() |
| 655 | { |
| 656 | // fp8e5m2 data (stored as fp32) converted to bf16 (stored as fp32) |
| 657 | fcn = [](float in) -> float { return (float)in; }; |
| 658 | } |
| 659 | |
| 660 | CastHelper<TOSA_REF_TYPE_FP8E5M2, TOSA_REF_TYPE_FP32>::CastHelper() |
| 661 | { |
| 662 | // fp8e5m2 data (stored as fp32) converted to fp32 |
| 663 | fcn = [](InEigenType in) -> OutEigenType { return in; }; |
| 664 | } |
| 665 | |
| 666 | template <TOSA_REF_TYPE InDtype> |
| 667 | CastHelper<InDtype, TOSA_REF_TYPE_FP8E4M3>::CastHelper() |
| 668 | { |
| 669 | // Integer data converted to fp8e4m3 (stored as fp32) |
| 670 | fcn = [](InEigenType in) -> float { |
| 671 | auto f = static_cast<fp32>(static_cast<fp8e4m3>(float(in))); |
| 672 | float out = static_cast<float>(f); |
| 673 | return out; |
| 674 | }; |
| 675 | } |
| 676 | |
| 677 | CastHelper<TOSA_REF_TYPE_FP16, TOSA_REF_TYPE_FP8E4M3>::CastHelper() |
| 678 | { |
| 679 | // fp16 data (stored as fp32) converted to fp8e4m3 (stored as fp32) |
| 680 | fcn = [](float in) -> float { |
| 681 | auto f = static_cast<fp32>(static_cast<fp8e4m3>(in)); |
| 682 | float out = static_cast<float>(f); |
| 683 | return out; |
| 684 | }; |
| 685 | } |
| 686 | |
| 687 | CastHelper<TOSA_REF_TYPE_BF16, TOSA_REF_TYPE_FP8E4M3>::CastHelper() |
| 688 | { |
| 689 | // bf16 data (stored as fp32) converted to fp8e4m3 (stored as fp32) |
| 690 | fcn = [](float in) -> float { |
| 691 | auto f = static_cast<fp32>(static_cast<fp8e4m3>(in)); |
| 692 | float out = static_cast<float>(f); |
| 693 | return out; |
| 694 | }; |
| 695 | } |
| 696 | |
| 697 | CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_FP8E4M3>::CastHelper() |
| 698 | { |
| 699 | // fp32 data converted to fp8e4m3 (stored as fp32) |
| 700 | fcn = [](float in) -> float { |
| 701 | auto f = static_cast<fp32>(static_cast<fp8e4m3>(in)); |
| 702 | float out = static_cast<float>(f); |
| 703 | return out; |
| 704 | }; |
| 705 | } |
| 706 | |
| 707 | template <TOSA_REF_TYPE InDtype> |
| 708 | CastHelper<InDtype, TOSA_REF_TYPE_FP8E5M2>::CastHelper() |
| 709 | { |
| 710 | // Integer data converted to fp8e5m2 (stored as fp32) |
| 711 | fcn = [](InEigenType in) -> float { |
| 712 | auto f = static_cast<fp32>(static_cast<fp8e5m2>(float(in))); |
| 713 | float out = static_cast<float>(f); |
| 714 | return out; |
| 715 | }; |
| 716 | } |
| 717 | |
| 718 | CastHelper<TOSA_REF_TYPE_FP16, TOSA_REF_TYPE_FP8E5M2>::CastHelper() |
| 719 | { |
| 720 | // fp16 data (stored as fp32) converted to fp8e5m2 (stored as fp32) |
| 721 | fcn = [](float in) -> float { |
| 722 | auto f = static_cast<fp32>(static_cast<fp8e5m2>(in)); |
| 723 | float out = static_cast<float>(f); |
| 724 | return out; |
| 725 | }; |
| 726 | } |
| 727 | |
| 728 | CastHelper<TOSA_REF_TYPE_BF16, TOSA_REF_TYPE_FP8E5M2>::CastHelper() |
| 729 | { |
| 730 | // bf16 data (stored as fp32) converted to fp8e5m2 (stored as fp32) |
| 731 | fcn = [](float in) -> float { |
| 732 | auto f = static_cast<fp32>(static_cast<fp8e5m2>(in)); |
| 733 | float out = static_cast<float>(f); |
| 734 | return out; |
| 735 | }; |
| 736 | } |
| 737 | |
| 738 | CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_FP8E5M2>::CastHelper() |
| 739 | { |
| 740 | // fp32 data converted to fp8e5m2 (stored as fp32) |
| 741 | fcn = [](float in) -> float { |
| 742 | auto f = static_cast<fp32>(static_cast<fp8e5m2>(in)); |
| 743 | float out = static_cast<float>(f); |
| 744 | return out; |
| 745 | }; |
| 746 | } |
| 747 | |
| 748 | template <TOSA_REF_TYPE OutDtype> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 749 | CastHelper<TOSA_REF_TYPE_FP64, OutDtype>::CastHelper() |
| 750 | { |
| 751 | switch (OutDtype) |
| 752 | { |
| 753 | case TOSA_REF_TYPE_INT8: |
| 754 | case TOSA_REF_TYPE_INT16: |
| 755 | case TOSA_REF_TYPE_INT32: |
| 756 | // fp64 data converted to integer |
| 757 | fcn = [](InEigenType in) -> OutEigenType { |
Jerry Ge | d6a0461 | 2023-12-15 22:45:39 +0000 | [diff] [blame] | 758 | if (in >= double(OutMax)) |
| 759 | return OutMax; |
| 760 | |
| 761 | if (in <= double(OutMin)) |
| 762 | return OutMin; |
| 763 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 764 | OutEigenType out = std::rint(in); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 765 | return out; |
| 766 | }; |
| 767 | break; |
| 768 | case TOSA_REF_TYPE_FP64: |
| 769 | // no op |
| 770 | fcn = [](InEigenType in) -> OutEigenType { return in; }; |
| 771 | break; |
| 772 | default: |
| 773 | ASSERT_MSG(false, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(OutDtype)); |
| 774 | } |
| 775 | } |
| 776 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 777 | // template explicit instantiation |
| 778 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BOOL, INT8); |
| 779 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BOOL, INT16); |
| 780 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BOOL, INT32); |
| 781 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, BOOL); |
| 782 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, INT16); |
| 783 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, INT32); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 784 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, FP16); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 785 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 786 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, FP32); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 787 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, BOOL); |
| 788 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, INT8); |
| 789 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, INT32); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 790 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, FP16); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 791 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 792 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, FP32); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 793 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, BOOL); |
| 794 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, INT8); |
| 795 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, INT16); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 796 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, FP16); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 797 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, BF16); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 798 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, FP32); |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 799 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT8); |
| 800 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT16); |
| 801 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, INT32); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 802 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, FP32); |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 803 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT8); |
| 804 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT16); |
| 805 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, INT32); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 806 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, FP32); |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 807 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT8); |
| 808 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT16); |
| 809 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, INT32); |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 810 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, FP16); |
| 811 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, BF16); |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 812 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP64, INT8); |
| 813 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP64, INT16); |
| 814 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP64, INT32); |
| 815 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP64, FP64); |
| 816 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT8, FP64); |
| 817 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT16, FP64); |
| 818 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, INT32, FP64); |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 819 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, FP8E4M3); |
| 820 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, BF16, FP8E5M2); |
| 821 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP8E4M3, FP16); |
| 822 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP8E4M3, BF16); |
| 823 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP8E4M3, FP32); |
| 824 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP8E5M2, FP16); |
| 825 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP8E5M2, BF16); |
| 826 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP8E5M2, FP32); |
| 827 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, FP8E4M3); |
| 828 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP16, FP8E5M2); |
| 829 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, FP8E4M3); |
| 830 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpCast, FP32, FP8E5M2); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 831 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 832 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT8, INT8); |
| 833 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT8, INT16); |
| 834 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT8, INT32); |
| 835 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT16, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 836 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT16, INT16); |
| 837 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT16, INT32); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 838 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT32, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 839 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT32, INT16); |
| 840 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT32, INT32); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 841 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT48, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 842 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT48, INT16); |
| 843 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT48, INT32); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 844 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, UINT8, INT8); |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 845 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, UINT8, INT16); |
| 846 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, UINT16, INT16); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 847 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT8, UINT8); |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 848 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT16, UINT8); |
| 849 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpRescale, INT16, UINT16); |