blob: 61f7df6d61e8b53bffb5bddf7a922fd2aeea9769 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
James Ward8b390432022-08-12 20:48:56 +01002// Copyright (c) 2020-2022, 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{
Eric Kunzee5e26762020-10-13 16:11:07 -070028 switch (Dtype)
29 {
James Ward8b390432022-08-12 20:48:56 +010030 case DType_FP16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010031 case DType_FP32:
Eric Kunzee5e26762020-10-13 16:11:07 -070032 {
33 InEigenType min = (InEigenType)attribute->min_fp();
34 InEigenType max = (InEigenType)attribute->max_fp();
Kevin Chengcc61be32021-10-14 17:09:57 -070035 ERROR_IF(max < min, "OpClamp: max smaller than min");
36
37 this->fcn = [min, max](InEigenType a) -> OutEigenType { return a <= min ? min : a >= max ? max : a; };
Eric Kunzee5e26762020-10-13 16:11:07 -070038 }
39 break;
Kevin Cheng3a478572021-01-22 17:21:02 -080040 case DType_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -070041 case DType_INT16:
42 {
43 InEigenType min = (InEigenType)attribute->min_int();
44 InEigenType max = (InEigenType)attribute->max_int();
Kevin Chengcc61be32021-10-14 17:09:57 -070045 ERROR_IF(max < min, "OpClamp: max smaller than min");
46 this->fcn = [min, max](InEigenType a) -> OutEigenType { return a <= min ? min : a >= max ? max : a; };
Eric Kunzee5e26762020-10-13 16:11:07 -070047 }
48 break;
49 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070050 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070051 }
52
53 return 0;
54}
55
56template <int Rank, DType Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070057int OpSigmoid<Rank, Dtype>::register_fcn()
58{
59 switch (Dtype)
60 {
James Ward8b390432022-08-12 20:48:56 +010061 case DType_FP16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010062 case DType_FP32:
Eric Kunzee5e26762020-10-13 16:11:07 -070063 this->fcn = [](InEigenType a) -> OutEigenType { return (1.0 / (1.0 + (expf(-1.0 * a)))); };
64 break;
65 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070066 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070067 }
68
69 return 0;
70}
71
72template <int Rank, DType Dtype>
73int OpTanh<Rank, Dtype>::register_fcn()
74{
75 switch (Dtype)
76 {
James Ward8b390432022-08-12 20:48:56 +010077 case DType_FP16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010078 case DType_FP32:
Eric Kunzee5e26762020-10-13 16:11:07 -070079 this->fcn = [](InEigenType a) -> OutEigenType { return tanhf(a); };
80 break;
81 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070082 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070083 }
84
85 return 0;
86}
87
88// template explicit instantiation
James Ward8b390432022-08-12 20:48:56 +010089DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, FP16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010090DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, FP32);
Kevin Cheng3a478572021-01-22 17:21:02 -080091DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -070092DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT16);
93
James Ward8b390432022-08-12 20:48:56 +010094DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSigmoid, FP16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010095DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSigmoid, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -070096
James Ward8b390432022-08-12 20:48:56 +010097DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTanh, FP16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010098DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTanh, FP32);