blob: 2983575cb6dd44db9ba157de98d778bf5687d5d8 [file] [log] [blame]
Sheri Zhang61243902021-01-12 18:25:16 +00001/*
Giorgio Arena5ae8d802021-11-18 18:02:13 +00002 * Copyright (c) 2021-2022 Arm Limited.
Sheri Zhang61243902021-01-12 18:25:16 +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/CpuAddKernel.h"
Sheri Zhang61243902021-01-12 18:25:16 +000025
26#include "arm_compute/core/ITensor.h"
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Validate.h"
29#include "src/core/CPP/Validate.h"
30#include "src/core/common/Registrars.h"
Sheri Zhang61243902021-01-12 18:25:16 +000031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
Dana Zlotnikbd2942d2021-11-15 08:46:04 +020033#include "src/cpu/kernels/add/list.h"
Sheri Zhang61243902021-01-12 18:25:16 +000034#include <array>
35
Gunes Bayirf16973b2022-11-29 13:12:08 +000036#if defined(ENABLE_FP32_KERNELS)
Fadi Arafeh73bb6b72022-10-06 16:20:14 +000037namespace
38{
39 static constexpr size_t default_mws_N1_fp32_neon = 24536;
40 static constexpr size_t default_mws_V1_fp32_neon = 40510;
41}
Gunes Bayirf16973b2022-11-29 13:12:08 +000042#endif /* ENABLE_FP32_KERNELS */
43
Sheri Zhang61243902021-01-12 18:25:16 +000044namespace arm_compute
45{
46namespace cpu
47{
48namespace kernels
49{
50namespace
51{
Giorgio Arena5ae8d802021-11-18 18:02:13 +000052static const std::vector<CpuAddKernel::AddKernel> available_kernels =
Sheri Zhang61243902021-01-12 18:25:16 +000053{
Michalis Spyrou20fca522021-06-07 14:23:57 +010054 {
Viet-Hoa Do40b44192022-09-22 10:24:23 +010055 "neon_qu8_add_fixedpoint",
56 [](const CpuAddKernelDataTypeISASelectorData & data)
57 {
58 return (data.dt == DataType::QASYMM8) && data.can_use_fixedpoint;
59 },
60 REGISTER_FP32_NEON(arm_compute::cpu::add_q8_neon_fixedpoint<uint8_t>)
61 },
62 {
63 "neon_qs8_add_fixedpoint",
64 [](const CpuAddKernelDataTypeISASelectorData & data)
65 {
66 return (data.dt == DataType::QASYMM8_SIGNED) && data.can_use_fixedpoint;
67 },
68 REGISTER_FP32_NEON(arm_compute::cpu::add_q8_neon_fixedpoint<int8_t>)
69 },
70 {
Gunes Bayir9b921be2022-07-28 17:44:00 +010071 "sve2_qu8_add",
72 [](const CpuAddKernelDataTypeISASelectorData & data)
73 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +010074 return (data.dt == DataType::QASYMM8) && data.isa.sve2;
Michalis Spyrou20fca522021-06-07 14:23:57 +010075 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +020076 REGISTER_QASYMM8_SVE2(arm_compute::cpu::add_qasymm8_sve2)
Michalis Spyrou20fca522021-06-07 14:23:57 +010077 },
78 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010079 "sve2_qs8_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +010080 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +010081 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +010082 return (data.dt == DataType::QASYMM8_SIGNED) && data.isa.sve2;
Michalis Spyrou20fca522021-06-07 14:23:57 +010083 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +020084 REGISTER_QASYMM8_SIGNED_SVE2(arm_compute::cpu::add_qasymm8_signed_sve2)
Michalis Spyrou20fca522021-06-07 14:23:57 +010085 },
86 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010087 "sve2_qs16_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +010088 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +010089 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +010090 return (data.dt == DataType::QSYMM16) && data.isa.sve2;
Michalis Spyrou20fca522021-06-07 14:23:57 +010091 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +020092 REGISTER_QSYMM16_SVE2(arm_compute::cpu::add_qsymm16_sve2)
Michalis Spyrou20fca522021-06-07 14:23:57 +010093 },
Sheri Zhang61243902021-01-12 18:25:16 +000094 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010095 "sve_fp32_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +010096 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +010097 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +010098 return (data.dt == DataType::F32) && data.isa.sve;
Michalis Spyrou20fca522021-06-07 14:23:57 +010099 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200100 REGISTER_FP32_SVE(arm_compute::cpu::add_fp32_sve)
Sheri Zhang61243902021-01-12 18:25:16 +0000101 },
102 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100103 "sve_fp16_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100104 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +0100105 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100106 return (data.dt == DataType::F16) && data.isa.sve && data.isa.fp16;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100107 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200108 REGISTER_FP16_SVE(arm_compute::cpu::add_fp16_sve)
Sheri Zhang61243902021-01-12 18:25:16 +0000109 },
110 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100111 "sve_u8_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100112 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +0100113 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100114 return (data.dt == DataType::U8) && data.isa.sve;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100115 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200116 REGISTER_INTEGER_SVE(arm_compute::cpu::add_u8_sve)
Sheri Zhang61243902021-01-12 18:25:16 +0000117 },
118 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100119 "sve_s16_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100120 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +0100121 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100122 return (data.dt == DataType::S16) && data.isa.sve;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100123 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200124 REGISTER_INTEGER_SVE(arm_compute::cpu::add_s16_sve)
Sheri Zhang61243902021-01-12 18:25:16 +0000125 },
126 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100127 "sve_s32_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100128 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +0100129 {
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100130 return (data.dt == DataType::S32) && data.isa.sve;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100131 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200132 REGISTER_INTEGER_SVE(arm_compute::cpu::add_s32_sve)
Sheri Zhang61243902021-01-12 18:25:16 +0000133 },
Sheri Zhang61243902021-01-12 18:25:16 +0000134 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100135 "neon_fp32_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100136 [](const CpuAddKernelDataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200137 REGISTER_FP32_NEON(arm_compute::cpu::add_fp32_neon)
Sheri Zhang61243902021-01-12 18:25:16 +0000138 },
Sheri Zhang61243902021-01-12 18:25:16 +0000139 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100140 "neon_fp16_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100141 [](const CpuAddKernelDataTypeISASelectorData & data)
Michalis Spyrou20fca522021-06-07 14:23:57 +0100142 {
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000143 return (data.dt == DataType::F16) && data.isa.fp16;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100144 },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200145 REGISTER_FP16_NEON(arm_compute::cpu::add_fp16_neon)
Sheri Zhang61243902021-01-12 18:25:16 +0000146 },
Sheri Zhang61243902021-01-12 18:25:16 +0000147 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100148 "neon_u8_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100149 [](const CpuAddKernelDataTypeISASelectorData & data) { return (data.dt == DataType::U8); },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200150 REGISTER_INTEGER_NEON(arm_compute::cpu::add_u8_neon)
Sheri Zhang61243902021-01-12 18:25:16 +0000151 },
152 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100153 "neon_s16_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100154 [](const CpuAddKernelDataTypeISASelectorData & data) { return (data.dt == DataType::S16); },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200155 REGISTER_INTEGER_NEON(arm_compute::cpu::add_s16_neon)
Sheri Zhang61243902021-01-12 18:25:16 +0000156 },
157 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100158 "neon_s32_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100159 [](const CpuAddKernelDataTypeISASelectorData & data) { return (data.dt == DataType::S32); },
Dana Zlotnikbd2942d2021-11-15 08:46:04 +0200160 REGISTER_INTEGER_NEON(arm_compute::cpu::add_s32_neon)
Sheri Zhang61243902021-01-12 18:25:16 +0000161 },
Sheri Zhang61243902021-01-12 18:25:16 +0000162 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100163 "neon_qu8_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100164 [](const CpuAddKernelDataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
Sheri Zhang61243902021-01-12 18:25:16 +0000165 REGISTER_QASYMM8_NEON(arm_compute::cpu::add_qasymm8_neon)
166 },
167 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100168 "neon_qs8_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100169 [](const CpuAddKernelDataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
Sheri Zhang61243902021-01-12 18:25:16 +0000170 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::add_qasymm8_signed_neon)
171 },
172 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100173 "neon_qs16_add",
Gunes Bayir9b921be2022-07-28 17:44:00 +0100174 [](const CpuAddKernelDataTypeISASelectorData & data) { return (data.dt == DataType::QSYMM16); },
Sheri Zhang61243902021-01-12 18:25:16 +0000175 REGISTER_QSYMM16_NEON(arm_compute::cpu::add_qsymm16_neon)
Sheri Zhang61243902021-01-12 18:25:16 +0000176 }
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000177};
Sheri Zhang61243902021-01-12 18:25:16 +0000178
179Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst, ConvertPolicy policy)
180{
181 ARM_COMPUTE_UNUSED(policy);
182
183 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src0);
184 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src0, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
185 DataType::S16, DataType::QSYMM16, DataType::F16,
186 DataType::S32, DataType::F32);
Georgios Pinitasda816752021-07-02 09:22:14 +0100187 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &src1);
Sheri Zhang61243902021-01-12 18:25:16 +0000188
189 const TensorShape out_shape = TensorShape::broadcast_shape(src0.tensor_shape(), src1.tensor_shape());
190
191 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
192 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src0.tensor_shape().x() != src1.tensor_shape().x()) && ((src0.data_type() != src1.data_type()) || (src0.data_type() != dst.data_type())
193 || (src1.data_type() != dst.data_type())),
194 "Broadcasting across width is supported on configurations where all tensors have the same data type");
195
196 // Validate in case of configured dst
197 if(dst.total_size() > 0)
198 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100199 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &dst);
Sheri Zhang61243902021-01-12 18:25:16 +0000200 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
201 "Wrong shape for dst");
202 }
203
Viet-Hoa Do40b44192022-09-22 10:24:23 +0100204 const auto can_use_fixedpoint = add_q8_neon_fixedpoint_possible(&src0, &src1, &dst);
Gunes Bayir9b921be2022-07-28 17:44:00 +0100205 const auto uk = CpuAddKernel::get_implementation<CpuAddKernelDataTypeISASelectorData>(CpuAddKernelDataTypeISASelectorData{ src0.data_type(),
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100206 CPUInfo::get().get_isa(), can_use_fixedpoint });
Sheri Zhang61243902021-01-12 18:25:16 +0000207 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
208
209 return Status{};
210}
Sheri Zhang61243902021-01-12 18:25:16 +0000211} // namespace
212
213void CpuAddKernel::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ConvertPolicy policy)
214{
215 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
216 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*src0, *src1, *dst, policy));
217
Viet-Hoa Do40b44192022-09-22 10:24:23 +0100218 const auto can_use_fixedpoint = add_q8_neon_fixedpoint_possible(src0, src1, dst);
Gunes Bayir9b921be2022-07-28 17:44:00 +0100219 const auto uk = CpuAddKernel::get_implementation<CpuAddKernelDataTypeISASelectorData>(CpuAddKernelDataTypeISASelectorData{ src0->data_type(),
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100220 CPUInfo::get().get_isa(), can_use_fixedpoint });
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000221
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100222 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
223
224 _policy = policy;
225 _run_method = uk->ukernel;
226 _name = std::string("CpuAddKernel").append("/").append(uk->name);
Sheri Zhang61243902021-01-12 18:25:16 +0000227
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100228 // Auto initialize dst if not initialized
229 const TensorShape &out_shape = TensorShape::broadcast_shape(src0->tensor_shape(), src1->tensor_shape());
230 set_shape_if_empty(*dst, out_shape);
231 set_data_type_if_unknown(*dst, src0->data_type());
232
Sheri Zhang61243902021-01-12 18:25:16 +0000233 // Configure kernel window
Viet-Hoa Do9fc0b5c2022-10-20 11:18:17 +0100234 Window win;
235 std::tie(win, _split_dimension) = calculate_squashed_or_max_window(*src0, *src1);
236
237 ICpuKernel::configure(win);
Sheri Zhang61243902021-01-12 18:25:16 +0000238}
239
240Status CpuAddKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ConvertPolicy policy)
241{
242 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
243
244 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*src0, *src1, *dst, policy));
Sheri Zhang61243902021-01-12 18:25:16 +0000245
246 return Status{};
247}
248
249void CpuAddKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
250{
251 ARM_COMPUTE_UNUSED(info);
252 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +0200253 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Sheri Zhang61243902021-01-12 18:25:16 +0000254
255 ARM_COMPUTE_ERROR_ON(tensors.empty());
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100256 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Sheri Zhang61243902021-01-12 18:25:16 +0000257
258 const ITensor *src0 = tensors.get_const_tensor(TensorType::ACL_SRC_0);
259 const ITensor *src1 = tensors.get_const_tensor(TensorType::ACL_SRC_1);
260 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
261
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100262 _run_method(src0, src1, dst, _policy, window);
Sheri Zhang61243902021-01-12 18:25:16 +0000263}
264
265const char *CpuAddKernel::name() const
266{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100267 return _name.c_str();
Sheri Zhang61243902021-01-12 18:25:16 +0000268}
Dana Zlotnik4cdd6b82021-10-07 15:31:54 +0300269
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000270const std::vector<CpuAddKernel::AddKernel> &CpuAddKernel::get_available_kernels()
271{
272 return available_kernels;
273}
274
Dana Zlotnik4cdd6b82021-10-07 15:31:54 +0300275size_t CpuAddKernel::get_mws(const CPUInfo &platform, size_t thread_count) const
276{
Dana Zlotnikd7154db2021-11-10 11:50:58 +0200277 ARM_COMPUTE_UNUSED(thread_count);
Mohammed Suhail Munshi066607f2022-01-19 12:22:50 +0000278
Fadi Arafeh73bb6b72022-10-06 16:20:14 +0000279#if defined(ENABLE_FP32_KERNELS)
280 if(this->_run_method == &add_fp32_neon)
281 {
282 size_t mws = ICPPKernel::default_mws;
283 if(platform.get_cpu_model() == CPUModel::N1)
284 {
285 mws = default_mws_N1_fp32_neon;
286 }
287 else if(platform.get_cpu_model() == CPUModel::V1)
288 {
289 mws = default_mws_V1_fp32_neon;
290 }
291 else
292 {
293 return ICPPKernel::default_mws;
294 }
295
296 // tensor is 1D or was re-interpreted as 1D
297 if(this->window().shape().num_dimensions() == 1)
298 {
299 return mws;
300 }
301 else
302 {
303 // scale mws down by the number of elements along all the dimensions (x, z, w, etc) except the one
304 // that we parallelize along (the y dimension). This allows for parallelization when the Y_SIZE is small
305 // but the other sizes are large, which boosts performance.
306 mws = static_cast<size_t>(mws / (this->window().num_iterations_total() / this->window().num_iterations(1)));
307 return std::max(static_cast<size_t>(1), mws);
308 }
309 }
310#else /* ENABLE_FP32_KERNELS */
311 ARM_COMPUTE_UNUSED(platform);
312#endif /* ENABLE_FP32_KERNELS */
Mohammed Suhail Munshi066607f2022-01-19 12:22:50 +0000313 return ICPPKernel::default_mws;
Dana Zlotnik4cdd6b82021-10-07 15:31:54 +0300314}
315
Sheri Zhang61243902021-01-12 18:25:16 +0000316} // namespace kernels
317} // namespace cpu
318} // namespace arm_compute