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