blob: 440f4e1cfaaa04194f053c3befe24775ed2b1647 [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 "activation_funcs.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
25template <int Rank, DType Dtype>
26int OpClamp<Rank, Dtype>::register_fcn()
27{
28
29 switch (Dtype)
30 {
31 case DType_FLOAT:
32 {
33 InEigenType min = (InEigenType)attribute->min_fp();
34 InEigenType max = (InEigenType)attribute->max_fp();
35 this->fcn = [min, max](InEigenType a) -> OutEigenType { return a <= min ? min : a >= max ? max : a; };
36 }
37 break;
Kevin Cheng3a478572021-01-22 17:21:02 -080038 case DType_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -070039 case DType_INT16:
40 {
41 InEigenType min = (InEigenType)attribute->min_int();
42 InEigenType max = (InEigenType)attribute->max_int();
43 this->fcn = [min, max](InEigenType a) -> OutEigenType { return a <= min ? min : a >= max ? max : a; };
44 }
45 break;
46 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070047 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070048 }
49
50 return 0;
51}
52
53template <int Rank, DType Dtype>
54int OpReluN<Rank, Dtype>::register_fcn()
55{
56
57 switch (Dtype)
58 {
59 case DType_FLOAT:
60 {
61 InEigenType N = (InEigenType)attribute->max_fp();
62 this->fcn = [N](InEigenType a) -> OutEigenType { return a >= 0 ? (a <= N ? a : N) : 0; };
63 }
64 break;
65 case DType_INT32:
66 {
67 InEigenType N = (InEigenType)attribute->max_int();
68 this->fcn = [N](InEigenType a) -> OutEigenType { return a >= 0 ? (a <= N ? a : N) : 0; };
69 }
70 break;
71 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070072 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070073 }
74
75 return 0;
76}
77
78template <int Rank, DType Dtype>
79int OpSigmoid<Rank, Dtype>::register_fcn()
80{
81 switch (Dtype)
82 {
83 case DType_FLOAT:
84 this->fcn = [](InEigenType a) -> OutEigenType { return (1.0 / (1.0 + (expf(-1.0 * a)))); };
85 break;
86 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070087 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070088 }
89
90 return 0;
91}
92
93template <int Rank, DType Dtype>
94int OpTanh<Rank, Dtype>::register_fcn()
95{
96 switch (Dtype)
97 {
98 case DType_FLOAT:
99 this->fcn = [](InEigenType a) -> OutEigenType { return tanhf(a); };
100 break;
101 default:
Kevin Chengacb550f2021-06-29 15:32:19 -0700102 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -0700103 }
104
105 return 0;
106}
107
108// template explicit instantiation
109DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, FLOAT);
Kevin Cheng3a478572021-01-22 17:21:02 -0800110DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700111DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT16);
112
113DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReluN, FLOAT);
114DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReluN, INT32);
115
116DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSigmoid, FLOAT);
117
118DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTanh, FLOAT);