blob: 9613caa10a3e9b315990704d40021a8a662fb179 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2017-2021, 2023 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H
25#define ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H
Giorgio Arena93a690e2017-08-01 16:09:33 +010026
Matthew Benthamf1aeab92023-05-30 13:35:34 +000027#include "arm_compute/core/ActivationLayerInfo.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010028#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas05045c12018-12-07 18:31:47 +000030#include "arm_compute/runtime/CL/functions/CLPermute.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010031#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010032#include "arm_compute/runtime/MemoryGroup.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010033
Giorgio Arena93a690e2017-08-01 16:09:33 +010034namespace arm_compute
35{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036class CLCompileContext;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010037class CLDepthwiseConvolutionLayerNativeKernel;
Giorgio Arena93a690e2017-08-01 16:09:33 +010038class ICLTensor;
39
Manuel Bottini05069f02019-09-26 17:18:26 +010040/** Function to execute a depthwise convolution
Gian Marco Iodice8155c022021-04-16 15:08:59 +010041 *
42 * -# @ref CLDepthwiseConvolutionLayerNativeKernel
43 * -# @ref CLPermute (if the data layout is NCHW)
44 *
Manuel Bottini05069f02019-09-26 17:18:26 +010045 */
46class CLDepthwiseConvolutionLayer : public IFunction
47{
48public:
49 /** Default constructor */
50 CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
51 /** Prevent instances of this class from being copied (As this class contains pointers) */
52 CLDepthwiseConvolutionLayer(const CLDepthwiseConvolutionLayer &) = delete;
53 /** Default move constructor */
54 CLDepthwiseConvolutionLayer(CLDepthwiseConvolutionLayer &&) = default;
55 /** Prevent instances of this class from being copied (As this class contains pointers) */
56 CLDepthwiseConvolutionLayer &operator=(const CLDepthwiseConvolutionLayer &) = delete;
57 /** Default move assignment operator */
58 CLDepthwiseConvolutionLayer &operator=(CLDepthwiseConvolutionLayer &&) = default;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010059 /** Default destructor */
60 ~CLDepthwiseConvolutionLayer();
Manuel Bottini05069f02019-09-26 17:18:26 +010061 /** Initialize the function's source, destination, weights and convolution information.
62 *
Teresa Charlin62687422021-04-28 10:58:49 +010063 * 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 *
Manuel Bottini2b84be52020-04-08 10:15:51 +010077 * @param[in] compile_context The compile context to be used.
morgolock3e77c272020-10-14 12:12:55 +010078 * @param[in, out] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/FP16/FP32. Data layout supported: NHWC, NCHW
Manuel Bottini2b84be52020-04-08 10:15:51 +010079 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
morgolock3e77c272020-10-14 12:12:55 +010080 * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
Manuel Bottini2b84be52020-04-08 10:15:51 +010081 * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
morgolock3e77c272020-10-14 12:12:55 +010082 * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
SiCongLibc4e3112021-06-29 13:18:30 +010083 * @param[out] output Destination tensor. Pass in nullptr or @p input for in-place operation. Data type supported: same as @p input.
Manuel Bottini2b84be52020-04-08 10:15:51 +010084 * @param[in] conv_info Padding and stride information to use for the convolution.
85 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
86 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
87 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
SiCongLibc4e3112021-06-29 13:18:30 +010088 *
89 * @note: For in-place support, please check @ref CLDepthwiseConvolutionLayerNativeKernel
Manuel Bottini2b84be52020-04-08 10:15:51 +010090 */
91 void configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
92 unsigned int depth_multiplier = 1, ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
Manuel Bottini05069f02019-09-26 17:18:26 +010093
SiCongLibc4e3112021-06-29 13:18:30 +010094 /** Initialize the function's source, destination, weights and convolution information.
95 *
96 * Similar to @ref CLDepthwiseConvolutionLayer::configure()
97 */
98 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
99 unsigned int depth_multiplier = 1, ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
100
Manuel Bottini05069f02019-09-26 17:18:26 +0100101 /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer
102 *
SiCongLibc4e3112021-06-29 13:18:30 +0100103 * Similar to @ref CLDepthwiseConvolutionLayer::configure()
Manuel Bottini05069f02019-09-26 17:18:26 +0100104 *
105 * @return a status
106 */
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100107 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
108 unsigned int depth_multiplier = 1, ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
Manuel Bottini05069f02019-09-26 17:18:26 +0100109
110 // Inherited methods overriden:
111 void run() override;
112 void prepare() override;
113
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100114 void set_memory_group(std::shared_ptr<IMemoryManager> memory_manager)
115 {
116 _memory_group = MemoryGroup(std::move(memory_manager));
117 };
118
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000119private:
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100120 MemoryGroup _memory_group;
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000121
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100122 std::unique_ptr<CLDepthwiseConvolutionLayerNativeKernel> _dwc_native_kernel;
123 CLPermute _permute_input_to_nhwc;
124 CLPermute _permute_weights_to_nhwc;
125 CLPermute _permute_output_to_nchw;
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000126
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100127 CLTensor _permuted_input;
128 CLTensor _permuted_weights;
129 CLTensor _permuted_output;
130 CLTensor _output_multipliers;
131 CLTensor _output_shifts;
132 const ITensor *_original_weights;
133 const ITensor *_input;
134 const ITensor *_output;
Gian Marco Iodicec63b7222021-06-30 08:39:44 +0000135
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100136 bool _needs_permute;
137 bool _is_prepared;
138 bool _is_quantized;
Manuel Bottini05069f02019-09-26 17:18:26 +0100139};
Georgios Pinitas05045c12018-12-07 18:31:47 +0000140} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000141#endif /*ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H */