Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | |
| 27 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace detail |
| 32 | { |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 33 | /** Dummy activation object */ |
| 34 | template <typename T, int S> |
| 35 | struct dummy |
| 36 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 37 | /** NEON vector type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 38 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 39 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 40 | /** Construct a dummy activation object. |
| 41 | * |
| 42 | * @param[in] act_info Activation layer information. |
| 43 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 44 | explicit dummy(ActivationLayerInfo act_info) |
| 45 | { |
| 46 | ARM_COMPUTE_UNUSED(act_info); |
| 47 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 48 | /** Run activation function. |
| 49 | * |
| 50 | * @param[in] vval Vector of values. |
| 51 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 52 | void operator()(ExactType &vval) |
| 53 | { |
| 54 | ARM_COMPUTE_UNUSED(vval); |
| 55 | } |
| 56 | }; |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 57 | /** Linear activation object */ |
| 58 | template <typename T, int S> |
| 59 | struct linear |
| 60 | { |
| 61 | /** NEON vector type. */ |
| 62 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 63 | /** NEON vector tag type. */ |
| 64 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 65 | |
| 66 | /** Construct a Linear activation object. |
| 67 | * |
| 68 | * @param[in] act_info Activation layer information. |
| 69 | */ |
| 70 | explicit linear(ActivationLayerInfo act_info) |
| 71 | : valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})), |
| 72 | vbeta(wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{})) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | /** Run activation function. |
| 77 | * |
| 78 | * @param[in] vval Vector of values. |
| 79 | */ |
| 80 | void operator()(ExactType &vval) |
| 81 | { |
| 82 | vval = wrapper::vmla(vval, valpha, vbeta); |
| 83 | } |
| 84 | |
| 85 | /** Vector of alphas. */ |
| 86 | const ExactType valpha; |
| 87 | /** Vector of betas. */ |
| 88 | const ExactType vbeta; |
| 89 | }; |
| 90 | /** Square activation object */ |
| 91 | template <typename T, int S> |
| 92 | struct square |
| 93 | { |
| 94 | /** NEON vector type. */ |
| 95 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 96 | /** NEON vector tag type. */ |
| 97 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 98 | |
| 99 | /** Construct a Square activation object. |
| 100 | * |
| 101 | * @param[in] act_info Activation layer information. |
| 102 | */ |
| 103 | explicit square(ActivationLayerInfo act_info) |
| 104 | { |
| 105 | ARM_COMPUTE_UNUSED(act_info); |
| 106 | } |
| 107 | |
| 108 | /** Run activation function. |
| 109 | * |
| 110 | * @param[in] vval Vector of values. |
| 111 | */ |
| 112 | void operator()(ExactType &vval) |
| 113 | { |
| 114 | vval = wrapper::vmul(vval, vval); |
| 115 | } |
| 116 | }; |
| 117 | /** Logistic activation object */ |
| 118 | template <typename T, int S> |
| 119 | struct logistic |
| 120 | { |
| 121 | /** NEON vector type. */ |
| 122 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 123 | /** NEON vector tag type. */ |
| 124 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 125 | |
| 126 | /** Construct a Logistic activation object. |
| 127 | * |
| 128 | * @param[in] act_info Activation layer information. |
| 129 | */ |
| 130 | explicit logistic(ActivationLayerInfo act_info) |
| 131 | : vone(wrapper::vdup_n(static_cast<T>(1.f), ExactTagType{})) |
| 132 | { |
| 133 | ARM_COMPUTE_UNUSED(act_info); |
| 134 | } |
| 135 | |
| 136 | /** Run activation function. |
| 137 | * |
| 138 | * @param[in] vval Vector of values. |
| 139 | */ |
| 140 | void operator()(ExactType &vval) |
| 141 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 142 | vval = wrapper::vinv(wrapper::vadd(vone, wrapper::vexpq(wrapper::vneg(vval)))); |
Michalis Spyrou | a6825a4 | 2018-09-13 12:24:03 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** Vector of ones. */ |
| 146 | const ExactType vone; |
| 147 | }; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 148 | /** RELU activation object */ |
| 149 | template <typename T, int S> |
| 150 | struct relu |
| 151 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 152 | /** NEON vector type. */ |
| 153 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 154 | /** NEON vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 155 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 156 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 157 | /** Construct a RELU activation object. |
| 158 | * |
| 159 | * @param[in] act_info Activation layer information. |
| 160 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 161 | explicit relu(ActivationLayerInfo act_info) |
| 162 | : vzero(wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{})) |
| 163 | { |
| 164 | ARM_COMPUTE_UNUSED(act_info); |
| 165 | } |
| 166 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 167 | /** Run activation function. |
| 168 | * |
| 169 | * @param[in] vval Vector of values. |
| 170 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 171 | void operator()(ExactType &vval) |
| 172 | { |
| 173 | vval = wrapper::vmax(vzero, vval); |
| 174 | } |
| 175 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 176 | /** Vector of zeroes. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 177 | const ExactType vzero; |
| 178 | }; |
| 179 | /** Bounded RELU activation object */ |
| 180 | template <typename T, int S> |
| 181 | struct brelu |
| 182 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 183 | /** NEON vector type. */ |
| 184 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 185 | /** NEON vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 186 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 187 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 188 | /** Construct a bounded RELU activation object. |
| 189 | * |
| 190 | * @param[in] act_info Activation layer information. |
| 191 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 192 | explicit brelu(ActivationLayerInfo act_info) |
| 193 | : vzero(wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{})), |
| 194 | valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})) |
| 195 | { |
| 196 | } |
| 197 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 198 | /** Run activation function. |
| 199 | * |
| 200 | * @param[in] vval Vector of values. |
| 201 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 202 | void operator()(ExactType &vval) |
| 203 | { |
| 204 | vval = wrapper::vmin(valpha, wrapper::vmax(vzero, vval)); |
| 205 | } |
| 206 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 207 | /** Vector of zeroes. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 208 | const ExactType vzero; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 209 | /** Vector of alphas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 210 | const ExactType valpha; |
| 211 | }; |
| 212 | /** Lower-Upper Bounded RELU activation object */ |
| 213 | template <typename T, int S> |
| 214 | struct lubrelu |
| 215 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 216 | /** NEON vector type. */ |
| 217 | using ExactType = typename wrapper::traits::neon_vector<T, S>::type; |
| 218 | /** NEON vector tag type. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 219 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 220 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 221 | /** Construct a lower-upper bounded RELU activation object. |
| 222 | * |
| 223 | * @param[in] act_info Activation layer information. |
| 224 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 225 | explicit lubrelu(ActivationLayerInfo act_info) |
| 226 | : valpha(wrapper::vdup_n(static_cast<T>(act_info.a()), ExactTagType{})), |
| 227 | vbeta(wrapper::vdup_n(static_cast<T>(act_info.b()), ExactTagType{})) |
| 228 | { |
| 229 | } |
| 230 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 231 | /** Run activation function. |
| 232 | * |
| 233 | * @param[in] vval Vector of values. |
| 234 | */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 235 | void operator()(ExactType &vval) |
| 236 | { |
| 237 | vval = wrapper::vmin(valpha, wrapper::vmax(vbeta, vval)); |
| 238 | } |
| 239 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 240 | /** Vector of alphas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 241 | const ExactType valpha; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 242 | /** Vector of betas. */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 243 | const ExactType vbeta; |
| 244 | }; |
| 245 | } // namespace detail |
| 246 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame^] | 247 | #endif /* ARM_COMPUTE_DETAIL_NEACTIVATION_FUNCTION_DETAIL_H */ |