| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef FUSED_ACTIVATION_H |
| 16 | #define FUSED_ACTIVATION_H |
| 17 | |
| 18 | #include "mat.h" |
| 19 | #include "layer_type.h" |
| 20 | |
| 21 | static NCNN_FORCEINLINE float activation_ss(float v, int activation_type, const ncnn::Mat& activation_params) |
| 22 | { |
| 23 | switch (activation_type) |
| 24 | { |
| 25 | case 1: |
| 26 | { |
| 27 | v = fmaxf(v, 0.f); |
| 28 | break; |
| 29 | } |
| 30 | case 2: |
| 31 | { |
| 32 | float slope = activation_params[0]; |
| 33 | v = v > 0.f ? v : v * slope; |
| 34 | break; |
| 35 | } |
| 36 | case 3: |
| 37 | { |
| 38 | float min = activation_params[0]; |
| 39 | float max = activation_params[1]; |
| 40 | if (v < min) |
| 41 | v = min; |
| 42 | if (v > max) |
| 43 | v = max; |
| 44 | break; |
| 45 | } |
| 46 | case 4: |
| 47 | { |
| 48 | v = std::min(v, 88.3762626647949f); |
| 49 | v = std::max(v, -88.3762626647949f); |
| 50 | v = 1.f / (1.f + expf(-v)); |
| 51 | break; |
| 52 | } |
| 53 | case 5: |
| 54 | { |
| 55 | v = v * tanhf(logf(expf(v) + 1.f)); |
| 56 | break; |
| 57 | } |
| 58 | case 6: |
| 59 | { |
| 60 | float alpha = activation_params[0]; |
| 61 | float beta = activation_params[1]; |
| 62 | float lower = -beta / alpha; |
| 63 | float upper = (1.f / alpha) + lower; |
| 64 | if (v < lower) |
| 65 | v = 0.f; |
| 66 | else if (v > upper) |
| 67 | ; |
| 68 | else |
| 69 | v = v * (v * alpha + beta); |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return v; |
| 75 | } |
| 76 | |
| 77 | static ncnn::Layer* create_activation_layer(int activation_type, const ncnn::Mat& activation_params, const ncnn::Option& opt) |
| 78 | { |
| 79 | ncnn::Layer* activation = 0; |
| 80 | |
| 81 | if (activation_type == 1) |
| 82 | { |
| 83 | activation = ncnn::create_layer_cpu(ncnn::LayerType::ReLU); |
| 84 | |
| 85 | ncnn::ParamDict pd; |
| 86 | activation->load_param(pd); |
| 87 | } |
| 88 | else if (activation_type == 2) |
| 89 | { |
| 90 | activation = ncnn::create_layer_cpu(ncnn::LayerType::ReLU); |
| 91 | |
| 92 | ncnn::ParamDict pd; |
| 93 | pd.set(0, activation_params[0]); // slope |
| 94 | activation->load_param(pd); |
| 95 | } |
| 96 | else if (activation_type == 3) |
| 97 | { |
| 98 | activation = ncnn::create_layer_cpu(ncnn::LayerType::Clip); |
| 99 | |
| 100 | ncnn::ParamDict pd; |
| 101 | pd.set(0, activation_params[0]); // min |
| 102 | pd.set(1, activation_params[1]); // max |
| 103 | |
| 104 | activation->load_param(pd); |
| 105 | } |
| 106 | else if (activation_type == 4) |
| 107 | { |
| 108 | activation = ncnn::create_layer_cpu(ncnn::LayerType::Sigmoid); |
| 109 | |
| 110 | ncnn::ParamDict pd; |
| 111 | activation->load_param(pd); |
| 112 | } |
| 113 | else if (activation_type == 5) |
| 114 | { |
| 115 | activation = ncnn::create_layer_cpu(ncnn::LayerType::Mish); |
| 116 | |
| 117 | ncnn::ParamDict pd; |
| 118 | activation->load_param(pd); |
| 119 | } |
| 120 | else if (activation_type == 6) |
| 121 | { |
| 122 | activation = ncnn::create_layer_cpu(ncnn::LayerType::HardSwish); |
| 123 | |
| 124 | ncnn::ParamDict pd; |
| 125 | pd.set(0, activation_params[0]); // alpha |
| 126 | pd.set(1, activation_params[1]); // beta |
| 127 | |
| 128 | activation->load_param(pd); |
| 129 | } |
| 130 | |
| 131 | if (activation) |
| 132 | { |
| 133 | activation->create_pipeline(opt); |
| 134 | } |
| 135 | |
| 136 | return activation; |
| 137 | } |
| 138 | |
| 139 | #endif // FUSED_ACTIVATION_H |