Compute Library
 22.11
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  {
48  "sve_fp32_elementwise_unary",
49  [](const DataTypeISASelectorData & data)
50  {
51  return (data.dt == DataType::F32 && data.isa.sve);
52  },
54  },
55  {
56  "sve_fp16_elementwise_unary",
57  [](const DataTypeISASelectorData & data)
58  {
59  return (data.dt == DataType::F16 && data.isa.sve && data.isa.fp16);
60  },
62  },
63  {
64  "sve_s32_elementwise_unary",
65  [](const DataTypeISASelectorData & data)
66  {
67  return (data.dt == DataType::S32 && data.isa.sve);
68  },
70  },
71  {
72  "neon_fp32_elementwise_unary",
73  [](const DataTypeISASelectorData & data)
74  {
75  return data.dt == DataType::F32;
76  },
78  },
79  {
80  "neon_fp16_elementwise_unary",
81  [](const DataTypeISASelectorData & data)
82  {
83  return data.dt == DataType::F16 && data.isa.fp16;
84  },
86  },
87  {
88  "neon_s32_elementwise_unary",
89  [](const DataTypeISASelectorData & data)
90  {
91  return data.dt == DataType::S32;
92  },
94  },
95 };
96 
97 } // namespace
98 
100 {
101  ARM_COMPUTE_ERROR_THROW_ON(validate(op, src, dst));
103  ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
104 
105  _op = op;
106  _run_method = uk->ukernel;
107  _name = std::string("CpuElementwiseUnaryKernel").append("/").append(uk->name);
108 
109  // If input shape is dynamic, expect a configured window and dst at run-time.
110  if(src.is_dynamic())
111  {
112  return;
113  }
114 
115  auto shape_and_window = compute_output_shape_and_window(src.tensor_shape());
116  auto_init_if_empty(dst, shape_and_window.first, 1, src.data_type());
117  ICpuKernel::configure(shape_and_window.second);
118 }
119 
121 {
123 
125 
126  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
127 
128  switch(op)
129  {
136  break;
140  break;
141  default:
142  ARM_COMPUTE_ERROR("ElementWiseUnary operation not supported");
143  }
144  // Validate in case of configured dst
145  if(dst.total_size() > 0)
146  {
148  }
149 
150  return Status{};
151 }
152 
154 {
155  ARM_COMPUTE_UNUSED(info);
156 
157  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
158  auto dst = tensors.get_tensor(TensorType::ACL_DST);
159 
160  _run_method(src, dst, window, _op);
161 }
162 
164 {
165  return _name.c_str();
166 }
167 
168 const std::vector<CpuElementwiseUnaryKernel::ElementwiseUnaryKernel> &CpuElementwiseUnaryKernel::get_available_kernels()
169 {
170  return available_kernels;
171 }
172 
173 } // namespace kernels
174 } // namespace cpu
175 } // 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
#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-2022 Arm Limited.
1 channel, 1 F16 per channel
#define REGISTER_INTEGER_NEON(func_name)
Definition: Registrars.h:171
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.
void sve_s32_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
Definition: integer.cpp:32
#define REGISTER_INTEGER_SVE(func_name)
Definition: Registrars.h:172
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:179
void sve_fp32_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
Definition: fp32.cpp:32
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
static const std::vector< ElementwiseUnaryKernel > & get_available_kernels()
ElementWiseUnary
Available element wise unary operations.
Definition: Types.h:502
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:124
void neon_s32_elementwise_unary(const ITensor *in, ITensor *out, const Window &window, ElementWiseUnary op)
Definition: integer.cpp:31