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