blob: 0db3cfb37bb5266eb013c76d8bee7819f2b562d1 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
2// Copyright (c) 2020, ARM Limited.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef OPS_EWISE_UNARY_H
17#define OPS_EWISE_UNARY_H
18
19#include "graph_node.h"
20
21using namespace tosa;
22
23namespace TosaReference
24{
25template <int Rank, DType Dtype>
26class UnaryNode : public GraphNode
27{
28public:
29 UnaryNode(const Op& nodeType, const uint64_t id_);
30 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) \
48 template <int Rank, DType Dtype> \
49 class Op##Opname : public UnaryNode<Rank, Dtype> \
50 { \
51 public: \
52 Op##Opname(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_) \
53 : UnaryNode<Rank, Dtype>(Op_##OPNAME, id_) \
54 { \
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
64#define DEF_TEMPLATE_UNARY_OP_WITH_QUANT_INFO(Opname, OPNAME) \
65 template <int Rank, DType Dtype> \
66 class Op##Opname : public UnaryNode<Rank, Dtype> \
67 { \
68 public: \
69 Op##Opname(TosaAttributeBase* attribute_, TosaQuantInfoBase* qinfo_, uint64_t id_) \
70 : UnaryNode<Rank, Dtype>(Op_##OPNAME, id_) \
71 { \
72 INIT_QINFO(Unary); \
73 register_fcn(); \
74 } \
75 static constexpr int32_t QMin = GetQMin<Dtype>::value; \
76 static constexpr int32_t QMax = GetQMax<Dtype>::value; \
77 using InEigenType = typename GetEigenType<Dtype>::type; \
78 using OutEigenType = typename GetEigenType<Dtype>::type; \
79 virtual int register_fcn(); \
80 \
81 protected: \
82 TosaUnaryQuantInfo* qinfo; \
83 };
84
85DEF_TEMPLATE_UNARY_OP(Abs, ABS)
86DEF_TEMPLATE_UNARY_OP(BitwiseNot, BITWISE_NOT)
87DEF_TEMPLATE_UNARY_OP(Ceil, CEIL)
88DEF_TEMPLATE_UNARY_OP(Clz, CLZ)
89DEF_TEMPLATE_UNARY_OP(Exp, EXP)
90DEF_TEMPLATE_UNARY_OP(Floor, FLOOR)
91DEF_TEMPLATE_UNARY_OP(Log, LOG)
92DEF_TEMPLATE_UNARY_OP(LogicalNot, LOGICAL_NOT)
93DEF_TEMPLATE_UNARY_OP_WITH_QUANT_INFO(Negate, NEGATE)
94DEF_TEMPLATE_UNARY_OP(Reciprocal, RECIPROCAL)
95DEF_TEMPLATE_UNARY_OP(Rsqrt, RSQRT)
96
97#undef DEF_TEMPLATE_UNARY_OP
98#undef DEF_TEMPLATE_UNARY_OP_WITH_QUANT_INFO
99
100}; // namespace TosaReference
101
102#endif