blob: 22cd44288943c4f1a8bfcde0b292a8f0972194dc [file] [log] [blame]
George Wort5801a552018-12-13 17:50:26 +00001/*
Manuel Bottinib56c1752020-11-18 17:56:30 +00002 * Copyright (c) 2018-2021 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"
28#include "arm_compute/core/IAccessWindow.h"
29#include "arm_compute/core/ITensor.h"
George Wort5801a552018-12-13 17:50:26 +000030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010034#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
George Wort5801a552018-12-13 17:50:26 +000037
38#include <arm_neon.h>
39#include <map>
40#include <string>
41
42namespace arm_compute
43{
44namespace
45{
46template <typename ScalarType, typename VectorType>
47void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window,
48 const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *))
49{
50 Window win = window;
51 win.set(Window::DimX, Window::Dimension(0, 1, 1));
52
53 Iterator condition(cond, win);
54 Iterator input1(in1, win);
55 Iterator input2(in2, win);
56 Iterator output(out, win);
57
Michalis Spyroua4f378d2019-04-26 14:54:54 +010058 execute_window_loop(win, [&](const Coordinates &)
George Wort5801a552018-12-13 17:50:26 +000059 {
60 auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
61 const auto condition_ptr = reinterpret_cast<const uint8_t *>(condition.ptr());
62 const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
63 const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
64
65 int x = window_start_x;
66 for(; x <= limit; x += window_step_x)
67 {
68 const auto c = (*condition_conversion)(condition_ptr + x);
69 const auto a = wrapper::vloadq(input1_ptr + x);
70 const auto b = wrapper::vloadq(input2_ptr + x);
Michalis Spyrouaea14c62019-01-03 11:10:25 +000071 wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b));
George Wort5801a552018-12-13 17:50:26 +000072 }
73 for(; x < window_end_x; ++x)
74 {
75 const auto c = *(condition_ptr + x);
76 const auto a = *(input1_ptr + x);
77 const auto b = *(input2_ptr + x);
78 *(output_ptr + x) = static_cast<bool>(c) ? a : b;
79 }
80 },
81 condition, input1, input2, output);
82}
83
84template <typename ScalarType, typename VectorType>
85void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
86{
87 const auto window_step_x = 16 / sizeof(ScalarType);
88 const auto window_start_x = static_cast<int>(window.x().start());
89 const auto window_end_x = static_cast<int>(window.x().end());
90
Michalis Spyrouebdde652019-07-08 11:52:46 +010091 select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
George Wort5801a552018-12-13 17:50:26 +000092 {
93 static const auto zero = wrapper::vdup_n(static_cast<uint8_t>(0), arm_compute::wrapper::traits::vector_128_tag());
Michalis Spyrouaea14c62019-01-03 11:10:25 +000094 return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero);
George Wort5801a552018-12-13 17:50:26 +000095 });
96}
97
98template <typename ScalarType, typename VectorType>
99void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
100{
101 const auto window_step_x = 16 / sizeof(ScalarType);
102 const auto window_start_x = static_cast<int>(window.x().start());
103 const auto window_end_x = static_cast<int>(window.x().end());
104
Michalis Spyrouebdde652019-07-08 11:52:46 +0100105 select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
George Wort5801a552018-12-13 17:50:26 +0000106 {
107 static const auto zero = wrapper::vdup_n(static_cast<uint16_t>(0), arm_compute::wrapper::traits::vector_128_tag());
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000108 return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero);
George Wort5801a552018-12-13 17:50:26 +0000109 });
110}
111
112template <typename ScalarType, typename VectorType>
113void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
114{
115 const auto window_step_x = 16 / sizeof(ScalarType);
116 const auto window_start_x = static_cast<int>(window.x().start());
117 const auto window_end_x = static_cast<int>(window.x().end());
118
Michalis Spyrouebdde652019-07-08 11:52:46 +0100119 select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
George Wort5801a552018-12-13 17:50:26 +0000120 {
121 static const auto zero = wrapper::vdup_n(static_cast<uint32_t>(0), arm_compute::wrapper::traits::vector_128_tag());
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000122 return wrapper::vcgt(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vload(condition_ptr)))), zero);
George Wort5801a552018-12-13 17:50:26 +0000123 });
124}
125
126template <typename ScalarType>
127void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
128{
129 ARM_COMPUTE_UNUSED(window);
130
131 auto output_ptr = reinterpret_cast<ScalarType *>(out->buffer());
132 const auto condition_ptr = reinterpret_cast<const uint8_t *>(cond->buffer());
133 const auto input1_ptr = reinterpret_cast<const ScalarType *>(in1->buffer());
134 const auto input2_ptr = reinterpret_cast<const ScalarType *>(in2->buffer());
135
136 const int outer_size = cond->info()->total_size() / cond->info()->element_size();
137 const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size;
138 int offset = 0;
139 const int step = 16 / in1->info()->element_size();
140
141 for(int i = 0; i < outer_size; ++i)
142 {
143 int x = offset;
144 const auto input_ptr = static_cast<bool>(*(condition_ptr + i)) ? input1_ptr : input2_ptr;
145 for(; x <= offset + inner_size - step; x += step)
146 {
147 wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x));
148 }
149 if(x <= offset + inner_size - (step / 2))
150 {
151 wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x));
152 x += step / 2;
153 }
154 for(; x < offset + inner_size; ++x)
155 {
156 *(output_ptr + x) = *(input_ptr + x);
157 }
158 offset += inner_size;
159 }
160}
161} // namespace
162
163NESelectKernel::NESelectKernel()
164 : _function(nullptr), _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false)
165{
166}
167
168void NESelectKernel::configure(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output)
169{
170 ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output);
171
172 // Auto initialize output if not initialized
173 auto_init_if_empty(*output->info(), x->info()->tensor_shape(), 1, x->info()->data_type());
174 ARM_COMPUTE_ERROR_THROW_ON(validate(c->info(), x->info(), y->info(), output->info()));
175
176 _c = c;
177 _x = x;
178 _y = y;
179 _output = output;
180 _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions());
181
182 std::string function_to_call("op_");
183 function_to_call += string_from_data_type(x->info()->data_type());
184
185 static std::map<std::string, SelectFunction *> map_function;
186
187 if(_has_same_rank)
188 {
189 map_function =
190 {
191 { "op_S8", &select_op_8<int8_t, uint8x16_t> },
192 { "op_S16", &select_op_16<int16_t, uint16x8_t> },
193 { "op_S32", &select_op_32<int32_t, uint32x4_t> },
194 { "op_U8", &select_op_8<uint8_t, uint8x16_t> },
195 { "op_U16", &select_op_16<uint16_t, uint16x8_t> },
196 { "op_U32", &select_op_32<uint32_t, uint32x4_t> },
197 { "op_F32", &select_op_32<float, uint32x4_t> }
198 };
199#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
200 map_function["op_F16"] = &select_op_16<float16_t, uint16x8_t>;
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000201#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
George Wort5801a552018-12-13 17:50:26 +0000202 }
203 else
204 {
205 map_function =
206 {
207 { "op_S8", &select_op_not_same_rank<int8_t> },
208 { "op_S16", &select_op_not_same_rank<int16_t> },
209 { "op_S32", &select_op_not_same_rank<int32_t> },
210 { "op_U8", &select_op_not_same_rank<uint8_t> },
211 { "op_U16", &select_op_not_same_rank<uint16_t> },
212 { "op_U32", &select_op_not_same_rank<uint32_t> },
213 { "op_F32", &select_op_not_same_rank<float> }
214 };
215#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
216 map_function["op_F16"] = &select_op_not_same_rank<float16_t>;
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000217#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
George Wort5801a552018-12-13 17:50:26 +0000218 }
219
220 auto it = map_function.find(function_to_call);
221
222 if(it != map_function.end())
223 {
224 _function = it->second;
225 }
226
SiCongLic7b1e842021-02-22 14:28:33 +0000227 Window win = calculate_max_window(*x->info());
George Wort5801a552018-12-13 17:50:26 +0000228 INEKernel::configure(win);
229}
230
231Status NESelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output)
232{
Georgios Pinitasddb93bb2020-10-02 16:38:59 +0100233 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(c, x, y);
George Wort5801a552018-12-13 17:50:26 +0000234 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(x);
Manuel Bottini8481d832019-12-10 15:28:40 +0000235 ARM_COMPUTE_RETURN_ERROR_ON(x->data_type() == DataType::UNKNOWN);
George Wort5801a552018-12-13 17:50:26 +0000236 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, y);
237 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, y);
238 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::U8);
239
240 const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions());
241 ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape()));
242 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])));
243
244 if(output != nullptr && output->total_size() != 0)
245 {
246 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, output);
247 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, output);
248 }
249
250 return Status{};
251}
252
253void NESelectKernel::run(const Window &window, const ThreadInfo &info)
254{
255 ARM_COMPUTE_UNUSED(info);
256 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
257 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
258 ARM_COMPUTE_ERROR_ON(_function == nullptr);
259 _function(_c, _x, _y, _output, window);
260}
261} // namespace arm_compute