• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-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  */
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"
30 #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"
34 #include "arm_compute/runtime/GLES_COMPUTE/functions/GCActivationLayer.h"
35 #include "arm_compute/runtime/GLES_COMPUTE/functions/GCGEMM.h"
36 #include "arm_compute/runtime/IFunction.h"
37 #include "arm_compute/runtime/MemoryGroup.h"
38 
39 #include <memory>
40 
41 namespace arm_compute
42 {
43 class IGCTensor;
44 
45 /** Function to reshape and transpose the weights. This function calls the following kernels:
46  * -# @ref GCWeightsReshapeKernel
47  *
48  * @deprecated This function is deprecated and is intended to be removed in 21.05 release
49  *
50  */
51 class GCConvolutionLayerReshapeWeights : public IFunction
52 {
53 public:
54     /** Constructor */
55     GCConvolutionLayerReshapeWeights();
56     /** Set the input and output tensors.
57      *
58      * @param[in]  weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
59      *                     Data type supported: F16/F32.
60      * @param[in]  biases  Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
61      * @param[out] output  Destination tensor. Data types supported: Same as @p weights.
62      */
63     void configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output);
64     // Inherited methods overridden:
65     void run() override;
66 
67 private:
68     GCWeightsReshapeKernel _weights_reshape_kernel;
69 };
70 
71 /** Basic function to compute the convolution layer. This function calls the following GLES kernels:
72  *
73  * -# @ref GCWeightsReshapeKernel (executed only once for each configuration)
74  * -# @ref GCGEMMTranspose1xWKernel (executed only once for each configuration)
75  * -# @ref GCIm2ColKernel
76  * -# @ref GCGEMMInterleave4x4Kernel
77  * -# @ref GCCol2ImKernel
78  *
79  * @deprecated This function is deprecated and is intended to be removed in 21.05 release
80  *
81  */
82 class GCConvolutionLayer : public IFunction
83 {
84 public:
85     /** Default constructor */
86     GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
87     /** Prevent instances of this class from being copied (As this class contains pointers) */
88     GCConvolutionLayer(const GCConvolutionLayer &) = delete;
89     /** Default move constructor */
90     GCConvolutionLayer(GCConvolutionLayer &&) = default;
91     /** Prevent instances of this class from being copied (As this class contains pointers) */
92     GCConvolutionLayer &operator=(const GCConvolutionLayer &) = delete;
93     /** Default move assignment operator */
94     GCConvolutionLayer &operator=(GCConvolutionLayer &&) = default;
95     /** Set the input and output tensors.
96      *
97      * @param[in]  input        Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
98      *                          while every optional dimension from 4 and above represent a batch of inputs.
99      *                          Data types supported: F16/F32.
100      * @param[in]  weights      Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
101      * @param[in]  biases       Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
102      *                          Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
103      * @param[out] output       Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
104      *                          Data types supported: Same as @p input.
105      * @param[in]  conv_info    Contains padding and stride information described in @ref PadStrideInfo.
106      * @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
107      *                          tensor has also been transposed with GCGEMMTranspose1xWKernel. Data type supported: Same as @p input.
108      * @param[in]  dilation     (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
109      * @param[in]  act_info     (Optional) Activation layer information in case of a fused activation.
110      * @param[in]  num_groups   (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
111      */
112     void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info,
113                    const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), unsigned int num_groups = 1);
114 
115     // Inherited methods overridden:
116     void run() override;
117     void prepare() override;
118 
119 private:
120     /** Configures the appropriate matrix multiply routine
121      *
122      * @param input                     Input tensor. Data types supported: F16/F32.
123      * @param weights                   Weights tensor. Data type supported: Same as @p input.
124      * @param output                    Output tensor. Data types supported: Same as @p input,
125      */
126     void configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
127     /** Static function to check if given info will lead to a valid configuration of @ref GCGEMMConvolutionLayer matrix multiply routines
128      *
129      * @param[in] input   Input tensor. Data types supported: QASYMM8/F16/F32.
130      * @param[in] weights Weights tensor. Data type supported: Same as @p input.
131      * @param[in] output  Output tensor. Data types supported: Same as @p input,
132      *                                      except for input of QASYMM8 type where output should be of S32 type.
133      *
134      * @return a status
135      */
136     static Status validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output);
137 
138 private:
139     MemoryGroup                      _memory_group;
140     GCConvolutionLayerReshapeWeights _reshape_weights;
141     GCIm2ColKernel                   _input_im2col_kernel;
142     GCGEMM                           _mm_gemm;
143     GCCol2ImKernel                   _output_col2im_kernel;
144     GCFillBorderKernel               _fill_border;
145     GCActivationLayer                _activationlayer_function;
146 
147     const IGCTensor *_original_weights;
148 
149     GCTensor _input_im2col_reshaped;
150     GCTensor _input_interleaved_reshaped;
151     GCTensor _weights_reshaped;
152     GCTensor _weights_transposed;
153     GCTensor _gemm_output;
154     GCTensor _tmp_output;
155 
156     bool _is_activationlayer_enabled;
157     bool _is_prepared;
158 };
159 }
160 
161 #endif /* ARM_COMPUTE_GCCONVOLUTIONLAYER_H */
162