Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H |
| 25 | #define ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 26 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 27 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 29 | #include "src/core/NEON/wrapper/wrapper.h" |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace detail |
| 34 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 35 | /** Dummy activation object */ |
| 36 | template <typename T, int S> |
| 37 | struct dummy |
| 38 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 39 | /** SIMD vector type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 40 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 41 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 42 | /** Construct a dummy activation object. |
| 43 | * |
| 44 | * @param[in] act_info Activation layer information. |
| 45 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 46 | explicit dummy(ActivationLayerInfo act_info) |
| 47 | { |
| 48 | ARM_COMPUTE_UNUSED(act_info); |
| 49 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 50 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 51 | /** Run activation function. |
| 52 | * |
| 53 | * @param[in] vval Vector of values. |
| 54 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 55 | void operator()(ExactType &vval) |
| 56 | { |
| 57 | ARM_COMPUTE_UNUSED(vval); |
| 58 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 59 | |
| 60 | /** Run activation function. |
| 61 | * |
| 62 | * @param[in] val Scalar value. |
| 63 | */ |
| 64 | void operator()(T &val) |
| 65 | { |
| 66 | ARM_COMPUTE_UNUSED(val); |
| 67 | } |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 68 | }; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 69 | /** Linear activation object */ |
| 70 | template <typename T, int S> |
| 71 | struct linear |
| 72 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 73 | /** SIMD vector type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 74 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 75 | /** SIMD vector tag type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 76 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 77 | |
| 78 | /** Construct a Linear activation object. |
| 79 | * |
| 80 | * @param[in] act_info Activation layer information. |
| 81 | */ |
| 82 | explicit linear(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 83 | : alpha(act_info.a()), |
| 84 | beta(act_info.b()), |
| 85 | valpha(wrapper::vdup_n(static_cast<T>(alpha), ExactTagType{})), |
| 86 | vbeta(wrapper::vdup_n(static_cast<T>(beta), ExactTagType{})) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 87 | { |
| 88 | } |
| 89 | |
| 90 | /** Run activation function. |
| 91 | * |
| 92 | * @param[in] vval Vector of values. |
| 93 | */ |
| 94 | void operator()(ExactType &vval) |
| 95 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 96 | vval = wrapper::vmla(vbeta, vval, valpha); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 99 | /** Run activation function. |
| 100 | * |
| 101 | * @param[in] val Scalar value. |
| 102 | */ |
| 103 | void operator()(T &val) |
| 104 | { |
| 105 | val = alpha * val + beta; |
| 106 | } |
| 107 | |
| 108 | const T alpha; /**< Scalar alpha */ |
| 109 | const T beta; /**< Scalar alpha */ |
| 110 | const ExactType valpha; /**< Vector of alphas. */ |
| 111 | const ExactType vbeta; /**< Vector of betas. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 112 | }; |
| 113 | /** Square activation object */ |
| 114 | template <typename T, int S> |
| 115 | struct square |
| 116 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 117 | /** SIMD vector type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 118 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 119 | /** SIMD vector tag type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 120 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 121 | |
| 122 | /** Construct a Square activation object. |
| 123 | * |
| 124 | * @param[in] act_info Activation layer information. |
| 125 | */ |
| 126 | explicit square(ActivationLayerInfo act_info) |
| 127 | { |
| 128 | ARM_COMPUTE_UNUSED(act_info); |
| 129 | } |
| 130 | |
| 131 | /** Run activation function. |
| 132 | * |
| 133 | * @param[in] vval Vector of values. |
| 134 | */ |
| 135 | void operator()(ExactType &vval) |
| 136 | { |
| 137 | vval = wrapper::vmul(vval, vval); |
| 138 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 139 | |
| 140 | /** Run activation function. |
| 141 | * |
| 142 | * @param[in] val Scalar value. |
| 143 | */ |
| 144 | void operator()(T &val) |
| 145 | { |
| 146 | val = val * val; |
| 147 | } |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 148 | }; |
| 149 | /** Logistic activation object */ |
| 150 | template <typename T, int S> |
| 151 | struct logistic |
| 152 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 153 | /** SIMD vector type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 154 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 155 | /** SIMD vector tag type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 156 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 157 | |
| 158 | /** Construct a Logistic activation object. |
| 159 | * |
| 160 | * @param[in] act_info Activation layer information. |
| 161 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 162 | explicit logistic(ActivationLayerInfo act_info) : vone(wrapper::vdup_n(static_cast<T>(1), ExactTagType{})) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 163 | { |
| 164 | ARM_COMPUTE_UNUSED(act_info); |
| 165 | } |
| 166 | |
| 167 | /** Run activation function. |
| 168 | * |
| 169 | * @param[in] vval Vector of values. |
| 170 | */ |
| 171 | void operator()(ExactType &vval) |
| 172 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 173 | vval = wrapper::vinv(wrapper::vadd(vone, wrapper::vexpq(wrapper::vneg(vval)))); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 176 | /** Run activation function. |
| 177 | * |
| 178 | * @param[in] val Scalar value. |
| 179 | */ |
| 180 | void operator()(T &val) |
| 181 | { |
| 182 | val = 1 / (1 + std::exp(-val)); |
| 183 | } |
| 184 | |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 185 | /** Vector of ones. */ |
| 186 | const ExactType vone; |
| 187 | }; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 188 | /** RELU activation object */ |
| 189 | template <typename T, int S> |
| 190 | struct relu |
| 191 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 192 | /** SIMD vector type. */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 193 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 194 | /** SIMD vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 195 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 196 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 197 | /** Construct a RELU activation object. |
| 198 | * |
| 199 | * @param[in] act_info Activation layer information. |
| 200 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 201 | explicit relu(ActivationLayerInfo act_info) : vzero(wrapper::vdup_n(static_cast<T>(0), ExactTagType{})) |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 202 | { |
| 203 | ARM_COMPUTE_UNUSED(act_info); |
| 204 | } |
| 205 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 206 | /** Run activation function. |
| 207 | * |
| 208 | * @param[in] vval Vector of values. |
| 209 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 210 | void operator()(ExactType &vval) |
| 211 | { |
| 212 | vval = wrapper::vmax(vzero, vval); |
| 213 | } |
| 214 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 215 | /** Run activation function. |
| 216 | * |
| 217 | * @param[in] val Scalar value. |
| 218 | */ |
| 219 | void operator()(T &val) |
| 220 | { |
| 221 | val = std::max(static_cast<T>(0), val); |
| 222 | } |
| 223 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 224 | /** Vector of zeroes. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 225 | const ExactType vzero; |
| 226 | }; |
| 227 | /** Bounded RELU activation object */ |
| 228 | template <typename T, int S> |
| 229 | struct brelu |
| 230 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 231 | /** SIMD vector type. */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 232 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 233 | /** SIMD vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 234 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 235 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 236 | /** Construct a bounded RELU activation object. |
| 237 | * |
| 238 | * @param[in] act_info Activation layer information. |
| 239 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 240 | explicit brelu(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 241 | : alpha(act_info.a()), |
| 242 | vzero(wrapper::vdup_n(static_cast<T>(0), ExactTagType{})), |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 243 | valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})) |
| 244 | { |
| 245 | } |
| 246 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 247 | /** Run activation function. |
| 248 | * |
| 249 | * @param[in] vval Vector of values. |
| 250 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 251 | void operator()(ExactType &vval) |
| 252 | { |
| 253 | vval = wrapper::vmin(valpha, wrapper::vmax(vzero, vval)); |
| 254 | } |
| 255 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 256 | /** Run activation function. |
| 257 | * |
| 258 | * @param[in] val Scalar value. |
| 259 | */ |
| 260 | void operator()(T &val) |
| 261 | { |
| 262 | val = std::min(alpha, std::max(static_cast<T>(0), val)); |
| 263 | } |
| 264 | |
| 265 | const T alpha; /** Scalar alpha */ |
| 266 | const ExactType vzero; /** Vector of zeroes. */ |
| 267 | const ExactType valpha; /** Vector of alphas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 268 | }; |
| 269 | /** Lower-Upper Bounded RELU activation object */ |
| 270 | template <typename T, int S> |
| 271 | struct lubrelu |
| 272 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 273 | /** SIMD vector type. */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 274 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 275 | /** SIMD vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 276 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 277 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 278 | /** Construct a lower-upper bounded RELU activation object. |
| 279 | * |
| 280 | * @param[in] act_info Activation layer information. |
| 281 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 282 | explicit lubrelu(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 283 | : alpha(act_info.a()), |
| 284 | beta(act_info.b()), |
| 285 | valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})), |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 286 | vbeta(wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{})) |
| 287 | { |
| 288 | } |
| 289 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 290 | /** Run activation function. |
| 291 | * |
| 292 | * @param[in] vval Vector of values. |
| 293 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 294 | void operator()(ExactType &vval) |
| 295 | { |
| 296 | vval = wrapper::vmin(valpha, wrapper::vmax(vbeta, vval)); |
| 297 | } |
| 298 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 299 | /** Run activation function. |
| 300 | * |
| 301 | * @param[in] val Scalar value. |
| 302 | */ |
| 303 | void operator()(T &val) |
| 304 | { |
| 305 | val = std::min(alpha, std::max(beta, val)); |
| 306 | } |
| 307 | |
| 308 | const T alpha; /**< Scalar alpha */ |
| 309 | const T beta; /**< Scalar alpha */ |
| 310 | const ExactType valpha; /** Vector of alphas. */ |
| 311 | const ExactType vbeta; /** Vector of betas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 312 | }; |
| 313 | } // namespace detail |
| 314 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 315 | #endif /* ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H */ |