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" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 28 | #include "src/core/NEON/wrapper/wrapper.h" |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace detail |
| 33 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 34 | /** Dummy activation object */ |
| 35 | template <typename T, int S> |
| 36 | struct dummy |
| 37 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 38 | /** SIMD vector type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 39 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 40 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 41 | /** Construct a dummy activation object. |
| 42 | * |
| 43 | * @param[in] act_info Activation layer information. |
| 44 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 45 | explicit dummy(ActivationLayerInfo act_info) |
| 46 | { |
| 47 | ARM_COMPUTE_UNUSED(act_info); |
| 48 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 49 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 50 | /** Run activation function. |
| 51 | * |
| 52 | * @param[in] vval Vector of values. |
| 53 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 54 | void operator()(ExactType &vval) |
| 55 | { |
| 56 | ARM_COMPUTE_UNUSED(vval); |
| 57 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 58 | |
| 59 | /** Run activation function. |
| 60 | * |
| 61 | * @param[in] val Scalar value. |
| 62 | */ |
| 63 | void operator()(T &val) |
| 64 | { |
| 65 | ARM_COMPUTE_UNUSED(val); |
| 66 | } |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 67 | }; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 68 | /** Linear activation object */ |
| 69 | template <typename T, int S> |
| 70 | struct linear |
| 71 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 72 | /** SIMD vector type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 73 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 74 | /** SIMD vector tag type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 75 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 76 | |
| 77 | /** Construct a Linear activation object. |
| 78 | * |
| 79 | * @param[in] act_info Activation layer information. |
| 80 | */ |
| 81 | explicit linear(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 82 | : alpha(act_info.a()), |
| 83 | beta(act_info.b()), |
| 84 | valpha(wrapper::vdup_n(static_cast<T>(alpha), ExactTagType{})), |
| 85 | vbeta(wrapper::vdup_n(static_cast<T>(beta), ExactTagType{})) |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
| 89 | /** Run activation function. |
| 90 | * |
| 91 | * @param[in] vval Vector of values. |
| 92 | */ |
| 93 | void operator()(ExactType &vval) |
| 94 | { |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 95 | vval = wrapper::vmla(vbeta, vval, valpha); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 98 | /** Run activation function. |
| 99 | * |
| 100 | * @param[in] val Scalar value. |
| 101 | */ |
| 102 | void operator()(T &val) |
| 103 | { |
| 104 | val = alpha * val + beta; |
| 105 | } |
| 106 | |
| 107 | const T alpha; /**< Scalar alpha */ |
| 108 | const T beta; /**< Scalar alpha */ |
| 109 | const ExactType valpha; /**< Vector of alphas. */ |
| 110 | const ExactType vbeta; /**< Vector of betas. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 111 | }; |
| 112 | /** Square activation object */ |
| 113 | template <typename T, int S> |
| 114 | struct square |
| 115 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 116 | /** SIMD vector type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 117 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 118 | /** SIMD vector tag type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 119 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 120 | |
| 121 | /** Construct a Square activation object. |
| 122 | * |
| 123 | * @param[in] act_info Activation layer information. |
| 124 | */ |
| 125 | explicit square(ActivationLayerInfo act_info) |
| 126 | { |
| 127 | ARM_COMPUTE_UNUSED(act_info); |
| 128 | } |
| 129 | |
| 130 | /** Run activation function. |
| 131 | * |
| 132 | * @param[in] vval Vector of values. |
| 133 | */ |
| 134 | void operator()(ExactType &vval) |
| 135 | { |
| 136 | vval = wrapper::vmul(vval, vval); |
| 137 | } |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 138 | |
| 139 | /** Run activation function. |
| 140 | * |
| 141 | * @param[in] val Scalar value. |
| 142 | */ |
| 143 | void operator()(T &val) |
| 144 | { |
| 145 | val = val * val; |
| 146 | } |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 147 | }; |
| 148 | /** Logistic activation object */ |
| 149 | template <typename T, int S> |
| 150 | struct logistic |
| 151 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 152 | /** SIMD vector type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 153 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 154 | /** SIMD vector tag type. */ |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 155 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 156 | |
| 157 | /** Construct a Logistic activation object. |
| 158 | * |
| 159 | * @param[in] act_info Activation layer information. |
| 160 | */ |
| 161 | explicit logistic(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 162 | : 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 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 201 | explicit relu(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 202 | : vzero(wrapper::vdup_n(static_cast<T>(0), ExactTagType{})) |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 203 | { |
| 204 | ARM_COMPUTE_UNUSED(act_info); |
| 205 | } |
| 206 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 207 | /** Run activation function. |
| 208 | * |
| 209 | * @param[in] vval Vector of values. |
| 210 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 211 | void operator()(ExactType &vval) |
| 212 | { |
| 213 | vval = wrapper::vmax(vzero, vval); |
| 214 | } |
| 215 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 216 | /** Run activation function. |
| 217 | * |
| 218 | * @param[in] val Scalar value. |
| 219 | */ |
| 220 | void operator()(T &val) |
| 221 | { |
| 222 | val = std::max(static_cast<T>(0), val); |
| 223 | } |
| 224 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 225 | /** Vector of zeroes. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 226 | const ExactType vzero; |
| 227 | }; |
| 228 | /** Bounded RELU activation object */ |
| 229 | template <typename T, int S> |
| 230 | struct brelu |
| 231 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 232 | /** SIMD vector type. */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 233 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 234 | /** SIMD vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 235 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 236 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 237 | /** Construct a bounded RELU activation object. |
| 238 | * |
| 239 | * @param[in] act_info Activation layer information. |
| 240 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 241 | explicit brelu(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 242 | : alpha(act_info.a()), |
| 243 | vzero(wrapper::vdup_n(static_cast<T>(0), ExactTagType{})), |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 244 | valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})) |
| 245 | { |
| 246 | } |
| 247 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 248 | /** Run activation function. |
| 249 | * |
| 250 | * @param[in] vval Vector of values. |
| 251 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 252 | void operator()(ExactType &vval) |
| 253 | { |
| 254 | vval = wrapper::vmin(valpha, wrapper::vmax(vzero, vval)); |
| 255 | } |
| 256 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 257 | /** Run activation function. |
| 258 | * |
| 259 | * @param[in] val Scalar value. |
| 260 | */ |
| 261 | void operator()(T &val) |
| 262 | { |
| 263 | val = std::min(alpha, std::max(static_cast<T>(0), val)); |
| 264 | } |
| 265 | |
| 266 | const T alpha; /** Scalar alpha */ |
| 267 | const ExactType vzero; /** Vector of zeroes. */ |
| 268 | const ExactType valpha; /** Vector of alphas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 269 | }; |
| 270 | /** Lower-Upper Bounded RELU activation object */ |
| 271 | template <typename T, int S> |
| 272 | struct lubrelu |
| 273 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 274 | /** SIMD vector type. */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 275 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 276 | /** SIMD vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 277 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 278 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 279 | /** Construct a lower-upper bounded RELU activation object. |
| 280 | * |
| 281 | * @param[in] act_info Activation layer information. |
| 282 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 283 | explicit lubrelu(ActivationLayerInfo act_info) |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 284 | : alpha(act_info.a()), |
| 285 | beta(act_info.b()), |
| 286 | valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})), |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 287 | vbeta(wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{})) |
| 288 | { |
| 289 | } |
| 290 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 291 | /** Run activation function. |
| 292 | * |
| 293 | * @param[in] vval Vector of values. |
| 294 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 295 | void operator()(ExactType &vval) |
| 296 | { |
| 297 | vval = wrapper::vmin(valpha, wrapper::vmax(vbeta, vval)); |
| 298 | } |
| 299 | |
Georgios Pinitas | 980a916 | 2020-06-03 20:16:46 +0100 | [diff] [blame] | 300 | /** Run activation function. |
| 301 | * |
| 302 | * @param[in] val Scalar value. |
| 303 | */ |
| 304 | void operator()(T &val) |
| 305 | { |
| 306 | val = std::min(alpha, std::max(beta, val)); |
| 307 | } |
| 308 | |
| 309 | const T alpha; /**< Scalar alpha */ |
| 310 | const T beta; /**< Scalar alpha */ |
| 311 | const ExactType valpha; /** Vector of alphas. */ |
| 312 | const ExactType vbeta; /** Vector of betas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 313 | }; |
| 314 | } // namespace detail |
| 315 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 316 | #endif /* ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H */ |