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