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