Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +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/NEMinMaxLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include <algorithm> |
| 37 | #include <arm_neon.h> |
| 38 | #include <climits> |
| 39 | #include <cstddef> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | NEMinMaxLayerKernel::NEMinMaxLayerKernel() |
| 44 | : _input(nullptr), _output(nullptr), _mtx() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void NEMinMaxLayerKernel::configure(const ITensor *input, ITensor *output) |
| 49 | { |
| 50 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 51 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); |
| 52 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 53 | |
| 54 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 55 | output_shape.set(Window::DimX, 2); |
| 56 | output_shape.remove_dimension(1); |
| 57 | output_shape.remove_dimension(1); |
| 58 | |
| 59 | // Output auto initialization if not yet initialized |
| 60 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 61 | |
| 62 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 63 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
| 64 | |
| 65 | _input = input; |
| 66 | _output = output; |
| 67 | |
| 68 | // Configure kernel window |
| 69 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 70 | |
| 71 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 72 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 73 | AccessWindowHorizontal output_access(output->info(), 0, 2); |
| 74 | |
| 75 | update_window_and_padding(win, input_access, output_access); |
| 76 | |
| 77 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 78 | |
| 79 | INEKernel::configure(win); |
| 80 | } |
| 81 | |
| 82 | void NEMinMaxLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 83 | { |
| 84 | ARM_COMPUTE_UNUSED(info); |
| 85 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 86 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 87 | |
| 88 | const int x_start = window.x().start(); |
| 89 | const int x_end = window.x().end(); |
| 90 | |
| 91 | Window window_output; |
| 92 | window_output.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 93 | window_output.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 94 | |
| 95 | // Handle X dimension manually to split into two loops |
| 96 | // First one will use vector operations, second one processes the left over pixels |
| 97 | Window window_input(window); |
| 98 | window_input.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 99 | window_input.collapse_if_possible(INEKernel::window(), 3); |
| 100 | window_input.set(3, Window::Dimension(0, 1, 1)); |
| 101 | |
| 102 | Iterator input(_input, window_input); |
| 103 | Iterator output(_output, window_output); |
| 104 | |
| 105 | execute_window_loop(window_output, [&](const Coordinates & id_batch) |
| 106 | { |
| 107 | float32x2_t carry_min = vdup_n_f32(std::numeric_limits<float>::max()); |
| 108 | float32x2_t carry_max = vdup_n_f32(std::numeric_limits<float>::lowest()); |
| 109 | |
| 110 | float carry_min_scalar = std::numeric_limits<float>::max(); |
| 111 | float carry_max_scalar = std::numeric_limits<float>::lowest(); |
| 112 | |
| 113 | execute_window_loop(window_input, [&](const Coordinates & id) |
| 114 | { |
| 115 | int x = x_start; |
| 116 | const auto in_ptr = reinterpret_cast<const float *const>(input.ptr() + id_batch[1] * _input->info()->strides_in_bytes()[3]); |
| 117 | |
| 118 | // Vector loop |
| 119 | for(; x <= x_end - 8; x += 8) |
| 120 | { |
| 121 | const float32x4x2_t pixels = vld2q_f32(in_ptr + x); |
| 122 | const float32x4_t tmp_min1 = vminq_f32(pixels.val[0], pixels.val[1]); |
| 123 | const float32x4_t tmp_max1 = vmaxq_f32(pixels.val[0], pixels.val[1]); |
| 124 | const float32x2_t tmp_min2 = vmin_f32(vget_high_f32(tmp_min1), vget_low_f32(tmp_min1)); |
| 125 | const float32x2_t tmp_max2 = vmax_f32(vget_high_f32(tmp_max1), vget_low_f32(tmp_max1)); |
| 126 | carry_min = vmin_f32(tmp_min2, carry_min); |
| 127 | carry_max = vmax_f32(tmp_max2, carry_max); |
| 128 | } |
| 129 | |
| 130 | // Process leftover pixels |
| 131 | for(; x < x_end; ++x) |
| 132 | { |
| 133 | const float pixel = in_ptr[x]; |
| 134 | carry_min_scalar = std::min(pixel, carry_min_scalar); |
| 135 | carry_max_scalar = std::max(pixel, carry_max_scalar); |
| 136 | } |
| 137 | }, |
| 138 | input); |
| 139 | |
| 140 | // Reduce result |
| 141 | carry_min = vpmin_f32(carry_min, carry_min); |
| 142 | carry_max = vpmax_f32(carry_max, carry_max); |
| 143 | carry_min = vpmin_f32(carry_min, carry_min); |
| 144 | carry_max = vpmax_f32(carry_max, carry_max); |
| 145 | |
| 146 | // Extract max/min values |
| 147 | const float min_i = std::min(vget_lane_f32(carry_min, 0), carry_min_scalar); |
| 148 | const float max_i = std::max(vget_lane_f32(carry_max, 0), carry_max_scalar); |
| 149 | |
| 150 | auto out_ptr = reinterpret_cast<float *const>(output.ptr()); |
| 151 | |
| 152 | // Perform reduction of local min/max values |
| 153 | update_min_max(out_ptr, min_i, max_i); |
| 154 | }, |
| 155 | output); |
| 156 | } |
| 157 | |
| 158 | void NEMinMaxLayerKernel::reset() |
| 159 | { |
| 160 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 161 | |
| 162 | float32x2_t reset_values = vdup_n_f32(0.0f); |
| 163 | reset_values = vset_lane_f32(std::numeric_limits<float>::max(), reset_values, 0); |
| 164 | reset_values = vset_lane_f32(std::numeric_limits<float>::min(), reset_values, 1); |
| 165 | |
| 166 | Window window_output; |
| 167 | window_output.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 168 | window_output.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 169 | |
| 170 | Iterator output(_output, window_output); |
| 171 | |
| 172 | execute_window_loop(window_output, [&](const Coordinates & id) |
| 173 | { |
| 174 | vst1_f32(reinterpret_cast<float *const>(output.ptr()), reset_values); |
| 175 | }, |
| 176 | output); |
| 177 | } |
| 178 | |
| 179 | void NEMinMaxLayerKernel::update_min_max(float *out_ptr, float min, float max) |
| 180 | { |
Pablo Tello | 9e40cf7 | 2017-09-15 16:14:55 +0100 | [diff] [blame] | 181 | std::lock_guard<Mutex> lock(_mtx); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 182 | |
| 183 | const float32x2_t old_min = vld1_dup_f32(out_ptr); |
| 184 | const float32x2_t old_max = vld1_dup_f32(out_ptr + 1); |
| 185 | const float32x2_t new_min = vmin_f32(vdup_n_f32(min), old_min); |
| 186 | const float32x2_t new_max = vmax_f32(vdup_n_f32(max), old_max); |
| 187 | |
| 188 | vst1_f32(out_ptr, vzip_f32(new_min, new_max).val[0]); |
| 189 | } |
Pablo Tello | 9e40cf7 | 2017-09-15 16:14:55 +0100 | [diff] [blame] | 190 | } // namespace arm_compute |