blob: 51257cb96b8b5a3f7bef0ccaa33fbb5edb276b57 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEActivationLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/CPP/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
Michalis Spyrouc4d45552020-10-19 12:41:30 +010033#include "src/core/NEON/kernels/activation/impl/list.h"
34#include "src/core/common/Registrars.h"
35
Georgios Pinitas4b3fba12019-06-04 17:31:46 +010036#include <set>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +010038namespace arm_compute
39{
Michalis Spyrouafa5d812017-11-30 14:25:57 +000040namespace
41{
Michalis Spyrouc4d45552020-10-19 12:41:30 +010042struct ActivationSelectorData
43{
44 DataType dt;
45};
46
47using ActivationSelectorPtr = std::add_pointer<bool(const ActivationSelectorData &data)>::type;
48using ActivationKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const ActivationLayerInfo &, const Window &)>::type;
49
50struct ActivationKernel
51{
52 const char *name;
53 const ActivationSelectorPtr is_selected;
54 ActivationKernelPtr ukernel;
55};
56
57static 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
86const 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 Pinitas4b3fba12019-06-04 17:31:46 +010098Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &activation_info)
Michalis Spyrouafa5d812017-11-30 14:25:57 +000099{
Anthony Barbiereaefd002018-07-20 17:49:35 +0100100 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100101 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000102
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100103 const auto *uk = get_implementation(ActivationSelectorData{ input->data_type() });
104 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
105
morgolockaa85cdf2020-02-28 15:38:28 +0000106 const static std::set<ActivationLayerInfo::ActivationFunction> qasymm8_supported_activations =
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100107 {
108 ActivationLayerInfo::ActivationFunction::RELU,
109 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
110 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
111 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolockaa85cdf2020-02-28 15:38:28 +0000112 ActivationLayerInfo::ActivationFunction::TANH,
113 ActivationLayerInfo::ActivationFunction::HARD_SWISH
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100114 };
morgolockaa85cdf2020-02-28 15:38:28 +0000115 const static std::set<ActivationLayerInfo::ActivationFunction> qsymm16_supported_activations =
giuros01c9573f32019-06-20 10:30:17 +0100116 {
117 ActivationLayerInfo::ActivationFunction::LOGISTIC,
morgolockaa85cdf2020-02-28 15:38:28 +0000118 ActivationLayerInfo::ActivationFunction::TANH,
119 ActivationLayerInfo::ActivationFunction::HARD_SWISH
giuros01c9573f32019-06-20 10:30:17 +0100120 };
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100121 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
giuros01c9573f32019-06-20 10:30:17 +0100125 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_asymmetric(data_type) && (qasymm8_supported_activations.count(f_act) == 0),
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100126 "For QASYMM8 only tanh, logistic, relu and lower/upper bounded relu are supported");
giuros01c9573f32019-06-20 10:30:17 +0100127
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 Spyrou8d4d1b82019-11-28 11:31:23 +0000130 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 Pinitas4b3fba12019-06-04 17:31:46 +0100137
giuros01c9573f32019-06-20 10:30:17 +0100138 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 Spyrouafa5d812017-11-30 14:25:57 +0000141 // 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 Spyrouafa5d812017-11-30 14:25:57 +0000146 }
147
148 return Status{};
149}
150
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100151std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000152{
Georgios Pinitas5a594532018-12-03 14:30:05 +0000153 // Configure kernel window
154 Window win = calculate_max_window(*input, Steps());
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000155
Georgios Pinitas5a594532018-12-03 14:30:05 +0000156 if(output != nullptr)
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000157 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000158 // Output auto inizialitation if not yet initialized
159 auto_init_if_empty(*output, *input->clone());
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000160
Georgios Pinitas5a594532018-12-03 14:30:05 +0000161 // 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 Spyrouafa5d812017-11-30 14:25:57 +0000165 }
166
Georgios Pinitas5a594532018-12-03 14:30:05 +0000167 return std::make_pair(Status{}, win);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000168}
169} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170
171NEActivationLayerKernel::NEActivationLayerKernel()
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100172 : _act_info()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173{
174}
175
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100176void NEActivationLayerKernel::configure(const ITensorInfo *input, ITensorInfo *output, ActivationLayerInfo activation_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177{
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100178 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100180 _act_info = activation_info;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100182 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, activation_info));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000183
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100184 // Configure kernel window
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100185 auto win_config = validate_and_configure_window(input, output);
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000186 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
187 ICPPKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188}
189
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000190Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
191{
192 ARM_COMPUTE_UNUSED(act_info);
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100193 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000194 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 Pinitas0499dff2020-07-31 22:21:38 +0100199void NEActivationLayerKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200{
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100201 // Early exit on disabled activation
202 if(!_act_info.enabled())
203 {
204 return;
205 }
206
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100207 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Gian Marco Iodiceb30dcc52017-06-20 09:07:21 +0100209 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100211 ARM_COMPUTE_ERROR_ON(tensors.empty());
Georgios Pinitas1fd2c802020-06-16 17:44:46 +0100212
Michalis Spyrouc4d45552020-10-19 12:41:30 +0100213 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 Barbier6ff3b192017-09-04 18:44:23 +0100219}
Michele Di Giorgiof9b595a2020-07-03 13:34:52 +0100220} // namespace arm_compute