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