blob: 311b3e273b7fe7a9e59c01cebf3b8123bc18260e [file] [log] [blame]
Stephen Lie855c232018-01-04 14:13:22 +08001/*
Georgios Pinitas26014cf2019-09-09 19:00:57 +01002 * Copyright (c) 2017-2019 ARM Limited.
Stephen Lie855c232018-01-04 14:13:22 +08003 *
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
Michalis Spyrouf4643372019-11-29 16:17:13 +000025#ifndef ARM_COMPUTE_GCCONVOLUTIONLAYER_H
26#define ARM_COMPUTE_GCCONVOLUTIONLAYER_H
Stephen Lie855c232018-01-04 14:13:22 +080027
28#include "arm_compute/core/GLES_COMPUTE/kernels/GCCol2ImKernel.h"
29#include "arm_compute/core/GLES_COMPUTE/kernels/GCFillBorderKernel.h"
Stephen Lie855c232018-01-04 14:13:22 +080030#include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h"
31#include "arm_compute/core/GLES_COMPUTE/kernels/GCWeightsReshapeKernel.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000034#include "arm_compute/runtime/GLES_COMPUTE/functions/GCActivationLayer.h"
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010035#include "arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h"
Stephen Lie855c232018-01-04 14:13:22 +080036#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010037#include "arm_compute/runtime/MemoryGroup.h"
Stephen Lie855c232018-01-04 14:13:22 +080038
39#include <memory>
40
41namespace arm_compute
42{
43class IGCTensor;
44
45/** Function to reshape and transpose the weights. This function calls the following kernels:
46 * -# @ref GCWeightsReshapeKernel
Stephen Lie855c232018-01-04 14:13:22 +080047 */
48class GCConvolutionLayerReshapeWeights : public IFunction
49{
50public:
51 /** Constructor */
52 GCConvolutionLayerReshapeWeights();
53 /** Set the input and output tensors.
54 *
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010055 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
56 * Data type supported: F16/F32.
57 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
58 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
Stephen Lie855c232018-01-04 14:13:22 +080059 */
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010060 void configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output);
Stephen Lie855c232018-01-04 14:13:22 +080061 // Inherited methods overridden:
62 void run() override;
63
64private:
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010065 GCWeightsReshapeKernel _weights_reshape_kernel;
Stephen Lie855c232018-01-04 14:13:22 +080066};
67
68/** Basic function to compute the convolution layer. This function calls the following GLES kernels:
69 *
70 * -# @ref GCWeightsReshapeKernel (executed only once for each configuration)
71 * -# @ref GCGEMMTranspose1xWKernel (executed only once for each configuration)
72 * -# @ref GCIm2ColKernel
73 * -# @ref GCGEMMInterleave4x4Kernel
74 * -# @ref GCCol2ImKernel
75 */
76class GCConvolutionLayer : public IFunction
77{
78public:
79 /** Default constructor */
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000080 GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010081 /** Prevent instances of this class from being copied (As this class contains pointers) */
82 GCConvolutionLayer(const GCConvolutionLayer &) = delete;
83 /** Default move constructor */
84 GCConvolutionLayer(GCConvolutionLayer &&) = default;
85 /** Prevent instances of this class from being copied (As this class contains pointers) */
86 GCConvolutionLayer &operator=(const GCConvolutionLayer &) = delete;
87 /** Default move assignment operator */
88 GCConvolutionLayer &operator=(GCConvolutionLayer &&) = default;
Stephen Lie855c232018-01-04 14:13:22 +080089 /** Set the input and output tensors.
90 *
91 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
92 * while every optional dimension from 4 and above represent a batch of inputs.
93 * Data types supported: F16/F32.
94 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
95 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
96 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
97 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
98 * Data types supported: Same as @p input.
99 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
100 * @param[in] weights_info Specifies if the weights tensor has been reshaped with GCWeightsReshapeKernel. If this is not part of the fully connected layer the weights
101 * tensor has also been transposed with GCGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Alex Gilday7da29b62018-03-23 14:16:00 +0000102 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000103 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100104 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
Stephen Lie855c232018-01-04 14:13:22 +0800105 */
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000106 void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100107 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
Stephen Lie855c232018-01-04 14:13:22 +0800108
109 // Inherited methods overridden:
110 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100111 void prepare() override;
Stephen Lie855c232018-01-04 14:13:22 +0800112
113private:
114 /** Configures the appropriate matrix multiply routine
115 *
116 * @param input Input tensor. Data types supported: F16/F32.
117 * @param weights Weights tensor. Data type supported: Same as @p input.
118 * @param output Output tensor. Data types supported: Same as @p input,
Stephen Lie855c232018-01-04 14:13:22 +0800119 */
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100120 void configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
121 /** Static function to check if given info will lead to a valid configuration of @ref GCGEMMConvolutionLayer matrix multiply routines
122 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100123 * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32.
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100124 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
125 * @param[in] output Output tensor. Data types supported: Same as @p input,
126 * except for input of QASYMM8 type where output should be of S32 type.
127 *
128 * @return a status
129 */
130 static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output);
Stephen Lie855c232018-01-04 14:13:22 +0800131
132private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100133 MemoryGroup _memory_group;
Stephen Lie855c232018-01-04 14:13:22 +0800134 GCConvolutionLayerReshapeWeights _reshape_weights;
135 GCIm2ColKernel _input_im2col_kernel;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100136 GCGEMM _mm_gemm;
Stephen Lie855c232018-01-04 14:13:22 +0800137 GCCol2ImKernel _output_col2im_kernel;
138 GCFillBorderKernel _fill_border;
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000139 GCActivationLayer _activationlayer_function;
Stephen Lie855c232018-01-04 14:13:22 +0800140
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100141 const IGCTensor *_original_weights;
142
Stephen Lie855c232018-01-04 14:13:22 +0800143 GCTensor _input_im2col_reshaped;
144 GCTensor _input_interleaved_reshaped;
145 GCTensor _weights_reshaped;
146 GCTensor _weights_transposed;
147 GCTensor _gemm_output;
148 GCTensor _tmp_output;
149
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000150 bool _is_activationlayer_enabled;
Georgios Pinitas72219332018-06-05 14:56:06 +0100151 bool _is_prepared;
Stephen Lie855c232018-01-04 14:13:22 +0800152};
153}
154
Michalis Spyrouf4643372019-11-29 16:17:13 +0000155#endif /* ARM_COMPUTE_GCCONVOLUTIONLAYER_H */