blob: 28f0560e93266aabe39ada6d9c3d15e9a699a6ca [file] [log] [blame]
Michalis Spyrou7362f0d2017-10-18 17:58:22 +01001/*
Georgios Pinitas47d39dc2019-03-11 14:03:23 +00002 * Copyright (c) 2017-2019 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 Pinitas60e98252018-10-22 16:17:20 +010038#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Georgios Pinitas284cfe22018-02-13 12:15:13 +000039#include "arm_compute/runtime/NEON/functions/NEPermute.h"
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000040#include "arm_compute/runtime/NEON/functions/assembly/NEDepthwiseConvolutionAssemblyDispatch.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010041#include "arm_compute/runtime/Tensor.h"
42
43namespace arm_compute
44{
45class ITensor;
46
47/** Basic function to execute a depthwise convolution for kernel size 3x3xC. This function calls the following NEON kernels:
48 *
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000049 * -# @ref NEDepthwiseConvolutionLayer3x3
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010050 * -# @ref NEFillBorderKernel (if pad_x or pad_y > 0)
51 *
52 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000053class NEDepthwiseConvolutionLayer3x3 : public IFunction
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010054{
55public:
56 /** Default constructor */
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000057 NEDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
58 /** Prevent instances of this class from being copied (As this class contains pointers) */
59 NEDepthwiseConvolutionLayer3x3(const NEDepthwiseConvolutionLayer3x3 &) = delete;
60 /** Default move constructor */
61 NEDepthwiseConvolutionLayer3x3(NEDepthwiseConvolutionLayer3x3 &&) = default;
62 /** Prevent instances of this class from being copied (As this class contains pointers) */
63 NEDepthwiseConvolutionLayer3x3 &operator=(const NEDepthwiseConvolutionLayer3x3 &) = delete;
64 /** Default move assignment operator */
65 NEDepthwiseConvolutionLayer3x3 &operator=(NEDepthwiseConvolutionLayer3x3 &&) = default;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010066 /** Initialize the function's source, destination, kernels and border_size.
67 *
Georgios Pinitas20c246a2018-09-12 16:45:53 +010068 * @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 +010069 * @param[in] weights Weights tensor. These are 3D tensors with shape [3, 3, IFM]. Data type supported: Same as @p input.
70 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
71 * Data type supported: Same as @p input.
72 * @param[out] output Destination tensor. Data type supported: same as @p input.
73 * @param[in] conv_info Padding and stride information to use for the convolution.
74 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
Georgios Pinitas60e98252018-10-22 16:17:20 +010075 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010076 */
Georgios Pinitas60e98252018-10-22 16:17:20 +010077 void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
78 unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010079
Abe Mbise7784c832018-05-31 16:48:41 +010080 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayer3x3
81 *
Georgios Pinitas20c246a2018-09-12 16:45:53 +010082 * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
Abe Mbise7784c832018-05-31 16:48:41 +010083 * @param[in] weights Weights tensor. These are 3D tensors with shape [3, 3, IFM]. Data type supported: Same as @p input.
84 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
85 * Data type supported: Same as @p input.
86 * @param[in] output Destination tensor. Data type supported: same as @p input.
87 * @param[in] conv_info Padding and stride information to use for the convolution.
88 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
Georgios Pinitas60e98252018-10-22 16:17:20 +010089 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Abe Mbise7784c832018-05-31 16:48:41 +010090 *
91 * @return a status
92 */
Georgios Pinitas60e98252018-10-22 16:17:20 +010093 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
94 unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Abe Mbise7784c832018-05-31 16:48:41 +010095
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010096 // Inherited methods overriden:
97 void run() override;
Georgios Pinitas47d39dc2019-03-11 14:03:23 +000098 void prepare() override;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010099
100private:
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000101 /** Configure the kernels/functions for the generic pipeline.
102 *
103 * @param[in, out] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
104 * @param[in] weights Weights tensor. These are 3D tensors with shape [3, 3, IFM]. Data type supported: Same as @p input.
105 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
106 * Data type supported: Same as @p input.
107 * @param[out] output Destination tensor. Data type supported: same as @p input.
108 * @param[in] conv_info Padding and stride information to use for the convolution.
109 * @param[in] depth_multiplier Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
110 * @param[in] act_info Activation layer information in case of a fused activation.
111 */
112 void configure_generic(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
113 unsigned int depth_multiplier, const ActivationLayerInfo &act_info);
114 /** Configure the kernels/functions for the optimized pipeline.
115 *
116 * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
117 * @param[in] weights Weights tensor. These are 3D tensors with shape [3, 3, IFM]. Data type supported: Same as @p input.
118 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
119 * Data type supported: Same as @p input.
120 * @param[out] output Destination tensor. Data type supported: same as @p input.
121 * @param[in] conv_info Padding and stride information to use for the convolution.
122 * @param[in] depth_multiplier Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
123 * @param[in] act_info Activation layer information in case of a fused activation.
124 */
125 void configure_optimized(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
126 unsigned int depth_multiplier, const ActivationLayerInfo &act_info);
127 /** Run generic kernel */
128 void run_generic();
129 /** Run optimized function */
130 void run_optimized();
131
132private:
133 MemoryGroup _memory_group;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000134 NEDepthwiseConvolutionLayer3x3Kernel _dwc_kernel;
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000135 NEDepthwiseConvolutionAssemblyDispatch _dwc_optimized_func;
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000136 NEDirectConvolutionLayerOutputStageKernel _output_stage_kernel;
137 NEFillBorderKernel _border_handler;
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000138 NEPermute _permute_input;
139 NEPermute _permute_weights;
140 NEPermute _permute_output;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100141 NEActivationLayer _activationlayer_function;
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000142 Tensor _accumulator;
Giorgio Arena26b22162018-08-13 15:49:49 +0100143 Tensor _permuted_input;
144 Tensor _permuted_weights;
145 Tensor _permuted_output;
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000146 const ITensor *_original_weights;
Michalis Spyroub91e34c2017-12-20 15:50:55 +0000147 bool _has_bias;
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000148 bool _is_quantized;
Georgios Pinitas4074c992018-01-30 18:13:46 +0000149 bool _is_optimized;
Giorgio Arena1ed1fc62018-03-26 16:20:05 +0100150 bool _is_nchw;
Giorgio Arena26b22162018-08-13 15:49:49 +0100151 bool _permute;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100152 bool _is_activationlayer_enabled;
Georgios Pinitas47d39dc2019-03-11 14:03:23 +0000153 bool _is_prepared;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100154};
Michalis Spyroub7b31532017-11-23 12:10:21 +0000155
Giorgio Arena39725282017-12-12 15:04:43 +0000156/** Basic function to execute a generic depthwise convolution. This function calls the following NEON kernels:
Michalis Spyroub7b31532017-11-23 12:10:21 +0000157 *
158 * -# @ref NEDepthwiseIm2ColKernel
159 * -# @ref NEDepthwiseWeightsReshapeKernel
160 * -# @ref NEGEMMMatrixVectorMultiplyKernel
161 * -# @ref NEFillBorderKernel (if pad_x or pad_y > 0)
162 *
163 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000164class NEDepthwiseConvolutionLayer : public IFunction
Michalis Spyroub7b31532017-11-23 12:10:21 +0000165{
166public:
167 /** Default constructor */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000168 NEDepthwiseConvolutionLayer();
Georgios Pinitas1562be32018-03-08 19:09:19 +0000169 /** Prevent instances of this class from being copied (As this class contains pointers) */
170 NEDepthwiseConvolutionLayer(const NEDepthwiseConvolutionLayer &) = delete;
171 /** Default move constructor */
172 NEDepthwiseConvolutionLayer(NEDepthwiseConvolutionLayer &&) = default;
173 /** Prevent instances of this class from being copied (As this class contains pointers) */
174 NEDepthwiseConvolutionLayer &operator=(const NEDepthwiseConvolutionLayer &) = delete;
175 /** Default move assignment operator */
176 NEDepthwiseConvolutionLayer &operator=(NEDepthwiseConvolutionLayer &&) = default;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000177 /** Initialize the function's source, destination, weights and convolution information.
178 *
Georgios Pinitas8cffcd62018-11-16 17:11:50 +0000179 * @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 +0100180 * @param[out] output Destination tensor. Data type supported: same as @p input.
181 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
182 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
183 * Data type supported: Same as @p input, S32 when input is QASYMM8.
184 * @param[in] conv_info Padding and stride information to use for the convolution.
185 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
Georgios Pinitas60e98252018-10-22 16:17:20 +0100186 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Michalis Spyroub7b31532017-11-23 12:10:21 +0000187 */
Georgios Pinitas60e98252018-10-22 16:17:20 +0100188 void configure(ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info,
189 unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Michalis Spyroub7b31532017-11-23 12:10:21 +0000190
Abe Mbise7784c832018-05-31 16:48:41 +0100191 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayer
192 *
Georgios Pinitas8cffcd62018-11-16 17:11:50 +0000193 * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
Abe Mbise7784c832018-05-31 16:48:41 +0100194 * @param[in] output Destination tensor. Data type supported: same as @p input.
195 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
196 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
197 * Data type supported: Same as @p input, S32 when input is QASYMM8.
198 * @param[in] conv_info Padding and stride information to use for the convolution.
199 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
Georgios Pinitas60e98252018-10-22 16:17:20 +0100200 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Abe Mbise7784c832018-05-31 16:48:41 +0100201 *
202 * @return a status
203 */
Georgios Pinitas60e98252018-10-22 16:17:20 +0100204 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
205 unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo());
Abe Mbise7784c832018-05-31 16:48:41 +0100206
Michalis Spyroub7b31532017-11-23 12:10:21 +0000207 // Inherited methods overriden:
208 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100209 void prepare() override;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000210
211private:
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000212 NEDepthwiseIm2ColKernel _im2col_kernel;
213 NEDepthwiseWeightsReshapeKernel _weights_reshape_kernel;
214 NEGEMMMatrixVectorMultiplyKernel _v2mm_kernel;
215 NEDepthwiseVectorToTensorKernel _vector_to_tensor_kernel;
216 NEDirectConvolutionLayerOutputStageKernel _output_stage_kernel;
217 NEFillBorderKernel _v2mm_input_fill_border;
218 NEFillBorderKernel _v2mm_weights_fill_border;
Giorgio Arena26b22162018-08-13 15:49:49 +0100219 NEPermute _permute_input;
220 NEPermute _permute_weights;
221 NEPermute _permute_output;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100222 NEActivationLayer _activationlayer_function;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000223 Tensor _input_reshaped;
224 Tensor _weights_reshaped;
225 Tensor _v2mm_output;
226 Tensor _output_reshaped;
Giorgio Arena26b22162018-08-13 15:49:49 +0100227 Tensor _permuted_input;
228 Tensor _permuted_weights;
229 Tensor _permuted_output;
Georgios Pinitas72219332018-06-05 14:56:06 +0100230 bool _is_prepared;
Georgios Pinitasd05dce42018-01-22 16:29:17 +0000231 bool _is_quantized;
Giorgio Arena26b22162018-08-13 15:49:49 +0100232 bool _is_nhwc;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100233 bool _is_activationlayer_enabled;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000234 const ITensor *_original_weights;
Michalis Spyroub7b31532017-11-23 12:10:21 +0000235};
Georgios Pinitas8cffcd62018-11-16 17:11:50 +0000236} // namespace arm_compute
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100237#endif /* __ARM_COMPUTE_NEDEPTHWISECONVOLUTION_H__ */