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