Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 2 | // Copyright (c) 2020-2023, 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_EWISE_UNARY_H |
| 17 | #define OPS_EWISE_UNARY_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 Dtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 26 | class UnaryNode : public GraphNode |
| 27 | { |
| 28 | public: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 29 | UnaryNode(SubgraphTraverser* sgt_, const Op& nodeType, const uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 30 | virtual ~UnaryNode(); |
| 31 | |
| 32 | virtual int checkTensorAttributes() final; |
| 33 | virtual int eval() final; |
| 34 | virtual int register_fcn() = 0; |
| 35 | |
| 36 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 37 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 38 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 39 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 40 | |
| 41 | protected: |
| 42 | std::function<OutEigenType(InEigenType)> fcn; |
| 43 | TosaReference::TensorTemplate<TIn>* a; |
| 44 | TosaReference::TensorTemplate<TOut>* result; |
| 45 | }; |
| 46 | |
| 47 | #define DEF_TEMPLATE_UNARY_OP(Opname, OPNAME) \ |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 48 | template <int Rank, TOSA_REF_TYPE Dtype> \ |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 49 | class Op##Opname : public UnaryNode<Rank, Dtype> \ |
| 50 | { \ |
| 51 | public: \ |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 52 | Op##Opname(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) \ |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 53 | : UnaryNode<Rank, Dtype>(sgt_, Op_##OPNAME, id_) \ |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 54 | { \ |
| 55 | register_fcn(); \ |
| 56 | } \ |
| 57 | static constexpr int32_t QMin = GetQMin<Dtype>::value; \ |
| 58 | static constexpr int32_t QMax = GetQMax<Dtype>::value; \ |
| 59 | using InEigenType = typename GetEigenType<Dtype>::type; \ |
| 60 | using OutEigenType = typename GetEigenType<Dtype>::type; \ |
| 61 | virtual int register_fcn(); \ |
| 62 | }; |
| 63 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 64 | DEF_TEMPLATE_UNARY_OP(Abs, ABS) |
| 65 | DEF_TEMPLATE_UNARY_OP(BitwiseNot, BITWISE_NOT) |
| 66 | DEF_TEMPLATE_UNARY_OP(Ceil, CEIL) |
| 67 | DEF_TEMPLATE_UNARY_OP(Clz, CLZ) |
| 68 | DEF_TEMPLATE_UNARY_OP(Exp, EXP) |
| 69 | DEF_TEMPLATE_UNARY_OP(Floor, FLOOR) |
| 70 | DEF_TEMPLATE_UNARY_OP(Log, LOG) |
| 71 | DEF_TEMPLATE_UNARY_OP(LogicalNot, LOGICAL_NOT) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 72 | DEF_TEMPLATE_UNARY_OP(Reciprocal, RECIPROCAL) |
| 73 | DEF_TEMPLATE_UNARY_OP(Rsqrt, RSQRT) |
| 74 | |
| 75 | #undef DEF_TEMPLATE_UNARY_OP |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 76 | |
| 77 | // Negate is the only unary op with attributes |
Tai Ly | a4d748b | 2023-03-28 22:06:56 +0000 | [diff] [blame] | 78 | template <int Rank, TOSA_REF_TYPE Dtype> |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 79 | class OpNegate : public UnaryNode<Rank, Dtype> |
| 80 | { |
| 81 | public: |
| 82 | OpNegate(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
| 83 | virtual ~OpNegate(); |
| 84 | |
| 85 | static constexpr int32_t QMin = GetQMin<Dtype>::value; |
| 86 | static constexpr int32_t QMax = GetQMax<Dtype>::value; |
| 87 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 88 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 89 | virtual int register_fcn(); |
| 90 | |
| 91 | protected: |
| 92 | tosa::TosaNegateAttribute* attribute; |
| 93 | }; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 94 | |
| 95 | }; // namespace TosaReference |
| 96 | |
| 97 | #endif |