Compute Library
 21.05
CLGEMMConvolutionLayer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 #ifndef ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
25 #define ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
26 
28 
30 #include "arm_compute/core/Types.h"
39 
40 #include <memory>
41 
42 namespace arm_compute
43 {
44 class CLCol2ImKernel;
45 class CLIm2ColKernel;
46 class CLWeightsReshapeKernel;
47 class ICLTensor;
48 
49 /** Function to reshape and transpose the weights. This function calls the following kernels:
50  * -# @ref CLWeightsReshapeKernel
51  */
53 {
54 public:
55  /** Constructor */
57  /** Prevent instances of this class from being copied */
59  /** Prevent instances of this class from being copied */
61  /** Default move constructor */
63  /** Default move assignment operator */
65  /** Default destructor */
67  /** Set the input and output tensors.
68  *
69  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
70  * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
71  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
72  * @param[out] output Destination tensor. Data types supported: Same as @p weights.
73  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
74  */
75  void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups = 1);
76  /** Set the input and output tensors.
77  *
78  * @param[in] compile_context The compile context to be used.
79  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
80  * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
81  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
82  * @param[out] output Destination tensor. Data types supported: Same as @p weights.
83  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
84  */
85  void configure(const CLCompileContext &compile_context, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups = 1);
86  /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayerReshapeWeights
87  *
88  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
89  * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
90  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
91  * @param[in] output Destination tensor. Data types supported: Same as @p weights.
92  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
93  *
94  * @return a status
95  */
96  static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups = 1);
97  // Inherited methods overridden:
98  void run() override;
99 
100 private:
101  std::unique_ptr<CLWeightsReshapeKernel> _weights_reshape_kernel;
102 };
103 
104 namespace weights_transformations
105 {
106 /** Basic function to manage the reshape weights generated from @ref CLConvolutionLayerReshapeWeights */
108 {
109 public:
110  /** Configures the @ref CLConvolutionLayerReshapeWeights function
111  *
112  * @param[in] input Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
113  * @param[in] biases Biases tensor. Data type supported: same as @p input, S32 if @p input is quantized.
114  * @param[in] num_groups Number of groups when performing a grouped convolution.
115  */
116  void configure(const ICLTensor *input, const ICLTensor *biases, unsigned int num_groups)
117  {
118  configure(CLKernelLibrary::get().get_compile_context(), input, biases, num_groups);
119  }
120  /** Configures the @ref CLConvolutionLayerReshapeWeights function
121  *
122  * @param[in] compile_context The compile context to be used.
123  * @param[in] input Input tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/F16/F32.
124  * @param[in] biases Biases tensor. Data type supported: same as @p input, S32 if @p input is quantized.
125  * @param[in] num_groups Number of groups when performing a grouped convolution.
126  */
127  void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *biases, unsigned int num_groups)
128  {
129  _bias_bit = (biases != nullptr) ? 1 : 0;
130  _num_groups = num_groups;
131  _func.configure(compile_context, input, biases, &_output, num_groups);
132  }
133 
134  //Inherited method override
135  void run() override
136  {
137  _output.allocator()->allocate();
138  _func.run();
139  _reshape_run = true;
140  }
141 
142  //Inherited method override
143  ICLTensor *get_weights() override
144  {
145  return &_output;
146  }
147 
148  //Inherited method override
149  void release() override
150  {
151  _output.allocator()->free();
152  }
153 
154  //Inherited method override
155  uint32_t uid() override
156  {
157  return ((0x9) | (_bias_bit << 7) | (_num_groups << 8));
158  }
159 
160 private:
161  CLTensor _output{};
163  int32_t _bias_bit{ 0 };
164  unsigned int _num_groups{ 0 };
165 };
166 } // namespace weights_transformations
167 
168 /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
169  *
170  * -# @ref CLIm2ColKernel
171  * -# @ref CLGEMM (if the data type is FP32 or FP16)
172  * -# @ref CLGEMMLowpMatrixMultiplyCore (if the data type is QASYMM8/QASYMM8_SIGNED)
173  * -# @ref CLGEMMLowpOutputStage with QUANTIZE_DOWN_FIXEDPOINT type of quantization (if the data type is QASYMM8/QASYMM8_SIGNED)
174  * -# @ref CLCol2ImKernel (if NCHW data layout)
175  */
177 {
178 public:
179  /** Constructor
180  *
181  * @param[in] memory_manager (Optional) Memory manager.
182  * @param[in] weights_manager (Optional) Weights manager.
183  */
184  CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
185  /** Prevent instances of this class from being copied (As this class contains pointers) */
187  /** Default move constructor */
189  /** Prevent instances of this class from being copied (As this class contains pointers) */
191  /** Default move assignment operator */
193  /**Default destructor */
195  /** Set the input and output tensors.
196  *
197  * Valid data layouts:
198  * - NHWC
199  * - NCHW
200  *
201  * Valid data type configurations:
202  * |src0 |src1 |src2 |dst |
203  * |:--------------|:------------------|:--------|:--------------|
204  * |F16 |F16 |F16 |F16 |
205  * |F32 |F32 |F32 |F32 |
206  * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
207  * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
208  * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
209  * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
210  *
211  * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
212  * while every optional dimension from 4 and above represent a batch of inputs.
213  * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
214  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
215  * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
216  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
217  * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
218  * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
219  * Data types supported: Same as @p input.
220  * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
221  * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights
222  * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
223  * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
224  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
225  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
226  */
227  void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
228  const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
229  /** Set the input and output tensors.
230  *
231  * @param[in] compile_context The compile context to be used.
232  * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
233  * while every optional dimension from 4 and above represent a batch of inputs.
234  * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
235  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
236  * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
237  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
238  * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
239  * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
240  * Data types supported: Same as @p input.
241  * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
242  * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights
243  * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
244  * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
245  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
246  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
247  */
248  void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
250  const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
251  /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer.
252  *
253  * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
254  * while every optional dimension from 4 and above represent a batch of inputs.
255  * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
256  * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
257  * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
258  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
259  * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
260  * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
261  * Data types supported: Same as @p input.
262  * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
263  * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. If this is not part of the fully connected layer the weights
264  * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
265  * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
266  * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
267  * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
268  *
269  * @return a status
270  */
271  static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
272  const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
273 
274  // Inherited methods overridden:
275  void run() override;
276  void prepare() override;
277 
278 private:
279  /** Configures the appropriate matrix multiply routine
280  *
281  * @param[in] compile_context The compile context to be used.
282  * @param[in] input Input tensor. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
283  * @param[in] weights Weights tensor. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or
284  * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
285  * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
286  * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
287  * @param[in, out] output Output tensor. Data types supported: same as @p input.
288  * @param[in] gemmlowp_output_stage GEMMLowp output stage info
289  * @param[in] gemm_3d_depth Depth of GEMM 3D
290  * @param[in] act_info Activation to apply after the matrix multiplication
291  */
292  void configure_mm(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
293  const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
294  int gemm_3d_depth, const ActivationLayerInfo &act_info);
295  /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer matrix multiply routines
296  *
297  * @param[in] input Input tensor info. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
298  * @param[in] weights Weights tensor info. Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8 or
299  * QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8_SIGNED.
300  * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
301  * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
302  * @param[in] output Output tensor info. Data types supported: same as @p input.
303  * @param[in] gemmlowp_output_stage GEMMLowp output stage info
304  * @param[in] gemm_3d_depth Depth of GEMM 3D
305  * @param[in] skip_im2col Flag which specifies if im2col has to be skipped. i.e. 1x1 convolution with NHWC data layout.
306  * @param[in] act_info Activation to apply after the matrix multiplication
307  *
308  * @return a status
309  */
310  static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const GEMMLowpOutputStageInfo &gemmlowp_output_stage,
311  int gemm_3d_depth, bool skip_im2col, const ActivationLayerInfo &act_info);
312 
313 private:
314  MemoryGroup _memory_group;
315  IWeightsManager *_weights_manager;
316  CLConvolutionLayerReshapeWeights _reshape_weights;
318  std::unique_ptr<CLIm2ColKernel> _im2col_kernel;
319  CLGEMM _mm_gemm;
320  CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
321  std::unique_ptr<CLCol2ImKernel> _col2im_kernel;
322  CLActivationLayer _activationlayer_function;
323 
324  const ICLTensor *_original_weights;
325 
326  CLTensor _im2col_output;
327  CLTensor _weights_reshaped;
328  CLTensor _gemm_output;
329 
330  bool _skip_im2col;
331  bool _skip_col2im;
332  bool _is_quantized;
333  bool _fuse_activation;
334  bool _is_prepared;
335 };
336 } // namespace arm_compute
337 #endif /* ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H */
CLGEMMConvolutionLayer & operator=(const CLGEMMConvolutionLayer &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
Base class for all functions.
Definition: IFunction.h:30
Basic function to compute the convolution layer.
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info=WeightsInfo(), const Size2D &dilation=Size2D(1U, 1U), const ActivationLayerInfo &act_info=ActivationLayerInfo(), unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of CLGEMMConvolutionLayer.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:61
Basic function to run opencl::kernels::ClActivationKernel.
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1478
Copyright (c) 2017-2021 Arm Limited.
Convolution Layer Weights Information class.
Definition: Types.h:1693
ICLTensor * get_weights() override
Get a pointer to the transformed weights.
CLGEMMConvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
Constructor.
CLConvolutionLayerReshapeWeights & operator=(const CLConvolutionLayerReshapeWeights &)=delete
Prevent instances of this class from being copied.
Basic function to execute GEMM on OpenCL.
Definition: CLGEMM.h:108
const unsigned int num_groups
Definition: Im2Col.cpp:153
GEMMLowp output stage info.
Definition: Types.h:1887
Padding and stride information class.
Definition: Types.h:650
void run() override
Run the kernels contained in the function.
Weights manager interface to handle weights transformations.
CLCompileContext class.
void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info=WeightsInfo(), const Size2D &dilation=Size2D(1U, 1U), const ActivationLayerInfo &act_info=ActivationLayerInfo(), unsigned int num_groups=1)
Set the input and output tensors.
Basic function to manage the reshape weights generated from CLConvolutionLayerReshapeWeights.
static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of CLConvolutionLayerReshap...
Function to reshape and transpose the weights.
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
void configure(const ICLTensor *input, const ICLTensor *biases, unsigned int num_groups)
Configures the CLConvolutionLayerReshapeWeights function.
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *biases, unsigned int num_groups)
Configures the CLConvolutionLayerReshapeWeights function.
void free() override
Free allocated OpenCL memory.
Weights tensor transform interface In order to identify the different reshape functions,...
~CLConvolutionLayerReshapeWeights()
Default destructor.
void prepare() override
Prepare the function for executing.
Basic function to execute GEMMLowpMatrixMultiplyCore on OpenCL.
void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups=1)
Set the input and output tensors.
uint32_t uid() override
Function that returns a unique id of the reshape function.
void run() override
Run the kernels contained in the function.
~CLGEMMConvolutionLayer()
Default destructor.
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41