Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H |
| 25 | #define ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 26 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Types.h" |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 28 | #include "arm_compute/function_info/ActivationLayerInfo.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLTensor.h" |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/functions/CLPermute.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/MemoryGroup.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 33 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 36 | class CLCompileContext; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 37 | class CLDepthwiseConvolutionLayerNativeKernel; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 38 | class ICLTensor; |
| 39 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 40 | /** Function to execute a depthwise convolution |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 41 | * |
| 42 | * -# @ref CLDepthwiseConvolutionLayerNativeKernel |
| 43 | * -# @ref CLPermute (if the data layout is NCHW) |
| 44 | * |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 45 | */ |
| 46 | class CLDepthwiseConvolutionLayer : public IFunction |
| 47 | { |
| 48 | public: |
| 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 Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 59 | /** Default destructor */ |
| 60 | ~CLDepthwiseConvolutionLayer(); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 61 | /** Initialize the function's source, destination, weights and convolution information. |
| 62 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 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 | * |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 77 | * @param[in] compile_context The compile context to be used. |
morgolock | 3e77c27 | 2020-10-14 12:12:55 +0100 | [diff] [blame] | 78 | * @param[in, out] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/FP16/FP32. Data layout supported: NHWC, NCHW |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 79 | * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. |
morgolock | 3e77c27 | 2020-10-14 12:12:55 +0100 | [diff] [blame] | 80 | * Data type supported: Same as @p input or QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL when @p input is QASYMM8. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 81 | * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed. |
morgolock | 3e77c27 | 2020-10-14 12:12:55 +0100 | [diff] [blame] | 82 | * Data type supported: Same as @p input, S32 when input is QASYMM8/QASYMM8_SIGNED. |
SiCongLi | bc4e311 | 2021-06-29 13:18:30 +0100 | [diff] [blame] | 83 | * @param[out] output Destination tensor. Pass in nullptr or @p input for in-place operation. Data type supported: same as @p input. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 84 | * @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). |
SiCongLi | bc4e311 | 2021-06-29 13:18:30 +0100 | [diff] [blame] | 88 | * |
| 89 | * @note: For in-place support, please check @ref CLDepthwiseConvolutionLayerNativeKernel |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 90 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | void configure(const CLCompileContext &compile_context, |
| 92 | ICLTensor *input, |
| 93 | const ICLTensor *weights, |
| 94 | const ICLTensor *biases, |
| 95 | ICLTensor *output, |
| 96 | const PadStrideInfo &conv_info, |
| 97 | unsigned int depth_multiplier = 1, |
| 98 | ActivationLayerInfo act_info = ActivationLayerInfo(), |
| 99 | const Size2D &dilation = Size2D(1U, 1U)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 100 | |
SiCongLi | bc4e311 | 2021-06-29 13:18:30 +0100 | [diff] [blame] | 101 | /** Initialize the function's source, destination, weights and convolution information. |
| 102 | * |
| 103 | * Similar to @ref CLDepthwiseConvolutionLayer::configure() |
| 104 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | void configure(ICLTensor *input, |
| 106 | const ICLTensor *weights, |
| 107 | const ICLTensor *biases, |
| 108 | ICLTensor *output, |
| 109 | const PadStrideInfo &conv_info, |
| 110 | unsigned int depth_multiplier = 1, |
| 111 | ActivationLayerInfo act_info = ActivationLayerInfo(), |
| 112 | const Size2D &dilation = Size2D(1U, 1U)); |
SiCongLi | bc4e311 | 2021-06-29 13:18:30 +0100 | [diff] [blame] | 113 | |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 114 | /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer |
| 115 | * |
SiCongLi | bc4e311 | 2021-06-29 13:18:30 +0100 | [diff] [blame] | 116 | * Similar to @ref CLDepthwiseConvolutionLayer::configure() |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 117 | * |
| 118 | * @return a status |
| 119 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 120 | static Status validate(const ITensorInfo *input, |
| 121 | const ITensorInfo *weights, |
| 122 | const ITensorInfo *biases, |
| 123 | const ITensorInfo *output, |
| 124 | const PadStrideInfo &conv_info, |
| 125 | unsigned int depth_multiplier = 1, |
| 126 | ActivationLayerInfo act_info = ActivationLayerInfo(), |
| 127 | const Size2D &dilation = Size2D(1U, 1U)); |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 128 | |
| 129 | // Inherited methods overriden: |
| 130 | void run() override; |
| 131 | void prepare() override; |
| 132 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 133 | void set_memory_group(std::shared_ptr<IMemoryManager> memory_manager) |
| 134 | { |
| 135 | _memory_group = MemoryGroup(std::move(memory_manager)); |
| 136 | }; |
| 137 | |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 138 | private: |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 139 | MemoryGroup _memory_group; |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 140 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 141 | std::unique_ptr<CLDepthwiseConvolutionLayerNativeKernel> _dwc_native_kernel; |
| 142 | CLPermute _permute_input_to_nhwc; |
| 143 | CLPermute _permute_weights_to_nhwc; |
| 144 | CLPermute _permute_output_to_nchw; |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 145 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 146 | CLTensor _permuted_input; |
| 147 | CLTensor _permuted_weights; |
| 148 | CLTensor _permuted_output; |
| 149 | CLTensor _output_multipliers; |
| 150 | CLTensor _output_shifts; |
| 151 | const ITensor *_original_weights; |
| 152 | const ITensor *_input; |
| 153 | const ITensor *_output; |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 154 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 155 | bool _needs_permute; |
| 156 | bool _is_prepared; |
| 157 | bool _is_quantized; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 158 | }; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 159 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 160 | #endif /*ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H */ |