blob: 421150e18ee8c0a29199337448219eceb7be9f4a [file] [log] [blame]
Stephen Lie855c232018-01-04 14:13:22 +08001/*
2 * Copyright (c) 2017-2018 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
25#ifndef __ARM_COMPUTE_GCCONVOLUTIONLAYER_H__
26#define __ARM_COMPUTE_GCCONVOLUTIONLAYER_H__
27
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"
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000033#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
Stephen Lie855c232018-01-04 14:13:22 +080034#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000035#include "arm_compute/runtime/GLES_COMPUTE/functions/GCActivationLayer.h"
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010036#include "arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h"
Stephen Lie855c232018-01-04 14:13:22 +080037#include "arm_compute/runtime/IFunction.h"
38
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.
Stephen Lie855c232018-01-04 14:13:22 +0800104 */
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000105 void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info,
106 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo());
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100107 /** Static function to check if given info will lead to a valid configuration of @ref GCConvolutionLayer.
108 *
109 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
110 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100111 * Data types supported: QASYMM8/F16/F32.
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100112 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
113 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
114 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
115 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
116 * Data types supported: Same as @p input.
117 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
118 * @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
119 * tensor has also been transposed with GCGEMMTranspose1xWKernel. Data type supported: Same as @p input.
120 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
121 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
122 *
123 * @return a status
124 */
125 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
126 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo());
Stephen Lie855c232018-01-04 14:13:22 +0800127
128 // Inherited methods overridden:
129 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100130 void prepare() override;
Stephen Lie855c232018-01-04 14:13:22 +0800131
132private:
133 /** Configures the appropriate matrix multiply routine
134 *
135 * @param input Input tensor. Data types supported: F16/F32.
136 * @param weights Weights tensor. Data type supported: Same as @p input.
137 * @param output Output tensor. Data types supported: Same as @p input,
Stephen Lie855c232018-01-04 14:13:22 +0800138 */
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100139 void configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
140 /** Static function to check if given info will lead to a valid configuration of @ref GCGEMMConvolutionLayer matrix multiply routines
141 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100142 * @param[in] input Input tensor. Data types supported: QASYMM8/F16/F32.
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100143 * @param[in] weights Weights tensor. Data type supported: Same as @p input.
144 * @param[in] output Output tensor. Data types supported: Same as @p input,
145 * except for input of QASYMM8 type where output should be of S32 type.
146 *
147 * @return a status
148 */
149 static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output);
Stephen Lie855c232018-01-04 14:13:22 +0800150
151private:
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000152 GCMemoryGroup _memory_group;
Stephen Lie855c232018-01-04 14:13:22 +0800153 GCConvolutionLayerReshapeWeights _reshape_weights;
154 GCIm2ColKernel _input_im2col_kernel;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100155 GCGEMM _mm_gemm;
Stephen Lie855c232018-01-04 14:13:22 +0800156 GCCol2ImKernel _output_col2im_kernel;
157 GCFillBorderKernel _fill_border;
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000158 GCActivationLayer _activationlayer_function;
Stephen Lie855c232018-01-04 14:13:22 +0800159
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100160 const IGCTensor *_original_weights;
161
Stephen Lie855c232018-01-04 14:13:22 +0800162 GCTensor _input_im2col_reshaped;
163 GCTensor _input_interleaved_reshaped;
164 GCTensor _weights_reshaped;
165 GCTensor _weights_transposed;
166 GCTensor _gemm_output;
167 GCTensor _tmp_output;
168
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000169 bool _is_activationlayer_enabled;
Georgios Pinitas72219332018-06-05 14:56:06 +0100170 bool _is_prepared;
Stephen Lie855c232018-01-04 14:13:22 +0800171};
172}
173
174#endif /* __ARM_COMPUTE_GCCONVOLUTIONLAYER_H__ */