blob: b8c9b244eec0e459e8c101fd107f946134ed45ef [file] [log] [blame]
George Wort5801a552018-12-13 17:50:26 +00001/*
Anton Vainer8a9a0fb2022-01-09 14:37:12 +02002 * Copyright (c) 2018-2022 Arm Limited.
George Wort5801a552018-12-13 17:50:26 +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
Michalis Spyrouaea14c62019-01-03 11:10:25 +000017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
George Wort5801a552018-12-13 17:50:26 +000018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michalis Spyrouaea14c62019-01-03 11:10:25 +000019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
George Wort5801a552018-12-13 17:50:26 +000020 * 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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NESelectKernel.h"
George Wort5801a552018-12-13 17:50:26 +000025
George Wort5801a552018-12-13 17:50:26 +000026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
George Wort5801a552018-12-13 17:50:26 +000028#include "arm_compute/core/ITensor.h"
George Wort5801a552018-12-13 17:50:26 +000029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010033#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
George Wort5801a552018-12-13 17:50:26 +000036
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020037#include "src/core/common/Registrars.h"
38
39#include "src/cpu/kernels/select/list.h"
40
George Wort5801a552018-12-13 17:50:26 +000041#include <arm_neon.h>
42#include <map>
43#include <string>
44
45namespace arm_compute
46{
47namespace
48{
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020049
50struct SelectKernelSelectorData
George Wort5801a552018-12-13 17:50:26 +000051{
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020052 DataType dt;
53 bool is_same_rank;
54};
George Wort5801a552018-12-13 17:50:26 +000055
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020056using SelectorPtr = std::add_pointer<bool(const SelectKernelSelectorData &data)>::type;
57using KernelPtr = std::add_pointer<void(const ITensor *, const ITensor *, const ITensor *, ITensor *, const Window &)>::type;
George Wort5801a552018-12-13 17:50:26 +000058
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020059struct SelectKernelSelector
60{
61 const char *name;
62 const SelectorPtr is_selected;
63 KernelPtr ukernel;
64};
65
66static const SelectKernelSelector available_kernels[] =
67{
George Wort5801a552018-12-13 17:50:26 +000068 {
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020069 "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 Wort5801a552018-12-13 17:50:26 +000072 },
George Wort5801a552018-12-13 17:50:26 +000073 {
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020074 "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 Wort5801a552018-12-13 17:50:26 +000078 {
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020079 "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 Wort5801a552018-12-13 17:50:26 +000083 {
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020084 "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 Wort5801a552018-12-13 17:50:26 +000088 {
Anton Vainer8a9a0fb2022-01-09 14:37:12 +020089 "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
150const SelectKernelSelector *get_implementation(const SelectKernelSelectorData &data)
151{
152 for(const auto &uk : available_kernels)
153 {
154 if(uk.is_selected(data))
George Wort5801a552018-12-13 17:50:26 +0000155 {
Anton Vainer8a9a0fb2022-01-09 14:37:12 +0200156 return &uk;
George Wort5801a552018-12-13 17:50:26 +0000157 }
George Wort5801a552018-12-13 17:50:26 +0000158 }
Anton Vainer8a9a0fb2022-01-09 14:37:12 +0200159 return nullptr;
George Wort5801a552018-12-13 17:50:26 +0000160}
Anton Vainer8a9a0fb2022-01-09 14:37:12 +0200161
George Wort5801a552018-12-13 17:50:26 +0000162} // namespace
163
164NESelectKernel::NESelectKernel()
Anton Vainer8a9a0fb2022-01-09 14:37:12 +0200165 : /*_function(nullptr), */ _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false)
George Wort5801a552018-12-13 17:50:26 +0000166{
167}
168
169void 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
SiCongLic7b1e842021-02-22 14:28:33 +0000183 Window win = calculate_max_window(*x->info());
George Wort5801a552018-12-13 17:50:26 +0000184 INEKernel::configure(win);
185}
186
187Status NESelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
188{
Georgios Pinitasddb93bb2020-10-02 16:38:59 +0100189 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(c, x, y);
George Wort5801a552018-12-13 17:50:26 +0000190 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(x);
Manuel Bottini8481d832019-12-10 15:28:40 +0000191 ARM_COMPUTE_RETURN_ERROR_ON(x->data_type() == DataType::UNKNOWN);
George Wort5801a552018-12-13 17:50:26 +0000192 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
209void 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 Vainer8a9a0fb2022-01-09 14:37:12 +0200214 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 Wort5801a552018-12-13 17:50:26 +0000221}
222} // namespace arm_compute