blob: 3fe6604db939c0e7530250771a5b66f0363fa249 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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_CLCONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__
26
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/CL/kernels/CLCol2ImKernel.h"
30#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
31#include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
32#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
33#include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h"
34#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
35#include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h"
36#include "arm_compute/core/Types.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010037#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038#include "arm_compute/runtime/CL/CLTensor.h"
Chunosov5124be52017-11-22 20:42:13 +070039#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
40#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010041#include "arm_compute/runtime/IMemoryManager.h"
42
43#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45namespace arm_compute
46{
47class ICLTensor;
48
49/** Function to reshape and transpose the weights. This function calls the following kernels:
50 * -# @ref CLWeightsReshapeKernel
51 * -# @ref CLGEMMTranspose1xWKernel
52 */
53class CLConvolutionLayerReshapeWeights : public IFunction
54{
55public:
56 /** Constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010057 CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 /** Set the input and output tensors.
59 *
Chunosov5124be52017-11-22 20:42:13 +070060 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
61 * Data type supported: QS8/QASYMM8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
63 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
64 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
65 * Data types supported: Same as @p weights.
66 */
67 void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW);
68 // Inherited methods overridden:
69 void run() override;
70
71private:
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010072 CLMemoryGroup _memory_group;
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010073 CLWeightsReshapeKernel _weights_reshape_kernel;
74 CLGEMMTranspose1xWKernel _weights_transposed_kernel;
75 CLTensor _weights_reshaped;
76 bool _transpose1xW;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077};
78
79/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels:
80 *
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010081 * -# @ref CLWeightsReshapeKernel (executed only once for each configuration)
82 * -# @ref CLGEMMTranspose1xWKernel (executed only once for each configuration)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 * -# @ref CLIm2ColKernel
84 * -# @ref CLGEMMInterleave4x4Kernel
Chunosov5124be52017-11-22 20:42:13 +070085 * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
86 * -# @ref CLGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 * -# @ref CLCol2ImKernel
88 */
89class CLConvolutionLayer : public IFunction
90{
91public:
92 /** Default constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010093 CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 /** Set the input and output tensors.
95 *
96 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
97 * while every optional dimension from 4 and above represent a batch of inputs.
Chunosov5124be52017-11-22 20:42:13 +070098 * Data types supported: QS8/QASYMM8/QS16/F16/F32.
99 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
100 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
101 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
103 * Data types supported: Same as @p input.
104 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Giorgio Arena39725282017-12-12 15:04:43 +0000105 * @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
106 * tensor has also been transposed with CLGEMMTranspose1xWKernel. Data type supported: Same as @p input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 */
108 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo());
109
110 // Inherited methods overridden:
111 void run() override;
112
113private:
Chunosov5124be52017-11-22 20:42:13 +0700114 /** Configures the appropriate matrix multiply routine
115 *
116 * @param input Input tensor. Data types supported: QS8/QASYMM8/QS16/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,
119 * except for input of QASYMM8 type where output should be of S32 type.
120 * @param is_interleaved_transposed Flag that signals if matrix is interleaved transposed
121 */
122 void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool is_interleaved_transposed = true);
123
124private:
125 CLMemoryGroup _memory_group;
126 CLConvolutionLayerReshapeWeights _reshape_weights;
127 CLIm2ColKernel _input_im2col_kernel;
128 CLGEMMInterleave4x4Kernel _input_interleave_kernel;
129 CLGEMMMatrixMultiplyKernel _mm_kernel;
130 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
131 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
132 CLCol2ImKernel _output_col2im_kernel;
133
134 CLTensor _input_im2col_reshaped;
135 CLTensor _input_interleaved_reshaped;
136 CLTensor _weights_reshaped;
137 CLTensor _weights_transposed;
138 CLTensor _gemm_output;
139 CLTensor _tmp_output;
140
Chunosov5124be52017-11-22 20:42:13 +0700141 bool _are_weights_reshaped;
142 bool _is_quantized;
Gian Marco1d25ed52017-12-16 19:33:50 +0000143 bool _is_interleaved_transposed;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144};
145}
146#endif /* __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ */