blob: 7f30e30debc13d13428b41af63ce10ac5277dacd [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Kevin Cheng3a478572021-01-22 17:21:02 -08002// Copyright (c) 2020-2021, 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
25template <int Rank, DType InDtype, DType OutDtype>
Kevin Chengacb550f2021-06-29 15:32:19 -070026BinaryNodeBase<Rank, InDtype, OutDtype>::BinaryNodeBase(SubgraphTraverser* sgt_,
27 const Op& op_,
28 TosaQuantInfoBase* qinfo_,
29 uint64_t id_)
30 : GraphNode(sgt_, op_, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070031{
32 setRequiredOperands(2, 1);
33 setRequiredRank(0, 6);
34
Kevin Chengc42addc2021-09-28 15:41:57 -070035 a = b = nullptr;
36 result = nullptr;
Eric Kunzee5e26762020-10-13 16:11:07 -070037
38 fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return OutEigenType(); };
39}
40
41template <int Rank, DType InDtype, DType OutDtype>
42BinaryNodeBase<Rank, InDtype, OutDtype>::~BinaryNodeBase()
43{}
44
45template <int Rank, DType InDtype, DType OutDtype>
46int BinaryNodeBase<Rank, InDtype, OutDtype>::checkTensorAttributes()
47{
48 if (validateRequiredOperands())
49 return 1;
50
51 if (validateRequiredRank(inputs[0]) || validateRequiredRank(inputs[1]) || validateRequiredRank(outputs[0]))
52 {
53 return 1;
54 }
55
Kevin Chengc42addc2021-09-28 15:41:57 -070056 // A & B must be the same rank and types
57 if (inputs[0]->matchRankType(*inputs[1]))
Eric Kunzee5e26762020-10-13 16:11:07 -070058 {
59 printNodeValidationError("Binary operator input types must match");
60 return 1;
61 }
62
Kevin Cheng1c3c8472021-11-08 11:19:10 -080063 if (inputs[0]->matchRankShape(*outputs[0], true /* broadcastOk */))
Kevin Cheng478101b2021-10-04 10:43:14 -070064 {
65 std::string err =
Kevin Cheng1c3c8472021-11-08 11:19:10 -080066 "Binary operators " + std::string(EnumNamesOp()[nodeType]) + " lhs input and output rank/shape must match";
67 printNodeValidationError(err.c_str());
68 return 1;
69 }
70
71 if (inputs[1]->matchRankShape(*outputs[0], true /* broadcastOk */))
72 {
73 std::string err =
74 "Binary operators " + std::string(EnumNamesOp()[nodeType]) + " rhs input and output rank/shape must match";
Kevin Cheng478101b2021-10-04 10:43:14 -070075 printNodeValidationError(err.c_str());
76 return 1;
77 }
Eric Kunzee5e26762020-10-13 16:11:07 -070078
Kevin Chengcc61be32021-10-14 17:09:57 -070079 ERROR_IF(outputs[0]->getDtype() != OutDtype, "Binary operator type doesn't match");
80
Kevin Chengc42addc2021-09-28 15:41:57 -070081 a = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
82 b = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[1]);
Eric Kunzee5e26762020-10-13 16:11:07 -070083 result = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
84
Kevin Chengc42addc2021-09-28 15:41:57 -070085 ASSERT_MEM(a && b && result);
Eric Kunzee5e26762020-10-13 16:11:07 -070086
87 return 0;
88}
89
90template <int Rank, DType InDtype, DType OutDtype>
91int BinaryNodeBase<Rank, InDtype, OutDtype>::broadcast()
92{
Kevin Cheng1c3c8472021-11-08 11:19:10 -080093 const std::vector<int>& a_shape = a->getShape();
94 const std::vector<int>& b_shape = b->getShape();
95 const std::vector<int>& output_shape = result->getShape();
Eric Kunzee5e26762020-10-13 16:11:07 -070096
Kevin Cheng1c3c8472021-11-08 11:19:10 -080097 for (int i = 0; i < Rank; i++)
Eric Kunzee5e26762020-10-13 16:11:07 -070098 {
Kevin Cheng1c3c8472021-11-08 11:19:10 -080099 bcast_a[i] = (a_shape[i] != output_shape[i] && a_shape[i] == 1) ? output_shape[i] : 1;
100 bcast_b[i] = (b_shape[i] != output_shape[i] && b_shape[i] == 1) ? output_shape[i] : 1;
Eric Kunzee5e26762020-10-13 16:11:07 -0700101 }
102
103 return 0;
104}
105
106template <int Rank, DType InDtype, DType OutDtype>
107int BinaryNode<Rank, InDtype, OutDtype>::eval()
108{
109 this->broadcast();
110
111 Eigen::array<int, Rank> reshaper;
112 reshaper.fill(1);
113 TIn ia, ib;
114
Kevin Chengc42addc2021-09-28 15:41:57 -0700115 ia = this->a->getTensor().broadcast(this->bcast_a);
116 ib = this->b->getTensor().broadcast(this->bcast_b);
Eric Kunzee5e26762020-10-13 16:11:07 -0700117
118 this->result->getTensor() = ia.binaryExpr(ib, this->fcn);
119
120 return GraphNode::eval();
121}
122
123// still need to partial specialize this, or Eigen will throw static assertion
124template <DType InDtype, DType OutDtype>
125int BinaryNode<0, InDtype, OutDtype>::eval()
126{
127 this->result->getTensor() = this->a->getTensor().binaryExpr(this->b->getTensor(), this->fcn);
128
129 return GraphNode::eval();
130}
131
132template <int Rank, DType Dtype>
133int OpAdd<Rank, Dtype>::register_fcn()
134{
135 switch (InDtype)
136 {
Eric Kunzee5e26762020-10-13 16:11:07 -0700137 case DType_INT32:
Jeremy Johnson90347472021-09-06 12:04:07 +0100138 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
139 int64_t res_in_64 = static_cast<int64_t>(a) + b;
140 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::max());
141 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::min());
142 REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpAdd: result not in i32 range");
143 return static_cast<InEigenType>(res_in_64);
144 };
145 break;
146 case DType_FLOAT:
Eric Kunzee5e26762020-10-13 16:11:07 -0700147 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a + b; };
148 break;
149 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700150 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[InDtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700151 }
152
153 return 0;
154}
155
156template <int Rank, DType Dtype>
157int OpArithmeticRightShift<Rank, Dtype>::register_fcn()
158{
Kevin Chengaee1fac2020-11-11 13:54:06 -0800159 bool round = attribute->round();
Eric Kunzee5e26762020-10-13 16:11:07 -0700160 int32_t num_bits = 0;
161 switch (Dtype)
162 {
163 case DType_INT8:
164 num_bits = 8;
165 break;
166 case DType_INT16:
167 num_bits = 16;
168 break;
169 case DType_INT32:
170 num_bits = 32;
171 break;
172 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700173 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700174 }
175
Kevin Chengaee1fac2020-11-11 13:54:06 -0800176 this->fcn = [this, round, num_bits](InEigenType a, InEigenType b) -> OutEigenType {
Kevin Chengacb550f2021-06-29 15:32:19 -0700177 REQUIRE(b >= 0 && b < num_bits, "OpArithmeticRightShift: shift value %d is out of valid range [0, %d]",
178 (int32_t)b, num_bits);
Kevin Chengaee1fac2020-11-11 13:54:06 -0800179
180 InEigenType acc = a >> b;
181
182 if (round && b > 0 && (a >> (b - 1) & 1) != 0)
183 {
184 acc++;
185 }
186
187 return acc;
Eric Kunzee5e26762020-10-13 16:11:07 -0700188 };
189
190 return 0;
191}
192
193template <int Rank, DType Dtype>
194int OpBitwiseAnd<Rank, Dtype>::register_fcn()
195{
196 switch (Dtype)
197 {
Kevin Cheng3a478572021-01-22 17:21:02 -0800198 case DType_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -0700199 case DType_INT16:
200 case DType_INT32:
201 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a & b; };
202 break;
203 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700204 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700205 }
206
207 return 0;
208}
209
210template <int Rank, DType Dtype>
211int OpBitwiseOr<Rank, Dtype>::register_fcn()
212{
213 switch (Dtype)
214 {
Kevin Cheng3a478572021-01-22 17:21:02 -0800215 case DType_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -0700216 case DType_INT16:
217 case DType_INT32:
218 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a | b; };
219 break;
220 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700221 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700222 }
223
224 return 0;
225}
226
227template <int Rank, DType Dtype>
228int OpBitwiseXor<Rank, Dtype>::register_fcn()
229{
230 switch (Dtype)
231 {
Kevin Cheng3a478572021-01-22 17:21:02 -0800232 case DType_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -0700233 case DType_INT16:
234 case DType_INT32:
235 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a ^ b; };
236 break;
237 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700238 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700239 }
240
241 return 0;
242}
243
244template <int Rank, DType Dtype>
Matthew Haddon459443c2021-08-23 16:43:13 +0100245int OpIntdiv<Rank, Dtype>::register_fcn()
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700246{
247 switch (InDtype)
248 {
249 case DType_INT32:
250 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
Matthew Haddon459443c2021-08-23 16:43:13 +0100251 REQUIRE(b != 0, "OpIntDiv: divisor must be non-zero value");
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700252 int64_t res_in_64 = static_cast<int64_t>(a) / b;
253 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::max());
Jeremy Johnson90347472021-09-06 12:04:07 +0100254 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::min());
255 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 -0700256 return static_cast<InEigenType>(res_in_64);
257 };
258 break;
259 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700260 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[InDtype]);
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700261 }
262
263 return 0;
264}
265
266template <int Rank, DType Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700267int OpLogicalAnd<Rank, Dtype>::register_fcn()
268{
269 switch (Dtype)
270 {
271 case DType_BOOL:
272 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a && b; };
273 break;
274 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700275 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700276 }
277
278 return 0;
279}
280
281template <int Rank, DType Dtype>
282int OpLogicalLeftShift<Rank, Dtype>::register_fcn()
283{
Jeremy Johnson66bad802022-01-18 14:48:35 +0000284 int32_t num_bits = 0;
Eric Kunzee5e26762020-10-13 16:11:07 -0700285 switch (Dtype)
286 {
287 case DType_INT8:
Jeremy Johnson66bad802022-01-18 14:48:35 +0000288 num_bits = 8;
289 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700290 case DType_INT16:
Jeremy Johnson66bad802022-01-18 14:48:35 +0000291 num_bits = 16;
292 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700293 case DType_INT32:
Jeremy Johnson66bad802022-01-18 14:48:35 +0000294 num_bits = 32;
Eric Kunzee5e26762020-10-13 16:11:07 -0700295 break;
296 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700297 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700298 }
Jeremy Johnson66bad802022-01-18 14:48:35 +0000299 this->fcn = [this, num_bits](InEigenType a, InEigenType b) -> OutEigenType {
300 uint32_t mask = ONES_MASK(num_bits);
301 REQUIRE(b >= 0 && b <= 31, "OpLogicalLeftShift: shift value %d is out of valid range [0, 31]",
302 (int32_t)b);
303 return (a << b) & mask;
304 };
Eric Kunzee5e26762020-10-13 16:11:07 -0700305
306 return 0;
307}
308
309template <int Rank, DType Dtype>
310int OpLogicalRightShift<Rank, Dtype>::register_fcn()
311{
312 int32_t num_bits = 0;
313 switch (Dtype)
314 {
315 case DType_INT8:
316 num_bits = 8;
317 break;
318 case DType_INT16:
319 num_bits = 16;
320 break;
321 case DType_INT32:
322 num_bits = 32;
323 break;
324 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700325 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700326 }
327
Jeremy Johnson66bad802022-01-18 14:48:35 +0000328 this->fcn = [this, num_bits](InEigenType a, InEigenType b) -> OutEigenType {
Eric Kunzee5e26762020-10-13 16:11:07 -0700329 uint32_t mask = ONES_MASK(num_bits) >> b;
Jeremy Johnson66bad802022-01-18 14:48:35 +0000330 REQUIRE(b >= 0 && b <= 31, "OpLogicalRightShift: shift value %d is out of valid range [0, 31]",
331 (int32_t)b);
Eric Kunzee5e26762020-10-13 16:11:07 -0700332 return (a >> b) & mask;
333 };
334
335 return 0;
336}
337
338template <int Rank, DType Dtype>
339int OpLogicalOr<Rank, Dtype>::register_fcn()
340{
341 switch (Dtype)
342 {
343 case DType_BOOL:
344 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a || b; };
345 break;
346 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700347 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700348 }
349
350 return 0;
351}
352
353template <int Rank, DType Dtype>
354int OpLogicalXor<Rank, Dtype>::register_fcn()
355{
356 switch (Dtype)
357 {
358 case DType_BOOL:
359 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a ^ b; };
360 break;
361 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700362 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700363 }
364
365 return 0;
366}
367
368template <int Rank, DType Dtype>
369int OpMaximum<Rank, Dtype>::register_fcn()
370{
371 switch (Dtype)
372 {
373 case DType_FLOAT:
374 case DType_INT32:
375 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a > b ? a : b; };
376 break;
377 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700378 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700379 }
380
381 return 0;
382}
383
384template <int Rank, DType Dtype>
385int OpMinimum<Rank, Dtype>::register_fcn()
386{
387 switch (Dtype)
388 {
389 case DType_FLOAT:
390 case DType_INT32:
391 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a < b ? a : b; };
392 break;
393 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700394 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700395 }
396
397 return 0;
398}
399
400template <int Rank, DType InDtype, DType OutDtype>
401int OpMul<Rank, InDtype, OutDtype>::register_fcn()
402{
Kevin Chengaee1fac2020-11-11 13:54:06 -0800403 int32_t shift = attribute->shift();
Kevin Chengaee1fac2020-11-11 13:54:06 -0800404
Eric Kunzee5e26762020-10-13 16:11:07 -0700405 switch (InDtype)
406 {
407 case DType_FLOAT:
Kevin Chengaee1fac2020-11-11 13:54:06 -0800408 this->fcn = [shift](InEigenType a, InEigenType b) -> OutEigenType { return a * b; };
409 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700410 case DType_INT32:
Kevin Chengaee1fac2020-11-11 13:54:06 -0800411 this->fcn = [this, shift](InEigenType a, InEigenType b) -> OutEigenType {
412 int64_t result;
413 if (shift > 0)
414 {
415 int64_t round = 1L << (shift - 1);
Kevin Cheng9257fd52021-04-14 15:55:31 -0700416 result = static_cast<int64_t>(a) * static_cast<int64_t>(b) + round;
Kevin Chengaee1fac2020-11-11 13:54:06 -0800417 result = result >> shift;
418
Kevin Chengacb550f2021-06-29 15:32:19 -0700419 REQUIRE(result >= QMin && result <= QMax, "OpMul: result %ld exceeds valid range [%ld, %ld]",
420 result, QMin, QMax);
Kevin Chengaee1fac2020-11-11 13:54:06 -0800421 }
422 else
423 {
Kevin Chengc42addc2021-09-28 15:41:57 -0700424 result = static_cast<int64_t>(a) * b;
Jeremy Johnson90347472021-09-06 12:04:07 +0100425 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::max());
426 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<InEigenType>::min());
427 REQUIRE(result <= i32_max_in_64 && result >= i32_min_in_64, "OpMul: result not in i32 range");
428 return static_cast<InEigenType>(result);
Kevin Chengaee1fac2020-11-11 13:54:06 -0800429 }
430
431 return static_cast<OutEigenType>(result);
432 };
Eric Kunzee5e26762020-10-13 16:11:07 -0700433 break;
434 case DType_INT8:
435 case DType_INT16:
436 this->fcn = [this](InEigenType lhs, InEigenType rhs) -> OutEigenType {
437 OutEigenType raw_output = (OutEigenType)lhs * (OutEigenType)rhs;
438
439 OutEigenType clamped_output = std::min<OutEigenType>(QMax, std::max<OutEigenType>(raw_output, QMin));
440
441 return clamped_output;
442 };
443 break;
444 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700445 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[InDtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700446 }
447
448 return 0;
449}
450
451template <int Rank, DType Dtype>
452int OpPow<Rank, Dtype>::register_fcn()
453{
454 switch (Dtype)
455 {
456 case DType_FLOAT:
457 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return powf(a, b); };
458 break;
459 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700460 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700461 }
462
463 return 0;
464}
465
466template <int Rank, DType Dtype>
467int OpSub<Rank, Dtype>::register_fcn()
468{
469 switch (InDtype)
470 {
Eric Kunzee5e26762020-10-13 16:11:07 -0700471 case DType_INT32:
Jeremy Johnson90347472021-09-06 12:04:07 +0100472 this->fcn = [this](InEigenType a, InEigenType b) -> OutEigenType {
473 int64_t res_in_64 = static_cast<int64_t>(a) - b;
474 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(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64, "OpSub: result not in i32 range");
477 return static_cast<InEigenType>(res_in_64);
478 };
479 break;
480 case DType_FLOAT:
Eric Kunzee5e26762020-10-13 16:11:07 -0700481 this->fcn = [](InEigenType a, InEigenType b) -> OutEigenType { return a - b; };
482 break;
483 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700484 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[InDtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700485 }
486
487 return 0;
488}
489
Kevin Cheng571f7182021-05-24 17:20:01 -0700490template <int Rank, DType InDtype>
Kevin Chengacb550f2021-06-29 15:32:19 -0700491OpTable<Rank, InDtype>::OpTable(SubgraphTraverser* sgt_,
492 TosaAttributeBase* attribute_,
493 TosaQuantInfoBase* qinfo_,
494 uint64_t id_)
495 : GraphNode(sgt_, Op_TABLE, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -0700496{
Kevin Chengfe392ce2021-10-18 21:51:55 +0000497 setRequiredOperands(1, 1);
Eric Kunzee5e26762020-10-13 16:11:07 -0700498 setRequiredRank(0, 6);
Kevin Chengfe392ce2021-10-18 21:51:55 +0000499
500 INIT_ATTRIBUTE(Table);
Eric Kunzee5e26762020-10-13 16:11:07 -0700501}
502
Kevin Cheng571f7182021-05-24 17:20:01 -0700503template <int Rank, DType InDtype>
504OpTable<Rank, InDtype>::~OpTable()
Eric Kunzee5e26762020-10-13 16:11:07 -0700505{}
506
Kevin Cheng571f7182021-05-24 17:20:01 -0700507template <int Rank, DType InDtype>
508int OpTable<Rank, InDtype>::checkTensorAttributes()
Eric Kunzee5e26762020-10-13 16:11:07 -0700509{
510 if (validateRequiredOperands())
511 return 1;
512
513 if (validateRequiredRank(inputs[0]) || validateRequiredRank(outputs[0]))
514 {
515 return 1;
516 }
517
Kevin Chengfe392ce2021-10-18 21:51:55 +0000518 ERROR_IF(inputs[0]->getDtype() != InDtype, "OpTable: Unexpected input type");
Kevin Chengf3e016f2021-11-02 01:15:50 +0000519 ERROR_IF(outputs[0]->getDtype() != OutDtype, "OpTable: Unexpected output type");
Kevin Chengfe392ce2021-10-18 21:51:55 +0000520 ERROR_IF(attribute->table().size() != TableNumEntries, "OpTable: table attribute size must be %u", TableNumEntries);
521
522 for (uint32_t i = 0; i < TableNumEntries; i++)
Eric Kunzee5e26762020-10-13 16:11:07 -0700523 {
Kevin Chengfe392ce2021-10-18 21:51:55 +0000524 table[i] = (TableEigenType)attribute->table()[i];
Eric Kunzee5e26762020-10-13 16:11:07 -0700525 }
526
Kevin Chengfe392ce2021-10-18 21:51:55 +0000527 in = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
528 out = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
Kevin Cheng571f7182021-05-24 17:20:01 -0700529
Kevin Chengfe392ce2021-10-18 21:51:55 +0000530 ASSERT_MEM(in && out);
Eric Kunzee5e26762020-10-13 16:11:07 -0700531
532 return 0;
533}
534
Kevin Cheng571f7182021-05-24 17:20:01 -0700535template <int Rank, DType InDtype>
536int OpTable<Rank, InDtype>::eval()
Eric Kunzee5e26762020-10-13 16:11:07 -0700537{
Kevin Cheng571f7182021-05-24 17:20:01 -0700538 switch (InDtype)
539 {
540 case DType_INT8:
541 this->out->getTensor() = this->in->getTensor().unaryExpr([this](InEigenType in) -> OutEigenType {
542 int32_t input_truncated = std::min<int32_t>(std::max<int32_t>(in, QInMin), QInMax);
543 int32_t index = input_truncated - QInMin;
Kevin Chengfe392ce2021-10-18 21:51:55 +0000544 int32_t value = table[index];
Eric Kunzee5e26762020-10-13 16:11:07 -0700545
Kevin Cheng571f7182021-05-24 17:20:01 -0700546 return value;
547 });
548 break;
549 case DType_INT16:
550 this->out->getTensor() = this->in->getTensor().unaryExpr([this](InEigenType in) -> OutEigenType {
551 // 1. make sure input is int16 range
552 int32_t input_truncated = std::min<int32_t>(std::max<int32_t>(in, QInMin), QInMax);
Eric Kunzee5e26762020-10-13 16:11:07 -0700553
Kevin Cheng571f7182021-05-24 17:20:01 -0700554 // 2. calculate index and interpolation fraction
555 int32_t index = (input_truncated >> FractionBits) + (1 << (IntegerBits - 1));
556 index = std::min<int32_t>(std::max<int32_t>(index, 0), NumTableEntries - 1); // 9-bit index
557 int32_t frac = (input_truncated)&0x7F; // 7-bit fraction
Eric Kunzee5e26762020-10-13 16:11:07 -0700558
Kevin Cheng571f7182021-05-24 17:20:01 -0700559 // 3. interpolate, generate 16.7 (23-bit) output
Kevin Chengfe392ce2021-10-18 21:51:55 +0000560 int32_t base = table[index];
561 int32_t next = table[index + 1];
Kevin Cheng571f7182021-05-24 17:20:01 -0700562 int32_t value = (base << 7) + (next - base) * frac;
563
564 return value;
565 });
566 break;
567 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700568 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[InDtype]);
Kevin Cheng571f7182021-05-24 17:20:01 -0700569 }
Eric Kunzee5e26762020-10-13 16:11:07 -0700570
571 return GraphNode::eval();
572}
573
574// template explicit instantiation
575DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAdd, FLOAT);
576DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAdd, INT32);
577
578DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpArithmeticRightShift, INT8);
579DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpArithmeticRightShift, INT16);
580DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpArithmeticRightShift, INT32);
581
Kevin Cheng3a478572021-01-22 17:21:02 -0800582DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseAnd, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700583DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseAnd, INT16);
584DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseAnd, INT32);
585
Kevin Cheng3a478572021-01-22 17:21:02 -0800586DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseOr, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700587DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseOr, INT16);
588DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseOr, INT32);
589
Kevin Cheng3a478572021-01-22 17:21:02 -0800590DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseXor, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700591DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseXor, INT16);
592DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseXor, INT32);
593
Matthew Haddon459443c2021-08-23 16:43:13 +0100594DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpIntdiv, INT32);
Kevin Cheng14d7f7a2021-05-12 10:44:49 -0700595
Eric Kunzee5e26762020-10-13 16:11:07 -0700596DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalAnd, BOOL);
597
598DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalLeftShift, INT8);
599DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalLeftShift, INT16);
600DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalLeftShift, INT32);
601
602DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalRightShift, INT8);
603DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalRightShift, INT16);
604DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalRightShift, INT32);
605
606DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalOr, BOOL);
607
608DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalXor, BOOL);
609
610DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMaximum, FLOAT);
611DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMaximum, INT32);
612
613DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMinimum, FLOAT);
614DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpMinimum, INT32);
615
616DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, FLOAT, FLOAT);
617DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, INT8, INT32);
618DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, INT16, INT32);
619DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(OpMul, INT32, INT32);
620
621DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpPow, FLOAT);
622
623DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSub, FLOAT);
624DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSub, INT32);
625
Kevin Cheng571f7182021-05-24 17:20:01 -0700626DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTable, INT8);
627DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTable, INT16);
Eric Kunzee5e26762020-10-13 16:11:07 -0700628
629DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNode, FLOAT, BOOL);
630DEF_INSTANTIATE_RANK0_6_ONE_RANK_TWO_TYPE(BinaryNode, INT32, BOOL);