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