blob: b4163a59863ee1e54df621f0130c795170715c8b [file] [log] [blame]
Manuel Bottinic6f4ec32021-05-18 18:41:56 +01001/*
Gunes Bayircc034192022-08-10 15:58:51 +01002 * Copyright (c) 2018-2022 Arm Limited.
Manuel Bottinic6f4ec32021-05-18 18:41:56 +01003 *
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/gpu/cl/operators/ClWinogradConv2d.h"
Manuel Bottinic6f4ec32021-05-18 18:41:56 +010025
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"
Manuel Bottinic6f4ec32021-05-18 18:41:56 +010034#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/gpu/cl/kernels/ClWinogradFilterTransformKernel.h"
36#include "src/gpu/cl/kernels/ClWinogradInputTransformKernel.h"
37#include "src/gpu/cl/kernels/ClWinogradOutputTransformKernel.h"
38#include "src/gpu/cl/utils/ClAuxTensorHandler.h"
ramelg012e53f172021-09-22 10:48:25 +010039
40#include "src/common/utils/Log.h"
Manuel Bottinic6f4ec32021-05-18 18:41:56 +010041#include "support/Cast.h"
42
43using namespace arm_compute::experimental;
44
45namespace arm_compute
46{
47namespace opencl
48{
49namespace
50{
51Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataLayout data_layout)
52{
53 Size2D output_tile = Size2D{};
54
55 const unsigned int kernel_max_dim = std::max(kernel_dims.width, kernel_dims.height);
56
57 // Check if the input spatial dimensions are smaller than 4
58 const bool is_input_lt4_nchw = (input_dims.width <= 4 && input_dims.height <= 4) && (data_layout == DataLayout::NCHW);
59
60 if(kernel_max_dim == 3U)
61 {
62 if(kernel_dims == Size2D(3U, 3U))
63 {
64 output_tile = is_input_lt4_nchw ? Size2D(2U, 2U) : Size2D(4U, 4U);
65 }
66 else if(kernel_dims == Size2D(3U, 1U))
67 {
68 output_tile = is_input_lt4_nchw ? Size2D(2U, 1U) : Size2D(4U, 1U);
69 }
70 else
71 {
72 output_tile = is_input_lt4_nchw ? Size2D(1U, 2U) : Size2D(1U, 4U);
73 }
74 }
75 else if(kernel_max_dim == 5U)
76 {
77 output_tile = Size2D(kernel_dims.width == 1 ? 1U : 4U,
78 kernel_dims.height == 1 ? 1U : 4U);
79 }
80 else if(kernel_max_dim == 7U)
81 {
82 output_tile = Size2D(kernel_dims.width == 1 ? 1U : 2U,
83 kernel_dims.height == 1 ? 1U : 2U);
84 }
85
86 return output_tile;
87}
88
89bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
90{
91 // Check if we want to configure a Winograd configuration which requires fast math
92 using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
93
94 std::vector<WinogradConfiguration> fast_math_winograd =
95 {
96 WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
97 WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7))
98 };
99
100 auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
101 std::pair<int, int>(kernel_size.width, kernel_size.height));
102
103 return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
104}
105
106Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info,
107 const ActivationLayerInfo &act_info, bool enable_fast_math)
108{
109 // Get indeces for the width and height
110 const size_t idx_width = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
111 const size_t idx_height = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
112
113 // Input shape, kernel size and output tile
114 const Size2D input_dims = Size2D(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height]);
115 const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
116 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, src->data_layout());
117
118 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");
119 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");
120
121 // Check if the Winograd configuration requires fast math
122 if(!enable_fast_math)
123 {
124 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
125 ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
126 }
127
128 const WinogradInfo winograd_info = WinogradInfo(output_tile,
129 kernel_size,
130 input_dims,
131 conv_info,
132 src->data_layout());
133
134 // Validate input transform
135 const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*src, winograd_info);
136 const TensorInfo input0 = src->clone()->set_tensor_shape(input0_shape);
137 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWinogradInputTransformKernel::validate(src, &input0, winograd_info));
138
139 // Validate filter transform
140 const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
141 const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
142 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWinogradFilterTransformKernel::validate(weights, &input1, winograd_info));
143
144 // Validate batched matrix multiply
145 TensorShape batched_mm_output_shape = input0.tensor_shape();
146 batched_mm_output_shape[0] = input1.tensor_shape()[0];
147 const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
148 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,
149 GEMMLowpOutputStageInfo(), (src->data_type() == DataType::F16))));
150
151 // Configure output transform
152 ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWinogradOutputTransformKernel::validate(&batched_mm_output, biases, dst, winograd_info, act_info));
153 return Status{};
154}
155
156} // namespace
157
158ClWinogradConv2d::ClWinogradConv2d()
159 : _batched_mm(),
160 _input_transform(std::make_unique<kernels::ClWinogradInputTransformKernel>()),
161 _filter_transform(std::make_unique<kernels::ClWinogradFilterTransformKernel>()),
162 _output_transform(std::make_unique<kernels::ClWinogradOutputTransformKernel>()),
163 _border_handler(),
164 _input0(),
165 _input1(),
166 _batched_mm_output(),
167 _is_prepared(false),
168 _aux_mem()
169{
170}
171
172ClWinogradConv2d::~ClWinogradConv2d() = default;
173
174void ClWinogradConv2d::configure(const ClCompileContext &compile_context, ITensorInfo *src, ITensorInfo *weights, ITensorInfo *biases, ITensorInfo *dst,
175 const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info, bool enable_fast_math)
176{
177 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, weights, biases, dst, conv_info, act_info, enable_fast_math));
ramelg012e53f172021-09-22 10:48:25 +0100178 ARM_COMPUTE_LOG_PARAMS(src, weights, biases, dst, conv_info, act_info, enable_fast_math);
179
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100180 // Get indices for the width and height
181 const size_t idx_width = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::WIDTH);
182 const size_t idx_height = get_data_layout_dimension_index(src->data_layout(), DataLayoutDimension::HEIGHT);
183
184 // Input shape, kernel size and output tile
185 const Size2D input_dims = Size2D(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height]);
186 const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
187 const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, src->data_layout());
188
189 // Check if the Winograd configuration requires fast math
190 if(!enable_fast_math)
191 {
192 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
193 ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
194 }
195 const WinogradInfo winograd_info = WinogradInfo(output_tile,
196 kernel_size,
197 input_dims,
198 conv_info,
199 src->data_layout());
200
201 _is_prepared = false;
202
203 // Configure input transform
204 _input_transform->configure(compile_context, src, &_input0, winograd_info);
205 _border_handler.configure(compile_context, src, _input_transform->border_size(), BorderMode::CONSTANT, PixelValue());
206
207 // Configure filter transform
208 _filter_transform->configure(compile_context, weights, &_input1, winograd_info);
209
210 // Configure batched matrix multiply
211 _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,
212 false, false,
213 GEMMLowpOutputStageInfo(),
214 (src->data_type() == DataType::F16)));
215
216 // Configure output transform
Gunes Bayircc034192022-08-10 15:58:51 +0100217 _output_transform->set_target(CLScheduler::get().target());
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100218 _output_transform->configure(compile_context, &_batched_mm_output, biases, dst, winograd_info, act_info);
219
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100220 _aux_mem = _batched_mm.workspace();
221 const MemoryLifetime wino_wei_lifetm = std::any_of(std::begin(_aux_mem), std::end(_aux_mem), [](const auto & r)
222 {
223 return (r.lifetime == MemoryLifetime::Persistent) && (r.size > 0);
224 }) ?
225 MemoryLifetime::Prepare :
226 MemoryLifetime::Persistent;
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100227 _aux_mem.push_back(MemoryInfo(offset_int_vec(2), MemoryLifetime::Temporary, _input0.total_size()));
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100228 _aux_mem.push_back(MemoryInfo(offset_int_vec(3), wino_wei_lifetm, _input1.total_size()));
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100229 _aux_mem.push_back(MemoryInfo(offset_int_vec(4), MemoryLifetime::Temporary, _batched_mm_output.total_size()));
230}
231
232Status ClWinogradConv2d::validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const PadStrideInfo &conv_info,
233 const ActivationLayerInfo &act_info, bool enable_fast_math)
234{
235 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, weights, biases, dst, conv_info, act_info, enable_fast_math));
236 return Status{};
237}
238
239void ClWinogradConv2d::run(ITensorPack &tensors)
240{
Georgios Pinitase92c23e2021-07-23 20:38:47 +0100241 const bool is_gemm_reshaped = _aux_mem[3].lifetime == MemoryLifetime::Prepare;
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100242
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100243 auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
244 auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
245 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
246
247 CLAuxTensorHandler input0(offset_int_vec(2), _input0, tensors, true);
Georgios Pinitase92c23e2021-07-23 20:38:47 +0100248 CLAuxTensorHandler input1(offset_int_vec(3), _input1, tensors, true, is_gemm_reshaped);
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100249 CLAuxTensorHandler batched_mm_output(offset_int_vec(4), _batched_mm_output, tensors, true);
250
Georgios Pinitase92c23e2021-07-23 20:38:47 +0100251 prepare(tensors);
252
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100253 // Run input transform
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100254 ITensorPack pack_it
255 {
256 { TensorType::ACL_SRC, src },
257 { TensorType::ACL_DST, input0.get() },
258 };
Georgios Pinitase92c23e2021-07-23 20:38:47 +0100259 CLScheduler::get().enqueue_op(_border_handler, pack_it, false);
260 CLScheduler::get().enqueue_op(*_input_transform, pack_it, false);
Manuel Bottinic6f4ec32021-05-18 18:41:56 +0100261
262 // Run batched matrix multiplication
Georgios Pinitas2b147ee2021-07-08 18:14:45 +0100263 ITensorPack pack_mm = tensors;
264 pack_mm.add_const_tensor(TensorType::ACL_SRC_0, input0.get());
265 pack_mm.add_tensor(TensorType::ACL_DST, batched_mm_output.get());
Georgios Pinitase92c23e2021-07-23 20:38:47 +0100266 is_gemm_reshaped ? pack_mm.remove_tensor(TensorType::ACL_SRC_1) : pack_mm.add_const_tensor(TensorType::ACL_SRC_1, input1.get());
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