Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | */ |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 24 | #include "src/core/gpu/cl/kernels/ClPool2dKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Utils.h" |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CL/CLValidate.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 36 | #include "support/Cast.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 41 | namespace opencl |
| 42 | { |
| 43 | namespace kernels |
| 44 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 45 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 47 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 49 | // Internal window config info |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 50 | using ClPoolingConfig = std::pair<unsigned int, BorderSize>; //num_elems_processed_per_iteration, border_size |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 51 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 52 | void auto_init(const ITensorInfo *src, ITensorInfo *dst, ITensorInfo *indices, PoolingLayerInfo pool_info) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 53 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 54 | TensorShape out_shape = compute_pool_shape(*src, pool_info); |
| 55 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 56 | if(indices) |
| 57 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 58 | auto_init_if_empty(*indices, src->clone()->set_tensor_shape(out_shape).set_data_type(DataType::U32)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 59 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 62 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices) |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 63 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(src->data_type()) && pool_info.pool_type == PoolingType::L2), |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 68 | "Unsupported combination of parameters!"); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 69 | |
Freddie Liardet | afcbb8f | 2021-05-04 12:41:16 +0100 | [diff] [blame] | 70 | const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout; |
| 71 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 72 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 73 | const bool is_global_pooling = pool_info.is_global_pooling; |
| 74 | unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width; |
| 75 | unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height; |
| 76 | int output_width = 0; |
| 77 | int output_height = 0; |
| 78 | std::tie(output_width, output_height) = scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height], |
| 79 | pool_size_x, pool_size_y, pool_info.pad_stride_info); |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1), "Calculated output dimension size is invalid"); |
| 81 | |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 82 | // Check indices |
| 83 | if(indices) |
| 84 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 85 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_info.pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method"); |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_info.pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2"); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 88 | |
| 89 | if(indices->total_size() != 0) |
| 90 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 91 | TensorInfo idx_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, DataType::U32)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 92 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &idx_info); |
| 93 | } |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 94 | } |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 95 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 96 | // Checks performed when dst is configured |
| 97 | if(dst->total_size() != 0) |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 98 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 99 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst); |
| 101 | TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type())); |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info); |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 105 | return Status{}; |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 108 | std::tuple<Status, Window, ClPoolingConfig> validate_and_configure_window(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices = nullptr) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 109 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 111 | |
| 112 | // Get data layout |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 113 | const DataLayout data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout; |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 114 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 115 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 116 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 117 | int pool_stride_x = 0; |
| 118 | int pool_stride_y = 0; |
| 119 | unsigned int pooled_w = 0; |
| 120 | unsigned int pooled_h = 0; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 121 | int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width; |
| 122 | int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 123 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 124 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 125 | const int pool_pad_right = pad_stride_info.pad_right(); |
| 126 | const int pool_pad_top = pad_stride_info.pad_top(); |
| 127 | const int pool_pad_left = pad_stride_info.pad_left(); |
| 128 | const int pool_pad_bottom = pad_stride_info.pad_bottom(); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 129 | BorderSize border_size = BorderSize(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 130 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 131 | auto_init(src, dst, indices, pool_info); |
| 132 | pooled_w = dst->tensor_shape()[idx_width]; |
| 133 | pooled_h = dst->tensor_shape()[idx_height]; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 134 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 135 | const DataType data_type = src->data_type(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 136 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 137 | const int src_width = src->dimension(idx_width); |
| 138 | const int src_height = src->dimension(idx_height); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 139 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 140 | unsigned int num_elems_processed_per_iteration = 0; |
| 141 | bool window_changed = false; |
| 142 | Window win{}; |
| 143 | switch(data_layout) |
| 144 | { |
| 145 | case DataLayout::NCHW: |
| 146 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 147 | // Initialize border size |
| 148 | border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 149 | // Change the number of elements processed per iteration |
| 150 | // for pooling 3x3 with stride less equal than 3 |
| 151 | const bool can_optimize = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3) && !is_data_type_quantized(data_type); |
| 152 | num_elems_processed_per_iteration = can_optimize ? 4 : 1; |
| 153 | const unsigned int num_elems_read_per_iteration = (num_elems_processed_per_iteration - 1) * pool_stride_x + pool_size_x; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 154 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 155 | // Number of iterations in X dimension |
| 156 | const int num_iterations_x = (pooled_w + num_elems_processed_per_iteration - 1) / num_elems_processed_per_iteration; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 157 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 158 | // Upper limit for the number of right/bottom border elements that are accessed |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 159 | 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) - src_width; |
| 160 | const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - src_height; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 161 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 162 | border_size.right = std::max(upper_bound_w, pool_pad_right); |
| 163 | border_size.bottom = std::max(upper_bound_h, pool_pad_bottom); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 164 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 165 | win = calculate_max_window(*dst, Steps(num_elems_processed_per_iteration)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 166 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 167 | AccessWindowRectangle src_access(src, -pool_pad_left, -pool_pad_top, num_elems_read_per_iteration, pool_size_y, |
| 168 | pool_stride_x, pool_stride_y); |
| 169 | AccessWindowHorizontal dst_access(dst, 0, num_elems_processed_per_iteration); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 170 | |
| 171 | // Update indices window |
| 172 | if(indices) |
| 173 | { |
| 174 | AccessWindowHorizontal indices_access(indices, 0, num_elems_processed_per_iteration); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 175 | window_changed = update_window_and_padding(win, src_access, dst_access, indices_access); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 176 | indices_access.set_valid_region(win, ValidRegion(Coordinates(), indices->tensor_shape())); |
| 177 | } |
| 178 | else |
| 179 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 180 | window_changed = update_window_and_padding(win, src_access, dst_access); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 183 | dst_access.set_valid_region(win, ValidRegion(Coordinates(), dst->tensor_shape())); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | case DataLayout::NHWC: |
| 187 | { |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame] | 188 | const size_t vec_size = dst->data_type() == DataType::F32 ? 2 : 4; |
| 189 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 190 | // Initialize border size |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 191 | border_size = BorderSize(); |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame] | 192 | num_elems_processed_per_iteration = adjust_vec_size(vec_size, dst->dimension(0)); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 193 | win = calculate_max_window(*dst, Steps(num_elems_processed_per_iteration)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | default: |
| 197 | ARM_COMPUTE_ERROR("Not implemented"); |
| 198 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 199 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 200 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 201 | return std::make_tuple(err, win, ClPoolingConfig(num_elems_processed_per_iteration, border_size)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 202 | } |
| 203 | } // namespace |
| 204 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 205 | ClPool2dKernel::ClPool2dKernel() |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 206 | : _pool_info(), _data_layout(DataLayout::UNKNOWN), _border_size(0), _num_elems_processed_per_iteration(1) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 207 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame^] | 208 | _type = CLKernelType::POOL; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 211 | BorderSize ClPool2dKernel::border_size() const |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 212 | { |
| 213 | return _border_size; |
| 214 | } |
| 215 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 216 | void ClPool2dKernel::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 217 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 218 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 219 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 220 | auto padding_info = get_padding_info({ src, dst, indices }); |
SiCong Li | 04a0706 | 2020-11-17 14:09:01 +0000 | [diff] [blame] | 221 | |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 222 | // Set instance variables |
morgolock | cc1f6c9 | 2020-03-24 09:26:48 +0000 | [diff] [blame] | 223 | _pool_info = pool_info; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 224 | _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 225 | int pool_stride_x = 0; |
| 226 | int pool_stride_y = 0; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 227 | const PoolingType pool_type = pool_info.pool_type; |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 228 | const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 229 | const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 230 | const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 231 | const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 232 | const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width; |
| 233 | const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 234 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info; |
| 235 | const bool exclude_padding = pool_info.exclude_padding; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 236 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 237 | const int pool_pad_top = pad_stride_info.pad_top(); |
| 238 | const int pool_pad_left = pad_stride_info.pad_left(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 239 | |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 240 | // Set build options |
| 241 | CLBuildOptions build_opts; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 242 | const DataType data_type = src->data_type(); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 243 | |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 244 | // Configure kernel window |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 245 | auto win_config = validate_and_configure_window(src, dst, pool_info, indices); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 246 | |
| 247 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
| 248 | ICLKernel::configure_internal(std::get<1>(win_config)); |
| 249 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 250 | ClPoolingConfig pooling_config = std::get<2>(win_config); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 251 | _num_elems_processed_per_iteration = pooling_config.first; |
| 252 | _border_size = pooling_config.second; |
| 253 | |
| 254 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration)); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 255 | |
| 256 | // Tensor paddings are used to calculate the indicies for MAX pooling |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 257 | if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && is_data_type_float(data_type)) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 258 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 259 | build_opts.add_option("-DPAD_TENSOR_LEFT=" + support::cpp11::to_string(src->padding().left)); |
| 260 | build_opts.add_option("-DPAD_TENSOR_RIGHT=" + support::cpp11::to_string(src->padding().right)); |
| 261 | build_opts.add_option("-DPAD_TENSOR_TOP=" + support::cpp11::to_string(src->padding().top)); |
| 262 | build_opts.add_option("-DPAD_TENSOR_BOTTOM=" + support::cpp11::to_string(src->padding().bottom)); |
| 263 | build_opts.add_option("-DTENSOR_CHANNEL=" + support::cpp11::to_string(src->dimension(idx_channel))); |
| 264 | build_opts.add_option("-DTENSOR_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width))); |
| 265 | build_opts.add_option("-DTENSOR_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height))); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 268 | if(is_data_type_quantized_asymmetric(data_type) && src->quantization_info() != dst->quantization_info()) |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 269 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 270 | const UniformQuantizationInfo iq_info = src->quantization_info().uniform(); |
| 271 | const UniformQuantizationInfo oq_info = dst->quantization_info().uniform(); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 272 | |
| 273 | build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset)); |
| 274 | build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset)); |
| 275 | build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale)); |
| 276 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale)); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 279 | // Check dst dimensions |
| 280 | auto_init(src, dst, indices, pool_info); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 281 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 282 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 283 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 284 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 285 | build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 286 | build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 287 | build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y)); |
| 288 | build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left)); |
| 289 | build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top)); |
| 290 | build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x)); |
| 291 | build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y)); |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 292 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 293 | // Set the initial value for the pooling operation accordingly with the data type |
| 294 | if(pool_type == PoolingType::MAX) |
| 295 | { |
| 296 | if(is_data_type_quantized(data_type)) |
| 297 | { |
| 298 | PixelValue type_min{}; |
| 299 | std::tie(type_min, std::ignore) = get_min_max(data_type); |
| 300 | build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>())); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | build_opts.add_option("-DINITIAL_VALUE=" + float_to_string_with_full_precision(std::numeric_limits<float>::lowest())); |
| 305 | } |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | // Pool AVG and Pool L2 initial value |
| 310 | build_opts.add_option("-DINITIAL_VALUE=0"); |
| 311 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 312 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 313 | build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left))); |
| 314 | build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top))); |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 315 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 316 | // Create kernel |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 317 | switch(_data_layout) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 318 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 319 | case DataLayout::NCHW: |
| 320 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 321 | const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision; |
| 322 | const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX); |
| 323 | const auto acc_data_type = get_cl_type_from_data_type(use_wider_accumulator ? DataType::F32 : data_type); |
| 324 | build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type); |
| 325 | build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION"); |
| 326 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 327 | if(pool_type != PoolingType::MAX) |
| 328 | { |
| 329 | build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING"); |
| 330 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 331 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 332 | if((pool_size_x == 3) && (pool_size_y == 3) && !is_data_type_quantized_asymmetric(data_type)) |
| 333 | { |
| 334 | // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 335 | // each thread computes 4 dst elements |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 336 | const bool is_pool3x3_stride_le3 = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 337 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 338 | std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_") |
| 339 | + support::cpp11::to_string(pool_size_x); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 340 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 341 | } |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 342 | else if(pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && is_data_type_float(data_type)) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 343 | { |
| 344 | // For max pooling with pool2x2, store indicies which will be used in max unpooling |
| 345 | if(data_type == DataType::F32) |
| 346 | { |
| 347 | std::string kernel_name = "pooling_layer_2_nchw_indices_fp32"; |
| 348 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
| 349 | } |
| 350 | else if(data_type == DataType::F16) |
| 351 | { |
| 352 | std::string kernel_name = "pooling_layer_2_nchw_indices_fp16"; |
| 353 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
| 354 | } |
| 355 | } |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 356 | else // Run general case |
| 357 | { |
| 358 | std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nchw" : "pooling_layer_MxN_nchw"; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 359 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 360 | } |
| 361 | break; |
| 362 | } |
| 363 | case DataLayout::NHWC: |
| 364 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 365 | // Floating point mixed precision is support on F16 only |
| 366 | const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision && pool_type != PoolingType::MAX; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 367 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 368 | // Wider accumulation is required to avoid accuracy loss |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 369 | // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation) |
| 370 | // Cast 2: Quantized (int8/uint8 src data and int32 accumulation ) |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 371 | DataType acc_data_type = data_type; |
| 372 | |
| 373 | if(use_fp_mixed_precision) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 374 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 375 | acc_data_type = DataType::F32; |
| 376 | } |
| 377 | else if(is_data_type_quantized(data_type) && pool_type != PoolingType::MAX) |
| 378 | { |
| 379 | acc_data_type = DataType::S32; |
| 380 | } |
| 381 | |
| 382 | build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type)); |
| 383 | build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION"); |
| 384 | build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING"); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 385 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width))); |
| 386 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height))); |
| 387 | build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height))); |
| 388 | build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel))); |
| 389 | build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size))); |
| 390 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 391 | if(pool_info.pool_size == Size2D(2, 2) && is_data_type_float(data_type)) |
| 392 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 393 | build_opts.add_option_if(indices != nullptr && pool_type == PoolingType::MAX, "-DEXTRACT_MAX_INDEX"); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 394 | |
| 395 | std::string kernel_name = "pooling_layer_2x2_nhwc"; |
| 396 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 397 | } |
| 398 | else |
| 399 | { |
| 400 | std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nhwc" : "pooling_layer_MxN_nhwc"; |
| 401 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
| 402 | } |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 403 | break; |
| 404 | } |
| 405 | default: |
| 406 | ARM_COMPUTE_ERROR("Not implemented"); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 409 | // Set config_id for enabling LWS tuning |
| 410 | _config_id = "pooling_layer_"; |
| 411 | _config_id += lower_string(string_from_data_type(data_type)); |
| 412 | _config_id += "_"; |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 413 | _config_id += lower_string(string_from_data_layout(_data_layout)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 414 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 415 | _config_id += support::cpp11::to_string(dst->dimension(idx_width)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 416 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 417 | _config_id += support::cpp11::to_string(dst->dimension(idx_height)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 418 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 419 | _config_id += support::cpp11::to_string(dst->dimension(idx_channel)); |
Giorgio Arena | 00b93f5 | 2018-06-28 17:18:50 +0100 | [diff] [blame] | 420 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 421 | _config_id += lower_string(string_from_data_layout(src->data_layout())); |
SiCong Li | 04a0706 | 2020-11-17 14:09:01 +0000 | [diff] [blame] | 422 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 423 | ARM_COMPUTE_ERROR_ON(src->data_layout() == DataLayout::NHWC && has_padding_changed(padding_info)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 426 | Status ClPool2dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 427 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 428 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, indices)); |
| 429 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(src->clone().get(), dst->clone().get(), pool_info))); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 430 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 431 | return Status{}; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 434 | void ClPool2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 435 | { |
| 436 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 437 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 438 | |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 439 | unsigned int pool_stride_x = 0; |
| 440 | unsigned int pool_stride_y = 0; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 441 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info.stride(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 442 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 443 | const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 444 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0)); |
| 445 | auto indices = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_1)); |
| 446 | |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 447 | // Collapse window |
| 448 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 449 | |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 450 | switch(_data_layout) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 451 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 452 | case DataLayout::NCHW: |
| 453 | { |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 454 | Window slice = window_collapsed.first_slice_window_3D(); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 455 | do |
| 456 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 457 | // Upsample src by pool size |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 458 | Window in_slice(slice); |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 459 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - _pool_info.pad_stride_info.pad_left(), |
| 460 | (in_slice.x().end() - _pool_info.pad_stride_info.pad_left()) * pool_stride_x, |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 461 | pool_stride_x * _num_elems_processed_per_iteration)); |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 462 | in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - _pool_info.pad_stride_info.pad_top(), |
| 463 | (in_slice.y().end() - _pool_info.pad_stride_info.pad_top()) * pool_stride_y, |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 464 | pool_stride_y)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 465 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 466 | // Set srcs |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 467 | unsigned int idx = 0; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 468 | add_3D_tensor_argument(idx, src, in_slice); |
| 469 | add_3D_tensor_argument(idx, dst, slice); |
| 470 | if(indices && is_data_type_float(src->info()->data_type()) && (_pool_info.pool_size == Size2D(2, 2))) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 471 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 472 | add_3D_tensor_argument(idx, indices, slice); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 473 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 474 | enqueue(queue, *this, slice, lws_hint()); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 475 | } |
| 476 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 477 | break; |
| 478 | } |
| 479 | case DataLayout::NHWC: |
| 480 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 481 | const size_t batch_size = dst->info()->tensor_shape().total_size_upper(3); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 482 | |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 483 | Window slice = window_collapsed.first_slice_window_4D(); |
| 484 | Window in_slice = window_collapsed.first_slice_window_4D(); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 485 | in_slice.set(Window::DimX, Window::Dimension(0, src->info()->dimension(0), _num_elems_processed_per_iteration)); |
| 486 | in_slice.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x)); |
| 487 | in_slice.set(Window::DimZ, Window::Dimension(0, src->info()->dimension(2), pool_stride_y)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 488 | in_slice.set(3, Window::Dimension(0, batch_size, 1)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 489 | do |
| 490 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 491 | // Set srcs |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 492 | unsigned int idx = 0; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 493 | add_4D_tensor_argument(idx, src, in_slice); |
| 494 | add_4D_tensor_argument(idx, dst, slice); |
| 495 | if(indices && is_data_type_float(src->info()->data_type()) && (_pool_info.pool_type == PoolingType::MAX) && (_pool_info.pool_size == Size2D(2, 2))) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 496 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 497 | add_4D_tensor_argument(idx, indices, slice); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 498 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 499 | enqueue(queue, *this, slice, lws_hint()); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 500 | } |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 501 | while(window.slide_window_slice_4D(slice) && window.slide_window_slice_4D(in_slice)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 502 | break; |
| 503 | } |
| 504 | default: |
| 505 | ARM_COMPUTE_ERROR("Not implemented"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 506 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 507 | } |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 508 | } // namespace kernels |
| 509 | } // namespace opencl |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 510 | } // namespace arm_compute |