Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ |
| 25 | #define __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ |
| 26 | |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/functions/CLDirectConvolutionLayer.h" |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/functions/CLFFTConvolutionLayer.h" |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/functions/CLGEMMConvolutionLayer.h" |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/functions/CLWinogradConvolutionLayer.h" |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 31 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/IMemoryManager.h" |
| 33 | |
| 34 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
Gian Marco | 20d7848 | 2018-01-11 15:10:58 +0000 | [diff] [blame] | 38 | /** Basic function to compute the convolution layer. This function calls the following OpenCL kernels/functions: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | * |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 40 | * -# @ref CLGEMMConvolutionLayer |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 41 | * -# @ref CLWinogradConvolutionLayer |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 42 | * -# @ref CLDirectConvolutionLayer |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 43 | * -# @ref CLFFTConvolutionLayer |
Pablo Tello | 8f309ab | 2019-07-19 17:46:12 +0100 | [diff] [blame] | 44 | * |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | */ |
| 71 | class CLConvolutionLayer : public IFunction |
| 72 | { |
| 73 | public: |
| 74 | /** Default constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 75 | CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | /** Set the input and output tensors. |
| 77 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 78 | * @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. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 80 | * Data types supported: QASYMM8/F16/F32. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 81 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: Same as @p input. |
| 82 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. |
| 83 | * Data type supported: Should match @p input data type, except for input of QASYMM8 type where biases should be of S32 type. |
| 84 | * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 85 | * Data types supported: Same as @p input. |
| 86 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
| 87 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. Data type supported: Same as @p input. |
| 88 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 89 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 90 | * @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 Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 91 | * available which may introduce a drop of accuracy as well. Default is false |
| 92 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | */ |
Alex Gilday | 7da29b6 | 2018-03-23 14:16:00 +0000 | [diff] [blame] | 94 | void configure(ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(), |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 95 | const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 96 | /** Static function to check if given info will lead to a valid configuration of @ref CLConvolutionLayer |
| 97 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 98 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 99 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 100 | * Data types supported: QASYMM8/F16/F32. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 101 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 102 | * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM]. Data type supported:Same as @p input. |
| 103 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 104 | * Data types supported: Same as @p input. |
| 105 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
Michele Di Giorgio | 70ba7d6 | 2018-06-06 17:03:36 +0100 | [diff] [blame] | 106 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 107 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 108 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 109 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 110 | * available which may introduce a drop of accuracy as well. Default is false |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 111 | * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is only supported for NCHW data layout |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 112 | * |
| 113 | * @return a status |
| 114 | */ |
| 115 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 916d1bc | 2018-08-13 11:20:41 +0100 | [diff] [blame] | 116 | const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, |
| 117 | unsigned int num_groups = 1); |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 118 | /** Static function to check if given info will return the convolution called by @ref CLConvolutionLayer |
| 119 | * |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 120 | * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], |
| 121 | * while every optional dimension from 4 and above represent a batch of inputs. |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 122 | * Data types supported: QASYMM8/F16/F32. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 123 | * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported:Same as @p input. |
| 124 | * @param[in] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. |
| 125 | * Data types supported: Same as @p input. |
| 126 | * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. |
Michele Di Giorgio | 70ba7d6 | 2018-06-06 17:03:36 +0100 | [diff] [blame] | 127 | * @param[in] weights_info Specifies if the weights tensor has been reshaped with CLWeightsReshapeKernel. |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 128 | * @param[in] act_info (Optional) Activation layer information in case of a fused activation. |
| 129 | * @param[in] gpu_target Specifies the @p GPUTarget. |
| 130 | * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1). |
| 131 | * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation |
| 132 | * available which may introduce a drop of accuracy as well. Default is false |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 133 | * |
| 134 | * @return a status |
| 135 | */ |
Gian Marco Iodice | e52a300 | 2018-04-11 15:59:10 +0100 | [diff] [blame] | 136 | static ConvolutionMethod get_convolution_method(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output, const PadStrideInfo &conv_info, |
Gian Marco Iodice | 2213d4b | 2018-04-27 10:39:06 +0100 | [diff] [blame] | 137 | const WeightsInfo &weights_info, const ActivationLayerInfo &act_info, const GPUTarget gpu_target, const Size2D &dilation = Size2D(1U, 1U), bool enable_fast_math = false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | // Inherited methods overridden: |
| 139 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 140 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | |
| 142 | private: |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 143 | std::shared_ptr<IMemoryManager> _memory_manager; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 144 | std::unique_ptr<IFunction> _function; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | }; |
| 146 | } |
| 147 | #endif /* __ARM_COMPUTE_CLCONVOLUTIONLAYER_H__ */ |