blob: ec65f12dfccd1e58798a1941660421d488d18aa6 [file] [log] [blame]
Sheri Zhangfc6744a2021-01-13 15:54:05 +00001/*
2 * Copyright (c) 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuSubKernel.h"
Sheri Zhangfc6744a2021-01-13 15:54:05 +000025
26#include "arm_compute/core/TensorInfo.h"
27#include "arm_compute/core/Validate.h"
28#include "src/core/CPP/Validate.h"
29#include "src/core/common/Registrars.h"
Sheri Zhangfc6744a2021-01-13 15:54:05 +000030#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/cpu/kernels/sub/neon/list.h"
Sheri Zhangfc6744a2021-01-13 15:54:05 +000033
34namespace arm_compute
35{
36namespace cpu
37{
38namespace kernels
39{
40namespace
41{
42struct SubSelectorData
43{
Georgios Pinitasda816752021-07-02 09:22:14 +010044 DataType dt;
Sheri Zhangfc6744a2021-01-13 15:54:05 +000045};
46
47using SubSelectorPtr = std::add_pointer<bool(const SubSelectorData &data)>::type;
48using SubKernelPtr = std::add_pointer<void(const ITensor *, const ITensor *, ITensor *, const ConvertPolicy &, const Window &)>::type;
49
50struct SubKernel
51{
52 const char *name;
53 const SubSelectorPtr is_selected;
54 SubKernelPtr ukernel;
55};
56
57static const SubKernel available_kernels[] =
58{
59 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010060 "neon_fp32_sub",
Georgios Pinitasda816752021-07-02 09:22:14 +010061 [](const SubSelectorData & data) { return (data.dt == DataType::F32); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000062 REGISTER_FP32_NEON(arm_compute::cpu::sub_same_neon<float>)
63 },
64#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC)
65 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010066 "neon_fp16_sub",
Georgios Pinitasda816752021-07-02 09:22:14 +010067 [](const SubSelectorData & data) { return (data.dt == DataType::F16); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000068 REGISTER_FP16_NEON(arm_compute::cpu::sub_same_neon<float16_t>)
69 },
70#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */
71 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010072 "neon_u8_sub",
Georgios Pinitasda816752021-07-02 09:22:14 +010073 [](const SubSelectorData & data) { return (data.dt == DataType::U8); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000074 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<uint8_t>)
75 },
76 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010077 "neon_s16_sub",
Georgios Pinitasda816752021-07-02 09:22:14 +010078 [](const SubSelectorData & data) { return (data.dt == DataType::S16); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000079 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int16_t>)
80 },
81 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010082 "neon_s32_sub",
Georgios Pinitasda816752021-07-02 09:22:14 +010083 [](const SubSelectorData & data) { return (data.dt == DataType::S32); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000084 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int32_t>)
85 },
86 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010087 "neon_qu8_sub",
Georgios Pinitasda816752021-07-02 09:22:14 +010088 [](const SubSelectorData & data) { return (data.dt == DataType::QASYMM8); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000089 REGISTER_QASYMM8_NEON(arm_compute::cpu::sub_qasymm8_neon)
90 },
91 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010092 "neon_qs8_sub",
Georgios Pinitasda816752021-07-02 09:22:14 +010093 [](const SubSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000094 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::sub_qasymm8_signed_neon)
95 },
96 {
Georgios Pinitasda816752021-07-02 09:22:14 +010097 "neon_qs16_sub",
98 [](const SubSelectorData & data) { return (data.dt == DataType::QSYMM16); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000099 REGISTER_QSYMM16_NEON(arm_compute::cpu::sub_qsymm16_neon)
100 },
101};
102
103/** Micro-kernel selector
104 *
105 * @param[in] data Selection data passed to help pick the appropriate micro-kernel
106 *
107 * @return A matching micro-kernel else nullptr
108 */
Georgios Pinitasda816752021-07-02 09:22:14 +0100109const SubKernel *get_implementation(DataType dt)
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000110{
111 for(const auto &uk : available_kernels)
112 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100113 if(uk.is_selected({ dt }))
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000114 {
115 return &uk;
116 }
117 }
118 return nullptr;
119}
120
121inline Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst, ConvertPolicy policy)
122{
123 ARM_COMPUTE_UNUSED(policy);
124 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src0);
125 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src0, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16, DataType::S16, DataType::S32, DataType::F16,
126 DataType::F32);
Georgios Pinitasda816752021-07-02 09:22:14 +0100127 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &src1);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000128
Georgios Pinitasda816752021-07-02 09:22:14 +0100129 const auto *uk = get_implementation(src0.data_type());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000130 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
131
132 const TensorShape out_shape = TensorShape::broadcast_shape(src0.tensor_shape(), src1.tensor_shape());
133 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
134
Georgios Pinitasda816752021-07-02 09:22:14 +0100135 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(src0.data_type()) && (policy == ConvertPolicy::WRAP),
136 "Convert policy cannot be WRAP if datatype is quantized");
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000137
138 // Validate in case of configured dst
139 if(dst.total_size() > 0)
140 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100141 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &dst);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000142 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
143 "Wrong shape for dst");
144 }
145 return Status{};
146}
147} // namespace
148
149void CpuSubKernel::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ConvertPolicy policy)
150{
151 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
152 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*src0, *src1, *dst, policy));
153
SiCongLic7b1e842021-02-22 14:28:33 +0000154 const TensorShape &out_shape = TensorShape::broadcast_shape(src0->tensor_shape(), src1->tensor_shape());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000155
156 // Auto initialize dst if not initialized
157 set_shape_if_empty(*dst, out_shape);
Georgios Pinitasda816752021-07-02 09:22:14 +0100158 set_data_type_if_unknown(*dst, src0->data_type());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000159
Georgios Pinitasda816752021-07-02 09:22:14 +0100160 const auto *uk = get_implementation(src0->data_type());
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100161 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
162
163 _policy = policy;
164 _run_method = uk->ukernel;
165 _name = std::string("CpuSubKernel").append("/").append(uk->name);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000166
167 // CpuSubKernel doesn't need padding so update_window_and_padding() can be skipped
SiCongLic7b1e842021-02-22 14:28:33 +0000168 Window win = calculate_max_window(out_shape, Steps());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000169
170 ICpuKernel::configure(win);
171}
172
173Status CpuSubKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ConvertPolicy policy)
174{
175 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
176 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*src0, *src1, *dst, policy));
177
178 return Status{};
179}
180
181void CpuSubKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
182{
183 ARM_COMPUTE_UNUSED(info);
184 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
185 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100186 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000187
188 const ITensor *src0 = tensors.get_const_tensor(TensorType::ACL_SRC_0);
189 const ITensor *src1 = tensors.get_const_tensor(TensorType::ACL_SRC_1);
190 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
191
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100192 _run_method(src0, src1, dst, _policy, window);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000193}
194
195const char *CpuSubKernel::name() const
196{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100197 return _name.c_str();
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000198}
199} // namespace kernels
200} // namespace cpu
201} // namespace arm_compute