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" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include <set> |
| 38 | #include <string> |
| 39 | #include <tuple> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | CLPoolingLayerKernel::CLPoolingLayerKernel() |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame^] | 44 | : _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] | 45 | { |
| 46 | } |
| 47 | |
| 48 | BorderSize CLPoolingLayerKernel::border_size() const |
| 49 | { |
| 50 | return _border_size; |
| 51 | } |
| 52 | |
| 53 | void CLPoolingLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const PoolingLayerInfo &pool_info) |
| 54 | { |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 55 | int pool_pad_x = 0; |
| 56 | int pool_pad_y = 0; |
| 57 | int pool_stride_x = 0; |
| 58 | int pool_stride_y = 0; |
| 59 | unsigned int pooled_w = 0; |
| 60 | unsigned int pooled_h = 0; |
| 61 | const PoolingType pool_type = pool_info.pool_type(); |
| 62 | const int pool_size = pool_info.pool_size(); |
| 63 | const PadStrideInfo pad_stride_info = pool_info.pad_stride_info(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad(); |
| 65 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
| 66 | |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 67 | static const std::set<int> supported_pool_sizes = { 2, 3, 7 }; |
| 68 | ARM_COMPUTE_UNUSED(supported_pool_sizes); |
| 69 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
Georgios Pinitas | 1dad50e | 2017-07-03 17:51:34 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_ERROR_ON(supported_pool_sizes.find(pool_size) == supported_pool_sizes.end()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON(pool_pad_x >= pool_size || pool_pad_y >= pool_size); |
| 74 | |
| 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 | |
| 88 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 89 | } |
| 90 | |
| 91 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 92 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h)); |
| 94 | |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame^] | 95 | // Check if we have pool3x3 with stride_x less equal than 3. In these cases, run an optimized OpenCL kernel where |
| 96 | // each thread computes 4 output elements |
| 97 | const bool is_pool3x3_stride_le3 = (pool_size == 3) && (pool_stride_x <= 3); |
| 98 | |
| 99 | int num_elements_read_per_iteration = (pool_size == 7) ? 8 : pool_size; |
| 100 | if(is_pool3x3_stride_le3) |
| 101 | { |
| 102 | // Change the number of elements processed and number of elements read per iteration for pooling 3x3 with stride less equal than 3 |
| 103 | _num_elems_processed_per_iteration = 4; |
| 104 | num_elements_read_per_iteration = pool_size * (pool_stride_x + 1); |
| 105 | } |
| 106 | const int input_width = input->info()->dimension(0); |
| 107 | const int input_height = input->info()->dimension(1); |
| 108 | const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + num_elements_read_per_iteration) - input_width; |
| 109 | const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | |
| 111 | // Set instance variables |
| 112 | _input = input; |
| 113 | _output = output; |
| 114 | _pool_info = pool_info; |
| 115 | _border_size = BorderSize(pool_pad_y, pool_pad_x); |
| 116 | _border_size.right = std::max(upper_bound_w, pool_pad_x); |
| 117 | _border_size.bottom = std::max(upper_bound_h, pool_pad_y); |
| 118 | |
| 119 | // Set build options |
| 120 | std::set<std::string> build_opts; |
| 121 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 122 | build_opts.emplace(("-DPOOL_" + ((PoolingType::MAX == pool_type) ? std::string("MAX") : std::string("AVG")))); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame^] | 123 | build_opts.emplace(("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x))); |
| 124 | if(pool_type == PoolingType::AVG) |
| 125 | { |
| 126 | build_opts.emplace(("-DMAX_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0) + pool_pad_x))); |
| 127 | build_opts.emplace(("-DMAX_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1) + pool_pad_y))); |
| 128 | build_opts.emplace(("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y))); |
| 129 | build_opts.emplace(("-DPAD_X=" + support::cpp11::to_string(pool_pad_x))); |
| 130 | build_opts.emplace(("-DPAD_Y=" + support::cpp11::to_string(pool_pad_y))); |
| 131 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | |
| 133 | // Create kernel |
Moritz Pflanzer | 05da6dd | 2017-07-04 12:08:41 +0100 | [diff] [blame] | 134 | std::string kernel_name = "pooling_layer_" + support::cpp11::to_string(pool_size); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame^] | 135 | if(is_pool3x3_stride_le3) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | { |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame^] | 137 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name + "_optimized", build_opts)); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Configure kernel window |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame^] | 145 | Window win = calculate_max_window(*output->info(), Steps(_num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | 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^] | 147 | AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 149 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | ICLKernel::configure(win); |
| 151 | } |
| 152 | |
| 153 | void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 154 | { |
| 155 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 156 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 157 | |
| 158 | unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0; |
| 159 | std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad(); |
| 160 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 161 | |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 162 | Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 163 | Window slice = window_collapsed.first_slice_window_3D(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | |
| 165 | do |
| 166 | { |
| 167 | // Upsample input by pool size |
| 168 | Window in_slice(slice); |
Gian Marco Iodice | cb29283 | 2017-08-02 13:19:48 +0100 | [diff] [blame^] | 169 | 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] | 170 | in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y)); |
| 171 | |
| 172 | // Set inputs |
| 173 | unsigned int idx = 0; |
| 174 | add_3D_tensor_argument(idx, _input, in_slice); |
| 175 | add_3D_tensor_argument(idx, _output, slice); |
| 176 | enqueue(queue, *this, slice); |
| 177 | } |
steniu01 | f70256b | 2017-07-13 14:03:35 +0100 | [diff] [blame] | 178 | while(window_collapsed.slide_window_slice_3D(slice)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | } |