blob: 6e5ce4cd4862502a9d35274f8dd7a0bdb9ef5478 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +00002 * Copyright (c) 2017-2018 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +01003 *
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 Arena9fe41442017-08-23 16:36:24 +010024#ifndef __ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__
25#define __ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__
Giorgio Arena93a690e2017-08-01 16:09:33 +010026
Giorgio Arenadfca60b2018-01-31 10:30:59 +000027#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NCHWKernel.h"
28#include "arm_compute/core/CL/kernels/CLDepthwiseConvolutionLayer3x3NHWCKernel.h"
Giorgio Arena9fe41442017-08-23 16:36:24 +010029#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 Pinitasde5a1cc2018-02-02 12:52:07 +000032#include "arm_compute/core/CL/kernels/CLDirectConvolutionLayerOutputStageKernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010033#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
Giorgio Arena9fe41442017-08-23 16:36:24 +010034#include "arm_compute/core/CL/kernels/CLGEMMMatrixVectorMultiplyKernel.h"
Giorgio Arenadfca60b2018-01-31 10:30:59 +000035#include "arm_compute/core/CL/kernels/ICLDepthwiseConvolutionLayer3x3Kernel.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010036#include "arm_compute/core/Types.h"
37#include "arm_compute/runtime/CL/CLTensor.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010038#include "arm_compute/runtime/IFunction.h"
39
Giorgio Arena93a690e2017-08-01 16:09:33 +010040namespace arm_compute
41{
42class ICLTensor;
43
Giorgio Arenadfca60b2018-01-31 10:30:59 +000044/** 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 Arena93a690e2017-08-01 16:09:33 +010045 *
Giorgio Arenadfca60b2018-01-31 10:30:59 +000046 * -# @ref CLDepthwiseConvolutionLayer3x3NCHWKernel (if data_layout == NCHW)
47 * -# @ref CLDepthwiseConvolutionLayer3x3NHWCKernel (if data_layout == NHWC)
Giorgio Arena9fe41442017-08-23 16:36:24 +010048 * -# @ref CLFillBorderKernel (if pad_x or pad_y > 0)
Giorgio Arena93a690e2017-08-01 16:09:33 +010049 *
50 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000051class CLDepthwiseConvolutionLayer3x3 : public IFunction
Giorgio Arena93a690e2017-08-01 16:09:33 +010052{
53public:
54 /** Default constructor */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000055 CLDepthwiseConvolutionLayer3x3();
Giorgio Arena93a690e2017-08-01 16:09:33 +010056 /** Initialize the function's source, destination, conv and border_size.
57 *
Giorgio Arena76572242018-04-04 17:44:26 +010058 * @param[in, out] input Source tensor. Data type supported: QASYMM8/F16/F32. (Written to only for border filling).
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.
62 * @param[out] output Destination tensor. Data type supported: same as @p input.
63 * @param[in] conv_info Padding and stride information to use for the convolution.
64 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
65 * @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 Arena93a690e2017-08-01 16:09:33 +010066 */
Giorgio Arena76572242018-04-04 17:44:26 +010067 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1,
68 ActivationLayerInfo act_info = ActivationLayerInfo());
Giorgio Arena93a690e2017-08-01 16:09:33 +010069
70 // Inherited methods overriden:
71 void run() override;
72
73private:
Giorgio Arenadfca60b2018-01-31 10:30:59 +000074 std::unique_ptr<ICLDepthwiseConvolutionLayer3x3Kernel> _kernel;
75 CLFillBorderKernel _border_handler;
Giorgio Arena9fe41442017-08-23 16:36:24 +010076};
77
78/** Basic function to execute a generic depthwise convolution. This function calls the following OpenCL kernels:
79 *
80 * -# @ref CLDepthwiseIm2ColKernel
81 * -# @ref CLGEMMMatrixVectorMultiplyKernel
82 * -# @ref CLDepthwiseWeightsReshapeKernel
83 * -# @ref CLFillBorderKernel (if pad_x or pad_y > 0)
84 *
85 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000086class CLDepthwiseConvolutionLayer : public IFunction
Giorgio Arena9fe41442017-08-23 16:36:24 +010087{
88public:
89 /** Default constructor */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000090 CLDepthwiseConvolutionLayer();
Georgios Pinitas1562be32018-03-08 19:09:19 +000091 /** Prevent instances of this class from being copied (As this class contains pointers) */
92 CLDepthwiseConvolutionLayer(const CLDepthwiseConvolutionLayer &) = delete;
93 /** Default move constructor */
94 CLDepthwiseConvolutionLayer(CLDepthwiseConvolutionLayer &&) = default;
95 /** Prevent instances of this class from being copied (As this class contains pointers) */
96 CLDepthwiseConvolutionLayer &operator=(const CLDepthwiseConvolutionLayer &) = delete;
97 /** Default move assignment operator */
98 CLDepthwiseConvolutionLayer &operator=(CLDepthwiseConvolutionLayer &&) = default;
Giorgio Arena9fe41442017-08-23 16:36:24 +010099 /** Initialize the function's source, destination, weights and convolution information.
100 *
Giorgio Arena76572242018-04-04 17:44:26 +0100101 * @param[in, out] input Source tensor. Data type supported: QASYMM8/F32. (Written to only for border filling).
102 * @param[in] weights Weights tensor. These are 3D tensors with shape [kernel_x, kernel_y, IFM]. Data type supported: Same as @p input.
103 * @param[in] biases (Optional) Biases tensor. A 1D tensor with shape [IFM]. Must be nullptr if not needed.
104 * Data type supported: Same as @p input, S32 when input is QASYMM8.
105 * @param[out] output Destination tensor. Data type supported: same as @p input.
106 * @param[in] conv_info Padding and stride information to use for the convolution.
107 * @param[in] depth_multiplier (Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.
Giorgio Arena9fe41442017-08-23 16:36:24 +0100108 */
Giorgio Arena76572242018-04-04 17:44:26 +0100109 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier = 1);
Giorgio Arena9fe41442017-08-23 16:36:24 +0100110
111 // Inherited methods overriden:
112 void run() override;
113
114private:
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000115 CLDepthwiseIm2ColKernel _im2col_kernel;
116 CLDepthwiseWeightsReshapeKernel _weights_reshape_kernel;
117 CLGEMMMatrixVectorMultiplyKernel _v2mm_kernel;
118 CLDepthwiseVectorToTensorKernel _vector_to_tensor_kernel;
119 CLDirectConvolutionLayerOutputStageKernel _output_stage_kernel;
120 CLFillBorderKernel _v2mm_input_fill_border;
121 CLFillBorderKernel _v2mm_weights_fill_border;
122 CLTensor _input_reshaped;
123 CLTensor _weights_reshaped;
124 CLTensor _v2mm_output;
125 CLTensor _output_reshaped;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000126 bool _is_first_run;
Georgios Pinitasde5a1cc2018-02-02 12:52:07 +0000127 bool _is_quantized;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000128 const ICLTensor *_original_weights;
Giorgio Arena93a690e2017-08-01 16:09:33 +0100129};
130}
Giorgio Arena9fe41442017-08-23 16:36:24 +0100131#endif /*__ARM_COMPUTE_CLDEPTHWISECONVOLUTION_H__ */