Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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 | */ |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [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 | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h" |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayerReshapeWeightsGenericKernel.h" |
| 30 | #include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayerReshapeWeightsKernel.h" |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/CL/kernels/CLDepthwiseIm2ColKernel.h" |
| 32 | #include "arm_compute/core/CL/kernels/CLDepthwiseVectorToTensorKernel.h" |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/CL/kernels/CLDirectConvolutionLayerOutputStageKernel.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/CL/kernels/CLFillBorderKernel.h" |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 35 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixVectorMultiplyKernel.h" |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 36 | #include "arm_compute/core/CL/kernels/ICLDepthwiseConvolutionLayer3x3Kernel.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/Types.h" |
| 38 | #include "arm_compute/runtime/CL/CLTensor.h" |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 39 | #include "arm_compute/runtime/CL/functions/CLActivationLayer.h" |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 40 | #include "arm_compute/runtime/CL/functions/CLPermute.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 41 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame^] | 42 | #include "arm_compute/runtime/MemoryGroup.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 43 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 44 | namespace arm_compute |
| 45 | { |
| 46 | class ICLTensor; |
| 47 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 48 | /** 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 Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 49 | * |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 50 | * -# @ref CLDepthwiseConvolutionLayer3x3NCHWKernel (if data_layout == NCHW) |
| 51 | * -# @ref CLDepthwiseConvolutionLayer3x3NHWCKernel (if data_layout == NHWC) |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 52 | * -# @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel (if data_layout == NHWC) |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 53 | * -# @ref CLFillBorderKernel (if pad_x or pad_y > 0) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 54 | * |
| 55 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 56 | class CLDepthwiseConvolutionLayer3x3 : public IFunction |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 57 | { |
| 58 | public: |
| 59 | /** Default constructor */ |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 60 | CLDepthwiseConvolutionLayer3x3(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
| 61 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 62 | CLDepthwiseConvolutionLayer3x3(const CLDepthwiseConvolutionLayer3x3 &) = delete; |
| 63 | /** Default move constructor */ |
| 64 | CLDepthwiseConvolutionLayer3x3(CLDepthwiseConvolutionLayer3x3 &&) = default; |
| 65 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 66 | CLDepthwiseConvolutionLayer3x3 &operator=(const CLDepthwiseConvolutionLayer3x3 &) = delete; |
| 67 | /** Default move assignment operator */ |
| 68 | CLDepthwiseConvolutionLayer3x3 &operator=(CLDepthwiseConvolutionLayer3x3 &&) = default; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 69 | /** Initialize the function's source, destination, conv and border_size. |
| 70 | * |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 71 | * @param[in, out] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling). |
| 72 | * @param[in] weights Weights tensor. A 3D tensor with shape [3, 3, IFM]. Data type supported: Same as @p input. |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 73 | * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed. |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 74 | * Data type supported: Same as @p input. |
| 75 | * @param[out] output Destination tensor. Data type supported: same as @p input. |
| 76 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 77 | * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1. |
| 78 | * @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 Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 79 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 80 | */ |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 81 | void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 82 | ActivationLayerInfo act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U)); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 83 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 84 | /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer3x3 |
| 85 | * |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 86 | * @param[in] input Source tensor info. Data type supported: QASYMM8 for all layouts, F16/F32 for NCHW. |
| 87 | * @param[in] weights Weights tensor info. A 3D tensor with shape [3, 3, IFM]. Data type supported: Same as @p input. |
| 88 | * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed. |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 89 | * Data type supported: Same as @p input, S32 when input is QASYMM8. |
| 90 | * @param[in] output Destination tensor. Data type supported: same as @p input. |
| 91 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 92 | * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1. |
| 93 | * @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. |
| 94 | * @param[in] gpu_target (Optional) GPU target to validate the kernel for. Defaults to midgard. |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 95 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 96 | * |
| 97 | * @return a status |
| 98 | */ |
| 99 | 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 Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 100 | ActivationLayerInfo act_info = ActivationLayerInfo(), GPUTarget gpu_target = GPUTarget::MIDGARD, const Size2D &dilation = Size2D(1U, 1U)); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 101 | // Inherited methods overriden: |
| 102 | void run() override; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 103 | void prepare() override; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 104 | |
| 105 | private: |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame^] | 106 | MemoryGroup _memory_group; |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame] | 107 | std::unique_ptr<ICLDepthwiseConvolutionLayer3x3Kernel> _kernel; |
| 108 | CLFillBorderKernel _border_handler; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 109 | CLPermute _permute_input_to_nchw; |
| 110 | CLPermute _permute_weights_to_nchw; |
| 111 | CLPermute _permute_output_to_nhwc; |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 112 | CLDepthwiseConvolutionLayerReshapeWeightsKernel _reshape_weights; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 113 | CLTensor _permuted_input; |
| 114 | CLTensor _permuted_weights; |
| 115 | CLTensor _permuted_output; |
| 116 | const ITensor *_original_weights; |
| 117 | bool _needs_permute; |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 118 | bool _needs_weights_reshape; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 119 | bool _is_prepared; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /** Basic function to execute a generic depthwise convolution. This function calls the following OpenCL kernels: |
| 123 | * |
| 124 | * -# @ref CLDepthwiseIm2ColKernel |
| 125 | * -# @ref CLGEMMMatrixVectorMultiplyKernel |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 126 | * -# @ref CLDepthwiseConvolutionLayerReshapeWeightsGenericKernel |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 127 | * -# @ref CLFillBorderKernel (if pad_x or pad_y > 0) |
| 128 | * |
| 129 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 130 | class CLDepthwiseConvolutionLayer : public IFunction |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 131 | { |
| 132 | public: |
| 133 | /** Default constructor */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 134 | CLDepthwiseConvolutionLayer(); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 135 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 136 | CLDepthwiseConvolutionLayer(const CLDepthwiseConvolutionLayer &) = delete; |
| 137 | /** Default move constructor */ |
| 138 | CLDepthwiseConvolutionLayer(CLDepthwiseConvolutionLayer &&) = default; |
| 139 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 140 | CLDepthwiseConvolutionLayer &operator=(const CLDepthwiseConvolutionLayer &) = delete; |
| 141 | /** Default move assignment operator */ |
| 142 | CLDepthwiseConvolutionLayer &operator=(CLDepthwiseConvolutionLayer &&) = default; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 143 | /** Initialize the function's source, destination, weights and convolution information. |
| 144 | * |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 145 | * @param[in, out] input Source tensor. Data type supported: QASYMM8/F32. (Written to only for border filling). |
| 146 | * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input. |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 147 | * @param[in] biases Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed. |
Giorgio Arena | 7657224 | 2018-04-04 17:44:26 +0100 | [diff] [blame] | 148 | * Data type supported: Same as @p input, S32 when input is QASYMM8. |
| 149 | * @param[out] output Destination tensor. Data type supported: same as @p input. |
| 150 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 151 | * @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 Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 152 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 153 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 154 | */ |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 155 | void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 156 | unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U)); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 157 | |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 158 | /** Static function to check if given info will lead to a valid configuration of @ref CLDepthwiseConvolutionLayer |
| 159 | * |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 160 | * @param[in] input Source tensor info. Data type supported: QASYMM8/F32. |
| 161 | * @param[in] weights Weights tensor info. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input. |
| 162 | * @param[in] biases Biases tensor info. A 1D tensor with shape [IFM]. Must be nullptr if not needed. |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 163 | * Data type supported: Same as @p input, S32 when input is QASYMM8. |
| 164 | * @param[in] output Destination tensor. Data type supported: same as @p input. |
| 165 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 166 | * @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 Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 167 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 168 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 169 | * |
| 170 | * @return a status |
| 171 | */ |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 172 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Usama Arif | e73686a | 2019-04-08 17:30:48 +0100 | [diff] [blame] | 173 | unsigned int depth_multiplier = 1, const ActivationLayerInfo &act_info = ActivationLayerInfo(), const Size2D &dilation = Size2D(1U, 1U)); |
Giorgio Arena | ad0c738 | 2018-04-23 16:16:21 +0100 | [diff] [blame] | 174 | |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 175 | // Inherited methods overriden: |
| 176 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 177 | void prepare() override; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 178 | |
| 179 | private: |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 180 | CLDepthwiseIm2ColKernel _im2col_kernel; |
| 181 | CLDepthwiseConvolutionLayerReshapeWeightsGenericKernel _weights_reshape_kernel; |
| 182 | CLGEMMMatrixVectorMultiplyKernel _v2mm_kernel; |
| 183 | CLDepthwiseVectorToTensorKernel _vector_to_tensor_kernel; |
| 184 | CLDirectConvolutionLayerOutputStageKernel _output_stage_kernel; |
| 185 | CLActivationLayer _activationlayer_function; |
| 186 | CLFillBorderKernel _v2mm_input_fill_border; |
| 187 | CLFillBorderKernel _v2mm_weights_fill_border; |
| 188 | CLTensor _input_reshaped; |
| 189 | CLTensor _weights_reshaped; |
| 190 | CLTensor _v2mm_output; |
| 191 | CLTensor _output_reshaped; |
| 192 | bool _is_prepared; |
| 193 | bool _is_quantized; |
| 194 | bool _is_activationlayer_enabled; |
| 195 | const ICLTensor *_original_weights; |
| 196 | std::unique_ptr<IFunction> _optimised_function; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 197 | }; |
Georgios Pinitas | 05045c1 | 2018-12-07 18:31:47 +0000 | [diff] [blame] | 198 | } // namespace arm_compute |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 199 | #endif /*__ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__ */ |