blob: a5b1059022acb3398e3f699d072b807ad30dc5a4 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
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
21using namespace tosa;
22
23namespace 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
41template <int Rank, DType InDtype, DType OutDtype>
42class BinaryNodeBase : public GraphNode
43{
44public:
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
57protected:
58 int broadcast();
59
60protected:
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
75template <int Rank, DType InDtype, DType OutDtype>
76class BinaryNode : public BinaryNodeBase<Rank, InDtype, OutDtype>
77{
78public:
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
94template <DType InDtype, DType OutDtype>
95class BinaryNode<0, InDtype, OutDtype> : public BinaryNodeBase<0, InDtype, OutDtype>
96{
97public:
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 Chengaee1fac2020-11-11 13:54:06 -0800107#define DEF_TEMPLATE_BINARY_OP_DEFAULT(Opname, OPNAME) \
Eric Kunzee5e26762020-10-13 16:11:07 -0700108 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 Chengaee1fac2020-11-11 13:54:06 -0800124DEF_TEMPLATE_BINARY_OP_DEFAULT(Add, ADD)
125DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseAnd, BITWISE_AND)
126DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseOr, BITWISE_OR)
127DEF_TEMPLATE_BINARY_OP_DEFAULT(BitwiseXor, BITWISE_XOR)
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700128DEF_TEMPLATE_BINARY_OP_DEFAULT(Div, DIV)
Kevin Chengaee1fac2020-11-11 13:54:06 -0800129DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalAnd, LOGICAL_AND)
130DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalLeftShift, LOGICAL_LEFT_SHIFT)
131DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalRightShift, LOGICAL_RIGHT_SHIFT)
132DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalOr, LOGICAL_OR)
133DEF_TEMPLATE_BINARY_OP_DEFAULT(LogicalXor, LOGICAL_XOR)
134DEF_TEMPLATE_BINARY_OP_DEFAULT(Maximum, MAXIMUM)
135DEF_TEMPLATE_BINARY_OP_DEFAULT(Minimum, MINIMUM)
136DEF_TEMPLATE_BINARY_OP_DEFAULT(Pow, POW)
137DEF_TEMPLATE_BINARY_OP_DEFAULT(Sub, SUB)
Eric Kunzee5e26762020-10-13 16:11:07 -0700138
Kevin Chengaee1fac2020-11-11 13:54:06 -0800139#undef DEF_TEMPLATE_BINARY_OP_DEFAULT
Eric Kunzee5e26762020-10-13 16:11:07 -0700140
Kevin Chengaee1fac2020-11-11 13:54:06 -0800141template <int Rank, DType Dtype>
142class OpArithmeticRightShift : public BinaryNode<Rank, Dtype, Dtype>
143{
144public:
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
155protected:
156 TosaArithmeticRightShiftAttribute* attribute;
157};
158
159template <int Rank, DType InDtype, DType OutDtype>
160class OpMul : public BinaryNode<Rank, InDtype, OutDtype>
161{
162public:
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
175protected:
176 TosaMulAttribute* attribute;
177};
Eric Kunzee5e26762020-10-13 16:11:07 -0700178
Kevin Cheng571f7182021-05-24 17:20:01 -0700179template <int Rank, DType InDtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700180class OpTable : public GraphNode
181{
182public:
183 OpTable(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_);
184 virtual ~OpTable();
185
186 virtual int checkTensorAttributes();
187 virtual int eval();
188
Kevin Cheng571f7182021-05-24 17:20:01 -0700189 static constexpr DType TableDtype = (InDtype == DType_INT8) ? DType_INT8 : DType_INT16;
190 static constexpr DType OutDtype = (InDtype == DType_INT8) ? DType_INT8 : DType_INT32;
Eric Kunzee5e26762020-10-13 16:11:07 -0700191 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
205protected:
206 TosaReference::TensorTemplate<TIn>* in;
207 TosaReference::TensorTemplate<TTable>* table;
208 TosaReference::TensorTemplate<TOut>* out;
209};
210
211}; // namespace TosaReference
212
213#endif