blob: 09a35a6f27a18b3c237a875096a79c74962dcae3 [file] [log] [blame]
Gian Marco Iodiced2fab732018-03-02 11:18:12 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodiced2fab732018-03-02 11:18:12 +00003 *
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
32using namespace arm_compute;
33
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010034namespace
35{
Gian Marco Iodiced30714a2018-08-15 16:53:27 +010036Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataLayout data_layout)
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010037{
38 Size2D output_tile = Size2D{};
39
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010040 const unsigned int kernel_max_dim = std::max(kernel_dims.width, kernel_dims.height);
41
42 // Check if the input spatial dimensions are smaller than 4
Gian Marco Iodiced30714a2018-08-15 16:53:27 +010043 const bool is_input_lt4_nchw = (input_dims.width <= 4 && input_dims.height <= 4) && (data_layout == DataLayout::NCHW);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010044
45 if(kernel_max_dim == 3U)
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010046 {
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010047 if(kernel_dims == Size2D(3U, 3U))
48 {
Gian Marco Iodiced30714a2018-08-15 16:53:27 +010049 output_tile = is_input_lt4_nchw ? Size2D(2U, 2U) : Size2D(4U, 4U);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010050 }
51 else if(kernel_dims == Size2D(3U, 1U))
52 {
Gian Marco Iodiced30714a2018-08-15 16:53:27 +010053 output_tile = is_input_lt4_nchw ? Size2D(2U, 1U) : Size2D(4U, 1U);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010054 }
55 else
56 {
Gian Marco Iodiced30714a2018-08-15 16:53:27 +010057 output_tile = is_input_lt4_nchw ? Size2D(1U, 2U) : Size2D(1U, 4U);
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010058 }
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010059 }
Gian Marco Iodicef1c2bf02018-06-13 14:05:54 +010060 else if(kernel_max_dim == 5U)
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010061 {
Gian Marco Iodice876be2a2018-07-03 12:22:09 +010062 output_tile = Size2D(kernel_dims.width == 1 ? 1U : 4U,
63 kernel_dims.height == 1 ? 1U : 4U);
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010064 }
giuros013bfacb22019-04-01 12:07:02 +010065 else if(kernel_max_dim == 7U)
66 {
Gian Marco Iodice4fbcac62019-04-04 10:14:58 +010067 output_tile = Size2D(kernel_dims.width == 1 ? 1U : 2U,
68 kernel_dims.height == 1 ? 1U : 2U);
giuros013bfacb22019-04-01 12:07:02 +010069 }
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010070
71 return output_tile;
72}
73
74bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
75{
76 // Check if we want to configure a Winograd configuration which requires fast math
77 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
78
79 std::vector<WinogradConfiguration> fast_math_winograd =
80 {
giuros013bfacb22019-04-01 12:07:02 +010081 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
82 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7))
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010083 };
84
85 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
86 std::pair<int, int>(kernel_size.width, kernel_size.height));
87
88 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
89}
90} // namespace
91
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000092CLWinogradConvolutionLayer::CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Manuel Bottini0d0028c2018-10-02 16:41:52 +010093 : _memory_group(memory_manager), _batched_mm(memory_manager), _input_transform(), _filter_transform(), _output_transform(), _input0(), _input1(), _batched_mm_output(), _original_weights(nullptr),
94 _is_prepared(false)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000095{
96}
97
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010098void CLWinogradConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
99 bool enable_fast_math)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000100{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100101 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, act_info, enable_fast_math);
102}
103
104void CLWinogradConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
105 const PadStrideInfo &conv_info,
106 const ActivationLayerInfo &act_info, bool enable_fast_math)
107{
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000108 // Get indices for the width and height
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000109 const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
110 const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
111
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100112 // Input shape, kernel size and output tile
113 const Size2D input_dims = Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]);
114 const Size2D kernel_size = Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]);
Gian Marco Iodiced30714a2018-08-15 16:53:27 +0100115 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, input->info()->data_layout());
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000116
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100117 // Check if the Winograd configuration requires fast math
118 if(!enable_fast_math)
119 {
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000120 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100121 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
122 }
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100123 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100124 kernel_size,
125 input_dims,
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000126 conv_info,
127 input->info()->data_layout());
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000128
Georgios Pinitase0437672018-05-02 14:07:55 +0100129 _is_prepared = false;
130 _original_weights = weights;
131
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000132 // Manage intermediate tensors
133 _memory_group.manage(&_input0);
134 _memory_group.manage(&_batched_mm_output);
135
136 // Do not manage _input1 as it contains the weights
137
138 // Configure input transform
Manuel Bottini2b84be52020-04-08 10:15:51 +0100139 _input_transform.configure(compile_context, input, &_input0, winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000140
141 // Configure filter transform
Manuel Bottini2b84be52020-04-08 10:15:51 +0100142 _filter_transform.configure(compile_context, weights, &_input1, winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000143
144 // Configure batched matrix multiply
Manuel Bottini2b84be52020-04-08 10:15:51 +0100145 _batched_mm.configure(compile_context, &_input0, &_input1, nullptr, &_batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/, 0, false, false,
146 GEMMLowpOutputStageInfo(),
147 (input->info()->data_type() == DataType::F16)));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000148
149 // Configure output transform
Manuel Bottini2b84be52020-04-08 10:15:51 +0100150 _output_transform.configure(compile_context, &_batched_mm_output, biases, output, winograd_info, act_info);
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000151
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000152 // Allocate temporary tensors
153 _input0.allocator()->allocate();
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000154 _batched_mm_output.allocator()->allocate();
155}
156
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000157Status CLWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100158 const ActivationLayerInfo &act_info, bool enable_fast_math)
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000159{
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000160 // Get indeces for the width and height
161 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
162 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
163
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100164 // Input shape, kernel size and output tile
165 const Size2D input_dims = Size2D(input->tensor_shape()[idx_width], input->tensor_shape()[idx_height]);
166 const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
Gian Marco Iodiced30714a2018-08-15 16:53:27 +0100167 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, input->data_layout());
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000168
Pablo Tello7786f252019-08-05 12:22:37 +0100169 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((conv_info.pad_left() > (kernel_size.x() / 2u)) || (conv_info.pad_right() > (kernel_size.x() / 2u))), "Winograd only supports padding up to half kernel size");
170 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((conv_info.pad_top() > (kernel_size.y() / 2u)) || (conv_info.pad_bottom() > (kernel_size.y() / 2u))), "Winograd only supports padding up to half kernel size");
171
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100172 // Check if the Winograd configuration requires fast math
173 if(!enable_fast_math)
174 {
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000175 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100176 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
177 }
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100178
179 const WinogradInfo winograd_info = WinogradInfo(output_tile,
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100180 kernel_size,
181 input_dims,
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000182 conv_info,
183 input->data_layout());
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000184
185 // Validate input transform
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000186 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000187 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000188 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradInputTransform::validate(input, &input0, winograd_info));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000189
190 // Validate filter transform
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000191 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000192 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000193 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradFilterTransformKernel::validate(weights, &input1, winograd_info));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000194
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000195 // Validate batched matrix multiply
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000196 TensorShape batched_mm_output_shape = input0.tensor_shape();
197 batched_mm_output_shape[0] = input1.tensor_shape()[0];
198 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +0000199 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*/, 0, false, false,
200 GEMMLowpOutputStageInfo(), (input->data_type() == DataType::F16))));
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000201
Gian Marco Iodice247f52c2018-03-22 11:24:56 +0000202 // Configure output transform
Gian Marco Iodice607339f2019-08-01 15:53:23 +0100203 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradOutputTransformKernel::validate(&batched_mm_output, biases, output, winograd_info, act_info));
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000204
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000205 return Status{};
206}
207
208void CLWinogradConvolutionLayer::run()
209{
Georgios Pinitase0437672018-05-02 14:07:55 +0100210 prepare();
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000211
Georgios Pinitasda953f22019-04-02 17:27:03 +0100212 MemoryGroupResourceScope scope_mg(_memory_group);
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000213
214 // Run input transform
215 _input_transform.run();
216
217 // Run batched matrix multiplication
218 _batched_mm.run();
219
220 // Run output transform
221 CLScheduler::get().enqueue(_output_transform);
Georgios Pinitase0437672018-05-02 14:07:55 +0100222}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100223
Georgios Pinitase0437672018-05-02 14:07:55 +0100224void CLWinogradConvolutionLayer::prepare()
225{
226 if(!_is_prepared)
227 {
228 // Run filter transform and mark original weights as unused
229 _input1.allocator()->allocate();
230 CLScheduler::get().enqueue(_filter_transform, false);
231 _original_weights->mark_as_unused();
232
233 // Prepare GEMM and release reshaped weights if marked unused by CLGEMM
234 _batched_mm.prepare();
235 if(!_input1.is_used())
236 {
237 _input1.allocator()->free();
238 }
239
240 CLScheduler::get().queue().finish();
241 _is_prepared = true;
242 }
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000243}