Compute Library
 21.08
CpuActivationKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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  */
25 
28 #include "arm_compute/core/Utils.h"
29 #include "src/core/CPP/Validate.h"
32 
35 
36 #include <array>
37 
38 namespace arm_compute
39 {
40 namespace cpu
41 {
42 namespace kernels
43 {
44 namespace
45 {
46 struct ActivationSelectorData
47 {
49  const CPUInfo &ci;
50 };
51 
54 
55 struct ActivationKernel
56 {
57  const char *name;
58  const ActivationSelectorPtr is_selected;
59  ActivationKernelPtr ukernel;
60 };
61 
62 static const ActivationKernel available_kernels[] =
63 {
64 #if defined(ARM_COMPUTE_ENABLE_SVE)
65  {
66  "sve_fp16_activation",
67  [](const ActivationSelectorData & data) { return data.dt == DataType::F16 && data.ci.has_sve(); },
69  },
70  {
71  "sve_fp32_activation",
72  [](const ActivationSelectorData & data) { return data.dt == DataType::F32 && data.ci.has_sve(); },
74  },
75 #endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
76 #if defined(ARM_COMPUTE_ENABLE_NEON)
77  {
78  "neon_fp16_activation",
79  [](const ActivationSelectorData & data) { return data.dt == DataType::F16; },
81  },
82  {
83  "neon_fp32_activation",
84  [](const ActivationSelectorData & data) { return data.dt == DataType::F32; },
86  },
87 #endif /* defined(ARM_COMPUTE_ENABLE_NEON) */
88 #if defined(ARM_COMPUTE_ENABLE_SVE2)
89  {
90  "sve_qu8_activation",
91  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8 && data.ci.has_sve2(); },
93  },
94  {
95  "sve_qs8_activation",
96  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED && data.ci.has_sve2(); },
98  },
99  {
100  "sve_qs16_activation",
101  [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16 && data.ci.has_sve2(); },
103  },
104 #endif /* defined(ARM_COMPUTE_ENABLE_SVE2) */
105  {
106  "neon_qu8_activation",
107  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8; },
109  },
110  {
111  "neon_qs8_activation",
112  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
114  },
115  {
116  "neon_qs16_activation",
117  [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; },
119  },
120 };
121 
122 const ActivationKernel *get_implementation(const ActivationSelectorData &data)
123 {
124  for(const auto &uk : available_kernels)
125  {
126  if(uk.is_selected(data))
127  {
128  return &uk;
129  }
130  }
131  return nullptr;
132 }
133 
134 /* Supported activation in the 8-bit integer domain */
135 static const std::array<ActivationLayerInfo::ActivationFunction, 7> qasymm8_activations =
136 {
144 };
145 /* Supported activation in the 16-bit integer domain */
146 static const std::array<ActivationLayerInfo::ActivationFunction, 3> qsymm16_activations =
147 {
151 };
152 
153 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &activation_info)
154 {
157 
158  const auto *uk = get_implementation(ActivationSelectorData{ src->data_type(), CPUInfo::get() });
159  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
160 
161  const DataType data_type = src->data_type();
162  const QuantizationInfo &oq_info = (dst != nullptr) ? dst->quantization_info() : src->quantization_info();
163  const ActivationLayerInfo::ActivationFunction f_act = activation_info.activation();
164 
165  ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_asymmetric(data_type) && (std::find(std::begin(qasymm8_activations), std::end(qasymm8_activations), f_act) == std::end(qasymm8_activations)),
166  "For QASYMM8 only hard swish, leaky relu, tanh, logistic, relu and lower/upper bounded relu are supported");
167 
168  ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized_symmetric(data_type) && (std::find(std::begin(qsymm16_activations), std::end(qsymm16_activations), f_act) == std::end(qsymm16_activations)),
169  "For QSYMM16 only tanh and logistic are supported");
171  && (oq_info != QuantizationInfo(1.f / 128.f, 128)));
173  && (oq_info != QuantizationInfo(1.f / 256.f, 0)));
174 
175  ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::TANH) && (oq_info != QuantizationInfo(1.f / 128.f, 0)));
176  ARM_COMPUTE_RETURN_ERROR_ON(data_type == DataType::QASYMM8_SIGNED && (f_act == ActivationLayerInfo::ActivationFunction::LOGISTIC) && (oq_info != QuantizationInfo(1.f / 256.f, -128)));
177 
178  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)));
179  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)));
180 
181  // Checks performed when dst is configured
182  if((dst != nullptr) && (dst->total_size() != 0))
183  {
186  }
187 
188  return Status{};
189 }
190 
191 std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst)
192 {
193  // Configure kernel window
194  Window win = calculate_max_window(*src, Steps());
195 
196  if(dst != nullptr)
197  {
198  // dst auto inizialitation if not yet initialized
199  auto_init_if_empty(*dst, *src->clone());
200  }
201 
202  return std::make_pair(Status{}, win);
203 }
204 } // namespace
205 
207 {
209  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, activation_info));
210 
211  const auto uk = get_implementation(ActivationSelectorData{ src->data_type(), CPUInfo::get() });
213 
214  _act_info = activation_info;
215  _run_method = uk->ukernel;
216  _name = std::string("CpuActivationKernel").append("/").append(uk->name);
217 
218  // Configure kernel window
219  auto win_config = validate_and_configure_window(src, dst);
220  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
221  ICPPKernel::configure(win_config.second);
222 }
223 
225 {
226  ARM_COMPUTE_UNUSED(act_info);
227  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, act_info));
228  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), (dst != nullptr) ? dst->clone().get() : nullptr).first);
229 
230  return Status{};
231 }
232 
233 void CpuActivationKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
234 {
235  // Early exit on disabled activation
236  if(!_act_info.enabled())
237  {
238  return;
239  }
240 
241  ARM_COMPUTE_UNUSED(info);
244 
245  ARM_COMPUTE_ERROR_ON(tensors.empty());
246  ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
247 
250 
251  _run_method(src, dst, _act_info, window);
252 }
253 
254 const char *CpuActivationKernel::name() const
255 {
256  return _name.c_str();
257 }
258 } // namespace kernels
259 } // namespace cpu
260 } // namespace arm_compute
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
void fp16_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
quantized, symmetric fixed-point 16-bit number
void configure(const ITensorInfo *src, ITensorInfo *dst, ActivationLayerInfo activation_info)
Configure kernel for a given list of arguments.
const CPUInfo & ci
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:42
void qasymm8_signed_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:80
const ActivationSelectorPtr is_selected
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:61
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define REGISTER_FP32_SVE(func_name)
Definition: Registrars.h:62
void qasymm8_signed_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
#define REGISTER_QASYMM8_SVE(func_name)
Definition: Registrars.h:91
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
#define REGISTER_QASYMM8_SIGNED_NEON(func_name)
Definition: Registrars.h:76
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
quantized, asymmetric fixed-point 16-bit number
Status class.
Definition: Error.h:52
bool is_data_type_quantized_symmetric(DataType dt)
Check if a given data type is of symmetric quantized type.
Definition: Utils.h:1039
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Activation Layer Information class.
Definition: Types.h:1475
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
const char * name() const override
Name of the kernel.
Copyright (c) 2017-2021 Arm Limited.
ActivationFunction
Available activation functions.
Definition: Types.h:1479
1 channel, 1 F16 per channel
DataType dt
#define REGISTER_QASYMM8_SIGNED_SVE(func_name)
Definition: Registrars.h:77
const DataType data_type
Definition: Im2Col.cpp:150
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:90
quantized, asymmetric fixed-point 8-bit number unsigned
#define REGISTER_QSYMM16_NEON(func_name)
Definition: Registrars.h:105
void fp32_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: fp32.cpp:49
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
void fp16_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
void qasymm8_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: qasymm8.cpp:39
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
void qsymm16_neon_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
Definition: qsymm16.cpp:40
void qsymm16_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
const char * name
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1003
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
Information about executing thread and CPU.
Definition: CPPTypes.h:158
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
#define REGISTER_QSYMM16_SVE(func_name)
Definition: Registrars.h:106
#define REGISTER_FP16_SVE(func_name)
Definition: Registrars.h:43
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
ActivationKernelPtr ukernel
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
void qasymm8_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
void fp32_sve_activation(const ITensor *src, ITensor *dst, const ActivationLayerInfo &act_info, const Window &window)
quantized, asymmetric fixed-point 8-bit number signed
static CPUInfo & get()
Access the KernelLibrary singleton.
Definition: CPPTypes.cpp:39
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const ActivationLayerInfo &act_info)
Static function to check if given info will lead to a valid configuration.
DataType
Available data types.
Definition: Types.h:77
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201