Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +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_EWISE_BINARY_H |
| 17 | #define OPS_EWISE_BINARY_H |
| 18 | |
| 19 | #include "graph_node.h" |
| 20 | |
| 21 | using namespace tosa; |
| 22 | |
| 23 | namespace TosaReference |
| 24 | { |
| 25 | |
| 26 | // class BinaryNodeBase: hold common functions of all the binary nodes |
| 27 | // when an binary op is created, the virtual OpXXX::register_fcn() will be called |
| 28 | // and 'fcn' will be register with lambda function which has two inputs |
| 29 | // class BinaryNode: the level of indirection to partially specialize template for rank 0 |
| 30 | // eval() from toplevel called should call the .binaryExpr(dims, fcn) here |
| 31 | // this needs to be partially specialize or |
| 32 | // compiler will statically fail when trying to broadcast rank0 tensor |
| 33 | // class OpXXX: implement per-element lambda function based on different data type |
| 34 | // unlike BinaryNode, this doesn't need to be partially specialized |
| 35 | |
| 36 | // Eigen::Tensor does support some binary element-wise natively (e.g. CWiseMax, or '+', etc.) |
| 37 | // which might be faster since it could be implemented with SIMD instructions |
| 38 | // the way of registering lambda + .binaryExpr() might sacrifice performance here |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 39 | // but it can avoid partially specialization for combination of {rankN, rank0} x {FP32/INT32, QU8, ...} |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 40 | // needs to revisit if performance becomes a bottleneck here |
| 41 | template <int Rank, DType InDtype, DType OutDtype> |
| 42 | class BinaryNodeBase : public GraphNode |
| 43 | { |
| 44 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 45 | BinaryNodeBase(SubgraphTraverser* sgt_, const Op& nodeType, const uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 46 | virtual ~BinaryNodeBase(); |
| 47 | |
| 48 | virtual int checkTensorAttributes() final; |
| 49 | virtual int eval() = 0; |
| 50 | virtual int register_fcn() = 0; |
| 51 | |
| 52 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 53 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 54 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 55 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 56 | |
| 57 | protected: |
| 58 | int broadcast(); |
| 59 | |
| 60 | protected: |
| 61 | std::function<OutEigenType(InEigenType, InEigenType)> fcn; |
| 62 | Eigen::array<int, Rank> bcast_a; |
| 63 | Eigen::array<int, Rank> bcast_b; |
| 64 | TosaReference::TensorTemplate<TIn>* a; |
| 65 | TosaReference::TensorTemplate<TIn>* b; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 66 | TosaReference::TensorTemplate<TOut>* result; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | // primary class |
| 70 | template <int Rank, DType InDtype, DType OutDtype> |
| 71 | class BinaryNode : public BinaryNodeBase<Rank, InDtype, OutDtype> |
| 72 | { |
| 73 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 74 | BinaryNode(SubgraphTraverser* sgt_, const Op& op_, const uint64_t id_) |
| 75 | : BinaryNodeBase<Rank, InDtype, OutDtype>(sgt_, op_, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 76 | {} |
| 77 | virtual ~BinaryNode() |
| 78 | {} |
| 79 | |
| 80 | virtual int eval(); |
| 81 | |
| 82 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 83 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 84 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 85 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 86 | }; |
| 87 | |
| 88 | // partial specialization for rank 0 |
| 89 | template <DType InDtype, DType OutDtype> |
| 90 | class BinaryNode<0, InDtype, OutDtype> : public BinaryNodeBase<0, InDtype, OutDtype> |
| 91 | { |
| 92 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 93 | BinaryNode(SubgraphTraverser* sgt_, const Op& op_, const uint64_t id_) |
| 94 | : BinaryNodeBase<0, InDtype, OutDtype>(sgt_, op_, id_) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 95 | {} |
| 96 | virtual ~BinaryNode() |
| 97 | {} |
| 98 | |
| 99 | virtual int eval(); |
| 100 | }; |
| 101 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 102 | #define DEF_TEMPLATE_BINARY_OP_DEFAULT(Opname, OPNAME) \ |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 103 | template <int Rank, DType Dtype> \ |
| 104 | class Op##Opname : public BinaryNode<Rank, Dtype, Dtype> \ |
| 105 | { \ |
| 106 | public: \ |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 107 | Op##Opname(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) \ |
| 108 | : BinaryNode<Rank, Dtype, Dtype>(sgt_, Op_##OPNAME, id_) \ |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 109 | { \ |
| 110 | register_fcn(); \ |
| 111 | } \ |
| 112 | static constexpr DType InDtype = Dtype; \ |
| 113 | static constexpr DType OutDtype = Dtype; \ |
| 114 | using InEigenType = typename GetEigenType<InDtype>::type; \ |
| 115 | using OutEigenType = typename GetEigenType<OutDtype>::type; \ |
| 116 | virtual int register_fcn(); \ |
| 117 | }; |
| 118 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 119 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Add, ADD) |
| 120 | DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseAnd, BITWISE_AND) |
| 121 | DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseOr, BITWISE_OR) |
| 122 | DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseXor, BITWISE_XOR) |
Matthew Haddon | 459443c | 2021-08-23 16:43:13 +0100 | [diff] [blame] | 123 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Intdiv, INTDIV) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 124 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalAnd, LOGICAL_AND) |
| 125 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalLeftShift, LOGICAL_LEFT_SHIFT) |
| 126 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalRightShift, LOGICAL_RIGHT_SHIFT) |
| 127 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalOr, LOGICAL_OR) |
| 128 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalXor, LOGICAL_XOR) |
| 129 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Maximum, MAXIMUM) |
| 130 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Minimum, MINIMUM) |
| 131 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Pow, POW) |
| 132 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Sub, SUB) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 133 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 134 | #undef DEF_TEMPLATE_BINARY_OP_DEFAULT |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 135 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 136 | template <int Rank, DType Dtype> |
| 137 | class OpArithmeticRightShift : public BinaryNode<Rank, Dtype, Dtype> |
| 138 | { |
| 139 | public: |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 140 | OpArithmeticRightShift(SubgraphTraverser* sgt_, |
| 141 | TosaAttributeBase* attribute_, |
Kevin Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame] | 142 | uint64_t id_) |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 143 | : BinaryNode<Rank, Dtype, Dtype>(sgt_, Op_ARITHMETIC_RIGHT_SHIFT, id_) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 144 | { |
| 145 | INIT_ATTRIBUTE(ArithmeticRightShift); |
| 146 | register_fcn(); |
| 147 | } |
| 148 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 149 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 150 | virtual int register_fcn(); |
Jerry Ge | a682749 | 2022-11-16 10:41:55 -0800 | [diff] [blame] | 151 | virtual ~OpArithmeticRightShift(); |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 152 | |
| 153 | protected: |
| 154 | TosaArithmeticRightShiftAttribute* attribute; |
| 155 | }; |
| 156 | |
| 157 | template <int Rank, DType InDtype, DType OutDtype> |
| 158 | class OpMul : public BinaryNode<Rank, InDtype, OutDtype> |
| 159 | { |
| 160 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 161 | OpMul(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) |
| 162 | : BinaryNode<Rank, InDtype, OutDtype>(sgt_, Op_MUL, id_) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 163 | { |
| 164 | INIT_ATTRIBUTE(Mul); |
| 165 | register_fcn(); |
| 166 | } |
Jerry Ge | a682749 | 2022-11-16 10:41:55 -0800 | [diff] [blame] | 167 | virtual ~OpMul(); |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 168 | static constexpr int64_t QMin = GetQMin<OutDtype>::value; |
| 169 | static constexpr int64_t QMax = GetQMax<OutDtype>::value; |
| 170 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 171 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 172 | virtual int register_fcn(); |
| 173 | |
| 174 | protected: |
| 175 | TosaMulAttribute* attribute; |
| 176 | }; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 177 | |
Kevin Cheng | 571f718 | 2021-05-24 17:20:01 -0700 | [diff] [blame] | 178 | template <int Rank, DType InDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 179 | class OpTable : public GraphNode |
| 180 | { |
| 181 | public: |
Eric Kunze | b5fabec | 2022-06-07 05:20:44 +0000 | [diff] [blame] | 182 | OpTable(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 183 | virtual ~OpTable(); |
| 184 | |
| 185 | virtual int checkTensorAttributes(); |
| 186 | virtual int eval(); |
| 187 | |
Kevin Cheng | fe392ce | 2021-10-18 21:51:55 +0000 | [diff] [blame] | 188 | static constexpr DType TableDtype = (InDtype == DType_INT8) ? DType_INT8 : DType_INT16; |
| 189 | static constexpr DType OutDtype = (InDtype == DType_INT8) ? DType_INT8 : DType_INT32; |
| 190 | static constexpr uint32_t TableNumEntries = (InDtype == DType_INT8) ? 256 : 513; |
| 191 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 192 | using TableEigenType = typename GetEigenType<TableDtype>::type; |
| 193 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 194 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 195 | using TTable = Eigen::Tensor<TableEigenType, 1>; |
| 196 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 197 | static constexpr int32_t IntegerBits = 9; |
| 198 | static constexpr int32_t FractionBits = 7; |
| 199 | static constexpr int32_t NumTableEntries = (1 << IntegerBits); |
| 200 | static constexpr int32_t QInMin = GetQMin<InDtype>::value; |
| 201 | static constexpr int32_t QInMax = GetQMax<InDtype>::value; |
| 202 | static constexpr int32_t QOutMin = GetQMin<OutDtype>::value; |
| 203 | static constexpr int32_t QOutMax = GetQMax<OutDtype>::value; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 204 | |
| 205 | protected: |
| 206 | TosaReference::TensorTemplate<TIn>* in; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 207 | TosaReference::TensorTemplate<TOut>* out; |
Kevin Cheng | fe392ce | 2021-10-18 21:51:55 +0000 | [diff] [blame] | 208 | TosaTableAttribute* attribute; |
| 209 | std::array<TableEigenType, TableNumEntries> table; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | }; // namespace TosaReference |
| 213 | |
| 214 | #endif |