blob: 1abcb671328d95a4e8478b2cf0d3612912f30cd2 [file] [log] [blame]
Jenkinsb3a371b2018-05-23 11:36:53 +01001/*
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
32using namespace arm_compute;
33
34namespace
35{
Jenkins52ba29e2018-08-29 15:32:11 +000036Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataLayout data_layout)
Jenkinsb3a371b2018-05-23 11:36:53 +010037{
38 Size2D output_tile = Size2D{};
39
Jenkins52ba29e2018-08-29 15:32:11 +000040 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
43 const bool is_input_lt4_nchw = (input_dims.width <= 4 && input_dims.height <= 4) && (data_layout == DataLayout::NCHW);
44
45 if(kernel_max_dim == 3U)
Jenkinsb3a371b2018-05-23 11:36:53 +010046 {
Jenkins52ba29e2018-08-29 15:32:11 +000047 if(kernel_dims == Size2D(3U, 3U))
48 {
49 output_tile = is_input_lt4_nchw ? Size2D(2U, 2U) : Size2D(4U, 4U);
50 }
51 else if(kernel_dims == Size2D(3U, 1U))
52 {
53 output_tile = is_input_lt4_nchw ? Size2D(2U, 1U) : Size2D(4U, 1U);
54 }
55 else
56 {
57 output_tile = is_input_lt4_nchw ? Size2D(1U, 2U) : Size2D(1U, 4U);
58 }
Jenkinsb3a371b2018-05-23 11:36:53 +010059 }
Jenkins52ba29e2018-08-29 15:32:11 +000060 else if(kernel_max_dim == 5U)
Jenkinsb3a371b2018-05-23 11:36:53 +010061 {
Jenkins52ba29e2018-08-29 15:32:11 +000062 output_tile = Size2D(kernel_dims.width == 1 ? 1U : 4U,
63 kernel_dims.height == 1 ? 1U : 4U);
Jenkinsb3a371b2018-05-23 11:36:53 +010064 }
65
66 return output_tile;
67}
68
69bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
70{
71 // Check if we want to configure a Winograd configuration which requires fast math
72 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
73
74 std::vector<WinogradConfiguration> fast_math_winograd =
75 {
76 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5))
77 };
78
79 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
80 std::pair<int, int>(kernel_size.width, kernel_size.height));
81
82 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
83}
84} // namespace
85
86CLWinogradConvolutionLayer::CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
87 : _memory_group(memory_manager), _batched_mm(memory_manager), _input_transform(), _filter_transform(), _output_transform(), _activationlayer_function(), _input0(), _input1(), _batched_mm_output(),
88 _original_weights(nullptr), _is_prepared(false), _is_activationlayer_enabled(false)
89{
90}
91
92void CLWinogradConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
93 bool enable_fast_math)
94{
95 // Get indices for the width and height
96 const size_t idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH);
97 const size_t idx_height = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT);
98
99 // Input shape, kernel size and output tile
100 const Size2D input_dims = Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]);
101 const Size2D kernel_size = Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]);
Jenkins52ba29e2018-08-29 15:32:11 +0000102 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, input->info()->data_layout());
Jenkinsb3a371b2018-05-23 11:36:53 +0100103
104 // Check if the Winograd configuration requires fast math
105 if(!enable_fast_math)
106 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000107 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
Jenkinsb3a371b2018-05-23 11:36:53 +0100108 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
109 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100110 const WinogradInfo winograd_info = WinogradInfo(output_tile,
111 kernel_size,
112 input_dims,
113 conv_info,
114 input->info()->data_layout());
115
116 _is_prepared = false;
117 _original_weights = weights;
118
119 // Manage intermediate tensors
120 _memory_group.manage(&_input0);
121 _memory_group.manage(&_batched_mm_output);
122
123 // Do not manage _input1 as it contains the weights
124
125 // Configure input transform
126 _input_transform.configure(input, &_input0, winograd_info);
127
128 // Configure filter transform
129 _filter_transform.configure(weights, &_input1, winograd_info);
130
131 // Configure batched matrix multiply
Jenkinsb9abeae2018-11-22 11:58:08 +0000132 _batched_mm.configure(&_input0, &_input1, nullptr, &_batched_mm_output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run*/, 0, false, false, GEMMLowpOutputStageInfo(),
133 (input->info()->data_type() == DataType::F16)));
Jenkinsb3a371b2018-05-23 11:36:53 +0100134
135 // Configure output transform
136 _output_transform.configure(&_batched_mm_output, biases, output, winograd_info);
137
138 // Configure activation layer
139 _is_activationlayer_enabled = act_info.enabled();
140 if(_is_activationlayer_enabled)
141 {
142 _activationlayer_function.configure(output, nullptr, act_info);
143 }
144
145 // Allocate temporary tensors
146 _input0.allocator()->allocate();
147 _batched_mm_output.allocator()->allocate();
148}
149
150Status CLWinogradConvolutionLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
151 const ActivationLayerInfo &act_info, bool enable_fast_math)
152{
153 // Get indeces for the width and height
154 const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
155 const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
156
157 // Input shape, kernel size and output tile
158 const Size2D input_dims = Size2D(input->tensor_shape()[idx_width], input->tensor_shape()[idx_height]);
159 const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
Jenkins52ba29e2018-08-29 15:32:11 +0000160 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, input->data_layout());
Jenkinsb3a371b2018-05-23 11:36:53 +0100161
162 // Check if the Winograd configuration requires fast math
163 if(!enable_fast_math)
164 {
Jenkinsb9abeae2018-11-22 11:58:08 +0000165 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
Jenkinsb3a371b2018-05-23 11:36:53 +0100166 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
167 }
168
169 const WinogradInfo winograd_info = WinogradInfo(output_tile,
170 kernel_size,
171 input_dims,
172 conv_info,
173 input->data_layout());
174
175 // Validate input transform
176 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
177 const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
178 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradInputTransform::validate(input, &input0, winograd_info));
179
180 // Validate filter transform
181 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
182 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
183 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradFilterTransformKernel::validate(weights, &input1, winograd_info));
184
185 // Validate batched matrix multiply
186 TensorShape batched_mm_output_shape = input0.tensor_shape();
187 batched_mm_output_shape[0] = input1.tensor_shape()[0];
188 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
Jenkinsb9abeae2018-11-22 11:58:08 +0000189 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,
190 GEMMLowpOutputStageInfo(), (input->data_type() == DataType::F16))));
Jenkinsb3a371b2018-05-23 11:36:53 +0100191
192 // Configure output transform
193 ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradOutputTransformKernel::validate(&batched_mm_output, biases, output, winograd_info));
194
195 // Validate Activation Layer
196 if(act_info.enabled())
197 {
198 ARM_COMPUTE_RETURN_ON_ERROR(CLActivationLayer::validate(output, nullptr, act_info));
199 }
200
201 return Status{};
202}
203
204void CLWinogradConvolutionLayer::run()
205{
206 prepare();
207
208 _memory_group.acquire();
209
210 // Run input transform
211 _input_transform.run();
212
213 // Run batched matrix multiplication
214 _batched_mm.run();
215
216 // Run output transform
217 CLScheduler::get().enqueue(_output_transform);
218
219 if(_is_activationlayer_enabled)
220 {
221 _activationlayer_function.run();
222 }
223
224 _memory_group.release();
225}
226
227void CLWinogradConvolutionLayer::prepare()
228{
229 if(!_is_prepared)
230 {
231 // Run filter transform and mark original weights as unused
232 _input1.allocator()->allocate();
233 CLScheduler::get().enqueue(_filter_transform, false);
234 _original_weights->mark_as_unused();
235
236 // Prepare GEMM and release reshaped weights if marked unused by CLGEMM
237 _batched_mm.prepare();
238 if(!_input1.is_used())
239 {
240 _input1.allocator()->free();
241 }
242
243 CLScheduler::get().queue().finish();
244 _is_prepared = true;
245 }
246}