Compute Library
 21.02
CLWinogradConvolutionLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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  */
25 
27 #include "arm_compute/core/Utils.h"
39 
40 using namespace arm_compute;
41 
42 namespace
43 {
44 Size2D winograd_output_tile(const Size2D &input_dims, const Size2D &kernel_dims, DataLayout data_layout)
45 {
46  Size2D output_tile = Size2D{};
47 
48  const unsigned int kernel_max_dim = std::max(kernel_dims.width, kernel_dims.height);
49 
50  // Check if the input spatial dimensions are smaller than 4
51  const bool is_input_lt4_nchw = (input_dims.width <= 4 && input_dims.height <= 4) && (data_layout == DataLayout::NCHW);
52 
53  if(kernel_max_dim == 3U)
54  {
55  if(kernel_dims == Size2D(3U, 3U))
56  {
57  output_tile = is_input_lt4_nchw ? Size2D(2U, 2U) : Size2D(4U, 4U);
58  }
59  else if(kernel_dims == Size2D(3U, 1U))
60  {
61  output_tile = is_input_lt4_nchw ? Size2D(2U, 1U) : Size2D(4U, 1U);
62  }
63  else
64  {
65  output_tile = is_input_lt4_nchw ? Size2D(1U, 2U) : Size2D(1U, 4U);
66  }
67  }
68  else if(kernel_max_dim == 5U)
69  {
70  output_tile = Size2D(kernel_dims.width == 1 ? 1U : 4U,
71  kernel_dims.height == 1 ? 1U : 4U);
72  }
73  else if(kernel_max_dim == 7U)
74  {
75  output_tile = Size2D(kernel_dims.width == 1 ? 1U : 2U,
76  kernel_dims.height == 1 ? 1U : 2U);
77  }
78 
79  return output_tile;
80 }
81 
82 bool check_support_fast_math(const Size2D &output_tile, const Size2D &kernel_size)
83 {
84  // Check if we want to configure a Winograd configuration which requires fast math
85  using WinogradConfiguration = std::pair<std::pair<int, int>, std::pair<int, int>>;
86 
87  std::vector<WinogradConfiguration> fast_math_winograd =
88  {
89  WinogradConfiguration(std::pair<int, int>(4, 4), std::pair<int, int>(5, 5)),
90  WinogradConfiguration(std::pair<int, int>(2, 2), std::pair<int, int>(7, 7))
91  };
92 
93  auto p = std::make_pair(std::pair<int, int>(output_tile.width, output_tile.height),
94  std::pair<int, int>(kernel_size.width, kernel_size.height));
95 
96  return std::find(fast_math_winograd.begin(), fast_math_winograd.end(), p) != fast_math_winograd.end();
97 }
98 } // namespace
99 
100 CLWinogradConvolutionLayer::CLWinogradConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
101  : _memory_group(memory_manager), _batched_mm(memory_manager), _input_transform(), _filter_transform(std::make_unique<CLWinogradFilterTransformKernel>()),
102  _output_transform(std::make_unique<CLWinogradOutputTransformKernel>()), _input0(), _input1(), _batched_mm_output(), _original_weights(nullptr), _is_prepared(false)
103 {
104 }
105 
107 
108 void CLWinogradConvolutionLayer::configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info,
109  bool enable_fast_math)
110 {
111  configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, act_info, enable_fast_math);
112 }
113 
114 void CLWinogradConvolutionLayer::configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
115  const PadStrideInfo &conv_info,
116  const ActivationLayerInfo &act_info, bool enable_fast_math)
117 {
118  // Get indices for the width and height
121 
122  // Input shape, kernel size and output tile
123  const Size2D input_dims = Size2D(input->info()->tensor_shape()[idx_width], input->info()->tensor_shape()[idx_height]);
124  const Size2D kernel_size = Size2D(weights->info()->tensor_shape()[idx_width], weights->info()->tensor_shape()[idx_height]);
125  const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, input->info()->data_layout());
126 
127  // Check if the Winograd configuration requires fast math
128  if(!enable_fast_math)
129  {
130  ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
131  ARM_COMPUTE_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
132  }
133  const WinogradInfo winograd_info = WinogradInfo(output_tile,
134  kernel_size,
135  input_dims,
136  conv_info,
137  input->info()->data_layout());
138 
139  _is_prepared = false;
140  _original_weights = weights;
141 
142  // Manage intermediate tensors
143  _memory_group.manage(&_input0);
144  _memory_group.manage(&_batched_mm_output);
145 
146  // Do not manage _input1 as it contains the weights
147 
148  // Configure input transform
149  _input_transform.configure(compile_context, input, &_input0, winograd_info);
150 
151  // Configure filter transform
152  _filter_transform->configure(compile_context, weights, &_input1, winograd_info);
153 
154  // Configure batched matrix multiply
155  _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,
157  (input->info()->data_type() == DataType::F16)));
158 
159  // Configure output transform
160  _output_transform->configure(compile_context, &_batched_mm_output, biases, output, winograd_info, act_info);
161 
162  // Allocate temporary tensors
163  _input0.allocator()->allocate();
164  _batched_mm_output.allocator()->allocate();
165 }
166 
168  const ActivationLayerInfo &act_info, bool enable_fast_math)
169 {
170  // Get indeces for the width and height
173 
174  // Input shape, kernel size and output tile
175  const Size2D input_dims = Size2D(input->tensor_shape()[idx_width], input->tensor_shape()[idx_height]);
176  const Size2D kernel_size = Size2D(weights->tensor_shape()[idx_width], weights->tensor_shape()[idx_height]);
177  const Size2D output_tile = winograd_output_tile(input_dims, kernel_size, input->data_layout());
178 
179  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");
180  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");
181 
182  // Check if the Winograd configuration requires fast math
183  if(!enable_fast_math)
184  {
185  ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); //disable winograd for fp16 if fast math is false.
186  ARM_COMPUTE_RETURN_ERROR_ON_MSG(check_support_fast_math(output_tile, kernel_size), "This Winograd configuration requires enable_fast_math=true");
187  }
188 
189  const WinogradInfo winograd_info = WinogradInfo(output_tile,
190  kernel_size,
191  input_dims,
192  conv_info,
193  input->data_layout());
194 
195  // Validate input transform
196  const TensorShape input0_shape = misc::shape_calculator::compute_winograd_input_transform_shape(*input, winograd_info);
197  const TensorInfo input0 = input->clone()->set_tensor_shape(input0_shape);
198  ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradInputTransform::validate(input, &input0, winograd_info));
199 
200  // Validate filter transform
201  const TensorShape input1_shape = misc::shape_calculator::compute_winograd_filter_transform_shape(*weights, winograd_info);
202  const TensorInfo input1 = weights->clone()->set_tensor_shape(input1_shape);
204 
205  // Validate batched matrix multiply
206  TensorShape batched_mm_output_shape = input0.tensor_shape();
207  batched_mm_output_shape[0] = input1.tensor_shape()[0];
208  const TensorInfo batched_mm_output = input0.clone()->set_tensor_shape(batched_mm_output_shape);
209  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,
210  GEMMLowpOutputStageInfo(), (input->data_type() == DataType::F16))));
211 
212  // Configure output transform
213  ARM_COMPUTE_RETURN_ON_ERROR(CLWinogradOutputTransformKernel::validate(&batched_mm_output, biases, output, winograd_info, act_info));
214 
215  return Status{};
216 }
217 
219 {
220  prepare();
221 
222  MemoryGroupResourceScope scope_mg(_memory_group);
223 
224  // Run input transform
225  _input_transform.run();
226 
227  // Run batched matrix multiplication
228  _batched_mm.run();
229 
230  // Run output transform
231  CLScheduler::get().enqueue(*_output_transform);
232 }
233 
235 {
236  if(!_is_prepared)
237  {
238  // Run filter transform and mark original weights as unused
239  _input1.allocator()->allocate();
240  CLScheduler::get().enqueue(*_filter_transform, false);
241  _original_weights->mark_as_unused();
242 
243  // Prepare GEMM and release reshaped weights if marked unused by CLGEMM
244  _batched_mm.prepare();
245  if(!_input1.is_used())
246  {
247  _input1.allocator()->free();
248  }
249 
250  CLScheduler::get().queue().finish();
251  _is_prepared = true;
252  }
253 }
Shape of a tensor.
Definition: TensorShape.h:39
void prepare() override
Prepare the function for executing.
Definition: CLGEMM.cpp:870
TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Calculate the winograd input transform shape.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Static function to check if given info will lead to a valid configuration of CLWinogradFilterTransfor...
static Status validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const WinogradInfo &winograd_info, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLWinogradOutputTransfor...
std::unique_ptr< ITensorInfo > clone() const override
Provide a clone of the current object of class T.
Definition: TensorInfo.cpp:316
void run() override
Run the kernels contained in the function.
Definition: CLGEMM.cpp:778
CLWinogradConvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Default constructor.
static CLScheduler & get()
Access the scheduler singleton.
Winograd information.
Definition: Types.h:2182
void run() override
Run the kernels contained in the function.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:163
1 channel, 1 F32 per channel
void prepare() override
Prepare the function for executing.
const DataLayout data_layout
Definition: Im2Col.cpp:151
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
CLTensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: CLTensor.cpp:61
unsigned int pad_top() const
Get the top padding.
Definition: Types.h:806
Status class.
Definition: Error.h:52
Interface for the Winograd output transform kernel.
Activation Layer Information class.
Definition: Types.h:1550
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
1 channel, 1 F16 per channel
void mark_as_unused() const
Marks a tensor as unused.
Definition: ITensor.cpp:168
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
void run() override final
Run the kernels contained in the function.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
GEMMLowp output stage info.
Definition: Types.h:1952
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
unsigned int pad_right() const
Get the right padding.
Definition: Types.h:801
Padding and stride information class.
Definition: Types.h:722
TensorShape compute_winograd_filter_transform_shape(const ITensorInfo &input, const WinogradInfo &winograd_info)
Calculate the winograd filter transform shape.
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo(), bool enable_fast_math=false)
Static function to check if given info will lead to a valid configuration of CLWinogradConvolutionLay...
cl::CommandQueue & queue()
Accessor for the associated CL command queue.
Definition: CLScheduler.cpp:41
Interface for the Winograd filter transform kernel.
void enqueue(ICLKernel &kernel, bool flush=true)
Schedule the execution of the passed kernel if possible.
Num samples, channels, height, width.
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
~CLWinogradConvolutionLayer()
Default destructor.
CLCompileContext class.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel&#39;s inputs and output.
Definition: CLGEMM.cpp:666
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
void free() override
Free allocated OpenCL memory.
void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const ActivationLayerInfo &act_info=ActivationLayerInfo(), bool enable_fast_math=false)
Set the input and output tensors.
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration of CLGEMM.
Definition: CLGEMM.cpp:727
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
GEMM information class.
Definition: Types.h:2003
void configure(ICLTensor *input, ICLTensor *output, const WinogradInfo &winograd_info)
Set the input and output tensors.
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
unsigned int pad_bottom() const
Get the bottom padding.
Definition: Types.h:811
const TensorShape & tensor_shape() const override
Size for each dimension of the tensor.
Definition: TensorInfo.h:262
unsigned int pad_left() const
Get the left padding.
Definition: Types.h:796
def find(path, pattern)
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const WinogradInfo &winograd_info)
Static function to check if given info will lead to a valid configuration of CLWinogradInputTransform...
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.