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/runtime/CL/functions/CLWinogradConvolutionLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | |
| 32 | using namespace arm_compute; |
| 33 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 34 | namespace |
| 35 | { |
| 36 | Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, bool enable_fast_math) |
| 37 | { |
| 38 | Size2D output_tile = Size2D{}; |
| 39 | |
| 40 | if(kernel_dims == Size2D(3U, 3U)) |
| 41 | { |
| 42 | output_tile = ((input_dims.width <= 4 && input_dims.height <= 4) || !enable_fast_math) ? Size2D(2U, 2U) : Size2D(4U, 4U); |
| 43 | } |
| 44 | else if(kernel_dims == Size2D(5U, 5U)) |
| 45 | { |
| 46 | output_tile = Size2D(4U, 4U); |
| 47 | } |
| 48 | |
| 49 | return output_tile; |
| 50 | } |
| 51 | |
| 52 | bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size) |
| 53 | { |
| 54 | // Check if we want to configure a Winograd configuration which requires fast math |
| 55 | using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>; |
| 56 | |
| 57 | std::vector<WinogradConfiguration> fast_math_winograd = |
| 58 | { |
| 59 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(3, 3)), |
| 60 | WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)) |
| 61 | }; |
| 62 | |
| 63 | auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height), |
| 64 | std::pair<int, int>(kernel_size.width, kernel_size.height)); |
| 65 | |
| 66 | return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end(); |
| 67 | } |
| 68 | } // namespace |
| 69 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 70 | CLWinogradConvolutionLayer::CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 71 | : _memory_group(memory_manager), _batched_mm(memory_manager), _input_transform(), _filter_transform(), _output_transform(), _activationlayer_function(), _input0(), _input1(), _batched_mm_output(), |
| 72 | _is_first_run(true), _is_activationlayer_enabled(false) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 73 | { |
| 74 | } |
| 75 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 76 | void CLWinogradConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, |
| 77 | bool enable_fast_math) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 78 | { |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 79 | // Get indices for the width and height |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 80 | const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 81 | const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT); |
| 82 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 83 | // Input shape, kernel size and output tile |
| 84 | const Size2D input_dims = Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]); |
| 85 | const Size2D kernel_size = Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]); |
| 86 | const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, enable_fast_math); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 87 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 88 | // Check if the Winograd configuration requires fast math |
| 89 | if(!enable_fast_math) |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true"); |
| 92 | } |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 93 | |
| 94 | const WinogradInfo winograd_info = WinogradInfo(output_tile, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 95 | kernel_size, |
| 96 | input_dims, |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 97 | conv_info, |
| 98 | input->info()->data_layout()); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 99 | |
| 100 | // Manage intermediate tensors |
| 101 | _memory_group.manage(&_input0); |
| 102 | _memory_group.manage(&_batched_mm_output); |
| 103 | |
| 104 | // Do not manage _input1 as it contains the weights |
| 105 | |
| 106 | // Configure input transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 107 | _input_transform.configure(input, &_input0, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 108 | |
| 109 | // Configure filter transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 110 | _filter_transform.configure(weights, &_input1, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 111 | |
| 112 | // Configure batched matrix multiply |
| 113 | _batched_mm.configure(&_input0, &_input1, nullptr, &_batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/)); |
| 114 | |
| 115 | // Configure output transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 116 | _output_transform.configure(&_batched_mm_output, biases, output, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 117 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 118 | // Configure activation layer |
| 119 | _is_activationlayer_enabled = act_info.enabled(); |
| 120 | if(_is_activationlayer_enabled) |
| 121 | { |
| 122 | _activationlayer_function.configure(output, nullptr, act_info); |
| 123 | } |
| 124 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 125 | // Allocate temporary tensors |
| 126 | _input0.allocator()->allocate(); |
| 127 | _input1.allocator()->allocate(); |
| 128 | _batched_mm_output.allocator()->allocate(); |
| 129 | } |
| 130 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 131 | Status CLWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 132 | const ActivationLayerInfo &act_info, bool enable_fast_math) |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 133 | { |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 134 | // Get indeces for the width and height |
| 135 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 136 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
| 137 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 138 | // Input shape, kernel size and output tile |
| 139 | const Size2D input_dims = Size2D(input->tensor_shape()[idx_width], input->tensor_shape()[idx_height]); |
| 140 | const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]); |
| 141 | const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, enable_fast_math); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 142 | |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 143 | // Check if the Winograd configuration requires fast math |
| 144 | if(!enable_fast_math) |
| 145 | { |
| 146 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true"); |
| 147 | } |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 148 | |
| 149 | const WinogradInfo winograd_info = WinogradInfo(output_tile, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 150 | kernel_size, |
| 151 | input_dims, |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 152 | conv_info, |
| 153 | input->data_layout()); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 154 | |
| 155 | // Validate input transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 156 | const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 157 | const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 158 | ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradInputTransform::validate(input, &input0, winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 159 | |
| 160 | // Validate filter transform |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 161 | const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 162 | const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape); |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 163 | ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradFilterTransformKernel::validate(weights, &input1, winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 164 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 165 | // Validate batched matrix multiply |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 166 | TensorShape batched_mm_output_shape = input0.tensor_shape(); |
| 167 | batched_mm_output_shape[0] = input1.tensor_shape()[0]; |
| 168 | const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape); |
| 169 | ARM_COMPUTE_RETURN_ON_ERROR(CLGEMM::validate(&input0, &input1, nullptr, &batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/))); |
| 170 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 171 | // Configure output transform |
| 172 | ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradOutputTransformKernel::validate(&batched_mm_output, biases, output, winograd_info)); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 173 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 174 | // Validate Activation Layer |
| 175 | if(act_info.enabled()) |
| 176 | { |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame^] | 177 | ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info)); |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 180 | return Status{}; |
| 181 | } |
| 182 | |
| 183 | void CLWinogradConvolutionLayer::run() |
| 184 | { |
| 185 | if(_is_first_run) |
| 186 | { |
| 187 | // Run filter transform |
| 188 | CLScheduler::get().enqueue(_filter_transform, false); |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | _memory_group.acquire(); |
| 192 | |
| 193 | // Run input transform |
| 194 | _input_transform.run(); |
| 195 | |
| 196 | // Run batched matrix multiplication |
| 197 | _batched_mm.run(); |
| 198 | |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 199 | // Release reshaped weights if marked unused by CLGEMM |
| 200 | if(_is_first_run && !_input1.is_used()) |
| 201 | { |
| 202 | CLScheduler::get().queue().finish(); |
| 203 | _input1.allocator()->free(); |
| 204 | } |
| 205 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 206 | // Run output transform |
| 207 | CLScheduler::get().enqueue(_output_transform); |
| 208 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 209 | if(_is_activationlayer_enabled) |
| 210 | { |
| 211 | _activationlayer_function.run(); |
| 212 | } |
| 213 | |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 214 | _memory_group.release(); |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 215 | |
| 216 | _is_first_run = false; |
Gian Marco Iodice | d2fab73 | 2018-03-02 11:18:12 +0000 | [diff] [blame] | 217 | } |