Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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" |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/NEON/NESymm.h" |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Utils.h" |
| 36 | #include "arm_compute/core/Validate.h" |
| 37 | #include "arm_compute/core/Window.h" |
| 38 | |
| 39 | #include <arm_neon.h> |
| 40 | #include <array> |
| 41 | #include <cmath> |
| 42 | #include <map> |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 43 | #include <set> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | |
| 45 | using namespace arm_compute; |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 46 | namespace |
| 47 | { |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 48 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &activation_info) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 49 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 52 | |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 53 | const static std::set<ActivationLayerInfo::ActivationFunction> qasymm8_supported_activations = |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 54 | { |
| 55 | ActivationLayerInfo::ActivationFunction::RELU, |
| 56 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
| 57 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 58 | ActivationLayerInfo::ActivationFunction::LOGISTIC, |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 59 | ActivationLayerInfo::ActivationFunction::TANH, |
| 60 | ActivationLayerInfo::ActivationFunction::HARD_SWISH |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 61 | }; |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 62 | const static std::set<ActivationLayerInfo::ActivationFunction> qsymm16_supported_activations = |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 63 | { |
| 64 | ActivationLayerInfo::ActivationFunction::LOGISTIC, |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 65 | ActivationLayerInfo::ActivationFunction::TANH, |
| 66 | ActivationLayerInfo::ActivationFunction::HARD_SWISH |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 67 | }; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 68 | const DataType data_type = input->data_type(); |
| 69 | const QuantizationInfo &oq_info = (output != nullptr) ? output->quantization_info() : input->quantization_info(); |
| 70 | const ActivationLayerInfo::ActivationFunction f_act = activation_info.activation(); |
| 71 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_asymmetric(data_type) && (qasymm8_supported_activations.count(f_act) == 0), |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 73 | "For QASYMM8 only tanh, logistic, relu and lower/upper bounded relu are supported"); |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 74 | |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_symmetric(data_type) && (qsymm16_supported_activations.count(f_act) == 0), |
| 76 | "For QSYMM16 only tanh and logistic are supported"); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) |
| 78 | && (oq_info != QuantizationInfo(1.f / 128.f, 128))); |
| 79 | ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) |
| 80 | && (oq_info != QuantizationInfo(1.f / 256.f, 0))); |
| 81 | |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0))); |
| 83 | ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128))); |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 84 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 85 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 32768.f, 0))); |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_symmetric(data_type) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 32768.f, 0))); |
| 87 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 88 | // Checks performed when output is configured |
| 89 | if((output != nullptr) && (output->total_size() != 0)) |
| 90 | { |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return Status{}; |
| 96 | } |
| 97 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 98 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 99 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 100 | // Configure kernel window |
| 101 | Window win = calculate_max_window(*input, Steps()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 102 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 103 | if(output != nullptr) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 104 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 105 | // Output auto inizialitation if not yet initialized |
| 106 | auto_init_if_empty(*output, *input->clone()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 107 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 108 | // NEActivationLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
| 109 | Coordinates coord; |
| 110 | coord.set_num_dimensions(output->num_dimensions()); |
| 111 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 114 | return std::make_pair(Status{}, win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 115 | } |
| 116 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | |
| 118 | NEActivationLayerKernel::NEActivationLayerKernel() |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 119 | : _func(nullptr), _act_info() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | { |
| 121 | } |
| 122 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 123 | void NEActivationLayerKernel::configure(const ITensorInfo *input, ITensorInfo *output, ActivationLayerInfo activation_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | { |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 125 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 127 | _act_info = activation_info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 129 | // Disabled activation, thus no operation needed |
| 130 | if(!activation_info.enabled()) |
| 131 | { |
| 132 | _func = nullptr; |
| 133 | } |
| 134 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 135 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, activation_info)); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 136 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | // Activation functions : FP32 |
| 138 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f32 = |
| 139 | { |
| 140 | { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float> }, |
| 141 | { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float> }, |
| 142 | { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float> }, |
| 143 | { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float> }, |
| 144 | { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float> }, |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 145 | { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float> }, |
Georgios Pinitas | 579c049 | 2017-07-12 16:12:12 +0100 | [diff] [blame] | 146 | { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float> }, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float> }, |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 148 | { ActivationFunction::ELU, &NEActivationLayerKernel::activation<ActivationFunction::ELU, float> }, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float> }, |
| 150 | { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float> }, |
| 151 | { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float> }, |
Usama Arif | 80e55db | 2019-05-14 17:48:47 +0100 | [diff] [blame] | 152 | { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float> }, |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 153 | { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, float> }, |
| 154 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | }; |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 156 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 157 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 158 | // Activation functions : FP16 |
| 159 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_f16 = |
| 160 | { |
| 161 | { ActivationFunction::ABS, &NEActivationLayerKernel::activation<ActivationFunction::ABS, float16_t> }, |
| 162 | { ActivationFunction::LINEAR, &NEActivationLayerKernel::activation<ActivationFunction::LINEAR, float16_t> }, |
| 163 | { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, float16_t> }, |
| 164 | { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, float16_t> }, |
| 165 | { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, float16_t> }, |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 166 | { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, float16_t> }, |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 167 | { ActivationFunction::LEAKY_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LEAKY_RELU, float16_t> }, |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 168 | { ActivationFunction::SOFT_RELU, &NEActivationLayerKernel::activation<ActivationFunction::SOFT_RELU, float16_t> }, |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 169 | { ActivationFunction::ELU, &NEActivationLayerKernel::activation<ActivationFunction::ELU, float16_t> }, |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 170 | { ActivationFunction::SQRT, &NEActivationLayerKernel::activation<ActivationFunction::SQRT, float16_t> }, |
| 171 | { ActivationFunction::SQUARE, &NEActivationLayerKernel::activation<ActivationFunction::SQUARE, float16_t> }, |
| 172 | { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, float16_t> }, |
Usama Arif | 80e55db | 2019-05-14 17:48:47 +0100 | [diff] [blame] | 173 | { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, float16_t> }, |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 174 | { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, float16_t> }, |
| 175 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 176 | }; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 177 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/ |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 178 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 179 | // Activation functions : QASYMM8_SIGNED |
| 180 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8_signed = |
| 181 | { |
| 182 | { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qasymm8_signed_t> }, |
| 183 | { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, qasymm8_signed_t> }, |
| 184 | { ActivationFunction::LU_BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::LU_BOUNDED_RELU, qasymm8_signed_t> }, |
| 185 | { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_signed_t> }, |
| 186 | { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qasymm8_signed_t> }, |
| 187 | { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, qasymm8_signed_t> }, |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 188 | { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, qasymm8_signed_t> }, |
| 189 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 192 | // Activation functions : QASYMM8 |
| 193 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qasymm8 = |
| 194 | { |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 195 | { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qasymm8_t> }, |
Isabella Gottardi | 5d62c01 | 2019-01-29 15:05:41 +0000 | [diff] [blame] | 196 | { ActivationFunction::BOUNDED_RELU, &NEActivationLayerKernel::activation<ActivationFunction::BOUNDED_RELU, qasymm8_t> }, |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 197 | { 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] | 198 | { ActivationFunction::RELU, &NEActivationLayerKernel::activation<ActivationFunction::RELU, qasymm8_t> }, |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 199 | { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qasymm8_t> }, |
Usama Arif | 80e55db | 2019-05-14 17:48:47 +0100 | [diff] [blame] | 200 | { ActivationFunction::IDENTITY, &NEActivationLayerKernel::activation<ActivationFunction::IDENTITY, qasymm8_t> }, |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 201 | { ActivationFunction::HARD_SWISH, &NEActivationLayerKernel::activation<ActivationFunction::HARD_SWISH, qasymm8_t> }, |
| 202 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 203 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 205 | // Activation functions : QSYMM16 |
| 206 | static std::map<ActivationFunction, ActivationFunctionExecutorPtr> act_map_qsymm16 = |
| 207 | { |
| 208 | { ActivationFunction::LOGISTIC, &NEActivationLayerKernel::activation<ActivationFunction::LOGISTIC, qsymm16_t> }, |
| 209 | { ActivationFunction::TANH, &NEActivationLayerKernel::activation<ActivationFunction::TANH, qsymm16_t> }, |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 210 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 211 | }; |
| 212 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 213 | switch(input->data_type()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | { |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 215 | case DataType::QASYMM8_SIGNED: |
| 216 | _func = act_map_qasymm8_signed[activation_info.activation()]; |
| 217 | break; |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 218 | case DataType::QASYMM8: |
| 219 | _func = act_map_qasymm8[activation_info.activation()]; |
| 220 | break; |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 221 | case DataType::QSYMM16: |
| 222 | _func = act_map_qsymm16[activation_info.activation()]; |
| 223 | break; |
Georgios Pinitas | ccc65d4 | 2017-06-27 17:39:11 +0100 | [diff] [blame] | 224 | case DataType::F32: |
| 225 | _func = act_map_f32[activation_info.activation()]; |
| 226 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 227 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 228 | case DataType::F16: |
| 229 | _func = act_map_f16[activation_info.activation()]; |
| 230 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 231 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 232 | default: |
| 233 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 234 | } |
| 235 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 236 | // Configure kernel window |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 237 | auto win_config = validate_and_configure_window(input, output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 238 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 239 | ICPPKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 242 | template <ActivationLayerInfo::ActivationFunction F, typename T> |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 243 | typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 244 | NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window) |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 245 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 246 | /** NEON vector tag type. */ |
| 247 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 248 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 249 | const int window_step_x = 16 / sizeof(T); |
| 250 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 251 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 252 | const ActivationFunction act = F; |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 253 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 254 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 255 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 256 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 257 | Iterator input(src, win_collapsed); |
| 258 | Iterator output(dst, win_collapsed); |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 259 | |
SiCong Li | a32e2ae | 2020-06-08 17:30:51 +0100 | [diff] [blame] | 260 | // A small delta added to the input to prevent NAN values caused by zeros in inputs to SQRT |
| 261 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 262 | const auto delta = wrapper::vdup_n(static_cast<T>(1e-7), ExactTagType {}); |
SiCong Li | a32e2ae | 2020-06-08 17:30:51 +0100 | [diff] [blame] | 263 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 264 | const auto delta = wrapper::vdup_n(static_cast<T>(1e-24), ExactTagType {}); |
SiCong Li | a32e2ae | 2020-06-08 17:30:51 +0100 | [diff] [blame] | 265 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 266 | const auto const_1 = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType {}); |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 267 | const auto const_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{}); |
| 268 | const auto const_6 = wrapper::vdup_n(static_cast<T>(6.f), ExactTagType{}); |
| 269 | const auto const_3 = wrapper::vdup_n(static_cast<T>(3.f), ExactTagType{}); |
| 270 | const auto const_inv_6 = wrapper::vdup_n(static_cast<T>(0.166666667f), ExactTagType{}); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 271 | |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 272 | const auto va = wrapper::vdup_n(static_cast<T>(_act_info.a()), ExactTagType{}); |
| 273 | const auto vb = wrapper::vdup_n(static_cast<T>(_act_info.b()), ExactTagType{}); |
| 274 | const auto a = static_cast<T>(_act_info.a()); |
| 275 | const auto b = static_cast<T>(_act_info.b()); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 276 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 277 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 278 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 279 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 280 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 281 | wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp; |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 282 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 283 | // Compute S elements per iteration |
| 284 | int x = window_start_x; |
| 285 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 286 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 287 | const auto vin = wrapper::vloadq(input_ptr + x); |
| 288 | switch(act) |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 289 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 290 | case ActivationFunction::ABS: |
| 291 | tmp = wrapper::vabs(vin); |
| 292 | break; |
| 293 | case ActivationFunction::LINEAR: |
| 294 | tmp = wrapper::vmla(vb, va, vin); |
| 295 | break; |
| 296 | case ActivationFunction::LOGISTIC: |
| 297 | tmp = wrapper::vinv(wrapper::vadd(const_1, wrapper::vexpq(wrapper::vneg(vin)))); |
| 298 | break; |
| 299 | case ActivationFunction::RELU: |
| 300 | tmp = wrapper::vmax(const_0, vin); |
| 301 | break; |
| 302 | case ActivationFunction::BOUNDED_RELU: |
| 303 | tmp = wrapper::vmin(va, wrapper::vmax(const_0, vin)); |
| 304 | break; |
| 305 | case ActivationFunction::LU_BOUNDED_RELU: |
| 306 | tmp = wrapper::vmin(va, wrapper::vmax(vb, vin)); |
| 307 | break; |
| 308 | case ActivationFunction::LEAKY_RELU: |
| 309 | tmp = wrapper::vbsl(wrapper::vcgt(vin, const_0), vin, wrapper::vmul(va, vin)); |
| 310 | break; |
| 311 | case ActivationFunction::SOFT_RELU: |
| 312 | tmp = wrapper::vlog(wrapper::vadd(const_1, wrapper::vexpq(vin))); |
| 313 | break; |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 314 | case ActivationFunction::ELU: |
| 315 | tmp = wrapper::vbsl(wrapper::vcge(vin, const_0), vin, wrapper::vmul(va, wrapper::vsub(wrapper::vexpq(vin), const_1))); |
| 316 | break; |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 317 | case ActivationFunction::SQRT: |
SiCong Li | a32e2ae | 2020-06-08 17:30:51 +0100 | [diff] [blame] | 318 | tmp = wrapper::vinv(wrapper::vinvsqrt(wrapper::vadd(vin, delta))); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 319 | break; |
| 320 | case ActivationFunction::SQUARE: |
| 321 | tmp = wrapper::vmul(vin, vin); |
| 322 | break; |
| 323 | case ActivationFunction::TANH: |
| 324 | tmp = wrapper::vmul(va, wrapper::vtanh(wrapper::vmul(vb, vin))); |
| 325 | break; |
Usama Arif | 80e55db | 2019-05-14 17:48:47 +0100 | [diff] [blame] | 326 | case ActivationFunction::IDENTITY: |
| 327 | tmp = vin; |
| 328 | break; |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 329 | case ActivationFunction::HARD_SWISH: |
| 330 | tmp = wrapper::vmul(vin, wrapper::vmul(const_inv_6, wrapper::vmin(const_6, wrapper::vmax(const_0, wrapper::vadd(vin, const_3))))); |
| 331 | break; |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 332 | default: |
| 333 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
Georgios Pinitas | 3463a8b | 2018-08-23 13:11:53 +0100 | [diff] [blame] | 334 | } |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 335 | wrapper::vstore(output_ptr + x, tmp); |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 338 | // Compute left-over elements |
| 339 | for(; x < window_end_x; ++x) |
Georgios Pinitas | f525eab | 2018-01-30 14:47:39 +0000 | [diff] [blame] | 340 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 341 | const T in = *(reinterpret_cast<const T *>(input_ptr + x)); |
| 342 | T tmp; |
| 343 | switch(act) |
Georgios Pinitas | f525eab | 2018-01-30 14:47:39 +0000 | [diff] [blame] | 344 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 345 | case ActivationFunction::ABS: |
| 346 | tmp = std::abs(in); |
| 347 | break; |
| 348 | case ActivationFunction::LINEAR: |
| 349 | tmp = a * in + b; |
| 350 | break; |
| 351 | case ActivationFunction::LOGISTIC: |
| 352 | tmp = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-in)); |
| 353 | break; |
| 354 | case ActivationFunction::RELU: |
| 355 | tmp = std::max<T>(static_cast<T>(0), in); |
| 356 | break; |
| 357 | case ActivationFunction::BOUNDED_RELU: |
| 358 | tmp = std::min<T>(a, std::max(static_cast<T>(0), in)); |
| 359 | break; |
| 360 | case ActivationFunction::LU_BOUNDED_RELU: |
| 361 | tmp = std::min<T>(a, std::max<T>(b, in)); |
| 362 | break; |
| 363 | case ActivationFunction::LEAKY_RELU: |
| 364 | tmp = (in > 0) ? in : a * in; |
| 365 | break; |
| 366 | case ActivationFunction::SOFT_RELU: |
| 367 | tmp = std::log(static_cast<T>(1) + std::exp(in)); |
| 368 | break; |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 369 | case ActivationFunction::ELU: |
| 370 | tmp = (in >= 0) ? in : a * (std::exp(in) - 1); |
| 371 | break; |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 372 | case ActivationFunction::SQRT: |
| 373 | tmp = std::sqrt(in); |
| 374 | break; |
| 375 | case ActivationFunction::SQUARE: |
| 376 | tmp = in * in; |
| 377 | break; |
| 378 | case ActivationFunction::TANH: |
| 379 | tmp = a * std::tanh(b * in); |
| 380 | break; |
Usama Arif | 80e55db | 2019-05-14 17:48:47 +0100 | [diff] [blame] | 381 | case ActivationFunction::IDENTITY: |
| 382 | tmp = in; |
| 383 | break; |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 384 | case ActivationFunction::HARD_SWISH: |
| 385 | tmp = in * ((std::min(std::max((in + 3), 0.0f), 6.0f)) * 0.166666667f); |
| 386 | break; |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 387 | default: |
| 388 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
Georgios Pinitas | f525eab | 2018-01-30 14:47:39 +0000 | [diff] [blame] | 389 | } |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 390 | *(output_ptr + x) = tmp; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 391 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 392 | }, |
| 393 | input, output); |
| 394 | } |
| 395 | |
| 396 | template <ActivationLayerInfo::ActivationFunction F, typename T> |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 397 | typename std::enable_if<std::is_same<T, qasymm8_t>::value, void>::type NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window) |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 398 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 399 | const int window_step_x = 16 / sizeof(T); |
| 400 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 401 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 402 | const ActivationFunction act = F; |
| 403 | |
| 404 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 405 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 406 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 407 | Iterator input(src, win_collapsed); |
| 408 | Iterator output(dst, win_collapsed); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 409 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 410 | const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform(); |
| 411 | const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform(); |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 412 | const qasymm8x16_t va = vdupq_n_u8(quantize_qasymm8(_act_info.a(), qi_in)); |
| 413 | const qasymm8x16_t vb = vdupq_n_u8(quantize_qasymm8(_act_info.b(), qi_in)); |
| 414 | const qasymm8_t a = quantize_qasymm8(_act_info.a(), qi_in); |
| 415 | const qasymm8_t b = quantize_qasymm8(_act_info.b(), qi_in); |
| 416 | const qasymm8_t const_0 = quantize_qasymm8(0.f, qi_in); |
| 417 | const qasymm8x16_t vconst_0 = vdupq_n_u8(const_0); |
| 418 | const auto vconst_1 = vdupq_n_f32(1.f); |
| 419 | const float32x4_t va_f32 = vdupq_n_f32(_act_info.a()); |
| 420 | const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b()); |
| 421 | const float a_f32 = _act_info.a(); |
| 422 | const float b_f32 = _act_info.b(); |
| 423 | const auto const_6_f32 = vdupq_n_f32(6.f); |
| 424 | const auto const_0_f32 = vdupq_n_f32(0.f); |
| 425 | const auto const_3_f32 = vdupq_n_f32(3.f); |
| 426 | const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f); |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 427 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 428 | // Initialise scale/offset for re-quantization |
| 429 | float s = qi_in.scale / qi_out.scale; |
| 430 | float o = -qi_in.offset * s + qi_out.offset; |
| 431 | float32x4_t vs = vdupq_n_f32(s); |
| 432 | float32x4_t vo = vdupq_n_f32(o); |
| 433 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 434 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 435 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 436 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 437 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 438 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 439 | wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp; |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 440 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 441 | // Compute S elements per iteration |
| 442 | int x = window_start_x; |
| 443 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 444 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 445 | const auto vin = wrapper::vloadq(input_ptr + x); |
| 446 | if(act == ActivationFunction::RELU) |
| 447 | { |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 448 | // Perform activation |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 449 | tmp = vmaxq_u8(vconst_0, vin); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 450 | // Re-quantize to new output space |
| 451 | tmp = vmlaq_qasymm8(tmp, vs, vo); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 452 | } |
| 453 | else if(act == ActivationFunction::BOUNDED_RELU) |
| 454 | { |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 455 | // Perform activation |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 456 | tmp = vminq_u8(va, vmaxq_u8(vconst_0, vin)); |
Michele Di Giorgio | dde3ad9 | 2018-01-23 16:55:24 +0000 | [diff] [blame] | 457 | // Re-quantize to new output space |
| 458 | tmp = vmlaq_qasymm8(tmp, vs, vo); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 459 | } |
| 460 | else if(act == ActivationFunction::LU_BOUNDED_RELU) |
| 461 | { |
| 462 | // Perform activation |
| 463 | tmp = vminq_u8(va, vmaxq_u8(vb, vin)); |
| 464 | // Re-quantize to new output space |
| 465 | tmp = vmlaq_qasymm8(tmp, vs, vo); |
| 466 | } |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 467 | else if(act == ActivationFunction::LOGISTIC) |
| 468 | { |
Isabella Gottardi | f7a3bf2 | 2019-03-15 14:58:24 +0000 | [diff] [blame] | 469 | // De-quantize |
| 470 | const auto vin_deq = vdequantize(vin, qi_in); |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 471 | // Perform activation |
Isabella Gottardi | f7a3bf2 | 2019-03-15 14:58:24 +0000 | [diff] [blame] | 472 | const float32x4x4_t tmp_dep = |
| 473 | { |
| 474 | { |
| 475 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))), |
| 476 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))), |
| 477 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))), |
| 478 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))), |
| 479 | } |
| 480 | }; |
| 481 | // Re-quantize to new output space |
| 482 | tmp = vquantize(tmp_dep, qi_out); |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 483 | } |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 484 | else if(act == ActivationFunction::TANH) |
| 485 | { |
| 486 | // De-quantize |
| 487 | const auto vin_deq = vdequantize(vin, qi_in); |
| 488 | // Perform activation |
| 489 | const float32x4x4_t tmp_dep = |
| 490 | { |
| 491 | { |
| 492 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))), |
| 493 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))), |
| 494 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))), |
| 495 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))), |
| 496 | } |
| 497 | }; |
| 498 | // Re-quantize to new output space |
| 499 | tmp = vquantize(tmp_dep, qi_out); |
| 500 | } |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 501 | else if(act == ActivationFunction::HARD_SWISH) |
| 502 | { |
| 503 | // De-quantize |
| 504 | const auto vin_deq = vdequantize(vin, qi_in); |
| 505 | // Perform activation |
| 506 | const float32x4x4_t tmp_dep = |
| 507 | { |
| 508 | { |
| 509 | wrapper::vmul(vin_deq.val[0], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[0], const_3_f32))))), |
| 510 | wrapper::vmul(vin_deq.val[1], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[1], const_3_f32))))), |
| 511 | wrapper::vmul(vin_deq.val[2], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[2], const_3_f32))))), |
| 512 | wrapper::vmul(vin_deq.val[3], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[3], const_3_f32))))), |
| 513 | } |
| 514 | }; |
| 515 | // Re-quantize to new output space |
| 516 | tmp = vquantize(tmp_dep, qi_out); |
| 517 | } |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 518 | else |
| 519 | { |
| 520 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 521 | } |
| 522 | wrapper::vstore(output_ptr + x, tmp); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 525 | // Compute left-over elements |
| 526 | for(; x < window_end_x; ++x) |
| 527 | { |
| 528 | T in = *(reinterpret_cast<const T *>(input_ptr + x)); |
| 529 | T tmp; |
| 530 | if(act == ActivationFunction::RELU) |
| 531 | { |
| 532 | tmp = std::max(const_0, in); |
Michele Di Giorgio | b70770e | 2020-04-22 12:26:10 +0100 | [diff] [blame] | 533 | tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 534 | } |
| 535 | else if(act == ActivationFunction::BOUNDED_RELU) |
| 536 | { |
| 537 | tmp = std::min(a, std::max(const_0, in)); |
Michele Di Giorgio | b70770e | 2020-04-22 12:26:10 +0100 | [diff] [blame] | 538 | tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 539 | } |
| 540 | else if(act == ActivationFunction::LU_BOUNDED_RELU) |
| 541 | { |
| 542 | tmp = std::min(a, std::max(b, in)); |
Michele Di Giorgio | b70770e | 2020-04-22 12:26:10 +0100 | [diff] [blame] | 543 | tmp = utility::clamp<int32_t, qasymm8_t>(tmp * s + o); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 544 | } |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 545 | else if(act == ActivationFunction::LOGISTIC) |
| 546 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 547 | float tmp_f = dequantize_qasymm8(in, qi_in); |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 548 | tmp_f = 1.f / (1.f + std::exp(-tmp_f)); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 549 | tmp = quantize_qasymm8(tmp_f, qi_out); |
Isabella Gottardi | 03bb550 | 2019-01-31 17:45:07 +0000 | [diff] [blame] | 550 | } |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 551 | else if(act == ActivationFunction::TANH) |
| 552 | { |
| 553 | float tmp_f = dequantize_qasymm8(in, qi_in); |
| 554 | tmp_f = a_f32 * std::tanh(b_f32 * tmp_f); |
| 555 | tmp = quantize_qasymm8(tmp_f, qi_out); |
| 556 | } |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 557 | else if(act == ActivationFunction::HARD_SWISH) |
| 558 | { |
| 559 | float tmp_f = dequantize_qasymm8(in, qi_in); |
| 560 | tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f); |
| 561 | tmp = quantize_qasymm8(tmp_f, qi_out); |
| 562 | } |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 563 | else |
| 564 | { |
| 565 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 566 | } |
| 567 | *(output_ptr + x) = tmp; |
| 568 | } |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 569 | }, |
| 570 | input, output); |
| 571 | } |
| 572 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 573 | template <ActivationLayerInfo::ActivationFunction F, typename T> |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 574 | typename std::enable_if<std::is_same<T, qasymm8_signed_t>::value, void>::type NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window) |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 575 | { |
| 576 | const int window_step_x = 16 / sizeof(T); |
| 577 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 578 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 579 | const ActivationFunction act = F; |
| 580 | |
| 581 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 582 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 583 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 584 | Iterator input(src, win_collapsed); |
| 585 | Iterator output(dst, win_collapsed); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 586 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 587 | const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform(); |
| 588 | const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform(); |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 589 | const qasymm8x16_signed_t va = vdupq_n_s8(quantize_qasymm8_signed(_act_info.a(), qi_in)); |
| 590 | const qasymm8x16_signed_t vb = vdupq_n_s8(quantize_qasymm8_signed(_act_info.b(), qi_in)); |
| 591 | const qasymm8_signed_t a = quantize_qasymm8_signed(_act_info.a(), qi_in); |
| 592 | const qasymm8_signed_t b = quantize_qasymm8_signed(_act_info.b(), qi_in); |
| 593 | const qasymm8_signed_t const_0 = quantize_qasymm8_signed(0.f, qi_in); |
| 594 | const qasymm8x16_signed_t vconst_0 = vdupq_n_s8(const_0); |
| 595 | const auto vconst_1 = vdupq_n_f32(1.f); |
| 596 | const float32x4_t va_f32 = vdupq_n_f32(_act_info.a()); |
| 597 | const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b()); |
| 598 | const float a_f32 = _act_info.a(); |
| 599 | const float b_f32 = _act_info.b(); |
| 600 | const auto const_6_f32 = vdupq_n_f32(6.f); |
| 601 | const auto const_0_f32 = vdupq_n_f32(0.f); |
| 602 | const auto const_3_f32 = vdupq_n_f32(3.f); |
| 603 | const auto const_inv_6_f32 = vdupq_n_f32(0.166666667f); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 604 | |
| 605 | // Initialise scale/offset for re-quantization |
| 606 | float s = qi_in.scale / qi_out.scale; |
| 607 | float o = -qi_in.offset * s + qi_out.offset; |
| 608 | float32x4_t vs = vdupq_n_f32(s); |
| 609 | float32x4_t vo = vdupq_n_f32(o); |
| 610 | |
| 611 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 612 | { |
| 613 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 614 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
| 615 | |
| 616 | wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp; |
| 617 | |
| 618 | // Compute S elements per iteration |
| 619 | int x = window_start_x; |
| 620 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 621 | { |
| 622 | const auto vin = wrapper::vloadq(input_ptr + x); |
| 623 | if(act == ActivationFunction::RELU) |
| 624 | { |
| 625 | // Perform activation |
| 626 | tmp = vmaxq_s8(vconst_0, vin); |
| 627 | // Re-quantize to new output space |
| 628 | tmp = vmlaq_qasymm8_signed(tmp, vs, vo); |
| 629 | } |
| 630 | else if(act == ActivationFunction::BOUNDED_RELU) |
| 631 | { |
| 632 | // Perform activation |
| 633 | tmp = vminq_s8(va, vmaxq_s8(vconst_0, vin)); |
| 634 | // Re-quantize to new output space |
| 635 | tmp = vmlaq_qasymm8_signed(tmp, vs, vo); |
| 636 | } |
| 637 | else if(act == ActivationFunction::LU_BOUNDED_RELU) |
| 638 | { |
| 639 | // Perform activation |
| 640 | tmp = vminq_s8(va, vmaxq_s8(vb, vin)); |
| 641 | // Re-quantize to new output space |
| 642 | tmp = vmlaq_qasymm8_signed(tmp, vs, vo); |
| 643 | } |
| 644 | else if(act == ActivationFunction::LOGISTIC) |
| 645 | { |
| 646 | // De-quantize |
| 647 | const auto vin_deq = vdequantize(vin, qi_in); |
| 648 | // Perform activation |
| 649 | const float32x4x4_t tmp_dep = |
| 650 | { |
| 651 | { |
| 652 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))), |
| 653 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))), |
| 654 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[2])))), |
| 655 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[3])))), |
| 656 | } |
| 657 | }; |
| 658 | // Re-quantize to new output space |
| 659 | tmp = vquantize_signed(tmp_dep, qi_out); |
| 660 | } |
| 661 | else if(act == ActivationFunction::TANH) |
| 662 | { |
| 663 | // De-quantize |
| 664 | const auto vin_deq = vdequantize(vin, qi_in); |
| 665 | // Perform activation |
| 666 | const float32x4x4_t tmp_dep = |
| 667 | { |
| 668 | { |
| 669 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))), |
| 670 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))), |
| 671 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[2], vb_f32))), |
| 672 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[3], vb_f32))), |
| 673 | } |
| 674 | }; |
| 675 | // Re-quantize to new output space |
| 676 | tmp = vquantize_signed(tmp_dep, qi_out); |
| 677 | } |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 678 | else if(act == ActivationFunction::HARD_SWISH) |
| 679 | { |
| 680 | // De-quantize |
| 681 | const auto vin_deq = vdequantize(vin, qi_in); |
| 682 | // Perform activation |
| 683 | const float32x4x4_t tmp_dep = |
| 684 | { |
| 685 | { |
| 686 | wrapper::vmul(vin_deq.val[0], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[0], const_3_f32))))), |
| 687 | wrapper::vmul(vin_deq.val[1], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[1], const_3_f32))))), |
| 688 | wrapper::vmul(vin_deq.val[2], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[2], const_3_f32))))), |
| 689 | wrapper::vmul(vin_deq.val[3], wrapper::vmul(const_inv_6_f32, wrapper::vmin(const_6_f32, wrapper::vmax(const_0_f32, wrapper::vadd(vin_deq.val[3], const_3_f32))))), |
| 690 | } |
| 691 | }; |
| 692 | // Re-quantize to new output space |
| 693 | tmp = vquantize_signed(tmp_dep, qi_out); |
| 694 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 695 | else |
| 696 | { |
| 697 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 698 | } |
| 699 | wrapper::vstore(output_ptr + x, tmp); |
| 700 | } |
| 701 | |
| 702 | // Compute left-over elements |
| 703 | for(; x < window_end_x; ++x) |
| 704 | { |
| 705 | T in = *(reinterpret_cast<const T *>(input_ptr + x)); |
| 706 | T tmp; |
| 707 | if(act == ActivationFunction::RELU) |
| 708 | { |
| 709 | tmp = std::max(const_0, in); |
Michele Di Giorgio | b70770e | 2020-04-22 12:26:10 +0100 | [diff] [blame] | 710 | tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 711 | } |
| 712 | else if(act == ActivationFunction::BOUNDED_RELU) |
| 713 | { |
| 714 | tmp = std::min(a, std::max(const_0, in)); |
Michele Di Giorgio | b70770e | 2020-04-22 12:26:10 +0100 | [diff] [blame] | 715 | tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 716 | } |
| 717 | else if(act == ActivationFunction::LU_BOUNDED_RELU) |
| 718 | { |
| 719 | tmp = std::min(a, std::max(b, in)); |
Michele Di Giorgio | b70770e | 2020-04-22 12:26:10 +0100 | [diff] [blame] | 720 | tmp = utility::clamp<int32_t, qasymm8_signed_t>(tmp * s + o); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 721 | } |
| 722 | else if(act == ActivationFunction::LOGISTIC) |
| 723 | { |
| 724 | float tmp_f = dequantize_qasymm8_signed(in, qi_in); |
| 725 | tmp_f = 1.f / (1.f + std::exp(-tmp_f)); |
| 726 | tmp = quantize_qasymm8_signed(tmp_f, qi_out); |
| 727 | } |
| 728 | else if(act == ActivationFunction::TANH) |
| 729 | { |
| 730 | float tmp_f = dequantize_qasymm8_signed(in, qi_in); |
| 731 | tmp_f = a_f32 * std::tanh(b_f32 * tmp_f); |
| 732 | tmp = quantize_qasymm8_signed(tmp_f, qi_out); |
| 733 | } |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 734 | else if(act == ActivationFunction::HARD_SWISH) |
| 735 | { |
| 736 | float tmp_f = dequantize_qasymm8_signed(in, qi_in); |
| 737 | tmp_f = tmp_f * ((std::min(std::max((tmp_f + 3), 0.0f), 6.0f)) * 0.166666667f); |
| 738 | tmp = quantize_qasymm8_signed(tmp_f, qi_out); |
| 739 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 740 | else |
| 741 | { |
| 742 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 743 | } |
| 744 | *(output_ptr + x) = tmp; |
| 745 | } |
| 746 | }, |
| 747 | input, output); |
| 748 | } |
| 749 | |
| 750 | template <ActivationLayerInfo::ActivationFunction F, typename T> |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 751 | typename std::enable_if<std::is_same<T, qsymm16_t>::value, void>::type NEActivationLayerKernel::activation(const ITensor *src, ITensor *dst, const Window &window) |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 752 | { |
| 753 | const int window_step_x = 16 / sizeof(T); |
| 754 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 755 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 756 | const ActivationFunction act = F; |
| 757 | |
| 758 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 759 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 760 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 761 | Iterator input(src, win_collapsed); |
| 762 | Iterator output(dst, win_collapsed); |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 763 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 764 | const UniformQuantizationInfo qi_in = src->info()->quantization_info().uniform(); |
| 765 | const UniformQuantizationInfo qi_out = dst->info()->quantization_info().uniform(); |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 766 | const auto vconst_1 = vdupq_n_f32(1.f); |
| 767 | const float32x4_t va_f32 = vdupq_n_f32(_act_info.a()); |
| 768 | const float32x4_t vb_f32 = vdupq_n_f32(_act_info.b()); |
| 769 | const float a_f32 = _act_info.a(); |
| 770 | const float b_f32 = _act_info.b(); |
| 771 | |
| 772 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 773 | { |
| 774 | const auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 775 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
| 776 | |
| 777 | wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128> tmp; |
| 778 | ARM_COMPUTE_UNUSED(tmp); |
| 779 | |
| 780 | // Compute S elements per iteration |
| 781 | int x = window_start_x; |
| 782 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 783 | { |
| 784 | const auto vin = wrapper::vloadq(input_ptr + x); |
| 785 | if(act == ActivationFunction::LOGISTIC) |
| 786 | { |
| 787 | // De-quantize |
| 788 | const auto vin_deq = vdequantize_int16(vin, qi_in.scale); |
| 789 | // Perform activation |
| 790 | const float32x4x2_t tmp_dep = |
| 791 | { |
| 792 | { |
| 793 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[0])))), |
| 794 | wrapper::vdiv(vconst_1, wrapper::vadd(vconst_1, wrapper::vexpq(wrapper::vneg(vin_deq.val[1])))), |
| 795 | } |
| 796 | }; |
| 797 | // Re-quantize to new output space |
| 798 | tmp = vquantize_int16(tmp_dep, qi_out.scale); |
| 799 | } |
| 800 | else if(act == ActivationFunction::TANH) |
| 801 | { |
| 802 | // De-quantize |
| 803 | const auto vin_deq = vdequantize_int16(vin, qi_in.scale); |
| 804 | // Perform activation |
| 805 | const float32x4x2_t tmp_dep = |
| 806 | { |
| 807 | { |
| 808 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[0], vb_f32))), |
| 809 | wrapper::vmul(va_f32, wrapper::vtanh(wrapper::vmul(vin_deq.val[1], vb_f32))), |
| 810 | } |
| 811 | }; |
| 812 | // Re-quantize to new output space |
| 813 | tmp = vquantize_int16(tmp_dep, qi_out.scale); |
| 814 | } |
| 815 | else |
| 816 | { |
| 817 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 818 | } |
| 819 | wrapper::vstore(output_ptr + x, tmp); |
| 820 | } |
| 821 | |
| 822 | // Compute left-over elements |
| 823 | for(; x < window_end_x; ++x) |
| 824 | { |
| 825 | T in = *(reinterpret_cast<const T *>(input_ptr + x)); |
| 826 | T tmp; |
| 827 | if(act == ActivationFunction::LOGISTIC) |
| 828 | { |
| 829 | float tmp_f = dequantize_qsymm16(in, qi_in.scale); |
| 830 | tmp_f = 1.f / (1.f + std::exp(-tmp_f)); |
| 831 | tmp = quantize_qsymm16(tmp_f, qi_out); |
| 832 | } |
| 833 | else if(act == ActivationFunction::TANH) |
| 834 | { |
| 835 | float tmp_f = dequantize_qsymm16(in, qi_in.scale); |
| 836 | tmp_f = a_f32 * std::tanh(b_f32 * tmp_f); |
| 837 | tmp = quantize_qsymm16(tmp_f, qi_out); |
| 838 | } |
| 839 | else |
| 840 | { |
| 841 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 842 | } |
| 843 | *(output_ptr + x) = tmp; |
| 844 | } |
| 845 | }, |
| 846 | input, output); |
| 847 | } |
| 848 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 849 | Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
| 850 | { |
| 851 | ARM_COMPUTE_UNUSED(act_info); |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 852 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 853 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first); |
| 854 | |
| 855 | return Status{}; |
| 856 | } |
| 857 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 858 | void NEActivationLayerKernel::run_op(const std::vector<InputTensor> &inputs, |
| 859 | const std::vector<OutputTensor> &outputs, |
| 860 | const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 861 | { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 862 | // Early exit on disabled activation |
| 863 | if(!_act_info.enabled()) |
| 864 | { |
| 865 | return; |
| 866 | } |
| 867 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 868 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 869 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 870 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 871 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 872 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame^] | 873 | ARM_COMPUTE_ERROR_ON(inputs.empty() || outputs.empty()); |
| 874 | |
| 875 | (this->*_func)(inputs[0].tensor, outputs[0].tensor, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 876 | } |