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