blob: 2057b6ff8a18b1b108eb1af55849bee2a42e1e93 [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"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include "arm_compute/runtime/CL/CLTensor.h"
38
39namespace arm_compute
40{
41class ICLTensor;
42
43/** Function to reshape and transpose the weights. This function calls the following kernels:
44 * -# @ref CLWeightsReshapeKernel
45 * -# @ref CLGEMMTranspose1xWKernel
46 */
47class CLConvolutionLayerReshapeWeights : public IFunction
48{
49public:
50 /** Constructor */
51 CLConvolutionLayerReshapeWeights();
52 /** Set the input and output tensors.
53 *
Gian Marco Iodice7d323a62017-07-05 20:05:23 +010054 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
56 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
57 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
58 * Data types supported: Same as @p weights.
59 */
60 void configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW);
61 // Inherited methods overridden:
62 void run() override;
63
64private:
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010065 CLWeightsReshapeKernel _weights_reshape_kernel;
66 CLGEMMTranspose1xWKernel _weights_transposed_kernel;
67 CLTensor _weights_reshaped;
68 bool _transpose1xW;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069};
70
71/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels:
72 *
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010073 * -# @ref CLWeightsReshapeKernel (executed only once for each configuration)
74 * -# @ref CLGEMMTranspose1xWKernel (executed only once for each configuration)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 * -# @ref CLIm2ColKernel
76 * -# @ref CLGEMMInterleave4x4Kernel
77 * -# @ref CLGEMMMatrixMultiplyKernel
78 * -# @ref CLCol2ImKernel
79 */
80class CLConvolutionLayer : public IFunction
81{
82public:
83 /** Default constructor */
84 CLConvolutionLayer();
85 /** Set the input and output tensors.
86 *
87 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
88 * while every optional dimension from 4 and above represent a batch of inputs.
Gian Marco Iodice7d323a62017-07-05 20:05:23 +010089 * Data types supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
91 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported:Same as @p input.
92 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
93 * Data types supported: Same as @p input.
94 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
95 * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
96 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
97 */
98 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo());
99
100 // Inherited methods overridden:
101 void run() override;
102
103private:
104 CLConvolutionLayerReshapeWeights _reshape_weights;
105 CLIm2ColKernel _input_im2col_kernel;
106 CLGEMMInterleave4x4Kernel _input_interleave_kernel;
107 CLGEMMMatrixMultiplyKernel _mm_kernel;
108 CLCol2ImKernel _output_col2im_kernel;
109 CLTensor _input_im2col_reshaped;
110 CLTensor _input_interleaved_reshaped;
111 CLTensor _weights_reshaped;
112 CLTensor _weights_transposed;
113 CLTensor _gemm_output;
114 bool _has_bias;
115 bool _is_fully_connected_convolution;
116 bool _are_weights_reshaped;
117};
118}
119#endif /* __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ */