blob: 98581a21feb5b22994e20bb00523324b22ee0ea0 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
giuros016d109962019-01-07 17:47:19 +00002 * Copyright (c) 2017-2019 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +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 */
Giorgio Arena9fe41442017-08-23 16:36:24 +010024#ifndef __ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__
25#define __ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__
Giorgio Arena93a690e2017-08-01 16:09:33 +010026
Giorgio Arenadfca60b2018-01-31 10:30:59 +000027#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
28#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
Michele Di Giorgioa046e162019-10-08 09:36:26 +010029#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayerNativeKernel.h"
giuros016d109962019-01-07 17:47:19 +000030#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayerReshapeWeightsKernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010031#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
Giorgio Arenadfca60b2018-01-31 10:30:59 +000032#include "arm_compute/core/CL/kernels/ICLDepthwiseConvolutionLayer3x3Kernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010033#include "arm_compute/core/Types.h"
34#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas05045c12018-12-07 18:31:47 +000035#include "arm_compute/runtime/CL/functions/CLPermute.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010036#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010037#include "arm_compute/runtime/MemoryGroup.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010038
Giorgio Arena93a690e2017-08-01 16:09:33 +010039namespace arm_compute
40{
41class ICLTensor;
42
Giorgio Arenadfca60b2018-01-31 10:30:59 +000043/** Basic function to execute a depthwise convolution for kernel size 3x3xC (when data layout NCHW) or Cx3x3 (when data layout NHWC). This function calls the following OpenCL kernels:
Giorgio Arena93a690e2017-08-01 16:09:33 +010044 *
Giorgio Arenadfca60b2018-01-31 10:30:59 +000045 * -# @ref CLDepthwiseConvolutionLayer3x3NCHWKernel (if data_layout == NCHW)
46 * -# @ref CLDepthwiseConvolutionLayer3x3NHWCKernel (if data_layout == NHWC)
giuros016d109962019-01-07 17:47:19 +000047 * -# @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel (if data_layout == NHWC)
Giorgio Arena9fe41442017-08-23 16:36:24 +010048 * -# @ref CLFillBorderKernel (if pad_x or pad_y > 0)
Giorgio Arena93a690e2017-08-01 16:09:33 +010049 *
50 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000051class CLDepthwiseConvolutionLayer3x3 : public IFunction
Giorgio Arena93a690e2017-08-01 16:09:33 +010052{
53public:
54 /** Default constructor */
Georgios Pinitas05045c12018-12-07 18:31:47 +000055 CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
56 /** Prevent instances of this class from being copied (As this class contains pointers) */
57 CLDepthwiseConvolutionLayer3x3(const CLDepthwiseConvolutionLayer3x3 &) = delete;
58 /** Default move constructor */
59 CLDepthwiseConvolutionLayer3x3(CLDepthwiseConvolutionLayer3x3 &&) = default;
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 CLDepthwiseConvolutionLayer3x3 &operator=(const CLDepthwiseConvolutionLayer3x3 &) = delete;
62 /** Default move assignment operator */
63 CLDepthwiseConvolutionLayer3x3 &operator=(CLDepthwiseConvolutionLayer3x3 &&) = default;
Giorgio Arena93a690e2017-08-01 16:09:33 +010064 /** Initialize the function's source, destination, conv and border_size.
65 *
Giorgio Arena76572242018-04-04 17:44:26 +010066 * @param[in, out] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
67 * @param[in] weights Weights tensor. A 3D tensor with shape [3, 3, IFM]. Data type supported: Same as @p input.
Usama Arife73686a2019-04-08 17:30:48 +010068 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
Giorgio Arena76572242018-04-04 17:44:26 +010069 * Data type supported: Same as @p input.
70 * @param[out] output Destination tensor. Data type supported: same as @p input.
71 * @param[in] conv_info Padding and stride information to use for the convolution.
72 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
73 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU for 3x3 QASYMM8 supported.
Usama Arife73686a2019-04-08 17:30:48 +010074 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Giorgio Arena93a690e2017-08-01 16:09:33 +010075 */
Giorgio Arena76572242018-04-04 17:44:26 +010076 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1,
Usama Arife73686a2019-04-08 17:30:48 +010077 ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
Giorgio Arena93a690e2017-08-01 16:09:33 +010078
Giorgio Arenaad0c7382018-04-23 16:16:21 +010079 /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer3x3
80 *
Usama Arife73686a2019-04-08 17:30:48 +010081 * @param[in] input Source tensor info. Data type supported: QASYMM8 for all layouts, F16/F32 for NCHW.
82 * @param[in] weights Weights tensor info. A 3D tensor with shape [3, 3, IFM]. Data type supported: Same as @p input.
83 * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
Giorgio Arenaad0c7382018-04-23 16:16:21 +010084 * Data type supported: Same as @p input, S32 when input is QASYMM8.
85 * @param[in] output Destination tensor. Data type supported: same as @p input.
86 * @param[in] conv_info Padding and stride information to use for the convolution.
87 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
88 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU for 3x3 QASYMM8 supported.
89 * @param[in] gpu_target (Optional) GPU target to validate the kernel for. Defaults to midgard.
Usama Arife73686a2019-04-08 17:30:48 +010090 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Giorgio Arenaad0c7382018-04-23 16:16:21 +010091 *
92 * @return a status
93 */
94 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1,
Usama Arife73686a2019-04-08 17:30:48 +010095 ActivationLayerInfo act_info = ActivationLayerInfo(), GPUTarget gpu_target = GPUTarget::MIDGARD, const Size2D &dilation = Size2D(1U, 1U));
Giorgio Arena93a690e2017-08-01 16:09:33 +010096 // Inherited methods overriden:
97 void run() override;
Georgios Pinitas05045c12018-12-07 18:31:47 +000098 void prepare() override;
Giorgio Arena93a690e2017-08-01 16:09:33 +010099
100private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100101 MemoryGroup _memory_group;
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000102 std::unique_ptr<ICLDepthwiseConvolutionLayer3x3Kernel> _kernel;
103 CLFillBorderKernel _border_handler;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000104 CLPermute _permute_input_to_nchw;
105 CLPermute _permute_weights_to_nchw;
106 CLPermute _permute_output_to_nhwc;
giuros016d109962019-01-07 17:47:19 +0000107 CLDepthwiseConvolutionLayerReshapeWeightsKernel _reshape_weights;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000108 CLTensor _permuted_input;
109 CLTensor _permuted_weights;
110 CLTensor _permuted_output;
111 const ITensor *_original_weights;
112 bool _needs_permute;
giuros016d109962019-01-07 17:47:19 +0000113 bool _needs_weights_reshape;
Georgios Pinitas05045c12018-12-07 18:31:47 +0000114 bool _is_prepared;
Giorgio Arena9fe41442017-08-23 16:36:24 +0100115};
116
117/** Basic function to execute a generic depthwise convolution. This function calls the following OpenCL kernels:
118 *
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100119 * -# @ref CLDepthwiseConvolutionLayerNativeKernel
120 * -# @ref CLPermute (x 3) if the data layout is NCHW
Giorgio Arena9fe41442017-08-23 16:36:24 +0100121 *
122 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000123class CLDepthwiseConvolutionLayer : public IFunction
Giorgio Arena9fe41442017-08-23 16:36:24 +0100124{
125public:
126 /** Default constructor */
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100127 CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000128 /** Prevent instances of this class from being copied (As this class contains pointers) */
129 CLDepthwiseConvolutionLayer(const CLDepthwiseConvolutionLayer &) = delete;
130 /** Default move constructor */
131 CLDepthwiseConvolutionLayer(CLDepthwiseConvolutionLayer &&) = default;
132 /** Prevent instances of this class from being copied (As this class contains pointers) */
133 CLDepthwiseConvolutionLayer &operator=(const CLDepthwiseConvolutionLayer &) = delete;
134 /** Default move assignment operator */
135 CLDepthwiseConvolutionLayer &operator=(CLDepthwiseConvolutionLayer &&) = default;
Giorgio Arena9fe41442017-08-23 16:36:24 +0100136 /** Initialize the function's source, destination, weights and convolution information.
137 *
Giorgio Arena76572242018-04-04 17:44:26 +0100138 * @param[in, out] input Source tensor. Data type supported: QASYMM8/F32. (Written to only for border filling).
139 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
Usama Arife73686a2019-04-08 17:30:48 +0100140 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
Giorgio Arena76572242018-04-04 17:44:26 +0100141 * Data type supported: Same as @p input, S32 when input is QASYMM8.
142 * @param[out] output Destination tensor. Data type supported: same as @p input.
143 * @param[in] conv_info Padding and stride information to use for the convolution.
144 * @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 +0100145 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Usama Arife73686a2019-04-08 17:30:48 +0100146 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Giorgio Arena9fe41442017-08-23 16:36:24 +0100147 */
Georgios Pinitas60e98252018-10-22 16:17:20 +0100148 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100149 unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
Giorgio Arena9fe41442017-08-23 16:36:24 +0100150
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100151 /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer
152 *
Usama Arife73686a2019-04-08 17:30:48 +0100153 * @param[in] input Source tensor info. Data type supported: QASYMM8/F32.
154 * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
155 * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100156 * Data type supported: Same as @p input, S32 when input is QASYMM8.
157 * @param[in] output Destination tensor. Data type supported: same as @p input.
158 * @param[in] conv_info Padding and stride information to use for the convolution.
159 * @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 +0100160 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
Usama Arife73686a2019-04-08 17:30:48 +0100161 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100162 *
163 * @return a status
164 */
Georgios Pinitas60e98252018-10-22 16:17:20 +0100165 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Usama Arife73686a2019-04-08 17:30:48 +0100166 unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
Giorgio Arenaad0c7382018-04-23 16:16:21 +0100167
Giorgio Arena9fe41442017-08-23 16:36:24 +0100168 // Inherited methods overriden:
169 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100170 void prepare() override;
Giorgio Arena9fe41442017-08-23 16:36:24 +0100171
172private:
Michele Di Giorgioa046e162019-10-08 09:36:26 +0100173 MemoryGroup _memory_group;
174
175 std::unique_ptr<IFunction> _optimised_function;
176 CLDepthwiseConvolutionLayerNativeKernel _dwc_native_kernel;
177 CLPermute _permute_input_to_nhwc;
178 CLPermute _permute_weights_to_nhwc;
179 CLPermute _permute_output_to_nchw;
180
181 CLTensor _permuted_input;
182 CLTensor _permuted_weights;
183 CLTensor _permuted_output;
184 const ITensor *_original_weights;
185
186 bool _needs_permute;
187 bool _is_prepared;
Giorgio Arena93a690e2017-08-01 16:09:33 +0100188};
Georgios Pinitas05045c12018-12-07 18:31:47 +0000189} // namespace arm_compute
Giorgio Arena9fe41442017-08-23 16:36:24 +0100190#endif /*__ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__ */