Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 2 | // Copyright (c) 2020-2021, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 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 | #include "activation_funcs.h" |
| 17 | #include "quant_util.h" |
| 18 | #include "template_types.h" |
| 19 | #include <cmath> |
| 20 | |
| 21 | using namespace TosaReference; |
| 22 | using namespace Eigen; |
| 23 | using namespace tosa; |
| 24 | |
| 25 | template <int Rank, DType Dtype> |
| 26 | int 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 Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 38 | case DType_INT8: |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 39 | 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 Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame^] | 47 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | template <int Rank, DType Dtype> |
| 54 | int 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 Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame^] | 72 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | template <int Rank, DType Dtype> |
| 79 | int 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 Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame^] | 87 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | template <int Rank, DType Dtype> |
| 94 | int 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 Cheng | acb550f | 2021-06-29 15:32:19 -0700 | [diff] [blame^] | 102 | ERROR_IF(true, "unsupported DType %s", EnumNamesDType()[Dtype]); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | // template explicit instantiation |
| 109 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, FLOAT); |
Kevin Cheng | 3a47857 | 2021-01-22 17:21:02 -0800 | [diff] [blame] | 110 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT8); |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 111 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpClamp, INT16); |
| 112 | |
| 113 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReluN, FLOAT); |
| 114 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpReluN, INT32); |
| 115 | |
| 116 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpSigmoid, FLOAT); |
| 117 | |
| 118 | DEF_INSTANTIATE_RANK0_6_ONE_RANK_ONE_TYPE(OpTanh, FLOAT); |