blob: dc85088c65df88e9b3124e9f3b26eb8356e6a61b [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"
James Ward24dbc422022-10-19 12:20:31 +010019#include "arith_util.h"
Eric Kunzee5e26762020-10-13 16:11:07 -070020#include <cmath>
21
22using namespace TosaReference;
23using namespace Eigen;
24using namespace tosa;
25
26template <int Rank, DType Dtype>
27int OpClamp<Rank, Dtype>::register_fcn()
28{
Eric Kunzee5e26762020-10-13 16:11:07 -070029 switch (Dtype)
30 {
James Ward8b390432022-08-12 20:48:56 +010031 case DType_FP16:
James Ward24dbc422022-10-19 12:20:31 +010032 case DType_BF16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010033 case DType_FP32:
Eric Kunzee5e26762020-10-13 16:11:07 -070034 {
35 InEigenType min = (InEigenType)attribute->min_fp();
36 InEigenType max = (InEigenType)attribute->max_fp();
Kevin Chengcc61be32021-10-14 17:09:57 -070037 ERROR_IF(max < min, "OpClamp: max smaller than min");
38
James Ward24dbc422022-10-19 12:20:31 +010039 this->fcn = [min, max](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(a <= min ? min : a >= max ? max : a); };
Eric Kunzee5e26762020-10-13 16:11:07 -070040 }
41 break;
Kevin Cheng3a478572021-01-22 17:21:02 -080042 case DType_INT8:
Eric Kunzee5e26762020-10-13 16:11:07 -070043 case DType_INT16:
44 {
45 InEigenType min = (InEigenType)attribute->min_int();
46 InEigenType max = (InEigenType)attribute->max_int();
Kevin Chengcc61be32021-10-14 17:09:57 -070047 ERROR_IF(max < min, "OpClamp: max smaller than min");
48 this->fcn = [min, max](InEigenType a) -> OutEigenType { return a <= min ? min : a >= max ? max : a; };
Eric Kunzee5e26762020-10-13 16:11:07 -070049 }
50 break;
51 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070052 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070053 }
54
55 return 0;
56}
57
58template <int Rank, DType Dtype>
Jerry Gea6827492022-11-16 10:41:55 -080059OpClamp<Rank, Dtype>::~OpClamp()
60{
61 if (attribute) delete attribute;
62}
63
64template <int Rank, DType Dtype>
Eric Kunzee5e26762020-10-13 16:11:07 -070065int OpSigmoid<Rank, Dtype>::register_fcn()
66{
67 switch (Dtype)
68 {
James Ward8b390432022-08-12 20:48:56 +010069 case DType_FP16:
James Ward24dbc422022-10-19 12:20:31 +010070 case DType_BF16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010071 case DType_FP32:
Jerry Gea633bc62022-11-09 14:30:59 -080072 this->fcn = [](InEigenType a) -> OutEigenType {
73 return fpTrunc<Dtype>(1.f / (1.f + (expf(-1.f * a))));
74 };
Eric Kunzee5e26762020-10-13 16:11:07 -070075 break;
76 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070077 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070078 }
79
80 return 0;
81}
82
83template <int Rank, DType Dtype>
84int OpTanh<Rank, Dtype>::register_fcn()
85{
86 switch (Dtype)
87 {
James Ward8b390432022-08-12 20:48:56 +010088 case DType_FP16:
James Ward24dbc422022-10-19 12:20:31 +010089 case DType_BF16:
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +010090 case DType_FP32:
James Ward24dbc422022-10-19 12:20:31 +010091 this->fcn = [](InEigenType a) -> OutEigenType { return fpTrunc<Dtype>(tanhf(a)); };
Eric Kunzee5e26762020-10-13 16:11:07 -070092 break;
93 default:
Kevin Chengacb550f2021-06-29 15:32:19 -070094 ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]);
Eric Kunzee5e26762020-10-13 16:11:07 -070095 }
96
97 return 0;
98}
99
100// template explicit instantiation
James Ward8b390432022-08-12 20:48:56 +0100101DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, FP16);
James Ward24dbc422022-10-19 12:20:31 +0100102DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, BF16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100103DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, FP32);
Kevin Cheng3a478572021-01-22 17:21:02 -0800104DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700105DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT16);
106
James Ward24dbc422022-10-19 12:20:31 +0100107DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSigmoid, BF16);
James Ward8b390432022-08-12 20:48:56 +0100108DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSigmoid, FP16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100109DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSigmoid, FP32);
Eric Kunzee5e26762020-10-13 16:11:07 -0700110
James Ward24dbc422022-10-19 12:20:31 +0100111DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTanh, BF16);
James Ward8b390432022-08-12 20:48:56 +0100112DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTanh, FP16);
Jeremy Johnsonbc2a3db2022-09-27 13:50:00 +0100113DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTanh, FP32);