blob: b52695463acd33ed462789adc3151bf1c5994131 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +00002 * Copyright (c) 2017-2020 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLCONVOLUTIONLAYER_H
25#define ARM_COMPUTE_CLCONVOLUTIONLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
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
Pablo Tello8f309ab2019-07-19 17:46:12 +010044 *
45 * The function selects one of the algorithms mentioned above based on:
46 * - The size of the kernel
47 * - Number of input/output feature maps
48 * - Amount of memory needed
49 *
50 * Generally GEMM-based convolution is executed when neither Winograd nor FFT nor Direct convolution can be performed.
51 *
52 * FP32 Algorithm| Filter Size | Input/Output feature maps |
53 * --------------|-------------------------------------------------------------|-------------------------------------------|
54 * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5(fast maths) 7x1 1x7 | Input channels is greater than 3 |
55 * FFT | Squared kernels and greater than 9x9 | Input feature maps > Output feature maps |
56 * DirectConv | 9x9 | |
57 * GEMM | Any size | |
58 *
59 * Winograd 5x5 requires fast maths enabled.
60 *
61 * FP16 Algorithm| Filter Size | Input/Output feature maps |
62 * --------------|----------------------------|-------------------------------------------|
63 * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5 | Input channels is greater than 3 |
64 * FFT | Not supported | |
65 * DirectConv | 9x9 | |
66 * GEMM | Any size | |
67 *
68 * Winograd FP16 requires fast maths enabled.
69 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070 */
71class CLConvolutionLayer : public IFunction
72{
73public:
74 /** Default constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010075 CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 /** Set the input and output tensors.
77 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010078 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
79 * while every optional dimension from 4 and above represent a batch of inputs.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +000080 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000081 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
82 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010083 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
84 * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type.
85 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
86 * Data types supported: Same as @p input.
87 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
88 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input.
89 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
90 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
91 * @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 +010092 * available which may introduce a drop of accuracy as well. Default is false
93 * @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 +010094 */
Alex Gilday7da29b62018-03-23 14:16:00 +000095 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 +010096 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 +000097 /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer
98 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +010099 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
100 * while every optional dimension from 4 and above represent a batch of inputs.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +0000101 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000102 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
103 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100104 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported:Same as @p input.
105 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
106 * Data types supported: Same as @p input.
107 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100108 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100109 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
110 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
111 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
112 * available which may introduce a drop of accuracy as well. Default is false
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100113 * @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 +0000114 *
115 * @return a status
116 */
117 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 +0100118 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false,
119 unsigned int num_groups = 1);
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000120 /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer
121 *
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100122 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
123 * while every optional dimension from 4 and above represent a batch of inputs.
Sang-Hoon Park70d33bd2020-01-08 16:29:15 +0000124 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000125 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
126 * Data type supported: Same as @p input or QASYMM8/QSYMM8_PER_CHANNEL when @p input is QASYMM8.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100127 * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
128 * Data types supported: Same as @p input.
129 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
Michele Di Giorgio70ba7d62018-06-06 17:03:36 +0100130 * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel.
Gian Marco Iodice2213d4b2018-04-27 10:39:06 +0100131 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
132 * @param[in] gpu_target Specifies the @p GPUTarget.
133 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
134 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
135 * available which may introduce a drop of accuracy as well. Default is false
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000136 *
137 * @return a status
138 */
Gian Marco Iodicee52a3002018-04-11 15:59:10 +0100139 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 +0100140 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 +0100141 // Inherited methods overridden:
142 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100143 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
145private:
Isabella Gottardif07d28d2018-02-06 14:52:43 +0000146 std::shared_ptr<IMemoryManager> _memory_manager;
Georgios Pinitase0437672018-05-02 14:07:55 +0100147 std::unique_ptr<IFunction> _function;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148};
149}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000150#endif /* ARM_COMPUTE_CLCONVOLUTIONLAYER_H */