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