Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 99089ce | 2019-02-06 14:16:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/NEPoolingLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/NEAsymm.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/NEON/NEMath.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Utils.h" |
| 36 | #include "arm_compute/core/Validate.h" |
| 37 | #include "arm_compute/core/Window.h" |
Giorgio Arena | 9fb6c7e | 2018-08-22 12:15:25 +0100 | [diff] [blame] | 38 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 40 | #include "support/ToolchainSupport.h" |
| 41 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | #include <algorithm> |
| 43 | #include <arm_neon.h> |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 44 | #include <cmath> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | #include <limits> |
Michele Di Giorgio | 8af2dd6 | 2017-06-19 15:19:29 +0100 | [diff] [blame] | 46 | #include <set> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | #include <string> |
| 48 | #include <tuple> |
| 49 | |
| 50 | using namespace arm_compute; |
Giorgio Arena | 9fb6c7e | 2018-08-22 12:15:25 +0100 | [diff] [blame] | 51 | using namespace misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | |
| 53 | namespace |
| 54 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 55 | inline float calculate_avg_scale(bool exclude_padding, DataLayout data_layout, const Coordinates &id, const int pool_size_x, const int pool_size_y, const int upper_bound_w, const int upper_bound_h, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | const int pad_x, const int pad_y, const int stride_x, const int stride_y) |
| 57 | { |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 58 | const unsigned int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 59 | const unsigned int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 60 | |
| 61 | int start_x = id[idx_width] * stride_x - pad_x; |
| 62 | int start_y = id[idx_height] * stride_y - pad_y; |
| 63 | |
| 64 | const int end_x = std::min(start_x + pool_size_x, upper_bound_w); |
| 65 | const int end_y = std::min(start_y + pool_size_y, upper_bound_h); |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 66 | if(exclude_padding) |
| 67 | { |
| 68 | start_x = std::max(0, start_x); |
| 69 | start_y = std::max(0, start_y); |
| 70 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | return 1.f / ((end_y - start_y) * (end_x - start_x)); |
| 72 | } |
| 73 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 74 | inline void scale_vector_s16x8(bool exclude_padding, uint16x8_t &v, const Coordinates &id, int id_offset, int step, |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 75 | const int pool_size, const int upper_bound_w, const int upper_bound_h, |
| 76 | const int pad_x, const int pad_y, const int stride_x, const int stride_y) |
| 77 | { |
| 78 | int start_x = (id.x() + id_offset) * stride_x - pad_x; |
| 79 | int start_y = id.y() * stride_y - pad_y; |
| 80 | const int end_y = std::min(start_y + pool_size, upper_bound_h); |
| 81 | if(exclude_padding) |
| 82 | { |
| 83 | start_y = std::max(0, start_y); |
| 84 | } |
| 85 | |
| 86 | std::array<uint16_t, 8> elems = |
| 87 | { |
| 88 | { |
| 89 | vgetq_lane_u16(v, 0), |
| 90 | vgetq_lane_u16(v, 1), |
| 91 | vgetq_lane_u16(v, 2), |
| 92 | vgetq_lane_u16(v, 3), |
| 93 | vgetq_lane_u16(v, 4), |
| 94 | vgetq_lane_u16(v, 5), |
| 95 | vgetq_lane_u16(v, 6), |
| 96 | vgetq_lane_u16(v, 7), |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | for(auto &el : elems) |
| 101 | { |
| 102 | int c_start_x = start_x; |
| 103 | const int end_x = std::min(c_start_x + pool_size, upper_bound_w); |
| 104 | if(exclude_padding) |
| 105 | { |
| 106 | c_start_x = std::max(0, c_start_x); |
| 107 | } |
| 108 | float scale = 1.f / ((end_y - start_y) * (end_x - c_start_x)); |
| 109 | el *= scale; |
| 110 | start_x += step * stride_x; |
| 111 | } |
| 112 | |
| 113 | v = vsetq_lane_u16(elems[0], v, 0); |
| 114 | v = vsetq_lane_u16(elems[1], v, 1); |
| 115 | v = vsetq_lane_u16(elems[2], v, 2); |
| 116 | v = vsetq_lane_u16(elems[3], v, 3); |
| 117 | v = vsetq_lane_u16(elems[4], v, 4); |
| 118 | v = vsetq_lane_u16(elems[5], v, 5); |
| 119 | v = vsetq_lane_u16(elems[6], v, 6); |
| 120 | v = vsetq_lane_u16(elems[7], v, 7); |
| 121 | } |
| 122 | |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 123 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &pooled_w, unsigned int pooled_h) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 125 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 127 | int pool_stride_x = 0; |
| 128 | int pool_stride_y = 0; |
| 129 | PoolingType pool_type = pool_info.pool_type(); |
| 130 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
Michele Di Giorgio | 8af2dd6 | 2017-06-19 15:19:29 +0100 | [diff] [blame] | 132 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 133 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 134 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 135 | ARM_COMPUTE_RETURN_ERROR_ON(pool_type == PoolingType::L2 && is_data_type_quantized(input->data_type())); |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 136 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 137 | if(output->total_size() != 0) |
Georgios Pinitas | 1dad50e | 2017-07-03 17:51:34 +0100 | [diff] [blame] | 138 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 139 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH)) != pooled_w) |
| 142 | || (output->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT)) != pooled_h)); |
Georgios Pinitas | 1dad50e | 2017-07-03 17:51:34 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 145 | return Status{}; |
| 146 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 148 | Status validate_arguments_pool_info(const unsigned int pool_size_x, const unsigned int pool_size_y) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 149 | { |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 150 | ARM_COMPUTE_RETURN_ERROR_ON(pool_size_x == 0); |
| 151 | ARM_COMPUTE_RETURN_ERROR_ON(pool_size_y == 0); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 152 | |
| 153 | return Status{}; |
| 154 | } |
| 155 | |
| 156 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info, unsigned int &num_elems_processed_per_iteration, |
| 157 | BorderSize &border_size, |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 158 | unsigned int pooled_w, unsigned int pooled_h, int pool_size_x, int pool_size_y) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 159 | { |
Giorgio Arena | 9fb6c7e | 2018-08-22 12:15:25 +0100 | [diff] [blame] | 160 | // Output auto inizialitation if not yet initialized |
| 161 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_pool_shape(*input, pool_info))); |
| 162 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 163 | DataLayout data_layout = input->data_layout(); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 164 | unsigned int num_elems_read_per_iteration = 0; |
| 165 | unsigned int num_elems_horizontal_window = 0; |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 166 | int pool_stride_x = 0; |
| 167 | int pool_stride_y = 0; |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 168 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 169 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 170 | const int input_width = input->dimension(idx_width); |
| 171 | const int input_height = input->dimension(idx_height); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 172 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); |
| 173 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 174 | const int pool_pad_right = pad_stride_info.pad_right(); |
| 175 | const int pool_pad_top = pad_stride_info.pad_top(); |
| 176 | const int pool_pad_left = pad_stride_info.pad_left(); |
| 177 | const int pool_pad_bottom = pad_stride_info.pad_bottom(); |
| 178 | const bool is_square = pool_size_x == pool_size_y; |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 179 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 180 | // Check output dimensions |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 181 | std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width), |
| 182 | input->dimension(idx_height), |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 183 | pool_size_x, |
| 184 | pool_size_y, |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 185 | pad_stride_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 186 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 187 | //If it's not squared and optimized will be executed the MxN |
| 188 | num_elems_read_per_iteration = 1; |
| 189 | num_elems_processed_per_iteration = 1; |
| 190 | num_elems_horizontal_window = 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 192 | const bool is_nhwc = data_layout == DataLayout::NHWC; |
| 193 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 194 | if(is_square) |
| 195 | { |
| 196 | switch(input->data_type()) |
| 197 | { |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 198 | case DataType::QASYMM8: |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 199 | if(is_nhwc) |
| 200 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 201 | num_elems_processed_per_iteration = 16; |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 204 | switch(pool_size_x) |
| 205 | { |
| 206 | case 2: |
| 207 | num_elems_read_per_iteration = 16; |
| 208 | num_elems_processed_per_iteration = (pool_stride_x == 2) ? 8 : 15; |
| 209 | num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16; |
| 210 | break; |
| 211 | case 3: |
| 212 | num_elems_read_per_iteration = 16; |
| 213 | num_elems_processed_per_iteration = (pool_stride_x == 2) ? 7 : 14; |
| 214 | num_elems_horizontal_window = (pool_stride_x == 2) ? 8 : 16; |
| 215 | break; |
| 216 | default: |
| 217 | break; |
| 218 | } |
| 219 | break; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 220 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 221 | case DataType::F16: |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 222 | if(is_nhwc) |
| 223 | { |
| 224 | num_elems_processed_per_iteration = 8; |
| 225 | break; |
| 226 | } |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 227 | switch(pool_size_x) |
| 228 | { |
| 229 | case 2: |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 230 | case 3: |
| 231 | num_elems_read_per_iteration = 4; |
| 232 | num_elems_processed_per_iteration = 1; |
| 233 | num_elems_horizontal_window = 1; |
| 234 | break; |
| 235 | default: |
| 236 | break; |
| 237 | } |
| 238 | break; |
| 239 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 240 | case DataType::F32: |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 241 | if(is_nhwc) |
| 242 | { |
Georgios Pinitas | 64f1a90 | 2018-09-18 13:42:51 +0100 | [diff] [blame] | 243 | num_elems_processed_per_iteration = 4; |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 244 | break; |
| 245 | } |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 246 | switch(pool_size_x) |
| 247 | { |
| 248 | case 2: |
| 249 | num_elems_read_per_iteration = 2; |
| 250 | break; |
| 251 | case 3: |
| 252 | num_elems_read_per_iteration = 4; // We use vload4 for pooling3 |
| 253 | break; |
| 254 | case 7: |
| 255 | num_elems_read_per_iteration = 8; // We use vload8 for pooling7 |
| 256 | break; |
| 257 | default: |
| 258 | break; |
| 259 | } |
| 260 | num_elems_processed_per_iteration = 1; |
| 261 | num_elems_horizontal_window = 1; |
| 262 | break; |
| 263 | default: |
| 264 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 265 | break; |
| 266 | } |
| 267 | } |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 268 | else |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 269 | { |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 270 | if(is_nhwc) |
| 271 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 272 | num_elems_processed_per_iteration = 16 / input->element_size(); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | bool window_changed = false; |
| 277 | Window win{}; |
| 278 | if(data_layout == DataLayout::NCHW) |
| 279 | { |
| 280 | // Number of iterations in X dimension |
| 281 | const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration; |
| 282 | |
| 283 | // Upper limit for the number of right/bottom border elements that are accessed |
| 284 | const int upper_bound_w = ((num_iterations_x - 1) * num_elems_processed_per_iteration * pool_stride_x - pool_pad_left + num_elems_read_per_iteration) - input_width; |
| 285 | const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height; |
| 286 | |
| 287 | border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left); |
| 288 | border_size.right = std::max(upper_bound_w, pool_pad_right); |
| 289 | border_size.bottom = std::max(upper_bound_h, pool_pad_bottom); |
| 290 | |
| 291 | TensorShape output_shape{ input->tensor_shape() }; |
| 292 | output_shape.set(0, pooled_w); |
| 293 | output_shape.set(1, pooled_h); |
| 294 | TensorInfo output_info(input->clone()->set_tensor_shape(output_shape)); |
| 295 | |
| 296 | win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration)); |
| 297 | AccessWindowStatic input_access(input, -pool_pad_left, -pool_pad_top, input_width + border_size.right, input_height + border_size.bottom); |
| 298 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 299 | AccessWindowHorizontal output_access(output, 0, num_elems_horizontal_window); |
| 300 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 301 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 302 | } |
| 303 | else |
| 304 | { |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 305 | TensorShape output_shape{ input->tensor_shape() }; |
| 306 | output_shape.set(1, pooled_w); |
| 307 | output_shape.set(2, pooled_h); |
| 308 | TensorInfo output_info(input->clone()->set_tensor_shape(output_shape)); |
| 309 | |
| 310 | win = calculate_max_window(output_info, Steps(num_elems_processed_per_iteration)); |
| 311 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 312 | |
| 313 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 314 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 315 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 319 | return std::make_pair(err, win); |
| 320 | } |
| 321 | } // namespace |
| 322 | |
| 323 | NEPoolingLayerKernel::NEPoolingLayerKernel() |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 324 | : _func(nullptr), _input(nullptr), _output(nullptr), _pool_info(), _num_elems_processed_per_iteration(0), _border_size(0), _is_square(false) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 325 | { |
| 326 | } |
| 327 | |
| 328 | BorderSize NEPoolingLayerKernel::border_size() const |
| 329 | { |
| 330 | return _border_size; |
| 331 | } |
| 332 | |
| 333 | void NEPoolingLayerKernel::configure(const ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info) |
| 334 | { |
| 335 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 336 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 337 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 338 | const bool is_global_pooling = pool_info.is_global_pooling(); |
Diego Lopez Recas | 61ef5bf | 2017-12-11 12:36:55 +0000 | [diff] [blame] | 339 | const int pool_stride_x = pad_stride_info.stride().first; |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 340 | |
| 341 | // Get data layout |
| 342 | const DataLayout data_layout = input->info()->data_layout(); |
| 343 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 344 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 345 | |
| 346 | // Update pool size in case of global pooling |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 347 | const Size2D pool_size( |
| 348 | is_global_pooling ? input->info()->dimension(idx_width) : pool_info.pool_size().width, |
| 349 | is_global_pooling ? input->info()->dimension(idx_height) : pool_info.pool_size().height); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 350 | |
| 351 | // Validate pool info before calling scaled_dimensions |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 352 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_pool_info(pool_size.x(), pool_size.y())); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 353 | |
| 354 | // Check output dimensions |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 355 | unsigned int pooled_w; |
| 356 | unsigned int pooled_h; |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 357 | std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(idx_width), |
| 358 | input->info()->dimension(idx_height), |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 359 | pool_size.x(), |
| 360 | pool_size.y(), |
Diego Lopez Recas | 61ef5bf | 2017-12-11 12:36:55 +0000 | [diff] [blame] | 361 | pad_stride_info); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 362 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 363 | // Perform validation step |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 364 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info, pooled_w, pooled_h)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 365 | |
| 366 | // Set instance variables |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 367 | _input = input; |
| 368 | _output = output; |
| 369 | _pool_info = pool_info; |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 370 | _is_square = (pool_size.x() == pool_size.y()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 371 | |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 372 | // Get data type |
| 373 | const DataType data_type = input->info()->data_type(); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 374 | const bool is_nchw = data_layout == DataLayout::NCHW; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 375 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 376 | if(data_type == DataType::QASYMM8) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 377 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 378 | if(pool_size.x() == 2 && pool_stride_x < 3 && _is_square) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 379 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 380 | if(is_nchw) |
Michalis Spyrou | bbd9fb9 | 2017-06-22 12:57:51 +0100 | [diff] [blame] | 381 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 382 | _func = &NEPoolingLayerKernel::pooling2_qasymm8_nchw; |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 389 | else if(pool_size.x() == 3 && pool_stride_x < 3 && _is_square) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 390 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 391 | if(is_nchw) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 392 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 393 | _func = &NEPoolingLayerKernel::pooling3_qasymm8_nchw; |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | else |
| 401 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 402 | if(is_nchw) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 403 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 404 | _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nchw; |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | _func = &NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 412 | else if(data_type == DataType::F16) |
| 413 | { |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 414 | if(_is_square) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 415 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 416 | switch(pool_size.x()) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 417 | { |
| 418 | case 2: |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 419 | { |
| 420 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 421 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 422 | _func = &NEPoolingLayerKernel::pooling2_f16_nchw; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 423 | } |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 424 | else |
| 425 | { |
| 426 | _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc; |
| 427 | } |
| 428 | } |
| 429 | break; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 430 | case 3: |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 431 | { |
| 432 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 433 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 434 | _func = &NEPoolingLayerKernel::pooling3_f16_nchw; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 435 | } |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 436 | else |
| 437 | { |
| 438 | _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc; |
| 439 | } |
| 440 | } |
| 441 | break; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 442 | default: |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 443 | { |
| 444 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 445 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 446 | _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw; |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 451 | } |
| 452 | break; |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 453 | } |
| 454 | break; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | else |
| 458 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 459 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 460 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 461 | _func = &NEPoolingLayerKernel::poolingMxN_f16_nchw; |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | _func = &NEPoolingLayerKernel::poolingMxN_f16_nhwc; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 466 | } |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | else if(data_type == DataType::F32) |
| 470 | { |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 471 | if(_is_square) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 472 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 473 | switch(pool_size.x()) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 474 | { |
| 475 | case 2: |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 476 | { |
| 477 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 478 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 479 | _func = &NEPoolingLayerKernel::pooling2_f32_nchw; |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 484 | } |
| 485 | break; |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 486 | } |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 487 | case 3: |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 488 | { |
| 489 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 490 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 491 | _func = &NEPoolingLayerKernel::pooling3_f32_nchw; |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 496 | } |
| 497 | break; |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 498 | } |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 499 | case 7: |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 500 | { |
| 501 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 502 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 503 | _func = &NEPoolingLayerKernel::pooling7_f32_nchw; |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 508 | } |
| 509 | break; |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 510 | } |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 511 | default: |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 512 | { |
| 513 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 514 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 515 | _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw; |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 520 | } |
| 521 | break; |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 522 | } |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | else |
| 526 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 527 | if(is_nchw) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 528 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 529 | _func = &NEPoolingLayerKernel::poolingMxN_f32_nchw; |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | _func = &NEPoolingLayerKernel::poolingMxN_f32_nhwc; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 534 | } |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 535 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | // Configure kernel window |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 539 | auto win_config = validate_and_configure_window(input->info(), output->info(), pool_info, _num_elems_processed_per_iteration, _border_size, pooled_w, pooled_h, pool_size.x(), pool_size.y()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 540 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 541 | INEKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 544 | void NEPoolingLayerKernel::pooling2_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 545 | { |
| 546 | Iterator input(_input, window_input); |
| 547 | Iterator output(_output, window); |
| 548 | |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 549 | constexpr int pool_size = 2; |
| 550 | int pool_stride_x = 0; |
| 551 | int pool_stride_y = 0; |
| 552 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 553 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 554 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 555 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 556 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 557 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 558 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 559 | |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 560 | const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))); |
| 561 | const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1)); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 562 | |
| 563 | const int scale_step_x = (pool_stride_x == 1) ? 2 : 1; |
| 564 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 565 | const UniformQuantizationInfo input_qinfo = _input->info()->quantization_info().uniform(); |
| 566 | const UniformQuantizationInfo output_qinfo = _output->info()->quantization_info().uniform(); |
| 567 | const bool have_different_qinfo = input_qinfo != output_qinfo; |
| 568 | |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 569 | execute_window_loop(window, [&](const Coordinates & id) |
| 570 | { |
| 571 | const auto top_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_top_ptr + input.offset())); |
| 572 | const auto bottom_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_bottom_ptr + input.offset())); |
| 573 | uint8x8_t lower_res = {}; |
| 574 | uint8x8_t upper_res = {}; |
| 575 | |
| 576 | if(pooling_type != PoolingType::MAX) |
| 577 | { |
| 578 | const uint16x8x2_t top_data_u16 = { { vmovl_u8(vget_low_u8(top_data)), vmovl_u8(vget_high_u8(top_data)) } }; |
| 579 | const uint16x8x2_t bottom_data_u16 = { { vmovl_u8(vget_low_u8(bottom_data)), vmovl_u8(vget_high_u8(bottom_data)) } }; |
| 580 | |
| 581 | // Add rows |
| 582 | const uint16x8x2_t vrsum = |
| 583 | { |
| 584 | { |
| 585 | vaddq_u16(top_data_u16.val[0], bottom_data_u16.val[0]), |
| 586 | vaddq_u16(top_data_u16.val[1], bottom_data_u16.val[1]), |
| 587 | } |
| 588 | }; |
| 589 | |
| 590 | // Pair-wise add row data |
| 591 | const uint16x4x2_t vpsum = |
| 592 | { |
| 593 | { |
| 594 | vpadd_u16(vget_low_u16(vrsum.val[0]), vget_high_u16(vrsum.val[0])), |
| 595 | vpadd_u16(vget_low_u16(vrsum.val[1]), vget_high_u16(vrsum.val[1])), |
| 596 | } |
| 597 | }; |
| 598 | |
| 599 | uint16x8_t res_lower = vcombine_u16(vpsum.val[0], vpsum.val[1]); |
| 600 | |
| 601 | // Scale lower result |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 602 | scale_vector_s16x8(exclude_padding, res_lower, id, 0, scale_step_x, |
| 603 | pool_size, upper_bound_w, upper_bound_h, |
| 604 | pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 605 | lower_res = vmovn_u16(res_lower); |
| 606 | |
| 607 | // Compute upper result for stride_x == 1 |
| 608 | if(pool_stride_x == 1) |
| 609 | { |
| 610 | // Shifted row sum |
| 611 | const uint16x8x2_t vrsum_shifted = |
| 612 | { |
| 613 | { |
| 614 | vextq_u16(vrsum.val[0], vrsum.val[1], 1), |
| 615 | vextq_u16(vrsum.val[1], vrsum.val[1], 1) |
| 616 | } |
| 617 | }; |
| 618 | |
| 619 | // Pair-wise add shifted row |
| 620 | const uint16x4x2_t vpsum_shifted = |
| 621 | { |
| 622 | { |
| 623 | vpadd_u16(vget_low_u16(vrsum_shifted.val[0]), vget_high_u16(vrsum_shifted.val[0])), |
| 624 | vpadd_u16(vget_low_u16(vrsum_shifted.val[1]), vget_high_u16(vrsum_shifted.val[1])), |
| 625 | } |
| 626 | }; |
| 627 | uint16x8_t res_upper = vcombine_u16(vpsum_shifted.val[0], vpsum_shifted.val[1]); |
| 628 | |
| 629 | // Scale lower result |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 630 | scale_vector_s16x8(exclude_padding, res_upper, id, 1, 2, |
| 631 | pool_size, upper_bound_w, upper_bound_h, |
| 632 | pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 633 | upper_res = vmovn_u16(res_upper); |
| 634 | } |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | const uint8x16_t max_data = vmaxq_u8(top_data, bottom_data); |
| 639 | lower_res = vpmax_u8(vget_low_u8(max_data), vget_high_u8(max_data)); |
| 640 | if(pool_stride_x == 1) |
| 641 | { |
| 642 | const uint8x16_t max_data_shifted = vextq_u8(max_data, max_data, 1); |
| 643 | upper_res = vpmax_u8(vget_low_u8(max_data_shifted), vget_high_u8(max_data_shifted)); |
| 644 | } |
| 645 | } |
| 646 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 647 | if(have_different_qinfo) |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 648 | { |
| 649 | const auto requantized_output = vquantize(vdequantize(vcombine_u8(lower_res, upper_res), input_qinfo), output_qinfo); |
| 650 | lower_res = vget_low_u8(requantized_output); |
| 651 | upper_res = vget_high_u8(requantized_output); |
| 652 | } |
| 653 | |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 654 | // Store result |
| 655 | if(pool_stride_x == 1) |
| 656 | { |
| 657 | const uint8x8x2_t res = { { lower_res, upper_res } }; |
| 658 | vst2_u8(reinterpret_cast<uint8_t *>(output.ptr()), res); |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), lower_res); |
| 663 | } |
| 664 | }, |
| 665 | input, output); |
| 666 | } |
| 667 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 668 | void NEPoolingLayerKernel::pooling3_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 669 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 670 | ARM_COMPUTE_UNUSED(pooling_type); |
| 671 | ARM_COMPUTE_UNUSED(exclude_padding); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 672 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 673 | Iterator input(_input, window_input); |
| 674 | Iterator output(_output, window); |
| 675 | |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 676 | constexpr const int pool_size = 3; |
| 677 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 678 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 679 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 680 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 681 | int pool_stride_x = 0; |
| 682 | int pool_stride_y = 0; |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 683 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 684 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 685 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 686 | |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 687 | const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))); |
| 688 | const unsigned char *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1)); |
| 689 | const unsigned char *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 2)); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 690 | |
| 691 | execute_window_loop(window, [&](const Coordinates & id) |
| 692 | { |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 693 | float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset())); |
| 694 | float16x4_t middle_data = vld1_f16(reinterpret_cast<const float16_t *>(input_middle_ptr + input.offset())); |
| 695 | float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset())); |
| 696 | float16x4_t res = {}; |
| 697 | |
| 698 | // Get power of 2 in case of l2 pooling |
| 699 | if(pooling_type == PoolingType::L2) |
| 700 | { |
| 701 | top_data = vmul_f16(top_data, top_data); |
| 702 | middle_data = vmul_f16(middle_data, middle_data); |
| 703 | bottom_data = vmul_f16(bottom_data, bottom_data); |
| 704 | } |
| 705 | |
| 706 | if(pooling_type != PoolingType::MAX) |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 707 | { |
| 708 | // Calculate scale |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 709 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 710 | const float16x4_t scale_v = vdup_n_f16(scale); |
| 711 | // Perform pooling |
| 712 | const float16x4_t sum_data = vadd_f16(vadd_f16(top_data, bottom_data), middle_data); |
| 713 | res = vpadd_f16(vset_lane_f16(0.f, sum_data, 3), sum_data); |
| 714 | res = vmul_f16(vpadd_f16(res, res), scale_v); |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | const float16x4_t max_data = vmax_f16(vmax_f16(top_data, bottom_data), middle_data); |
| 719 | res = vpmax_f16(vset_lane_f16(-std::numeric_limits<float>::max(), max_data, 3), max_data); |
| 720 | res = vpmax_f16(res, res); |
| 721 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 722 | |
| 723 | // Calculate square-root in case of l2 pooling |
| 724 | if(pooling_type == PoolingType::L2) |
| 725 | { |
| 726 | res = vinv_f16(vinvsqrt_f16(res)); |
| 727 | } |
| 728 | |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 729 | *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0); |
| 730 | }, |
| 731 | input, output); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 732 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 733 | ARM_COMPUTE_UNUSED(window_input); |
| 734 | ARM_COMPUTE_UNUSED(window); |
| 735 | ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a"); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 736 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 739 | void NEPoolingLayerKernel::pooling2_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 740 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 741 | ARM_COMPUTE_UNUSED(pooling_type); |
| 742 | ARM_COMPUTE_UNUSED(exclude_padding); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 743 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 744 | Iterator input(_input, window_input); |
| 745 | Iterator output(_output, window); |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 746 | constexpr int pool_size = 2; |
| 747 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 748 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 749 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 750 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 751 | int pool_stride_x, pool_stride_y = 0; |
| 752 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 753 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 754 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 755 | |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 756 | const unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))); |
| 757 | const unsigned char *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1)); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 758 | |
| 759 | execute_window_loop(window, [&](const Coordinates & id) |
| 760 | { |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 761 | float16x4_t top_data = vld1_f16(reinterpret_cast<const float16_t *>(input_top_ptr + input.offset())); |
| 762 | float16x4_t bottom_data = vld1_f16(reinterpret_cast<const float16_t *>(input_bottom_ptr + input.offset())); |
| 763 | float16x4_t res = {}; |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 764 | |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 765 | // Get power of 2 in case of l2 pooling |
| 766 | if(pooling_type == PoolingType::L2) |
| 767 | { |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 768 | top_data = vmul_f16(top_data, top_data); |
| 769 | bottom_data = vmul_f16(bottom_data, bottom_data); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | if(pooling_type != PoolingType::MAX) |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 773 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 774 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 775 | const float16x4_t scale_v = vdup_n_f16(scale); |
| 776 | |
| 777 | const float16x4_t sum_data = vadd_f16(top_data, bottom_data); |
| 778 | res = vmul_f16(vpadd_f16(sum_data, sum_data), scale_v); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 779 | } |
| 780 | else |
| 781 | { |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 782 | const float16x4_t max_data = vmax_f16(top_data, bottom_data); |
| 783 | res = vpmax_f16(max_data, max_data); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 784 | } |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 785 | |
| 786 | // Calculate square-root in case of l2 pooling |
| 787 | if(pooling_type == PoolingType::L2) |
| 788 | { |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 789 | res = vinv_f16(vinvsqrt_f16(res)); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // Store result |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 793 | *(reinterpret_cast<float16_t *>(output.ptr())) = vget_lane_f16(res, 0); |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 794 | }, |
| 795 | input, output); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 796 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 797 | ARM_COMPUTE_UNUSED(window_input); |
| 798 | ARM_COMPUTE_UNUSED(window); |
| 799 | ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a"); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 800 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 803 | void NEPoolingLayerKernel::pooling3_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 804 | { |
| 805 | Iterator input(_input, window_input); |
| 806 | Iterator output(_output, window); |
| 807 | |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 808 | constexpr int pool_size = 3; |
| 809 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 810 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 811 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 812 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 813 | int pool_stride_x = 0; |
| 814 | int pool_stride_y = 0; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 815 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 816 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 817 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 818 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 819 | const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform(); |
| 820 | const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform(); |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 821 | |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 822 | const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))); |
| 823 | const uint8_t *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1)); |
| 824 | const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 2)); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 825 | |
| 826 | execute_window_loop(window, [&](const Coordinates & id) |
| 827 | { |
| 828 | const auto top_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_top_ptr + input.offset())); |
| 829 | const auto middle_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_middle_ptr + input.offset())); |
| 830 | const auto bottom_data = vld1q_u8(reinterpret_cast<const uint8_t *>(input_bottom_ptr + input.offset())); |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 831 | uint8x8_t fres = {}; |
| 832 | uint8x16_t fqres = {}; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 833 | |
| 834 | if(pooling_type == PoolingType::AVG) |
| 835 | { |
| 836 | // Convert data to u16 |
| 837 | const uint16x8x2_t top_data_u16 = { { vmovl_u8(vget_low_u8(top_data)), vmovl_u8(vget_high_u8(top_data)) } }; |
| 838 | const uint16x8x2_t middle_data_u16 = { { vmovl_u8(vget_low_u8(middle_data)), vmovl_u8(vget_high_u8(middle_data)) } }; |
| 839 | const uint16x8x2_t bottom_data_u16 = { { vmovl_u8(vget_low_u8(bottom_data)), vmovl_u8(vget_high_u8(bottom_data)) } }; |
| 840 | |
| 841 | // Calculate row sums |
| 842 | const uint16x8x2_t vrsum = |
| 843 | { |
| 844 | { |
| 845 | vaddq_u16(vaddq_u16(top_data_u16.val[0], bottom_data_u16.val[0]), middle_data_u16.val[0]), |
| 846 | vaddq_u16(vaddq_u16(top_data_u16.val[1], bottom_data_u16.val[1]), middle_data_u16.val[1]), |
| 847 | } |
| 848 | }; |
| 849 | const uint16x8x2_t vrsum_shifted_1 = |
| 850 | { |
| 851 | { |
| 852 | vextq_u16(vrsum.val[0], vrsum.val[1], 1), |
| 853 | vextq_u16(vrsum.val[1], vrsum.val[1], 1) |
| 854 | } |
| 855 | }; |
| 856 | const uint16x8x2_t vrsum_shifted_2 = |
| 857 | { |
| 858 | { |
| 859 | vextq_u16(vrsum.val[0], vrsum.val[1], 2), |
| 860 | vextq_u16(vrsum.val[1], vrsum.val[1], 2) |
| 861 | } |
| 862 | }; |
| 863 | // Calculate final sum |
| 864 | uint16x8x2_t final_sum = |
| 865 | { |
| 866 | { |
| 867 | vaddq_u16(vaddq_u16(vrsum.val[0], vrsum_shifted_1.val[0]), vrsum_shifted_2.val[0]), |
| 868 | vaddq_u16(vaddq_u16(vrsum.val[1], vrsum_shifted_1.val[1]), vrsum_shifted_2.val[1]), |
| 869 | } |
| 870 | }; |
| 871 | if(pool_stride_x == 2) |
| 872 | { |
| 873 | uint16x8_t res = |
| 874 | { |
| 875 | vgetq_lane_u16(final_sum.val[0], 0), |
| 876 | vgetq_lane_u16(final_sum.val[0], 2), |
| 877 | vgetq_lane_u16(final_sum.val[0], 4), |
| 878 | vgetq_lane_u16(final_sum.val[0], 6), |
| 879 | vgetq_lane_u16(final_sum.val[1], 0), |
| 880 | vgetq_lane_u16(final_sum.val[1], 2), |
| 881 | vgetq_lane_u16(final_sum.val[1], 4), |
| 882 | vgetq_lane_u16(final_sum.val[1], 6), |
| 883 | }; |
| 884 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 885 | scale_vector_s16x8(exclude_padding, res, id, 0, 1, |
| 886 | pool_size, upper_bound_w, upper_bound_h, |
| 887 | pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 888 | fres = vmovn_u16(res); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 889 | } |
| 890 | else |
| 891 | { |
| 892 | // Scale lower result |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 893 | scale_vector_s16x8(exclude_padding, final_sum.val[0], id, 0, 1, |
| 894 | pool_size, upper_bound_w, upper_bound_h, |
| 895 | pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 896 | // Scale lower result |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 897 | scale_vector_s16x8(exclude_padding, final_sum.val[1], id, 8, 1, |
| 898 | pool_size, upper_bound_w, upper_bound_h, |
| 899 | pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 900 | fqres = vcombine_u8(vmovn_u16(final_sum.val[0]), vmovn_u16(final_sum.val[1])); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | else |
| 904 | { |
| 905 | const uint8x16_t max_data = vmaxq_u8(vmaxq_u8(top_data, bottom_data), middle_data); |
| 906 | const uint8x16_t max_data_shift1 = vextq_u8(max_data, max_data, 1); |
| 907 | const uint8x16_t max_data_shift2 = vextq_u8(max_data, max_data, 2); |
| 908 | const uint8x16_t final_max = vmaxq_u8(vmaxq_u8(max_data, max_data_shift1), max_data_shift2); |
| 909 | |
| 910 | if(pool_stride_x == 2) |
| 911 | { |
| 912 | const uint8x8x2_t table = { { vget_low_u8(final_max), vget_high_u8(final_max) } }; |
| 913 | static const uint8x8_t lookup_val = { 0, 2, 4, 6, 8, 10, 12, 14 }; |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 914 | fres = vtbl2_u8(table, lookup_val); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 915 | } |
| 916 | else |
| 917 | { |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 918 | fqres = final_max; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 919 | } |
| 920 | } |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 921 | |
| 922 | // Store result |
| 923 | if(pool_stride_x == 1) |
| 924 | { |
| 925 | if(input_qinfo != output_qinfo) |
| 926 | { |
| 927 | fqres = vquantize(vdequantize(fqres, input_qinfo), output_qinfo); |
| 928 | } |
| 929 | vst1q_u8(reinterpret_cast<uint8_t *>(output.ptr()), fqres); |
| 930 | } |
| 931 | else |
| 932 | { |
| 933 | if(input_qinfo != output_qinfo) |
| 934 | { |
| 935 | fres = vquantize(vdequantize(fres, input_qinfo), output_qinfo); |
| 936 | } |
| 937 | vst1_u8(reinterpret_cast<uint8_t *>(output.ptr()), fres); |
| 938 | } |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 939 | }, |
| 940 | input, output); |
| 941 | } |
| 942 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 943 | void NEPoolingLayerKernel::poolingMxN_f16_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 944 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 945 | ARM_COMPUTE_UNUSED(pooling_type); |
| 946 | ARM_COMPUTE_UNUSED(exclude_padding); |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 947 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 948 | Iterator input(_input, window_input); |
| 949 | Iterator output(_output, window); |
| 950 | |
| 951 | const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width; |
| 952 | const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height; |
| 953 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 954 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 955 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 956 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 957 | int pool_stride_x = 0; |
| 958 | int pool_stride_y = 0; |
| 959 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 960 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 961 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
| 962 | |
| 963 | execute_window_loop(window, [&](const Coordinates & id) |
| 964 | { |
| 965 | float16_t res = 0.0f; |
| 966 | float16x8_t vres = vdupq_n_f16(0.0f); |
| 967 | |
| 968 | if(pooling_type != PoolingType::MAX) |
| 969 | { |
| 970 | // Calculate scale |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 971 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 972 | |
| 973 | // Perform pooling |
| 974 | |
| 975 | for(int y = 0; y < pool_size_y; ++y) |
| 976 | { |
| 977 | int x = 0; |
| 978 | for(; x <= (pool_size_x - 8); x += 8) |
| 979 | { |
| 980 | const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + |
| 981 | (y - pool_pad_top) * _input->info()->strides_in_bytes().y())); |
| 982 | |
| 983 | // Get power of 2 in case of l2 pooling and accumulate |
| 984 | if(pooling_type == PoolingType::L2) |
| 985 | { |
| 986 | vres = vaddq_f16(vres, vmulq_f16(data, data)); |
| 987 | } |
| 988 | else |
| 989 | { |
| 990 | vres = vaddq_f16(vres, data); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | // Leftover for loop |
| 995 | for(; x < pool_size_x; ++x) |
| 996 | { |
| 997 | float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y())); |
| 998 | |
| 999 | // Get power of 2 in case of l2 pooling |
| 1000 | if(pooling_type == PoolingType::L2) |
| 1001 | { |
| 1002 | data *= data; |
| 1003 | } |
| 1004 | |
| 1005 | res += data; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | // Reduction |
| 1010 | float16x4_t tmp = vpadd_f16(vget_high_f16(vres), vget_low_f16(vres)); |
| 1011 | res += vget_lane_f16(tmp, 0); |
| 1012 | res += vget_lane_f16(tmp, 1); |
| 1013 | res += vget_lane_f16(tmp, 2); |
| 1014 | res += vget_lane_f16(tmp, 3); |
| 1015 | |
| 1016 | // Divide by scale |
| 1017 | res *= scale; |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | float16x8_t vres = vdupq_n_f16(std::numeric_limits<float>::lowest()); |
| 1022 | res = std::numeric_limits<float>::lowest(); |
| 1023 | |
| 1024 | for(int y = 0; y < pool_size_y; ++y) |
| 1025 | { |
| 1026 | int x = 0; |
| 1027 | for(; x <= (pool_size_x - 8); x += 8) |
| 1028 | { |
| 1029 | const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + |
| 1030 | (y - pool_pad_top) * _input->info()->strides_in_bytes().y())); |
| 1031 | vres = vmaxq_f16(vres, data); |
| 1032 | } |
| 1033 | |
| 1034 | // Leftover for loop |
| 1035 | for(; x < pool_size_x; ++x) |
| 1036 | { |
| 1037 | const float16_t data = *(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().x() + (y - pool_pad_top) * _input->info()->strides_in_bytes().y())); |
| 1038 | res = std::max(res, data); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | float16x4_t tmp = vpmax_f16(vget_high_f16(vres), vget_low_f16(vres)); |
| 1043 | res = std::max(res, vget_lane_f16(tmp, 0)); |
| 1044 | res = std::max(res, vget_lane_f16(tmp, 1)); |
| 1045 | res = std::max(res, vget_lane_f16(tmp, 2)); |
| 1046 | res = std::max(res, vget_lane_f16(tmp, 3)); |
| 1047 | } |
| 1048 | |
| 1049 | // Calculate square-root in case of l2 pooling |
| 1050 | if(pooling_type == PoolingType::L2) |
| 1051 | { |
| 1052 | res = std::sqrt(res); |
| 1053 | } |
| 1054 | |
| 1055 | // Store result |
| 1056 | *(reinterpret_cast<float16_t *>(output.ptr())) = res; |
| 1057 | }, |
| 1058 | input, output); |
| 1059 | |
| 1060 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 1061 | ARM_COMPUTE_UNUSED(window_input); |
| 1062 | ARM_COMPUTE_UNUSED(window); |
| 1063 | ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a"); |
| 1064 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 1065 | } |
| 1066 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1067 | void NEPoolingLayerKernel::poolingMxN_f16_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1068 | { |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1069 | ARM_COMPUTE_UNUSED(pooling_type); |
| 1070 | ARM_COMPUTE_UNUSED(exclude_padding); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1071 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1072 | Iterator input(_input, window_input); |
| 1073 | Iterator output(_output, window); |
| 1074 | |
| 1075 | const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width; |
| 1076 | const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height; |
| 1077 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1078 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1079 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1080 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1081 | int pool_stride_x = 0; |
| 1082 | int pool_stride_y = 0; |
| 1083 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 1084 | const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right); |
| 1085 | const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom); |
| 1086 | |
| 1087 | float16x8_t vres; |
| 1088 | |
| 1089 | execute_window_loop(window, [&](const Coordinates & id) |
| 1090 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1091 | const int idx_width = id.y() * pool_stride_x; |
| 1092 | const int idx_height = id.z() * pool_stride_y; |
| 1093 | const int pool_limit_y = pool_pad_top - idx_height; |
| 1094 | const int pool_limit_x = pool_pad_left - idx_width; |
| 1095 | |
| 1096 | const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y); |
| 1097 | const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y); |
| 1098 | const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x); |
| 1099 | const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x); |
| 1100 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1101 | if(pooling_type != PoolingType::MAX) |
| 1102 | { |
| 1103 | // Calculate scale |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1104 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, |
| 1105 | pool_stride_y); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1106 | const float16x8_t scale_v = vdupq_n_f16(scale); |
| 1107 | |
| 1108 | // Perform pooling |
| 1109 | vres = vdupq_n_f16(0.0f); |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1110 | for(int y = pool_start_y; y < pool_end_y; ++y) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1111 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1112 | for(int x = pool_start_x; x < pool_end_x; ++x) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1113 | { |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1114 | const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() + |
| 1115 | (y - pool_pad_top) * _input->info()->strides_in_bytes().z())); |
| 1116 | |
| 1117 | // Get power of 2 in case of l2 pooling and accumulate |
| 1118 | if(pooling_type == PoolingType::L2) |
| 1119 | { |
| 1120 | vres = vaddq_f16(vres, vmulq_f16(data, data)); |
| 1121 | } |
| 1122 | else |
| 1123 | { |
| 1124 | vres = vaddq_f16(vres, data); |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | // Divide by scale |
| 1129 | vres = vmulq_f16(vres, scale_v); |
| 1130 | } |
| 1131 | else |
| 1132 | { |
| 1133 | vres = vdupq_n_f16(std::numeric_limits<float>::lowest()); |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1134 | |
| 1135 | for(int y = pool_start_y; y < pool_end_y; ++y) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1136 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1137 | for(int x = pool_start_x; x < pool_end_x; ++x) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1138 | { |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1139 | const float16x8_t data = vld1q_f16(reinterpret_cast<const float16_t *>(input.ptr() + (x - pool_pad_left) * _input->info()->strides_in_bytes().y() + |
| 1140 | (y - pool_pad_top) * _input->info()->strides_in_bytes().z())); |
| 1141 | vres = vmaxq_f16(vres, data); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | // Calculate square-root in case of l2 pooling |
| 1147 | if(pooling_type == PoolingType::L2) |
| 1148 | { |
| 1149 | float16x8_t sqrt_reciprocal = vrsqrteq_f16(vres); |
| 1150 | vres = vmulq_f16(vres, vmulq_f16(vrsqrtsq_f16(vmulq_f16(vres, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal)); |
| 1151 | } |
| 1152 | |
| 1153 | // Store result |
| 1154 | vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vres); |
| 1155 | }, |
| 1156 | input, output); |
| 1157 | |
| 1158 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 1159 | ARM_COMPUTE_UNUSED(window_input); |
| 1160 | ARM_COMPUTE_UNUSED(window); |
| 1161 | ARM_COMPUTE_ERROR("FP16 Not supported! Recompile the library with arch=arm64-v8.2-a"); |
| 1162 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 1163 | } |
| 1164 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1165 | void NEPoolingLayerKernel::poolingMxN_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1166 | { |
| 1167 | Iterator input(_input, window_input); |
| 1168 | Iterator output(_output, window); |
| 1169 | |
| 1170 | const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width; |
| 1171 | const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height; |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 1172 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1173 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1174 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1175 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1176 | int pool_stride_x = 0; |
| 1177 | int pool_stride_y = 0; |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1178 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 1179 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 1180 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1181 | |
| 1182 | execute_window_loop(window, [&](const Coordinates & id) |
| 1183 | { |
| 1184 | float res = 0.0f; |
| 1185 | |
| 1186 | if(pooling_type != PoolingType::MAX) |
| 1187 | { |
| 1188 | // Calculate scale |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1189 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1190 | |
| 1191 | // Perform pooling |
| 1192 | float32x4_t vres = vdupq_n_f32(0.0f); |
| 1193 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1194 | for(int y = 0; y < pool_size_y; ++y) |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1195 | { |
| 1196 | int x = 0; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1197 | for(; x <= (pool_size_x - 4); x += 4) |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1198 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1199 | const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1200 | (_input->info()->strides_in_bytes().y()))); |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1201 | |
| 1202 | // Get power of 2 in case of l2 pooling and accumulate |
| 1203 | if(pooling_type == PoolingType::L2) |
| 1204 | { |
| 1205 | vres = vmlaq_f32(vres, data, data); |
| 1206 | } |
| 1207 | else |
| 1208 | { |
| 1209 | vres = vaddq_f32(vres, data); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | // Leftover for loop |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1214 | for(; x < pool_size_x; ++x) |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1215 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1216 | float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1217 | (_input->info()->strides_in_bytes().y()))); |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1218 | |
| 1219 | // Get power of 2 in case of l2 pooling |
| 1220 | if(pooling_type == PoolingType::L2) |
| 1221 | { |
| 1222 | data *= data; |
| 1223 | } |
| 1224 | |
| 1225 | res += data; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | #if defined(__aarch64__) |
| 1230 | // Reduction operation available on 64 bit architectures only |
| 1231 | res += vaddvq_f32(vres); |
| 1232 | #else // __aarch64__ |
| 1233 | // Reduction |
| 1234 | float32x2_t tmp = vpadd_f32(vget_high_f32(vres), vget_low_f32(vres)); |
| 1235 | tmp = vpadd_f32(tmp, tmp); |
| 1236 | |
| 1237 | res += vget_lane_f32(tmp, 0); |
| 1238 | #endif // __aarch64__ |
| 1239 | // Divide by scale |
| 1240 | res *= scale; |
| 1241 | } |
| 1242 | else |
| 1243 | { |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1244 | float32x4_t vres = vdupq_n_f32(std::numeric_limits<float>::lowest()); |
| 1245 | res = std::numeric_limits<float>::lowest(); |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1246 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1247 | for(int y = 0; y < pool_size_y; ++y) |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1248 | { |
| 1249 | int x = 0; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1250 | for(; x <= (pool_size_x - 4); x += 4) |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1251 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1252 | const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1253 | (_input->info()->strides_in_bytes().y()))); |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1254 | vres = vmaxq_f32(vres, data); |
| 1255 | } |
| 1256 | |
| 1257 | // Leftover for loop |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1258 | for(; x < pool_size_x; ++x) |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1259 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1260 | const float data = *(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1261 | (_input->info()->strides_in_bytes().y()))); |
Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1262 | res = std::max(res, data); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | #if defined(__aarch64__) |
| 1267 | // Reduction operation available on 64 bit architectures only |
| 1268 | res = std::max(vmaxvq_f32(vres), res); |
| 1269 | #else // __aarch64__ |
| 1270 | float32x2_t tmp = vpmax_f32(vget_high_f32(vres), vget_low_f32(vres)); |
| 1271 | tmp = vpmax_f32(tmp, tmp); |
| 1272 | |
| 1273 | res = std::max(res, vget_lane_f32(tmp, 0)); |
| 1274 | #endif // __aarch64__ |
| 1275 | } |
| 1276 | |
| 1277 | // Calculate square-root in case of l2 pooling |
| 1278 | if(pooling_type == PoolingType::L2) |
| 1279 | { |
| 1280 | res = std::sqrt(res); |
| 1281 | } |
| 1282 | |
| 1283 | // Store result |
| 1284 | *(reinterpret_cast<float *>(output.ptr())) = res; |
| 1285 | }, |
| 1286 | input, output); |
| 1287 | } |
| 1288 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1289 | void NEPoolingLayerKernel::pooling2_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
| 1290 | { |
| 1291 | Iterator input(_input, window_input); |
| 1292 | Iterator output(_output, window); |
| 1293 | |
| 1294 | constexpr int pool_size = 2; |
| 1295 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1296 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1297 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1298 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1299 | int pool_stride_x = 0; |
| 1300 | int pool_stride_y = 0; |
| 1301 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 1302 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 1303 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
| 1304 | |
| 1305 | const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))); |
| 1306 | const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1)); |
| 1307 | |
| 1308 | execute_window_loop(window, [&](const Coordinates & id) |
| 1309 | { |
| 1310 | float32x2_t top_data = vld1_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset())); |
| 1311 | float32x2_t bottom_data = vld1_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset())); |
| 1312 | float32x2_t res = {}; |
| 1313 | float final_res = 0; |
| 1314 | |
| 1315 | // Get power of 2 in case of l2 pooling |
| 1316 | if(pooling_type == PoolingType::L2) |
| 1317 | { |
| 1318 | top_data = vmul_f32(top_data, top_data); |
| 1319 | bottom_data = vmul_f32(bottom_data, bottom_data); |
| 1320 | } |
| 1321 | |
| 1322 | if(pooling_type != PoolingType::MAX) |
| 1323 | { |
| 1324 | // Calculate scale |
| 1325 | float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
| 1326 | const float32x2_t scale_v = vdup_n_f32(scale); |
| 1327 | |
| 1328 | // Perform pooling |
| 1329 | const float32x2_t sum_data = vadd_f32(top_data, bottom_data); |
| 1330 | res = vmul_f32(vpadd_f32(sum_data, sum_data), scale_v); |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | const float32x2_t max_data = vmax_f32(top_data, bottom_data); |
| 1335 | res = vpmax_f32(max_data, max_data); |
| 1336 | } |
| 1337 | final_res = vget_lane_f32(res, 0); |
| 1338 | |
| 1339 | // Calculate square-root in case of l2 pooling |
| 1340 | if(pooling_type == PoolingType::L2) |
| 1341 | { |
| 1342 | final_res = sqrt(final_res); |
| 1343 | } |
| 1344 | |
| 1345 | // Store result |
| 1346 | *(reinterpret_cast<float *>(output.ptr())) = final_res; |
| 1347 | }, |
| 1348 | input, output); |
| 1349 | } |
| 1350 | |
| 1351 | void NEPoolingLayerKernel::pooling3_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
| 1352 | { |
| 1353 | Iterator input(_input, window_input); |
| 1354 | Iterator output(_output, window); |
| 1355 | |
| 1356 | constexpr const int pool_size = 3; |
| 1357 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1358 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1359 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1360 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1361 | int pool_stride_x = 0; |
| 1362 | int pool_stride_y = 0; |
| 1363 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 1364 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 1365 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
| 1366 | |
| 1367 | const uint8_t *const input_top_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top))); |
| 1368 | const uint8_t *const input_middle_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 1)); |
| 1369 | const uint8_t *const input_bottom_ptr = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + 2)); |
| 1370 | |
| 1371 | execute_window_loop(window, [&](const Coordinates & id) |
| 1372 | { |
| 1373 | float32x4_t top_data = vld1q_f32(reinterpret_cast<const float *>(input_top_ptr + input.offset())); |
| 1374 | float32x4_t middle_data = vld1q_f32(reinterpret_cast<const float *>(input_middle_ptr + input.offset())); |
| 1375 | float32x4_t bottom_data = vld1q_f32(reinterpret_cast<const float *>(input_bottom_ptr + input.offset())); |
| 1376 | float32x2_t res = {}; |
| 1377 | float final_res = 0; |
| 1378 | |
| 1379 | // Get power of 2 in case of l2 pooling |
| 1380 | if(pooling_type == PoolingType::L2) |
| 1381 | { |
| 1382 | top_data = vmulq_f32(top_data, top_data); |
| 1383 | middle_data = vmulq_f32(middle_data, middle_data); |
| 1384 | bottom_data = vmulq_f32(bottom_data, bottom_data); |
| 1385 | } |
| 1386 | |
| 1387 | if(pooling_type != PoolingType::MAX) |
| 1388 | { |
| 1389 | // Calculate scale |
| 1390 | float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
| 1391 | const float32x2_t scale_v = vdup_n_f32(scale); |
| 1392 | |
| 1393 | // Perform pooling |
| 1394 | const float32x4_t sum_data = vaddq_f32(vaddq_f32(top_data, bottom_data), middle_data); |
| 1395 | res = vpadd_f32(vget_high_f32(vsetq_lane_f32(0.f, sum_data, 3)), vget_low_f32(sum_data)); |
| 1396 | res = vmul_f32(vpadd_f32(res, res), scale_v); |
| 1397 | } |
| 1398 | else |
| 1399 | { |
| 1400 | const float32x4_t max_data = vmaxq_f32(vmaxq_f32(top_data, bottom_data), middle_data); |
| 1401 | res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data, 3)), vget_low_f32(max_data)); |
| 1402 | res = vpmax_f32(res, res); |
| 1403 | } |
| 1404 | final_res = vget_lane_f32(res, 0); |
| 1405 | |
| 1406 | // Calculate square-root in case of l2 pooling |
| 1407 | if(pooling_type == PoolingType::L2) |
| 1408 | { |
| 1409 | final_res = sqrt(final_res); |
| 1410 | } |
| 1411 | |
| 1412 | // Store result |
| 1413 | *(reinterpret_cast<float *>(output.ptr())) = final_res; |
| 1414 | }, |
| 1415 | input, output); |
| 1416 | } |
| 1417 | |
| 1418 | void NEPoolingLayerKernel::pooling7_f32_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
| 1419 | { |
| 1420 | Iterator input(_input, window_input); |
| 1421 | Iterator output(_output, window); |
| 1422 | |
| 1423 | constexpr const int pool_size = 7; |
| 1424 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1425 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1426 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1427 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1428 | int pool_stride_x = 0; |
| 1429 | int pool_stride_y = 0; |
| 1430 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 1431 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 1432 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
| 1433 | |
| 1434 | std::array<const uint8_t *, pool_size> input_ptrs{ {} }; |
| 1435 | for(int i = 0; i < pool_size; ++i) |
| 1436 | { |
| 1437 | input_ptrs[i] = _input->ptr_to_element(Coordinates(-static_cast<int>(pool_pad_left), -static_cast<int>(pool_pad_top) + i)); |
| 1438 | } |
| 1439 | |
| 1440 | execute_window_loop(window, [&](const Coordinates & id) |
| 1441 | { |
| 1442 | float32x2_t res = {}; |
| 1443 | float final_res = 0.f; |
| 1444 | if(pooling_type != PoolingType::MAX) |
| 1445 | { |
| 1446 | // Calculate scale |
| 1447 | float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size, pool_size, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
| 1448 | const float32x2_t scale_v = vdup_n_f32(scale); |
| 1449 | |
| 1450 | // Perform pooling |
| 1451 | float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset())); |
| 1452 | // Get power of 2 in case of l2 pooling |
| 1453 | if(pooling_type == PoolingType::L2) |
| 1454 | { |
| 1455 | data.val[0] = vmulq_f32(data.val[0], data.val[0]); |
| 1456 | data.val[1] = vmulq_f32(data.val[1], data.val[1]); |
| 1457 | } |
| 1458 | float32x4_t sum_data = vaddq_f32(data.val[0], vsetq_lane_f32(0.f, data.val[1], 3)); |
| 1459 | for(int i = 1; i < pool_size; ++i) |
| 1460 | { |
| 1461 | data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset())); |
| 1462 | // Get power of 2 in case of l2 pooling |
| 1463 | if(pooling_type == PoolingType::L2) |
| 1464 | { |
| 1465 | data.val[0] = vmulq_f32(data.val[0], data.val[0]); |
| 1466 | data.val[1] = vmulq_f32(data.val[1], data.val[1]); |
| 1467 | } |
| 1468 | sum_data = vaddq_f32(sum_data, data.val[0]); |
| 1469 | sum_data = vaddq_f32(sum_data, vsetq_lane_f32(0.f, data.val[1], 3)); |
| 1470 | } |
| 1471 | res = vpadd_f32(vget_high_f32(sum_data), vget_low_f32(sum_data)); |
| 1472 | res = vmul_f32(vpadd_f32(res, res), scale_v); |
| 1473 | } |
| 1474 | else |
| 1475 | { |
| 1476 | float32x4x2_t max_data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[0] + input.offset())); |
| 1477 | for(int i = 1; i < pool_size; ++i) |
| 1478 | { |
| 1479 | const float32x4x2_t data = vld2q_f32(reinterpret_cast<const float *>(input_ptrs[i] + input.offset())); |
| 1480 | max_data = vmax2q_f32(max_data, data); |
| 1481 | } |
| 1482 | res = vpmax_f32(vget_high_f32(vsetq_lane_f32(-std::numeric_limits<float>::max(), max_data.val[1], 3)), vget_low_f32(max_data.val[1])); |
| 1483 | res = vpmax_f32(res, vpmax_f32(vget_high_f32(max_data.val[0]), vget_low_f32(max_data.val[0]))); |
| 1484 | res = vpmax_f32(res, res); |
| 1485 | } |
| 1486 | final_res = vget_lane_f32(res, 0); |
| 1487 | |
| 1488 | // Calculate square-root in case of l2 pooling |
| 1489 | if(pooling_type == PoolingType::L2) |
| 1490 | { |
| 1491 | final_res = sqrt(final_res); |
| 1492 | } |
| 1493 | |
| 1494 | // Store result |
| 1495 | *(reinterpret_cast<float *>(output.ptr())) = final_res; |
| 1496 | }, |
| 1497 | input, output); |
| 1498 | } |
| 1499 | |
| 1500 | void NEPoolingLayerKernel::poolingMxN_f32_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1501 | { |
| 1502 | Iterator input(_input, window_input); |
| 1503 | Iterator output(_output, window); |
| 1504 | |
| 1505 | const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width; |
| 1506 | const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height; |
| 1507 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1508 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1509 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1510 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1511 | int pool_stride_x = 0; |
| 1512 | int pool_stride_y = 0; |
| 1513 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 1514 | const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right); |
| 1515 | const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom); |
| 1516 | |
| 1517 | float32x4_t vres; |
| 1518 | |
| 1519 | execute_window_loop(window, [&](const Coordinates & id) |
| 1520 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1521 | const int idx_width = id.y() * pool_stride_x; |
| 1522 | const int idx_height = id.z() * pool_stride_y; |
| 1523 | const int pool_limit_y = pool_pad_top - idx_height; |
| 1524 | const int pool_limit_x = pool_pad_left - idx_width; |
| 1525 | |
| 1526 | const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y); |
| 1527 | const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y); |
| 1528 | const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x); |
| 1529 | const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x); |
| 1530 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1531 | if(pooling_type != PoolingType::MAX) |
| 1532 | { |
| 1533 | // Calculate scale |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1534 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, |
| 1535 | pool_stride_y); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1536 | const float32x4_t scale_v = vdupq_n_f32(scale); |
| 1537 | |
| 1538 | // Perform pooling |
| 1539 | vres = vdupq_n_f32(0.0f); |
| 1540 | |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1541 | for(int y = pool_start_y; y < pool_end_y; ++y) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1542 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1543 | for(int x = pool_start_x; x < pool_end_x; ++x) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1544 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1545 | const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int> |
| 1546 | (_input->info()->strides_in_bytes().z()))); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Get power of 2 in case of l2 pooling and accumulate |
| 1549 | if(pooling_type == PoolingType::L2) |
| 1550 | { |
| 1551 | vres = vmlaq_f32(vres, data, data); |
| 1552 | } |
| 1553 | else |
| 1554 | { |
| 1555 | vres = vaddq_f32(vres, data); |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | // Divide by scale |
| 1560 | vres = vmulq_f32(vres, scale_v); |
| 1561 | } |
| 1562 | else |
| 1563 | { |
| 1564 | vres = vdupq_n_f32(std::numeric_limits<float>::lowest()); |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1565 | for(int y = pool_start_y; y < pool_end_y; ++y) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1566 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1567 | for(int x = pool_start_x; x < pool_end_x; ++x) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1568 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1569 | const float32x4_t data = vld1q_f32(reinterpret_cast<const float *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int> |
| 1570 | (_input->info()->strides_in_bytes().z()))); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1571 | vres = vmaxq_f32(vres, data); |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | // Calculate square-root in case of l2 pooling |
| 1577 | if(pooling_type == PoolingType::L2) |
| 1578 | { |
Georgios Pinitas | 027ce5b | 2018-11-08 18:55:36 +0000 | [diff] [blame] | 1579 | vres = vmulq_f32(vres, vinvsqrtq_f32(vres)); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | // Store result |
| 1583 | vst1q_f32(reinterpret_cast<float *>(output.ptr()), vres); |
| 1584 | }, |
| 1585 | input, output); |
| 1586 | } |
| 1587 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1588 | void NEPoolingLayerKernel::poolingMxN_qasymm8_nchw(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1589 | { |
| 1590 | Iterator input(_input, window_input); |
| 1591 | Iterator output(_output, window); |
| 1592 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1593 | const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().x() : _pool_info.pool_size().width; |
| 1594 | const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().height; |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 1595 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1596 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1597 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1598 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1599 | int pool_stride_x = 0; |
| 1600 | int pool_stride_y = 0; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1601 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
Michalis Spyrou | bd0e612 | 2018-01-23 09:52:16 +0000 | [diff] [blame] | 1602 | const int upper_bound_w = _input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_right); |
| 1603 | const int upper_bound_h = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_bottom); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1604 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1605 | const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform(); |
| 1606 | const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform(); |
| 1607 | |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1608 | execute_window_loop(window, [&](const Coordinates & id) |
| 1609 | { |
| 1610 | uint8_t res = 0; |
| 1611 | |
| 1612 | if(pooling_type != PoolingType::MAX) |
| 1613 | { |
| 1614 | uint32x4_t vres = vdupq_n_u32(0); |
| 1615 | uint32_t sres = 0; |
| 1616 | |
| 1617 | // Calculate scale |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1618 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NCHW, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, pool_stride_y); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1619 | |
| 1620 | // Perform pooling |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1621 | for(int y = 0; y < pool_size_y; ++y) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1622 | { |
| 1623 | int x = 0; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1624 | for(; x <= (pool_size_x - 8); x += 8) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1625 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1626 | const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1627 | (_input->info()->strides_in_bytes().y()))); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1628 | |
| 1629 | const uint16x8_t data_u16 = vmovl_u8(data); |
| 1630 | vres = vaddq_u32(vres, vaddl_u16(vget_high_u16(data_u16), vget_low_u16(data_u16))); |
| 1631 | } |
| 1632 | |
| 1633 | // Leftover for loop |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1634 | for(; x < pool_size_x; ++x) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1635 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1636 | uint8_t data = *(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1637 | (_input->info()->strides_in_bytes().y()))); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1638 | sres += data; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | // Reduction |
| 1643 | const auto tmp = vpadd_u32(vget_high_u32(vres), vget_low_u32(vres)); |
| 1644 | sres += vget_lane_u32(tmp, 0) + vget_lane_u32(tmp, 1); |
| 1645 | |
| 1646 | // Divide by scale |
| 1647 | res = static_cast<uint8_t>(support::cpp11::round(sres * scale)); |
| 1648 | } |
| 1649 | else |
| 1650 | { |
| 1651 | uint8x8_t vres = vdup_n_u8(0); |
| 1652 | res = 0; |
| 1653 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1654 | for(int y = 0; y < pool_size_y; ++y) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1655 | { |
| 1656 | int x = 0; |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1657 | for(; x <= (pool_size_x - 8); x += 8) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1658 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1659 | const uint8x8_t data = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1660 | (_input->info()->strides_in_bytes().y()))); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1661 | vres = vmax_u8(vres, data); |
| 1662 | } |
| 1663 | |
| 1664 | // Leftover for loop |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1665 | for(; x < pool_size_x; ++x) |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1666 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1667 | const uint8_t data = *(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().x()) + (y - pool_pad_top) * static_cast<int> |
| 1668 | (_input->info()->strides_in_bytes().y()))); |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1669 | res = std::max(res, data); |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | // Reduce max |
| 1674 | vres = vpmax_u8(vres, vres); |
| 1675 | vres = vpmax_u8(vres, vres); |
| 1676 | vres = vpmax_u8(vres, vres); |
| 1677 | |
| 1678 | // Get max value |
| 1679 | res = std::max(res, vget_lane_u8(vres, 0)); |
| 1680 | } |
| 1681 | |
| 1682 | // Store result |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1683 | res = (input_qinfo != output_qinfo) ? quantize_qasymm8(dequantize_qasymm8(res, input_qinfo), output_qinfo) : res; |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1684 | *(reinterpret_cast<uint8_t *>(output.ptr())) = res; |
| 1685 | }, |
| 1686 | input, output); |
| 1687 | } |
| 1688 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1689 | void NEPoolingLayerKernel::poolingMxN_qasymm8_nhwc(const Window &window_input, const Window &window, PoolingType pooling_type, bool exclude_padding) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1690 | { |
| 1691 | Iterator input(_input, window_input); |
| 1692 | Iterator output(_output, window); |
| 1693 | |
| 1694 | const int pool_size_x = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().y() : _pool_info.pool_size().width; |
| 1695 | const int pool_size_y = _pool_info.is_global_pooling() ? _input->info()->tensor_shape().z() : _pool_info.pool_size().height; |
| 1696 | const int pool_pad_right = _pool_info.pad_stride_info().pad_right(); |
| 1697 | const int pool_pad_top = _pool_info.pad_stride_info().pad_top(); |
| 1698 | const int pool_pad_left = _pool_info.pad_stride_info().pad_left(); |
| 1699 | const int pool_pad_bottom = _pool_info.pad_stride_info().pad_bottom(); |
| 1700 | int pool_stride_x = 0; |
| 1701 | int pool_stride_y = 0; |
| 1702 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 1703 | const int upper_bound_w = _input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_right); |
| 1704 | const int upper_bound_h = _input->info()->dimension(2) + (exclude_padding ? 0 : pool_pad_bottom); |
| 1705 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1706 | const float32x4_t half_scale_v = vdupq_n_f32(0.5f); |
| 1707 | const UniformQuantizationInfo input_qinfo = _input->info()->quantization_info().uniform(); |
| 1708 | const UniformQuantizationInfo output_qinfo = _output->info()->quantization_info().uniform(); |
Georgios Pinitas | 283fc60 | 2018-11-09 10:46:43 +0000 | [diff] [blame] | 1709 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1710 | execute_window_loop(window, [&](const Coordinates & id) |
| 1711 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1712 | const int idx_width = id.y() * pool_stride_x; |
| 1713 | const int idx_height = id.z() * pool_stride_y; |
| 1714 | const int pool_limit_y = pool_pad_top - idx_height; |
| 1715 | const int pool_limit_x = pool_pad_left - idx_width; |
| 1716 | |
| 1717 | const int pool_start_y = std::max(0, window_input.z().start() + pool_limit_y); |
| 1718 | const int pool_end_y = std::min(pool_size_y, window_input.z().end() + pool_limit_y); |
| 1719 | const int pool_start_x = std::max(0, window_input.y().start() + pool_limit_x); |
| 1720 | const int pool_end_x = std::min(pool_size_x, window_input.y().end() + pool_limit_x); |
| 1721 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1722 | if(pooling_type != PoolingType::MAX) |
| 1723 | { |
| 1724 | uint32x4_t vres1 = vdupq_n_u32(0); |
| 1725 | uint32x4_t vres2 = vdupq_n_u32(0); |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1726 | uint32x4_t vres3 = vdupq_n_u32(0); |
| 1727 | uint32x4_t vres4 = vdupq_n_u32(0); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1728 | |
| 1729 | // Calculate scale |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1730 | const float scale = calculate_avg_scale(exclude_padding, DataLayout::NHWC, id, pool_size_x, pool_size_y, upper_bound_w, upper_bound_h, pool_pad_left, pool_pad_top, pool_stride_x, |
| 1731 | pool_stride_y); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1732 | const float32x4_t scale_v = vdupq_n_f32(scale); |
| 1733 | |
| 1734 | // Perform pooling |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1735 | for(int y = pool_start_y; y < pool_end_y; ++y) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1736 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1737 | for(int x = pool_start_x; x < pool_end_x; ++x) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1738 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1739 | const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int> |
| 1740 | (_input->info()->strides_in_bytes().z()))); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1741 | |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1742 | const uint16x8_t data_u16 = vmovl_u8(vget_low_u8(data)); |
| 1743 | const uint16x8_t data2_u16 = vmovl_u8(vget_high_u8(data)); |
| 1744 | vres1 = vaddq_u32(vres1, vmovl_u16(vget_low_u16(data_u16))); |
| 1745 | vres2 = vaddq_u32(vres2, vmovl_u16(vget_high_u16(data_u16))); |
| 1746 | vres3 = vaddq_u32(vres3, vmovl_u16(vget_low_u16(data2_u16))); |
| 1747 | vres4 = vaddq_u32(vres4, vmovl_u16(vget_high_u16(data2_u16))); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1748 | } |
| 1749 | } |
Georgios Pinitas | 283fc60 | 2018-11-09 10:46:43 +0000 | [diff] [blame] | 1750 | // Divide by scale and add 0.5f to round to nearest instead of rounding towards zero |
| 1751 | vres1 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres1), scale_v)); |
| 1752 | vres2 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres2), scale_v)); |
| 1753 | vres3 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres3), scale_v)); |
| 1754 | vres4 = vcvtq_u32_f32(vmlaq_f32(half_scale_v, vcvtq_f32_u32(vres4), scale_v)); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1755 | |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1756 | uint8x8_t res1 = vmovn_u16(vcombine_u16(vmovn_u32(vres1), vmovn_u32(vres2))); |
| 1757 | uint8x8_t res2 = vmovn_u16(vcombine_u16(vmovn_u32(vres3), vmovn_u32(vres4))); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 1758 | if(input_qinfo != output_qinfo) |
| 1759 | { |
| 1760 | const auto requantized_output = vquantize(vdequantize(vcombine_u8(res1, res2), input_qinfo), output_qinfo); |
| 1761 | res1 = vget_low_u8(requantized_output); |
| 1762 | res2 = vget_high_u8(requantized_output); |
| 1763 | } |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1764 | |
| 1765 | // Store result |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1766 | vst1_u8(output.ptr(), res1); |
| 1767 | vst1_u8(output.ptr() + 8, res2); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1768 | } |
| 1769 | else |
| 1770 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1771 | uint8x16_t vres = vdupq_n_u8(0); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1772 | |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1773 | for(int y = pool_start_y; y < pool_end_y; ++y) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1774 | { |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1775 | for(int x = pool_start_x; x < pool_end_x; ++x) |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1776 | { |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame^] | 1777 | const uint8x16_t data = vld1q_u8(reinterpret_cast<const uint8_t *>(input.ptr() + (x - pool_pad_left) * static_cast<int>(_input->info()->strides_in_bytes().y()) + (y - pool_pad_top) * static_cast<int> |
| 1778 | (_input->info()->strides_in_bytes().z()))); |
Michalis Spyrou | ced2557 | 2018-10-01 16:26:20 +0100 | [diff] [blame] | 1779 | vres = vmaxq_u8(vres, data); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | // Store result |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 1784 | vst1q_u8(output.ptr(), (input_qinfo != output_qinfo) ? vquantize(vdequantize(vres, input_qinfo), output_qinfo) : vres); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1785 | } |
| 1786 | }, |
| 1787 | input, output); |
| 1788 | } |
| 1789 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 1790 | Status NEPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info) |
| 1791 | { |
| 1792 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 1793 | |
| 1794 | unsigned int pooled_w = 0; |
| 1795 | unsigned int pooled_h = 0; |
| 1796 | unsigned int num_elems_processed_per_iteration = 0; |
| 1797 | BorderSize border_size(0); |
| 1798 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1799 | const bool is_global_pooling = pool_info.is_global_pooling(); |
| 1800 | unsigned int pool_size_x = 0; |
| 1801 | unsigned int pool_size_y = 0; |
| 1802 | |
| 1803 | // Get data layout |
| 1804 | const DataLayout data_layout = input->data_layout(); |
| 1805 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 1806 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 1807 | |
| 1808 | pool_size_x = is_global_pooling ? input->dimension(idx_width) : pool_info.pool_size().width; |
| 1809 | pool_size_y = is_global_pooling ? input->dimension(idx_height) : pool_info.pool_size().height; |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 1810 | |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1811 | // Validate pool info before calling scaled_dimensions |
| 1812 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_pool_info(pool_size_x, pool_size_y)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Check output dimensions |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1815 | std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(idx_width), |
| 1816 | input->dimension(idx_height), |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1817 | pool_size_x, |
| 1818 | pool_size_y, |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 1819 | pool_info.pad_stride_info()); |
| 1820 | |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 1821 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info, pooled_w, pooled_h)); |
Isabella Gottardi | 7567f5f | 2018-01-30 15:26:00 +0000 | [diff] [blame] | 1822 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), pool_info, num_elems_processed_per_iteration, border_size, pooled_w, pooled_h, |
| 1823 | pool_size_x, pool_size_y) |
| 1824 | .first); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 1825 | |
| 1826 | return Status{}; |
| 1827 | } |
| 1828 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 1829 | void NEPoolingLayerKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1830 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 1831 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1832 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 1833 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 1834 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 1835 | |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1836 | const unsigned int pool_stride_x = _pool_info.pad_stride_info().stride().first; |
| 1837 | const unsigned int pool_stride_y = _pool_info.pad_stride_info().stride().second; |
| 1838 | const unsigned int pool_size = _pool_info.pool_size().width; |
| 1839 | const bool exclude_padding = _pool_info.exclude_padding(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1840 | |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1841 | Window window_input(window); |
| 1842 | if(_input->info()->data_layout() == DataLayout::NCHW) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1843 | { |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1844 | // Set step for input in x and y direction for the input |
| 1845 | unsigned int window_x_inc = 0; |
| 1846 | switch(_input->info()->data_type()) |
Pablo Tello | 0c34fe2 | 2017-06-26 17:17:42 +0100 | [diff] [blame] | 1847 | { |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1848 | case DataType::QASYMM8: |
| 1849 | { |
| 1850 | window_x_inc = pool_stride_x; |
| 1851 | if((pool_size == 2 || pool_size == 3) && pool_stride_x < 3) |
| 1852 | { |
| 1853 | window_x_inc = (pool_stride_x == 2) ? _num_elems_processed_per_iteration * 2 : _num_elems_processed_per_iteration; |
| 1854 | } |
| 1855 | break; |
| 1856 | } |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1857 | |
Georgios Pinitas | 13d96e0 | 2018-08-23 11:20:23 +0100 | [diff] [blame] | 1858 | case DataType::F16: |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1859 | case DataType::F32: |
| 1860 | { |
| 1861 | window_x_inc = pool_stride_x; |
| 1862 | break; |
| 1863 | } |
| 1864 | default: |
| 1865 | { |
| 1866 | ARM_COMPUTE_ERROR("Not supported"); |
| 1867 | } |
Georgios Pinitas | 5518671 | 2018-01-08 17:37:12 +0000 | [diff] [blame] | 1868 | } |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1869 | window_input.set(Window::DimX, Window::Dimension(window.x().start() * pool_stride_x, window.x().end() * pool_stride_x, window_x_inc)); |
| 1870 | window_input.set(Window::DimY, Window::Dimension(window.y().start() * pool_stride_y, window.y().end() * pool_stride_y, pool_stride_y)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1871 | } |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1872 | else |
| 1873 | { |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 1874 | window_input.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), _num_elems_processed_per_iteration)); |
Michalis Spyrou | 57dac84 | 2018-03-01 16:03:50 +0000 | [diff] [blame] | 1875 | window_input.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x)); |
| 1876 | window_input.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y)); |
| 1877 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1878 | |
| 1879 | // Run function |
Pablo Tello | 77e6c55 | 2018-12-04 15:33:49 +0000 | [diff] [blame] | 1880 | (this->*_func)(window_input, window, _pool_info.pool_type(), exclude_padding); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1881 | } |