blob: a79f21f3aca10b8093ff2e7b649d4ce4f22f29dd [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_NECONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_NECONVOLUTIONLAYER_H__
26
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/NEON/kernels/NECol2ImKernel.h"
30#include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010031#include "arm_compute/core/NEON/kernels/NEGEMMAssemblyBaseKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
33#include "arm_compute/core/NEON/kernels/NEGEMMMatrixMultiplyKernel.h"
34#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
35#include "arm_compute/core/NEON/kernels/NEIm2ColKernel.h"
36#include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h"
37#include "arm_compute/core/Types.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010038#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039#include "arm_compute/runtime/Tensor.h"
40
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010041#include <memory>
42
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043namespace arm_compute
44{
45class ITensor;
46
47/** Function to reshape and perform 1xW transposition on the weights. This function calls the following kernels:
48 * -# @ref NEWeightsReshapeKernel
49 * -# @ref NEGEMMTranspose1xWKernel (executed in case GEMM is required for the operation)
50 */
51class NEConvolutionLayerReshapeWeights : public IFunction
52{
53public:
54 /** Constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010055 NEConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 /** Set the input and output tensors.
57 *
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +010058 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QS16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
60 * @param[out] output Destination tensor. Data types supported: Same as @p weights.
61 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
62 * Data types supported: Same as @p weights.
63 */
64 void configure(const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose1xW);
Giorgio Arena7c23ad02017-11-30 15:08:38 +000065 /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayerReshapeWeights
66 *
67 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QS16/F16/F32.
68 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p weights.
69 * @param[in] output Destination tensor. Data types supported: Same as @p weights.
70 * @param[in] transpose1xW True if the weights are to undergo a 1xW transposition after reshaping (in case of GEMM operation), false otherwise.
71 * Data types supported: Same as @p weights.
72 *
73 * @return an error status
74 */
75 static Status validate(const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose1xW);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010076
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 // Inherited methods overridden:
78 void run() override;
79
80private:
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010081 MemoryGroup _memory_group;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 NEWeightsReshapeKernel _weights_reshape_kernel;
83 NEGEMMTranspose1xWKernel _weights_transposed_kernel;
84 Tensor _weights_reshaped;
85 bool _transpose1xW;
86};
87
88/** Basic function to simulate a convolution layer. This function calls the following NEON kernels:
89 * -# @ref NEWeightsReshapeKernel (executed only once for each configuration)
90 * -# @ref NEIm2ColKernel
91 * -# @ref NEGEMMInterleave4x4Kernel (executed only in case GEMM is required for the operation)
92 * -# @ref NEGEMMMatrixMultiplyKernel
93 * -# @ref NECol2ImKernel
94 */
95class NEConvolutionLayer : public IFunction
96{
97public:
98 /** Constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010099 NEConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100100
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 /** Set the input and output tensors.
102 *
103 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
104 * while every optional dimension from 4 and above represent a batch of inputs.
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100105 * Data types supported: QS8/QS16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
107 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
108 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
109 * Data types supported: Same as @p input.
110 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
111 * @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
112 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
113 */
114 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo());
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000115 /** Static function to check if given info will lead to a valid configuration of @ref NEConvolutionLayer
116 *
117 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
118 * while every optional dimension from 4 and above represent a batch of inputs.
119 * Data types supported: QS8/QS16/F16/F32.
120 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
121 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported:Same as @p input.
122 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
123 * Data types supported: Same as @p input.
124 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
125 * @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
126 * tensor has also been transposed with NEGEMMTranspose1xWKernel. Data type supported: Same as @p input.
127 *
128 * @return a status
129 */
130 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
131 const WeightsInfo &weights_info = WeightsInfo());
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100132
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 // Inherited methods overridden:
134 void run() override;
135
136private:
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100137 MemoryGroup _memory_group;
138 NEIm2ColKernel _input_im2col_kernel;
139 NEGEMMInterleave4x4Kernel _input_interleave_kernel;
140 NEConvolutionLayerReshapeWeights _reshape_weights;
141 NEGEMMMatrixMultiplyKernel _mm_kernel;
142 std::unique_ptr<NEGEMMAssemblyBaseKernel> _mm_optimised_kernel;
143 NECol2ImKernel _output_col2im_kernel;
144 Tensor _input_im2col_reshaped;
145 Tensor _input_interleaved_reshaped;
146 Tensor _weights_reshaped;
147 Tensor _gemm_output;
148 Tensor _workspace;
149 bool _has_bias;
150 bool _is_fully_connected_convolution;
151 bool _are_weights_reshaped;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152};
153}
154#endif /* __ARM_COMPUTE_NECONVOLUTIONLAYER_H__ */