Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame^] | 24 | #include "src/core/NEON/kernels/NEActivationLayerKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/core/Utils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "src/core/CPP/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 33 | #include "src/core/NEON/kernels/activation/impl/list.h" |
| 34 | #include "src/core/common/Registrars.h" |
| 35 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 36 | #include <set> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 42 | struct ActivationSelectorData |
| 43 | { |
| 44 | DataType dt; |
| 45 | }; |
| 46 | |
| 47 | using ActivationSelectorPtr = std::add_pointer<bool(const ActivationSelectorData &data)>::type; |
| 48 | using ActivationKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const ActivationLayerInfo &, const Window &)>::type; |
| 49 | |
| 50 | struct ActivationKernel |
| 51 | { |
| 52 | const char *name; |
| 53 | const ActivationSelectorPtr is_selected; |
| 54 | ActivationKernelPtr ukernel; |
| 55 | }; |
| 56 | |
| 57 | static const ActivationKernel available_kernels[] = |
| 58 | { |
| 59 | { |
| 60 | "fp16_neon_activation", |
| 61 | [](const ActivationSelectorData & data) { return data.dt == DataType::F16; }, |
| 62 | REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_activation) |
| 63 | }, |
| 64 | { |
| 65 | "fp32_neon_activation", |
| 66 | [](const ActivationSelectorData & data) { return data.dt == DataType::F32; }, |
| 67 | REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_activation) |
| 68 | }, |
| 69 | { |
| 70 | "qasymm8_neon_activation", |
| 71 | [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8; }, |
| 72 | REGISTER_QASYMM8_NEON(arm_compute::cpu::qasymm8_neon_activation) |
| 73 | }, |
| 74 | { |
| 75 | "qasymm8_signed_neon_activation", |
| 76 | [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; }, |
| 77 | REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::qasymm8_signed_neon_activation) |
| 78 | }, |
| 79 | { |
| 80 | "qsymm16_neon_activation", |
| 81 | [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; }, |
| 82 | REGISTER_QSYMM16_NEON(arm_compute::cpu::qsymm16_neon_activation) |
| 83 | }, |
| 84 | }; |
| 85 | |
| 86 | const ActivationKernel *get_implementation(const ActivationSelectorData &data) |
| 87 | { |
| 88 | for(const auto &uk : available_kernels) |
| 89 | { |
| 90 | if(uk.is_selected(data)) |
| 91 | { |
| 92 | return &uk; |
| 93 | } |
| 94 | } |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 98 | 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] | 99 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 101 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 102 | |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 103 | const auto *uk = get_implementation(ActivationSelectorData{ input->data_type() }); |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
| 105 | |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 106 | const static std::set<ActivationLayerInfo::ActivationFunction> qasymm8_supported_activations = |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 107 | { |
| 108 | ActivationLayerInfo::ActivationFunction::RELU, |
| 109 | ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, |
| 110 | ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, |
| 111 | ActivationLayerInfo::ActivationFunction::LOGISTIC, |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 112 | ActivationLayerInfo::ActivationFunction::TANH, |
| 113 | ActivationLayerInfo::ActivationFunction::HARD_SWISH |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 114 | }; |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 115 | const static std::set<ActivationLayerInfo::ActivationFunction> qsymm16_supported_activations = |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 116 | { |
| 117 | ActivationLayerInfo::ActivationFunction::LOGISTIC, |
morgolock | aa85cdf | 2020-02-28 15:38:28 +0000 | [diff] [blame] | 118 | ActivationLayerInfo::ActivationFunction::TANH, |
| 119 | ActivationLayerInfo::ActivationFunction::HARD_SWISH |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 120 | }; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 121 | const DataType data_type = input->data_type(); |
| 122 | const QuantizationInfo &oq_info = (output != nullptr) ? output->quantization_info() : input->quantization_info(); |
| 123 | const ActivationLayerInfo::ActivationFunction f_act = activation_info.activation(); |
| 124 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 125 | 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] | 126 | "For QASYMM8 only tanh, logistic, relu and lower/upper bounded relu are supported"); |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 127 | |
| 128 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_symmetric(data_type) && (qsymm16_supported_activations.count(f_act) == 0), |
| 129 | "For QSYMM16 only tanh and logistic are supported"); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 130 | ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::TANH) |
| 131 | && (oq_info != QuantizationInfo(1.f / 128.f, 128))); |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON((data_type == DataType::QASYMM8 || data_type == DataType::QASYMM16) && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) |
| 133 | && (oq_info != QuantizationInfo(1.f / 256.f, 0))); |
| 134 | |
| 135 | ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0))); |
| 136 | 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] | 137 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 138 | 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))); |
| 139 | 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))); |
| 140 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 141 | // Checks performed when output is configured |
| 142 | if((output != nullptr) && (output->total_size() != 0)) |
| 143 | { |
| 144 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 145 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return Status{}; |
| 149 | } |
| 150 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame] | 151 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 152 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 153 | // Configure kernel window |
| 154 | Window win = calculate_max_window(*input, Steps()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 155 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 156 | if(output != nullptr) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 157 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 158 | // Output auto inizialitation if not yet initialized |
| 159 | auto_init_if_empty(*output, *input->clone()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 160 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 161 | // NEActivationLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
| 162 | Coordinates coord; |
| 163 | coord.set_num_dimensions(output->num_dimensions()); |
| 164 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 167 | return std::make_pair(Status{}, win); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 168 | } |
| 169 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | |
| 171 | NEActivationLayerKernel::NEActivationLayerKernel() |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 172 | : _act_info() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 173 | { |
| 174 | } |
| 175 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame] | 176 | void NEActivationLayerKernel::configure(const ITensorInfo *input, ITensorInfo *output, ActivationLayerInfo activation_info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | { |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame] | 178 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 180 | _act_info = activation_info; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame] | 182 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, activation_info)); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 183 | |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 184 | // Configure kernel window |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame] | 185 | auto win_config = validate_and_configure_window(input, output); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 186 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 187 | ICPPKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 190 | Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info) |
| 191 | { |
| 192 | ARM_COMPUTE_UNUSED(act_info); |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 193 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 194 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (output != nullptr) ? output->clone().get() : nullptr).first); |
| 195 | |
| 196 | return Status{}; |
| 197 | } |
| 198 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 199 | void NEActivationLayerKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 200 | { |
Georgios Pinitas | 48b3ef8 | 2019-10-14 19:03:09 +0100 | [diff] [blame] | 201 | // Early exit on disabled activation |
| 202 | if(!_act_info.enabled()) |
| 203 | { |
| 204 | return; |
| 205 | } |
| 206 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 207 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 208 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 209 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 211 | ARM_COMPUTE_ERROR_ON(tensors.empty()); |
Georgios Pinitas | 1fd2c80 | 2020-06-16 17:44:46 +0100 | [diff] [blame] | 212 | |
Michalis Spyrou | c4d4555 | 2020-10-19 12:41:30 +0100 | [diff] [blame] | 213 | const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 214 | ITensor *dst = tensors.get_tensor(TensorType::ACL_DST); |
| 215 | |
| 216 | const auto *uk = get_implementation(ActivationSelectorData{ src->info()->data_type() }); |
| 217 | |
| 218 | uk->ukernel(src, dst, _act_info, window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | } |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 220 | } // namespace arm_compute |