Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Adnan AlSinan | 227db8d | 2023-02-14 14:24:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/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/ICLTensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/CL/CLValidate.h" |
| 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 35 | #include "support/Cast.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 39 | namespace opencl |
| 40 | { |
| 41 | namespace kernels |
| 42 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 43 | using namespace arm_compute::misc::shape_calculator; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 45 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | Status validate_arguments(const ITensorInfo *src, |
| 48 | const ITensorInfo *dst, |
| 49 | const PoolingLayerInfo &pool_info, |
| 50 | const ITensorInfo *indices) |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 51 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 55 | DataType::F16, DataType::F32); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 57 | (is_data_type_quantized_asymmetric(src->data_type()) && pool_info.pool_type == PoolingType::L2), |
| 58 | "Unsupported combination of parameters!"); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 59 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | const auto data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout; |
| 61 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 62 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 63 | const bool is_global_pooling = pool_info.is_global_pooling; |
| 64 | unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width; |
| 65 | unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height; |
| 66 | int output_width = 0; |
| 67 | int output_height = 0; |
SiCongLi | c4270cf | 2021-12-22 11:22:40 +0000 | [diff] [blame] | 68 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_region_entirely_outside_input(pool_info), |
| 70 | "Pooling region that is entirely outside input tensor is unsupported"); |
SiCongLi | c4270cf | 2021-12-22 11:22:40 +0000 | [diff] [blame] | 71 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | std::tie(output_width, output_height) = |
| 73 | scaled_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height], pool_size_x, |
| 74 | pool_size_y, pool_info.pad_stride_info); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1), |
| 76 | "Calculated output dimension size is invalid"); |
Freddie Liardet | afcbb8f | 2021-05-04 12:41:16 +0100 | [diff] [blame] | 77 | |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 78 | // Check indices |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | if (indices) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 80 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 81 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 82 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_info.pool_type != PoolingType::MAX, |
| 83 | "Pooling indices only supported for MAX pooling method"); |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_info.pool_size != Size2D(2, 2)), |
| 85 | "Pooling indices only supported for pool size 2x2"); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 86 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | if (indices->total_size() != 0) |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 88 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 89 | 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] | 90 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(indices, &idx_info); |
| 91 | } |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 92 | } |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 93 | |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 94 | // Checks performed when dst is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 95 | if (dst->total_size() != 0) |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 96 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 97 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst); |
| 99 | TensorInfo out_info(TensorInfo(compute_pool_shape(*src, pool_info), 1, dst->data_type())); |
| 100 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info); |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 103 | return Status{}; |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 104 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 105 | } // namespace |
| 106 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 107 | ClPool2dKernel::ClPool2dKernel() |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 108 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 109 | _type = CLKernelType::POOL; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 112 | void ClPool2dKernel::configure(const ClCompileContext &compile_context, |
| 113 | ITensorInfo *src, |
| 114 | ITensorInfo *dst, |
| 115 | const PoolingLayerInfo &pool_info, |
| 116 | ITensorInfo *indices) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 117 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 118 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 119 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info, indices)); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 120 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | auto padding_info = get_padding_info({src, dst, indices}); |
SiCong Li | 04a0706 | 2020-11-17 14:09:01 +0000 | [diff] [blame] | 122 | |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 123 | // Auto init if empty |
| 124 | TensorShape out_shape = compute_pool_shape(*src, pool_info); |
| 125 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | if (indices) |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 127 | { |
| 128 | auto_init_if_empty(*indices, src->clone()->set_tensor_shape(out_shape).set_data_type(DataType::U32)); |
| 129 | } |
| 130 | |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 131 | // Set instance variables |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | _pool_info = pool_info; |
| 133 | _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout; |
| 134 | _num_elems_processed_per_iteration = |
| 135 | (_data_layout == DataLayout::NCHW) ? 1 : ((dst->data_type() == DataType::F32) ? 2 : 4); |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 136 | _num_elems_processed_per_iteration = adjust_vec_size(_num_elems_processed_per_iteration, dst->dimension(0)); |
| 137 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 138 | int pool_stride_x = 0; |
| 139 | int pool_stride_y = 0; |
| 140 | const PoolingType pool_type = pool_info.pool_type; |
| 141 | const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 142 | const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 143 | const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL); |
| 144 | const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES); |
| 145 | const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width; |
| 146 | const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height; |
| 147 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info; |
| 148 | const bool exclude_padding = pool_info.exclude_padding; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 149 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 150 | const int pool_pad_top = pad_stride_info.pad_top(); |
| 151 | const int pool_pad_left = pad_stride_info.pad_left(); |
| 152 | const DataType data_type = src->data_type(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 153 | |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 154 | // Set build options |
| 155 | CLBuildOptions build_opts; |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 156 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 157 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 158 | build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 159 | 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] | 160 | build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y)); |
| 161 | build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left)); |
| 162 | build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top)); |
| 163 | build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x)); |
| 164 | build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y)); |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 165 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width))); |
| 166 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height))); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 167 | build_opts.add_option("-DMAX_WIDTH=" + |
| 168 | support::cpp11::to_string(src->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left))); |
| 169 | build_opts.add_option("-DMAX_HEIGHT=" + |
| 170 | support::cpp11::to_string(src->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top))); |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 171 | |
| 172 | // Tensor paddings are used to calculate the indicies for MAX pooling |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 173 | if (pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && |
| 174 | is_data_type_float(data_type)) |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 175 | { |
| 176 | build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(src->tensor_shape().total_size_lower(3))); |
| 177 | } |
| 178 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 179 | if (is_data_type_quantized_asymmetric(data_type)) |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 180 | { |
| 181 | build_opts.add_option("-DQUANTIZED"); |
| 182 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 183 | if (src->quantization_info() != dst->quantization_info()) |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 184 | { |
| 185 | const UniformQuantizationInfo iq_info = src->quantization_info().uniform(); |
| 186 | const UniformQuantizationInfo oq_info = dst->quantization_info().uniform(); |
| 187 | |
| 188 | build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset)); |
| 189 | build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset)); |
| 190 | build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale)); |
| 191 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale)); |
| 192 | } |
| 193 | } |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 194 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 195 | // Set the initial value for the pooling operation accordingly with the data type |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 196 | if (pool_type == PoolingType::MAX) |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 197 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | if (is_data_type_quantized(data_type)) |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 199 | { |
| 200 | PixelValue type_min{}; |
| 201 | std::tie(type_min, std::ignore) = get_min_max(data_type); |
| 202 | build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>())); |
| 203 | } |
| 204 | else |
| 205 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 206 | std::string initial_value = pool_info.use_inf_as_limit |
| 207 | ? "(-INFINITY)" |
| 208 | : float_to_string_with_full_precision(std::numeric_limits<float>::lowest()); |
Adnan AlSinan | 227db8d | 2023-02-14 14:24:09 +0000 | [diff] [blame] | 209 | build_opts.add_option("-DINITIAL_VALUE=" + initial_value); |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | // Pool AVG and Pool L2 initial value |
| 215 | build_opts.add_option("-DINITIAL_VALUE=0"); |
| 216 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 217 | |
| 218 | // Create kernel |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 219 | switch (_data_layout) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 220 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 221 | case DataLayout::NCHW: |
| 222 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 223 | const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision; |
| 224 | const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 225 | const auto acc_data_type = get_cl_type_from_data_type( |
| 226 | use_wider_accumulator ? DataType::F32 |
| 227 | : (is_data_type_quantized(data_type) ? DataType::S32 : data_type)); |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 228 | build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type); |
| 229 | build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION"); |
| 230 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 231 | if (pool_type != PoolingType::MAX) |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 232 | { |
| 233 | build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING"); |
| 234 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 235 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 236 | if (pool_info.pool_size == Size2D(2, 2) && pool_type == PoolingType::MAX && indices && |
| 237 | is_data_type_float(data_type)) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 238 | { |
| 239 | // For max pooling with pool2x2, store indicies which will be used in max unpooling |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 240 | std::string kernel_name = "pooling_layer_2_nchw_indices"; |
| 241 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 242 | } |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 243 | else // Run general case |
| 244 | { |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 245 | std::string kernel_name = "pooling_layer_MxN_nchw"; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 246 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 247 | } |
| 248 | break; |
| 249 | } |
| 250 | case DataLayout::NHWC: |
| 251 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 252 | // Floating point mixed precision is support on F16 only |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 253 | const auto use_fp_mixed_precision = |
| 254 | (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] | 255 | |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 256 | // Wider accumulation is required to avoid accuracy loss |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 257 | // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation) |
| 258 | // Cast 2: Quantized (int8/uint8 src data and int32 accumulation ) |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 259 | DataType acc_data_type = data_type; |
| 260 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 261 | if (use_fp_mixed_precision) |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 262 | { |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 263 | acc_data_type = DataType::F32; |
| 264 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 265 | else if (is_data_type_quantized(data_type) && pool_type != PoolingType::MAX) |
Gian Marco Iodice | 7333e1f | 2020-10-08 10:25:49 +0100 | [diff] [blame] | 266 | { |
| 267 | acc_data_type = DataType::S32; |
| 268 | } |
| 269 | |
| 270 | build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type)); |
| 271 | build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION"); |
| 272 | build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING"); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 273 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width))); |
| 274 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height))); |
| 275 | build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height))); |
| 276 | build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel))); |
| 277 | build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size))); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 278 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + |
| 279 | support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration)); |
| 280 | if (pool_info.pool_size == Size2D(2, 2) && is_data_type_float(data_type)) |
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 | 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] | 283 | |
| 284 | std::string kernel_name = "pooling_layer_2x2_nhwc"; |
| 285 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 289 | std::string kernel_name = is_data_type_quantized_asymmetric(data_type) |
| 290 | ? "pooling_layer_MxN_quantized_nhwc" |
| 291 | : "pooling_layer_MxN_nhwc"; |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 292 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
| 293 | } |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | default: |
| 297 | ARM_COMPUTE_ERROR("Not implemented"); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 300 | // Configure kernel window |
| 301 | Window win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration)); |
| 302 | ICLKernel::configure_internal(win); |
| 303 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 304 | // Set config_id for enabling LWS tuning |
| 305 | _config_id = "pooling_layer_"; |
| 306 | _config_id += lower_string(string_from_data_type(data_type)); |
| 307 | _config_id += "_"; |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 308 | _config_id += lower_string(string_from_data_layout(_data_layout)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 309 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 310 | _config_id += support::cpp11::to_string(dst->dimension(idx_width)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 311 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 312 | _config_id += support::cpp11::to_string(dst->dimension(idx_height)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 313 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 314 | _config_id += support::cpp11::to_string(dst->dimension(idx_channel)); |
Giorgio Arena | 00b93f5 | 2018-06-28 17:18:50 +0100 | [diff] [blame] | 315 | _config_id += "_"; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 316 | _config_id += lower_string(string_from_data_layout(src->data_layout())); |
SiCong Li | 04a0706 | 2020-11-17 14:09:01 +0000 | [diff] [blame] | 317 | |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 318 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 321 | Status ClPool2dKernel::validate(const ITensorInfo *src, |
| 322 | const ITensorInfo *dst, |
| 323 | const PoolingLayerInfo &pool_info, |
| 324 | const ITensorInfo *indices) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 325 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 326 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info, indices)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 327 | return Status{}; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 330 | void ClPool2dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 331 | { |
| 332 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 333 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 334 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 335 | unsigned int pool_stride_x = 0; |
| 336 | unsigned int pool_stride_y = 0; |
Sang-Hoon Park | 0cb3da6 | 2020-01-15 12:39:56 +0000 | [diff] [blame] | 337 | 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] | 338 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 339 | const auto src = |
| 340 | utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 341 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0)); |
| 342 | auto indices = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_1)); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 343 | |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 344 | // Collapse window |
| 345 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 346 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 347 | switch (_data_layout) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 349 | case DataLayout::NCHW: |
| 350 | { |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 351 | Window slice = window_collapsed.first_slice_window_3D(); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 352 | do |
| 353 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 354 | // Set srcs |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 355 | unsigned int idx = 0; |
Giorgio Arena | 8fce496 | 2021-09-01 14:05:00 +0100 | [diff] [blame] | 356 | add_3D_tensor_argument(idx, src, slice); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 357 | add_3D_tensor_argument(idx, dst, slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 358 | 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] | 359 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 360 | add_3D_tensor_argument(idx, indices, slice); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 361 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 362 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 363 | } while (window_collapsed.slide_window_slice_3D(slice)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | case DataLayout::NHWC: |
| 367 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 368 | 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] | 369 | |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 370 | Window slice = window_collapsed.first_slice_window_4D(); |
| 371 | Window in_slice = window_collapsed.first_slice_window_4D(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 372 | in_slice.set(Window::DimX, |
| 373 | Window::Dimension(0, src->info()->dimension(0), _num_elems_processed_per_iteration)); |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 374 | in_slice.set(Window::DimY, Window::Dimension(0, src->info()->dimension(1), pool_stride_x)); |
| 375 | 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] | 376 | in_slice.set(3, Window::Dimension(0, batch_size, 1)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 377 | do |
| 378 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 379 | // Set srcs |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 380 | unsigned int idx = 0; |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 381 | add_4D_tensor_argument(idx, src, in_slice); |
| 382 | add_4D_tensor_argument(idx, dst, slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 383 | if (indices && is_data_type_float(src->info()->data_type()) && |
| 384 | (_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] | 385 | { |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 386 | add_4D_tensor_argument(idx, indices, slice); |
Sheri Zhang | 801bbcb | 2020-08-03 20:11:56 +0100 | [diff] [blame] | 387 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 388 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 389 | } 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] | 390 | break; |
| 391 | } |
| 392 | default: |
| 393 | ARM_COMPUTE_ERROR("Not implemented"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 394 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 395 | } |
Michele Di Giorgio | e131466 | 2021-02-01 17:09:32 +0000 | [diff] [blame] | 396 | } // namespace kernels |
| 397 | } // namespace opencl |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 398 | } // namespace arm_compute |