Debian Codegraph @

arm_compute/runtime/NEON/functions/NEDepthwiseConvolutionLayer.h

raw
1 /*
2 * Copyright (c) 2017-2021, 2024 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 ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEDEPTHWISECONVOLUTIONLAYER_H
25 #define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEDEPTHWISECONVOLUTIONLAYER_H
26
27 #include "arm_compute/runtime/IMemoryManager.h"
28 #include "arm_compute/runtime/MemoryGroup.h"
29 #include "arm_compute/runtime/MemoryManagerOnDemand.h"
30 #include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
31 #include "arm_compute/runtime/NEON/functions/NEPermute.h"
32
33 #include <memory>
34
35 namespace arm_compute
36 {
37 // Forward declarations
38 class ITensor;
39 class NEDepthwiseConvolutionLayerNativeKernel;
40
41 /** Function to execute a depthwise convolution.
42 */
43 class NEDepthwiseConvolutionLayer : public IFunction
44 {
45 public:
46 /** Default constructor */
47 NEDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager);
48 NEDepthwiseConvolutionLayer() : NEDepthwiseConvolutionLayer(MemoryManagerOnDemand::make_default())
49 {
50 }
51 /** Prevent instances of this class from being copied (As this class contains pointers) */
52 NEDepthwiseConvolutionLayer(const NEDepthwiseConvolutionLayer &) = delete;
53 /** Default move constructor */
54 NEDepthwiseConvolutionLayer(NEDepthwiseConvolutionLayer &&) = default;
55 /** Prevent instances of this class from being copied (As this class contains pointers) */
56 NEDepthwiseConvolutionLayer &operator=(const NEDepthwiseConvolutionLayer &) = delete;
57 /** Default move assignment operator */
58 NEDepthwiseConvolutionLayer &operator=(NEDepthwiseConvolutionLayer &&) = default;
59 /** Default destructor */
60 ~NEDepthwiseConvolutionLayer();
61 /** Initialize the function's source, destination, weights and convolution information.
62 *
63 * Valid data layouts:
64 * - NHWC
65 * - NCHW
66 *
67 * Valid data type configurations:
68 * |src0 |src1 |src2 |dst |
69 * |:--------------|:------------------|:------|:--------------|
70 * |F16 |F16 |F16 |F16 |
71 * |F32 |F32 |F32 |F32 |
72 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
73 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
74 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
75 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
76 *
77 * @param[in, out] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32
78 * @param[out] output Destination tensor. Data type supported: same as @p input.
79 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
80 * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8/QASYMM8_SIGNED.
81 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
82 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
83 * @param[in] conv_info Padding and stride information to use for the convolution.
84 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
85 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
86 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
87 */
88 void configure(ITensor *input,
89 const ITensor *weights,
90 const ITensor *biases,
91 ITensor *output,
92 const PadStrideInfo &conv_info,
93 unsigned int depth_multiplier = 1,
94 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
95 const Size2D &dilation = Size2D(1U, 1U));
96
97 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayer
98 *
99 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32
100 * @param[in] output Destination tensor. Data type supported: same as @p input.
101 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
102 * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8/QASYMM8_SIGNED.
103 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
104 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
105 * @param[in] conv_info Padding and stride information to use for the convolution.
106 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
107 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
108 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
109 *
110 * @return a status
111 */
112 static Status validate(const ITensorInfo *input,
113 const ITensorInfo *weights,
114 const ITensorInfo *biases,
115 const ITensorInfo *output,
116 const PadStrideInfo &conv_info,
117 unsigned int depth_multiplier = 1,
118 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
119 const Size2D &dilation = Size2D(1U, 1U));
120
121 // Inherited methods overriden:
122 void run() override;
123 void prepare() override;
124
125 private:
126 /** Basic function to execute optimized depthwise convolution routines. This function calls the following kernels:
127 *
128 * @note At the moment 3x3 and 5x5 convolution of stride 1, 2 are supported
129 *
130 * -# NEFillBorderKernel (if pad_x or pad_y > 0) and no assembly kernel implementation is present
131 * -# NEDepthwiseConvolutionLayer3x3Kernel if 3x3 and no assembly kernel implementation is present
132 * -# cpu::CpuDepthwiseConvolutionAssemblyDispatch if assembly kernel implementation is present
133 * -# NEDirectConvolutionLayerOutputStageKernel if re-quantization of output is required
134 * -# @ref NEActivationLayer if fused activation is required
135 *
136 */
137 class NEDepthwiseConvolutionLayerOptimizedInternal : public IFunction
138 {
139 public:
140 /** Default constructor */
141 NEDepthwiseConvolutionLayerOptimizedInternal(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
142 /** Prevent instances of this class from being copied (As this class contains pointers) */
143 NEDepthwiseConvolutionLayerOptimizedInternal(const NEDepthwiseConvolutionLayerOptimizedInternal &) = delete;
144 /** Default move constructor */
145 NEDepthwiseConvolutionLayerOptimizedInternal(NEDepthwiseConvolutionLayerOptimizedInternal &&) = default;
146 /** Prevent instances of this class from being copied (As this class contains pointers) */
147 NEDepthwiseConvolutionLayerOptimizedInternal &
148 operator=(const NEDepthwiseConvolutionLayerOptimizedInternal &) = delete;
149 /** Default move assignment operator */
150 NEDepthwiseConvolutionLayerOptimizedInternal &
151 operator=(NEDepthwiseConvolutionLayerOptimizedInternal &&) = default;
152 /** Default destructor */
153 ~NEDepthwiseConvolutionLayerOptimizedInternal() = default;
154 /** Initialize the function's source, destination, kernels and border_size.
155 *
156 * @param[in, out] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling).
157 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
158 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
159 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
160 * @param[out] output Destination tensor. Data type supported: same as @p input.
161 * @param[in] conv_info Padding and stride information to use for the convolution.
162 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
163 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
164 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
165 */
166 void configure(ITensor *input,
167 const ITensor *weights,
168 const ITensor *biases,
169 ITensor *output,
170 const PadStrideInfo &conv_info,
171 unsigned int depth_multiplier = 1,
172 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
173 const Size2D &dilation = Size2D(1U, 1U));
174
175 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayer3x3
176 *
177 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling).
178 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
179 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
180 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
181 * @param[in] output Destination tensor. Data type supported: same as @p input.
182 * @param[in] conv_info Padding and stride information to use for the convolution.
183 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
184 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
185 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
186 *
187 * @return a status
188 */
189 static Status validate(const ITensorInfo *input,
190 const ITensorInfo *weights,
191 const ITensorInfo *biases,
192 const ITensorInfo *output,
193 const PadStrideInfo &conv_info,
194 unsigned int depth_multiplier = 1,
195 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
196 const Size2D &dilation = Size2D(1U, 1U));
197
198 // Inherited methods overriden:
199 void run() override;
200 void prepare() override;
201
202 private:
203 MemoryGroup _memory_group;
204 struct Impl;
205 std::unique_ptr<Impl> _impl;
206 };
207
208 /** Basic function to execute a generic depthwise convolution. This function calls the following kernel:
209 *
210 * -# @ref NEDepthwiseConvolutionLayerNativeKernel
211 *
212 */
213 class NEDepthwiseConvolutionLayerGeneric : public IFunction
214 {
215 public:
216 /** Default constructor */
217 NEDepthwiseConvolutionLayerGeneric();
218 /** Prevent instances of this class from being copied (As this class contains pointers) */
219 NEDepthwiseConvolutionLayerGeneric(const NEDepthwiseConvolutionLayerGeneric &) = delete;
220 /** Default move constructor */
221 NEDepthwiseConvolutionLayerGeneric(NEDepthwiseConvolutionLayerGeneric &&) = default;
222 /** Prevent instances of this class from being copied (As this class contains pointers) */
223 NEDepthwiseConvolutionLayerGeneric &operator=(const NEDepthwiseConvolutionLayerGeneric &) = delete;
224 /** Default move assignment operator */
225 NEDepthwiseConvolutionLayerGeneric &operator=(NEDepthwiseConvolutionLayerGeneric &&) = default;
226 /** Default destructor */
227 ~NEDepthwiseConvolutionLayerGeneric() = default;
228 /** Initialize the function's source, destination, weights and convolution information.
229 *
230 * @param[in, out] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling).
231 * @param[out] output Destination tensor. Data type supported: same as @p input.
232 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
233 * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8/QASYMM8_SIGNED.
234 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
235 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
236 * @param[in] conv_info Padding and stride information to use for the convolution.
237 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
238 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
239 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
240 */
241 void configure(ITensor *input,
242 const ITensor *weights,
243 const ITensor *biases,
244 ITensor *output,
245 const PadStrideInfo &conv_info,
246 unsigned int depth_multiplier = 1,
247 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
248 const Size2D &dilation = Size2D(1U, 1U));
249
250 /** Static function to check if given info will lead to a valid configuration of @ref NEDepthwiseConvolutionLayerGeneric
251 *
252 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. (Written to only for border filling).
253 * @param[in] output Destination tensor. Data type supported: same as @p input.
254 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
255 * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8/QASYMM8_SIGNED.
256 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
257 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
258 * @param[in] conv_info Padding and stride information to use for the convolution.
259 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
260 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
261 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
262 *
263 * @return a status
264 */
265 static Status validate(const ITensorInfo *input,
266 const ITensorInfo *weights,
267 const ITensorInfo *biases,
268 const ITensorInfo *output,
269 const PadStrideInfo &conv_info,
270 unsigned int depth_multiplier = 1,
271 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
272 const Size2D &dilation = Size2D(1U, 1U));
273
274 // Inherited methods overriden:
275 void run() override;
276
277 private:
278 struct Impl;
279 std::unique_ptr<Impl> _impl;
280 };
281 MemoryGroup _memory_group;
282 struct Impl;
283 std::unique_ptr<Impl> _impl;
284 };
285 } // namespace arm_compute
286 #endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEDEPTHWISECONVOLUTIONLAYER_H