Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEActivationLayerKernel.h" |
| 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/NEON/NEAsymm.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 30 | #include "arm_compute/core/NEON/NEMath.h" |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/QAsymm8.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include <arm_neon.h> |
| 38 | #include <array> |
| 39 | #include <cmath> |
| 40 | #include <map> |
| 41 | |
| 42 | using namespace arm_compute; |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 43 | namespace |
| 44 | { |
| 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 49 | |
| 50 | // Checks performed when output is configured |
| 51 | if((output != nullptr) && (output->total_size() != 0)) |
| 52 | { |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return Status{}; |
| 58 | } |
| 59 | |
| 60 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 61 | { |
| 62 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 63 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 64 | bool window_changed = false; |
| 65 | |
| 66 | if(output != nullptr && (output->total_size() != 0)) |
| 67 | { |
| 68 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 69 | |
| 70 | window_changed = update_window_and_padding(win, |
| 71 | AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration), |
| 72 | output_access); |
| 73 | |
| 74 | output_access.set_valid_region(win, input->valid_region()); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | // In-place computation |
| 79 | window_changed = update_window_and_padding(win, |
| 80 | AccessWindowHorizontal(input, 0, num_elems_processed_per_iteration)); |
| 81 | } |
| 82 | |
| 83 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 84 | return std::make_pair(err, win); |
| 85 | } |
| 86 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | |
| 88 | NEActivationLayerKernel::NEActivationLayerKernel() |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 89 | : _input(nullptr), _output(nullptr), _func(nullptr), _act_info(ActivationFunction::LOGISTIC) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | { |
| 91 | } |
| 92 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 93 | void NEActivationLayerKernel::configure(ITensor *input, ITensor *output, ActivationLayerInfo activation_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 97 | _input = input; |
| 98 | _act_info = activation_info; |
| 99 | _output = input; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 101 | if(output != nullptr) |
| 102 | { |
| 103 | // Output auto inizialitation if not yet initialized |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 104 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 105 | _output = output; |
| 106 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 108 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr)); |
| 109 | |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_ON_MSG((input->info()->data_type() == DataType::QASYMM8) && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU) |
| 111 | && (activation_info.activation() != ActivationLayerInfo::ActivationFunction::RELU), |
| 112 | "For QASYMM8 only relu and lower/upper bounded relu are supported"); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 113 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | // Activation functions : FP32 |
| 115 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f32 = |
| 116 | { |
| 117 | { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float> }, |
| 118 | { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float> }, |
| 119 | { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float> }, |
| 120 | { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float> }, |
| 121 | { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float> }, |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 122 | { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float> }, |
Georgios Pinitas | 579c049 | 2017-07-12 16:12:12 +0100 | [diff] [blame] | 123 | { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float> }, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float> }, |
| 125 | { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float> }, |
| 126 | { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float> }, |
| 127 | { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float> }, |
| 128 | }; |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 129 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 130 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 131 | // Activation functions : FP16 |
| 132 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f16 = |
| 133 | { |
| 134 | { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float16_t> }, |
| 135 | { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float16_t> }, |
| 136 | { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float16_t> }, |
| 137 | { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float16_t> }, |
| 138 | { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float16_t> }, |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 139 | { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float16_t> }, |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 140 | { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float16_t> }, |
| 141 | { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float16_t> }, |
| 142 | { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float16_t> }, |
| 143 | { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float16_t> }, |
| 144 | }; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 145 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/ |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 146 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 147 | // Activation functions : QASYMM8 |
| 148 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8 = |
| 149 | { |
| 150 | { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_t> }, |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 151 | { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_t> }, |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 152 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | switch(input->info()->data_type()) |
| 155 | { |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 156 | case DataType::QASYMM8: |
| 157 | _func = act_map_qasymm8[activation_info.activation()]; |
| 158 | break; |
Georgios Pinitas | ccc65d4 | 2017-06-27 17:39:11 +0100 | [diff] [blame] | 159 | case DataType::F32: |
| 160 | _func = act_map_f32[activation_info.activation()]; |
| 161 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 162 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 163 | case DataType::F16: |
| 164 | _func = act_map_f16[activation_info.activation()]; |
| 165 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 166 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | default: |
| 168 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 169 | } |
| 170 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 171 | // Configure kernel window |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 172 | auto win_config = validate_and_configure_window(input->info(), (output != nullptr) ? output->info() : nullptr); |
| 173 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 174 | ICPPKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 177 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 178 | template <ActivationLayerInfo::ActivationFunction F, typename T> |
| 179 | typename std::enable_if<std::is_same<T, float16_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window) |
| 180 | { |
| 181 | Iterator input(_input, window); |
| 182 | Iterator output(_output, window); |
| 183 | |
| 184 | static const float16x8_t CONST_0 = vdupq_n_f16(0.f); |
| 185 | static const float16x8_t CONST_1 = vdupq_n_f16(1.f); |
| 186 | |
| 187 | const float16x8_t a = vdupq_n_f16(_act_info.a()); |
| 188 | const float16x8_t b = vdupq_n_f16(_act_info.b()); |
| 189 | |
| 190 | execute_window_loop(window, [&](const Coordinates &) |
| 191 | { |
| 192 | const auto input_ptr = reinterpret_cast<const float16_t *>(input.ptr()); |
| 193 | const auto output_ptr = reinterpret_cast<float16_t *>(output.ptr()); |
| 194 | |
| 195 | const float16x8x2_t in = vld2q_f16(input_ptr); |
| 196 | float16x8x2_t tmp = { {} }; |
| 197 | |
| 198 | switch(F) |
| 199 | { |
| 200 | case ActivationFunction::ABS: |
| 201 | tmp = |
| 202 | { |
| 203 | { |
| 204 | vabsq_f16(in.val[0]), |
| 205 | vabsq_f16(in.val[1]), |
| 206 | } |
| 207 | }; |
| 208 | break; |
| 209 | case ActivationFunction::BOUNDED_RELU: |
| 210 | tmp = |
| 211 | { |
| 212 | { |
| 213 | vminq_f16(a, vmaxq_f16(CONST_0, in.val[0])), |
| 214 | vminq_f16(a, vmaxq_f16(CONST_0, in.val[1])) |
| 215 | } |
| 216 | }; |
| 217 | break; |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 218 | case ActivationFunction::LU_BOUNDED_RELU: |
| 219 | tmp = |
| 220 | { |
| 221 | { |
| 222 | vminq_f16(a, vmaxq_f16(b, in.val[0])), |
| 223 | vminq_f16(a, vmaxq_f16(b, in.val[1])) |
| 224 | } |
| 225 | }; |
| 226 | break; |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 227 | case ActivationFunction::LINEAR: |
| 228 | tmp = |
| 229 | { |
| 230 | { |
| 231 | vaddq_f16(b, vmulq_f16(a, in.val[0])), |
| 232 | vaddq_f16(b, vmulq_f16(a, in.val[1])) |
| 233 | } |
| 234 | }; |
| 235 | break; |
| 236 | case ActivationFunction::LOGISTIC: |
| 237 | tmp = |
| 238 | { |
| 239 | { |
| 240 | vinvq_f16(vaddq_f16(CONST_1, vexpq_f16(vnegq_f16(in.val[0])))), |
| 241 | vinvq_f16(vaddq_f16(CONST_1, vexpq_f16(vnegq_f16(in.val[1])))), |
| 242 | } |
| 243 | }; |
| 244 | break; |
| 245 | case ActivationFunction::RELU: |
| 246 | tmp = |
| 247 | { |
| 248 | { |
| 249 | vmaxq_f16(CONST_0, in.val[0]), |
| 250 | vmaxq_f16(CONST_0, in.val[1]) |
| 251 | } |
| 252 | }; |
| 253 | break; |
| 254 | case ActivationFunction::LEAKY_RELU: |
| 255 | tmp = |
| 256 | { |
| 257 | { |
| 258 | vbslq_f16(vcgtq_f16(in.val[0], CONST_0), in.val[0], vmulq_f16(a, in.val[0])), |
| 259 | vbslq_f16(vcgtq_f16(in.val[1], CONST_0), in.val[1], vmulq_f16(a, in.val[1])) |
| 260 | } |
| 261 | }; |
| 262 | break; |
| 263 | case ActivationFunction::SOFT_RELU: |
| 264 | tmp = |
| 265 | { |
| 266 | { |
| 267 | vlogq_f16(vaddq_f16(CONST_1, vexpq_f16(in.val[0]))), |
| 268 | vlogq_f16(vaddq_f16(CONST_1, vexpq_f16(in.val[1]))), |
| 269 | } |
| 270 | }; |
| 271 | break; |
| 272 | case ActivationFunction::SQRT: |
| 273 | tmp = |
| 274 | { |
| 275 | { |
| 276 | vinvq_f16(vinvsqrtq_f16(in.val[0])), |
| 277 | vinvq_f16(vinvsqrtq_f16(in.val[1])), |
| 278 | } |
| 279 | }; |
| 280 | break; |
| 281 | case ActivationFunction::SQUARE: |
| 282 | tmp = |
| 283 | { |
| 284 | { |
| 285 | vmulq_f16(in.val[0], in.val[0]), |
| 286 | vmulq_f16(in.val[1], in.val[1]) |
| 287 | } |
| 288 | }; |
| 289 | break; |
| 290 | case ActivationFunction::TANH: |
| 291 | tmp = |
| 292 | { |
| 293 | { |
| 294 | vmulq_f16(a, vtanhq_f16(vmulq_f16(b, in.val[0]))), |
| 295 | vmulq_f16(a, vtanhq_f16(vmulq_f16(b, in.val[1]))), |
| 296 | } |
| 297 | }; |
| 298 | break; |
| 299 | default: |
| 300 | ARM_COMPUTE_ERROR("Not implemented"); |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | vst2q_f16(output_ptr, tmp); |
| 305 | }, |
| 306 | input, output); |
| 307 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 308 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 309 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 310 | template <ActivationLayerInfo::ActivationFunction F, typename T> |
| 311 | typename std::enable_if<std::is_same<T, float>::value, void>::type NEActivationLayerKernel::activation(const Window &window) |
| 312 | { |
| 313 | Iterator input(_input, window); |
| 314 | Iterator output(_output, window); |
| 315 | |
| 316 | static const float32x4_t CONST_1 = vdupq_n_f32(1.f); |
| 317 | static const float32x4_t CONST_0 = vdupq_n_f32(0.f); |
| 318 | const float32x4_t a = vdupq_n_f32(_act_info.a()); |
| 319 | const float32x4_t b = vdupq_n_f32(_act_info.b()); |
| 320 | |
| 321 | execute_window_loop(window, [&](const Coordinates & id) |
| 322 | { |
| 323 | const auto input_ptr = reinterpret_cast<const float *>(input.ptr()); |
| 324 | const auto output_ptr = reinterpret_cast<float *>(output.ptr()); |
| 325 | |
Georgios Pinitas | f525eab | 2018-01-30 14:47:39 +0000 | [diff] [blame] | 326 | const float32x4x4_t in = |
| 327 | { |
| 328 | { |
| 329 | vld1q_f32(input_ptr), |
| 330 | vld1q_f32(input_ptr + 4), |
| 331 | vld1q_f32(input_ptr + 8), |
| 332 | vld1q_f32(input_ptr + 12) |
| 333 | } |
| 334 | }; |
| 335 | float32x4x4_t tmp = { {} }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 336 | |
| 337 | switch(F) |
| 338 | { |
| 339 | case ActivationFunction::ABS: |
| 340 | tmp = |
| 341 | { |
| 342 | { |
| 343 | vabsq_f32(in.val[0]), |
| 344 | vabsq_f32(in.val[1]), |
| 345 | vabsq_f32(in.val[2]), |
| 346 | vabsq_f32(in.val[3]), |
| 347 | } |
| 348 | }; |
| 349 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 350 | case ActivationFunction::LINEAR: |
| 351 | tmp = |
| 352 | { |
| 353 | { |
| 354 | vmlaq_f32(b, a, in.val[0]), |
| 355 | vmlaq_f32(b, a, in.val[1]), |
| 356 | vmlaq_f32(b, a, in.val[2]), |
| 357 | vmlaq_f32(b, a, in.val[3]), |
| 358 | } |
| 359 | }; |
| 360 | break; |
| 361 | case ActivationFunction::LOGISTIC: |
| 362 | tmp = |
| 363 | { |
| 364 | { |
| 365 | vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[0])))), |
| 366 | vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[1])))), |
| 367 | vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[2])))), |
| 368 | vinvq_f32(vaddq_f32(CONST_1, vexpq_f32(vnegq_f32(in.val[3])))), |
| 369 | } |
| 370 | }; |
| 371 | break; |
| 372 | case ActivationFunction::RELU: |
| 373 | tmp = |
| 374 | { |
| 375 | { |
| 376 | vmaxq_f32(CONST_0, in.val[0]), |
| 377 | vmaxq_f32(CONST_0, in.val[1]), |
| 378 | vmaxq_f32(CONST_0, in.val[2]), |
| 379 | vmaxq_f32(CONST_0, in.val[3]), |
| 380 | } |
| 381 | }; |
| 382 | break; |
Georgios Pinitas | 579c049 | 2017-07-12 16:12:12 +0100 | [diff] [blame] | 383 | case ActivationFunction::BOUNDED_RELU: |
| 384 | tmp = |
| 385 | { |
| 386 | { |
| 387 | vminq_f32(a, vmaxq_f32(CONST_0, in.val[0])), |
| 388 | vminq_f32(a, vmaxq_f32(CONST_0, in.val[1])), |
| 389 | vminq_f32(a, vmaxq_f32(CONST_0, in.val[2])), |
| 390 | vminq_f32(a, vmaxq_f32(CONST_0, in.val[3])), |
| 391 | } |
| 392 | }; |
| 393 | break; |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 394 | case ActivationFunction::LU_BOUNDED_RELU: |
| 395 | tmp = |
| 396 | { |
| 397 | { |
| 398 | vminq_f32(a, vmaxq_f32(b, in.val[0])), |
| 399 | vminq_f32(a, vmaxq_f32(b, in.val[1])), |
| 400 | vminq_f32(a, vmaxq_f32(b, in.val[2])), |
| 401 | vminq_f32(a, vmaxq_f32(b, in.val[3])), |
| 402 | } |
| 403 | }; |
| 404 | break; |
Georgios Pinitas | 579c049 | 2017-07-12 16:12:12 +0100 | [diff] [blame] | 405 | case ActivationFunction::LEAKY_RELU: |
| 406 | tmp = |
| 407 | { |
| 408 | { |
| 409 | vbslq_f32(vcgtq_f32(in.val[0], CONST_0), in.val[0], vmulq_f32(a, in.val[0])), |
| 410 | vbslq_f32(vcgtq_f32(in.val[1], CONST_0), in.val[1], vmulq_f32(a, in.val[1])), |
| 411 | vbslq_f32(vcgtq_f32(in.val[2], CONST_0), in.val[2], vmulq_f32(a, in.val[2])), |
| 412 | vbslq_f32(vcgtq_f32(in.val[3], CONST_0), in.val[3], vmulq_f32(a, in.val[3])), |
| 413 | } |
| 414 | }; |
| 415 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 416 | case ActivationFunction::SOFT_RELU: |
| 417 | tmp = |
| 418 | { |
| 419 | { |
| 420 | vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[0]))), |
| 421 | vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[1]))), |
| 422 | vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[2]))), |
| 423 | vlogq_f32(vaddq_f32(CONST_1, vexpq_f32(in.val[3]))), |
| 424 | } |
| 425 | }; |
| 426 | break; |
| 427 | case ActivationFunction::SQRT: |
| 428 | tmp = |
| 429 | { |
| 430 | { |
| 431 | vinvq_f32(vinvsqrtq_f32(in.val[0])), |
| 432 | vinvq_f32(vinvsqrtq_f32(in.val[1])), |
| 433 | vinvq_f32(vinvsqrtq_f32(in.val[2])), |
| 434 | vinvq_f32(vinvsqrtq_f32(in.val[3])), |
| 435 | } |
| 436 | }; |
| 437 | break; |
| 438 | case ActivationFunction::SQUARE: |
| 439 | tmp = |
| 440 | { |
| 441 | { |
| 442 | vmulq_f32(in.val[0], in.val[0]), |
| 443 | vmulq_f32(in.val[1], in.val[1]), |
| 444 | vmulq_f32(in.val[2], in.val[2]), |
| 445 | vmulq_f32(in.val[3], in.val[3]), |
| 446 | } |
| 447 | }; |
| 448 | break; |
| 449 | case ActivationFunction::TANH: |
| 450 | tmp = |
| 451 | { |
| 452 | { |
| 453 | vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[0]))), |
| 454 | vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[1]))), |
| 455 | vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[2]))), |
| 456 | vmulq_f32(a, vtanhq_f32(vmulq_f32(b, in.val[3]))), |
| 457 | } |
| 458 | }; |
| 459 | break; |
| 460 | default: |
| 461 | break; |
| 462 | } |
| 463 | |
Georgios Pinitas | f525eab | 2018-01-30 14:47:39 +0000 | [diff] [blame] | 464 | vst1q_f32(output_ptr, tmp.val[0]); |
| 465 | vst1q_f32(output_ptr + 4, tmp.val[1]); |
| 466 | vst1q_f32(output_ptr + 8, tmp.val[2]); |
| 467 | vst1q_f32(output_ptr + 12, tmp.val[3]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 468 | }, |
| 469 | input, output); |
| 470 | } |
| 471 | |
| 472 | template <ActivationLayerInfo::ActivationFunction F, typename T> |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 473 | typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const Window &window) |
| 474 | { |
Isabella Gottardi | abca1c9 | 2018-02-26 16:06:06 +0000 | [diff] [blame] | 475 | Iterator input(_input, window); |
| 476 | Iterator output(_output, window); |
| 477 | const QuantizationInfo qi_in = _input->info()->quantization_info(); |
| 478 | const QuantizationInfo qi_out = _output->info()->quantization_info(); |
| 479 | const qasymm8x16_t a = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.a(), qi_in.scale, qi_in.offset)); |
| 480 | const qasymm8x16_t b = vdupq_n_u8(sqcvt_qasymm8_f32(_act_info.b(), qi_in.scale, qi_in.offset)); |
| 481 | const qasymm8x16_t CONST_0 = vdupq_n_u8(sqcvt_qasymm8_f32(0.f, qi_in.scale, qi_in.offset)); |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 482 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 483 | // Initialise scale/offset for re-quantization |
| 484 | float s = qi_in.scale / qi_out.scale; |
| 485 | float o = -qi_in.offset * s + qi_out.offset; |
| 486 | float32x4_t vs = vdupq_n_f32(s); |
| 487 | float32x4_t vo = vdupq_n_f32(o); |
| 488 | |
| 489 | execute_window_loop(window, [&](const Coordinates & id) |
| 490 | { |
| 491 | const auto input_ptr = reinterpret_cast<const qasymm8_t *>(input.ptr()); |
| 492 | const auto output_ptr = reinterpret_cast<qasymm8_t *>(output.ptr()); |
| 493 | |
| 494 | const qasymm8x16_t in = vld1q_u8(input_ptr); |
| 495 | qasymm8x16_t tmp = {}; |
| 496 | |
| 497 | switch(F) |
| 498 | { |
| 499 | case ActivationFunction::LU_BOUNDED_RELU: |
| 500 | // Perform activation |
| 501 | tmp = vminq_u8(a, vmaxq_u8(b, in)); |
| 502 | // Re-quantize to new output space |
| 503 | tmp = vmlaq_qasymm8(tmp, vs, vo); |
| 504 | break; |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 505 | case ActivationFunction::RELU: |
| 506 | // Perform activation |
| 507 | tmp = vmaxq_u8(CONST_0, in); |
| 508 | // Re-quantize to new output space |
| 509 | tmp = vmlaq_qasymm8(tmp, vs, vo); |
| 510 | break; |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 511 | default: |
| 512 | ARM_COMPUTE_ERROR("Function not implemented"); |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | vst1q_u8(output_ptr, tmp); |
| 517 | }, |
| 518 | input, output); |
| 519 | } |
| 520 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 521 | Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
| 522 | { |
| 523 | ARM_COMPUTE_UNUSED(act_info); |
| 524 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 525 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first); |
| 526 | |
| 527 | return Status{}; |
| 528 | } |
| 529 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 530 | void NEActivationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 531 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 532 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 533 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 534 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 535 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 536 | |
| 537 | (this->*_func)(window); |
| 538 | } |