Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2 | // Copyright (c) 2020-2022, 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 | { |
| 25 | template <int Rank, DType InDtype, DType OutDtype> |
| 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 | |
| 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 | |
| 40 | static constexpr int32_t QMin = GetQMin<OutDtype>::value; |
| 41 | static constexpr int32_t QMax = GetQMax<OutDtype>::value; |
| 42 | |
| 43 | protected: |
| 44 | TosaRescaleAttribute* attribute; |
| 45 | TosaReference::TensorTemplate<TIn>* in; |
| 46 | TosaReference::TensorTemplate<TOut>* out; |
| 47 | }; |
| 48 | |
| 49 | template <DType InDtype, DType OutDtype> |
| 50 | class CastHelper |
| 51 | { |
| 52 | public: |
| 53 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 54 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 55 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 56 | static constexpr int32_t OutBits = GetNumBits<OutDtype>::value; |
| 57 | CastHelper(); |
| 58 | const FcnType& get_fcn() const |
| 59 | { |
| 60 | return fcn; |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | FcnType fcn; |
| 65 | }; |
| 66 | |
| 67 | template <DType InDtype> |
| 68 | class CastHelper<InDtype, DType_BOOL> |
| 69 | { |
| 70 | public: |
| 71 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 72 | using OutEigenType = typename GetEigenType<DType_BOOL>::type; |
| 73 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 74 | CastHelper(); |
| 75 | const FcnType& get_fcn() const |
| 76 | { |
| 77 | return fcn; |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | FcnType fcn; |
| 82 | }; |
| 83 | |
| 84 | template <DType OutDtype> |
| 85 | class CastHelper<DType_BOOL, OutDtype> |
| 86 | { |
| 87 | public: |
| 88 | using InEigenType = typename GetEigenType<DType_BOOL>::type; |
| 89 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 90 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 91 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 92 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 93 | CastHelper(); |
| 94 | const FcnType& get_fcn() const |
| 95 | { |
| 96 | return fcn; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | FcnType fcn; |
| 101 | }; |
| 102 | |
| 103 | template <DType InDtype> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 104 | class CastHelper<InDtype, DType_FP16> |
| 105 | { |
| 106 | public: |
| 107 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 108 | using OutEigenType = typename GetEigenType<DType_FP16>::type; |
| 109 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 110 | CastHelper(); |
| 111 | const FcnType& get_fcn() const |
| 112 | { |
| 113 | return fcn; |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | FcnType fcn; |
| 118 | }; |
| 119 | |
| 120 | template <DType OutDtype> |
| 121 | class CastHelper<DType_FP16, OutDtype> |
| 122 | { |
| 123 | public: |
| 124 | using InEigenType = typename GetEigenType<DType_FP16>::type; |
| 125 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 126 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 127 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 128 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 129 | CastHelper(); |
| 130 | const FcnType& get_fcn() const |
| 131 | { |
| 132 | return fcn; |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | FcnType fcn; |
| 137 | }; |
| 138 | |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 139 | template <> |
| 140 | class CastHelper<DType_FP32, DType_FP16> |
| 141 | { |
| 142 | public: |
| 143 | using InEigenType = typename GetEigenType<DType_FP32>::type; |
| 144 | using OutEigenType = typename GetEigenType<DType_FP16>::type; |
| 145 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 146 | CastHelper(); |
| 147 | const FcnType& get_fcn() const |
| 148 | { |
| 149 | return fcn; |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | FcnType fcn; |
| 154 | }; |
| 155 | |
| 156 | template <DType InDtype> |
| 157 | class CastHelper<InDtype, DType_BF16> |
| 158 | { |
| 159 | public: |
| 160 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 161 | using OutEigenType = typename GetEigenType<DType_BF16>::type; |
| 162 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 163 | CastHelper(); |
| 164 | const FcnType& get_fcn() const |
| 165 | { |
| 166 | return fcn; |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | FcnType fcn; |
| 171 | }; |
| 172 | |
| 173 | template <DType OutDtype> |
| 174 | class CastHelper<DType_BF16, OutDtype> |
| 175 | { |
| 176 | public: |
| 177 | using InEigenType = typename GetEigenType<DType_BF16>::type; |
| 178 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 179 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 180 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 181 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 182 | CastHelper(); |
| 183 | const FcnType& get_fcn() const |
| 184 | { |
| 185 | return fcn; |
| 186 | } |
| 187 | |
| 188 | private: |
| 189 | FcnType fcn; |
| 190 | }; |
| 191 | |
| 192 | template <> |
| 193 | class CastHelper<DType_FP32, DType_BF16> |
| 194 | { |
| 195 | public: |
| 196 | using InEigenType = typename GetEigenType<DType_FP32>::type; |
| 197 | using OutEigenType = typename GetEigenType<DType_BF16>::type; |
| 198 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 199 | CastHelper(); |
| 200 | const FcnType& get_fcn() const |
| 201 | { |
| 202 | return fcn; |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | FcnType fcn; |
| 207 | }; |
| 208 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 209 | template <DType InDtype> |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 210 | class CastHelper<InDtype, DType_FP32> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 211 | { |
| 212 | public: |
| 213 | using InEigenType = typename GetEigenType<InDtype>::type; |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 214 | using OutEigenType = typename GetEigenType<DType_FP32>::type; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 215 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 216 | CastHelper(); |
| 217 | const FcnType& get_fcn() const |
| 218 | { |
| 219 | return fcn; |
| 220 | } |
| 221 | |
| 222 | private: |
| 223 | FcnType fcn; |
| 224 | }; |
| 225 | |
James Ward | 736fd1a | 2023-01-23 17:13:37 +0000 | [diff] [blame] | 226 | template <> |
| 227 | class CastHelper<DType_FP16, DType_FP32> |
| 228 | { |
| 229 | public: |
| 230 | using InEigenType = typename GetEigenType<DType_FP16>::type; |
| 231 | using OutEigenType = typename GetEigenType<DType_FP32>::type; |
| 232 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 233 | CastHelper(); |
| 234 | const FcnType& get_fcn() const |
| 235 | { |
| 236 | return fcn; |
| 237 | } |
| 238 | |
| 239 | private: |
| 240 | FcnType fcn; |
| 241 | }; |
| 242 | |
| 243 | template <> |
| 244 | class CastHelper<DType_BF16, DType_FP32> |
| 245 | { |
| 246 | public: |
| 247 | using InEigenType = typename GetEigenType<DType_BF16>::type; |
| 248 | using OutEigenType = typename GetEigenType<DType_FP32>::type; |
| 249 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 250 | CastHelper(); |
| 251 | const FcnType& get_fcn() const |
| 252 | { |
| 253 | return fcn; |
| 254 | } |
| 255 | |
| 256 | private: |
| 257 | FcnType fcn; |
| 258 | }; |
| 259 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 260 | template <DType OutDtype> |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 261 | class CastHelper<DType_FP32, OutDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 262 | { |
| 263 | public: |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 264 | using InEigenType = typename GetEigenType<DType_FP32>::type; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 265 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 266 | using FcnType = std::function<OutEigenType(InEigenType)>; |
| 267 | static constexpr int32_t OutMin = GetQMin<OutDtype>::value; |
| 268 | static constexpr int32_t OutMax = GetQMax<OutDtype>::value; |
| 269 | CastHelper(); |
| 270 | const FcnType& get_fcn() const |
| 271 | { |
| 272 | return fcn; |
| 273 | } |
| 274 | |
| 275 | private: |
| 276 | FcnType fcn; |
| 277 | }; |
| 278 | |
| 279 | template <int Rank, DType InDtype, DType OutDtype> |
| 280 | class OpCast : public GraphNode |
| 281 | { |
| 282 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 283 | OpCast(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 284 | virtual ~OpCast(); |
| 285 | |
| 286 | virtual int checkTensorAttributes() final; |
| 287 | virtual int eval() final; |
| 288 | |
| 289 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 290 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 291 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 292 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 293 | |
| 294 | protected: |
| 295 | CastHelper<InDtype, OutDtype> cast_helper; |
| 296 | TosaReference::TensorTemplate<TIn>* in; |
| 297 | TosaReference::TensorTemplate<TOut>* out; |
| 298 | }; |
| 299 | |
| 300 | }; // namespace TosaReference |
| 301 | |
| 302 | #endif |