From 9e8a7701532377073220613b3642468f96cba62f Mon Sep 17 00:00:00 2001 From: Yair Schwarzbaum Date: Mon, 8 Nov 2021 10:58:06 +0200 Subject: Decouple data type for NERangeKernel - Decouple data type for CPU implementation, supported data types are: fp32, fp16, u8, u16, u32, s8, s16, s32 Resolves COMPMID-4612 Signed-off-by: Yair Schwarzbaum Change-Id: Iec9aab9f59c5a344950c788281fc500290a19bbc Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/6686 Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Reviewed-by: Giorgio Arena --- src/core/NEON/kernels/NERangeKernel.cpp | 152 ++++++++++++++++---------------- src/core/NEON/kernels/NERangeKernel.h | 11 ++- 2 files changed, 79 insertions(+), 84 deletions(-) (limited to 'src/core/NEON/kernels') diff --git a/src/core/NEON/kernels/NERangeKernel.cpp b/src/core/NEON/kernels/NERangeKernel.cpp index 0395e0bd34..82d1403c56 100644 --- a/src/core/NEON/kernels/NERangeKernel.cpp +++ b/src/core/NEON/kernels/NERangeKernel.cpp @@ -30,68 +30,96 @@ #include "arm_compute/core/Validate.h" #include "src/core/NEON/NEAsymm.h" #include "src/core/NEON/wrapper/wrapper.h" +#include "src/core/common/Registrars.h" #include "src/core/helpers/AutoConfiguration.h" #include "src/core/helpers/WindowHelpers.h" - -#include "arm_compute/core/Utils.h" +#include "src/cpu/kernels/range/list.h" namespace arm_compute { namespace { -template -void range_function(ITensor *output, float start, float step, const Window &window) +struct RangeSelectorData { - /** SIMD vector tag type. */ - using ExactTagType = typename wrapper::traits::neon_bitvector::tag_type; - - const auto step_vec = wrapper::vdup_n(static_cast(step), ExactTagType{}); - const auto start_vec = wrapper::vdup_n(static_cast(start), ExactTagType{}); - auto id_vec = wrapper::vdup_n(static_cast(0.f), ExactTagType{}); + DataType dt; +}; - const auto window_start_x = static_cast(window.x().start()); - const auto window_end_x = static_cast(window.x().end()); - const int window_step_x = 16 / sizeof(T); +using RangeSelectorPtr = std::add_pointer::type; +using RangeUKernelPtr = std::add_pointer::type; - Window win{ window }; - win.set(Window::DimX, Window::Dimension(0, 1, 1)); - Iterator output_it(output, win); +struct RangeUKernel +{ + const char *name; + const RangeSelectorPtr is_selected; + RangeUKernelPtr ukernel; +}; - execute_window_loop(win, [&](const Coordinates &) +static const RangeUKernel available_kernels[] = +{ { - int x = window_start_x; - const auto out_ptr = reinterpret_cast(output_it.ptr()); - for(; x <= (window_end_x - window_step_x); x += window_step_x) - { - for(int count = 0; count < window_step_x; ++count) - { - id_vec = wrapper::vsetlane(static_cast(x + count), id_vec, count); - } - - // start + step * id - const auto res_vec = wrapper::vmla(start_vec, id_vec, step_vec); - wrapper::vstore(out_ptr + x, res_vec); - } + "fp16_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::F16; }, + REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_range_function) + }, + { + "f32_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::F32; }, + REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_range_function) + }, + { + "u8_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::U8; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::u8_neon_range_function) + }, + { + "u16_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::U16; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::u16_neon_range_function) + }, + { + "u32_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::U32; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::u32_neon_range_function) + }, + { + "s8_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::S8; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::s8_neon_range_function) + }, + { + "s16_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::S16; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::s16_neon_range_function) + }, + { + "s32_neon_range", + [](const RangeSelectorData & data) { return data.dt == DataType::S32; }, + REGISTER_INTEGER_NEON(arm_compute::cpu::s32_neon_range_function) + }, +}; - // Compute left-over elements - for(; x < window_end_x; ++x) +/** Micro-kernel selector + * + * @param[in] data Selection data passed to help pick the appropriate micro-kernel + * + * @return A matching micro-kernel else nullptr + */ +const RangeUKernel *get_implementation(const RangeSelectorData &data) +{ + for(const auto &uk : available_kernels) + { + if(uk.is_selected(data)) { - const auto res = start + x * step; - *(out_ptr + x) = res; + return &uk; } - - }, - output_it); + } + return nullptr; } Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step) { - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, - 1, - DataType::U8, DataType::S8, - DataType::U16, DataType::S16, - DataType::U32, DataType::S32, - DataType::F16, DataType::F32); + const auto *uk = get_implementation(RangeSelectorData{ output.data_type() }); + ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); @@ -111,7 +139,7 @@ Status validate_arguments(const ITensorInfo &output, const float start, const fl } // namespace NERangeKernel::NERangeKernel() - : _func(nullptr), _start(0), _end(1), _step(1), _output(nullptr) + : _start(0), _end(1), _step(1), _output(nullptr) { } @@ -131,38 +159,6 @@ void NERangeKernel::configure(ITensor *output, float start, float end, float ste _end = end; _step = step; _output = output; - switch(_output->info()->data_type()) - { - case DataType::U8: - _func = &range_function; - break; - case DataType::U16: - _func = &range_function; - break; - case DataType::U32: - _func = &range_function; - break; - case DataType::S8: - _func = &range_function; - break; - case DataType::S16: - _func = &range_function; - break; - case DataType::S32: - _func = &range_function; - break; - case DataType::F32: - _func = &range_function; - break; -#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - case DataType::F16: - _func = &range_function; - break; -#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - default: - ARM_COMPUTE_ERROR("Unsupported data type."); - break; - } INEKernel::configure(win); } @@ -181,8 +177,8 @@ void NERangeKernel::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(_func == nullptr); + const auto *uk = get_implementation(RangeSelectorData{ _output->info()->data_type() }); - (*_func)(_output, _start, _step, window); + uk->ukernel(_output, _start, _step, window); } } // namespace arm_compute diff --git a/src/core/NEON/kernels/NERangeKernel.h b/src/core/NEON/kernels/NERangeKernel.h index 7c42ef11dc..90560995e6 100644 --- a/src/core/NEON/kernels/NERangeKernel.h +++ b/src/core/NEON/kernels/NERangeKernel.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 Arm Limited. + * Copyright (c) 2018-2021 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -80,11 +80,10 @@ public: private: using RangeFunction = void(ITensor *output, float start, float step, const Window &window); - RangeFunction *_func; /**< Range function to be called */ - float _start; /**< Start of sequence */ - float _end; /**< End of sequence */ - float _step; /**< Increment/step value */ - ITensor *_output; /**< Destination tensor */ + float _start; /**< Start of sequence */ + float _end; /**< End of sequence */ + float _step; /**< Increment/step value */ + ITensor *_output; /**< Destination tensor */ }; } // namespace arm_compute #endif /* ARM_COMPUTE_NERANGEKERNEL_H */ -- cgit v1.2.1