blob: b7398f628a056ecf8b047cdfe63558c70e43cead [file] [log] [blame]
Michalis Spyrou7362f0d2017-10-18 17:58:22 +01001/*
Georgios Pinitasf72f9362018-01-12 16:29:45 +00002 * Copyright (c) 2017-2018 ARM Limited.
Michalis Spyrou7362f0d2017-10-18 17:58:22 +01003 *
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_NEDEPTHWISECONVOLUTION_H__
25#define __ARM_COMPUTE_NEDEPTHWISECONVOLUTION_H__
26
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000027#include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayer3x3Kernel.h"
Michalis Spyroub7b31532017-11-23 12:10:21 +000028#include "arm_compute/core/NEON/kernels/NEDepthwiseIm2ColKernel.h"
29#include "arm_compute/core/NEON/kernels/NEDepthwiseVectorToTensorKernel.h"
30#include "arm_compute/core/NEON/kernels/NEDepthwiseWeightsReshapeKernel.h"
Michalis Spyroub91e34c2017-12-20 15:50:55 +000031#include "arm_compute/core/NEON/kernels/NEDirectConvolutionLayerOutputStageKernel.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010032#include "arm_compute/core/NEON/kernels/NEFillBorderKernel.h"
Michalis Spyroub7b31532017-11-23 12:10:21 +000033#include "arm_compute/core/NEON/kernels/NEGEMMMatrixVectorMultiplyKernel.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010034#include "arm_compute/core/Types.h"
35#include "arm_compute/runtime/IFunction.h"
36#include "arm_compute/runtime/IMemoryManager.h"
37#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitas284cfe22018-02-13 12:15:13 +000038#include "arm_compute/runtime/NEON/functions/NEPermute.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010039#include "arm_compute/runtime/Tensor.h"
40
41namespace arm_compute
42{
43class ITensor;
44
45/** Basic function to execute a depthwise convolution for kernel size 3x3xC. This function calls the following NEON kernels:
46 *
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000047 * -# @ref NEDepthwiseConvolutionLayer3x3
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010048 * -# @ref NEFillBorderKernel (if pad_x or pad_y > 0)
49 *
50 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000051class NEDepthwiseConvolutionLayer3x3 : public IFunction
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010052{
53public:
54 /** Default constructor */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000055 NEDepthwiseConvolutionLayer3x3();
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010056 /** Initialize the function's source, destination, kernels and border_size.
57 *
Georgios Pinitas20c246a2018-09-12 16:45:53 +010058 * @param[in, out] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
Giorgio Arena76572242018-04-04 17:44:26 +010059 * @param[in] weights Weights tensor. These are 3D tensors with shape [3, 3, IFM]. Data type supported: Same as @p input.
60 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
61 * Data type supported: Same as @p input.
62 * @param[out] output Destination tensor. Data type supported: same as @p input.
63 * @param[in] conv_info Padding and stride information to use for the convolution.
64 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010065 */
Giorgio Arena76572242018-04-04 17:44:26 +010066 void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010067
Abe Mbise7784c832018-05-31 16:48:41 +010068 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayer3x3
69 *
Georgios Pinitas20c246a2018-09-12 16:45:53 +010070 * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
Abe Mbise7784c832018-05-31 16:48:41 +010071 * @param[in] weights Weights tensor. These are 3D tensors with shape [3, 3, IFM]. Data type supported: Same as @p input.
72 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
73 * Data type supported: Same as @p input.
74 * @param[in] output Destination tensor. Data type supported: same as @p input.
75 * @param[in] conv_info Padding and stride information to use for the convolution.
76 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
77 *
78 * @return a status
79 */
80 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1);
81
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010082 // Inherited methods overriden:
83 void run() override;
84
85private:
Georgios Pinitas4074c992018-01-30 18:13:46 +000086 NEDepthwiseConvolutionLayer3x3Kernel _dwc_kernel;
Michalis Spyroub91e34c2017-12-20 15:50:55 +000087 NEDirectConvolutionLayerOutputStageKernel _output_stage_kernel;
88 NEFillBorderKernel _border_handler;
Georgios Pinitas284cfe22018-02-13 12:15:13 +000089 NEPermute _permute_input;
90 NEPermute _permute_weights;
91 NEPermute _permute_output;
Georgios Pinitasf72f9362018-01-12 16:29:45 +000092 Tensor _accumulator;
Giorgio Arena26b22162018-08-13 15:49:49 +010093 Tensor _permuted_input;
94 Tensor _permuted_weights;
95 Tensor _permuted_output;
Michalis Spyroub91e34c2017-12-20 15:50:55 +000096 bool _has_bias;
Georgios Pinitasf72f9362018-01-12 16:29:45 +000097 bool _is_quantized;
Georgios Pinitas4074c992018-01-30 18:13:46 +000098 bool _is_optimized;
99 bool _are_weights_reshaped;
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100100 bool _is_nchw;
101 bool _is_first_run;
Giorgio Arena26b22162018-08-13 15:49:49 +0100102 bool _permute;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100103};
Michalis Spyroub7b31532017-11-23 12:10:21 +0000104
Giorgio Arena39725282017-12-12 15:04:43 +0000105/** Basic function to execute a generic depthwise convolution. This function calls the following NEON kernels:
Michalis Spyroub7b31532017-11-23 12:10:21 +0000106 *
107 * -# @ref NEDepthwiseIm2ColKernel
108 * -# @ref NEDepthwiseWeightsReshapeKernel
109 * -# @ref NEGEMMMatrixVectorMultiplyKernel
110 * -# @ref NEFillBorderKernel (if pad_x or pad_y > 0)
111 *
112 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000113class NEDepthwiseConvolutionLayer : public IFunction
Michalis Spyroub7b31532017-11-23 12:10:21 +0000114{
115public:
116 /** Default constructor */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000117 NEDepthwiseConvolutionLayer();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000118 /** Prevent instances of this class from being copied (As this class contains pointers) */
119 NEDepthwiseConvolutionLayer(const NEDepthwiseConvolutionLayer &) = delete;
120 /** Default move constructor */
121 NEDepthwiseConvolutionLayer(NEDepthwiseConvolutionLayer &&) = default;
122 /** Prevent instances of this class from being copied (As this class contains pointers) */
123 NEDepthwiseConvolutionLayer &operator=(const NEDepthwiseConvolutionLayer &) = delete;
124 /** Default move assignment operator */
125 NEDepthwiseConvolutionLayer &operator=(NEDepthwiseConvolutionLayer &&) = default;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000126 /** Initialize the function's source, destination, weights and convolution information.
127 *
Giorgio Arena76572242018-04-04 17:44:26 +0100128 * @param[in, out] input Source tensor. Data type supported: QASYMM8/F32. (Written to only for border filling).
129 * @param[out] output Destination tensor. Data type supported: same as @p input.
130 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
131 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
132 * Data type supported: Same as @p input, S32 when input is QASYMM8.
133 * @param[in] conv_info Padding and stride information to use for the convolution.
134 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
Michalis Spyroub7b31532017-11-23 12:10:21 +0000135 */
Giorgio Arena76572242018-04-04 17:44:26 +0100136 void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1);
Michalis Spyroub7b31532017-11-23 12:10:21 +0000137
Abe Mbise7784c832018-05-31 16:48:41 +0100138 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayer
139 *
140 * @param[in] input Source tensor. Data type supported: QASYMM8/F32. (Written to only for border filling).
141 * @param[in] output Destination tensor. Data type supported: same as @p input.
142 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
143 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
144 * Data type supported: Same as @p input, S32 when input is QASYMM8.
145 * @param[in] conv_info Padding and stride information to use for the convolution.
146 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
147 *
148 * @return a status
149 */
Georgios Pinitas10490202018-08-17 17:16:06 +0100150 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1);
Abe Mbise7784c832018-05-31 16:48:41 +0100151
Michalis Spyroub7b31532017-11-23 12:10:21 +0000152 // Inherited methods overriden:
153 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100154 void prepare() override;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000155
156private:
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000157 NEDepthwiseIm2ColKernel _im2col_kernel;
158 NEDepthwiseWeightsReshapeKernel _weights_reshape_kernel;
159 NEGEMMMatrixVectorMultiplyKernel _v2mm_kernel;
160 NEDepthwiseVectorToTensorKernel _vector_to_tensor_kernel;
161 NEDirectConvolutionLayerOutputStageKernel _output_stage_kernel;
162 NEFillBorderKernel _v2mm_input_fill_border;
163 NEFillBorderKernel _v2mm_weights_fill_border;
Giorgio Arena26b22162018-08-13 15:49:49 +0100164 NEPermute _permute_input;
165 NEPermute _permute_weights;
166 NEPermute _permute_output;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000167 Tensor _input_reshaped;
168 Tensor _weights_reshaped;
169 Tensor _v2mm_output;
170 Tensor _output_reshaped;
Giorgio Arena26b22162018-08-13 15:49:49 +0100171 Tensor _permuted_input;
172 Tensor _permuted_weights;
173 Tensor _permuted_output;
Georgios Pinitas72219332018-06-05 14:56:06 +0100174 bool _is_prepared;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000175 bool _is_quantized;
Giorgio Arena26b22162018-08-13 15:49:49 +0100176 bool _is_nhwc;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000177 const ITensor *_original_weights;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000178};
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100179}
180#endif /* __ARM_COMPUTE_NEDEPTHWISECONVOLUTION_H__ */