blob: 2ca1ff59dfa518c069e2668168c5e023489a6c54 [file] [log] [blame]
Manuel Bottinic6f4ec32021-05-18 18:41:56 +01001/*
2 * Copyright (c) 2018-2021 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 "src/runtime/gpu/cl/operators/ClWinogradConv2d.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/experimental/Types.h"
30#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32#include "src/core/CL/kernels/CLFillBorderKernel.h"
33#include "src/core/CL/kernels/CLFillBorderKernel.h"
34#include "src/core/gpu/cl/kernels/ClWinogradFilterTransformKernel.h"
35#include "src/core/gpu/cl/kernels/ClWinogradInputTransformKernel.h"
36#include "src/core/gpu/cl/kernels/ClWinogradOutputTransformKernel.h"
37#include "src/core/helpers/MemoryHelpers.h"
38#include "src/runtime/gpu/cl/utils/ClAuxTensorHandler.h"
39#include "support/Cast.h"
40
41using namespace arm_compute::experimental;
42
43namespace arm_compute
44{
45namespace opencl
46{
47namespace
48{
49Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataLayout data_layout)
50{
51 Size2D output_tile = Size2D{};
52
53 const unsigned int kernel_max_dim = std::max(kernel_dims.width, kernel_dims.height);
54
55 // Check if the input spatial dimensions are smaller than 4
56 const bool is_input_lt4_nchw = (input_dims.width <= 4 && input_dims.height <= 4) && (data_layout == DataLayout::NCHW);
57
58 if(kernel_max_dim == 3U)
59 {
60 if(kernel_dims == Size2D(3U, 3U))
61 {
62 output_tile = is_input_lt4_nchw ? Size2D(2U, 2U) : Size2D(4U, 4U);
63 }
64 else if(kernel_dims == Size2D(3U, 1U))
65 {
66 output_tile = is_input_lt4_nchw ? Size2D(2U, 1U) : Size2D(4U, 1U);
67 }
68 else
69 {
70 output_tile = is_input_lt4_nchw ? Size2D(1U, 2U) : Size2D(1U, 4U);
71 }
72 }
73 else if(kernel_max_dim == 5U)
74 {
75 output_tile = Size2D(kernel_dims.width == 1 ? 1U : 4U,
76 kernel_dims.height == 1 ? 1U : 4U);
77 }
78 else if(kernel_max_dim == 7U)
79 {
80 output_tile = Size2D(kernel_dims.width == 1 ? 1U : 2U,
81 kernel_dims.height == 1 ? 1U : 2U);
82 }
83
84 return output_tile;
85}
86
87bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
88{
89 // Check if we want to configure a Winograd configuration which requires fast math
90 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
91
92 std::vector<WinogradConfiguration> fast_math_winograd =
93 {
94 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
95 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7))
96 };
97
98 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
99 std::pair<int, int>(kernel_size.width, kernel_size.height));
100
101 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
102}
103
104Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info,
105 const ActivationLayerInfo &act_info, bool enable_fast_math)
106{
107 // Get indeces for the width and height
108 const size_t idx_width = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
109 const size_t idx_height = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
110
111 // Input shape, kernel size and output tile
112 const Size2D input_dims = Size2D(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height]);
113 const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
114 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, src->data_layout());
115
116 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");
117 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");
118
119 // Check if the Winograd configuration requires fast math
120 if(!enable_fast_math)
121 {
122 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
123 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
124 }
125
126 const WinogradInfo winograd_info = WinogradInfo(output_tile,
127 kernel_size,
128 input_dims,
129 conv_info,
130 src->data_layout());
131
132 // Validate input transform
133 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*src, winograd_info);
134 const TensorInfo input0 = src->clone()->set_tensor_shape(input0_shape);
135 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWinogradInputTransformKernel::validate(src, &input0, winograd_info));
136
137 // Validate filter transform
138 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
139 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
140 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWinogradFilterTransformKernel::validate(weights, &input1, winograd_info));
141
142 // Validate batched matrix multiply
143 TensorShape batched_mm_output_shape = input0.tensor_shape();
144 batched_mm_output_shape[0] = input1.tensor_shape()[0];
145 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
146 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,
147 GEMMLowpOutputStageInfo(), (src->data_type() == DataType::F16))));
148
149 // Configure output transform
150 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWinogradOutputTransformKernel::validate(&batched_mm_output, biases, dst, winograd_info, act_info));
151 return Status{};
152}
153
154} // namespace
155
156ClWinogradConv2d::ClWinogradConv2d()
157 : _batched_mm(),
158 _input_transform(std::make_unique<kernels::ClWinogradInputTransformKernel>()),
159 _filter_transform(std::make_unique<kernels::ClWinogradFilterTransformKernel>()),
160 _output_transform(std::make_unique<kernels::ClWinogradOutputTransformKernel>()),
161 _border_handler(),
162 _input0(),
163 _input1(),
164 _batched_mm_output(),
165 _is_prepared(false),
166 _aux_mem()
167{
168}
169
170ClWinogradConv2d::~ClWinogradConv2d() = default;
171
172void ClWinogradConv2d::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
173 const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, bool enable_fast_math)
174{
175 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info, act_info, enable_fast_math));
176 // Get indices for the width and height
177 const size_t idx_width = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
178 const size_t idx_height = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
179
180 // Input shape, kernel size and output tile
181 const Size2D input_dims = Size2D(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height]);
182 const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
183 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, src->data_layout());
184
185 // Check if the Winograd configuration requires fast math
186 if(!enable_fast_math)
187 {
188 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
189 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
190 }
191 const WinogradInfo winograd_info = WinogradInfo(output_tile,
192 kernel_size,
193 input_dims,
194 conv_info,
195 src->data_layout());
196
197 _is_prepared = false;
198
199 // Configure input transform
200 _input_transform->configure(compile_context, src, &_input0, winograd_info);
201 _border_handler.configure(compile_context, src, _input_transform->border_size(), BorderMode::CONSTANT, PixelValue());
202
203 // Configure filter transform
204 _filter_transform->configure(compile_context, weights, &_input1, winograd_info);
205
206 // Configure batched matrix multiply
207 _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,
208 false, false,
209 GEMMLowpOutputStageInfo(),
210 (src->data_type() == DataType::F16)));
211
212 // Configure output transform
213 _output_transform->configure(compile_context, &_batched_mm_output, biases, dst, winograd_info, act_info);
214
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100215 _aux_mem = _batched_mm.workspace();
216 const MemoryLifetime wino_wei_lifetm = std::any_of(std::begin(_aux_mem), std::end(_aux_mem), [](const auto & r)
217 {
218 return (r.lifetime == MemoryLifetime::Persistent) && (r.size > 0);
219 }) ?
220 MemoryLifetime::Prepare :
221 MemoryLifetime::Persistent;
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100222 _aux_mem.push_back(MemoryInfo(offset_int_vec(2), MemoryLifetime::Temporary, _input0.total_size()));
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100223 _aux_mem.push_back(MemoryInfo(offset_int_vec(3), wino_wei_lifetm, _input1.total_size()));
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100224 _aux_mem.push_back(MemoryInfo(offset_int_vec(4), MemoryLifetime::Temporary, _batched_mm_output.total_size()));
225}
226
227Status ClWinogradConv2d::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info,
228 const ActivationLayerInfo &act_info, bool enable_fast_math)
229{
230 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info, act_info, enable_fast_math));
231 return Status{};
232}
233
234void ClWinogradConv2d::run(ITensorPack &tensors)
235{
236 prepare(tensors);
237
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100238 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
239 auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
240 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
241
242 CLAuxTensorHandler input0(offset_int_vec(2), _input0, tensors, true);
243 CLAuxTensorHandler input1(offset_int_vec(3), _input1, tensors, true);
244 CLAuxTensorHandler batched_mm_output(offset_int_vec(4), _batched_mm_output, tensors, true);
245
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100246 // Run input transform
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100247 ITensorPack pack_it
248 {
249 { TensorType::ACL_SRC, src },
250 { TensorType::ACL_DST, input0.get() },
251 };
252 CLScheduler::get().enqueue_op(_border_handler, pack_it);
253 CLScheduler::get().enqueue_op(*_input_transform, pack_it);
254
255 // Run batched matrix multiplication
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100256 ITensorPack pack_mm = tensors;
257 pack_mm.add_const_tensor(TensorType::ACL_SRC_0, input0.get());
258 pack_mm.add_tensor(TensorType::ACL_DST, batched_mm_output.get());
259 if(_aux_mem[3].lifetime == MemoryLifetime::Prepare)
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100260 {
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100261 pack_mm.remove_tensor(TensorType::ACL_SRC_1);
262 }
263 else
264 {
265 pack_mm.add_const_tensor(TensorType::ACL_SRC_1, input1.get());
266 }
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100267 _batched_mm.run(pack_mm);
268
269 // Run output transform
270 ITensorPack pack_ot
271 {
272 { TensorType::ACL_SRC_0, batched_mm_output.get() },
273 { TensorType::ACL_SRC_1, biases },
274 { TensorType::ACL_DST, dst },
275 };
276 CLScheduler::get().enqueue_op(*_output_transform, pack_ot);
277}
278
279void ClWinogradConv2d::prepare(ITensorPack &tensors)
280{
281 if(!_is_prepared)
282 {
283 auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
284 ICLTensor *in1_aux = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(offset_int_vec(3)));
285
286 CLAuxTensorHandler input1(_input1, *in1_aux);
287 ITensorPack pack_ft
288 {
289 { TensorType::ACL_SRC, weights },
290 { TensorType::ACL_DST, input1.get() },
291 };
292 // Run filter transform and mark original weights as unused
293 CLScheduler::get().enqueue_op(*_filter_transform, pack_ft, false);
294 weights->mark_as_unused();
295
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100296 // Prepare GEMM and release reshaped weights if marked unused by ClGemm
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100297 ITensorPack mm_prepare_pack = tensors;
298 mm_prepare_pack.add_tensor(ACL_SRC_1, input1.get());
299 _batched_mm.prepare(mm_prepare_pack);
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100300
301 CLScheduler::get().queue().finish();
302 _is_prepared = true;
303 }
304}
305
306experimental::MemoryRequirements ClWinogradConv2d::workspace() const
307{
308 return _aux_mem;
309}
310} // namespace opencl
311} // namespace arm_compute