Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +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 | #ifndef OPS_TYPE_CONVERSION_H |
| 17 | #define OPS_TYPE_CONVERSION_H |
| 18 | |
| 19 | #include "graph_node.h" |
| 20 | |
| 21 | using namespace tosa; |
| 22 | |
| 23 | namespace TosaReference |
| 24 | { |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 25 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 26 | class OpRescale : public GraphNode |
| 27 | { |
| 28 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 29 | OpRescale(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 30 | virtual ~OpRescale(); |
| 31 | |
| 32 | virtual int checkTensorAttributes() final; |
| 33 | virtual int eval() final; |
| 34 | |
Tai Ly | 6e1e2bc | 2024-03-01 20:59:32 +0000 | [diff] [blame^] | 35 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 36 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 37 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 38 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 39 | using I8EigenType = typename GetEigenType<TOSA_REF_TYPE::TOSA_REF_TYPE_INT8>::type; |
| 40 | using I16EigenType = typename GetEigenType<TOSA_REF_TYPE::TOSA_REF_TYPE_INT16>::type; |
| 41 | using I32EigenType = typename GetEigenType<TOSA_REF_TYPE::TOSA_REF_TYPE_INT32>::type; |
| 42 | using TMultiplierI16 = Eigen::Tensor<I16EigenType, 1>; |
| 43 | using TMultiplierI32 = Eigen::Tensor<I32EigenType, 1>; |
| 44 | using TShift = Eigen::Tensor<I8EigenType, 1>; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 45 | |
| 46 | static constexpr int32_t QMin = GetQMin<OutDtype>::value; |
| 47 | static constexpr int32_t QMax = GetQMax<OutDtype>::value; |
| 48 | |
| 49 | protected: |
| 50 | TosaRescaleAttribute* attribute; |
| 51 | TosaReference::TensorTemplate<TIn>* in; |
| 52 | TosaReference::TensorTemplate<TOut>* out; |
Tai Ly | 6e1e2bc | 2024-03-01 20:59:32 +0000 | [diff] [blame^] | 53 | TosaReference::TensorTemplate<TMultiplierI16>* multiplierI16; |
| 54 | TosaReference::TensorTemplate<TMultiplierI32>* multiplierI32; |
| 55 | TosaReference::TensorTemplate<TShift>* shift; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 58 | template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 59 | class CastHelper |
| 60 | { |
| 61 | public: |
| 62 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 63 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 64 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 65 | static constexpr int32_t OutBits = GetNumBits<OutDtype>::value; |
| 66 | CastHelper(); |
| 67 | const FcnType& get_fcn() const |
| 68 | { |
| 69 | return fcn; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | FcnType fcn; |
| 74 | }; |
| 75 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 76 | template <TOSA_REF_TYPE InDtype> |
| 77 | class CastHelper<InDtype, TOSA_REF_TYPE_BOOL> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 78 | { |
| 79 | public: |
| 80 | using InEigenType = typename GetEigenType<InDtype>::type; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 81 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_BOOL>::type; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 82 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 83 | CastHelper(); |
| 84 | const FcnType& get_fcn() const |
| 85 | { |
| 86 | return fcn; |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | FcnType fcn; |
| 91 | }; |
| 92 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 93 | template <TOSA_REF_TYPE OutDtype> |
| 94 | class CastHelper<TOSA_REF_TYPE_BOOL, OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 95 | { |
| 96 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 97 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_BOOL>::type; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 98 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 99 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 100 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 101 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 102 | CastHelper(); |
| 103 | const FcnType& get_fcn() const |
| 104 | { |
| 105 | return fcn; |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | FcnType fcn; |
| 110 | }; |
| 111 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 112 | template <TOSA_REF_TYPE InDtype> |
| 113 | class CastHelper<InDtype, TOSA_REF_TYPE_FP16> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 114 | { |
| 115 | public: |
| 116 | using InEigenType = typename GetEigenType<InDtype>::type; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 117 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 118 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 119 | CastHelper(); |
| 120 | const FcnType& get_fcn() const |
| 121 | { |
| 122 | return fcn; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | FcnType fcn; |
| 127 | }; |
| 128 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 129 | template <TOSA_REF_TYPE OutDtype> |
| 130 | class CastHelper<TOSA_REF_TYPE_FP16, OutDtype> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 131 | { |
| 132 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 133 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 134 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 135 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 136 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 137 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 138 | CastHelper(); |
| 139 | const FcnType& get_fcn() const |
| 140 | { |
| 141 | return fcn; |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | FcnType fcn; |
| 146 | }; |
| 147 | |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 148 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 149 | class CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_FP16> |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 150 | { |
| 151 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 152 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
| 153 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 154 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 155 | CastHelper(); |
| 156 | const FcnType& get_fcn() const |
| 157 | { |
| 158 | return fcn; |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | FcnType fcn; |
| 163 | }; |
| 164 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 165 | template <TOSA_REF_TYPE InDtype> |
| 166 | class CastHelper<InDtype, TOSA_REF_TYPE_BF16> |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 167 | { |
| 168 | public: |
| 169 | using InEigenType = typename GetEigenType<InDtype>::type; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 170 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 171 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 172 | CastHelper(); |
| 173 | const FcnType& get_fcn() const |
| 174 | { |
| 175 | return fcn; |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | FcnType fcn; |
| 180 | }; |
| 181 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 182 | template <TOSA_REF_TYPE OutDtype> |
| 183 | class CastHelper<TOSA_REF_TYPE_BF16, OutDtype> |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 184 | { |
| 185 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 186 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 187 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 188 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 189 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 190 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 191 | CastHelper(); |
| 192 | const FcnType& get_fcn() const |
| 193 | { |
| 194 | return fcn; |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | FcnType fcn; |
| 199 | }; |
| 200 | |
| 201 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 202 | class CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_BF16> |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 203 | { |
| 204 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 205 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
| 206 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 207 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 208 | CastHelper(); |
| 209 | const FcnType& get_fcn() const |
| 210 | { |
| 211 | return fcn; |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | FcnType fcn; |
| 216 | }; |
| 217 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 218 | template <TOSA_REF_TYPE InDtype> |
| 219 | class CastHelper<InDtype, TOSA_REF_TYPE_FP32> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 220 | { |
| 221 | public: |
| 222 | using InEigenType = typename GetEigenType<InDtype>::type; |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 223 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 224 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 225 | CastHelper(); |
| 226 | const FcnType& get_fcn() const |
| 227 | { |
| 228 | return fcn; |
| 229 | } |
| 230 | |
| 231 | private: |
| 232 | FcnType fcn; |
| 233 | }; |
| 234 | |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 235 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 236 | class CastHelper<TOSA_REF_TYPE_FP16, TOSA_REF_TYPE_FP32> |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 237 | { |
| 238 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 239 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
| 240 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 241 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 242 | CastHelper(); |
| 243 | const FcnType& get_fcn() const |
| 244 | { |
| 245 | return fcn; |
| 246 | } |
| 247 | |
| 248 | private: |
| 249 | FcnType fcn; |
| 250 | }; |
| 251 | |
| 252 | template <> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 253 | class CastHelper<TOSA_REF_TYPE_BF16, TOSA_REF_TYPE_FP32> |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 254 | { |
| 255 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 256 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
| 257 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 258 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 259 | CastHelper(); |
| 260 | const FcnType& get_fcn() const |
| 261 | { |
| 262 | return fcn; |
| 263 | } |
| 264 | |
| 265 | private: |
| 266 | FcnType fcn; |
| 267 | }; |
| 268 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 269 | template <TOSA_REF_TYPE OutDtype> |
| 270 | class CastHelper<TOSA_REF_TYPE_FP32, OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 271 | { |
| 272 | public: |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 273 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 274 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 275 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 276 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 277 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 278 | CastHelper(); |
| 279 | const FcnType& get_fcn() const |
| 280 | { |
| 281 | return fcn; |
| 282 | } |
| 283 | |
| 284 | private: |
| 285 | FcnType fcn; |
| 286 | }; |
| 287 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 288 | template <TOSA_REF_TYPE OutDtype> |
Won Jeon | 2c34b46 | 2024-02-06 18:37:00 +0000 | [diff] [blame] | 289 | class CastHelper<TOSA_REF_TYPE_FP8E4M3, OutDtype> |
| 290 | { |
| 291 | public: |
| 292 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 293 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 294 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 295 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 296 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 297 | CastHelper(); |
| 298 | const FcnType& get_fcn() const |
| 299 | { |
| 300 | return fcn; |
| 301 | } |
| 302 | |
| 303 | private: |
| 304 | FcnType fcn; |
| 305 | }; |
| 306 | |
| 307 | template <> |
| 308 | class CastHelper<TOSA_REF_TYPE_FP8E4M3, TOSA_REF_TYPE_FP16> |
| 309 | { |
| 310 | public: |
| 311 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 312 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
| 313 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 314 | CastHelper(); |
| 315 | const FcnType& get_fcn() const |
| 316 | { |
| 317 | return fcn; |
| 318 | } |
| 319 | |
| 320 | private: |
| 321 | FcnType fcn; |
| 322 | }; |
| 323 | |
| 324 | template <> |
| 325 | class CastHelper<TOSA_REF_TYPE_FP8E4M3, TOSA_REF_TYPE_BF16> |
| 326 | { |
| 327 | public: |
| 328 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 329 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
| 330 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 331 | CastHelper(); |
| 332 | const FcnType& get_fcn() const |
| 333 | { |
| 334 | return fcn; |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | FcnType fcn; |
| 339 | }; |
| 340 | |
| 341 | template <> |
| 342 | class CastHelper<TOSA_REF_TYPE_FP8E4M3, TOSA_REF_TYPE_FP32> |
| 343 | { |
| 344 | public: |
| 345 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 346 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
| 347 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 348 | CastHelper(); |
| 349 | const FcnType& get_fcn() const |
| 350 | { |
| 351 | return fcn; |
| 352 | } |
| 353 | |
| 354 | private: |
| 355 | FcnType fcn; |
| 356 | }; |
| 357 | |
| 358 | template <TOSA_REF_TYPE OutDtype> |
| 359 | class CastHelper<TOSA_REF_TYPE_FP8E5M2, OutDtype> |
| 360 | { |
| 361 | public: |
| 362 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 363 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 364 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 365 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 366 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 367 | CastHelper(); |
| 368 | const FcnType& get_fcn() const |
| 369 | { |
| 370 | return fcn; |
| 371 | } |
| 372 | |
| 373 | private: |
| 374 | FcnType fcn; |
| 375 | }; |
| 376 | |
| 377 | template <> |
| 378 | class CastHelper<TOSA_REF_TYPE_FP8E5M2, TOSA_REF_TYPE_FP16> |
| 379 | { |
| 380 | public: |
| 381 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 382 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
| 383 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 384 | CastHelper(); |
| 385 | const FcnType& get_fcn() const |
| 386 | { |
| 387 | return fcn; |
| 388 | } |
| 389 | |
| 390 | private: |
| 391 | FcnType fcn; |
| 392 | }; |
| 393 | |
| 394 | template <> |
| 395 | class CastHelper<TOSA_REF_TYPE_FP8E5M2, TOSA_REF_TYPE_BF16> |
| 396 | { |
| 397 | public: |
| 398 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 399 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
| 400 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 401 | CastHelper(); |
| 402 | const FcnType& get_fcn() const |
| 403 | { |
| 404 | return fcn; |
| 405 | } |
| 406 | |
| 407 | private: |
| 408 | FcnType fcn; |
| 409 | }; |
| 410 | |
| 411 | template <> |
| 412 | class CastHelper<TOSA_REF_TYPE_FP8E5M2, TOSA_REF_TYPE_FP32> |
| 413 | { |
| 414 | public: |
| 415 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 416 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
| 417 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 418 | CastHelper(); |
| 419 | const FcnType& get_fcn() const |
| 420 | { |
| 421 | return fcn; |
| 422 | } |
| 423 | |
| 424 | private: |
| 425 | FcnType fcn; |
| 426 | }; |
| 427 | |
| 428 | template <TOSA_REF_TYPE InDtype> |
| 429 | class CastHelper<InDtype, TOSA_REF_TYPE_FP8E4M3> |
| 430 | { |
| 431 | public: |
| 432 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 433 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 434 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 435 | CastHelper(); |
| 436 | const FcnType& get_fcn() const |
| 437 | { |
| 438 | return fcn; |
| 439 | } |
| 440 | |
| 441 | private: |
| 442 | FcnType fcn; |
| 443 | }; |
| 444 | |
| 445 | template <> |
| 446 | class CastHelper<TOSA_REF_TYPE_FP16, TOSA_REF_TYPE_FP8E4M3> |
| 447 | { |
| 448 | public: |
| 449 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
| 450 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 451 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 452 | CastHelper(); |
| 453 | const FcnType& get_fcn() const |
| 454 | { |
| 455 | return fcn; |
| 456 | } |
| 457 | |
| 458 | private: |
| 459 | FcnType fcn; |
| 460 | }; |
| 461 | |
| 462 | template <> |
| 463 | class CastHelper<TOSA_REF_TYPE_BF16, TOSA_REF_TYPE_FP8E4M3> |
| 464 | { |
| 465 | public: |
| 466 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
| 467 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 468 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 469 | CastHelper(); |
| 470 | const FcnType& get_fcn() const |
| 471 | { |
| 472 | return fcn; |
| 473 | } |
| 474 | |
| 475 | private: |
| 476 | FcnType fcn; |
| 477 | }; |
| 478 | |
| 479 | template <> |
| 480 | class CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_FP8E4M3> |
| 481 | { |
| 482 | public: |
| 483 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
| 484 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E4M3>::type; |
| 485 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 486 | CastHelper(); |
| 487 | const FcnType& get_fcn() const |
| 488 | { |
| 489 | return fcn; |
| 490 | } |
| 491 | |
| 492 | private: |
| 493 | FcnType fcn; |
| 494 | }; |
| 495 | |
| 496 | template <TOSA_REF_TYPE InDtype> |
| 497 | class CastHelper<InDtype, TOSA_REF_TYPE_FP8E5M2> |
| 498 | { |
| 499 | public: |
| 500 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 501 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 502 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 503 | CastHelper(); |
| 504 | const FcnType& get_fcn() const |
| 505 | { |
| 506 | return fcn; |
| 507 | } |
| 508 | |
| 509 | private: |
| 510 | FcnType fcn; |
| 511 | }; |
| 512 | |
| 513 | template <> |
| 514 | class CastHelper<TOSA_REF_TYPE_FP16, TOSA_REF_TYPE_FP8E5M2> |
| 515 | { |
| 516 | public: |
| 517 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP16>::type; |
| 518 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 519 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 520 | CastHelper(); |
| 521 | const FcnType& get_fcn() const |
| 522 | { |
| 523 | return fcn; |
| 524 | } |
| 525 | |
| 526 | private: |
| 527 | FcnType fcn; |
| 528 | }; |
| 529 | |
| 530 | template <> |
| 531 | class CastHelper<TOSA_REF_TYPE_BF16, TOSA_REF_TYPE_FP8E5M2> |
| 532 | { |
| 533 | public: |
| 534 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_BF16>::type; |
| 535 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 536 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 537 | CastHelper(); |
| 538 | const FcnType& get_fcn() const |
| 539 | { |
| 540 | return fcn; |
| 541 | } |
| 542 | |
| 543 | private: |
| 544 | FcnType fcn; |
| 545 | }; |
| 546 | |
| 547 | template <> |
| 548 | class CastHelper<TOSA_REF_TYPE_FP32, TOSA_REF_TYPE_FP8E5M2> |
| 549 | { |
| 550 | public: |
| 551 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP32>::type; |
| 552 | using OutEigenType = typename GetEigenType<TOSA_REF_TYPE_FP8E5M2>::type; |
| 553 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 554 | CastHelper(); |
| 555 | const FcnType& get_fcn() const |
| 556 | { |
| 557 | return fcn; |
| 558 | } |
| 559 | |
| 560 | private: |
| 561 | FcnType fcn; |
| 562 | }; |
| 563 | |
| 564 | template <TOSA_REF_TYPE OutDtype> |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 565 | class CastHelper<TOSA_REF_TYPE_FP64, OutDtype> |
| 566 | { |
| 567 | public: |
| 568 | using InEigenType = typename GetEigenType<TOSA_REF_TYPE_FP64>::type; |
| 569 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 570 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 571 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 572 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 573 | CastHelper(); |
| 574 | const FcnType& get_fcn() const |
| 575 | { |
| 576 | return fcn; |
| 577 | } |
| 578 | |
| 579 | private: |
| 580 | FcnType fcn; |
| 581 | }; |
| 582 | |
| 583 | template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 584 | class OpCast : public GraphNode |
| 585 | { |
| 586 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 587 | OpCast(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 588 | virtual ~OpCast(); |
| 589 | |
| 590 | virtual int checkTensorAttributes() final; |
| 591 | virtual int eval() final; |
| 592 | |
| 593 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 594 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 595 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 596 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 597 | |
| 598 | protected: |
| 599 | CastHelper<InDtype, OutDtype> cast_helper; |
| 600 | TosaReference::TensorTemplate<TIn>* in; |
| 601 | TosaReference::TensorTemplate<TOut>* out; |
| 602 | }; |
| 603 | |
| 604 | }; // namespace TosaReference |
| 605 | |
| 606 | #endif |