Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/CLWinogradOutputTransformKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/IAccessWindow.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Utils.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/core/Window.h" |
| 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 38 | |
| 39 | #include "support/ToolchainSupport.h" |
| 40 | |
| 41 | #include <cmath> |
| 42 | |
| 43 | using namespace arm_compute; |
| 44 | using namespace arm_compute::misc::shape_calculator; |
| 45 | |
| 46 | namespace |
| 47 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 48 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 49 | { |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 51 | |
Georgios Pinitas | c084f0d | 2018-06-11 17:43:31 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON(output->data_layout() != winograd_info.output_data_layout); |
| 53 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 54 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 55 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 56 | const Size2D kernel_size = winograd_info.kernel_size; |
| 57 | const Size2D input_dimensions = winograd_info.input_dimensions; |
| 58 | |
Giorgio Arena | dd03870 | 2018-04-16 11:20:11 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size != Size2D(3U, 3U) && kernel_size != Size2D(5U, 5U), "Only 3x3 and 5x5 kernels are supported"); |
Giorgio Arena | 7210fe8 | 2018-06-08 12:24:14 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_layout() == DataLayout::NHWC && output_tile_size != Size2D(4U, 4U), "Only 4x4 output tile supported for NHWC data layout"); |
Giorgio Arena | dd03870 | 2018-04-16 11:20:11 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size == Size2D(3U, 3U) && output_tile_size == Size2D(2U, 2U) && input->dimension(2) != 16, "Wrong number of batches"); |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size == Size2D(3U, 3U) && output_tile_size == Size2D(4U, 4U) && input->dimension(2) != 36, "Wrong number of batches"); |
Giorgio Arena | dd03870 | 2018-04-16 11:20:11 +0100 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(kernel_size == Size2D(5U, 5U) && output_tile_size == Size2D(4U, 4U) && input->dimension(2) != 64, "Wrong number of batches"); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 64 | |
| 65 | // Compute number of elements to process in the X and Y direction |
| 66 | const int num_elements_x = input_dimensions.width - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right(); |
| 67 | const int num_elements_y = input_dimensions.height - (kernel_size.height - 1) + conv_info.pad_top() + conv_info.pad_bottom(); |
| 68 | const int num_tiles_x = std::ceil(num_elements_x / static_cast<float>(output_tile_size.width)); |
| 69 | const int num_tiles_y = std::ceil(num_elements_y / static_cast<float>(output_tile_size.height)); |
| 70 | |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) != static_cast<unsigned int>((num_tiles_x * num_tiles_y))); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 72 | |
| 73 | if(bias != nullptr) |
| 74 | { |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0)); |
| 77 | } |
| 78 | |
| 79 | // Checks performed when output is configured |
| 80 | if(output->total_size() != 0) |
| 81 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 82 | const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_winograd_output_transform_shape(*input, winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 83 | |
| 84 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 86 | } |
| 87 | |
| 88 | return Status{}; |
| 89 | } |
| 90 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 91 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output, const Size2D &output_tile_size) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 92 | { |
| 93 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 94 | |
| 95 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 96 | |
| 97 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 98 | bool window_changed = false; |
| 99 | |
Georgios Pinitas | c084f0d | 2018-06-11 17:43:31 +0100 | [diff] [blame] | 100 | int output_static_window_end_x = 0; |
| 101 | int output_static_window_end_y = 0; |
| 102 | |
| 103 | if(output->data_layout() == DataLayout::NCHW) |
| 104 | { |
| 105 | output_static_window_end_x = ceil_to_multiple(output->dimension(0), output_tile_size.width); |
| 106 | output_static_window_end_y = ceil_to_multiple(output->dimension(1), output_tile_size.height); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | output_static_window_end_x = output->dimension(0); |
| 111 | output_static_window_end_y = std::max(ceil_to_multiple(output->dimension(1), output_tile_size.width), output->dimension(1) + 1 /* For out of bound reads towards the z axis */); |
| 112 | } |
| 113 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 114 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration, num_elems_processed_per_iteration); |
Georgios Pinitas | c084f0d | 2018-06-11 17:43:31 +0100 | [diff] [blame] | 115 | AccessWindowStatic output_access(output, 0, 0, output_static_window_end_x, output_static_window_end_y); |
| 116 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 117 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 118 | |
| 119 | if(bias != nullptr) |
| 120 | { |
| 121 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
Georgios Pinitas | c084f0d | 2018-06-11 17:43:31 +0100 | [diff] [blame] | 122 | window_changed = window_changed || update_window_and_padding(win, bias_access); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 123 | } |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 124 | |
| 125 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 126 | return std::make_pair(err, win); |
| 127 | } |
| 128 | } // namespace |
| 129 | |
| 130 | CLWinogradOutputTransformKernel::CLWinogradOutputTransformKernel() |
| 131 | : _input(nullptr), _bias(nullptr), _output(nullptr) |
| 132 | { |
| 133 | } |
| 134 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 135 | void CLWinogradOutputTransformKernel::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 136 | { |
| 137 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 138 | |
| 139 | // Output tensor auto initialization if not yet initialized |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 140 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_winograd_output_transform_shape(*input->info(), winograd_info))); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 141 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr ? bias->info() : nullptr), output->info(), winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 143 | |
| 144 | _input = input; |
| 145 | _bias = bias; |
| 146 | _output = output; |
| 147 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 148 | // Compute num_tiles_x |
| 149 | const Size2D input_dimensions = winograd_info.input_dimensions; |
| 150 | const Size2D kernel_size = winograd_info.kernel_size; |
| 151 | const Size2D output_tile_size = winograd_info.output_tile_size; |
| 152 | const PadStrideInfo conv_info = winograd_info.convolution_info; |
| 153 | const int num_elements_x = input_dimensions.width - (kernel_size.width - 1) + conv_info.pad_left() + conv_info.pad_right(); |
| 154 | const int num_tiles_x = std::ceil(num_elements_x / static_cast<float>(output_tile_size.width)); |
| 155 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 156 | // Set build options |
| 157 | CLBuildOptions build_opts; |
| 158 | build_opts.add_option_if(_bias != nullptr, std::string("-DHAS_BIAS")); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 159 | build_opts.add_option("-DNUM_TILES_X=" + support::cpp11::to_string(num_tiles_x)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 160 | |
| 161 | // Create kernel |
Giorgio Arena | 3695f9a | 2018-04-23 17:41:22 +0100 | [diff] [blame] | 162 | std::string kernel_name = "winograd_output_transform_" + output_tile_size.to_string() + "_" + kernel_size.to_string() + "_" + lower_string(string_from_data_layout(winograd_info.output_data_layout)); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 163 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 164 | |
| 165 | // Configure kernel window |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 166 | auto win_config = validate_and_configure_window(input->info(), (bias != nullptr ? bias->info() : nullptr), output->info(), winograd_info.output_tile_size); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 167 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 168 | ICLKernel::configure(win_config.second); |
| 169 | |
| 170 | // Set config_id for enabling LWS tuning |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 171 | _config_id = kernel_name; |
| 172 | _config_id += "_"; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 173 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 174 | _config_id += "_"; |
| 175 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 176 | _config_id += "_"; |
| 177 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 178 | _config_id += "_"; |
| 179 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 180 | _config_id += "_"; |
| 181 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 182 | } |
| 183 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 184 | Status CLWinogradOutputTransformKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const WinogradInfo &winograd_info) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 185 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 186 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, (bias != nullptr ? bias->clone().get() : nullptr), output, winograd_info)); |
| 187 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), (bias != nullptr ? bias->clone().get() : nullptr), output->clone().get(), winograd_info.output_tile_size).first); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 188 | |
| 189 | return Status{}; |
| 190 | } |
| 191 | |
| 192 | void CLWinogradOutputTransformKernel::run(const Window &window, cl::CommandQueue &queue) |
| 193 | { |
| 194 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 195 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 196 | |
| 197 | // Get initial windows |
| 198 | Window slice = window.first_slice_window_3D(); |
| 199 | slice.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 200 | |
| 201 | // Setup output slice |
| 202 | Window slice_out(slice); |
| 203 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 204 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 205 | |
| 206 | if(_bias != nullptr) |
| 207 | { |
| 208 | unsigned int idx1 = 2 * num_arguments_per_3D_tensor(); |
| 209 | Window slice_biases; |
| 210 | slice_biases.use_tensor_dimensions(_bias->info()->tensor_shape()); |
| 211 | add_1D_tensor_argument(idx1, _bias, slice_biases); |
| 212 | } |
| 213 | |
Georgios Pinitas | c084f0d | 2018-06-11 17:43:31 +0100 | [diff] [blame] | 214 | if(_output->info()->data_layout() == DataLayout::NHWC) |
| 215 | { |
| 216 | unsigned int idx2 = 2 * num_arguments_per_3D_tensor() + ((_bias != nullptr) ? num_arguments_per_1D_tensor() : 0); |
| 217 | _kernel.setArg(idx2, static_cast<int>(_output->info()->total_size() - _output->info()->strides_in_bytes().y())); |
| 218 | } |
| 219 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 220 | do |
| 221 | { |
| 222 | unsigned int idx = 0; |
| 223 | add_3D_tensor_argument(idx, _input, slice); |
| 224 | add_3D_tensor_argument(idx, _output, slice_out); |
| 225 | enqueue(queue, *this, slice, _lws_hint); |
| 226 | } |
| 227 | while(window.slide_window_slice_3D(slice) && window.slide_window_slice_3D(slice_out)); |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 228 | } |