blob: 22ace956389335abfdce53ca74756ea82d3d92fb [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Won Jeon66704d52023-06-28 22:34:38 +00002// Copyright (c) 2020-2024, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07003//
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_binary.h"
17#include "arith_util.h"
18#include "quant_util.h"
19#include "template_types.h"
20
21using namespace TosaReference;
22using namespace Eigen;
23using namespace tosa;
24
Tai Lya4d748b2023-03-28 22:06:56 +000025template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Jerry Ge9c9c8da2023-07-19 23:08:16 +000026BinaryNodeBase<Rank, InDtype, OutDtype>::BinaryNodeBase(SubgraphTraverser* sgt_, const Op& op_, uint64_t id_)
Kevin Chengacb550f2021-06-29 15:32:19 -070027 : GraphNode(sgt_, op_, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070028{
29 setRequiredOperands(2, 1);
Eric Kunzee5e26762020-10-13 16:11:07 -070030
Kevin Chengc42addc2021-09-28 15:41:57 -070031 a = b = nullptr;
32 result = nullptr;
Eric Kunzee5e26762020-10-13 16:11:07 -070033
34 fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return OutEigenType(); };
35}
36
Tai Lya4d748b2023-03-28 22:06:56 +000037template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070038BinaryNodeBase<Rank, InDtype, OutDtype>::~BinaryNodeBase()
39{}
40
Tai Lya4d748b2023-03-28 22:06:56 +000041template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070042int BinaryNodeBase<Rank, InDtype, OutDtype>::checkTensorAttributes()
43{
Jerry Gea793f462023-04-11 00:05:02 +000044 // Check Tosa Level
45 auto tosa_level = g_func_config.tosa_level;
46 LEVEL_CHECK(Rank <= tosa_level.MAX_RANK, "Rank should be smaller than or equal to MAX_RANK");
47
Eric Kunzee5e26762020-10-13 16:11:07 -070048 if (validateRequiredOperands())
49 return 1;
50
Kevin Chengc42addc2021-09-28 15:41:57 -070051 // A & B must be the same rank and types
52 if (inputs[0]->matchRankType(*inputs[1]))
Eric Kunzee5e26762020-10-13 16:11:07 -070053 {
54 printNodeValidationError("Binary operator input types must match");
55 return 1;
56 }
57
Kevin Cheng1c3c8472021-11-08 11:19:10 -080058 if (inputs[0]->matchRankShape(*outputs[0], true /* broadcastOk */))
Kevin Cheng478101b2021-10-04 10:43:14 -070059 {
60 std::string err =
Kevin Cheng1c3c8472021-11-08 11:19:10 -080061 "Binary operators " + std::string(EnumNamesOp()[nodeType]) + " lhs input and output rank/shape must match";
62 printNodeValidationError(err.c_str());
63 return 1;
64 }
65
66 if (inputs[1]->matchRankShape(*outputs[0], true /* broadcastOk */))
67 {
68 std::string err =
69 "Binary operators " + std::string(EnumNamesOp()[nodeType]) + " rhs input and output rank/shape must match";
Kevin Cheng478101b2021-10-04 10:43:14 -070070 printNodeValidationError(err.c_str());
71 return 1;
72 }
Eric Kunzee5e26762020-10-13 16:11:07 -070073
Kevin Chengcc61be32021-10-14 17:09:57 -070074 ERROR_IF(outputs[0]->getDtype() != OutDtype, "Binary operator type doesn't match");
75
Kevin Chengc42addc2021-09-28 15:41:57 -070076 a = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
77 b = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]);
Eric Kunzee5e26762020-10-13 16:11:07 -070078 result = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
79
Kevin Chengc42addc2021-09-28 15:41:57 -070080 ASSERT_MEM(a && b && result);
Eric Kunzee5e26762020-10-13 16:11:07 -070081
82 return 0;
83}
84
Tai Lya4d748b2023-03-28 22:06:56 +000085template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Jerry Ge135c9552023-05-23 20:59:32 +000086int BinaryNodeBase<Rank, InDtype, OutDtype>::broadcast(std::vector<int>& calculated_shape)
Eric Kunzee5e26762020-10-13 16:11:07 -070087{
Kevin Cheng1c3c8472021-11-08 11:19:10 -080088 const std::vector<int>& a_shape = a->getShape();
89 const std::vector<int>& b_shape = b->getShape();
90 const std::vector<int>& output_shape = result->getShape();
Eric Kunzee5e26762020-10-13 16:11:07 -070091
Jerry Ge135c9552023-05-23 20:59:32 +000092 // calculates the multipliers for Eigen
Kevin Cheng1c3c8472021-11-08 11:19:10 -080093 for (int i = 0; i < Rank; i++)
Eric Kunzee5e26762020-10-13 16:11:07 -070094 {
Kevin Cheng1c3c8472021-11-08 11:19:10 -080095 bcast_a[i] = (a_shape[i] != output_shape[i] && a_shape[i] == 1) ? output_shape[i] : 1;
96 bcast_b[i] = (b_shape[i] != output_shape[i] && b_shape[i] == 1) ? output_shape[i] : 1;
Eric Kunzee5e26762020-10-13 16:11:07 -070097 }
98
Jerry Ge135c9552023-05-23 20:59:32 +000099 // calculates the broadcasted output shape
100 calculated_shape = a_shape;
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000101 for (size_t i = 0; i < calculated_shape.size(); i++)
102 {
103 if (calculated_shape[i] == 1)
104 {
Jerry Ge135c9552023-05-23 20:59:32 +0000105 calculated_shape[i] = b_shape[i];
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000106 }
107 else
108 {
109 ERROR_IF(b_shape[i] != 1 && b_shape[i] != calculated_shape[i],
110 "Broadcast_shape failure, input shapes are not compatible");
Jerry Ge135c9552023-05-23 20:59:32 +0000111 }
112 }
113
Eric Kunzee5e26762020-10-13 16:11:07 -0700114 return 0;
115}
116
Tai Lya4d748b2023-03-28 22:06:56 +0000117template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700118int BinaryNode<Rank, InDtype, OutDtype>::eval()
119{
Jerry Ge135c9552023-05-23 20:59:32 +0000120 std::vector<int> calculated_shape;
121 this->broadcast(calculated_shape);
122
123 auto result_shape = this->result->getShape();
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000124 ERROR_IF(calculated_shape != result_shape,
125 "Broadcast_shape failure, calculated_shape and result_shape don't match");
Eric Kunzee5e26762020-10-13 16:11:07 -0700126
127 Eigen::array<int, Rank> reshaper;
128 reshaper.fill(1);
129 TIn ia, ib;
130
Kevin Chengc42addc2021-09-28 15:41:57 -0700131 ia = this->a->getTensor().broadcast(this->bcast_a);
132 ib = this->b->getTensor().broadcast(this->bcast_b);
Eric Kunzee5e26762020-10-13 16:11:07 -0700133
134 this->result->getTensor() = ia.binaryExpr(ib, this->fcn);
135
136 return GraphNode::eval();
137}
138
139// still need to partial specialize this, or Eigen will throw static assertion
Tai Lya4d748b2023-03-28 22:06:56 +0000140template <TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700141int BinaryNode<0, InDtype, OutDtype>::eval()
142{
143 this->result->getTensor() = this->a->getTensor().binaryExpr(this->b->getTensor(), this->fcn);
144
145 return GraphNode::eval();
146}
147
Tai Lya4d748b2023-03-28 22:06:56 +0000148template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700149int OpAdd<Rank, Dtype>::register_fcn()
150{
151 switch (InDtype)
152 {
Tai Lya4d748b2023-03-28 22:06:56 +0000153 case TOSA_REF_TYPE_INT32:
Jeremy Johnson90347472021-09-06 12:04:07 +0100154 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
155 int64_t res_in_64 = static_cast<int64_t>(a) + b;
156 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::max());
157 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::min());
158 REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpAdd: result not in i32 range");
159 return static_cast<InEigenType>(res_in_64);
160 };
161 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000162 case TOSA_REF_TYPE_FP16:
163 case TOSA_REF_TYPE_BF16:
164 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100165 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return fpTrunc<OutDtype>(a + b); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700166 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000167 case TOSA_REF_TYPE_FP64:
168 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a + b; };
169 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700170 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000171 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(InDtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700172 }
173
174 return 0;
175}
176
Tai Lya4d748b2023-03-28 22:06:56 +0000177template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700178int OpArithmeticRightShift<Rank, Dtype>::register_fcn()
179{
Kevin Chengaee1fac2020-11-11 13:54:06 -0800180 bool round = attribute->round();
Eric Kunzee5e26762020-10-13 16:11:07 -0700181 int32_t num_bits = 0;
182 switch (Dtype)
183 {
Tai Lya4d748b2023-03-28 22:06:56 +0000184 case TOSA_REF_TYPE_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -0700185 num_bits = 8;
186 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000187 case TOSA_REF_TYPE_INT16:
Eric Kunzee5e26762020-10-13 16:11:07 -0700188 num_bits = 16;
189 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000190 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700191 num_bits = 32;
192 break;
193 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000194 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700195 }
196
Kevin Chengaee1fac2020-11-11 13:54:06 -0800197 this->fcn = [this, round, num_bits](InEigenType a, InEigenType b) -> OutEigenType {
Kevin Chengacb550f2021-06-29 15:32:19 -0700198 REQUIRE(b >= 0 && b < num_bits, "OpArithmeticRightShift: shift value %d is out of valid range [0, %d]",
199 (int32_t)b, num_bits);
Kevin Chengaee1fac2020-11-11 13:54:06 -0800200
201 InEigenType acc = a >> b;
202
203 if (round && b > 0 && (a >> (b - 1) & 1) != 0)
204 {
205 acc++;
206 }
207
208 return acc;
Eric Kunzee5e26762020-10-13 16:11:07 -0700209 };
210
211 return 0;
212}
213
Tai Lya4d748b2023-03-28 22:06:56 +0000214template <int Rank, TOSA_REF_TYPE Dtype>
Jerry Gea6827492022-11-16 10:41:55 -0800215OpArithmeticRightShift<Rank, Dtype>::~OpArithmeticRightShift()
216{
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000217 if (attribute)
218 delete attribute;
Jerry Gea6827492022-11-16 10:41:55 -0800219}
220
Tai Lya4d748b2023-03-28 22:06:56 +0000221template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700222int OpBitwiseAnd<Rank, Dtype>::register_fcn()
223{
224 switch (Dtype)
225 {
Tai Lya4d748b2023-03-28 22:06:56 +0000226 case TOSA_REF_TYPE_INT8:
227 case TOSA_REF_TYPE_INT16:
228 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700229 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a & b; };
230 break;
231 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000232 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700233 }
234
235 return 0;
236}
237
Tai Lya4d748b2023-03-28 22:06:56 +0000238template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700239int OpBitwiseOr<Rank, Dtype>::register_fcn()
240{
241 switch (Dtype)
242 {
Tai Lya4d748b2023-03-28 22:06:56 +0000243 case TOSA_REF_TYPE_INT8:
244 case TOSA_REF_TYPE_INT16:
245 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700246 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a | b; };
247 break;
248 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000249 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700250 }
251
252 return 0;
253}
254
Tai Lya4d748b2023-03-28 22:06:56 +0000255template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700256int OpBitwiseXor<Rank, Dtype>::register_fcn()
257{
258 switch (Dtype)
259 {
Tai Lya4d748b2023-03-28 22:06:56 +0000260 case TOSA_REF_TYPE_INT8:
261 case TOSA_REF_TYPE_INT16:
262 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700263 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a ^ b; };
264 break;
265 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000266 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700267 }
268
269 return 0;
270}
271
Tai Lya4d748b2023-03-28 22:06:56 +0000272template <int Rank, TOSA_REF_TYPE Dtype>
Matthew Haddon459443c2021-08-23 16:43:13 +0100273int OpIntdiv<Rank, Dtype>::register_fcn()
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700274{
275 switch (InDtype)
276 {
Tai Lya4d748b2023-03-28 22:06:56 +0000277 case TOSA_REF_TYPE_INT32:
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700278 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
Matthew Haddon459443c2021-08-23 16:43:13 +0100279 REQUIRE(b != 0, "OpIntDiv: divisor must be non-zero value");
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700280 int64_t res_in_64 = static_cast<int64_t>(a) / b;
281 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::max());
Jeremy Johnson90347472021-09-06 12:04:07 +0100282 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::min());
283 REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpIntDiv: result not in i32 range");
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700284 return static_cast<InEigenType>(res_in_64);
285 };
286 break;
287 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000288 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(InDtype));
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700289 }
290
291 return 0;
292}
293
Tai Lya4d748b2023-03-28 22:06:56 +0000294template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700295int OpLogicalAnd<Rank, Dtype>::register_fcn()
296{
297 switch (Dtype)
298 {
Tai Lya4d748b2023-03-28 22:06:56 +0000299 case TOSA_REF_TYPE_BOOL:
Eric Kunzee5e26762020-10-13 16:11:07 -0700300 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a && b; };
301 break;
302 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000303 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700304 }
305
306 return 0;
307}
308
Tai Lya4d748b2023-03-28 22:06:56 +0000309template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700310int OpLogicalLeftShift<Rank, Dtype>::register_fcn()
311{
312 switch (Dtype)
313 {
Tai Lya4d748b2023-03-28 22:06:56 +0000314 case TOSA_REF_TYPE_INT8:
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000315 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
316 REQUIRE(b >= 0 && b <= 31, "OpLogicalLeftShift: shift value %d is out of valid range [0, 31]",
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000317 (int32_t)b);
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000318 return static_cast<OutEigenType>(static_cast<int8_t>(a << b));
319 };
Jeremy Johnson66bad802022-01-18 14:48:35 +0000320 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000321 case TOSA_REF_TYPE_INT16:
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000322 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
323 REQUIRE(b >= 0 && b <= 31, "OpLogicalLeftShift: shift value %d is out of valid range [0, 31]",
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000324 (int32_t)b);
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000325 return static_cast<OutEigenType>(static_cast<int16_t>(a << b));
326 };
Jeremy Johnson66bad802022-01-18 14:48:35 +0000327 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000328 case TOSA_REF_TYPE_INT32:
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000329 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
330 REQUIRE(b >= 0 && b <= 31, "OpLogicalLeftShift: shift value %d is out of valid range [0, 31]",
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000331 (int32_t)b);
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000332 return static_cast<OutEigenType>(static_cast<int32_t>(a << b));
333 };
Eric Kunzee5e26762020-10-13 16:11:07 -0700334 break;
335 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000336 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700337 }
338
339 return 0;
340}
341
Tai Lya4d748b2023-03-28 22:06:56 +0000342template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700343int OpLogicalRightShift<Rank, Dtype>::register_fcn()
344{
Eric Kunzee5e26762020-10-13 16:11:07 -0700345 switch (Dtype)
346 {
Tai Lya4d748b2023-03-28 22:06:56 +0000347 case TOSA_REF_TYPE_INT8:
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000348 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
349 REQUIRE(b >= 0 && b <= 31, "OpLogicalRightShift: shift value %d is out of valid range [0, 31]",
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000350 (int32_t)b);
Won Jeon66704d52023-06-28 22:34:38 +0000351 return static_cast<OutEigenType>(static_cast<int8_t>(static_cast<uint8_t>(a) >> b));
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000352 };
Eric Kunzee5e26762020-10-13 16:11:07 -0700353 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000354 case TOSA_REF_TYPE_INT16:
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000355 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
356 REQUIRE(b >= 0 && b <= 31, "OpLogicalRightShift: shift value %d is out of valid range [0, 31]",
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000357 (int32_t)b);
Won Jeon66704d52023-06-28 22:34:38 +0000358 return static_cast<OutEigenType>(static_cast<int16_t>(static_cast<uint16_t>(a) >> b));
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000359 };
Eric Kunzee5e26762020-10-13 16:11:07 -0700360 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000361 case TOSA_REF_TYPE_INT32:
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000362 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
363 REQUIRE(b >= 0 && b <= 31, "OpLogicalRightShift: shift value %d is out of valid range [0, 31]",
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000364 (int32_t)b);
Won Jeon66704d52023-06-28 22:34:38 +0000365 return static_cast<OutEigenType>(static_cast<int32_t>(static_cast<uint32_t>(a) >> b));
Jeremy Johnsoneef86672023-01-18 16:23:20 +0000366 };
Eric Kunzee5e26762020-10-13 16:11:07 -0700367 break;
368 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000369 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700370 }
371
Eric Kunzee5e26762020-10-13 16:11:07 -0700372 return 0;
373}
374
Tai Lya4d748b2023-03-28 22:06:56 +0000375template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700376int OpLogicalOr<Rank, Dtype>::register_fcn()
377{
378 switch (Dtype)
379 {
Tai Lya4d748b2023-03-28 22:06:56 +0000380 case TOSA_REF_TYPE_BOOL:
Eric Kunzee5e26762020-10-13 16:11:07 -0700381 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a || b; };
382 break;
383 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000384 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700385 }
386
387 return 0;
388}
389
Tai Lya4d748b2023-03-28 22:06:56 +0000390template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700391int OpLogicalXor<Rank, Dtype>::register_fcn()
392{
393 switch (Dtype)
394 {
Tai Lya4d748b2023-03-28 22:06:56 +0000395 case TOSA_REF_TYPE_BOOL:
Eric Kunzee5e26762020-10-13 16:11:07 -0700396 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a ^ b; };
397 break;
398 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000399 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700400 }
401
402 return 0;
403}
404
Tai Lya4d748b2023-03-28 22:06:56 +0000405template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700406int OpMaximum<Rank, Dtype>::register_fcn()
407{
408 switch (Dtype)
409 {
Tai Lya4d748b2023-03-28 22:06:56 +0000410 case TOSA_REF_TYPE_FP16:
411 case TOSA_REF_TYPE_BF16:
412 case TOSA_REF_TYPE_FP32:
413 case TOSA_REF_TYPE_FP64:
414 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700415 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a > b ? a : b; };
416 break;
417 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000418 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700419 }
420
421 return 0;
422}
423
Tai Lya4d748b2023-03-28 22:06:56 +0000424template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700425int OpMinimum<Rank, Dtype>::register_fcn()
426{
427 switch (Dtype)
428 {
Tai Lya4d748b2023-03-28 22:06:56 +0000429 case TOSA_REF_TYPE_FP16:
430 case TOSA_REF_TYPE_BF16:
431 case TOSA_REF_TYPE_FP32:
432 case TOSA_REF_TYPE_FP64:
433 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700434 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a < b ? a : b; };
435 break;
436 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000437 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700438 }
439
440 return 0;
441}
442
Tai Lya4d748b2023-03-28 22:06:56 +0000443template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700444int OpMul<Rank, InDtype, OutDtype>::register_fcn()
445{
Kevin Chengaee1fac2020-11-11 13:54:06 -0800446 int32_t shift = attribute->shift();
Kevin Chengaee1fac2020-11-11 13:54:06 -0800447
Eric Kunzee5e26762020-10-13 16:11:07 -0700448 switch (InDtype)
449 {
Tai Lya4d748b2023-03-28 22:06:56 +0000450 case TOSA_REF_TYPE_FP16:
451 case TOSA_REF_TYPE_BF16:
452 case TOSA_REF_TYPE_FP32:
Eric Kunze17fab3b2023-08-02 18:15:20 +0000453 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return fpTrunc<OutDtype>(a * b); };
Kevin Chengaee1fac2020-11-11 13:54:06 -0800454 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000455 case TOSA_REF_TYPE_FP64:
Eric Kunze17fab3b2023-08-02 18:15:20 +0000456 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a * b; };
Tai Lya4d748b2023-03-28 22:06:56 +0000457 break;
458 case TOSA_REF_TYPE_INT32:
Kevin Chengaee1fac2020-11-11 13:54:06 -0800459 this->fcn = [this, shift](InEigenType a, InEigenType b) -> OutEigenType {
460 int64_t result;
461 if (shift > 0)
462 {
Jerry Gecf305db2023-03-06 13:07:36 -0800463 int64_t round = INT64_C(1) << (shift - 1);
Kevin Cheng9257fd52021-04-14 15:55:31 -0700464 result = static_cast<int64_t>(a) * static_cast<int64_t>(b) + round;
Kevin Chengaee1fac2020-11-11 13:54:06 -0800465 result = result >> shift;
466
Jack Frankland438ad7f2023-12-01 14:55:19 +0000467 REQUIRE(result >= QMin && result <= QMax,
468 "OpMul: result %" PRId64 " exceeds valid range [%" PRId64 ", %" PRId64 "]", result, QMin,
469 QMax);
Kevin Chengaee1fac2020-11-11 13:54:06 -0800470 }
471 else
472 {
Kevin Chengc42addc2021-09-28 15:41:57 -0700473 result = static_cast<int64_t>(a) * b;
Jeremy Johnson90347472021-09-06 12:04:07 +0100474 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::max());
475 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::min());
476 REQUIRE(result <= i32_max_in_64 && result >= i32_min_in_64, "OpMul: result not in i32 range");
477 return static_cast<InEigenType>(result);
Kevin Chengaee1fac2020-11-11 13:54:06 -0800478 }
479
480 return static_cast<OutEigenType>(result);
481 };
Eric Kunzee5e26762020-10-13 16:11:07 -0700482 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000483 case TOSA_REF_TYPE_INT8:
484 case TOSA_REF_TYPE_INT16:
Eric Kunze17fab3b2023-08-02 18:15:20 +0000485 this->fcn = [](InEigenType lhs, InEigenType rhs) -> OutEigenType {
Eric Kunzee5e26762020-10-13 16:11:07 -0700486 OutEigenType raw_output = (OutEigenType)lhs * (OutEigenType)rhs;
487
488 OutEigenType clamped_output = std::min<OutEigenType>(QMax, std::max<OutEigenType>(raw_output, QMin));
489
490 return clamped_output;
491 };
492 break;
493 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000494 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(InDtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700495 }
496
497 return 0;
498}
499
Tai Lya4d748b2023-03-28 22:06:56 +0000500template <int Rank, TOSA_REF_TYPE InDtype, TOSA_REF_TYPE OutDtype>
Jerry Gea6827492022-11-16 10:41:55 -0800501OpMul<Rank, InDtype, OutDtype>::~OpMul()
502{
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000503 if (attribute)
504 delete attribute;
Jerry Gea6827492022-11-16 10:41:55 -0800505}
506
Tai Lya4d748b2023-03-28 22:06:56 +0000507template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700508int OpPow<Rank, Dtype>::register_fcn()
509{
510 switch (Dtype)
511 {
Tai Lya4d748b2023-03-28 22:06:56 +0000512 case TOSA_REF_TYPE_FP16:
513 case TOSA_REF_TYPE_BF16:
514 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100515 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return fpTrunc<OutDtype>(powf(a, b)); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700516 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000517 case TOSA_REF_TYPE_FP64:
Jeremy Johnson9a758382023-11-07 16:27:35 +0000518 if (g_func_config.abs_mode)
519 {
520 // ABS_ERROR bounds return (1+abs(log(abs(a))*b))
521 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType {
522 OutEigenType c = log(a > (InEigenType)0 ? a : (-a)) * b;
523 return 1.0 + (c > (OutEigenType)0 ? c : (-c));
524 };
525 }
526 else
527 {
528 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return pow(a, b); };
529 }
Tai Lya4d748b2023-03-28 22:06:56 +0000530 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700531 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000532 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700533 }
534
535 return 0;
536}
537
Tai Lya4d748b2023-03-28 22:06:56 +0000538template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700539int OpSub<Rank, Dtype>::register_fcn()
540{
541 switch (InDtype)
542 {
Tai Lya4d748b2023-03-28 22:06:56 +0000543 case TOSA_REF_TYPE_INT32:
Jeremy Johnson90347472021-09-06 12:04:07 +0100544 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
545 int64_t res_in_64 = static_cast<int64_t>(a) - b;
546 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::max());
547 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::min());
548 REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpSub: result not in i32 range");
549 return static_cast<InEigenType>(res_in_64);
550 };
551 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000552 case TOSA_REF_TYPE_FP16:
553 case TOSA_REF_TYPE_BF16:
554 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100555 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return fpTrunc<OutDtype>(a - b); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700556 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000557 case TOSA_REF_TYPE_FP64:
558 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a - b; };
559 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700560 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000561 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(InDtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700562 }
563
564 return 0;
565}
566
Tai Lya4d748b2023-03-28 22:06:56 +0000567template <int Rank, TOSA_REF_TYPE InDtype>
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000568OpTable<Rank, InDtype>::OpTable(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_)
Kevin Chengacb550f2021-06-29 15:32:19 -0700569 : GraphNode(sgt_, Op_TABLE, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -0700570{
Kevin Chengfe392ce2021-10-18 21:51:55 +0000571 setRequiredOperands(1, 1);
Eric Kunzee5e26762020-10-13 16:11:07 -0700572 setRequiredRank(0, 6);
Kevin Chengfe392ce2021-10-18 21:51:55 +0000573
574 INIT_ATTRIBUTE(Table);
Eric Kunzee5e26762020-10-13 16:11:07 -0700575}
576
Tai Lya4d748b2023-03-28 22:06:56 +0000577template <int Rank, TOSA_REF_TYPE InDtype>
Kevin Cheng571f7182021-05-24 17:20:01 -0700578OpTable<Rank, InDtype>::~OpTable()
Jerry Gea6827492022-11-16 10:41:55 -0800579{
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000580 if (attribute)
581 delete attribute;
Jerry Gea6827492022-11-16 10:41:55 -0800582}
Eric Kunzee5e26762020-10-13 16:11:07 -0700583
Tai Lya4d748b2023-03-28 22:06:56 +0000584template <int Rank, TOSA_REF_TYPE InDtype>
Kevin Cheng571f7182021-05-24 17:20:01 -0700585int OpTable<Rank, InDtype>::checkTensorAttributes()
Eric Kunzee5e26762020-10-13 16:11:07 -0700586{
Jerry Gea793f462023-04-11 00:05:02 +0000587 // Check Tosa Level
588 auto tosa_level = g_func_config.tosa_level;
589 LEVEL_CHECK(Rank <= tosa_level.MAX_RANK, "Rank should be smaller than or equal to MAX_RANK");
590
Eric Kunzee5e26762020-10-13 16:11:07 -0700591 if (validateRequiredOperands())
592 return 1;
593
594 if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]))
595 {
596 return 1;
597 }
598
Kevin Chengfe392ce2021-10-18 21:51:55 +0000599 ERROR_IF(inputs[0]->getDtype() != InDtype, "OpTable: Unexpected input type");
Kevin Chengf3e016f2021-11-02 01:15:50 +0000600 ERROR_IF(outputs[0]->getDtype() != OutDtype, "OpTable: Unexpected output type");
Kevin Chengfe392ce2021-10-18 21:51:55 +0000601 ERROR_IF(attribute->table().size() != TableNumEntries, "OpTable: table attribute size must be %u", TableNumEntries);
602
603 for (uint32_t i = 0; i < TableNumEntries; i++)
Eric Kunzee5e26762020-10-13 16:11:07 -0700604 {
Kevin Chengfe392ce2021-10-18 21:51:55 +0000605 table[i] = (TableEigenType)attribute->table()[i];
Eric Kunzee5e26762020-10-13 16:11:07 -0700606 }
607
Kevin Chengfe392ce2021-10-18 21:51:55 +0000608 in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
609 out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
Kevin Cheng571f7182021-05-24 17:20:01 -0700610
Kevin Chengfe392ce2021-10-18 21:51:55 +0000611 ASSERT_MEM(in && out);
Eric Kunzee5e26762020-10-13 16:11:07 -0700612
613 return 0;
614}
615
Tai Lya4d748b2023-03-28 22:06:56 +0000616template <int Rank, TOSA_REF_TYPE InDtype>
Kevin Cheng571f7182021-05-24 17:20:01 -0700617int OpTable<Rank, InDtype>::eval()
Eric Kunzee5e26762020-10-13 16:11:07 -0700618{
Kevin Cheng571f7182021-05-24 17:20:01 -0700619 switch (InDtype)
620 {
Tai Lya4d748b2023-03-28 22:06:56 +0000621 case TOSA_REF_TYPE_INT8:
Kevin Cheng571f7182021-05-24 17:20:01 -0700622 this->out->getTensor() = this->in->getTensor().unaryExpr([this](InEigenType in) -> OutEigenType {
623 int32_t input_truncated = std::min<int32_t>(std::max<int32_t>(in, QInMin), QInMax);
624 int32_t index = input_truncated - QInMin;
Kevin Chengfe392ce2021-10-18 21:51:55 +0000625 int32_t value = table[index];
Eric Kunzee5e26762020-10-13 16:11:07 -0700626
Kevin Cheng571f7182021-05-24 17:20:01 -0700627 return value;
628 });
629 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000630 case TOSA_REF_TYPE_INT16:
Kevin Cheng571f7182021-05-24 17:20:01 -0700631 this->out->getTensor() = this->in->getTensor().unaryExpr([this](InEigenType in) -> OutEigenType {
632 // 1. make sure input is int16 range
633 int32_t input_truncated = std::min<int32_t>(std::max<int32_t>(in, QInMin), QInMax);
Eric Kunzee5e26762020-10-13 16:11:07 -0700634
Kevin Cheng571f7182021-05-24 17:20:01 -0700635 // 2. calculate index and interpolation fraction
636 int32_t index = (input_truncated >> FractionBits) + (1 << (IntegerBits - 1));
637 index = std::min<int32_t>(std::max<int32_t>(index, 0), NumTableEntries - 1); // 9-bit index
638 int32_t frac = (input_truncated)&0x7F; // 7-bit fraction
Eric Kunzee5e26762020-10-13 16:11:07 -0700639
Jerry Ged511f9e2022-08-12 16:12:40 -0700640 // 3. Add REQUIRE CHECK for extreme large/small slopes
Kevin Chengfe392ce2021-10-18 21:51:55 +0000641 int32_t base = table[index];
642 int32_t next = table[index + 1];
Jerry Ged511f9e2022-08-12 16:12:40 -0700643 int32_t slope = next - base;
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000644 REQUIRE(slope <= std::numeric_limits<int16_t>::max() && slope >= std::numeric_limits<int16_t>::min(),
645 "OpTable: slope out of int16_t range");
Jerry Ged511f9e2022-08-12 16:12:40 -0700646
647 // 4. interpolate, generate 16.7 (23-bit) output
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000648 int32_t value = (base << 7) + (slope)*frac;
Kevin Cheng571f7182021-05-24 17:20:01 -0700649
650 return value;
651 });
652 break;
653 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000654 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(InDtype));
Kevin Cheng571f7182021-05-24 17:20:01 -0700655 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700656
657 return GraphNode::eval();
658}
659
660// template explicit instantiation
Jared Smolens98c281f2022-12-20 15:09:25 -0800661DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, FP16, FP16);
662DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, BF16, BF16);
663DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, FP32, FP32);
664DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, INT8, INT8);
665DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, INT16, INT16);
666DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, INT32, INT32);
667DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, INT8, INT32);
668DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, INT16, INT32);
669DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, BOOL, BOOL);
670DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, FP16, BOOL);
671DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, BF16, BOOL);
672DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, FP32, BOOL);
673DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, INT32, BOOL);
Tai Lya4d748b2023-03-28 22:06:56 +0000674DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, FP64, FP64);
Eric Kunzeedac6ab2023-06-28 13:29:38 -0700675DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNodeBase, FP64, BOOL);
Jared Smolens98c281f2022-12-20 15:09:25 -0800676
James Ward8b390432022-08-12 20:48:56 +0100677DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAdd, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100678DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAdd, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100679DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAdd, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700680DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAdd, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000681DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAdd, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700682
683DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpArithmeticRightShift, INT8);
684DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpArithmeticRightShift, INT16);
685DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpArithmeticRightShift, INT32);
686
Kevin Cheng3a478572021-01-22 17:21:02 -0800687DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseAnd, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700688DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseAnd, INT16);
689DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseAnd, INT32);
690
Kevin Cheng3a478572021-01-22 17:21:02 -0800691DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseOr, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700692DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseOr, INT16);
693DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseOr, INT32);
694
Kevin Cheng3a478572021-01-22 17:21:02 -0800695DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseXor, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700696DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseXor, INT16);
697DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseXor, INT32);
698
Matthew Haddon459443c2021-08-23 16:43:13 +0100699DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIntdiv, INT32);
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700700
Eric Kunzee5e26762020-10-13 16:11:07 -0700701DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalAnd, BOOL);
702
703DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalLeftShift, INT8);
704DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalLeftShift, INT16);
705DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalLeftShift, INT32);
706
707DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalRightShift, INT8);
708DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalRightShift, INT16);
709DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalRightShift, INT32);
710
711DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalOr, BOOL);
712
713DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalXor, BOOL);
714
James Ward8b390432022-08-12 20:48:56 +0100715DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMaximum, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100716DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMaximum, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100717DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMaximum, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700718DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMaximum, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000719DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMaximum, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700720
James Ward8b390432022-08-12 20:48:56 +0100721DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMinimum, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100722DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMinimum, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100723DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMinimum, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700724DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMinimum, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000725DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMinimum, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700726
James Ward8b390432022-08-12 20:48:56 +0100727DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, FP16, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100728DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, BF16, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100729DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, FP32, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700730DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, INT8, INT32);
731DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, INT16, INT32);
732DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, INT32, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000733DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, FP64, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700734
James Ward8b390432022-08-12 20:48:56 +0100735DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpPow, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100736DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpPow, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100737DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpPow, FP32);
Tai Lya4d748b2023-03-28 22:06:56 +0000738DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpPow, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700739
James Ward8b390432022-08-12 20:48:56 +0100740DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSub, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100741DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSub, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100742DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSub, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700743DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSub, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000744DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSub, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700745
Kevin Cheng571f7182021-05-24 17:20:01 -0700746DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTable, INT8);
747DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTable, INT16);
Eric Kunzee5e26762020-10-13 16:11:07 -0700748
James Ward8b390432022-08-12 20:48:56 +0100749// Instantiation of nodes for comparison operators opEqual, opGreater
750// and opGreaterEqual
751DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNode, FP16, BOOL);
James Ward24dbc422022-10-19 12:20:31 +0100752DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNode, BF16, BOOL);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100753DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNode, FP32, BOOL);
Eric Kunzee5e26762020-10-13 16:11:07 -0700754DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNode, INT32, BOOL);
Tai Lya4d748b2023-03-28 22:06:56 +0000755DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNode, FP64, BOOL);