Compute Library
 21.05
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 };
50 
51 using ActivationSelectorPtr = std::add_pointer<bool(const ActivationSelectorData &data)>::type;
52 using ActivationKernelPtr = std::add_pointer<void(const ITensor *, ITensor *, const ActivationLayerInfo &, const Window &)>::type;
53 
54 struct ActivationKernel
55 {
56  const char *name;
57  const ActivationSelectorPtr is_selected;
58  ActivationKernelPtr ukernel;
59 };
60 
61 static const ActivationKernel available_kernels[] =
62 {
63 #if defined(__ARM_FEATURE_SVE)
64  {
65  "fp16_sve_activation",
66  [](const ActivationSelectorData & data) { return data.dt == DataType::F16; },
68  },
69  {
70  "fp32_sve_activation",
71  [](const ActivationSelectorData & data) { return data.dt == DataType::F32; },
73  },
74 #else /* !defined(__ARM_FEATURE_SVE) */
75  {
76  "fp16_neon_activation",
77  [](const ActivationSelectorData & data) { return data.dt == DataType::F16; },
79  },
80  {
81  "fp32_neon_activation",
82  [](const ActivationSelectorData & data) { return data.dt == DataType::F32; },
84  },
85 #endif /* defined(__ARM_FEATURE_SVE) */
86 
87 #if defined(__ARM_FEATURE_SVE2) /* defined(__ARM_FEATURE_SVE2) */
88  {
89  "qasymm8_sve_activation",
90  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8; },
92  },
93  {
94  "qasymm8_signed_sve_activation",
95  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
97  },
98  {
99  "qsymm16_sve_activation",
100  [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; },
102  },
103 #else /* !defined(__ARM_FEATURE_SVE2) */
104  {
105  "qasymm8_neon_activation",
106  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8; },
108  },
109  {
110  "qasymm8_signed_neon_activation",
111  [](const ActivationSelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
113  },
114  {
115  "qsymm16_neon_activation",
116  [](const ActivationSelectorData & data) { return data.dt == DataType::QSYMM16; },
118  },
119 #endif /* defined(__ARM_FEATURE_SVE2) */
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() });
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 
210  _act_info = activation_info;
211 
213 
214  // Configure kernel window
215  auto win_config = validate_and_configure_window(src, dst);
216  ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
217  ICPPKernel::configure(win_config.second);
218 }
219 
221 {
222  ARM_COMPUTE_UNUSED(act_info);
224  ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(src->clone().get(), (dst != nullptr) ? dst->clone().get() : nullptr).first);
225 
226  return Status{};
227 }
228 
229 void CpuActivationKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
230 {
231  // Early exit on disabled activation
232  if(!_act_info.enabled())
233  {
234  return;
235  }
236 
240 
241  ARM_COMPUTE_ERROR_ON(tensors.empty());
242 
245 
246  const auto *uk = get_implementation(ActivationSelectorData{ src->info()->data_type() });
247 
248  uk->ukernel(src, dst, _act_info, window);
249 }
250 
251 const char *CpuActivationKernel::name() const
252 {
253  return "CpuActivationKernel";
254 }
255 } // namespace kernels
256 } // namespace cpu
257 } // 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.
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:108
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:42
bool enabled() const
Check if initialised.
Definition: Types.h:1528
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:66
const ActivationSelectorPtr is_selected
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:52
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
1 channel, 1 F32 per channel
#define REGISTER_FP32_SVE(func_name)
Definition: Registrars.h:53
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:73
#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:62
Store the tensor'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:1025
#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:1478
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:1482
1 channel, 1 F16 per channel
DataType dt
#define REGISTER_QASYMM8_SIGNED_SVE(func_name)
Definition: Registrars.h:63
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:45
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:72
quantized, asymmetric fixed-point 8-bit number unsigned
#define REGISTER_QSYMM16_NEON(func_name)
Definition: Registrars.h:82
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...
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:989
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:55
Information about executing thread and CPU.
Definition: CPPTypes.h:252
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:439
#define REGISTER_QSYMM16_SVE(func_name)
Definition: Registrars.h:83
#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
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#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:37
#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 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 of CpuActivationKernel.
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