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 | |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/ActivationLayerInfo.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
| 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 | */ |
| 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 Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 93 | |
SiCongLi | bc4e311 | 2021-06-29 13:18:30 +0100 | [diff] [blame] | 94 | /** 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 Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 101 | /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer |
| 102 | * |
SiCongLi | bc4e311 | 2021-06-29 13:18:30 +0100 | [diff] [blame] | 103 | * Similar to @ref CLDepthwiseConvolutionLayer::configure() |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 104 | * |
| 105 | * @return a status |
| 106 | */ |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 107 | 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 Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 109 | |
| 110 | // Inherited methods overriden: |
| 111 | void run() override; |
| 112 | void prepare() override; |
| 113 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 114 | void set_memory_group(std::shared_ptr<IMemoryManager> memory_manager) |
| 115 | { |
| 116 | _memory_group = MemoryGroup(std::move(memory_manager)); |
| 117 | }; |
| 118 | |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 119 | private: |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 120 | MemoryGroup _memory_group; |
Gian Marco Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 121 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 122 | 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 Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 126 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 127 | 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 Iodice | c63b722 | 2021-06-30 08:39:44 +0000 | [diff] [blame] | 135 | |
Gian Marco Iodice | 8155c02 | 2021-04-16 15:08:59 +0100 | [diff] [blame] | 136 | bool _needs_permute; |
| 137 | bool _is_prepared; |
| 138 | bool _is_quantized; |
Manuel Bottini | 05069f0 | 2019-09-26 17:18:26 +0100 | [diff] [blame] | 139 | }; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 140 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 141 | #endif /*ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H */ |