Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/kernels/CLDepthwiseIm2ColKernel.h" |
| 30 | #include "arm_compute/core/CL/kernels/CLDepthwiseVectorToTensorKernel.h" |
| 31 | #include "arm_compute/core/CL/kernels/CLDepthwiseWeightsReshapeKernel.h" |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/CL/kernels/CLDirectConvolutionLayerOutputStageKernel.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/CL/kernels/CLFillBorderKernel.h" |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixVectorMultiplyKernel.h" |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame^] | 35 | #include "arm_compute/core/CL/kernels/ICLDepthwiseConvolutionLayer3x3Kernel.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Types.h" |
| 37 | #include "arm_compute/runtime/CL/CLTensor.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 38 | #include "arm_compute/runtime/IFunction.h" |
| 39 | |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
| 42 | class ICLTensor; |
| 43 | |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame^] | 44 | /** 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] | 45 | * |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame^] | 46 | * -# @ref CLDepthwiseConvolutionLayer3x3NCHWKernel (if data_layout == NCHW) |
| 47 | * -# @ref CLDepthwiseConvolutionLayer3x3NHWCKernel (if data_layout == NHWC) |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 48 | * -# @ref CLFillBorderKernel (if pad_x or pad_y > 0) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 49 | * |
| 50 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 51 | class CLDepthwiseConvolutionLayer3x3 : public IFunction |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 52 | { |
| 53 | public: |
| 54 | /** Default constructor */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 55 | CLDepthwiseConvolutionLayer3x3(); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 56 | /** Initialize the function's source, destination, conv and border_size. |
| 57 | * |
Michele Di Giorgio | 933fe86 | 2018-02-19 15:42:12 +0000 | [diff] [blame] | 58 | * @param[in, out] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling). |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 59 | * @param[in] weights Weights tensor. A 3D tensor with shape [3, 3, IFM]. Data type supported: Same as @p input. |
| 60 | * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed. |
| 61 | * Data type supported: Same as @p input. |
Giorgio Arena | 82afedf | 2017-11-15 13:36:15 +0000 | [diff] [blame] | 62 | * @param[out] output Destination tensor. Data type supported: same as @p input. |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 63 | * @param[in] conv_info Padding and stride information to use for the convolution. |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 64 | * @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. |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 65 | */ |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 66 | void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, ActivationLayerInfo act_info = ActivationLayerInfo()); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 67 | |
| 68 | // Inherited methods overriden: |
| 69 | void run() override; |
| 70 | |
| 71 | private: |
Giorgio Arena | dfca60b | 2018-01-31 10:30:59 +0000 | [diff] [blame^] | 72 | std::unique_ptr<ICLDepthwiseConvolutionLayer3x3Kernel> _kernel; |
| 73 | CLFillBorderKernel _border_handler; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /** Basic function to execute a generic depthwise convolution. This function calls the following OpenCL kernels: |
| 77 | * |
| 78 | * -# @ref CLDepthwiseIm2ColKernel |
| 79 | * -# @ref CLGEMMMatrixVectorMultiplyKernel |
| 80 | * -# @ref CLDepthwiseWeightsReshapeKernel |
| 81 | * -# @ref CLFillBorderKernel (if pad_x or pad_y > 0) |
| 82 | * |
| 83 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 84 | class CLDepthwiseConvolutionLayer : public IFunction |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 85 | { |
| 86 | public: |
| 87 | /** Default constructor */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 88 | CLDepthwiseConvolutionLayer(); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 89 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 90 | CLDepthwiseConvolutionLayer(const CLDepthwiseConvolutionLayer &) = delete; |
| 91 | /** Default move constructor */ |
| 92 | CLDepthwiseConvolutionLayer(CLDepthwiseConvolutionLayer &&) = default; |
| 93 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 94 | CLDepthwiseConvolutionLayer &operator=(const CLDepthwiseConvolutionLayer &) = delete; |
| 95 | /** Default move assignment operator */ |
| 96 | CLDepthwiseConvolutionLayer &operator=(CLDepthwiseConvolutionLayer &&) = default; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 97 | /** Initialize the function's source, destination, weights and convolution information. |
| 98 | * |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 99 | * @param[in, out] input Source tensor. Data type supported: QASYMM8/F32. (Written to only for border filling). |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 100 | * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input. |
Georgios Pinitas | 81a26ad | 2017-10-23 20:29:30 +0100 | [diff] [blame] | 101 | * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed. |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 102 | * Data type supported: Same as @p input, S32 when input is QASYMM8. |
Giorgio Arena | 82afedf | 2017-11-15 13:36:15 +0000 | [diff] [blame] | 103 | * @param[out] output Destination tensor. Data type supported: same as @p input. |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 104 | * @param[in] conv_info Padding and stride information to use for the convolution. |
| 105 | */ |
Giorgio Arena | 82afedf | 2017-11-15 13:36:15 +0000 | [diff] [blame] | 106 | void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 107 | |
| 108 | // Inherited methods overriden: |
| 109 | void run() override; |
| 110 | |
| 111 | private: |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 112 | CLDepthwiseIm2ColKernel _im2col_kernel; |
| 113 | CLDepthwiseWeightsReshapeKernel _weights_reshape_kernel; |
| 114 | CLGEMMMatrixVectorMultiplyKernel _v2mm_kernel; |
| 115 | CLDepthwiseVectorToTensorKernel _vector_to_tensor_kernel; |
| 116 | CLDirectConvolutionLayerOutputStageKernel _output_stage_kernel; |
| 117 | CLFillBorderKernel _v2mm_input_fill_border; |
| 118 | CLFillBorderKernel _v2mm_weights_fill_border; |
| 119 | CLTensor _input_reshaped; |
| 120 | CLTensor _weights_reshaped; |
| 121 | CLTensor _v2mm_output; |
| 122 | CLTensor _output_reshaped; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 123 | bool _is_first_run; |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 124 | bool _is_quantized; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 125 | const ICLTensor *_original_weights; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 126 | }; |
| 127 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 128 | #endif /*__ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__ */ |