From 861f0db548befac0cd5fb28fe2fa8ea1828c715d Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Mon, 26 Feb 2018 16:47:58 +0000 Subject: COMPMID-941 Add NEON broadcast multiply support Change-Id: I1f808c25750461bec9a28b2f6615fbd0f624117a Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/122262 Reviewed-by: Michele DiGiorgio Tested-by: Jenkins Reviewed-by: Anthony Barbier --- .../kernels/NEPixelWiseMultiplicationKernel.cpp | 132 ++++++++++++++------- 1 file changed, 89 insertions(+), 43 deletions(-) (limited to 'src/core/NEON/kernels/NEPixelWiseMultiplicationKernel.cpp') diff --git a/src/core/NEON/kernels/NEPixelWiseMultiplicationKernel.cpp b/src/core/NEON/kernels/NEPixelWiseMultiplicationKernel.cpp index c271032e54..193ca3799c 100644 --- a/src/core/NEON/kernels/NEPixelWiseMultiplicationKernel.cpp +++ b/src/core/NEON/kernels/NEPixelWiseMultiplicationKernel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017 ARM Limited. + * Copyright (c) 2016-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -54,18 +54,23 @@ const float scale255_constant = 1.f / 255.f; const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant); const float32x4_t positive_round_f32q = vdupq_n_f32(0.5f); +constexpr unsigned int num_elems_processed_per_iteration = 16; + inline Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) { ARM_COMPUTE_UNUSED(overflow_policy); ARM_COMPUTE_UNUSED(rounding_policy); - ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input1, input2, output); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32); ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QS8, DataType::QS16, DataType::S16, DataType::F16, DataType::F32); ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8), "Output can only be U8 if both inputs are U8"); + const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape()); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); + if(is_data_type_fixed_point(input1->data_type()) || is_data_type_fixed_point(input2->data_type()) || is_data_type_fixed_point(output->data_type())) { // Check that all data types are the same and all fixed-point positions are the same @@ -96,19 +101,44 @@ inline Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *i inline std::pair validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) { - constexpr unsigned int num_elems_processed_per_iteration = 16; + const std::pair broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); + const ValidRegion &valid_region = broadcast_pair.second; + + // Auto initialize output if not initialized + { + set_shape_if_empty(*output, input1->tensor_shape()); + + if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16) + { + set_format_if_unknown(*output, Format::S16); + } + else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32) + { + set_format_if_unknown(*output, Format::F32); + } + else if(input1->data_type() == DataType::F16 || input2->data_type() == DataType::F16) + { + set_format_if_unknown(*output, Format::F16); + } + else if(input1->data_type() == DataType::QS8 && input2->data_type() == DataType::QS8) + { + set_data_type_if_unknown(*output, DataType::QS8); + set_fixed_point_position_if_zero(*output, input1->fixed_point_position()); + } + } // Configure kernel window - Window win = calculate_max_window(*input1, Steps(num_elems_processed_per_iteration)); - AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); + Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); + Window win_input1 = win.broadcast_if_dimension_le_one(*input1); + Window win_input2 = win.broadcast_if_dimension_le_one(*input2); - bool window_changed = update_window_and_padding(win, - AccessWindowHorizontal(input1, 0, num_elems_processed_per_iteration), - AccessWindowHorizontal(input2, 0, num_elems_processed_per_iteration), - output_access); + AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration); + AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); - ValidRegion valid_region = intersect_valid_regions(input1->valid_region(), - input2->valid_region()); + bool window_changed = update_window_and_padding(win_input1, input1_access) + || update_window_and_padding(win_input2, input2_access) + || update_window_and_padding(win, output_access); output_access.set_valid_region(win, valid_region); @@ -508,31 +538,12 @@ void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITe ARM_COMPUTE_UNUSED(rounding_policy); ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); - // Auto initialize output if not initialized - { - set_shape_if_empty(*output->info(), input1->info()->tensor_shape()); - - if(input1->info()->data_type() == DataType::S16 || input2->info()->data_type() == DataType::S16) - { - set_format_if_unknown(*output->info(), Format::S16); - } - else if(input1->info()->data_type() == DataType::F32 || input2->info()->data_type() == DataType::F32) - { - set_format_if_unknown(*output->info(), Format::F32); - } - else if(input1->info()->data_type() == DataType::F16 || input2->info()->data_type() == DataType::F16) - { - set_format_if_unknown(*output->info(), Format::F16); - } - else if(input1->info()->data_type() == DataType::QS8 && input2->info()->data_type() == DataType::QS8) - { - set_data_type_if_unknown(*output->info(), DataType::QS8); - set_fixed_point_position_if_zero(*output->info(), input1->info()->fixed_point_position()); - } - } - ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy)); + // Configure kernel window + auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info()); + ARM_COMPUTE_ERROR_THROW_ON(win_config.first); + _input1 = input1; _input2 = input2; _output = output; @@ -656,15 +667,13 @@ void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITe ARM_COMPUTE_ERROR("You called with the wrong img formats"); } - // Configure kernel window - auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info()); - ARM_COMPUTE_ERROR_THROW_ON(win_config.first); INEKernel::configure(win_config.second); } Status NEPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) { + ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy)); ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first); @@ -677,34 +686,71 @@ void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); - Iterator input1(_input1, window); - Iterator input2(_input2, window); - Iterator output(_output, window); + const TensorShape &in_shape1 = _input1->info()->tensor_shape(); + const TensorShape &in_shape2 = _input2->info()->tensor_shape(); + const TensorShape &out_shape = _output->info()->tensor_shape(); + + bool can_collapse = true; + if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) + { + can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); + for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) + { + can_collapse = (in_shape1[d] == in_shape2[d]); + } + } + + bool has_collapsed = false; + Window collapsed = can_collapse ? window.collapse_if_possible(INEKernel::window(), Window::DimZ, &has_collapsed) : window; + + const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; + const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; + + Window slice = collapsed.first_slice_window_3D(); + Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); + Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); + + Iterator input1(_input1, slice_input1); + Iterator input2(_input2, slice_input2); + Iterator output(_output, slice); if(_func_int != nullptr) { - execute_window_loop(window, [&](const Coordinates & id) + execute_window_loop(collapsed, [&](const Coordinates & id) { (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent); + collapsed.slide_window_slice_3D(slice_input1); + collapsed.slide_window_slice_3D(slice_input2); }, input1, input2, output); } else if(_func_q_int != nullptr) { int fixed_point_position = _input1->info()->fixed_point_position(); - execute_window_loop(window, [&](const Coordinates & id) + execute_window_loop(collapsed, [&](const Coordinates & id) { (*_func_q_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent, fixed_point_position); + collapsed.slide_window_slice_3D(slice_input1); + collapsed.slide_window_slice_3D(slice_input2); }, input1, input2, output); } else { ARM_COMPUTE_ERROR_ON(_func_float == nullptr); - execute_window_loop(window, [&](const Coordinates & id) + execute_window_loop(collapsed, [&](const Coordinates & id) { (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale); + collapsed.slide_window_slice_3D(slice_input1); + collapsed.slide_window_slice_3D(slice_input2); }, input1, input2, output); } } + +BorderSize NEPixelWiseMultiplicationKernel::border_size() const +{ + const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); + const unsigned int border = std::min(num_elems_processed_per_iteration - 1U, replicateSize); + return BorderSize(0, border, 0, 0); +} \ No newline at end of file -- cgit v1.2.1