Compute Library
 22.02
CpuSubKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-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 
28 #include "src/core/CPP/Validate.h"
33 
34 namespace arm_compute
35 {
36 namespace cpu
37 {
38 namespace kernels
39 {
40 namespace
41 {
42 static const std::vector<CpuSubKernel::SubKernel> available_kernels =
43 {
44  {
45  "neon_fp32_sub",
46  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
47  REGISTER_FP32_NEON(arm_compute::cpu::sub_same_neon<float>)
48  },
49 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
50  {
51  "neon_fp16_sub",
52  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.fp16; },
53  REGISTER_FP16_NEON(arm_compute::cpu::sub_same_neon<float16_t>)
54  },
55 #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
56  {
57  "neon_u8_sub",
58  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::U8); },
59  REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<uint8_t>)
60  },
61  {
62  "neon_s16_sub",
63  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::S16); },
64  REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int16_t>)
65  },
66  {
67  "neon_s32_sub",
68  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::S32); },
69  REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int32_t>)
70  },
71  {
72  "neon_qu8_sub",
73  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
75  },
76  {
77  "neon_qs8_sub",
78  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
80  },
81  {
82  "neon_qs16_sub",
83  [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QSYMM16); },
85  },
86 };
87 
88 inline Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst, ConvertPolicy policy)
89 {
90  ARM_COMPUTE_UNUSED(policy);
95 
96  const auto *uk = CpuSubKernel::get_implementation(DataTypeISASelectorData{ src0.data_type(), CPUInfo::get().get_isa() });
97 
98  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
99 
100  const TensorShape out_shape = TensorShape::broadcast_shape(src0.tensor_shape(), src1.tensor_shape());
101  ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
102 
104  "Convert policy cannot be WRAP if datatype is quantized");
105 
106  // Validate in case of configured dst
107  if(dst.total_size() > 0)
108  {
110  ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
111  "Wrong shape for dst");
112  }
113  return Status{};
114 }
115 } // namespace
116 
117 void CpuSubKernel::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ConvertPolicy policy)
118 {
119  ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
120  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*src0, *src1, *dst, policy));
121 
122  const TensorShape &out_shape = TensorShape::broadcast_shape(src0->tensor_shape(), src1->tensor_shape());
123 
124  // Auto initialize dst if not initialized
125  set_shape_if_empty(*dst, out_shape);
126  set_data_type_if_unknown(*dst, src0->data_type());
127 
130 
131  _policy = policy;
132  _run_method = uk->ukernel;
133  _name = std::string("CpuSubKernel").append("/").append(uk->name);
134 
135  // CpuSubKernel doesn't need padding so update_window_and_padding() can be skipped
136  Window win = calculate_max_window(out_shape, Steps());
137 
138  ICpuKernel::configure(win);
139 }
140 
141 Status CpuSubKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ConvertPolicy policy)
142 {
143  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
144  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*src0, *src1, *dst, policy));
145 
146  return Status{};
147 }
148 
150 {
151  ARM_COMPUTE_UNUSED(info);
154  ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
155 
156  const ITensor *src0 = tensors.get_const_tensor(TensorType::ACL_SRC_0);
157  const ITensor *src1 = tensors.get_const_tensor(TensorType::ACL_SRC_1);
158  ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
159 
160  _run_method(src0, src1, dst, _policy, window);
161 }
162 
163 const char *CpuSubKernel::name() const
164 {
165  return _name.c_str();
166 }
167 
168 const std::vector<CpuSubKernel::SubKernel> &CpuSubKernel::get_available_kernels()
169 {
170  return available_kernels;
171 }
172 
173 } // namespace kernels
174 } // namespace cpu
175 } // namespace arm_compute
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:996
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
quantized, symmetric fixed-point 16-bit number
static const auto * get_implementation(const SelectorType &selector, KernelSelectionType selection_type=KernelSelectionType::Supported)
Micro-kernel selector.
Definition: ICpuKernel.h:53
bool set_data_type_if_unknown(ITensorInfo &info, DataType data_type)
Set the data type and number of channels to the specified value if the current data type is unknown...
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
1 channel, 1 U8 per channel
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
#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
static TensorShape broadcast_shape(const Shapes &... shapes)
If shapes are broadcast compatible, return the broadcasted shape.
Definition: TensorShape.h:211
#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:96
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
static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ConvertPolicy policy)
Static function to check if given info will lead to a valid configuration.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
#define REGISTER_INTEGER_NEON(func_name)
Definition: Registrars.h:165
const char * name() const override
Name of the kernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
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
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define REGISTER_QASYMM8_NEON(func_name)
Definition: Registrars.h:117
void configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ConvertPolicy policy)
Initialise the kernel&#39;s src and dst.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
#define REGISTER_QSYMM16_NEON(func_name)
Definition: Registrars.h:139
bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
Set the shape to the specified value if the current assignment is empty.
bool have_different_dimensions(const Dimensions< T > &dim1, const Dimensions< T > &dim2, unsigned int upper_dim)
Definition: Validate.h:47
static const std::vector< SubKernel > & get_available_kernels()
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
1 channel, 1 S16 per channel
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
void sub_qasymm8_signed_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
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
void sub_qsymm16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: qsymm16.cpp:35
Information about executing thread and CPU.
Definition: CPPTypes.h:169
void sub_qasymm8_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: qasymm8.cpp:35
#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
#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
quantized, asymmetric fixed-point 8-bit number signed
static CPUInfo & get()
Access the KernelLibrary singleton.
Definition: CPPTypes.cpp:40
Describe a multidimensional execution window.
Definition: Window.h:39
ConvertPolicy
Policy to handle integer overflow.
Definition: Types.h:391
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
cpuinfo::CpuIsaInfo get_isa() const
Gets the current cpu&#39;s ISA information.
Definition: CPPTypes.cpp:114