• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
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 "src/core/NEON/kernels/NEActivationLayerKernel.h"
25 
26 #include "arm_compute/core/ITensor.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "arm_compute/core/Utils.h"
29 #include "src/core/CPP/Validate.h"
30 #include "src/core/helpers/AutoConfiguration.h"
31 #include "src/core/helpers/WindowHelpers.h"
32 
33 #include "src/core/NEON/kernels/activation/impl/list.h"
34 #include "src/core/common/Registrars.h"
35 
36 #include <set>
37 
38 namespace arm_compute
39 {
40 namespace
41 {
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",
__anonf1cd04230202() 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",
__anonf1cd04230302() 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",
__anonf1cd04230402() 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",
__anonf1cd04230502() 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",
__anonf1cd04230602() 81         [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; },
82         REGISTER_QSYMM16_NEON(arm_compute::cpu::qsymm16_neon_activation)
83     },
84 };
85 
get_implementation(const ActivationSelectorData & data)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 
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,const ActivationLayerInfo & activation_info)98 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &activation_info)
99 {
100     ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
101     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::QSYMM16, DataType::F16, DataType::F32);
102 
103     const auto *uk = get_implementation(ActivationSelectorData{ input->data_type() });
104     ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
105 
106     const static std::set<ActivationLayerInfo::ActivationFunction> qasymm8_supported_activations =
107     {
108         ActivationLayerInfo::ActivationFunction::RELU,
109         ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
110         ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
111         ActivationLayerInfo::ActivationFunction::LOGISTIC,
112         ActivationLayerInfo::ActivationFunction::TANH,
113         ActivationLayerInfo::ActivationFunction::HARD_SWISH
114     };
115     const static std::set<ActivationLayerInfo::ActivationFunction> qsymm16_supported_activations =
116     {
117         ActivationLayerInfo::ActivationFunction::LOGISTIC,
118         ActivationLayerInfo::ActivationFunction::TANH,
119         ActivationLayerInfo::ActivationFunction::HARD_SWISH
120     };
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 
125     ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_asymmetric(data_type) && (qasymm8_supported_activations.count(f_act) == 0),
126                                     "For QASYMM8 only tanh, logistic, relu and lower/upper bounded relu are supported");
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");
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)));
137 
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 
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);
146     }
147 
148     return Status{};
149 }
150 
validate_and_configure_window(const ITensorInfo * input,ITensorInfo * output)151 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input, ITensorInfo *output)
152 {
153     // Configure kernel window
154     Window win = calculate_max_window(*input, Steps());
155 
156     if(output != nullptr)
157     {
158         // Output auto inizialitation if not yet initialized
159         auto_init_if_empty(*output, *input->clone());
160 
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()));
165     }
166 
167     return std::make_pair(Status{}, win);
168 }
169 } // namespace
170 
NEActivationLayerKernel()171 NEActivationLayerKernel::NEActivationLayerKernel()
172     : _act_info()
173 {
174 }
175 
configure(const ITensorInfo * input,ITensorInfo * output,ActivationLayerInfo activation_info)176 void NEActivationLayerKernel::configure(const ITensorInfo *input, ITensorInfo *output, ActivationLayerInfo activation_info)
177 {
178     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
179 
180     _act_info = activation_info;
181 
182     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output, activation_info));
183 
184     // Configure kernel window
185     auto win_config = validate_and_configure_window(input, output);
186     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
187     ICPPKernel::configure(win_config.second);
188 }
189 
validate(const ITensorInfo * input,const ITensorInfo * output,const ActivationLayerInfo & act_info)190 Status NEActivationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ActivationLayerInfo &act_info)
191 {
192     ARM_COMPUTE_UNUSED(act_info);
193     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, act_info));
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 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)199 void NEActivationLayerKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
200 {
201     // Early exit on disabled activation
202     if(!_act_info.enabled())
203     {
204         return;
205     }
206 
207     ARM_COMPUTE_UNUSED(info);
208     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
209     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
210 
211     ARM_COMPUTE_ERROR_ON(tensors.empty());
212 
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);
219 }
220 } // namespace arm_compute
221