Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 1 | /* |
| 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 Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/kernels/CpuSubKernel.h" |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 25 | |
| 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 Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 32 | #include "src/cpu/kernels/sub/neon/list.h" |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace cpu |
| 37 | { |
| 38 | namespace kernels |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | struct SubSelectorData |
| 43 | { |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 44 | DataType dt; |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | using SubSelectorPtr = std::add_pointer<bool(const SubSelectorData &data)>::type; |
| 48 | using SubKernelPtr = std::add_pointer<void(const ITensor *, const ITensor *, ITensor *, const ConvertPolicy &, const Window &)>::type; |
| 49 | |
| 50 | struct SubKernel |
| 51 | { |
| 52 | const char *name; |
| 53 | const SubSelectorPtr is_selected; |
| 54 | SubKernelPtr ukernel; |
| 55 | }; |
| 56 | |
| 57 | static const SubKernel available_kernels[] = |
| 58 | { |
| 59 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 60 | "neon_fp32_sub", |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 61 | [](const SubSelectorData & data) { return (data.dt == DataType::F32); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 62 | REGISTER_FP32_NEON(arm_compute::cpu::sub_same_neon<float>) |
| 63 | }, |
| 64 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
| 65 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 66 | "neon_fp16_sub", |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 67 | [](const SubSelectorData & data) { return (data.dt == DataType::F16); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 68 | REGISTER_FP16_NEON(arm_compute::cpu::sub_same_neon<float16_t>) |
| 69 | }, |
| 70 | #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) */ |
| 71 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 72 | "neon_u8_sub", |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 73 | [](const SubSelectorData & data) { return (data.dt == DataType::U8); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 74 | REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<uint8_t>) |
| 75 | }, |
| 76 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 77 | "neon_s16_sub", |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 78 | [](const SubSelectorData & data) { return (data.dt == DataType::S16); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 79 | REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int16_t>) |
| 80 | }, |
| 81 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 82 | "neon_s32_sub", |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 83 | [](const SubSelectorData & data) { return (data.dt == DataType::S32); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 84 | REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon<int32_t>) |
| 85 | }, |
| 86 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 87 | "neon_qu8_sub", |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 88 | [](const SubSelectorData & data) { return (data.dt == DataType::QASYMM8); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 89 | REGISTER_QASYMM8_NEON(arm_compute::cpu::sub_qasymm8_neon) |
| 90 | }, |
| 91 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 92 | "neon_qs8_sub", |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 93 | [](const SubSelectorData & data) { return (data.dt == DataType::QASYMM8_SIGNED); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 94 | REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::sub_qasymm8_signed_neon) |
| 95 | }, |
| 96 | { |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 97 | "neon_qs16_sub", |
| 98 | [](const SubSelectorData & data) { return (data.dt == DataType::QSYMM16); }, |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 99 | 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 Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 109 | const SubKernel *get_implementation(DataType dt) |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 110 | { |
| 111 | for(const auto &uk : available_kernels) |
| 112 | { |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 113 | if(uk.is_selected({ dt })) |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 114 | { |
| 115 | return &uk; |
| 116 | } |
| 117 | } |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
| 121 | inline 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 Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &src1); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 128 | |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 129 | const auto *uk = get_implementation(src0.data_type()); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 130 | 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 Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 135 | 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 Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 137 | |
| 138 | // Validate in case of configured dst |
| 139 | if(dst.total_size() > 0) |
| 140 | { |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 141 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src0, &dst); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 142 | 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 | |
| 149 | void 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 | |
SiCongLi | c7b1e84 | 2021-02-22 14:28:33 +0000 | [diff] [blame] | 154 | const TensorShape &out_shape = TensorShape::broadcast_shape(src0->tensor_shape(), src1->tensor_shape()); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 155 | |
| 156 | // Auto initialize dst if not initialized |
| 157 | set_shape_if_empty(*dst, out_shape); |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 158 | set_data_type_if_unknown(*dst, src0->data_type()); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 159 | |
Georgios Pinitas | da81675 | 2021-07-02 09:22:14 +0100 | [diff] [blame] | 160 | const auto *uk = get_implementation(src0->data_type()); |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 161 | 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 Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 166 | |
| 167 | // CpuSubKernel doesn't need padding so update_window_and_padding() can be skipped |
SiCongLi | c7b1e84 | 2021-02-22 14:28:33 +0000 | [diff] [blame] | 168 | Window win = calculate_max_window(out_shape, Steps()); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 169 | |
| 170 | ICpuKernel::configure(win); |
| 171 | } |
| 172 | |
| 173 | Status 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 | |
| 181 | void 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 Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 186 | ARM_COMPUTE_ERROR_ON(_run_method == nullptr); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 187 | |
| 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 Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 192 | _run_method(src0, src1, dst, _policy, window); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | const char *CpuSubKernel::name() const |
| 196 | { |
Georgios Pinitas | 5fdde99 | 2021-06-25 05:42:57 +0100 | [diff] [blame] | 197 | return _name.c_str(); |
Sheri Zhang | fc6744a | 2021-01-13 15:54:05 +0000 | [diff] [blame] | 198 | } |
| 199 | } // namespace kernels |
| 200 | } // namespace cpu |
| 201 | } // namespace arm_compute |