blob: 9918a61cab268835e8743b8e26f9bcd6418341b7 [file] [log] [blame]
Isabella Gottardif07d28d2018-02-06 14:52:43 +00001/*
Teresa Charlin62687422021-04-28 10:58:49 +01002 * Copyright (c) 2017-2021 Arm Limited.
Isabella Gottardif07d28d2018-02-06 14:52:43 +00003 *
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 */
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010024#ifndef ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
25#define ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H
Isabella Gottardif07d28d2018-02-06 14:52:43 +000026
SiCongLi579ca842021-10-18 09:38:33 +010027#include "arm_compute/core/experimental/IPostOp.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000028#include "arm_compute/runtime/CL/CLTensor.h"
Manuel Bottinid87aded2021-07-16 10:23:31 +010029#include "arm_compute/runtime/CL/CLTypes.h"
30#include "arm_compute/runtime/IFunction.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000031#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010032#include "arm_compute/runtime/IWeightsManager.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000033
34#include <memory>
35
36namespace arm_compute
37{
Manuel Bottinid87aded2021-07-16 10:23:31 +010038// Forward declarations
39class CLCompileContext;
Manuel Bottinid844c082021-07-14 12:58:54 +010040class ICLTensor;
Manuel Bottinid87aded2021-07-16 10:23:31 +010041class ITensorInfo;
Michalis Spyroub27e13a2019-09-27 11:04:27 +010042
Isabella Gottardif07d28d2018-02-06 14:52:43 +000043/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
44 *
Georgios Pinitas19884632021-08-16 12:38:54 +010045 * -# @ref opencl::ClGemmConv2d
Isabella Gottardif07d28d2018-02-06 14:52:43 +000046 */
47class CLGEMMConvolutionLayer : public IFunction
48{
49public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +010050 /** Constructor
Alex Gildayc357c472018-03-21 13:54:09 +000051 *
Michalis Spyroub27e13a2019-09-27 11:04:27 +010052 * @param[in] memory_manager (Optional) Memory manager.
53 * @param[in] weights_manager (Optional) Weights manager.
Alex Gildayc357c472018-03-21 13:54:09 +000054 */
Michalis Spyroub27e13a2019-09-27 11:04:27 +010055 CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +000056 /** Prevent instances of this class from being copied (As this class contains pointers) */
57 CLGEMMConvolutionLayer(const CLGEMMConvolutionLayer &) = delete;
58 /** Default move constructor */
59 CLGEMMConvolutionLayer(CLGEMMConvolutionLayer &&) = default;
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 CLGEMMConvolutionLayer &operator=(const CLGEMMConvolutionLayer &) = delete;
62 /** Default move assignment operator */
63 CLGEMMConvolutionLayer &operator=(CLGEMMConvolutionLayer &&) = default;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010064 /**Default destructor */
65 ~CLGEMMConvolutionLayer();
Isabella Gottardif07d28d2018-02-06 14:52:43 +000066 /** Set the input and output tensors.
67 *
Teresa Charlin62687422021-04-28 10:58:49 +010068 * Valid data layouts:
69 * - NHWC
70 * - NCHW
71 *
72 * Valid data type configurations:
73 * |src0 |src1 |src2 |dst |
74 * |:--------------|:------------------|:--------|:--------------|
75 * |F16 |F16 |F16 |F16 |
76 * |F32 |F32 |F32 |F32 |
77 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
78 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
79 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
80 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
81 *
Isabella Gottardif07d28d2018-02-06 14:52:43 +000082 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
83 * while every optional dimension from 4 and above represent a batch of inputs.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010084 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000085 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010086 * 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.
Isabella Gottardif07d28d2018-02-06 14:52:43 +000087 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Michele Di Giorgiof6f78762020-07-06 11:27:21 +010088 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
Isabella Gottardif07d28d2018-02-06 14:52:43 +000089 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
90 * Data types supported: Same as @p input.
91 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
92 * @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
Gian Marco Iodice5fc07aa2019-05-15 17:08:02 +010093 * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +000094 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000095 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010096 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
SiCongLi579ca842021-10-18 09:38:33 +010097 * @param[in] post_ops (Optional) A sequence of post operations that are performed after the main operation.
Isabella Gottardif07d28d2018-02-06 14:52:43 +000098 */
Alex Gilday7da29b62018-03-23 14:16:00 +000099 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
SiCongLi579ca842021-10-18 09:38:33 +0100100 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1,
101 const experimental::PostOpList<ICLTensor *> &post_ops = experimental::PostOpList<ICLTensor *> {});
Manuel Bottini2b84be52020-04-08 10:15:51 +0100102 /** Set the input and output tensors.
103 *
104 * @param[in] compile_context The compile context to be used.
105 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
106 * while every optional dimension from 4 and above represent a batch of inputs.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100107 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100108 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100109 * 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.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100110 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100111 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100112 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
113 * Data types supported: Same as @p input.
114 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
115 * @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
116 * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
117 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
118 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
119 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
SiCongLi579ca842021-10-18 09:38:33 +0100120 * @param[in] post_ops (Optional) A sequence of post operations that are performed after the main operation.
Manuel Bottini2b84be52020-04-08 10:15:51 +0100121 */
122 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
123 const WeightsInfo &weights_info = WeightsInfo(),
SiCongLi579ca842021-10-18 09:38:33 +0100124 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1,
125 const experimental::PostOpList<ICLTensor *> &post_ops = experimental::PostOpList<ICLTensor *> {});
Georgios Pinitas78c00902018-01-09 17:33:11 +0000126 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMConvolutionLayer.
127 *
128 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
129 * while every optional dimension from 4 and above represent a batch of inputs.
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100130 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000131 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100132 * 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.
Georgios Pinitas78c00902018-01-09 17:33:11 +0000133 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100134 * Data type supported: Should match @p input data type, except for input of quantized type where biases should be of S32 type.
Georgios Pinitas78c00902018-01-09 17:33:11 +0000135 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
136 * Data types supported: Same as @p input.
137 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
138 * @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
Gian Marco Iodice5fc07aa2019-05-15 17:08:02 +0100139 * tensor has also been transposed with CLGEMMReshapeRHSMatrixKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000140 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000141 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100142 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
SiCongLi579ca842021-10-18 09:38:33 +0100143 * @param[in] post_ops (Optional) A sequence of post operations that are performed after the main operation.
Alex Gildayc357c472018-03-21 13:54:09 +0000144 *
145 * @return a status
Georgios Pinitas78c00902018-01-09 17:33:11 +0000146 */
147 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
SiCongLi579ca842021-10-18 09:38:33 +0100148 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1,
149 const experimental::PostOpList<ITensorInfo *> &post_ops = experimental::PostOpList<ITensorInfo *> {});
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000150
151 // Inherited methods overridden:
152 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100153 void prepare() override;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000154
155private:
Manuel Bottinid87aded2021-07-16 10:23:31 +0100156 struct Impl;
157 std::unique_ptr<Impl> _impl;
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000158};
Georgios Pinitas19ea4192018-06-19 13:09:53 +0100159} // namespace arm_compute
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100160#endif /* ARM_COMPUTE_CLGEMMCONVOLUTIONLAYER_H */