blob: c8706ff651891b49716e4aa68d591844e5a3216a [file] [log] [blame]
Sheri Zhangfc6744a2021-01-13 15:54:05 +00001/*
Pablo Marquez Tello67773592023-10-06 13:49:44 +01002 * Copyright (c) 2021-2023 Arm Limited.
Sheri Zhangfc6744a2021-01-13 15:54:05 +00003 *
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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Sheri Zhangfc6744a2021-01-13 15:54:05 +000029#include "src/core/common/Registrars.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/core/CPP/Validate.h"
Sheri Zhangfc6744a2021-01-13 15:54:05 +000031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
Omar Al Khatib939b21a2022-12-20 14:36:45 +000033#include "src/cpu/kernels/add/generic/neon/impl.h"
Pablo Marquez Tello67773592023-10-06 13:49:44 +010034#include "src/cpu/kernels/sub/neon/impl.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/cpu/kernels/sub/neon/list.h"
Sheri Zhangfc6744a2021-01-13 15:54:05 +000036
Gunes Bayirf16973b2022-11-29 13:12:08 +000037#if defined(ENABLE_FP32_KERNELS)
Fadi Arafeh73bb6b72022-10-06 16:20:14 +000038namespace
39{
Omar Al Khatib939b21a2022-12-20 14:36:45 +000040static constexpr size_t default_mws_N1_fp32_neon = 24385;
41static constexpr size_t default_mws_V1_fp32_neon = 40520;
42} // namespace
Gunes Bayirf16973b2022-11-29 13:12:08 +000043#endif /* ENABLE_FP32_KERNELS */
44
Sheri Zhangfc6744a2021-01-13 15:54:05 +000045namespace arm_compute
46{
47namespace cpu
48{
49namespace kernels
50{
51namespace
52{
Omar Al Khatib939b21a2022-12-20 14:36:45 +000053using CpuSubKernelDataTypeISASelectorData = CpuAddKernelDataTypeISASelectorData;
54using CpuSubKernelDataTypeISASelectorDataPtr = CpuAddKernelDataTypeISASelectorDataPtr;
55
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056static const std::vector<CpuSubKernel::SubKernel> available_kernels = {
57 {"neon_fp32_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::F32); },
58 REGISTER_FP32_NEON(arm_compute::cpu::sub_same_neon<float>)},
59 {"neon_fp16_sub",
60 [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::F16) && data.isa.fp16; },
Pablo Marquez Tello67773592023-10-06 13:49:44 +010061 REGISTER_FP16_NEON(arm_compute::cpu::sub_same_neon_fp16)},
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 {"neon_u8_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::U8); },
63 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<uint8_t>)},
64 {"neon_s16_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::S16); },
65 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int16_t>)},
66 {"neon_s32_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::S32); },
67 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int32_t>)},
68 {"neon_qu8_sub_fixedpoint",
69 [](const CpuSubKernelDataTypeISASelectorData &data)
70 { return ((data.dt == DataType::QASYMM8) && data.can_use_fixedpoint); },
71 REGISTER_QASYMM8_NEON(arm_compute::cpu::sub_qasymm8_neon_fixedpoint)},
72 {"neon_qs8_sub_fixedpoint",
73 [](const CpuSubKernelDataTypeISASelectorData &data)
74 { return ((data.dt == DataType::QASYMM8_SIGNED) && data.can_use_fixedpoint); },
75 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::sub_qasymm8_signed_neon_fixedpoint)},
76 {"neon_qu8_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::QASYMM8); },
77 REGISTER_QASYMM8_NEON(arm_compute::cpu::sub_qasymm8_neon)},
78 {"neon_qs8_sub",
79 [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::QASYMM8_SIGNED); },
80 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::sub_qasymm8_signed_neon)},
81 {"neon_qs16_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::QSYMM16); },
82 REGISTER_QSYMM16_NEON(arm_compute::cpu::sub_qsymm16_neon)},
Sheri Zhangfc6744a2021-01-13 15:54:05 +000083};
84
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085inline Status
86validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst, ConvertPolicy policy)
Sheri Zhangfc6744a2021-01-13 15:54:05 +000087{
88 ARM_COMPUTE_UNUSED(policy);
89 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src0, 1, DataType::U8, DataType::QASYMM8,
91 DataType::QASYMM8_SIGNED, DataType::QSYMM16, DataType::S16,
92 DataType::S32, DataType::F16, DataType::F32);
Georgios Pinitasda816752021-07-02 09:22:14 +010093 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &src1);
Sheri Zhangfc6744a2021-01-13 15:54:05 +000094
Omar Al Khatib939b21a2022-12-20 14:36:45 +000095 const auto can_use_fixedpoint = sub_q8_neon_fixedpoint_possible(&src0, &src1, &dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 const auto uk = CpuSubKernel::get_implementation<CpuSubKernelDataTypeISASelectorData>(
97 CpuSubKernelDataTypeISASelectorData{src0.data_type(), CPUInfo::get().get_isa(), can_use_fixedpoint});
Giorgio Arena5ae8d802021-11-18 18:02:13 +000098
Sheri Zhangfc6744a2021-01-13 15:54:05 +000099 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
100
101 const TensorShape out_shape = TensorShape::broadcast_shape(src0.tensor_shape(), src1.tensor_shape());
102 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
103
Georgios Pinitasda816752021-07-02 09:22:14 +0100104 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(src0.data_type()) && (policy == ConvertPolicy::WRAP),
105 "Convert policy cannot be WRAP if datatype is quantized");
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000106
107 // Validate in case of configured dst
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 if (dst.total_size() > 0)
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000109 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100110 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &dst);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000111 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
112 "Wrong shape for dst");
113 }
114 return Status{};
115}
116} // namespace
117
118void CpuSubKernel::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ConvertPolicy policy)
119{
120 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
121 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*src0, *src1, *dst, policy));
122
SiCongLic7b1e842021-02-22 14:28:33 +0000123 const TensorShape &out_shape = TensorShape::broadcast_shape(src0->tensor_shape(), src1->tensor_shape());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000124
125 // Auto initialize dst if not initialized
126 set_shape_if_empty(*dst, out_shape);
Georgios Pinitasda816752021-07-02 09:22:14 +0100127 set_data_type_if_unknown(*dst, src0->data_type());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000128
Omar Al Khatib939b21a2022-12-20 14:36:45 +0000129 const auto can_use_fixedpoint = sub_q8_neon_fixedpoint_possible(src0, src1, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 const auto uk = CpuSubKernel::get_implementation<CpuSubKernelDataTypeISASelectorData>(
131 CpuSubKernelDataTypeISASelectorData{src0->data_type(), CPUInfo::get().get_isa(), can_use_fixedpoint});
Omar Al Khatib939b21a2022-12-20 14:36:45 +0000132
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100133 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
134
135 _policy = policy;
136 _run_method = uk->ukernel;
137 _name = std::string("CpuSubKernel").append("/").append(uk->name);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000138
139 // CpuSubKernel doesn't need padding so update_window_and_padding() can be skipped
Jakub Sujak842ad212022-09-17 13:08:56 +0100140 Window win;
141 std::tie(win, _split_dimension) = calculate_squashed_or_max_window(*src0, *src1);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000142
143 ICpuKernel::configure(win);
144}
145
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000146size_t CpuSubKernel::get_mws(const CPUInfo &platform, size_t thread_count) const
147{
148 ARM_COMPUTE_UNUSED(thread_count);
149
150#if defined(ENABLE_FP32_KERNELS)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 if (this->_run_method == &sub_same_neon<float>)
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000152 {
153 size_t mws = ICPPKernel::default_mws;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 if (platform.get_cpu_model() == CPUModel::N1)
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000155 {
156 mws = default_mws_N1_fp32_neon;
157 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 else if (platform.get_cpu_model() == CPUModel::V1)
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000159 {
160 mws = default_mws_V1_fp32_neon;
161 }
162 else
163 {
164 return ICPPKernel::default_mws;
165 }
166
167 // tensor is 1D or was re-interpreted as 1D
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 if (this->window().shape().num_dimensions() == 1)
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000169 {
170 return mws;
171 }
172 else
173 {
174 // scale mws down by the number of elements along all the dimensions (x, z, w, etc) except the one
175 // that we parallelize along (the y dimension). This allows for parallelization when the Y_SIZE is small
176 // but the other sizes are large, which boosts performance.
177 mws = static_cast<size_t>(mws / (this->window().num_iterations_total() / this->window().num_iterations(1)));
178 return std::max(static_cast<size_t>(1), mws);
179 }
180 }
Omar Al Khatib939b21a2022-12-20 14:36:45 +0000181#else /* ENABLE_FP32_KERNELS */
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000182 ARM_COMPUTE_UNUSED(platform);
183#endif /* ENABLE_FP32_KERNELS */
184 return ICPPKernel::default_mws;
185}
186
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187Status
188CpuSubKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ConvertPolicy policy)
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000189{
190 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
191 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*src0, *src1, *dst, policy));
192
193 return Status{};
194}
195
196void CpuSubKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
197{
198 ARM_COMPUTE_UNUSED(info);
199 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
200 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100201 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000202
203 const ITensor *src0 = tensors.get_const_tensor(TensorType::ACL_SRC_0);
204 const ITensor *src1 = tensors.get_const_tensor(TensorType::ACL_SRC_1);
205 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
206
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100207 _run_method(src0, src1, dst, _policy, window);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000208}
209
210const char *CpuSubKernel::name() const
211{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100212 return _name.c_str();
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000213}
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000214
215const std::vector<CpuSubKernel::SubKernel> &CpuSubKernel::get_available_kernels()
216{
217 return available_kernels;
218}
219
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000220} // namespace kernels
221} // namespace cpu
222} // namespace arm_compute