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() |
| 44 | : _input(nullptr), _output(nullptr), _pool_info(), _border_size(0) |
| 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 | { |
| 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(); |
| 64 | DimensionRoundingType pool_round = pad_stride_info.round(); |
| 65 | std::tie(pool_pad_x, pool_pad_y) = pad_stride_info.pad(); |
| 66 | std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride(); |
| 67 | |
| 68 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 69 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32); |
| 70 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 71 | ARM_COMPUTE_ERROR_ON(2 != pool_size && 3 != pool_size); |
| 72 | ARM_COMPUTE_ERROR_ON(pool_pad_x >= pool_size || pool_pad_y >= pool_size); |
| 73 | |
| 74 | // Check output dimensions |
| 75 | std::tie(pooled_w, pooled_h) = scaled_dimensions(input->info()->dimension(0), |
| 76 | input->info()->dimension(1), |
| 77 | pool_size, |
| 78 | pool_stride_x, pool_stride_y, |
| 79 | pool_pad_x, pool_pad_y, |
| 80 | pool_round); |
| 81 | ARM_COMPUTE_UNUSED(pooled_w); |
| 82 | ARM_COMPUTE_UNUSED(pooled_h); |
| 83 | ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h)); |
| 84 | |
| 85 | const int input_width = input->info()->dimension(0); |
| 86 | const int input_height = input->info()->dimension(1); |
| 87 | const int upper_bound_w = ((pooled_w - 1) * pool_stride_x - pool_pad_x + pool_size) - input_width; |
| 88 | const int upper_bound_h = ((pooled_h - 1) * pool_stride_y - pool_pad_y + pool_size) - input_height; |
| 89 | |
| 90 | // Set instance variables |
| 91 | _input = input; |
| 92 | _output = output; |
| 93 | _pool_info = pool_info; |
| 94 | _border_size = BorderSize(pool_pad_y, pool_pad_x); |
| 95 | _border_size.right = std::max(upper_bound_w, pool_pad_x); |
| 96 | _border_size.bottom = std::max(upper_bound_h, pool_pad_y); |
| 97 | |
| 98 | // Set build options |
| 99 | std::set<std::string> build_opts; |
| 100 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 101 | build_opts.emplace(("-DPOOL_" + ((PoolingType::MAX == pool_type) ? std::string("MAX") : std::string("AVG")))); |
| 102 | |
| 103 | // Create kernel |
| 104 | std::string kernel_name = "pooling_layer_" + val_to_string(pool_size); |
| 105 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 106 | |
| 107 | // Set static kernel arguments |
| 108 | if(pool_type == PoolingType::AVG) |
| 109 | { |
| 110 | // Create static kernel arguments |
| 111 | const cl_int2 max_dims = |
| 112 | { |
| 113 | { |
| 114 | static_cast<cl_int>(input->info()->dimension(0)) + pool_pad_x, |
| 115 | static_cast<cl_int>(input->info()->dimension(1)) + pool_pad_y, |
| 116 | } |
| 117 | }; |
| 118 | const cl_int2 strides = |
| 119 | { |
| 120 | { |
| 121 | pool_stride_x, |
| 122 | pool_stride_y, |
| 123 | } |
| 124 | }; |
| 125 | const cl_int2 paddings = |
| 126 | { |
| 127 | { |
| 128 | pool_pad_x, |
| 129 | pool_pad_y, |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | // Set static kernel arguments |
| 134 | unsigned int idx = 2 * num_arguments_per_3D_tensor(); |
| 135 | _kernel.setArg<cl_int2>(idx++, max_dims); |
| 136 | _kernel.setArg<cl_int2>(idx++, strides); |
| 137 | _kernel.setArg<cl_int2>(idx++, paddings); |
| 138 | } |
| 139 | |
| 140 | // Configure kernel window |
| 141 | const unsigned int num_elems_processed_per_iteration = 1; |
| 142 | |
| 143 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 144 | |
| 145 | AccessWindowStatic input_access(input->info(), -pool_pad_x, -pool_pad_y, input_width + _border_size.right, input_height + _border_size.bottom); |
| 146 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 147 | |
| 148 | update_window_and_padding(win, input_access, output_access); |
| 149 | |
| 150 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 151 | |
| 152 | ICLKernel::configure(win); |
| 153 | } |
| 154 | |
| 155 | void CLPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 156 | { |
| 157 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 158 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 159 | |
| 160 | unsigned int pool_pad_x, pool_pad_y, pool_stride_x, pool_stride_y = 0; |
| 161 | std::tie(pool_pad_x, pool_pad_y) = _pool_info.pad_stride_info().pad(); |
| 162 | std::tie(pool_stride_x, pool_stride_y) = _pool_info.pad_stride_info().stride(); |
| 163 | |
| 164 | Window slice = window.first_slice_window_3D(); |
| 165 | |
| 166 | do |
| 167 | { |
| 168 | // Upsample input by pool size |
| 169 | Window in_slice(slice); |
| 170 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start() - pool_pad_x, in_slice.x().end() * pool_stride_x, pool_stride_x)); |
| 171 | in_slice.set(Window::DimY, Window::Dimension(in_slice.y().start() - pool_pad_y, in_slice.y().end() * pool_stride_y, pool_stride_y)); |
| 172 | |
| 173 | // Set inputs |
| 174 | unsigned int idx = 0; |
| 175 | add_3D_tensor_argument(idx, _input, in_slice); |
| 176 | add_3D_tensor_argument(idx, _output, slice); |
| 177 | enqueue(queue, *this, slice); |
| 178 | } |
| 179 | while(window.slide_window_slice_3D(slice)); |
| 180 | } |