xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/CL/functions/CLDepthwiseConvolutionLayer.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-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_CLDEPTHWISECONVOLUTION_H
25 #define ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H
26 
27 #include "arm_compute/core/Types.h"
28 #include "arm_compute/runtime/CL/CLTensor.h"
29 #include "arm_compute/runtime/CL/functions/CLPermute.h"
30 #include "arm_compute/runtime/IFunction.h"
31 #include "arm_compute/runtime/MemoryGroup.h"
32 
33 namespace arm_compute
34 {
35 class CLCompileContext;
36 class CLDepthwiseConvolutionLayerNativeKernel;
37 class ICLTensor;
38 
39 /** Function to execute a depthwise convolution
40  *
41  * -# @ref CLDepthwiseConvolutionLayerNativeKernel
42  * -# @ref CLPermute (if the data layout is NCHW)
43  *
44  */
45 class CLDepthwiseConvolutionLayer : public IFunction
46 {
47 public:
48     /** Default constructor */
49     CLDepthwiseConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
50     /** Prevent instances of this class from being copied (As this class contains pointers) */
51     CLDepthwiseConvolutionLayer(const CLDepthwiseConvolutionLayer &) = delete;
52     /** Default move constructor */
53     CLDepthwiseConvolutionLayer(CLDepthwiseConvolutionLayer &&) = default;
54     /** Prevent instances of this class from being copied (As this class contains pointers) */
55     CLDepthwiseConvolutionLayer &operator=(const CLDepthwiseConvolutionLayer &) = delete;
56     /** Default move assignment operator */
57     CLDepthwiseConvolutionLayer &operator=(CLDepthwiseConvolutionLayer &&) = default;
58     /** Default destructor */
59     ~CLDepthwiseConvolutionLayer();
60     /** Initialize the function's source, destination, weights and convolution information.
61      *
62      * Valid data layouts:
63      * - NHWC
64      * - NCHW
65      *
66      * Valid data type configurations:
67      * |src0           |src1               |src2   |dst            |
68      * |:--------------|:------------------|:------|:--------------|
69      * |F16            |F16                |F16    |F16            |
70      * |F32            |F32                |F32    |F32            |
71      * |QASYMM8        |QASYMM8            |S32    |QASYMM8        |
72      * |QASYMM8        |QSYMM8_PER_CHANNEL |S32    |QASYMM8        |
73      * |QASYMM8_SIGNED |QASYMM8_SIGNED     |S32    |QASYMM8_SIGNED |
74      * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32    |QASYMM8_SIGNED |
75      *
76      * @param[in]      compile_context  The compile context to be used.
77      * @param[in, out] input            Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/FP16/FP32. Data layout supported: NHWC, NCHW
78      * @param[in]      weights          Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM].
79      *                                  Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
80      * @param[in]      biases           Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
81      *                                  Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED.
82      * @param[out]     output           Destination tensor. Pass in nullptr or @p input for in-place operation. Data type supported: same as @p input.
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      * @note: For in-place support, please check @ref CLDepthwiseConvolutionLayerNativeKernel
89      */
90     void configure(const CLCompileContext &compile_context, ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
91                    unsigned int depth_multiplier = 1, ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
92 
93     /** Initialize the function's source, destination, weights and convolution information.
94      *
95      * Similar to @ref CLDepthwiseConvolutionLayer::configure()
96      */
97     void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info,
98                    unsigned int depth_multiplier = 1, ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
99 
100     /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer
101      *
102      * Similar to @ref CLDepthwiseConvolutionLayer::configure()
103      *
104      * @return a status
105      */
106     static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
107                            unsigned int depth_multiplier = 1, ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U));
108 
109     // Inherited methods overriden:
110     void run() override;
111     void prepare() override;
112 
set_memory_group(std::shared_ptr<IMemoryManager> memory_manager)113     void set_memory_group(std::shared_ptr<IMemoryManager> memory_manager)
114     {
115         _memory_group = MemoryGroup(std::move(memory_manager));
116     };
117 
118 private:
119     MemoryGroup _memory_group;
120 
121     std::unique_ptr<CLDepthwiseConvolutionLayerNativeKernel> _dwc_native_kernel;
122     CLPermute                                                _permute_input_to_nhwc;
123     CLPermute                                                _permute_weights_to_nhwc;
124     CLPermute                                                _permute_output_to_nchw;
125 
126     CLTensor       _permuted_input;
127     CLTensor       _permuted_weights;
128     CLTensor       _permuted_output;
129     CLTensor       _output_multipliers;
130     CLTensor       _output_shifts;
131     const ITensor *_original_weights;
132     const ITensor *_input;
133     const ITensor *_output;
134 
135     bool _needs_permute;
136     bool _is_prepared;
137     bool _is_quantized;
138 };
139 } // namespace arm_compute
140 #endif /*ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H */
141