blob: 96f69f21d520b1714db7247bce5198c94c799b7f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
24#ifndef __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__
25#define __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__
26
Isabella Gottardif07d28d2018-02-06 14:52:43 +000027#include "arm_compute/runtime/CL/functions/CLDirectConvolutionLayer.h"
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010028#include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h"
Isabella Gottardif07d28d2018-02-06 14:52:43 +000029#include "arm_compute/runtime/CL/functions/CLGEMMConvolutionLayer.h"
Gian Marco Iodicee52a3002018-04-11 15:59:10 +010030#include "arm_compute/runtime/CL/functions/CLWinogradConvolutionLayer.h"
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000031#include "arm_compute/runtime/IFunction.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010032#include "arm_compute/runtime/IMemoryManager.h"
33
34#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36namespace arm_compute
37{
Gian Marco20d78482018-01-11 15:10:58 +000038/** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039 *
Isabella Gottardif07d28d2018-02-06 14:52:43 +000040 * -# @ref CLGEMMConvolutionLayer
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010041 * -# @ref CLWinogradConvolutionLayer
Isabella Gottardif07d28d2018-02-06 14:52:43 +000042 * -# @ref CLDirectConvolutionLayer
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +010043 * -# @ref CLFFTConvolutionLayer
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 */
45class CLConvolutionLayer : public IFunction
46{
47public:
48 /** Default constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010049 CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050 /** Set the input and output tensors.
51 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010052 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
53 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010054 * Data types supported: QASYMM8/F16/F32.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010055 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input.
56 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
57 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
58 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
59 * Data types supported: Same as @p input.
60 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
61 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input.
62 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
63 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
64 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010065 * available which may introduce a drop of accuracy as well. Default is false
66 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 */
Alex Gilday7da29b62018-03-23 14:16:00 +000068 void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010069 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000070 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer
71 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010072 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
73 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010074 * Data types supported: QASYMM8/F16/F32.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010075 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
76 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported:Same as @p input.
77 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
78 * Data types supported: Same as @p input.
79 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +010080 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010081 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
82 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
83 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
84 * available which may introduce a drop of accuracy as well. Default is false
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010085 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout
Isabella Gottardif07d28d2018-02-06 14:52:43 +000086 *
87 * @return a status
88 */
89 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010090 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false,
91 unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +000092 /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer
93 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010094 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
95 * while every optional dimension from 4 and above represent a batch of inputs.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010096 * Data types supported: QASYMM8/F16/F32.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010097 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input.
98 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
99 * Data types supported: Same as @p input.
100 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100101 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100102 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
103 * @param[in] gpu_target Specifies the @p GPUTarget.
104 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
105 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
106 * available which may introduce a drop of accuracy as well. Default is false
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000107 *
108 * @return a status
109 */
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100110 static ConvolutionMethod get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info,
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100111 const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation = Size2D(1U, 1U), bool enable_fast_math = false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 // Inherited methods overridden:
113 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100114 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116private:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000117 std::shared_ptr<IMemoryManager> _memory_manager;
Georgios Pinitase0437672018-05-02 14:07:55 +0100118 std::unique_ptr<IFunction> _function;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119};
120}
121#endif /* __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ */