blob: d908e4ed281fd099269ec7ffd3dbc01395649fa7 [file] [log] [blame]
Sheri Zhangfc6744a2021-01-13 15:54:05 +00001/*
Giorgio Arena5ae8d802021-11-18 18:02:13 +00002 * Copyright (c) 2021-2022 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"
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{
Giorgio Arena5ae8d802021-11-18 18:02:13 +000042static const std::vector<CpuSubKernel::SubKernel> available_kernels =
Sheri Zhangfc6744a2021-01-13 15:54:05 +000043{
44 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010045 "neon_fp32_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000046 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000047 REGISTER_FP32_NEON(arm_compute::cpu::sub_same_neon<float>)
48 },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000049 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010050 "neon_fp16_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000051 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16) && data.isa.fp16; },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000052 REGISTER_FP16_NEON(arm_compute::cpu::sub_same_neon<float16_t>)
53 },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000054 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010055 "neon_u8_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000056 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::U8); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000057 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<uint8_t>)
58 },
59 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010060 "neon_s16_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000061 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::S16); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000062 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int16_t>)
63 },
64 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010065 "neon_s32_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000066 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::S32); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000067 REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int32_t>)
68 },
69 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010070 "neon_qu8_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000071 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000072 REGISTER_QASYMM8_NEON(arm_compute::cpu::sub_qasymm8_neon)
73 },
74 {
Georgios Pinitas5fdde992021-06-25 05:42:57 +010075 "neon_qs8_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000076 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000077 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::sub_qasymm8_signed_neon)
78 },
79 {
Georgios Pinitasda816752021-07-02 09:22:14 +010080 "neon_qs16_sub",
Giorgio Arena5ae8d802021-11-18 18:02:13 +000081 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::QSYMM16); },
Sheri Zhangfc6744a2021-01-13 15:54:05 +000082 REGISTER_QSYMM16_NEON(arm_compute::cpu::sub_qsymm16_neon)
83 },
84};
85
Sheri Zhangfc6744a2021-01-13 15:54:05 +000086inline Status validate_arguments(const ITensorInfo &src0, const ITensorInfo &src1, const ITensorInfo &dst, ConvertPolicy policy)
87{
88 ARM_COMPUTE_UNUSED(policy);
89 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src0);
90 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,
91 DataType::F32);
Georgios Pinitasda816752021-07-02 09:22:14 +010092 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &src1);
Sheri Zhangfc6744a2021-01-13 15:54:05 +000093
Giorgio Arena5ae8d802021-11-18 18:02:13 +000094 const auto *uk = CpuSubKernel::get_implementation(DataTypeISASelectorData{ src0.data_type(), CPUInfo::get().get_isa() });
95
Sheri Zhangfc6744a2021-01-13 15:54:05 +000096 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
97
98 const TensorShape out_shape = TensorShape::broadcast_shape(src0.tensor_shape(), src1.tensor_shape());
99 ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible");
100
Georgios Pinitasda816752021-07-02 09:22:14 +0100101 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_quantized(src0.data_type()) && (policy == ConvertPolicy::WRAP),
102 "Convert policy cannot be WRAP if datatype is quantized");
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000103
104 // Validate in case of configured dst
105 if(dst.total_size() > 0)
106 {
Georgios Pinitasda816752021-07-02 09:22:14 +0100107 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &dst);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000108 ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst.tensor_shape(), 0),
109 "Wrong shape for dst");
110 }
111 return Status{};
112}
113} // namespace
114
115void CpuSubKernel::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ConvertPolicy policy)
116{
117 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
118 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*src0, *src1, *dst, policy));
119
SiCongLic7b1e842021-02-22 14:28:33 +0000120 const TensorShape &out_shape = TensorShape::broadcast_shape(src0->tensor_shape(), src1->tensor_shape());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000121
122 // Auto initialize dst if not initialized
123 set_shape_if_empty(*dst, out_shape);
Georgios Pinitasda816752021-07-02 09:22:14 +0100124 set_data_type_if_unknown(*dst, src0->data_type());
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000125
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000126 const auto *uk = CpuSubKernel::get_implementation(DataTypeISASelectorData{ src0->data_type(), CPUInfo::get().get_isa() });
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100127 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
128
129 _policy = policy;
130 _run_method = uk->ukernel;
131 _name = std::string("CpuSubKernel").append("/").append(uk->name);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000132
133 // CpuSubKernel doesn't need padding so update_window_and_padding() can be skipped
Jakub Sujak842ad212022-09-17 13:08:56 +0100134 Window win;
135 std::tie(win, _split_dimension) = calculate_squashed_or_max_window(*src0, *src1);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000136
137 ICpuKernel::configure(win);
138}
139
140Status CpuSubKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ConvertPolicy policy)
141{
142 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src0, src1, dst);
143 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*src0, *src1, *dst, policy));
144
145 return Status{};
146}
147
148void CpuSubKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
149{
150 ARM_COMPUTE_UNUSED(info);
151 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
152 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100153 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000154
155 const ITensor *src0 = tensors.get_const_tensor(TensorType::ACL_SRC_0);
156 const ITensor *src1 = tensors.get_const_tensor(TensorType::ACL_SRC_1);
157 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
158
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100159 _run_method(src0, src1, dst, _policy, window);
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000160}
161
162const char *CpuSubKernel::name() const
163{
Georgios Pinitas5fdde992021-06-25 05:42:57 +0100164 return _name.c_str();
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000165}
Giorgio Arena5ae8d802021-11-18 18:02:13 +0000166
167const std::vector<CpuSubKernel::SubKernel> &CpuSubKernel::get_available_kernels()
168{
169 return available_kernels;
170}
171
Sheri Zhangfc6744a2021-01-13 15:54:05 +0000172} // namespace kernels
173} // namespace cpu
174} // namespace arm_compute