From 8a9a0fbf6509159cbb8721500517a1497c390f27 Mon Sep 17 00:00:00 2001 From: Anton Vainer Date: Sun, 9 Jan 2022 14:37:12 +0200 Subject: Select kernel decoupling Resolves COMPMID-4614 Signed-off-by: Anton Vainer Change-Id: I19476d43b8e685de2eed973425d5d31b9cdb84ca Signed-off-by: Anton Vainer Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/6960 Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Giorgio Arena --- Android.bp | 4 + filelist.json | 8 +- src/core/NEON/kernels/NESelectKernel.cpp | 260 ++++++++++-------------- src/core/NEON/kernels/NESelectKernel.h | 22 +- src/cpu/kernels/select/generic/neon/fp16.cpp | 48 +++++ src/cpu/kernels/select/generic/neon/fp32.cpp | 45 ++++ src/cpu/kernels/select/generic/neon/impl.cpp | 191 +++++++++++++++++ src/cpu/kernels/select/generic/neon/impl.h | 54 +++++ src/cpu/kernels/select/generic/neon/integer.cpp | 87 ++++++++ src/cpu/kernels/select/list.h | 58 ++++++ 10 files changed, 611 insertions(+), 166 deletions(-) create mode 100644 src/cpu/kernels/select/generic/neon/fp16.cpp create mode 100644 src/cpu/kernels/select/generic/neon/fp32.cpp create mode 100644 src/cpu/kernels/select/generic/neon/impl.cpp create mode 100644 src/cpu/kernels/select/generic/neon/impl.h create mode 100644 src/cpu/kernels/select/generic/neon/integer.cpp create mode 100644 src/cpu/kernels/select/list.h diff --git a/Android.bp b/Android.bp index 7e18b3dbb9..136714b260 100644 --- a/Android.bp +++ b/Android.bp @@ -521,6 +521,10 @@ cc_library_static { "src/cpu/kernels/scale/sve/integer.cpp", "src/cpu/kernels/scale/sve/qasymm8.cpp", "src/cpu/kernels/scale/sve/qasymm8_signed.cpp", + "src/cpu/kernels/select/generic/neon/fp16.cpp", + "src/cpu/kernels/select/generic/neon/fp32.cpp", + "src/cpu/kernels/select/generic/neon/impl.cpp", + "src/cpu/kernels/select/generic/neon/integer.cpp", "src/cpu/kernels/softmax/generic/neon/fp16.cpp", "src/cpu/kernels/softmax/generic/neon/fp32.cpp", "src/cpu/kernels/softmax/generic/neon/qasymm8.cpp", diff --git a/filelist.json b/filelist.json index a4773ded04..37c0f1db12 100644 --- a/filelist.json +++ b/filelist.json @@ -1873,7 +1873,13 @@ "common": [ "src/core/NEON/kernels/NESelectKernel.cpp", "src/runtime/NEON/functions/NESelect.cpp" - ] + ], + "neon": { + "common": [ "src/cpu/kernels/select/generic/neon/impl.cpp" ], + "fp32": [ "src/cpu/kernels/select/generic/neon/fp32.cpp" ], + "fp16": [ "src/cpu/kernels/select/generic/neon/fp16.cpp" ], + "integer": [ "src/cpu/kernels/select/generic/neon/integer.cpp" ] + } } }, "Slice": { diff --git a/src/core/NEON/kernels/NESelectKernel.cpp b/src/core/NEON/kernels/NESelectKernel.cpp index 7c988e9fab..b8c9b244ee 100644 --- a/src/core/NEON/kernels/NESelectKernel.cpp +++ b/src/core/NEON/kernels/NESelectKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021 Arm Limited. + * Copyright (c) 2018-2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -34,6 +34,10 @@ #include "src/core/helpers/AutoConfiguration.h" #include "src/core/helpers/WindowHelpers.h" +#include "src/core/common/Registrars.h" + +#include "src/cpu/kernels/select/list.h" + #include #include #include @@ -42,125 +46,123 @@ namespace arm_compute { namespace { -template -void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, - const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *)) -{ - Window win = window; - win.set(Window::DimX, Window::Dimension(0, 1, 1)); - - Iterator condition(cond, win); - Iterator input1(in1, win); - Iterator input2(in2, win); - Iterator output(out, win); - - execute_window_loop(win, [&](const Coordinates &) - { - auto output_ptr = reinterpret_cast(output.ptr()); - const auto condition_ptr = reinterpret_cast(condition.ptr()); - const auto input1_ptr = reinterpret_cast(input1.ptr()); - const auto input2_ptr = reinterpret_cast(input2.ptr()); - int x = window_start_x; - for(; x <= limit; x += window_step_x) - { - const auto c = (*condition_conversion)(condition_ptr + x); - const auto a = wrapper::vloadq(input1_ptr + x); - const auto b = wrapper::vloadq(input2_ptr + x); - wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b)); - } - for(; x < window_end_x; ++x) - { - const auto c = *(condition_ptr + x); - const auto a = *(input1_ptr + x); - const auto b = *(input2_ptr + x); - *(output_ptr + x) = static_cast(c) ? a : b; - } - }, - condition, input1, input2, output); -} - -template -void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +struct SelectKernelSelectorData { - const auto window_step_x = 16 / sizeof(ScalarType); - const auto window_start_x = static_cast(window.x().start()); - const auto window_end_x = static_cast(window.x().end()); + DataType dt; + bool is_same_rank; +}; - select_op(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 - { - static const auto zero = wrapper::vdup_n(static_cast(0), arm_compute::wrapper::traits::vector_128_tag()); - return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero); - }); -} +using SelectorPtr = std::add_pointer::type; +using KernelPtr = std::add_pointer::type; -template -void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +struct SelectKernelSelector { - const auto window_step_x = 16 / sizeof(ScalarType); - const auto window_start_x = static_cast(window.x().start()); - const auto window_end_x = static_cast(window.x().end()); + const char *name; + const SelectorPtr is_selected; + KernelPtr ukernel; +}; - select_op(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 - { - static const auto zero = wrapper::vdup_n(static_cast(0), arm_compute::wrapper::traits::vector_128_tag()); - return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero); - }); -} - -template -void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +static const SelectKernelSelector available_kernels[] = { - const auto window_step_x = 16 / sizeof(ScalarType); - const auto window_start_x = static_cast(window.x().start()); - const auto window_end_x = static_cast(window.x().end()); - - select_op(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 { - static const auto zero = wrapper::vdup_n(static_cast(0), arm_compute::wrapper::traits::vector_128_tag()); - return wrapper::vcgt(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vload(condition_ptr)))), zero); - }); -} + "neon_s8_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::S8 && data.is_same_rank == true; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s8_select_same_rank) + }, + { + "neon_s16_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::S16 && data.is_same_rank == true; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s16_select_same_rank) + }, + { + "neon_s32_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::S32 && data.is_same_rank == true; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s32_select_same_rank) + }, + { + "neon_u8_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::U8 && data.is_same_rank == true; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u8_select_same_rank) + }, + { + "neon_u16_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::U16 && data.is_same_rank == true; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u16_select_same_rank) + }, + { + "neon_u32_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::U32 && data.is_same_rank == true; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u32_select_same_rank) + }, + { + "neon_s8_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::S8 && data.is_same_rank == false; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s8_select_not_same_rank) + }, + { + "neon_s16_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::S16 && data.is_same_rank == false; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s16_select_not_same_rank) + }, + { + "neon_s32_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::S32 && data.is_same_rank == false; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_s32_select_not_same_rank) + }, + { + "neon_u8_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::U8 && data.is_same_rank == false; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u8_select_not_same_rank) + }, + { + "neon_u16_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::U16 && data.is_same_rank == false; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u16_select_not_same_rank) + }, + { + "neon_u32_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::U32 && data.is_same_rank == false; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::neon_u32_select_not_same_rank) + }, + { + "neon_f16_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::F16 && data.is_same_rank == true; }, + REGISTER_FP16_NEON(arm_compute::cpu::neon_f16_select_same_rank) + }, + { + "neon_f16_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::F16 && data.is_same_rank == false; }, + REGISTER_FP16_NEON(arm_compute::cpu::neon_f16_select_not_same_rank) + }, + { + "neon_f32_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::F32 && data.is_same_rank == true; }, + REGISTER_FP32_NEON(arm_compute::cpu::neon_f32_select_same_rank) + }, + { + "neon_f32_not_same_rank", + [](const SelectKernelSelectorData & data) { return data.dt == DataType::F32 && data.is_same_rank == false; }, + REGISTER_FP32_NEON(arm_compute::cpu::neon_f32_select_not_same_rank) + }, +}; -template -void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +const SelectKernelSelector *get_implementation(const SelectKernelSelectorData &data) { - ARM_COMPUTE_UNUSED(window); - - auto output_ptr = reinterpret_cast(out->buffer()); - const auto condition_ptr = reinterpret_cast(cond->buffer()); - const auto input1_ptr = reinterpret_cast(in1->buffer()); - const auto input2_ptr = reinterpret_cast(in2->buffer()); - - const int outer_size = cond->info()->total_size() / cond->info()->element_size(); - const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size; - int offset = 0; - const int step = 16 / in1->info()->element_size(); - - for(int i = 0; i < outer_size; ++i) + for(const auto &uk : available_kernels) { - int x = offset; - const auto input_ptr = static_cast(*(condition_ptr + i)) ? input1_ptr : input2_ptr; - for(; x <= offset + inner_size - step; x += step) - { - wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x)); - } - if(x <= offset + inner_size - (step / 2)) - { - wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x)); - x += step / 2; - } - for(; x < offset + inner_size; ++x) + if(uk.is_selected(data)) { - *(output_ptr + x) = *(input_ptr + x); + return &uk; } - offset += inner_size; } + return nullptr; } + } // namespace NESelectKernel::NESelectKernel() - : _function(nullptr), _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false) + : /*_function(nullptr), */ _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false) { } @@ -178,51 +180,6 @@ void NESelectKernel::configure(const ITensor *c, const ITensor *x, const ITensor _output = output; _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions()); - std::string function_to_call("op_"); - function_to_call += string_from_data_type(x->info()->data_type()); - - static std::map map_function; - - if(_has_same_rank) - { - map_function = - { - { "op_S8", &select_op_8 }, - { "op_S16", &select_op_16 }, - { "op_S32", &select_op_32 }, - { "op_U8", &select_op_8 }, - { "op_U16", &select_op_16 }, - { "op_U32", &select_op_32 }, - { "op_F32", &select_op_32 } - }; -#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - map_function["op_F16"] = &select_op_16; -#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ - } - else - { - map_function = - { - { "op_S8", &select_op_not_same_rank }, - { "op_S16", &select_op_not_same_rank }, - { "op_S32", &select_op_not_same_rank }, - { "op_U8", &select_op_not_same_rank }, - { "op_U16", &select_op_not_same_rank }, - { "op_U32", &select_op_not_same_rank }, - { "op_F32", &select_op_not_same_rank } - }; -#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - map_function["op_F16"] = &select_op_not_same_rank; -#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ - } - - auto it = map_function.find(function_to_call); - - if(it != map_function.end()) - { - _function = it->second; - } - Window win = calculate_max_window(*x->info()); INEKernel::configure(win); } @@ -254,7 +211,12 @@ void NESelectKernel::run(const Window &window, const ThreadInfo &info) ARM_COMPUTE_UNUSED(info); ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); - ARM_COMPUTE_ERROR_ON(_function == nullptr); - _function(_c, _x, _y, _output, window); + ARM_COMPUTE_ERROR_ON(_output == nullptr); + ARM_COMPUTE_ERROR_ON(_output->info() == nullptr); + + const auto *uk = get_implementation(SelectKernelSelectorData{ _output->info()->data_type(), _has_same_rank }); + ARM_COMPUTE_ERROR_ON(uk == nullptr); + ARM_COMPUTE_ERROR_ON(uk->ukernel == nullptr); + uk->ukernel(_c, _x, _y, _output, window); } } // namespace arm_compute diff --git a/src/core/NEON/kernels/NESelectKernel.h b/src/core/NEON/kernels/NESelectKernel.h index f7142feff8..e82105a68e 100644 --- a/src/core/NEON/kernels/NESelectKernel.h +++ b/src/core/NEON/kernels/NESelectKernel.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2020, 2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -82,22 +82,12 @@ public: void run(const Window &window, const ThreadInfo &info) override; private: - /** Common signature for all the specialised select functions - * - * @param[in] c Condition input tensor. Data types supported: U8. - * @param[in] x First input tensor. Data types supported: All. - * @param[in] y Second input tensor. Data types supported: Same as @p x - * @param[in] output Output tensor. Data types supported: Same as @p x. - */ - using SelectFunction = void(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window); - /** Select function to use for the particular tensor types passed to configure() */ - SelectFunction *_function; - const ITensor *_c; /**< Condition tensor */ - const ITensor *_x; /**< Source tensor 1 */ - const ITensor *_y; /**< Source tensor 2 */ - ITensor *_output; /**< Destination tensor */ - bool _has_same_rank; /**< Flag that indicates if condition tensor and other inputs have the same rank */ + const ITensor *_c; /**< Condition tensor */ + const ITensor *_x; /**< Source tensor 1 */ + const ITensor *_y; /**< Source tensor 2 */ + ITensor *_output; /**< Destination tensor */ + bool _has_same_rank; /**< Flag that indicates if condition tensor and other inputs have the same rank */ }; } // namespace arm_compute #endif /* ARM_COMPUTE_NESELECTKERNEL_H */ diff --git a/src/cpu/kernels/select/generic/neon/fp16.cpp b/src/cpu/kernels/select/generic/neon/fp16.cpp new file mode 100644 index 0000000000..b460213c72 --- /dev/null +++ b/src/cpu/kernels/select/generic/neon/fp16.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) + +#include "src/cpu/kernels/select/generic/neon/impl.h" + +#include "arm_compute/core/Helpers.h" +#include "src/core/NEON/wrapper/wrapper.h" + +namespace arm_compute +{ +namespace cpu +{ +void neon_f16_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_16(c, x, y, output, window); +} +void neon_f16_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} + +} // namespace cpu + +} // namespace arm_compute + +#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */ \ No newline at end of file diff --git a/src/cpu/kernels/select/generic/neon/fp32.cpp b/src/cpu/kernels/select/generic/neon/fp32.cpp new file mode 100644 index 0000000000..63fd594901 --- /dev/null +++ b/src/cpu/kernels/select/generic/neon/fp32.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "src/cpu/kernels/select/generic/neon/impl.h" + +#include "arm_compute/core/Helpers.h" +#include "src/core/NEON/wrapper/wrapper.h" + +namespace arm_compute +{ +namespace cpu +{ +void neon_f32_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_32(c, x, y, output, window); +} +void neon_f32_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} + +} // namespace cpu + +} // namespace arm_compute diff --git a/src/cpu/kernels/select/generic/neon/impl.cpp b/src/cpu/kernels/select/generic/neon/impl.cpp new file mode 100644 index 0000000000..62c46f3c18 --- /dev/null +++ b/src/cpu/kernels/select/generic/neon/impl.cpp @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "arm_compute/core/TensorInfo.h" +#include "src/cpu/kernels/select/generic/neon/impl.h" +#include "src/core/NEON/NEAsymm.h" + +#include +#include +#include + +namespace arm_compute +{ +namespace cpu +{ +template +void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, + const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *)) +{ + Window win = window; + win.set(Window::DimX, Window::Dimension(0, 1, 1)); + + Iterator condition(cond, win); + Iterator input1(in1, win); + Iterator input2(in2, win); + Iterator output(out, win); + + execute_window_loop(win, [&](const Coordinates &) + { + auto output_ptr = reinterpret_cast(output.ptr()); + const auto condition_ptr = reinterpret_cast(condition.ptr()); + const auto input1_ptr = reinterpret_cast(input1.ptr()); + const auto input2_ptr = reinterpret_cast(input2.ptr()); + + int x = window_start_x; + for(; x <= limit; x += window_step_x) + { + const auto c = (*condition_conversion)(condition_ptr + x); + const auto a = wrapper::vloadq(input1_ptr + x); + const auto b = wrapper::vloadq(input2_ptr + x); + wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b)); + } + for(; x < window_end_x; ++x) + { + const auto c = *(condition_ptr + x); + const auto a = *(input1_ptr + x); + const auto b = *(input2_ptr + x); + *(output_ptr + x) = static_cast(c) ? a : b; + } + }, + condition, input1, input2, output); +} + +template +void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +{ + const auto window_step_x = 16 / sizeof(ScalarType); + const auto window_start_x = static_cast(window.x().start()); + const auto window_end_x = static_cast(window.x().end()); + + select_op(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 + { + static const auto zero = wrapper::vdup_n(static_cast(0), arm_compute::wrapper::traits::vector_128_tag()); + return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero); + }); +} + +template +void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +{ + const auto window_step_x = 16 / sizeof(ScalarType); + const auto window_start_x = static_cast(window.x().start()); + const auto window_end_x = static_cast(window.x().end()); + + select_op(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 + { + static const auto zero = wrapper::vdup_n(static_cast(0), arm_compute::wrapper::traits::vector_128_tag()); + return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero); + }); +} + +template +void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +{ + const auto window_step_x = 16 / sizeof(ScalarType); + const auto window_start_x = static_cast(window.x().start()); + const auto window_end_x = static_cast(window.x().end()); + + select_op(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 + { + static const auto zero = wrapper::vdup_n(static_cast(0), arm_compute::wrapper::traits::vector_128_tag()); + return wrapper::vcgt(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vload(condition_ptr)))), zero); + }); +} + +template +void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) +{ + ARM_COMPUTE_UNUSED(window); + + auto output_ptr = reinterpret_cast(out->buffer()); + const auto condition_ptr = reinterpret_cast(cond->buffer()); + const auto input1_ptr = reinterpret_cast(in1->buffer()); + const auto input2_ptr = reinterpret_cast(in2->buffer()); + + const int outer_size = cond->info()->total_size() / cond->info()->element_size(); + const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size; + int offset = 0; + const int step = 16 / in1->info()->element_size(); + + for(int i = 0; i < outer_size; ++i) + { + int x = offset; + const auto input_ptr = static_cast(*(condition_ptr + i)) ? input1_ptr : input2_ptr; + for(; x <= offset + inner_size - step; x += step) + { + wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x)); + } + if(x <= offset + inner_size - (step / 2)) + { + wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x)); + x += step / 2; + } + for(; x < offset + inner_size; ++x) + { + *(output_ptr + x) = *(input_ptr + x); + } + offset += inner_size; + } +} + +template void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) +template void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); +#endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */ + +template void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); +#endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */ + +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +} // namespace cpu + +} // namespace arm_compute diff --git a/src/cpu/kernels/select/generic/neon/impl.h b/src/cpu/kernels/select/generic/neon/impl.h new file mode 100644 index 0000000000..2bbc38b744 --- /dev/null +++ b/src/cpu/kernels/select/generic/neon/impl.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef SRC_CORE_NEON_KERNELS_SELECT_IMPL_H +#define SRC_CORE_NEON_KERNELS_SELECT_IMPL_H + +#include + +namespace arm_compute +{ +class ITensor; +class Window; + +namespace cpu +{ +template +void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template +void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template +void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template +void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window); + +template +void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, + const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *)); + +} // namespace cpu +} // namespace arm_compute +#endif //SRC_CORE_NEON_KERNELS_SELECT_IMPL_H \ No newline at end of file diff --git a/src/cpu/kernels/select/generic/neon/integer.cpp b/src/cpu/kernels/select/generic/neon/integer.cpp new file mode 100644 index 0000000000..71b2f0b933 --- /dev/null +++ b/src/cpu/kernels/select/generic/neon/integer.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" + +#include + +#include "src/cpu/kernels/select/generic/neon/impl.h" + +namespace arm_compute +{ +namespace cpu +{ +void neon_s8_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_8(c, x, y, output, window); +} +void neon_s16_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_16(c, x, y, output, window); +} +void neon_s32_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_32(c, x, y, output, window); +} +void neon_s8_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} +void neon_s16_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} +void neon_s32_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} +void neon_u8_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_8(c, x, y, output, window); +} +void neon_u16_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_16(c, x, y, output, window); +} +void neon_u32_select_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_32(c, x, y, output, window); +} +void neon_u8_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} +void neon_u16_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} +void neon_u32_select_not_same_rank(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) +{ + return select_op_not_same_rank(c, x, y, output, window); +} + +} // namespace cpu + +} // namespace arm_compute diff --git a/src/cpu/kernels/select/list.h b/src/cpu/kernels/select/list.h new file mode 100644 index 0000000000..c33a25f6d6 --- /dev/null +++ b/src/cpu/kernels/select/list.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef SRC_CORE_NEON_KERNELS_SELECT_LIST_H +#define SRC_CORE_NEON_KERNELS_SELECT_LIST_H + +#include "arm_compute/core/ITensor.h" + +namespace arm_compute +{ +namespace cpu +{ +#define DECLARE_SELECT_KERNEL(func_name) \ + void func_name(const ITensor *c, const ITensor *x, const ITensor *y, ITensor *output, const Window &window) + +DECLARE_SELECT_KERNEL(neon_s8_select_same_rank); +DECLARE_SELECT_KERNEL(neon_s16_select_same_rank); +DECLARE_SELECT_KERNEL(neon_s32_select_same_rank); +DECLARE_SELECT_KERNEL(neon_u8_select_same_rank); +DECLARE_SELECT_KERNEL(neon_u16_select_same_rank); +DECLARE_SELECT_KERNEL(neon_u32_select_same_rank); +DECLARE_SELECT_KERNEL(neon_f16_select_same_rank); +DECLARE_SELECT_KERNEL(neon_f32_select_same_rank); + +DECLARE_SELECT_KERNEL(neon_s8_select_not_same_rank); +DECLARE_SELECT_KERNEL(neon_s16_select_not_same_rank); +DECLARE_SELECT_KERNEL(neon_s32_select_not_same_rank); +DECLARE_SELECT_KERNEL(neon_u8_select_not_same_rank); +DECLARE_SELECT_KERNEL(neon_u16_select_not_same_rank); +DECLARE_SELECT_KERNEL(neon_u32_select_not_same_rank); +DECLARE_SELECT_KERNEL(neon_f16_select_not_same_rank); +DECLARE_SELECT_KERNEL(neon_f32_select_not_same_rank); + +#undef DECLARE_RANGE_KERNEL + +} // namespace cpu +} // namespace arm_compute +#endif //SRC_CORE_NEON_KERNELS_SELECT_LIST_H \ No newline at end of file -- cgit v1.2.1