From 6777359cd5b7aa322723d881e9ada426668491fa Mon Sep 17 00:00:00 2001 From: Pablo Marquez Tello Date: Fri, 6 Oct 2023 13:49:44 +0100 Subject: CpuSubKernel changes to enable fp16 in armv8a multi_isa builds * FP16 kernels must be instantiated in fp16.cpp. * Partially resolves MLCE-1102 Change-Id: I497fe0ba6e84493a5072c3e80bbba7ecd5de8095 Signed-off-by: Pablo Marquez Tello Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/10448 Tested-by: Arm Jenkins Reviewed-by: Viet-Hoa Do Comments-Addressed: Arm Jenkins Benchmark: Arm Jenkins --- Android.bp | 1 + filelist.json | 14 ++-- src/BUILD.bazel | 1 + src/CMakeLists.txt | 1 + src/cpu/kernels/CpuSubKernel.cpp | 5 +- src/cpu/kernels/sub/neon/fp16.cpp | 43 ++++++++++ src/cpu/kernels/sub/neon/impl.h | 164 ++++++++++++++++++++++++++++++++++++++ src/cpu/kernels/sub/neon/list.h | 130 ++---------------------------- 8 files changed, 227 insertions(+), 132 deletions(-) create mode 100644 src/cpu/kernels/sub/neon/fp16.cpp create mode 100644 src/cpu/kernels/sub/neon/impl.h diff --git a/Android.bp b/Android.bp index 1bd70c4f36..5760b7d08e 100644 --- a/Android.bp +++ b/Android.bp @@ -569,6 +569,7 @@ cc_library_static { "src/cpu/kernels/softmax/generic/neon/impl.cpp", "src/cpu/kernels/softmax/generic/neon/qasymm8.cpp", "src/cpu/kernels/softmax/generic/neon/qasymm8_signed.cpp", + "src/cpu/kernels/sub/neon/fp16.cpp", "src/cpu/kernels/sub/neon/qasymm8.cpp", "src/cpu/kernels/sub/neon/qasymm8_signed.cpp", "src/cpu/kernels/sub/neon/qsymm16.cpp", diff --git a/filelist.json b/filelist.json index bb90fcf4bb..aeb54c6d9d 100644 --- a/filelist.json +++ b/filelist.json @@ -2249,13 +2249,17 @@ "common": [ "src/cpu/operators/CpuSub.cpp", "src/cpu/kernels/CpuSubKernel.cpp", - "src/runtime/NEON/functions/NEArithmeticSubtraction.cpp", - "src/cpu/kernels/sub/neon/qasymm8.cpp", - "src/cpu/kernels/sub/neon/qasymm8_signed.cpp", - "src/cpu/kernels/sub/neon/qsymm16.cpp" - ] + "src/runtime/NEON/functions/NEArithmeticSubtraction.cpp" + ], + "neon": { + "fp16":["src/cpu/kernels/sub/neon/fp16.cpp"], + "qasymm8": ["src/cpu/kernels/sub/neon/qasymm8.cpp"], + "qasymm8_signed": ["src/cpu/kernels/sub/neon/qasymm8_signed.cpp"], + "qsymm16": ["src/cpu/kernels/sub/neon/qsymm16.cpp"] + } } }, + "Tile": { "files": { "common": [ diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 2c56f26c11..7c644fc25f 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -818,6 +818,7 @@ filegroup( "cpu/kernels/softmax/generic/neon/impl.cpp", "cpu/kernels/softmax/generic/neon/qasymm8.cpp", "cpu/kernels/softmax/generic/neon/qasymm8_signed.cpp", + "cpu/kernels/sub/neon/fp16.cpp", "cpu/kernels/sub/neon/qasymm8.cpp", "cpu/kernels/sub/neon/qasymm8_signed.cpp", "cpu/kernels/sub/neon/qsymm16.cpp", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4db76b1328..103f2d7c62 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -809,6 +809,7 @@ target_sources( cpu/kernels/softmax/generic/neon/impl.cpp cpu/kernels/softmax/generic/neon/qasymm8.cpp cpu/kernels/softmax/generic/neon/qasymm8_signed.cpp + cpu/kernels/sub/neon/fp16.cpp cpu/kernels/sub/neon/qasymm8.cpp cpu/kernels/sub/neon/qasymm8_signed.cpp cpu/kernels/sub/neon/qsymm16.cpp diff --git a/src/cpu/kernels/CpuSubKernel.cpp b/src/cpu/kernels/CpuSubKernel.cpp index 2b2c6f2e92..c8706ff651 100644 --- a/src/cpu/kernels/CpuSubKernel.cpp +++ b/src/cpu/kernels/CpuSubKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Arm Limited. + * Copyright (c) 2021-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -31,6 +31,7 @@ #include "src/core/helpers/AutoConfiguration.h" #include "src/core/helpers/WindowHelpers.h" #include "src/cpu/kernels/add/generic/neon/impl.h" +#include "src/cpu/kernels/sub/neon/impl.h" #include "src/cpu/kernels/sub/neon/list.h" #if defined(ENABLE_FP32_KERNELS) @@ -57,7 +58,7 @@ static const std::vector available_kernels = { REGISTER_FP32_NEON(arm_compute::cpu::sub_same_neon)}, {"neon_fp16_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::F16) && data.isa.fp16; }, - REGISTER_FP16_NEON(arm_compute::cpu::sub_same_neon)}, + REGISTER_FP16_NEON(arm_compute::cpu::sub_same_neon_fp16)}, {"neon_u8_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::U8); }, REGISTER_INTEGER_NEON(arm_compute::cpu::sub_same_neon)}, {"neon_s16_sub", [](const CpuSubKernelDataTypeISASelectorData &data) { return (data.dt == DataType::S16); }, diff --git a/src/cpu/kernels/sub/neon/fp16.cpp b/src/cpu/kernels/sub/neon/fp16.cpp new file mode 100644 index 0000000000..023068817b --- /dev/null +++ b/src/cpu/kernels/sub/neon/fp16.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 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/ITensor.h" +#include "arm_compute/core/Types.h" + +#include "src/cpu/kernels/sub/neon/impl.h" + +#if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) + +namespace arm_compute +{ +namespace cpu +{ +void sub_same_neon_fp16( + const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window) +{ + sub_same_neon(src0, src1, dst, policy, window); +} +} // namespace cpu +} // namespace arm_compute + +#endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */ diff --git a/src/cpu/kernels/sub/neon/impl.h b/src/cpu/kernels/sub/neon/impl.h new file mode 100644 index 0000000000..6123f7e25a --- /dev/null +++ b/src/cpu/kernels/sub/neon/impl.h @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2021-2023 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 ACL_SRC_CPU_KERNELS_SUB_NEON_IMPL_H +#define ACL_SRC_CPU_KERNELS_SUB_NEON_IMPL_H + +#include "arm_compute/core/Helpers.h" +#include "arm_compute/core/ITensor.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/Traits.h" + +#include "src/core/helpers/WindowHelpers.h" +#include "src/core/NEON/wrapper/intrinsics/intrinsics.h" +#include "src/core/NEON/wrapper/scalar/sub.h" + +namespace arm_compute +{ +namespace cpu +{ +template +void sub_same_neon( + const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window) +{ + /** SIMD vector tag type. */ + using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t; + + bool is_sat = policy == ConvertPolicy::SATURATE; + + // Create input windows + Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape()); + Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); + + // Clear X Dimension on execution window as we handle manually + Window win = window; + win.set(Window::DimX, Window::Dimension(0, 1, 1)); + + constexpr int window_step_x = 16 / sizeof(T); + const auto window_start_x = static_cast(window.x().start()); + const auto window_end_x = static_cast(window.x().end()); + const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x(); + + Iterator input1(src0, window.broadcast_if_dimension_le_one(src0->info()->tensor_shape())); + Iterator input2(src1, window.broadcast_if_dimension_le_one(src1->info()->tensor_shape())); + Iterator output(dst, window); + + if (is_broadcast_across_x) + { + const bool is_broadcast_input_2 = input2_win.x().step() == 0; + Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; + Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; + const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0; + const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0; + + // Clear X Dimension on execution window as we handle manually + non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); + + Iterator broadcast_input(broadcast_tensor, broadcast_win); + Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); + Iterator output(dst, win); + + execute_window_loop( + win, + [&](const Coordinates &) + { + const auto non_broadcast_input_ptr = reinterpret_cast(non_broadcast_input.ptr()); + const auto output_ptr = reinterpret_cast(output.ptr()); + + const T broadcast_value = *reinterpret_cast(broadcast_input.ptr()); + const auto broadcast_value_vec = wrapper::vdup_n(broadcast_value, ExactTagType{}); + + // Compute S elements per iteration + int x = window_start_x; + for (; x <= (window_end_x - window_step_x); x += window_step_x) + { + const auto non_broadcast_v = wrapper::vloadq(non_broadcast_input_ptr + x); + auto res = is_sat ? wrapper::vqsub(broadcast_value_vec, non_broadcast_v) + : wrapper::vsub(broadcast_value_vec, non_broadcast_v); + if (is_broadcast_input_2) + { + res = wrapper::vmul(res, wrapper::vdup_n(static_cast(-1), ExactTagType{})); + } + wrapper::vstore(output_ptr + x, res); + } + + // Compute left-over elements + for (; x < window_end_x; ++x) + { + const auto non_broadcast_v = *(non_broadcast_input_ptr + x); + auto res = + is_sat ? wrapper::sub_sat(broadcast_value, non_broadcast_v) : broadcast_value - non_broadcast_v; + if (is_broadcast_input_2) + { + res = static_cast(-1) * res; + } + + *(output_ptr + x) = res; + } + }, + broadcast_input, non_broadcast_input, output); + } + else + { + // Clear X Dimension on execution window as we handle manually + input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); + input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); + + Iterator input1(src0, input1_win); + Iterator input2(src1, input2_win); + Iterator output(dst, win); + + execute_window_loop( + win, + [&](const Coordinates &) + { + const auto input1_ptr = reinterpret_cast(input1.ptr()); + const auto input2_ptr = reinterpret_cast(input2.ptr()); + const auto output_ptr = reinterpret_cast(output.ptr()); + + // Compute S elements per iteration + int x = window_start_x; + for (; x <= (window_end_x - window_step_x); x += window_step_x) + { + const auto val1 = wrapper::vloadq(input1_ptr + x); + const auto val2 = wrapper::vloadq(input2_ptr + x); + const auto res = is_sat ? wrapper::vqsub(val1, val2) : wrapper::vsub(val1, val2); + wrapper::vstore(output_ptr + x, res); + } + + // Compute left-over elements + for (; x < window_end_x; ++x) + { + const auto val1 = *(input1_ptr + x); + const auto val2 = *(input2_ptr + x); + *(output_ptr + x) = is_sat ? wrapper::sub_sat(val1, val2) : val1 - val2; + } + }, + input1, input2, output); + } +} +} // namespace cpu +} // namespace arm_compute + +#endif // ACL_SRC_CPU_KERNELS_SUB_NEON_IMPL_H diff --git a/src/cpu/kernels/sub/neon/list.h b/src/cpu/kernels/sub/neon/list.h index 9f6c92271f..f29571f122 100644 --- a/src/cpu/kernels/sub/neon/list.h +++ b/src/cpu/kernels/sub/neon/list.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Arm Limited. + * Copyright (c) 2021-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef SRC_CORE_NEON_KERNELS_SUB_LIST_H -#define SRC_CORE_NEON_KERNELS_SUB_LIST_H +#ifndef ACL_SRC_CPU_KERNELS_SUB_NEON_LIST_H +#define ACL_SRC_CPU_KERNELS_SUB_NEON_LIST_H #include "arm_compute/core/Types.h" #include "arm_compute/core/utils/misc/Traits.h" @@ -42,129 +42,9 @@ DECLARE_SUB_KERNEL(sub_qasymm8_signed_neon_fixedpoint); DECLARE_SUB_KERNEL(sub_qasymm8_neon); DECLARE_SUB_KERNEL(sub_qasymm8_signed_neon); DECLARE_SUB_KERNEL(sub_qsymm16_neon); +DECLARE_SUB_KERNEL(sub_same_neon_fp16); #undef DECLARE_SUB_KERNEL - -template -void sub_same_neon( - const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window) -{ - /** SIMD vector tag type. */ - using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t; - - bool is_sat = policy == ConvertPolicy::SATURATE; - - // Create input windows - Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape()); - Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); - - // Clear X Dimension on execution window as we handle manually - Window win = window; - win.set(Window::DimX, Window::Dimension(0, 1, 1)); - - constexpr int window_step_x = 16 / sizeof(T); - const auto window_start_x = static_cast(window.x().start()); - const auto window_end_x = static_cast(window.x().end()); - const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x(); - - Iterator input1(src0, window.broadcast_if_dimension_le_one(src0->info()->tensor_shape())); - Iterator input2(src1, window.broadcast_if_dimension_le_one(src1->info()->tensor_shape())); - Iterator output(dst, window); - - if (is_broadcast_across_x) - { - const bool is_broadcast_input_2 = input2_win.x().step() == 0; - Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; - Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; - const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0; - const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0; - - // Clear X Dimension on execution window as we handle manually - non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); - - Iterator broadcast_input(broadcast_tensor, broadcast_win); - Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); - Iterator output(dst, win); - - execute_window_loop( - win, - [&](const Coordinates &) - { - const auto non_broadcast_input_ptr = reinterpret_cast(non_broadcast_input.ptr()); - const auto output_ptr = reinterpret_cast(output.ptr()); - - const T broadcast_value = *reinterpret_cast(broadcast_input.ptr()); - const auto broadcast_value_vec = wrapper::vdup_n(broadcast_value, ExactTagType{}); - - // Compute S elements per iteration - int x = window_start_x; - for (; x <= (window_end_x - window_step_x); x += window_step_x) - { - const auto non_broadcast_v = wrapper::vloadq(non_broadcast_input_ptr + x); - auto res = is_sat ? wrapper::vqsub(broadcast_value_vec, non_broadcast_v) - : wrapper::vsub(broadcast_value_vec, non_broadcast_v); - if (is_broadcast_input_2) - { - res = wrapper::vmul(res, wrapper::vdup_n(static_cast(-1), ExactTagType{})); - } - wrapper::vstore(output_ptr + x, res); - } - - // Compute left-over elements - for (; x < window_end_x; ++x) - { - const auto non_broadcast_v = *(non_broadcast_input_ptr + x); - auto res = - is_sat ? wrapper::sub_sat(broadcast_value, non_broadcast_v) : broadcast_value - non_broadcast_v; - if (is_broadcast_input_2) - { - res = static_cast(-1) * res; - } - - *(output_ptr + x) = res; - } - }, - broadcast_input, non_broadcast_input, output); - } - else - { - // Clear X Dimension on execution window as we handle manually - input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); - input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); - - Iterator input1(src0, input1_win); - Iterator input2(src1, input2_win); - Iterator output(dst, win); - - execute_window_loop( - win, - [&](const Coordinates &) - { - const auto input1_ptr = reinterpret_cast(input1.ptr()); - const auto input2_ptr = reinterpret_cast(input2.ptr()); - const auto output_ptr = reinterpret_cast(output.ptr()); - - // Compute S elements per iteration - int x = window_start_x; - for (; x <= (window_end_x - window_step_x); x += window_step_x) - { - const auto val1 = wrapper::vloadq(input1_ptr + x); - const auto val2 = wrapper::vloadq(input2_ptr + x); - const auto res = is_sat ? wrapper::vqsub(val1, val2) : wrapper::vsub(val1, val2); - wrapper::vstore(output_ptr + x, res); - } - - // Compute left-over elements - for (; x < window_end_x; ++x) - { - const auto val1 = *(input1_ptr + x); - const auto val2 = *(input2_ptr + x); - *(output_ptr + x) = is_sat ? wrapper::sub_sat(val1, val2) : val1 - val2; - } - }, - input1, input2, output); - } -} } // namespace cpu } // namespace arm_compute -#endif // SRC_CORE_NEON_KERNELS_SUB_LIST_H +#endif // ACL_SRC_CPU_KERNELS_SUB_NEON_LIST_H -- cgit v1.2.1