Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
| 2 | // Copyright (c) 2020, ARM Limited. |
| 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 |
| 39 | // but it can avoid partially specialization for combination of {rankN, rank0} x {FLOAT/INT32, QU8, ...} |
| 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: |
| 45 | BinaryNodeBase(const Op& nodeType, TosaQuantInfoBase* qinfo_, const uint64_t id_); |
| 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; |
| 66 | TosaReference::TensorTemplate<ETensor0<InEigenType>>* a_rank0; |
| 67 | TosaReference::TensorTemplate<ETensor0<InEigenType>>* b_rank0; |
| 68 | TosaReference::TensorTemplate<TOut>* result; |
| 69 | int a_rank; |
| 70 | int b_rank; |
| 71 | int max_input_rank; |
| 72 | }; |
| 73 | |
| 74 | // primary class |
| 75 | template <int Rank, DType InDtype, DType OutDtype> |
| 76 | class BinaryNode : public BinaryNodeBase<Rank, InDtype, OutDtype> |
| 77 | { |
| 78 | public: |
| 79 | BinaryNode(const Op& op_, TosaQuantInfoBase* qinfo_, const uint64_t id_) |
| 80 | : BinaryNodeBase<Rank, InDtype, OutDtype>(op_, qinfo_, id_) |
| 81 | {} |
| 82 | virtual ~BinaryNode() |
| 83 | {} |
| 84 | |
| 85 | virtual int eval(); |
| 86 | |
| 87 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 88 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 89 | using TIn = Eigen::Tensor<InEigenType, Rank>; |
| 90 | using TOut = Eigen::Tensor<OutEigenType, Rank>; |
| 91 | }; |
| 92 | |
| 93 | // partial specialization for rank 0 |
| 94 | template <DType InDtype, DType OutDtype> |
| 95 | class BinaryNode<0, InDtype, OutDtype> : public BinaryNodeBase<0, InDtype, OutDtype> |
| 96 | { |
| 97 | public: |
| 98 | BinaryNode(const Op& op_, TosaQuantInfoBase* qinfo_, const uint64_t id_) |
| 99 | : BinaryNodeBase<0, InDtype, OutDtype>(op_, qinfo_, id_) |
| 100 | {} |
| 101 | virtual ~BinaryNode() |
| 102 | {} |
| 103 | |
| 104 | virtual int eval(); |
| 105 | }; |
| 106 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 107 | #define DEF_TEMPLATE_BINARY_OP_DEFAULT(Opname, OPNAME) \ |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 108 | template <int Rank, DType Dtype> \ |
| 109 | class Op##Opname : public BinaryNode<Rank, Dtype, Dtype> \ |
| 110 | { \ |
| 111 | public: \ |
| 112 | Op##Opname(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_) \ |
| 113 | : BinaryNode<Rank, Dtype, Dtype>(Op_##OPNAME, qinfo_, id_) \ |
| 114 | { \ |
| 115 | register_fcn(); \ |
| 116 | } \ |
| 117 | static constexpr DType InDtype = Dtype; \ |
| 118 | static constexpr DType OutDtype = Dtype; \ |
| 119 | using InEigenType = typename GetEigenType<InDtype>::type; \ |
| 120 | using OutEigenType = typename GetEigenType<OutDtype>::type; \ |
| 121 | virtual int register_fcn(); \ |
| 122 | }; |
| 123 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 124 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Add, ADD) |
| 125 | DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseAnd, BITWISE_AND) |
| 126 | DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseOr, BITWISE_OR) |
| 127 | DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseXor, BITWISE_XOR) |
Kevin Cheng | 14d7f7a | 2021-05-12 10:44:49 -0700 | [diff] [blame] | 128 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Div, DIV) |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 129 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalAnd, LOGICAL_AND) |
| 130 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalLeftShift, LOGICAL_LEFT_SHIFT) |
| 131 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalRightShift, LOGICAL_RIGHT_SHIFT) |
| 132 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalOr, LOGICAL_OR) |
| 133 | DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalXor, LOGICAL_XOR) |
| 134 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Maximum, MAXIMUM) |
| 135 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Minimum, MINIMUM) |
| 136 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Pow, POW) |
| 137 | DEF_TEMPLATE_BINARY_OP_DEFAULT(Sub, SUB) |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 138 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 139 | #undef DEF_TEMPLATE_BINARY_OP_DEFAULT |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 140 | |
Kevin Cheng | aee1fac | 2020-11-11 13:54:06 -0800 | [diff] [blame] | 141 | template <int Rank, DType Dtype> |
| 142 | class OpArithmeticRightShift : public BinaryNode<Rank, Dtype, Dtype> |
| 143 | { |
| 144 | public: |
| 145 | OpArithmeticRightShift(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_) |
| 146 | : BinaryNode<Rank, Dtype, Dtype>(Op_ARITHMETIC_RIGHT_SHIFT, qinfo_, id_) |
| 147 | { |
| 148 | INIT_ATTRIBUTE(ArithmeticRightShift); |
| 149 | register_fcn(); |
| 150 | } |
| 151 | using InEigenType = typename GetEigenType<Dtype>::type; |
| 152 | using OutEigenType = typename GetEigenType<Dtype>::type; |
| 153 | virtual int register_fcn(); |
| 154 | |
| 155 | protected: |
| 156 | TosaArithmeticRightShiftAttribute* attribute; |
| 157 | }; |
| 158 | |
| 159 | template <int Rank, DType InDtype, DType OutDtype> |
| 160 | class OpMul : public BinaryNode<Rank, InDtype, OutDtype> |
| 161 | { |
| 162 | public: |
| 163 | OpMul(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_) |
| 164 | : BinaryNode<Rank, InDtype, OutDtype>(Op_MUL, qinfo_, id_) |
| 165 | { |
| 166 | INIT_ATTRIBUTE(Mul); |
| 167 | register_fcn(); |
| 168 | } |
| 169 | static constexpr int64_t QMin = GetQMin<OutDtype>::value; |
| 170 | static constexpr int64_t QMax = GetQMax<OutDtype>::value; |
| 171 | using InEigenType = typename GetEigenType<InDtype>::type; |
| 172 | using OutEigenType = typename GetEigenType<OutDtype>::type; |
| 173 | virtual int register_fcn(); |
| 174 | |
| 175 | protected: |
| 176 | TosaMulAttribute* attribute; |
| 177 | }; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 178 | |
Kevin Cheng | 571f718 | 2021-05-24 17:20:01 -0700 | [diff] [blame^] | 179 | template <int Rank, DType InDtype> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 180 | class OpTable : public GraphNode |
| 181 | { |
| 182 | public: |
| 183 | OpTable(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_); |
| 184 | virtual ~OpTable(); |
| 185 | |
| 186 | virtual int checkTensorAttributes(); |
| 187 | virtual int eval(); |
| 188 | |
Kevin Cheng | 571f718 | 2021-05-24 17:20:01 -0700 | [diff] [blame^] | 189 | static constexpr DType TableDtype = (InDtype == DType_INT8) ? DType_INT8 : DType_INT16; |
| 190 | static constexpr DType OutDtype = (InDtype == DType_INT8) ? DType_INT8 : DType_INT32; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 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; |
| 204 | |
| 205 | protected: |
| 206 | TosaReference::TensorTemplate<TIn>* in; |
| 207 | TosaReference::TensorTemplate<TTable>* table; |
| 208 | TosaReference::TensorTemplate<TOut>* out; |
| 209 | }; |
| 210 | |
| 211 | }; // namespace TosaReference |
| 212 | |
| 213 | #endif |