blob: 21ee27695be01608bdb9153352f747077cddbc01 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Tai Lya4d748b2023-03-28 22:06:56 +00002// Copyright (c) 2020-2023, 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#ifndef OPS_EWISE_UNARY_H
17#define OPS_EWISE_UNARY_H
18
19#include "graph_node.h"
20
21using namespace tosa;
22
23namespace TosaReference
24{
Tai Lya4d748b2023-03-28 22:06:56 +000025template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070026class UnaryNode : public GraphNode
27{
28public:
Kevin Chengacb550f2021-06-29 15:32:19 -070029 UnaryNode(SubgraphTraverser* sgt_, const Op& nodeType, const uint64_t id_);
Eric Kunzee5e26762020-10-13 16:11:07 -070030 virtual ~UnaryNode();
31
32 virtual int checkTensorAttributes() final;
33 virtual int eval() final;
34 virtual int register_fcn() = 0;
35
36 using InEigenType = typename GetEigenType<Dtype>::type;
37 using OutEigenType = typename GetEigenType<Dtype>::type;
38 using TIn = Eigen::Tensor<InEigenType, Rank>;
39 using TOut = Eigen::Tensor<OutEigenType, Rank>;
40
41protected:
42 std::function<OutEigenType(InEigenType)> fcn;
43 TosaReference::TensorTemplate<TIn>* a;
44 TosaReference::TensorTemplate<TOut>* result;
45};
46
47#define DEF_TEMPLATE_UNARY_OP(Opname, OPNAME) \
Tai Lya4d748b2023-03-28 22:06:56 +000048 template <int Rank, TOSA_REF_TYPE Dtype> \
Eric Kunzee5e26762020-10-13 16:11:07 -070049 class Op##Opname : public UnaryNode<Rank, Dtype> \
50 { \
51 public: \
Tai Lya4d748b2023-03-28 22:06:56 +000052 Op##Opname(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_) \
Kevin Chengacb550f2021-06-29 15:32:19 -070053 : UnaryNode<Rank, Dtype>(sgt_, Op_##OPNAME, id_) \
Eric Kunzee5e26762020-10-13 16:11:07 -070054 { \
55 register_fcn(); \
56 } \
57 static constexpr int32_t QMin = GetQMin<Dtype>::value; \
58 static constexpr int32_t QMax = GetQMax<Dtype>::value; \
59 using InEigenType = typename GetEigenType<Dtype>::type; \
60 using OutEigenType = typename GetEigenType<Dtype>::type; \
61 virtual int register_fcn(); \
62 };
63
Eric Kunzee5e26762020-10-13 16:11:07 -070064DEF_TEMPLATE_UNARY_OP(Abs, ABS)
65DEF_TEMPLATE_UNARY_OP(BitwiseNot, BITWISE_NOT)
66DEF_TEMPLATE_UNARY_OP(Ceil, CEIL)
67DEF_TEMPLATE_UNARY_OP(Clz, CLZ)
68DEF_TEMPLATE_UNARY_OP(Exp, EXP)
69DEF_TEMPLATE_UNARY_OP(Floor, FLOOR)
70DEF_TEMPLATE_UNARY_OP(Log, LOG)
71DEF_TEMPLATE_UNARY_OP(LogicalNot, LOGICAL_NOT)
Eric Kunzee5e26762020-10-13 16:11:07 -070072DEF_TEMPLATE_UNARY_OP(Reciprocal, RECIPROCAL)
73DEF_TEMPLATE_UNARY_OP(Rsqrt, RSQRT)
74
75#undef DEF_TEMPLATE_UNARY_OP
Eric Kunzeb5fabec2022-06-07 05:20:44 +000076
77// Negate is the only unary op with attributes
Tai Lya4d748b2023-03-28 22:06:56 +000078template <int Rank, TOSA_REF_TYPE Dtype>
Eric Kunzeb5fabec2022-06-07 05:20:44 +000079class OpNegate : public UnaryNode<Rank, Dtype>
80{
81public:
82 OpNegate(SubgraphTraverser* sgt_, TosaAttributeBase* attribute_, uint64_t id_);
83 virtual ~OpNegate();
84
85 static constexpr int32_t QMin = GetQMin<Dtype>::value;
86 static constexpr int32_t QMax = GetQMax<Dtype>::value;
87 using InEigenType = typename GetEigenType<Dtype>::type;
88 using OutEigenType = typename GetEigenType<Dtype>::type;
89 virtual int register_fcn();
90
91protected:
92 tosa::TosaNegateAttribute* attribute;
93};
Eric Kunzee5e26762020-10-13 16:11:07 -070094
95}; // namespace TosaReference
96
97#endif