Compute Library
 22.02
CpuElementwiseUnaryKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2022 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 
26 #include "arm_compute/core/Error.h"
30 #include "src/core/CPP/Validate.h"
36 
37 namespace arm_compute
38 {
39 namespace cpu
40 {
41 namespace kernels
42 {
43 namespace
44 {
45 static const std::vector<CpuElementwiseUnaryKernel::ElementwiseUnaryKernel> available_kernels =
46 {
47 #if defined(ARM_COMPUTE_ENABLE_SVE)
48  {
49  "sve_fp32_elementwise_unary",
50  [](const DataTypeISASelectorData & data)
51  {
52  return data.dt == DataType::F32 && data.isa.sve;
53  },
55  },
56  {
57  "sve_fp16_elementwise_unary",
58  [](const DataTypeISASelectorData & data)
59  {
60  return (data.dt == DataType::F16) && data.isa.sve;
61  },
63  },
64  {
65  "sve_s32_elementwise_unary",
66  [](const DataTypeISASelectorData & data) { return data.dt == DataType::S32 && data.isa.sve; },
68  },
69 #endif // defined(ARM_COMPUTE_ENABLE_SVE)
70 #if defined(ARM_COMPUTE_ENABLE_NEON)
71  {
72  "neon_fp32_elementwise_unary",
73  [](const DataTypeISASelectorData & data) { return data.dt == DataType::F32; },
75  },
76 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
77  {
78  "neon_fp16_elementwise_unary",
79  [](const DataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
81  },
82 #endif // defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
83  {
84  "neon_s32_elementwise_unary",
85  [](const DataTypeISASelectorData & data) { return data.dt == DataType::S32; },
87  },
88 #endif // defined(ARM_COMPUTE_ENABLE_NEON)
89 };
90 
91 } // namespace
92 
94 {
95  ARM_COMPUTE_ERROR_THROW_ON(validate(op, src, dst));
97  ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
98 
99  _op = op;
100  _run_method = uk->ukernel;
101  _name = std::string("CpuElementwiseUnaryKernel").append("/").append(uk->name);
102 
103  // If input shape is dynamic, expect a configured window and dst at run-time.
104  if(src.is_dynamic())
105  {
106  return;
107  }
108 
109  auto shape_and_window = compute_output_shape_and_window(src.tensor_shape());
110  auto_init_if_empty(dst, shape_and_window.first, 1, src.data_type());
111  ICpuKernel::configure(shape_and_window.second);
112 }
113 
115 {
117 
119 
120  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
121 
122  switch(op)
123  {
130  break;
134  break;
135  default:
136  ARM_COMPUTE_ERROR("ElementWiseUnary operation not supported");
137  }
138  // Validate in case of configured dst
139  if(dst.total_size() > 0)
140  {
142  }
143 
144  return Status{};
145 }
146 
148 {
149  ARM_COMPUTE_UNUSED(info);
150 
151  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
152  auto dst = tensors.get_tensor(TensorType::ACL_DST);
153 
154  _run_method(src, dst, window, _op);
155 }
156 
158 {
159  return _name.c_str();
160 }
161 
162 const std::vector<CpuElementwiseUnaryKernel::ElementwiseUnaryKernel> &CpuElementwiseUnaryKernel::get_available_kernels()
163 {
164  return available_kernels;
165 }
166 
167 } // namespace kernels
168 } // namespace cpu
169 } // namespace arm_compute
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void neon_fp32_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
Definition: fp32.cpp:31
static const auto * get_implementation(const SelectorType &selector, KernelSelectionType selection_type=KernelSelectionType::Supported)
Micro-kernel selector.
Definition: ICpuKernel.h:53
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
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:75
void sve_fp16_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
const char * name() const override
Name of the kernel.
#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
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
virtual bool is_dynamic() const =0
Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/func...
Status class.
Definition: Error.h:52
void sve_s32_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
#define REGISTER_INTEGER_NEON(func_name)
Definition: Registrars.h:165
1 channel, 1 S32 per channel
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define REGISTER_INTEGER_SVE(func_name)
Definition: Registrars.h:166
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 neon_fp16_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
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:169
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
#define REGISTER_FP16_SVE(func_name)
Definition: Registrars.h:49
static Status validate(ElementWiseUnary op, const ITensorInfo &src, const ITensorInfo &dst)
Static function to check if given info will lead to a valid configuration.
#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
void configure(ElementWiseUnary op, const ITensorInfo &src, ITensorInfo &dst)
Function to configure the CpuElementwiseUnaryKernel.
Tensor packing service.
Definition: ITensorPack.h:39
static CPUInfo & get()
Access the KernelLibrary singleton.
Definition: CPPTypes.cpp:40
void sve_fp32_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
static const std::vector< ElementwiseUnaryKernel > & get_available_kernels()
ElementWiseUnary
Available element wise unary operations.
Definition: Types.h:489
Describe a multidimensional execution window.
Definition: Window.h:39
cpuinfo::CpuIsaInfo get_isa() const
Gets the current cpu&#39;s ISA information.
Definition: CPPTypes.cpp:114
void neon_s32_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
Definition: integer.cpp:31