George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 1 | /* |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NESelectKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/Validate.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "utils/TypePrinter.h" |
| 36 | |
| 37 | #include <arm_neon.h> |
| 38 | #include <map> |
| 39 | #include <string> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | template <typename ScalarType, typename VectorType> |
| 46 | void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, |
| 47 | const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *)) |
| 48 | { |
| 49 | Window win = window; |
| 50 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 51 | |
| 52 | Iterator condition(cond, win); |
| 53 | Iterator input1(in1, win); |
| 54 | Iterator input2(in2, win); |
| 55 | Iterator output(out, win); |
| 56 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 57 | execute_window_loop(win, [&](const Coordinates &) |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 58 | { |
| 59 | auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr()); |
| 60 | const auto condition_ptr = reinterpret_cast<const uint8_t *>(condition.ptr()); |
| 61 | const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr()); |
| 62 | const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr()); |
| 63 | |
| 64 | int x = window_start_x; |
| 65 | for(; x <= limit; x += window_step_x) |
| 66 | { |
| 67 | const auto c = (*condition_conversion)(condition_ptr + x); |
| 68 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 69 | const auto b = wrapper::vloadq(input2_ptr + x); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 70 | wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b)); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 71 | } |
| 72 | for(; x < window_end_x; ++x) |
| 73 | { |
| 74 | const auto c = *(condition_ptr + x); |
| 75 | const auto a = *(input1_ptr + x); |
| 76 | const auto b = *(input2_ptr + x); |
| 77 | *(output_ptr + x) = static_cast<bool>(c) ? a : b; |
| 78 | } |
| 79 | }, |
| 80 | condition, input1, input2, output); |
| 81 | } |
| 82 | |
| 83 | template <typename ScalarType, typename VectorType> |
| 84 | void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 85 | { |
| 86 | const auto window_step_x = 16 / sizeof(ScalarType); |
| 87 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 88 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 89 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 90 | 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 Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 91 | { |
| 92 | static const auto zero = wrapper::vdup_n(static_cast<uint8_t>(0), arm_compute::wrapper::traits::vector_128_tag()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 93 | return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 94 | }); |
| 95 | } |
| 96 | |
| 97 | template <typename ScalarType, typename VectorType> |
| 98 | void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 99 | { |
| 100 | const auto window_step_x = 16 / sizeof(ScalarType); |
| 101 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 102 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 103 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 104 | 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 Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 105 | { |
| 106 | static const auto zero = wrapper::vdup_n(static_cast<uint16_t>(0), arm_compute::wrapper::traits::vector_128_tag()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 107 | return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 108 | }); |
| 109 | } |
| 110 | |
| 111 | template <typename ScalarType, typename VectorType> |
| 112 | void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 113 | { |
| 114 | const auto window_step_x = 16 / sizeof(ScalarType); |
| 115 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 116 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 117 | |
Michalis Spyrou | ebdde65 | 2019-07-08 11:52:46 +0100 | [diff] [blame] | 118 | 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 Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 119 | { |
| 120 | static const auto zero = wrapper::vdup_n(static_cast<uint32_t>(0), arm_compute::wrapper::traits::vector_128_tag()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 121 | return wrapper::vcgt(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vload(condition_ptr)))), zero); |
George Wort | 5801a55 | 2018-12-13 17:50:26 +0000 | [diff] [blame] | 122 | }); |
| 123 | } |
| 124 | |
| 125 | template <typename ScalarType> |
| 126 | void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 127 | { |
| 128 | ARM_COMPUTE_UNUSED(window); |
| 129 | |
| 130 | auto output_ptr = reinterpret_cast<ScalarType *>(out->buffer()); |
| 131 | const auto condition_ptr = reinterpret_cast<const uint8_t *>(cond->buffer()); |
| 132 | const auto input1_ptr = reinterpret_cast<const ScalarType *>(in1->buffer()); |
| 133 | const auto input2_ptr = reinterpret_cast<const ScalarType *>(in2->buffer()); |
| 134 | |
| 135 | const int outer_size = cond->info()->total_size() / cond->info()->element_size(); |
| 136 | const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size; |
| 137 | int offset = 0; |
| 138 | const int step = 16 / in1->info()->element_size(); |
| 139 | |
| 140 | for(int i = 0; i < outer_size; ++i) |
| 141 | { |
| 142 | int x = offset; |
| 143 | const auto input_ptr = static_cast<bool>(*(condition_ptr + i)) ? input1_ptr : input2_ptr; |
| 144 | for(; x <= offset + inner_size - step; x += step) |
| 145 | { |
| 146 | wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x)); |
| 147 | } |
| 148 | if(x <= offset + inner_size - (step / 2)) |
| 149 | { |
| 150 | wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x)); |
| 151 | x += step / 2; |
| 152 | } |
| 153 | for(; x < offset + inner_size; ++x) |
| 154 | { |
| 155 | *(output_ptr + x) = *(input_ptr + x); |
| 156 | } |
| 157 | offset += inner_size; |
| 158 | } |
| 159 | } |
| 160 | } // namespace |
| 161 | |
| 162 | NESelectKernel::NESelectKernel() |
| 163 | : _function(nullptr), _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | void NESelectKernel::configure(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output) |
| 168 | { |
| 169 | ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output); |
| 170 | |
| 171 | // Auto initialize output if not initialized |
| 172 | auto_init_if_empty(*output->info(), x->info()->tensor_shape(), 1, x->info()->data_type()); |
| 173 | ARM_COMPUTE_ERROR_THROW_ON(validate(c->info(), x->info(), y->info(), output->info())); |
| 174 | |
| 175 | _c = c; |
| 176 | _x = x; |
| 177 | _y = y; |
| 178 | _output = output; |
| 179 | _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions()); |
| 180 | |
| 181 | std::string function_to_call("op_"); |
| 182 | function_to_call += string_from_data_type(x->info()->data_type()); |
| 183 | |
| 184 | static std::map<std::string, SelectFunction *> map_function; |
| 185 | |
| 186 | if(_has_same_rank) |
| 187 | { |
| 188 | map_function = |
| 189 | { |
| 190 | { "op_S8", &select_op_8<int8_t, uint8x16_t> }, |
| 191 | { "op_S16", &select_op_16<int16_t, uint16x8_t> }, |
| 192 | { "op_S32", &select_op_32<int32_t, uint32x4_t> }, |
| 193 | { "op_U8", &select_op_8<uint8_t, uint8x16_t> }, |
| 194 | { "op_U16", &select_op_16<uint16_t, uint16x8_t> }, |
| 195 | { "op_U32", &select_op_32<uint32_t, uint32x4_t> }, |
| 196 | { "op_F32", &select_op_32<float, uint32x4_t> } |
| 197 | }; |
| 198 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 199 | map_function["op_F16"] = &select_op_16<float16_t, uint16x8_t>; |
| 200 | #endif /* ARM_COMPUTE_AARCH64_V8_2 */ |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | map_function = |
| 205 | { |
| 206 | { "op_S8", &select_op_not_same_rank<int8_t> }, |
| 207 | { "op_S16", &select_op_not_same_rank<int16_t> }, |
| 208 | { "op_S32", &select_op_not_same_rank<int32_t> }, |
| 209 | { "op_U8", &select_op_not_same_rank<uint8_t> }, |
| 210 | { "op_U16", &select_op_not_same_rank<uint16_t> }, |
| 211 | { "op_U32", &select_op_not_same_rank<uint32_t> }, |
| 212 | { "op_F32", &select_op_not_same_rank<float> } |
| 213 | }; |
| 214 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 215 | map_function["op_F16"] = &select_op_not_same_rank<float16_t>; |
| 216 | #endif /* ARM_COMPUTE_AARCH64_V8_2 */ |
| 217 | } |
| 218 | |
| 219 | auto it = map_function.find(function_to_call); |
| 220 | |
| 221 | if(it != map_function.end()) |
| 222 | { |
| 223 | _function = it->second; |
| 224 | } |
| 225 | |
| 226 | Window win = calculate_max_window(x->info()->valid_region()); |
| 227 | INEKernel::configure(win); |
| 228 | } |
| 229 | |
| 230 | Status NESelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) |
| 231 | { |
| 232 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(x); |
| 233 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(x, |
| 234 | 1, |
| 235 | DataType::U8, DataType::S8, |
| 236 | DataType::U16, DataType::S16, |
| 237 | DataType::U32, DataType::S32, |
| 238 | DataType::F16, DataType::F32); |
| 239 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, y); |
| 240 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, y); |
| 241 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::U8); |
| 242 | |
| 243 | const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions()); |
| 244 | ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape())); |
| 245 | 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]))); |
| 246 | |
| 247 | if(output != nullptr && output->total_size() != 0) |
| 248 | { |
| 249 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, output); |
| 250 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, output); |
| 251 | } |
| 252 | |
| 253 | return Status{}; |
| 254 | } |
| 255 | |
| 256 | void NESelectKernel::run(const Window &window, const ThreadInfo &info) |
| 257 | { |
| 258 | ARM_COMPUTE_UNUSED(info); |
| 259 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 260 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 261 | ARM_COMPUTE_ERROR_ON(_function == nullptr); |
| 262 | _function(_c, _x, _y, _output, window); |
| 263 | } |
| 264 | } // namespace arm_compute |