Compute Library
 21.02
CpuElementwiseUnaryKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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 
26 #include "arm_compute/core/Error.h"
30 #include "src/core/CPP/Validate.h"
37 
38 namespace arm_compute
39 {
40 namespace cpu
41 {
42 namespace kernels
43 {
44 namespace
45 {
46 using ElementwiseUnarySelector = std::add_pointer<bool(DataType)>::type;
47 
48 struct ElementwiseUnaryKernel
49 {
50  const char *name;
51  const ElementwiseUnarySelector is_selected;
53 };
54 
55 static const ElementwiseUnaryKernel available_kernels[] =
56 {
57 #if defined(__ARM_FEATURE_SVE)
58  {
59  "fp32_sve_elementwise_unary",
60  [](DataType dt) { return dt == DataType::F32; },
61  REGISTER_FP32_SVE(arm_compute::cpu::elementwise_sve_op<float>),
62  },
63  {
64  "fp16_sve_elementwise_unary",
65  [](DataType dt) { return dt == DataType::F16; },
66  REGISTER_FP16_SVE(arm_compute::cpu::elementwise_sve_op<__fp16>),
67  },
68  {
69  "s32_sve_elementwise_unary",
70  [](DataType dt) { return dt == DataType::S32; },
71  REGISTER_INTEGER_SVE(arm_compute::cpu::elementwise_sve_op<int32_t>),
72  },
73 #endif // defined(__ARM_FEATURE_SVE)
74  {
75  "fp32_neon_elementwise_unary",
76  [](DataType dt) { return dt == DataType::F32; },
77  REGISTER_FP32_NEON(arm_compute::cpu::elementwise_op<float>),
78  },
79 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
80  {
81  "fp16_neon_elementwise_unary",
82  [](DataType dt) { return dt == DataType::F16; },
83  REGISTER_FP32_NEON(arm_compute::cpu::elementwise_op<__fp16>),
84  },
85 #endif // defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
86  {
87  "s32_neon_elementwise_unary",
88  [](DataType dt) { return dt == DataType::S32; },
89  REGISTER_INTEGER_NEON(arm_compute::cpu::elementwise_op<int32_t>),
90  },
91 };
92 
93 const ElementwiseUnaryKernel *get_implementation(DataType dt)
94 {
95  for(const auto &uk : available_kernels)
96  {
97  if(uk.is_selected(dt))
98  {
99  return &uk;
100  }
101  }
102  return nullptr;
103 }
104 } // namespace
105 
107  : _op()
108 {
109 }
110 
112 {
113  ARM_COMPUTE_ERROR_THROW_ON(validate(op, input, output));
114 
115  // Configure kernel window
116  const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input);
117  const TensorShape &out_shape = broadcast_pair.first;
118  const ValidRegion &valid_region = broadcast_pair.second;
119 
120  // Auto initialize output if not initialized
121  auto_init_if_empty(output, out_shape, 1, input.data_type());
122 
123  Window win = calculate_max_window(valid_region);
124 
125  _op = op;
126 
127  ICpuKernel::configure(win);
128 }
129 
131 {
133 
134  const auto *uk = get_implementation(input.data_type());
135  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
136 
137  switch(op)
138  {
145  break;
149  break;
150  default:
151  ARM_COMPUTE_ERROR("ElementWiseUnary operation not supported");
152  }
153  // Validate in case of configured output
154  if(output.total_size() > 0)
155  {
157  }
158 
159  return Status{};
160 }
161 
163 {
164  ARM_COMPUTE_UNUSED(info);
167 
168  auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
169  auto dst = tensors.get_tensor(TensorType::ACL_DST);
170  auto func = get_implementation(src->info()->data_type())->ukernel;
171  ARM_COMPUTE_ERROR_ON(func == nullptr);
172  func(src, dst, window, _op);
173 }
174 } // namespace kernels
175 } // namespace cpu
176 } // namespace arm_compute
const ElementwiseUnarySelector is_selected
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:108
#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:52
virtual DataType data_type() const =0
Data type used for each element of the tensor.
CpuElementwiseUnaryKernel::ElementwiseUnaryUkernelPtr ukernel
1 channel, 1 F32 per channel
#define REGISTER_FP32_SVE(func_name)
Definition: Registrars.h:53
#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
Status class.
Definition: Error.h:52
const ValidRegion valid_region
Definition: Scale.cpp:221
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
static std::pair< TensorShape, ValidRegion > broadcast_shape_and_valid_region(const Infos &... infos)
If infos are broadcast compatible tensor info&#39;s, return the broadcasted shape and the intersection of...
Definition: ITensorInfo.h:271
decltype(strategy::transforms) typedef type
static Status validate(ElementWiseUnary op, const ITensorInfo &input, const ITensorInfo &output)
Static function to check if given info will lead to a valid configuration of CpuElementwiseUnaryKerne...
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:92
DataType dt
1 channel, 1 S32 per channel
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:40
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
#define REGISTER_INTEGER_SVE(func_name)
Definition: Registrars.h:93
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 configure(ElementWiseUnary op, const ITensorInfo &input, ITensorInfo &output)
Function to configure the CpuElementwiseUnaryKernel.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
std::add_pointer< void(const ITensor *, ITensor *, const Window &, ElementWiseUnary)>::type ElementwiseUnaryUkernelPtr
Common signature for all the specialised elementwise unary micro-kernels.
const char * name
FloorUKernelPtr func
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:50
Information about executing thread and CPU.
Definition: CPPTypes.h:235
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:43
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Tensor packing service.
Definition: ITensorPack.h:37
Container for valid region of a window.
Definition: Types.h:188
DataType
Available data types.
Definition: Types.h:77
ElementWiseUnary
Available element wise unary operations.
Definition: Types.h:547
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205