Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEReductionOperationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IAccessWindow.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/NEON/INEKernel.h" |
| 31 | #include "arm_compute/core/NEON/NEMath.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | |
| 34 | #include <arm_neon.h> |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
| 38 | namespace |
| 39 | { |
| 40 | template <class F> |
| 41 | class Reducer |
| 42 | { |
| 43 | public: |
| 44 | static void reduceX(const Window &window, const ITensor *input, ITensor *output, F f) |
| 45 | { |
| 46 | // Set out window |
| 47 | Window out_window(window); |
| 48 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 49 | |
| 50 | // Get first input and output slices |
| 51 | Window in_slice = window.first_slice_window_1D(); |
| 52 | Window out_slice = out_window.first_slice_window_1D(); |
| 53 | |
| 54 | do |
| 55 | { |
| 56 | Iterator in(input, in_slice); |
| 57 | Iterator out(output, out_slice); |
| 58 | |
| 59 | f(in, out, in_slice, out_slice); |
| 60 | } |
| 61 | while(window.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(out_slice)); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | struct SumsqOpX |
| 66 | { |
| 67 | inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice) |
| 68 | { |
| 69 | ARM_COMPUTE_UNUSED(out_slice); |
| 70 | float32x4_t vec_sum_value = vdupq_n_f32(0.f); |
| 71 | |
| 72 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 73 | { |
| 74 | const auto in_ptr = reinterpret_cast<const float *>(input.ptr()); |
| 75 | const float32x4_t vec_elements = vld1q_f32(in_ptr); |
| 76 | vec_sum_value = vaddq_f32(vmulq_f32(vec_elements, vec_elements), vec_sum_value); |
| 77 | }, |
| 78 | input); |
| 79 | |
| 80 | float32x2_t carry_addition = vpadd_f32(vget_high_f32(vec_sum_value), vget_low_f32(vec_sum_value)); |
| 81 | carry_addition = vpadd_f32(carry_addition, carry_addition); |
| 82 | |
| 83 | *(reinterpret_cast<float *>(output.ptr())) = vget_lane_f32(carry_addition, 0); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | void reduce_sumsq(const Window &window, const ITensor *input, ITensor *output, unsigned int axis) |
| 88 | { |
| 89 | switch(axis) |
| 90 | { |
| 91 | case 0: |
| 92 | return Reducer<SumsqOpX>::reduceX(window, input, output, SumsqOpX()); |
| 93 | default: |
| 94 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
| 95 | } |
| 96 | } |
| 97 | } // namespace |
| 98 | |
| 99 | NEReductionOperationKernel::NEReductionOperationKernel() |
| 100 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size() |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | BorderSize NEReductionOperationKernel::border_size() const |
| 105 | { |
| 106 | return _border_size; |
| 107 | } |
| 108 | |
| 109 | void NEReductionOperationKernel::configure(const ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op) |
| 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 112 | ARM_COMPUTE_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
| 113 | ARM_COMPUTE_ERROR_ON_MSG(axis > 0, "Unsupported reduction axis, Supported axis is 0"); |
| 114 | |
| 115 | // Calculate output shape and set if empty |
| 116 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 117 | output_shape.set(axis, 1); |
| 118 | |
| 119 | // Output auto initialization if not yet initialized |
| 120 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 121 | |
| 122 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 123 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 124 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
| 125 | |
| 126 | unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->info()->data_type()); |
| 127 | |
| 128 | _input = input; |
| 129 | _output = output; |
| 130 | _border_size = (axis == 0) ? BorderSize(0, num_elems_processed_per_iteration - (input->info()->dimension(0) % num_elems_processed_per_iteration), 0, 0) : BorderSize(); |
| 131 | _op = op; |
| 132 | |
| 133 | // Configure kernel window |
| 134 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 135 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 136 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 137 | |
| 138 | update_window_and_padding(win, input_access, output_access); |
| 139 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 140 | |
| 141 | INEKernel::configure(win); |
| 142 | } |
| 143 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 144 | void NEReductionOperationKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 145 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 146 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 147 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 148 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 149 | |
| 150 | switch(_op) |
| 151 | { |
| 152 | case ReductionOperation::SUM_SQUARE: |
| 153 | reduce_sumsq(window, _input, _output, _reduction_axis); |
| 154 | break; |
| 155 | default: |
| 156 | ARM_COMPUTE_ERROR("Unsupported reduction operation."); |
| 157 | } |
| 158 | } |