blob: 6818f8c822e6bfa02e8e1f0cfafaea7f01b269c0 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Jerry Ge51bd4f52024-02-20 11:21:19 -08002// 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_unary.h"
17#include "quant_util.h"
18#include "template_types.h"
19#include <cmath>
20
21using namespace TosaReference;
22using namespace Eigen;
23using namespace tosa;
24
Tai Lya4d748b2023-03-28 22:06:56 +000025template <int Rank, TOSA_REF_TYPE Dtype>
Kevin Chengacb550f2021-06-29 15:32:19 -070026UnaryNode<Rank, Dtype>::UnaryNode(SubgraphTraverser* sgt_, const Op& op_, uint64_t id_)
27 : GraphNode(sgt_, op_, id_)
Eric Kunzee5e26762020-10-13 16:11:07 -070028{
29 setRequiredOperands(1, 1);
Eric Kunzee5e26762020-10-13 16:11:07 -070030
Eric Kunzeb5fabec2022-06-07 05:20:44 +000031 fcn = [](InEigenType a) -> OutEigenType {
32 ASSERT_MSG(0, "In default UnaryNode function, missing function registration");
33 return OutEigenType();
34 };
Eric Kunzee5e26762020-10-13 16:11:07 -070035}
36
Tai Lya4d748b2023-03-28 22:06:56 +000037template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070038UnaryNode<Rank, Dtype>::~UnaryNode()
39{}
40
Tai Lya4d748b2023-03-28 22:06:56 +000041template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070042int UnaryNode<Rank, Dtype>::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
Eric Kunzee5e26762020-10-13 16:11:07 -070051 // output and input must be the same types
Kevin Chengc72b59c2021-09-29 16:57:55 -070052 if (inputs[0]->matchRankTypeShape(*outputs[0]))
Eric Kunzee5e26762020-10-13 16:11:07 -070053 {
Kevin Chengc72b59c2021-09-29 16:57:55 -070054 printNodeValidationError("UnaryNode: input and output rank/type/shape must match");
Eric Kunzee5e26762020-10-13 16:11:07 -070055 return 1;
56 }
57
58 a = dynamic_cast<TosaReference::TensorTemplate<TIn>*>(inputs[0]);
59 result = dynamic_cast<TosaReference::TensorTemplate<TOut>*>(outputs[0]);
60
61 ASSERT_MEM(a && result);
62
63 return 0;
64}
65
Tai Lya4d748b2023-03-28 22:06:56 +000066template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070067int UnaryNode<Rank, Dtype>::eval()
68{
Tai Ly5d0e9c72024-04-05 01:19:31 +000069 // call register_fcn() here to ensure inputs/outputs have been connected
70 // to the node by the time register_fcn() is called for Clamp Operator
71 if (register_fcn())
72 {
73 return 1;
74 }
75
Eric Kunzee5e26762020-10-13 16:11:07 -070076 this->result->getTensor() = this->a->getTensor().unaryExpr(this->fcn);
77
78 return GraphNode::eval();
79}
80
Tai Lya4d748b2023-03-28 22:06:56 +000081template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070082int OpAbs<Rank, Dtype>::register_fcn()
83{
84 switch (Dtype)
85 {
Tai Lya4d748b2023-03-28 22:06:56 +000086 case TOSA_REF_TYPE_FP32: // No fpTrunc for FP32 as it is a no-op
87 case TOSA_REF_TYPE_FP64:
88 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -070089 this->fcn = [](InEigenType a) -> OutEigenType { return a > (InEigenType)0 ? a : (-a); };
90 break;
Tai Lya4d748b2023-03-28 22:06:56 +000091 case TOSA_REF_TYPE_FP16:
92 case TOSA_REF_TYPE_BF16:
James Ward24dbc422022-10-19 12:20:31 +010093 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(a > (InEigenType)0 ? a : (-a)); };
94 break;
Eric Kunzee5e26762020-10-13 16:11:07 -070095 default:
Tai Lya4d748b2023-03-28 22:06:56 +000096 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -070097 }
98
99 return 0;
100}
101
Tai Lya4d748b2023-03-28 22:06:56 +0000102template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700103int OpBitwiseNot<Rank, Dtype>::register_fcn()
104{
105 switch (Dtype)
106 {
Tai Lya4d748b2023-03-28 22:06:56 +0000107 case TOSA_REF_TYPE_INT8:
108 case TOSA_REF_TYPE_INT16:
109 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700110 this->fcn = [](InEigenType a) -> OutEigenType { return ~a; };
111 break;
112 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000113 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700114 }
115
116 return 0;
117}
118
Tai Lya4d748b2023-03-28 22:06:56 +0000119template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700120int OpCeil<Rank, Dtype>::register_fcn()
121{
122 switch (Dtype)
123 {
Tai Lya4d748b2023-03-28 22:06:56 +0000124 case TOSA_REF_TYPE_FP16:
125 case TOSA_REF_TYPE_BF16:
126 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100127 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(ceilf(a)); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700128 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000129 case TOSA_REF_TYPE_FP64:
130 this->fcn = [](InEigenType a) -> OutEigenType { return ceil(a); };
131 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700132 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000133 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700134 }
135
136 return 0;
137}
138
Tai Lya4d748b2023-03-28 22:06:56 +0000139template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700140int OpClz<Rank, Dtype>::register_fcn()
141{
142 int32_t num_bits;
143 switch (Dtype)
144 {
Tai Lya4d748b2023-03-28 22:06:56 +0000145 case TOSA_REF_TYPE_INT32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700146 num_bits = 32;
147 break;
148 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000149 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700150 }
151
152 this->fcn = [num_bits](int32_t a) -> int32_t {
153 int32_t leading_zeros = 0;
154 for (int bit = num_bits - 1; bit >= 0; bit--)
155 {
156 if (((a >> bit) & 0x1) == 0)
157 {
158 leading_zeros++;
159 }
160 else
161 {
162 break;
163 }
164 }
165 return leading_zeros;
166 };
167
168 return 0;
169}
170
Tai Lya4d748b2023-03-28 22:06:56 +0000171template <int Rank, TOSA_REF_TYPE Dtype>
Jerry Ge51bd4f52024-02-20 11:21:19 -0800172int OpCos<Rank, Dtype>::register_fcn()
173{
174 switch (Dtype)
175 {
176 case TOSA_REF_TYPE_FP16:
177 case TOSA_REF_TYPE_BF16:
178 case TOSA_REF_TYPE_FP32:
179 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(cos(a)); };
180 break;
181 case TOSA_REF_TYPE_FP64:
182 if (g_func_config.abs_mode)
183 {
Jeremy Johnson1eb14552024-04-11 16:21:54 +0100184 // ABS_ERROR bounds return
185 this->fcn = [](InEigenType a) -> OutEigenType { return a; };
Jerry Ge51bd4f52024-02-20 11:21:19 -0800186 }
187 else
188 {
189 this->fcn = [](InEigenType a) -> OutEigenType { return cos(a); };
190 };
191 break;
192 default:
193 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
194 }
195
196 return 0;
197}
198
199template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700200int OpExp<Rank, Dtype>::register_fcn()
201{
202 switch (Dtype)
203 {
Tai Lya4d748b2023-03-28 22:06:56 +0000204 case TOSA_REF_TYPE_FP16:
205 case TOSA_REF_TYPE_BF16:
206 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100207 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(expf(a)); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700208 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000209 case TOSA_REF_TYPE_FP64:
Jeremy Johnson9a758382023-11-07 16:27:35 +0000210 if (g_func_config.abs_mode)
211 {
212 // ABS_ERROR bounds return (1+abs(a))
213 this->fcn = [](InEigenType a) -> OutEigenType { return 1.0 + (a > (InEigenType)0 ? a : (-a)); };
214 }
215 else
216 {
217 this->fcn = [](InEigenType a) -> OutEigenType { return exp(a); };
218 }
Tai Lya4d748b2023-03-28 22:06:56 +0000219 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700220 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000221 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700222 }
223
224 return 0;
225}
226
Tai Lya4d748b2023-03-28 22:06:56 +0000227template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700228int OpFloor<Rank, Dtype>::register_fcn()
229{
230 switch (Dtype)
231 {
Tai Lya4d748b2023-03-28 22:06:56 +0000232 case TOSA_REF_TYPE_FP16:
233 case TOSA_REF_TYPE_BF16:
234 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100235 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(floorf(a)); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700236 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000237 case TOSA_REF_TYPE_FP64:
238 this->fcn = [](InEigenType a) -> OutEigenType { return floor(a); };
239 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700240 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000241 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700242 }
243
244 return 0;
245}
246
Tai Lya4d748b2023-03-28 22:06:56 +0000247template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700248int OpLog<Rank, Dtype>::register_fcn()
249{
250 switch (Dtype)
251 {
Tai Lya4d748b2023-03-28 22:06:56 +0000252 case TOSA_REF_TYPE_FP16:
253 case TOSA_REF_TYPE_BF16:
254 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100255 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(logf(a)); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700256 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000257 case TOSA_REF_TYPE_FP64:
258 this->fcn = [](InEigenType a) -> OutEigenType { return log(a); };
259 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700260 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000261 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700262 }
263
264 return 0;
265}
266
Tai Lya4d748b2023-03-28 22:06:56 +0000267template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700268int OpLogicalNot<Rank, Dtype>::register_fcn()
269{
270 switch (Dtype)
271 {
Tai Lya4d748b2023-03-28 22:06:56 +0000272 case TOSA_REF_TYPE_BOOL:
Eric Kunzee5e26762020-10-13 16:11:07 -0700273 this->fcn = [](InEigenType a) -> OutEigenType { return !a; };
274 break;
275 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000276 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700277 }
278
279 return 0;
280}
281
Tai Lya4d748b2023-03-28 22:06:56 +0000282template <int Rank, TOSA_REF_TYPE Dtype>
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000283OpNegate<Rank, Dtype>::OpNegate(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_)
Eric Kunzeb5fabec2022-06-07 05:20:44 +0000284 : UnaryNode<Rank, Dtype>(sgt_, Op_NEGATE, id_)
285{
286 INIT_ATTRIBUTE(Negate);
287
288 register_fcn();
289}
290
Tai Lya4d748b2023-03-28 22:06:56 +0000291template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzeb5fabec2022-06-07 05:20:44 +0000292OpNegate<Rank, Dtype>::~OpNegate()
293{
294 if (attribute)
295 delete attribute;
296}
297
Tai Lya4d748b2023-03-28 22:06:56 +0000298template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700299int OpNegate<Rank, Dtype>::register_fcn()
300{
Tai Lya4d748b2023-03-28 22:06:56 +0000301 ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->input1_zp() != 0, "OpNegate: zeropoint only for int8_t");
302 ERROR_IF(Dtype != TOSA_REF_TYPE_INT8 && attribute->output_zp() != 0, "OpNegate: zeropoint only for int8_t");
Kevin Chengacb550f2021-06-29 15:32:19 -0700303
Eric Kunzee5e26762020-10-13 16:11:07 -0700304 switch (Dtype)
305 {
Tai Lya4d748b2023-03-28 22:06:56 +0000306 case TOSA_REF_TYPE_FP16:
307 case TOSA_REF_TYPE_BF16:
308 case TOSA_REF_TYPE_FP32:
Eric Kunzee5e26762020-10-13 16:11:07 -0700309 this->fcn = [](InEigenType a) -> OutEigenType {
310 InEigenType result = -(a);
James Ward24dbc422022-10-19 12:20:31 +0100311 return fpTrunc<Dtype>(result);
Eric Kunzee5e26762020-10-13 16:11:07 -0700312 };
313 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000314 case TOSA_REF_TYPE_FP64:
315 this->fcn = [](InEigenType a) -> OutEigenType {
316 OutEigenType result = -(a);
317 return result;
318 };
319 break;
320 case TOSA_REF_TYPE_INT16:
321 case TOSA_REF_TYPE_INT32:
Jeremy Johnson81ee53d2022-03-23 15:32:34 +0000322 this->fcn = [this](InEigenType a) -> OutEigenType {
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000323 int64_t res_in_64 = 0L - a;
Jeremy Johnson0e463642022-05-03 12:10:23 +0100324 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
325 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::min());
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000326 REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64,
327 "OpNegate: result not in acc type range (int32)");
Jeremy Johnson0e463642022-05-03 12:10:23 +0100328
329 int64_t max_clip_in_64, min_clip_in_64;
Tai Lya4d748b2023-03-28 22:06:56 +0000330 if (Dtype == TOSA_REF_TYPE_INT16)
Jeremy Johnson0e463642022-05-03 12:10:23 +0100331 {
332 max_clip_in_64 = static_cast<int64_t>(std::numeric_limits<int16_t>::max());
333 min_clip_in_64 = static_cast<int64_t>(std::numeric_limits<int16_t>::min());
Jeremy Johnson81ee53d2022-03-23 15:32:34 +0000334 }
335 else
336 {
Jeremy Johnson0e463642022-05-03 12:10:23 +0100337 max_clip_in_64 = i32_max_in_64;
338 min_clip_in_64 = i32_min_in_64;
Jeremy Johnson81ee53d2022-03-23 15:32:34 +0000339 }
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000340 return static_cast<InEigenType>(
341 std::min<int64_t>(max_clip_in_64, std::max<int64_t>(min_clip_in_64, res_in_64)));
Eric Kunzee5e26762020-10-13 16:11:07 -0700342 };
343 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000344 case TOSA_REF_TYPE_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -0700345 this->fcn = [this](InEigenType a) -> OutEigenType {
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000346 int64_t res_in_64 = 0 - (a - attribute->input1_zp());
Jeremy Johnson0e463642022-05-03 12:10:23 +0100347 int64_t i32_max_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::max());
348 int64_t i32_min_in_64 = static_cast<int64_t>(std::numeric_limits<int32_t>::min());
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000349 REQUIRE(res_in_64 <= i32_max_in_64 && res_in_64 >= i32_min_in_64,
350 "OpNegate: result not in acc type range (int32)");
Eric Kunzeb5fabec2022-06-07 05:20:44 +0000351 res_in_64 += attribute->output_zp();
Jerry Ge9c9c8da2023-07-19 23:08:16 +0000352 InEigenType result = static_cast<InEigenType>(
353 std::min(std::max(res_in_64, static_cast<int64_t>(QMin)), static_cast<int64_t>(QMax)));
Eric Kunzee5e26762020-10-13 16:11:07 -0700354 return result;
355 };
356 break;
357 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000358 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700359 }
360
361 return 0;
362}
363
Tai Lya4d748b2023-03-28 22:06:56 +0000364template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700365int OpReciprocal<Rank, Dtype>::register_fcn()
366{
367 switch (Dtype)
368 {
Tai Lya4d748b2023-03-28 22:06:56 +0000369 case TOSA_REF_TYPE_FP16:
370 case TOSA_REF_TYPE_BF16:
371 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100372 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(1.0 / a); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700373 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000374 case TOSA_REF_TYPE_FP64:
375 this->fcn = [](InEigenType a) -> OutEigenType { return (1.0L / a); };
376 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700377 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000378 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700379 }
380
381 return 0;
382}
383
Tai Lya4d748b2023-03-28 22:06:56 +0000384template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -0700385int OpRsqrt<Rank, Dtype>::register_fcn()
386{
387 switch (Dtype)
388 {
Tai Lya4d748b2023-03-28 22:06:56 +0000389 case TOSA_REF_TYPE_FP16:
390 case TOSA_REF_TYPE_BF16:
391 case TOSA_REF_TYPE_FP32:
James Ward24dbc422022-10-19 12:20:31 +0100392 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(1.0 / sqrtf(a)); };
Eric Kunzee5e26762020-10-13 16:11:07 -0700393 break;
Tai Lya4d748b2023-03-28 22:06:56 +0000394 case TOSA_REF_TYPE_FP64:
395 this->fcn = [](InEigenType a) -> OutEigenType { return (1.0L / sqrt(a)); };
396 break;
Eric Kunzee5e26762020-10-13 16:11:07 -0700397 default:
Tai Lya4d748b2023-03-28 22:06:56 +0000398 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
Eric Kunzee5e26762020-10-13 16:11:07 -0700399 }
400
401 return 0;
402}
403
Jerry Ge51bd4f52024-02-20 11:21:19 -0800404template <int Rank, TOSA_REF_TYPE Dtype>
405int OpSin<Rank, Dtype>::register_fcn()
406{
407 switch (Dtype)
408 {
409 case TOSA_REF_TYPE_FP16:
410 case TOSA_REF_TYPE_BF16:
411 case TOSA_REF_TYPE_FP32:
412 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(sin(a)); };
413 break;
414 case TOSA_REF_TYPE_FP64:
415 if (g_func_config.abs_mode)
416 {
Jeremy Johnson1eb14552024-04-11 16:21:54 +0100417 // ABS_ERROR bounds return
418 this->fcn = [](InEigenType a) -> OutEigenType { return a; };
Jerry Ge51bd4f52024-02-20 11:21:19 -0800419 }
420 else
421 {
422 this->fcn = [](InEigenType a) -> OutEigenType { return sin(a); };
423 };
424 break;
425 default:
426 ERROR_IF(true, "unsupported TOSA_REF_TYPE %s", EnumNameTOSAREFTYPE(Dtype));
427 }
428
429 return 0;
430}
431
Eric Kunzee5e26762020-10-13 16:11:07 -0700432// template explicit instantiation
Jared Smolens98c281f2022-12-20 15:09:25 -0800433DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, BOOL);
434DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, FP16);
435DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, BF16);
436DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, FP32);
437DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, INT8);
438DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, INT16);
439DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000440DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(UnaryNode, FP64);
Jared Smolens98c281f2022-12-20 15:09:25 -0800441
James Ward8b390432022-08-12 20:48:56 +0100442DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAbs, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100443DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAbs, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100444DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAbs, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700445DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAbs, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000446DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpAbs, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700447
Kevin Cheng3a478572021-01-22 17:21:02 -0800448DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseNot, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700449DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseNot, INT16);
450DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpBitwiseNot, INT32);
451
James Ward8b390432022-08-12 20:48:56 +0100452DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCeil, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100453DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCeil, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100454DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCeil, FP32);
Tai Lya4d748b2023-03-28 22:06:56 +0000455DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCeil, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700456
457DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClz, INT32);
458
Jerry Ge51bd4f52024-02-20 11:21:19 -0800459DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCos, FP16);
460DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCos, BF16);
461DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCos, FP32);
462DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpCos, FP64);
463
James Ward8b390432022-08-12 20:48:56 +0100464DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpExp, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100465DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpExp, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100466DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpExp, FP32);
Tai Lya4d748b2023-03-28 22:06:56 +0000467DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpExp, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700468
James Ward8b390432022-08-12 20:48:56 +0100469DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpFloor, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100470DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpFloor, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100471DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpFloor, FP32);
Tai Lya4d748b2023-03-28 22:06:56 +0000472DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpFloor, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700473
James Ward8b390432022-08-12 20:48:56 +0100474DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLog, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100475DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLog, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100476DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLog, FP32);
Tai Lya4d748b2023-03-28 22:06:56 +0000477DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLog, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700478
479DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpLogicalNot, BOOL);
480
James Ward8b390432022-08-12 20:48:56 +0100481DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100482DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100483DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, FP32);
Kevin Cheng3a478572021-01-22 17:21:02 -0800484DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700485DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, INT16);
486DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, INT32);
Tai Lya4d748b2023-03-28 22:06:56 +0000487DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpNegate, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700488
James Ward8b390432022-08-12 20:48:56 +0100489DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpRsqrt, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100490DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpRsqrt, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100491DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpRsqrt, FP32);
Tai Lya4d748b2023-03-28 22:06:56 +0000492DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpRsqrt, FP64);
Eric Kunzee5e26762020-10-13 16:11:07 -0700493
Jerry Ge51bd4f52024-02-20 11:21:19 -0800494DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSin, FP16);
495DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSin, BF16);
496DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSin, FP32);
497DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSin, FP64);
498
James Ward8b390432022-08-12 20:48:56 +0100499DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReciprocal, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100500DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReciprocal, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100501DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReciprocal, FP32);
Tai Lya4d748b2023-03-28 22:06:56 +0000502DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReciprocal, FP64);