Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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" |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLTensor.h" |
| 31 | #include "arm_compute/core/CL/OpenCL.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Utils.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/core/Window.h" |
| 37 | |
| 38 | #include <set> |
| 39 | #include <string> |
| 40 | #include <tuple> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | CLPoolingLayerKernel::CLPoolingLayerKernel() |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 45 | : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0), _num_elems_processed_per_iteration(1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | BorderSize CLPoolingLayerKernel::border_size() const |
| 50 | { |
| 51 | return _border_size; |
| 52 | } |
| 53 | |
| 54 | void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info) |
| 55 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 56 | int pool_pad_x = 0; |
| 57 | int pool_pad_y = 0; |
| 58 | int pool_stride_x = 0; |
| 59 | int pool_stride_y = 0; |
| 60 | unsigned int pooled_w = 0; |
| 61 | unsigned int pooled_h = 0; |
| 62 | const PoolingType pool_type = pool_info.pool_type(); |
| 63 | int pool_size = pool_info.pool_size(); |
| 64 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); |
| 65 | const bool exclude_padding = pool_info.exclude_padding(); |
| 66 | const bool is_global_pooling = pool_info.is_global_pooling(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad(); |
| 68 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
| 69 | |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 72 | // Update pool size in case of global pooling |
| 73 | pool_size = is_global_pooling ? input->info()->dimension(0) : pool_size; |
| 74 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | // Check output dimensions |
| 76 | std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0), |
| 77 | input->info()->dimension(1), |
| 78 | pool_size, |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 79 | pool_size, |
| 80 | pool_info.pad_stride_info()); |
Georgios Pinitas | 1dad50e | 2017-07-03 17:51:34 +0100 | [diff] [blame] | 81 | |
| 82 | // Output auto initialization if not yet initialized |
| 83 | { |
| 84 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 85 | output_shape.set(0, pooled_w); |
| 86 | output_shape.set(1, pooled_h); |
| 87 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 88 | auto_init_if_empty(*output->info(), |
| 89 | output_shape, |
| 90 | 1, |
| 91 | input->info()->data_type(), |
| 92 | input->info()->fixed_point_position(), |
| 93 | input->info()->quantization_info()); |
Georgios Pinitas | 1dad50e | 2017-07-03 17:51:34 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Georgios Pinitas | f9d3a0a | 2017-11-03 19:01:44 +0000 | [diff] [blame] | 96 | ARM_COMPUTE_ERROR_THROW_ON(CLPoolingLayerKernel::validate(input->info(), output->info(), pool_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 98 | const int input_width = input->info()->dimension(0); |
| 99 | const int input_height = input->info()->dimension(1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | |
| 101 | // Set instance variables |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 102 | _input = input; |
| 103 | _output = output; |
| 104 | _pool_info = pool_info; |
| 105 | _border_size = BorderSize(pool_pad_y, pool_pad_x); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 107 | const GPUTarget gpu_target = get_arch_from_target(get_target()); |
| 108 | const DataType data_type = input->info()->data_type(); |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame] | 109 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 110 | // Set build options |
| 111 | CLBuildOptions build_opts; |
| 112 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 113 | build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type)); |
| 114 | build_opts.add_option_if(is_data_type_fixed_point(data_type), |
| 115 | "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 116 | build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x)); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 117 | if(pool_type != PoolingType::MAX) |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 118 | { |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 119 | build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING"); |
| 120 | build_opts.add_option("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0) + (exclude_padding ? 0 : pool_pad_x))); |
| 121 | build_opts.add_option("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1) + (exclude_padding ? 0 : pool_pad_y))); |
| 122 | build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y)); |
| 123 | build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_x)); |
| 124 | build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_y)); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 125 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | |
| 127 | // Create kernel |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 128 | if((pool_size == 3) && !is_data_type_quantized_asymmetric(data_type)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | { |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 130 | // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where |
| 131 | // each thread computes 4 output elements |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 132 | const bool is_pool3x3_stride_le3 = (pool_size == 3) && (pool_stride_x <= 3) && !is_data_type_fixed_point(data_type); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 133 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 134 | int num_elems_read_per_iteration = pool_size; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 135 | if(is_pool3x3_stride_le3) |
| 136 | { |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 137 | // Change the number of elements processed and the number of elements read per iteration |
| 138 | // for pooling 3x3 with stride less equal than 3 |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 139 | _num_elems_processed_per_iteration = 4; |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 140 | num_elems_read_per_iteration = pool_size * (pool_stride_x + 1); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 143 | const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elems_read_per_iteration) - input_width; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 144 | const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height; |
| 145 | |
| 146 | _border_size.right = std::max(upper_bound_w, pool_pad_x); |
| 147 | _border_size.bottom = std::max(upper_bound_h, pool_pad_y); |
| 148 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 149 | std::string kernel_name = ((is_pool3x3_stride_le3) ? "pooling_layer_optimized_" : "pooling_layer_") |
| 150 | + support::cpp11::to_string(pool_size); |
| 151 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 152 | } |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 153 | else // Run general case |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 154 | { |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 155 | const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + pool_size) - input_width; |
| 156 | const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height; |
| 157 | |
| 158 | _border_size.right = std::max(upper_bound_w, pool_pad_x); |
| 159 | _border_size.bottom = std::max(upper_bound_h, pool_pad_y); |
| 160 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 161 | build_opts.add_option("-DPOOL_SIZE=" + support::cpp11::to_string(pool_size)); |
| 162 | build_opts.add_option_if(data_type == DataType::F16, "-DFP16"); |
| 163 | |
| 164 | std::string kernel_name = is_data_type_quantized_asymmetric(data_type) ? "pooling_layer_N_quantized" : "pooling_layer_N"; |
| 165 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Configure kernel window |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 169 | Window win = calculate_max_window(*output->info(), Steps(_num_elems_processed_per_iteration)); |
| 170 | |
| 171 | // Configure the local work size (hint) from the first two dimensions of the global work size. |
| 172 | // On Bifrost, this works for up to 35x35xC filters, for which the pooling_layer_3_optimized |
| 173 | // kernel is launched with gws=(9, 33, C). In any case, the hint will be ignored if it is |
| 174 | // invalid (e.g. exceeds the maximum workgroup size that the kernel can be launched with). |
| 175 | if(gpu_target == GPUTarget::BIFROST) |
| 176 | { |
| 177 | cl::NDRange gws = ICLKernel::gws_from_window(win); |
| 178 | _lws_hint = cl::NDRange(gws[0], gws[1], 1); |
| 179 | } |
| 180 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right, input_height + _border_size.bottom); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 182 | AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | ICLKernel::configure(win); |
Giorgio Arena | 63485ce | 2017-11-15 16:04:20 +0000 | [diff] [blame^] | 186 | |
| 187 | // Set config_id for enabling LWS tuning |
| 188 | _config_id = "pooling_layer_"; |
| 189 | _config_id += lower_string(string_from_data_type(data_type)); |
| 190 | _config_id += "_"; |
| 191 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 192 | _config_id += "_"; |
| 193 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 196 | Error CLPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info) |
| 197 | { |
| 198 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 199 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 200 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((is_data_type_quantized_asymmetric(input->data_type()) && pool_info.pool_type() == PoolingType::L2), |
| 201 | "Unsupported combination of parameters!"); |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 202 | |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 203 | const bool is_global_pooling = pool_info.is_global_pooling(); |
| 204 | const unsigned int pool_size = is_global_pooling ? input->tensor_shape().x() : pool_info.pool_size(); |
| 205 | |
| 206 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_global_pooling && (input->tensor_shape().x() != input->tensor_shape().y()), |
| 207 | "Global pooling is supported only with rectangular inputs!"); |
| 208 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_global_pooling && ((pool_info.pad_stride_info().pad().first >= pool_size) || (pool_info.pad_stride_info().pad().second >= pool_size)), |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 209 | "Invalid pool size and pool pad combination!"); |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 210 | |
| 211 | // Checks performed when output is configured |
| 212 | if(output->total_size() != 0) |
| 213 | { |
| 214 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 215 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 216 | |
| 217 | unsigned int pooled_w = 0; |
| 218 | unsigned int pooled_h = 0; |
| 219 | std::tie(pooled_w, pooled_h) = scaled_dimensions(input->dimension(0), |
| 220 | input->dimension(1), |
| 221 | pool_size, |
| 222 | pool_size, |
| 223 | pool_info.pad_stride_info()); |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 224 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != pooled_w) || (output->dimension(1) != pooled_h), |
Georgios Pinitas | 3faea25 | 2017-10-30 14:13:50 +0000 | [diff] [blame] | 225 | "Invalid output pooling dimensions!"); |
| 226 | } |
| 227 | |
| 228 | return Error{}; |
| 229 | } |
| 230 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 232 | { |
| 233 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 234 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 235 | |
| 236 | unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0; |
| 237 | std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad(); |
| 238 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 239 | |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 240 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 241 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 242 | |
| 243 | do |
| 244 | { |
| 245 | // Upsample input by pool size |
| 246 | Window in_slice(slice); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame] | 247 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - pool_pad_x, in_slice.x().end() * pool_stride_x, pool_stride_x * _num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y)); |
| 249 | |
| 250 | // Set inputs |
| 251 | unsigned int idx = 0; |
| 252 | add_3D_tensor_argument(idx, _input, in_slice); |
| 253 | add_3D_tensor_argument(idx, _output, slice); |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 254 | enqueue(queue, *this, slice, _lws_hint); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 255 | } |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 256 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 257 | } |