George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 1 | /* |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 2 | * Copyright (c) 2018-2022 Arm Limited. |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 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 |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NESelectKernel.h" |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 25 | |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/ITensor.h" |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/CPP/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 33 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 36 | |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 37 | #include "src/core/common/Registrars.h" |
| 38 | |
| 39 | #include "src/cpu/kernels/select/list.h" |
| 40 | |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 41 | #include <arm_neon.h> |
| 42 | #include <map> |
| 43 | #include <string> |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | namespace |
| 48 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 49 | |
| 50 | struct SelectKernelSelectorData |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 51 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 52 | DataType dt; |
| 53 | bool is_same_rank; |
| 54 | }; |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 55 | |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 56 | using SelectorPtr = std::add_pointer<bool(const SelectKernelSelectorData &data)>::type; |
| 57 | using KernelPtr = std::add_pointer<void(const ITensor *, const ITensor *, const ITensor *, ITensor *, const Window &)>::type; |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 58 | |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 59 | struct SelectKernelSelector |
| 60 | { |
| 61 | const char *name; |
| 62 | const SelectorPtr is_selected; |
| 63 | KernelPtr ukernel; |
| 64 | }; |
| 65 | |
| 66 | static const SelectKernelSelector available_kernels[] = |
| 67 | { |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 68 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 69 | "neon_s8_same_rank", |
| 70 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::S8 && data.is_same_rank == true; }, |
| 71 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s8_select_same_rank) |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 72 | }, |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 73 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 74 | "neon_s16_same_rank", |
| 75 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::S16 && data.is_same_rank == true; }, |
| 76 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s16_select_same_rank) |
| 77 | }, |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 78 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 79 | "neon_s32_same_rank", |
| 80 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::S32 && data.is_same_rank == true; }, |
| 81 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s32_select_same_rank) |
| 82 | }, |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 83 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 84 | "neon_u8_same_rank", |
| 85 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::U8 && data.is_same_rank == true; }, |
| 86 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u8_select_same_rank) |
| 87 | }, |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 88 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 89 | "neon_u16_same_rank", |
| 90 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::U16 && data.is_same_rank == true; }, |
| 91 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u16_select_same_rank) |
| 92 | }, |
| 93 | { |
| 94 | "neon_u32_same_rank", |
| 95 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::U32 && data.is_same_rank == true; }, |
| 96 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u32_select_same_rank) |
| 97 | }, |
| 98 | { |
| 99 | "neon_s8_not_same_rank", |
| 100 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::S8 && data.is_same_rank == false; }, |
| 101 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s8_select_not_same_rank) |
| 102 | }, |
| 103 | { |
| 104 | "neon_s16_not_same_rank", |
| 105 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::S16 && data.is_same_rank == false; }, |
| 106 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s16_select_not_same_rank) |
| 107 | }, |
| 108 | { |
| 109 | "neon_s32_not_same_rank", |
| 110 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::S32 && data.is_same_rank == false; }, |
| 111 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s32_select_not_same_rank) |
| 112 | }, |
| 113 | { |
| 114 | "neon_u8_not_same_rank", |
| 115 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::U8 && data.is_same_rank == false; }, |
| 116 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u8_select_not_same_rank) |
| 117 | }, |
| 118 | { |
| 119 | "neon_u16_not_same_rank", |
| 120 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::U16 && data.is_same_rank == false; }, |
| 121 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u16_select_not_same_rank) |
| 122 | }, |
| 123 | { |
| 124 | "neon_u32_not_same_rank", |
| 125 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::U32 && data.is_same_rank == false; }, |
| 126 | REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u32_select_not_same_rank) |
| 127 | }, |
| 128 | { |
| 129 | "neon_f16_same_rank", |
| 130 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::F16 && data.is_same_rank == true; }, |
| 131 | REGISTER_FP16_NEON(arm_compute::cpu::neon_f16_select_same_rank) |
| 132 | }, |
| 133 | { |
| 134 | "neon_f16_not_same_rank", |
| 135 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::F16 && data.is_same_rank == false; }, |
| 136 | REGISTER_FP16_NEON(arm_compute::cpu::neon_f16_select_not_same_rank) |
| 137 | }, |
| 138 | { |
| 139 | "neon_f32_same_rank", |
| 140 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::F32 && data.is_same_rank == true; }, |
| 141 | REGISTER_FP32_NEON(arm_compute::cpu::neon_f32_select_same_rank) |
| 142 | }, |
| 143 | { |
| 144 | "neon_f32_not_same_rank", |
| 145 | [](const SelectKernelSelectorData & data) { return data.dt == DataType::F32 && data.is_same_rank == false; }, |
| 146 | REGISTER_FP32_NEON(arm_compute::cpu::neon_f32_select_not_same_rank) |
| 147 | }, |
| 148 | }; |
| 149 | |
| 150 | const SelectKernelSelector *get_implementation(const SelectKernelSelectorData &data) |
| 151 | { |
| 152 | for(const auto &uk : available_kernels) |
| 153 | { |
| 154 | if(uk.is_selected(data)) |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 155 | { |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 156 | return &uk; |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 157 | } |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 158 | } |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 159 | return nullptr; |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 160 | } |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 161 | |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 162 | } // namespace |
| 163 | |
| 164 | NESelectKernel::NESelectKernel() |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 165 | : /*_function(nullptr), */ _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false) |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 166 | { |
| 167 | } |
| 168 | |
| 169 | void NESelectKernel::configure(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output) |
| 170 | { |
| 171 | ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output); |
| 172 | |
| 173 | // Auto initialize output if not initialized |
| 174 | auto_init_if_empty(*output->info(), x->info()->tensor_shape(), 1, x->info()->data_type()); |
| 175 | ARM_COMPUTE_ERROR_THROW_ON(validate(c->info(), x->info(), y->info(), output->info())); |
| 176 | |
| 177 | _c = c; |
| 178 | _x = x; |
| 179 | _y = y; |
| 180 | _output = output; |
| 181 | _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions()); |
| 182 | |
SiCongLi | c7b1e84 | 2021-02-22 14:28:33 +0000 | [diff] [blame] | 183 | Window win = calculate_max_window(*x->info()); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 184 | INEKernel::configure(win); |
| 185 | } |
| 186 | |
| 187 | Status NESelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) |
| 188 | { |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 189 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(c, x, y); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 190 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(x); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 191 | ARM_COMPUTE_RETURN_ERROR_ON(x->data_type() == DataType::UNKNOWN); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 192 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, y); |
| 193 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, y); |
| 194 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::U8); |
| 195 | |
| 196 | const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions()); |
| 197 | ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape())); |
| 198 | ARM_COMPUTE_RETURN_ERROR_ON(!is_same_rank && ((c->tensor_shape().num_dimensions() > 1) || (c->tensor_shape().x() != x->tensor_shape()[x->tensor_shape().num_dimensions() - 1]))); |
| 199 | |
| 200 | if(output != nullptr && output->total_size() != 0) |
| 201 | { |
| 202 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, output); |
| 203 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, output); |
| 204 | } |
| 205 | |
| 206 | return Status{}; |
| 207 | } |
| 208 | |
| 209 | void NESelectKernel::run(const Window &window, const ThreadInfo &info) |
| 210 | { |
| 211 | ARM_COMPUTE_UNUSED(info); |
| 212 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 213 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Anton Vainer | 8a9a0fb | 2022-01-09 14:37:12 +0200 | [diff] [blame] | 214 | ARM_COMPUTE_ERROR_ON(_output == nullptr); |
| 215 | ARM_COMPUTE_ERROR_ON(_output->info() == nullptr); |
| 216 | |
| 217 | const auto *uk = get_implementation(SelectKernelSelectorData{ _output->info()->data_type(), _has_same_rank }); |
| 218 | ARM_COMPUTE_ERROR_ON(uk == nullptr); |
| 219 | ARM_COMPUTE_ERROR_ON(uk->ukernel == nullptr); |
| 220 | uk->ukernel(_c, _x, _y, _output, window); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 221 | } |
| 222 | } // namespace arm_compute |