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