From bcf8a968da4b26926df8bb770df16d82146bcb54 Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Fri, 12 Oct 2018 10:51:31 +0100 Subject: COMPMID-1580 Implement ReduceMean in NEON Change-Id: Id974efad304c2513b8824a6561ad45ee60b9e7fb Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/153763 Reviewed-by: Giuseppe Rossini Reviewed-by: Isabella Gottardi Tested-by: bsgcomp --- .../NEON/kernels/NEReductionOperationKernel.cpp | 481 ++++++++++++++++++++- src/runtime/NEON/functions/NEReduceMean.cpp | 117 +++++ .../NEON/functions/NEReductionOperation.cpp | 32 +- 3 files changed, 598 insertions(+), 32 deletions(-) create mode 100644 src/runtime/NEON/functions/NEReduceMean.cpp (limited to 'src') diff --git a/src/core/NEON/kernels/NEReductionOperationKernel.cpp b/src/core/NEON/kernels/NEReductionOperationKernel.cpp index 30f21bbf33..b77219cd79 100644 --- a/src/core/NEON/kernels/NEReductionOperationKernel.cpp +++ b/src/core/NEON/kernels/NEReductionOperationKernel.cpp @@ -32,10 +32,11 @@ #include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Validate.h" +#include "arm_compute/core/NEON/wrapper/wrapper.h" #include -using namespace arm_compute; - +namespace arm_compute +{ namespace { template @@ -57,31 +58,281 @@ public: Iterator in(input, in_slice); Iterator out(output, out_slice); - f(in, out, in_slice, out_slice); + f(in, out, in_slice, out_slice, *input->info()); + } + while(window.slide_window_slice_1D(in_slice) && out_window.slide_window_slice_1D(out_slice)); + } + static void reduceY(const Window &window, const ITensor *input, ITensor *output, F f) + { + // Set in window + Window in_window(window); + + in_window.set(Window::DimY, Window::Dimension(0, 1, 1)); + + // Get first input and output slices + Window in_slice = in_window.first_slice_window_2D(); + Window out_slice = window.first_slice_window_2D(); + + do + { + Iterator in(input, in_slice); + Iterator out(output, out_slice); + + f(in, out, in_slice, out_slice, *input->info(), 1); + } + while(in_window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); + } + static void reduceZ(const Window &window, const ITensor *input, ITensor *output, F f) + { + // Set in window + Window in_window(window); + + in_window.set(Window::DimZ, Window::Dimension(0, 1, 1)); + + // Get first input and output slices + Window in_slice = in_window.first_slice_window_3D(); + Window out_slice = window.first_slice_window_3D(); + + do + { + Iterator in(input, in_slice); + Iterator out(output, out_slice); + + f(in, out, in_slice, out_slice, *input->info(), 2); } - while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(out_slice)); + while(in_window.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice)); + } + static void reduceW(const Window &window, const ITensor *input, ITensor *output, F f) + { + // Set in/out window + Window in_window(window); + Window out_window(window); + + in_window.set(3, Window::Dimension(0, 1, 1)); + out_window.set(3, Window::Dimension(0, 1, 1)); + + // Get first input and output slices + Window in_slice = in_window.first_slice_window_4D(); + Window out_slice = out_window.first_slice_window_4D(); + + do + { + Iterator in(input, in_slice); + Iterator out(output, out_slice); + + f(in, out, in_slice, out_slice, *input->info(), 3); + } + while(in_window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_4D(out_slice)); } }; -struct SumsqOpX +template +struct RedOpX { - inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice) + /** NEON vector tag type. */ + using ExactTagType = typename wrapper::traits::neon_vector::tag_type; + + inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info) { ARM_COMPUTE_UNUSED(out_slice); - float32x4_t vec_sum_value = vdupq_n_f32(0.f); + auto vec_sum_value = wrapper::vdup_n(static_cast(0.f), ExactTagType{}); execute_window_loop(in_slice, [&](const Coordinates & id) { - const auto in_ptr = reinterpret_cast(input.ptr()); - const float32x4_t vec_elements = vld1q_f32(in_ptr); - vec_sum_value = vaddq_f32(vmulq_f32(vec_elements, vec_elements), vec_sum_value); + const auto in_ptr = reinterpret_cast(input.ptr()); + const auto vec_elements = wrapper::vloadq(in_ptr); + + if(op == ReductionOperation::SUM_SQUARE) + { + vec_sum_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_sum_value); + } + else + { + vec_sum_value = wrapper::vadd(vec_elements, vec_sum_value); + } }, input); - float32x2_t carry_addition = vpadd_f32(vget_high_f32(vec_sum_value), vget_low_f32(vec_sum_value)); - carry_addition = vpadd_f32(carry_addition, carry_addition); + auto carry_addition = wrapper::vpadd(wrapper::vgethigh(vec_sum_value), wrapper::vgetlow(vec_sum_value)); + carry_addition = wrapper::vpadd(carry_addition, carry_addition); + + auto res = wrapper::vgetlane(carry_addition, 0); + if(op == ReductionOperation::MEAN_SUM) + { + res /= in_info.dimension(0); + } - *(reinterpret_cast(output.ptr())) = vget_lane_f32(carry_addition, 0); + *(reinterpret_cast(output.ptr())) = res; + } +}; + +template +struct RedOpX_qasymm8 +{ + inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info) + { + ARM_COMPUTE_UNUSED(out_slice); + auto vec_sum_value1 = vdupq_n_u32(static_cast(0.f)); + auto vec_sum_value2 = vdupq_n_u32(static_cast(0.f)); + auto vec_sum_value3 = vdupq_n_u32(static_cast(0.f)); + auto vec_sum_value4 = vdupq_n_u32(static_cast(0.f)); + + execute_window_loop(in_slice, [&](const Coordinates & id) + { + const auto vec_elements = wrapper::vloadq(input.ptr()); + + const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements)); + const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements)); + + const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1)); + const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1)); + const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2)); + const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2)); + + vec_sum_value1 = wrapper::vadd(temp32x4t_1, vec_sum_value1); + vec_sum_value2 = wrapper::vadd(temp32x4t_2, vec_sum_value2); + vec_sum_value3 = wrapper::vadd(temp32x4t_3, vec_sum_value3); + vec_sum_value4 = wrapper::vadd(temp32x4t_4, vec_sum_value4); + }, + input); + + auto carry_addition = wrapper::vadd(vec_sum_value1, vec_sum_value2); + carry_addition = wrapper::vadd(carry_addition, vec_sum_value3); + carry_addition = wrapper::vadd(carry_addition, vec_sum_value4); + + auto carry_paddition = wrapper::vpadd(wrapper::vgethigh(carry_addition), wrapper::vgetlow(carry_addition)); + carry_paddition = wrapper::vpadd(carry_paddition, carry_paddition); + auto res = wrapper::vgetlane(carry_paddition, 0); + + if(op == ReductionOperation::MEAN_SUM) + { + res /= in_info.dimension(0); + } + + *(output.ptr()) = static_cast(res); + } +}; + +template +struct RedOpYZW +{ + /** NEON vector tag type. */ + using ExactTagType = typename wrapper::traits::neon_vector::tag_type; + + inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int axis) + { + ARM_COMPUTE_UNUSED(out_slice); + + execute_window_loop(in_slice, [&](const Coordinates & id) + { + auto vec_sum_value = wrapper::vdup_n(static_cast(0.f), ExactTagType{}); + for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim) + { + T *in_ptr; + switch(axis) + { + case 1: + in_ptr = reinterpret_cast(input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, dim))); + break; + case 2: + in_ptr = reinterpret_cast(input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, dim))); + break; + case 3: + in_ptr = reinterpret_cast(input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, 0, dim))); + break; + default: + ARM_COMPUTE_ERROR("Not supported"); + } + const auto vec_elements = wrapper::vloadq(in_ptr); + + if(op == ReductionOperation::SUM_SQUARE) + { + vec_sum_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_sum_value); + } + else + { + vec_sum_value = wrapper::vadd(vec_elements, vec_sum_value); + } + } + + if(op == ReductionOperation::MEAN_SUM) + { + auto vec_width_inv = wrapper::vinv(wrapper::vdup_n(static_cast(in_info.dimension(axis)), ExactTagType{})); + vec_sum_value = wrapper::vmul(vec_sum_value, vec_width_inv); + } + + wrapper::vstore(reinterpret_cast(output.ptr()), vec_sum_value); + }, + input, output); + } +}; + +template +struct RedOpYZW_qasymm8 +{ + inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int axis) + { + ARM_COMPUTE_UNUSED(out_slice); + + execute_window_loop(in_slice, [&](const Coordinates & id) + { + auto vec_sum_value1 = vdupq_n_u32(static_cast(0.f)); + auto vec_sum_value2 = vdupq_n_u32(static_cast(0.f)); + auto vec_sum_value3 = vdupq_n_u32(static_cast(0.f)); + auto vec_sum_value4 = vdupq_n_u32(static_cast(0.f)); + for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim) + { + uint8_t *in_ptr; + switch(axis) + { + case 1: + in_ptr = input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, dim)); + break; + case 2: + in_ptr = input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, dim)); + break; + case 3: + in_ptr = input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, 0, dim)); + break; + default: + ARM_COMPUTE_ERROR("Not supported"); + } + const auto vec_elements = wrapper::vloadq(in_ptr); + + const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements)); + const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements)); + + const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1)); + const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1)); + const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2)); + const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2)); + + vec_sum_value1 = wrapper::vadd(temp32x4t_1, vec_sum_value1); + vec_sum_value2 = wrapper::vadd(temp32x4t_2, vec_sum_value2); + vec_sum_value3 = wrapper::vadd(temp32x4t_3, vec_sum_value3); + vec_sum_value4 = wrapper::vadd(temp32x4t_4, vec_sum_value4); + } + + if(op == ReductionOperation::MEAN_SUM) + { + const auto vec_width_inv = wrapper::vinv(vdupq_n_f32(in_info.dimension(axis))); + const auto vec_sum_value1_f = wrapper::vmul(vcvtq_f32_u32(vec_sum_value1), vec_width_inv); + const auto vec_sum_value2_f = wrapper::vmul(vcvtq_f32_u32(vec_sum_value2), vec_width_inv); + const auto vec_sum_value3_f = wrapper::vmul(vcvtq_f32_u32(vec_sum_value3), vec_width_inv); + const auto vec_sum_value4_f = wrapper::vmul(vcvtq_f32_u32(vec_sum_value4), vec_width_inv); + + vec_sum_value1 = vcvtq_u32_f32(vec_sum_value1_f); + vec_sum_value2 = vcvtq_u32_f32(vec_sum_value2_f); + vec_sum_value3 = vcvtq_u32_f32(vec_sum_value3_f); + vec_sum_value4 = vcvtq_u32_f32(vec_sum_value4_f); + } + + const auto temp16x8t_1 = vcombine_u16(wrapper::vqmovn(vec_sum_value1), wrapper::vqmovn(vec_sum_value2)); + const auto temp16x8t_2 = vcombine_u16(wrapper::vqmovn(vec_sum_value3), wrapper::vqmovn(vec_sum_value4)); + auto res = vcombine_u8(wrapper::vqmovn(temp16x8t_1), wrapper::vqmovn(temp16x8t_2)); + wrapper::vstore(output.ptr(), res); + }, + input, output); } }; @@ -90,7 +341,186 @@ void reduce_sumsq(const Window &window, const ITensor *input, ITensor *output, u switch(axis) { case 0: - return Reducer::reduceX(window, input, output, SumsqOpX()); + switch(input->info()->data_type()) + { +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceX(window, input, output, RedOpX()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceX(window, input, output, RedOpX()); + case DataType::QASYMM8: + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 1: + switch(input->info()->data_type()) + { +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceY(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceY(window, input, output, RedOpYZW()); + case DataType::QASYMM8: + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 2: + switch(input->info()->data_type()) + { +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceZ(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceZ(window, input, output, RedOpYZW()); + case DataType::QASYMM8: + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 3: + switch(input->info()->data_type()) + { +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceW(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceW(window, input, output, RedOpYZW()); + case DataType::QASYMM8: + default: + ARM_COMPUTE_ERROR("Not supported"); + } + default: + ARM_COMPUTE_ERROR("Unsupported reduction axis"); + } +} + +void reduce_sum(const Window &window, const ITensor *input, ITensor *output, unsigned int axis) +{ + switch(axis) + { + case 0: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceX(window, input, output, RedOpX_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceX(window, input, output, RedOpX()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceX(window, input, output, RedOpX()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 1: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceY(window, input, output, RedOpYZW_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceY(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceY(window, input, output, RedOpYZW()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 2: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceZ(window, input, output, RedOpYZW_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceZ(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceZ(window, input, output, RedOpYZW()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 3: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceW(window, input, output, RedOpYZW_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceW(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceW(window, input, output, RedOpYZW()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } + default: + ARM_COMPUTE_ERROR("Unsupported reduction axis"); + } +} +void reduce_mean_sum(const Window &window, const ITensor *input, ITensor *output, unsigned int axis) +{ + switch(axis) + { + case 0: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceX(window, input, output, RedOpX_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceX(window, input, output, RedOpX()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceX(window, input, output, RedOpX()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 1: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceY(window, input, output, RedOpYZW_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceY(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceY(window, input, output, RedOpYZW()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 2: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceZ(window, input, output, RedOpYZW_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceZ(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceZ(window, input, output, RedOpYZW()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } + case 3: + switch(input->info()->data_type()) + { + case DataType::QASYMM8: + return Reducer>::reduceW(window, input, output, RedOpYZW_qasymm8()); +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F16: + return Reducer>::reduceW(window, input, output, RedOpYZW()); +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + case DataType::F32: + return Reducer>::reduceW(window, input, output, RedOpYZW()); + default: + ARM_COMPUTE_ERROR("Not supported"); + } default: ARM_COMPUTE_ERROR("Unsupported reduction axis"); } @@ -109,16 +539,15 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, u ARM_COMPUTE_UNUSED(op); ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); - ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); - ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW); + ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 0, "Unsupported reduction axis, Supported axis is 0"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); if(output->total_size() != 0) { ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); - ARM_COMPUTE_RETURN_ERROR_ON(output->data_layout() != DataLayout::NCHW); + ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); const TensorShape output_shape = calculate_output_shape(input->tensor_shape(), axis); const TensorInfo tensor_info_reshaped = input->clone()->set_tensor_shape(output_shape); @@ -170,10 +599,11 @@ void NEReductionOperationKernel::configure(const ITensor *input, ITensor *output unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->info()->data_type()); - _input = input; - _output = output; - _border_size = (axis == 0) ? BorderSize(0, num_elems_processed_per_iteration - (input->info()->dimension(0) % num_elems_processed_per_iteration), 0, 0) : BorderSize(); - _op = op; + _input = input; + _output = output; + _border_size = (axis == 0) ? BorderSize(0, num_elems_processed_per_iteration - (input->info()->dimension(0) % num_elems_processed_per_iteration), 0, 0) : BorderSize(); + _op = op; + _reduction_axis = axis; // Configure kernel window auto win_config = validate_and_configure_window(_input->info(), _output->info(), axis); @@ -202,7 +632,14 @@ void NEReductionOperationKernel::run(const Window &window, const ThreadInfo &inf case ReductionOperation::SUM_SQUARE: reduce_sumsq(window, _input, _output, _reduction_axis); break; + case ReductionOperation::MEAN_SUM: + reduce_mean_sum(window, _input, _output, _reduction_axis); + break; + case ReductionOperation::SUM: + reduce_sum(window, _input, _output, _reduction_axis); + break; default: ARM_COMPUTE_ERROR("Unsupported reduction operation."); } } +} // namespace arm_compute diff --git a/src/runtime/NEON/functions/NEReduceMean.cpp b/src/runtime/NEON/functions/NEReduceMean.cpp new file mode 100644 index 0000000000..0b022df729 --- /dev/null +++ b/src/runtime/NEON/functions/NEReduceMean.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2018 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, INNEUDING 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 NEAIM, 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/runtime/NEON/functions/NEReduceMean.h" + +#include "arm_compute/core/Helpers.h" +#include "arm_compute/runtime/NEON/NEScheduler.h" + +using namespace arm_compute; + +NEReduceMean::NEReduceMean(std::shared_ptr memory_manager) + : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims() +{ +} + +Status NEReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) +{ + ARM_COMPUTE_UNUSED(keep_dims); + ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); + ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions()); + + for(unsigned int i = 0; i < reduction_axis.num_dimensions(); ++i) + { + if(output->total_size() > 0) + { + ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(reduction_axis[i]) != 1); + ARM_COMPUTE_RETURN_ERROR_ON(static_cast(reduction_axis[i]) > input->num_dimensions() - 1); + } + + ARM_COMPUTE_RETURN_ON_ERROR(NEReductionOperationKernel::validate(input, output, reduction_axis[i], ReductionOperation::MEAN_SUM)); + } + + return Status{}; +} + +void NEReduceMean::configure(ITensor *input, const Coordinates &reduction_axis, bool keep_dims, ITensor *output) +{ + ARM_COMPUTE_ERROR_ON_NULLPTR(input); + + _reduction_ops = reduction_axis.num_dimensions(); + _reduction_kernels = arm_compute::support::cpp14::make_unique(_reduction_ops); + _reduced_outs = arm_compute::support::cpp14::make_unique(_reduction_ops - (keep_dims ? 1 : 0)); + _keep_dims = keep_dims; + + // Perform reduction for every axis + for(unsigned int i = 0; i < _reduction_ops; ++i) + { + TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (_reduced_outs.get() + i - 1)->info()->tensor_shape(); + out_shape.set(reduction_axis[i], 1); + auto in = (i == 0) ? input : (_reduced_outs.get() + i - 1); + + if(i == _reduction_ops - 1 && keep_dims) + { + _reduction_kernels[i].configure(in, output, reduction_axis[i], ReductionOperation::MEAN_SUM); + } + else + { + _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type())); + _memory_group.manage(_reduced_outs.get() + i); + _reduction_kernels[i].configure(in, _reduced_outs.get() + i, reduction_axis[i], ReductionOperation::MEAN_SUM); + } + } + + // Allocate intermediate tensors + for(unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i) + { + _reduced_outs[i].allocator()->allocate(); + } + + // Configure reshape layer if we want to drop the dimensions + if(!keep_dims) + { + TensorShape out_shape = input->info()->tensor_shape(); + for(unsigned int i = 0; i < _reduction_ops; ++i) + { + out_shape.remove_dimension(reduction_axis[i]); + } + auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape)); + _reshape.configure(_reduced_outs.get() + _reduction_ops - 1, output); + } +} + +void NEReduceMean::run() +{ + _memory_group.acquire(); + + for(unsigned int i = 0; i < _reduction_ops; ++i) + { + _reduction_kernels[i].run(); + } + + if(!_keep_dims) + { + _reshape.run(); + } + _memory_group.release(); +} diff --git a/src/runtime/NEON/functions/NEReductionOperation.cpp b/src/runtime/NEON/functions/NEReductionOperation.cpp index cd0b42fbe3..188c2bbb18 100644 --- a/src/runtime/NEON/functions/NEReductionOperation.cpp +++ b/src/runtime/NEON/functions/NEReductionOperation.cpp @@ -26,8 +26,8 @@ #include "arm_compute/core/Helpers.h" #include "arm_compute/runtime/NEON/NEScheduler.h" -using namespace arm_compute; - +namespace arm_compute +{ namespace { /** Define dimension to split the window @@ -42,6 +42,10 @@ size_t reduction_window_split_dimension(unsigned int axis) { case 0: return Window::DimY; + case 1: + case 2: + case 3: + return Window::DimX; default: ARM_COMPUTE_ERROR("Unsupported reduction axis"); } @@ -59,7 +63,7 @@ BorderMode reduction_operation_border_mode(ReductionOperation op) } // namespace NEReductionOperation::NEReductionOperation() - : _reduction_kernel(), _fill_border_kernel(), _window_split(0) + : _reduction_kernel(), _fill_border_kernel(), _window_split(0), _reduction_axis() { } @@ -72,20 +76,28 @@ Status NEReductionOperation::validate(const ITensorInfo *input, const ITensorInf void NEReductionOperation::configure(ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op) { - ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); + ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); // Configure reduction kernel _reduction_kernel.configure(input, output, axis, op); - _window_split = reduction_window_split_dimension(axis); + _window_split = reduction_window_split_dimension(axis); + _reduction_axis = axis; - // Configure fill border kernel - BorderSize fill_border_size = (axis == 0) ? _reduction_kernel.border_size() : BorderSize(); - BorderMode fill_border_mode = reduction_operation_border_mode(op); - _fill_border_kernel.configure(input, fill_border_size, fill_border_mode, PixelValue(static_cast(0.f))); + if(axis == 0) + { + // Configure fill border kernel + BorderSize fill_border_size = (axis == 0) ? _reduction_kernel.border_size() : BorderSize(); + BorderMode fill_border_mode = reduction_operation_border_mode(op); + _fill_border_kernel.configure(input, fill_border_size, fill_border_mode, PixelValue(static_cast(0.f))); + } } void NEReductionOperation::run() { - NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY); + if(_reduction_axis == 0) + { + NEScheduler::get().schedule(&_fill_border_kernel, Window::DimY); + } NEScheduler::get().schedule(&_reduction_kernel, _window_split); } +} // namespace arm_compute -- cgit v1.2.1