blob: ae9f894aab5b2cfa044a05e05c5787c070fa1905 [file] [log] [blame]
Manuel Bottinib4bb6a02021-05-24 16:01:32 +01001/*
2 * Copyright (c) 2021 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_CPU_DEPTHWISECONV2D_H
25#define ARM_COMPUTE_CPU_DEPTHWISECONV2D_H
26
27#include "arm_compute/core/ITensorInfo.h"
28#include "arm_compute/core/experimental/Types.h"
29#include "src/core/cpu/ICpuKernel.h"
30#include "src/core/cpu/kernels/CpuDepthwiseConv2dNativeKernel.h"
31#include "src/runtime/cpu/ICpuOperator.h"
32#include "src/runtime/cpu/operators/CpuActivation.h"
33#include "src/runtime/cpu/operators/CpuDepthwiseConv2dAssemblyDispatch.h"
34#include "src/runtime/cpu/operators/CpuPermute.h"
35
36#include <memory>
37
38namespace arm_compute
39{
40namespace cpu
41{
42/** Function to execute a depthwise convolution.
43 */
44class CpuDepthwiseConv2d : public ICpuOperator
45{
46public:
47 /** Default constructor */
48 CpuDepthwiseConv2d();
49 /** Initialize the function's source, destination, weights and convolution information.
50 *
51 * @param[in, out] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32
52 * @param[out] dst Destination tensor info. Data type supported: same as @p src.
53 * @param[in] weights Weights tensor info. These are 3D tensor infos with shape [kernel_x, kernel_y, IFM].
54 * Data type supported: Same as @p src or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p src is QASYMM8/QASYMM8_SIGNED.
55 * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
56 * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED.
57 * @param[in] info Depthwise convolution meta-data.
58 */
59 void configure(ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ConvolutionInfo &info);
60
61 /** Static function to check if given info will lead to a valid configuration
62 *
63 * Similar to CpuDepthwiseConv2d::configure()
64 *
65 * @return a status
66 */
67 static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info);
68
69 /** Static function to choose the best depthwise convolution function for @ref CpuDepthwiseConv2d
70 *
71 * @param[in] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32
72 * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
73 * Data type supported: Same as @p src or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p src is QASYMM8/QASYMM8_SIGNED.
74 * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
75 * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED.
76 * @param[in] dst Destination tensor. Data type supported: same as @p src.
77 * @param[in] info Depthwise convolution meta-data.
78 *
79 * @return a Depthwise Convolution Function
80 */
81 static DepthwiseConvolutionFunction get_depthwiseconvolution_function(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
82 const ConvolutionInfo &info);
83
84 // Inherited methods overriden:
85 void run(ITensorPack &tensors) override;
86 void prepare(ITensorPack &tensors) override;
87
88private:
89 /** Basic function to execute optimized depthwise convolution routines. This function calls the following kernels:
90 *
91 * @note At the moment 3x3 and 5x5 convolution of stride 1, 2 are supported
92 *
93 * -# @ref NEFillBorderKernel (if pad_x or pad_y > 0) and no assembly kernel implementation is present
94 * -# @ref CpuDepthwiseConv2d3x3Kernel if 3x3 and no assembly kernel implementation is present
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000095 * -# @ref CpuDepthwiseConv2dAssemblyDispatch if assembly kernel implementation is present
96 * -# @ref CpuActivation if fused activation is required
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010097 *
98 */
99 class CpuDepthwiseConv2dOptimizedInternal : public ICpuOperator
100 {
101 public:
102 /** Default constructor */
103 CpuDepthwiseConv2dOptimizedInternal();
104 /** Prevent instances of this class from being copied (As this class contains pointers) */
105 CpuDepthwiseConv2dOptimizedInternal(const CpuDepthwiseConv2dOptimizedInternal &) = delete;
106 /** Default move constructor */
107 CpuDepthwiseConv2dOptimizedInternal(CpuDepthwiseConv2dOptimizedInternal &&) = default;
108 /** Prevent instances of this class from being copied (As this class contains pointers) */
109 CpuDepthwiseConv2dOptimizedInternal &operator=(const CpuDepthwiseConv2dOptimizedInternal &) = delete;
110 /** Default move assignment operator */
111 CpuDepthwiseConv2dOptimizedInternal &operator=(CpuDepthwiseConv2dOptimizedInternal &&) = default;
112 /** Default destructor */
113 ~CpuDepthwiseConv2dOptimizedInternal() = default;
114 /** Initialize the function's source, destination, kernels and border_size.
115 *
116 * @param[in, out] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling).
117 * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p src.
118 * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
119 * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED.
120 * @param[out] dst Destination tensor info. Data type supported: same as @p src.
121 * @param[in] info Depthwise convolution meta-data.
122 */
123 void configure(ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ConvolutionInfo &info);
124
125 /** Static function to check if given info will lead to a valid configuration
126 *
127 * Similar to CpuDepthwiseConv2dOptimizedInternal::configure()
128 *
129 * @return a status
130 */
131 static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info);
132
133 // Inherited methods overriden:
134 void run(ITensorPack &tensors) override;
135 void prepare(ITensorPack &tensors) override;
136
137 private:
138 std::unique_ptr<CpuDepthwiseConv2dAssemblyDispatch> _dwc_optimized_func{ nullptr };
139 std::unique_ptr<CpuPermute> _permute_input{ nullptr };
140 std::unique_ptr<CpuPermute> _permute_weights{ nullptr };
141 std::unique_ptr<CpuPermute> _permute_output{ nullptr };
142 std::unique_ptr<CpuActivation> _activationlayer_function{ nullptr };
143 bool _has_bias{ false };
144 bool _is_quantized{ false };
145 bool _is_nchw{ true };
146 bool _permute{ false };
147 bool _is_activationlayer_enabled{ false };
148 bool _is_prepared{ false };
149 };
150
151 /** Basic function to execute a generic depthwise convolution. This function calls the following kernel:
152 *
153 * -# @ref CpuDepthwiseConv2dNativeKernel
154 *
155 */
156 class CpuDepthwiseConv2dGeneric : public ICpuOperator
157 {
158 public:
159 /** Default constructor */
160 CpuDepthwiseConv2dGeneric();
161 /** Prevent instances of this class from being copied (As this class contains pointers) */
162 CpuDepthwiseConv2dGeneric(const CpuDepthwiseConv2dGeneric &) = delete;
163 /** Default move constructor */
164 CpuDepthwiseConv2dGeneric(CpuDepthwiseConv2dGeneric &&) = default;
165 /** Prevent instances of this class from being copied (As this class contains pointers) */
166 CpuDepthwiseConv2dGeneric &operator=(const CpuDepthwiseConv2dGeneric &) = delete;
167 /** Default move assignment operator */
168 CpuDepthwiseConv2dGeneric &operator=(CpuDepthwiseConv2dGeneric &&) = default;
169 /** Default destructor */
170 ~CpuDepthwiseConv2dGeneric() = default;
171 /** Initialize the function's source, destination, weights and convolution information.
172 *
173 * @param[in, out] src Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling).
174 * @param[out] dst Destination tensor info. Data type supported: same as @p src.
175 * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
176 * Data type supported: Same as @p src or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p src is QASYMM8/QASYMM8_SIGNED.
177 * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
178 * Data type supported: Same as @p src, S32 when src is QASYMM8/QASYMM8_SIGNED.
179 * @param[in] info Depthwise convolution meta-data.
180 */
181 void configure(ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, ITensorInfo *dst, const ConvolutionInfo &info);
182
183 /** Static function to check if given info will lead to a valid configuration
184 *
185 * Similar to CpuDepthwiseConv2dGeneric::configure()
186 *
187 * @return a status
188 */
189 static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, const ConvolutionInfo &info);
190
191 // Inherited methods overridden:
192 void run(ITensorPack &tensors) override;
193 void prepare(ITensorPack &tensors) override;
194
195 private:
196 std::unique_ptr<kernels::CpuDepthwiseConv2dNativeKernel> _depthwise_conv_kernel{ nullptr };
197 std::unique_ptr<CpuPermute> _permute_input{ nullptr };
198 std::unique_ptr<CpuPermute> _permute_weights{ nullptr };
199 std::unique_ptr<CpuPermute> _permute_output{ nullptr };
200 std::unique_ptr<CpuActivation> _activationlayer_function{ nullptr };
201 bool _is_nchw{ true };
202 bool _is_prepared{ false };
203 bool _is_activationlayer_enabled{ false };
204 };
205
206 DepthwiseConvolutionFunction _depth_conv_func;
207 CpuDepthwiseConv2dOptimizedInternal _func_optimized;
208 CpuDepthwiseConv2dGeneric _func_generic;
209};
210} // namespace cpu
211} // namespace arm_compute
212#endif /* ARM_COMPUTE_CPU_DEPTHWISECONV2D_H */