Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLPoolingLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/CL/ICLTensor.h" |
| 32 | #include "arm_compute/core/CL/OpenCL.h" |
| 33 | #include "arm_compute/core/Helpers.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Window.h" |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | #include <set> |
| 40 | #include <string> |
| 41 | #include <tuple> |
| 42 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame^] | 43 | namespace arm_compute |
| 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 |
| 50 | using CLPoolingConfig = std::pair<unsigned int, BorderSize>; //num_elems_processed_per_iteration, border_size |
| 51 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 52 | void auto_init(const ITensorInfo *input, ITensorInfo *output, PoolingLayerInfo pool_info) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 53 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 54 | TensorShape out_shape = compute_pool_shape(*input, pool_info); |
| 55 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(out_shape)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 58 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info) |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame^] | 62 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(input->data_type()) && pool_info.pool_type() == PoolingType::L2), |
| 64 | "Unsupported combination of parameters!"); |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 65 | |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 66 | // Checks performed when output is configured |
| 67 | if(output->total_size() != 0) |
| 68 | { |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 71 | TensorInfo out_info(TensorInfo(compute_pool_shape(*input, pool_info), 1, output->data_type())); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info); |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 75 | return Status{}; |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 78 | std::tuple<Status, Window, CLPoolingConfig> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &pool_info) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 79 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 80 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 81 | |
| 82 | // Get data layout |
| 83 | const DataLayout data_layout = input->data_layout(); |
| 84 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 85 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 86 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 87 | int pool_stride_x = 0; |
| 88 | int pool_stride_y = 0; |
| 89 | unsigned int pooled_w = 0; |
| 90 | unsigned int pooled_h = 0; |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 91 | int pool_size_x = pool_info.is_global_pooling() ? input->dimension(idx_width) : pool_info.pool_size().width; |
| 92 | int pool_size_y = pool_info.is_global_pooling() ? input->dimension(idx_height) : pool_info.pool_size().height; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 93 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 94 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 95 | const int pool_pad_right = pad_stride_info.pad_right(); |
| 96 | const int pool_pad_top = pad_stride_info.pad_top(); |
| 97 | const int pool_pad_left = pad_stride_info.pad_left(); |
| 98 | const int pool_pad_bottom = pad_stride_info.pad_bottom(); |
| 99 | BorderSize border_size = BorderSize(pool_pad_top, pool_pad_right, pool_pad_bottom, pool_pad_left); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 100 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 101 | auto_init(input, output, pool_info); |
| 102 | pooled_w = output->tensor_shape()[idx_width]; |
| 103 | pooled_h = output->tensor_shape()[idx_height]; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 104 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 105 | const DataType data_type = input->data_type(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 106 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 107 | const int input_width = input->dimension(idx_width); |
| 108 | const int input_height = input->dimension(idx_height); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 109 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 110 | unsigned int num_elems_processed_per_iteration = 0; |
| 111 | bool window_changed = false; |
| 112 | Window win{}; |
| 113 | switch(data_layout) |
| 114 | { |
| 115 | case DataLayout::NCHW: |
| 116 | { |
| 117 | // Change the number of elements processed per iteration |
| 118 | // for pooling 3x3 with stride less equal than 3 |
| 119 | const bool can_optimize = (pool_size_x == 3) && (pool_size_y == 3) && (pool_stride_x <= 3) && !is_data_type_quantized(data_type); |
| 120 | num_elems_processed_per_iteration = can_optimize ? 4 : 1; |
| 121 | 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] | 122 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 123 | // Number of iterations in X dimension |
| 124 | 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] | 125 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 126 | // Upper limit for the number of right/bottom border elements that are accessed |
| 127 | const int upper_bound_w = ((num_iterations_x - 1) * num_elems_processed_per_iteration * pool_stride_x - pool_pad_left + num_elems_read_per_iteration) - input_width; |
| 128 | const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_top + pool_size_y) - input_height; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 129 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 130 | border_size.right = std::max(upper_bound_w, pool_pad_right); |
| 131 | border_size.bottom = std::max(upper_bound_h, pool_pad_bottom); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 132 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 133 | win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 134 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 135 | AccessWindowRectangle input_access(input, -pool_pad_left, -pool_pad_top, num_elems_read_per_iteration, pool_size_y, |
Giorgio Arena | 3c520c5 | 2018-05-01 11:47:24 +0100 | [diff] [blame] | 136 | pool_stride_x, pool_stride_y); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 137 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 138 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 139 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 140 | break; |
| 141 | } |
| 142 | case DataLayout::NHWC: |
| 143 | { |
| 144 | num_elems_processed_per_iteration = 8; |
| 145 | win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 146 | |
Georgios Pinitas | e222055 | 2018-07-20 13:23:44 +0100 | [diff] [blame] | 147 | AccessWindowStatic input_access(input, |
| 148 | 0, -1, |
| 149 | ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 150 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 151 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 152 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 153 | break; |
| 154 | } |
| 155 | default: |
| 156 | ARM_COMPUTE_ERROR("Not implemented"); |
| 157 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 158 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 159 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 160 | return std::make_tuple(err, win, CLPoolingConfig(num_elems_processed_per_iteration, border_size)); |
| 161 | } |
| 162 | } // namespace |
| 163 | |
| 164 | CLPoolingLayerKernel::CLPoolingLayerKernel() |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 165 | : _input(nullptr), _output(nullptr), _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] | 166 | { |
| 167 | } |
| 168 | |
| 169 | BorderSize CLPoolingLayerKernel::border_size() const |
| 170 | { |
| 171 | return _border_size; |
| 172 | } |
| 173 | |
| 174 | void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info) |
| 175 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 176 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 177 | |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 178 | // Set instance variables |
| 179 | _input = input; |
| 180 | _output = output; |
| 181 | _pool_info = pool_info; |
| 182 | _data_layout = input->info()->data_layout(); |
| 183 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 184 | int pool_stride_x = 0; |
| 185 | int pool_stride_y = 0; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 186 | const PoolingType pool_type = pool_info.pool_type(); |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 187 | const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 188 | const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 189 | const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 190 | const int pool_size_x = pool_info.is_global_pooling() ? input->info()->dimension(idx_width) : pool_info.pool_size().width; |
| 191 | const int pool_size_y = pool_info.is_global_pooling() ? input->info()->dimension(idx_height) : pool_info.pool_size().height; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 192 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); |
| 193 | const bool exclude_padding = pool_info.exclude_padding(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 194 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 195 | const int pool_pad_top = pad_stride_info.pad_top(); |
| 196 | const int pool_pad_left = pad_stride_info.pad_left(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 197 | |
Pablo Tello | a52e4cf | 2019-04-01 14:55:18 +0100 | [diff] [blame] | 198 | // Set build options |
| 199 | CLBuildOptions build_opts; |
| 200 | |
| 201 | if(is_data_type_quantized_asymmetric(input->info()->data_type()) && input->info()->quantization_info() != output->info()->quantization_info()) |
| 202 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 203 | const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform(); |
| 204 | const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); |
| 205 | |
| 206 | build_opts.add_option("-DOFFSET_IN1=" + float_to_string_with_full_precision(iq_info.offset)); |
| 207 | build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset)); |
| 208 | build_opts.add_option("-DSCALE_IN1=" + float_to_string_with_full_precision(iq_info.scale)); |
| 209 | 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] | 210 | } |
| 211 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 212 | // Check output dimensions |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 213 | auto_init(input->info(), output->info(), pool_info); |
Giorgio Arena | f6a43c5 | 2017-12-01 12:16:25 +0000 | [diff] [blame] | 214 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), pool_info)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 215 | |
Georgios Pinitas | 17812ba | 2018-06-04 19:27:13 +0100 | [diff] [blame] | 216 | const DataType data_type = input->info()->data_type(); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 217 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 218 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 219 | build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 220 | 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] | 221 | build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y)); |
| 222 | build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left)); |
| 223 | build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top)); |
| 224 | build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x)); |
| 225 | 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] | 226 | |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame^] | 227 | // Set the initial value for the pooling operation accordingly with the data type |
| 228 | if(pool_type == PoolingType::MAX) |
| 229 | { |
| 230 | if(is_data_type_quantized(data_type)) |
| 231 | { |
| 232 | PixelValue type_min{}; |
| 233 | std::tie(type_min, std::ignore) = get_min_max(data_type); |
| 234 | build_opts.add_option("-DINITIAL_VALUE=" + support::cpp11::to_string(type_min.get<int32_t>())); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | build_opts.add_option("-DINITIAL_VALUE=" + float_to_string_with_full_precision(std::numeric_limits<float>::lowest())); |
| 239 | } |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | // Pool AVG and Pool L2 initial value |
| 244 | build_opts.add_option("-DINITIAL_VALUE=0"); |
| 245 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 246 | |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame] | 247 | const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision(); |
| 248 | const auto use_wider_accumulator = use_fp_mixed_precision && (pool_type != PoolingType::MAX); |
| 249 | const auto acc_data_type = get_cl_type_from_data_type(use_wider_accumulator ? DataType::F32 : data_type); |
| 250 | build_opts.add_option("-DACC_DATA_TYPE=" + acc_data_type); |
| 251 | build_opts.add_option_if(use_wider_accumulator, "-DFP_MIXED_PRECISION"); |
| 252 | |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 253 | // Create kernel |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 254 | switch(_data_layout) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 255 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 256 | case DataLayout::NCHW: |
| 257 | { |
| 258 | build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width) + (exclude_padding ? 0 : pool_pad_left))); |
| 259 | build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height) + (exclude_padding ? 0 : pool_pad_top))); |
| 260 | if(pool_type != PoolingType::MAX) |
| 261 | { |
| 262 | build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING"); |
| 263 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 264 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 265 | if((pool_size_x == 3) && (pool_size_y == 3) && !is_data_type_quantized_asymmetric(data_type)) |
| 266 | { |
| 267 | // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where |
| 268 | // each thread computes 4 output elements |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 269 | 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] | 270 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 271 | std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_") |
| 272 | + support::cpp11::to_string(pool_size_x); |
| 273 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 274 | } |
| 275 | else // Run general case |
| 276 | { |
| 277 | std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nchw" : "pooling_layer_MxN_nchw"; |
| 278 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 279 | } |
| 280 | break; |
| 281 | } |
| 282 | case DataLayout::NHWC: |
| 283 | { |
| 284 | build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING"); |
| 285 | build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(idx_width))); |
| 286 | build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(idx_height))); |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 287 | build_opts.add_option_if(output->info()->tensor_shape().total_size_upper(3) > 1, |
| 288 | "-DDST_DEPTH=" + support::cpp11::to_string(output->info()->dimension(idx_height))); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 289 | std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_MxN_quantized_nhwc" : "pooling_layer_MxN_nhwc"; |
| 290 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 291 | break; |
| 292 | } |
| 293 | default: |
| 294 | ARM_COMPUTE_ERROR("Not implemented"); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Configure kernel window |
| 298 | auto win_config = validate_and_configure_window(input->info(), output->info(), pool_info); |
| 299 | |
| 300 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 301 | ICLKernel::configure_internal(std::get<1>(win_config)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 302 | |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 303 | if(_data_layout == DataLayout::NCHW) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 304 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 305 | CLPoolingConfig pooling_config = std::get<2>(win_config); |
| 306 | _num_elems_processed_per_iteration = pooling_config.first; |
| 307 | _border_size = pooling_config.second; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 308 | } |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 309 | else |
| 310 | { |
| 311 | _border_size = BorderSize(1, 0, 0, 0); |
| 312 | _num_elems_processed_per_iteration = 8; |
| 313 | } |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 314 | |
| 315 | // Set config_id for enabling LWS tuning |
| 316 | _config_id = "pooling_layer_"; |
| 317 | _config_id += lower_string(string_from_data_type(data_type)); |
| 318 | _config_id += "_"; |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 319 | _config_id += lower_string(string_from_data_layout(_data_layout)); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 320 | _config_id += "_"; |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 321 | _config_id += support::cpp11::to_string(output->info()->dimension(idx_width)); |
| 322 | _config_id += "_"; |
| 323 | _config_id += support::cpp11::to_string(output->info()->dimension(idx_height)); |
| 324 | _config_id += "_"; |
| 325 | _config_id += support::cpp11::to_string(output->info()->dimension(idx_channel)); |
Giorgio Arena | 00b93f5 | 2018-06-28 17:18:50 +0100 | [diff] [blame] | 326 | _config_id += "_"; |
| 327 | _config_id += lower_string(string_from_data_layout(input->info()->data_layout())); |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 330 | Status CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info) |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 331 | { |
| 332 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, pool_info)); |
| 333 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), pool_info))); |
| 334 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 335 | return Status{}; |
Giorgio Arena | 9f26b3e | 2017-11-28 14:35:00 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 338 | void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 339 | { |
| 340 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 341 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 342 | |
Georgios Pinitas | 1599787 | 2018-02-19 13:58:22 +0000 | [diff] [blame] | 343 | unsigned int pool_stride_x = 0; |
| 344 | unsigned int pool_stride_y = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 345 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 346 | |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 347 | // Collapse window |
| 348 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 349 | |
Georgios Pinitas | 14d9d98 | 2019-12-13 12:33:09 +0000 | [diff] [blame] | 350 | switch(_data_layout) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 351 | { |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 352 | case DataLayout::NCHW: |
| 353 | { |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 354 | Window slice = window_collapsed.first_slice_window_3D(); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 355 | do |
| 356 | { |
| 357 | // Upsample input by pool size |
| 358 | Window in_slice(slice); |
| 359 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - _pool_info.pad_stride_info().pad_left(), |
| 360 | (in_slice.x().end() - _pool_info.pad_stride_info().pad_left()) * pool_stride_x, |
| 361 | pool_stride_x * _num_elems_processed_per_iteration)); |
| 362 | in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - _pool_info.pad_stride_info().pad_top(), |
| 363 | (in_slice.y().end() - _pool_info.pad_stride_info().pad_top()) * pool_stride_y, |
| 364 | pool_stride_y)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 365 | |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 366 | // Set inputs |
| 367 | unsigned int idx = 0; |
| 368 | add_3D_tensor_argument(idx, _input, in_slice); |
| 369 | add_3D_tensor_argument(idx, _output, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 370 | enqueue(queue, *this, slice, lws_hint()); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 371 | } |
| 372 | while(window_collapsed.slide_window_slice_3D(slice)); |
| 373 | break; |
| 374 | } |
| 375 | case DataLayout::NHWC: |
| 376 | { |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 377 | const size_t total_batches = _output->info()->tensor_shape().total_size_upper(3); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 378 | |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 379 | Window slice = window_collapsed.first_slice_window_4D(); |
| 380 | Window in_slice = window_collapsed.first_slice_window_4D(); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 381 | in_slice.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration)); |
| 382 | in_slice.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), pool_stride_x)); |
| 383 | in_slice.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), pool_stride_y)); |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 384 | in_slice.set(3, Window::Dimension(0, total_batches, 1)); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 385 | do |
| 386 | { |
| 387 | // Set inputs |
| 388 | unsigned int idx = 0; |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 389 | add_4D_tensor_argument(idx, _input, in_slice); |
| 390 | add_4D_tensor_argument(idx, _output, slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 391 | enqueue(queue, *this, slice, lws_hint()); |
Michalis Spyrou | e74b201 | 2018-04-18 09:49:16 +0100 | [diff] [blame] | 392 | } |
Georgios Pinitas | 89d7173 | 2018-10-29 20:07:15 +0000 | [diff] [blame] | 393 | 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] | 394 | break; |
| 395 | } |
| 396 | default: |
| 397 | ARM_COMPUTE_ERROR("Not implemented"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 398 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 399 | } |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame^] | 400 | } // namespace arm_compute |