blob: cc03a517bdc19bbe6e7b689b92aa87c875f3ff0d [file] [log] [blame]
Gian Marco Iodice06b184a2017-08-29 16:05:25 +01001/*
Michalis Spyrou995f5522018-01-29 13:43:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Gian Marco Iodice06b184a2017-08-29 16:05:25 +01003 *
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
41namespace arm_compute
42{
43NEMinMaxLayerKernel::NEMinMaxLayerKernel()
44 : _input(nullptr), _output(nullptr), _mtx()
45{
46}
47
48void 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
82void 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));
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010099 window_input.set(3, Window::Dimension(0, 1, 1));
100
101 Iterator input(_input, window_input);
102 Iterator output(_output, window_output);
103
104 execute_window_loop(window_output, [&](const Coordinates & id_batch)
105 {
106 float32x2_t carry_min = vdup_n_f32(std::numeric_limits<float>::max());
107 float32x2_t carry_max = vdup_n_f32(std::numeric_limits<float>::lowest());
108
109 float carry_min_scalar = std::numeric_limits<float>::max();
110 float carry_max_scalar = std::numeric_limits<float>::lowest();
111
112 execute_window_loop(window_input, [&](const Coordinates & id)
113 {
114 int x = x_start;
115 const auto in_ptr = reinterpret_cast<const float *const>(input.ptr() + id_batch[1] * _input->info()->strides_in_bytes()[3]);
116
117 // Vector loop
118 for(; x <= x_end - 8; x += 8)
119 {
120 const float32x4x2_t pixels = vld2q_f32(in_ptr + x);
121 const float32x4_t tmp_min1 = vminq_f32(pixels.val[0], pixels.val[1]);
122 const float32x4_t tmp_max1 = vmaxq_f32(pixels.val[0], pixels.val[1]);
123 const float32x2_t tmp_min2 = vmin_f32(vget_high_f32(tmp_min1), vget_low_f32(tmp_min1));
124 const float32x2_t tmp_max2 = vmax_f32(vget_high_f32(tmp_max1), vget_low_f32(tmp_max1));
125 carry_min = vmin_f32(tmp_min2, carry_min);
126 carry_max = vmax_f32(tmp_max2, carry_max);
127 }
128
129 // Process leftover pixels
130 for(; x < x_end; ++x)
131 {
132 const float pixel = in_ptr[x];
133 carry_min_scalar = std::min(pixel, carry_min_scalar);
134 carry_max_scalar = std::max(pixel, carry_max_scalar);
135 }
136 },
137 input);
138
139 // Reduce result
140 carry_min = vpmin_f32(carry_min, carry_min);
141 carry_max = vpmax_f32(carry_max, carry_max);
142 carry_min = vpmin_f32(carry_min, carry_min);
143 carry_max = vpmax_f32(carry_max, carry_max);
144
145 // Extract max/min values
146 const float min_i = std::min(vget_lane_f32(carry_min, 0), carry_min_scalar);
147 const float max_i = std::max(vget_lane_f32(carry_max, 0), carry_max_scalar);
148
149 auto out_ptr = reinterpret_cast<float *const>(output.ptr());
150
151 // Perform reduction of local min/max values
152 update_min_max(out_ptr, min_i, max_i);
153 },
154 output);
155}
156
157void NEMinMaxLayerKernel::reset()
158{
159 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
160
161 float32x2_t reset_values = vdup_n_f32(0.0f);
162 reset_values = vset_lane_f32(std::numeric_limits<float>::max(), reset_values, 0);
Isabella Gottardi7e1944d2018-03-12 16:39:05 +0000163 reset_values = vset_lane_f32(std::numeric_limits<float>::lowest(), reset_values, 1);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100164
165 Window window_output;
166 window_output.use_tensor_dimensions(_output->info()->tensor_shape());
167 window_output.set(Window::DimX, Window::Dimension(0, 1, 1));
168
169 Iterator output(_output, window_output);
170
171 execute_window_loop(window_output, [&](const Coordinates & id)
172 {
173 vst1_f32(reinterpret_cast<float *const>(output.ptr()), reset_values);
174 },
175 output);
176}
177
178void NEMinMaxLayerKernel::update_min_max(float *out_ptr, float min, float max)
179{
Pablo Tello9e40cf72017-09-15 16:14:55 +0100180 std::lock_guard<Mutex> lock(_mtx);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100181
182 const float32x2_t old_min = vld1_dup_f32(out_ptr);
183 const float32x2_t old_max = vld1_dup_f32(out_ptr + 1);
184 const float32x2_t new_min = vmin_f32(vdup_n_f32(min), old_min);
185 const float32x2_t new_max = vmax_f32(vdup_n_f32(max), old_max);
186
187 vst1_f32(out_ptr, vzip_f32(new_min, new_max).val[0]);
188}
Pablo Tello9e40cf72017-09-15 16:14:55 +0100189} // namespace arm_compute