Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2 | // Copyright (c) 2020-2021, 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 "ewise_unary.h" |
| 17 | #include "quant_util.h" |
| 18 | #include "template_types.h" |
| 19 | #include <cmath> |
| 20 | |
| 21 | using namespace TosaReference; |
| 22 | using namespace Eigen; |
| 23 | using namespace tosa; |
| 24 | |
| 25 | template <int Rank, DType Dtype> |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 26 | UnaryNode<Rank, Dtype>::UnaryNode(SubgraphTraverser* sgt_, const Op& op_, uint64_t id_) |
| 27 | : GraphNode(sgt_, op_, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 28 | { |
| 29 | setRequiredOperands(1, 1); |
| 30 | setRequiredRank(0, 6); |
| 31 | |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame^] | 32 | fcn = [](InEigenType a) -> OutEigenType { |
| 33 | ASSERT_MSG(0, "In default UnaryNode function, missing function registration"); |
| 34 | return OutEigenType(); |
| 35 | }; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | template <int Rank, DType Dtype> |
| 39 | UnaryNode<Rank, Dtype>::~UnaryNode() |
| 40 | {} |
| 41 | |
| 42 | template <int Rank, DType Dtype> |
| 43 | int UnaryNode<Rank, Dtype>::checkTensorAttributes() |
| 44 | { |
| 45 | if (validateRequiredOperands()) |
| 46 | return 1; |
| 47 | |
| 48 | if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0])) |
| 49 | { |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | // output and input must be the same types |
Kevin Cheng | c72b59c | 2021-09-29 16:57:55 -0700 | [diff] [blame] | 54 | if (inputs[0]->matchRankTypeShape(*outputs[0])) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 55 | { |
Kevin Cheng | c72b59c | 2021-09-29 16:57:55 -0700 | [diff] [blame] | 56 | printNodeValidationError("UnaryNode: input and output rank/type/shape must match"); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | a = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]); |
| 61 | result = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]); |
| 62 | |
| 63 | ASSERT_MEM(a && result); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | template <int Rank, DType Dtype> |
| 69 | int UnaryNode<Rank, Dtype>::eval() |
| 70 | { |
| 71 | this->result->getTensor() = this->a->getTensor().unaryExpr(this->fcn); |
| 72 | |
| 73 | return GraphNode::eval(); |
| 74 | } |
| 75 | |
| 76 | template <int Rank, DType Dtype> |
| 77 | int OpAbs<Rank, Dtype>::register_fcn() |
| 78 | { |
| 79 | switch (Dtype) |
| 80 | { |
| 81 | case DType_FLOAT: |
| 82 | case DType_INT32: |
| 83 | this->fcn = [](InEigenType a) -> OutEigenType { return a > (InEigenType)0 ? a : (-a); }; |
| 84 | break; |
| 85 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 86 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | template <int Rank, DType Dtype> |
| 93 | int OpBitwiseNot<Rank, Dtype>::register_fcn() |
| 94 | { |
| 95 | switch (Dtype) |
| 96 | { |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 97 | case DType_INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 98 | case DType_INT16: |
| 99 | case DType_INT32: |
| 100 | this->fcn = [](InEigenType a) -> OutEigenType { return ~a; }; |
| 101 | break; |
| 102 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 103 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | template <int Rank, DType Dtype> |
| 110 | int OpCeil<Rank, Dtype>::register_fcn() |
| 111 | { |
| 112 | switch (Dtype) |
| 113 | { |
| 114 | case DType_FLOAT: |
| 115 | this->fcn = [](InEigenType a) -> OutEigenType { return ceilf(a); }; |
| 116 | break; |
| 117 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 118 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | template <int Rank, DType Dtype> |
| 125 | int OpClz<Rank, Dtype>::register_fcn() |
| 126 | { |
| 127 | int32_t num_bits; |
| 128 | switch (Dtype) |
| 129 | { |
| 130 | case DType_INT32: |
| 131 | num_bits = 32; |
| 132 | break; |
| 133 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 134 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | this->fcn = [num_bits](int32_t a) -> int32_t { |
| 138 | int32_t leading_zeros = 0; |
| 139 | for (int bit = num_bits - 1; bit >= 0; bit--) |
| 140 | { |
| 141 | if (((a >> bit) & 0x1) == 0) |
| 142 | { |
| 143 | leading_zeros++; |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | return leading_zeros; |
| 151 | }; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | template <int Rank, DType Dtype> |
| 157 | int OpExp<Rank, Dtype>::register_fcn() |
| 158 | { |
| 159 | switch (Dtype) |
| 160 | { |
| 161 | case DType_FLOAT: |
| 162 | this->fcn = [](InEigenType a) -> OutEigenType { return expf(a); }; |
| 163 | break; |
| 164 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 165 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | template <int Rank, DType Dtype> |
| 172 | int OpFloor<Rank, Dtype>::register_fcn() |
| 173 | { |
| 174 | switch (Dtype) |
| 175 | { |
| 176 | case DType_FLOAT: |
| 177 | this->fcn = [](InEigenType a) -> OutEigenType { return floorf(a); }; |
| 178 | break; |
| 179 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 180 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | template <int Rank, DType Dtype> |
| 187 | int OpLog<Rank, Dtype>::register_fcn() |
| 188 | { |
| 189 | switch (Dtype) |
| 190 | { |
| 191 | case DType_FLOAT: |
| 192 | this->fcn = [](InEigenType a) -> OutEigenType { return logf(a); }; |
| 193 | break; |
| 194 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 195 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | template <int Rank, DType Dtype> |
| 202 | int OpLogicalNot<Rank, Dtype>::register_fcn() |
| 203 | { |
| 204 | switch (Dtype) |
| 205 | { |
| 206 | case DType_BOOL: |
| 207 | this->fcn = [](InEigenType a) -> OutEigenType { return !a; }; |
| 208 | break; |
| 209 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 210 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | template <int Rank, DType Dtype> |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame^] | 217 | OpNegate<Rank, Dtype>::OpNegate(SubgraphTraverser* sgt_, |
| 218 | TosaAttributeBase* attribute_, |
| 219 | uint64_t id_) |
| 220 | : UnaryNode<Rank, Dtype>(sgt_, Op_NEGATE, id_) |
| 221 | { |
| 222 | INIT_ATTRIBUTE(Negate); |
| 223 | |
| 224 | register_fcn(); |
| 225 | } |
| 226 | |
| 227 | template <int Rank, DType Dtype> |
| 228 | OpNegate<Rank, Dtype>::~OpNegate() |
| 229 | { |
| 230 | if (attribute) |
| 231 | delete attribute; |
| 232 | } |
| 233 | |
| 234 | template <int Rank, DType Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 235 | int OpNegate<Rank, Dtype>::register_fcn() |
| 236 | { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame^] | 237 | ERROR_IF(Dtype != DType_INT8 && attribute->input1_zp() != 0, "OpNegate: zeropoint only for int8_t"); |
| 238 | ERROR_IF(Dtype != DType_INT8 && attribute->output_zp() != 0, "OpNegate: zeropoint only for int8_t"); |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 239 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 240 | switch (Dtype) |
| 241 | { |
| 242 | case DType_FLOAT: |
| 243 | this->fcn = [](InEigenType a) -> OutEigenType { |
| 244 | InEigenType result = -(a); |
| 245 | return result; |
| 246 | }; |
| 247 | break; |
| 248 | case DType_INT16: |
| 249 | case DType_INT32: |
Jeremy Johnson | 81ee53d | 2022-03-23 15:32:34 +0000 | [diff] [blame] | 250 | this->fcn = [this](InEigenType a) -> OutEigenType { |
| 251 | int64_t res_in_64 = 0L - a; |
Jeremy Johnson | 0e46364 | 2022-05-03 12:10:23 +0100 | [diff] [blame] | 252 | int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::max()); |
| 253 | int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::min()); |
| 254 | REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpNegate: result not in acc type range (int32)"); |
| 255 | |
| 256 | int64_t max_clip_in_64, min_clip_in_64; |
| 257 | if (Dtype == DType_INT16) |
| 258 | { |
| 259 | max_clip_in_64 = static_cast<int64_t>(std::numeric_limits<int16_t>::max()); |
| 260 | min_clip_in_64 = static_cast<int64_t>(std::numeric_limits<int16_t>::min()); |
Jeremy Johnson | 81ee53d | 2022-03-23 15:32:34 +0000 | [diff] [blame] | 261 | } |
| 262 | else |
| 263 | { |
Jeremy Johnson | 0e46364 | 2022-05-03 12:10:23 +0100 | [diff] [blame] | 264 | max_clip_in_64 = i32_max_in_64; |
| 265 | min_clip_in_64 = i32_min_in_64; |
Jeremy Johnson | 81ee53d | 2022-03-23 15:32:34 +0000 | [diff] [blame] | 266 | } |
Jeremy Johnson | 0e46364 | 2022-05-03 12:10:23 +0100 | [diff] [blame] | 267 | return static_cast<InEigenType>(std::min<int64_t>(max_clip_in_64, std::max<int64_t>(min_clip_in_64, res_in_64))); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 268 | }; |
| 269 | break; |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 270 | case DType_INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 271 | this->fcn = [this](InEigenType a) -> OutEigenType { |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame^] | 272 | int64_t res_in_64 = 0 - (a - attribute->input1_zp()); |
Jeremy Johnson | 0e46364 | 2022-05-03 12:10:23 +0100 | [diff] [blame] | 273 | int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::max()); |
| 274 | int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::min()); |
| 275 | REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpNegate: result not in acc type range (int32)"); |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame^] | 276 | res_in_64 += attribute->output_zp(); |
Jeremy Johnson | 0e46364 | 2022-05-03 12:10:23 +0100 | [diff] [blame] | 277 | InEigenType result = static_cast<InEigenType>(std::min(std::max(res_in_64, static_cast<int64_t>(QMin)), static_cast<int64_t>(QMax))); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 278 | return result; |
| 279 | }; |
| 280 | break; |
| 281 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 282 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | template <int Rank, DType Dtype> |
| 289 | int OpReciprocal<Rank, Dtype>::register_fcn() |
| 290 | { |
| 291 | switch (Dtype) |
| 292 | { |
| 293 | case DType_FLOAT: |
| 294 | this->fcn = [](InEigenType a) -> OutEigenType { return 1.0 / a; }; |
| 295 | break; |
| 296 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 297 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | template <int Rank, DType Dtype> |
| 304 | int OpRsqrt<Rank, Dtype>::register_fcn() |
| 305 | { |
| 306 | switch (Dtype) |
| 307 | { |
| 308 | case DType_FLOAT: |
| 309 | this->fcn = [](InEigenType a) -> OutEigenType { return 1.0 / sqrtf(a); }; |
| 310 | break; |
| 311 | default: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 312 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | // template explicit instantiation |
| 319 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAbs, FLOAT); |
| 320 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAbs, INT32); |
| 321 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 322 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseNot, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 323 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseNot, INT16); |
| 324 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseNot, INT32); |
| 325 | |
| 326 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCeil, FLOAT); |
| 327 | |
| 328 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClz, INT32); |
| 329 | |
| 330 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpExp, FLOAT); |
| 331 | |
| 332 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpFloor, FLOAT); |
| 333 | |
| 334 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLog, FLOAT); |
| 335 | |
| 336 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalNot, BOOL); |
| 337 | |
| 338 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, FLOAT); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 339 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 340 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, INT16); |
| 341 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, INT32); |
| 342 | |
| 343 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpRsqrt, FLOAT); |
| 344 | |
| 345 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReciprocal, FLOAT); |