blob: 71b9e15dc1e758ef1268bbd3fdf85ba4e09da7ee [file] [log] [blame]
Michalis Spyroub55f8e82021-07-22 11:23:11 +01001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
Michalis Spyroub55f8e82021-07-22 11:23:11 +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 */
SiCong Li91295492023-07-21 18:16:13 +010024#include "arm_compute/function_info/ActivationLayerInfo.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010025
Michalis Spyroub55f8e82021-07-22 11:23:11 +010026#include "src/core/common/Macros.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010027#include "src/cpu/ICpuOperator.h"
Michalis Spyroub55f8e82021-07-22 11:23:11 +010028
29namespace arm_compute
30{
31namespace cpu
32{
33/** Basic function to simulate a convolution layer. This function calls one of the following functions:
34 * -# @ref CpuGemm (executed only in case GEMM is required for the operation)
35 * -# @ref CpuWinogradConv2d (executed only in case Winograd is required for the operation)
36 * -# @ref CpuDirectConv2d (executed only in case Direct Convolution is required for the operation)
37 *
38 *
39 * The function selects one of the algorithms mentioned above based on:
40 * - The size of the kernel
41 * - Number of input/output feature maps
42 * - Amount of memory needed
43 *
44 * Generally GEMM-based convolution is executed when neither Winograd nor FFT nor Direct convolution can be performed.
45 *
46 * FP32 Algorithm| Filter Size | Input/Output feature maps |
47 * --------------|----------------------------------------------------|-------------------------------------------|
48 * Winograd | 3x3 1x3 3x1 5x1 1x5 5x5(fast maths) 7x1 1x7 | Input channels is greater than 3 |
49 * FFT | Squared kernels and greater than 9x9 | Input feature maps > Output feature maps |
50 * DirectConv | 9x9 | |
51 * GEMM | Any size | |
52 *
53 * Winograd 5x5 requires fast maths enabled.
54 *
55 * FP16 Algorithm| Filter Size |
56 * --------------|------------------|
57 * Winograd | Not supported |
58 * FFT | Not supported |
59 * DirectConv | 9x9 |
60 * GEMM | Any size |
61 *
62 *
63 */
64class CpuConv2d : public ICpuOperator
65{
66public:
67 /** Constructor */
68 CpuConv2d();
69 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuConv2d);
70 /** Default destructor */
71 ~CpuConv2d();
72 /** Set the input and output tensors.
73 *
74 * Valid data layouts:
75 * - NHWC
76 * - NCHW
77 *
78 * Valid data type configurations:
79 * |src0 |src1 |src2 |dst |
80 * |:--------------|:------------------|:------|:--------------|
81 * |F16 |F16 |F16 |F16 |
82 * |F32 |F32 |F32 |F32 |
83 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
84 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
85 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
86 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
87 *
88 * @param[in] src Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
89 * while every optional dimension from 4 and above represent a batch of inputs.
90 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
91 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
92 * Data type supported: Same as @p src, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
93 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
94 * Data type supported: Same as @p src, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
95 * @param[out] dst Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
96 * Data types supported: Same as @p src.
97 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
98 * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
99 * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input.
100 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
101 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
102 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
103 * available which may introduce a drop of accuracy as well. Default is false
104 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
105 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 void configure(ITensorInfo *src,
107 ITensorInfo *weights,
108 const ITensorInfo *biases,
109 ITensorInfo *dst,
110 const PadStrideInfo &conv_info,
111 const WeightsInfo &weights_info = WeightsInfo(),
112 const Size2D &dilation = Size2D(1U, 1U),
113 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
114 bool enable_fast_math = false,
115 unsigned int num_groups = 1);
Michalis Spyroub55f8e82021-07-22 11:23:11 +0100116 /** Static function to check if given info will lead to a valid configuration of @ref CpuConv2d
117 *
118 * Similar to CpuConv2d::configure()
119 *
120 * @return a status
121 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 static Status validate(const ITensorInfo *src,
123 const ITensorInfo *weights,
124 const ITensorInfo *biases,
125 const ITensorInfo *output,
126 const PadStrideInfo &conv_info,
127 const WeightsInfo &weights_info = WeightsInfo(),
128 const Size2D &dilation = Size2D(1U, 1U),
129 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
130 bool enable_fast_math = false,
131 unsigned int num_groups = 1);
Michalis Spyroub55f8e82021-07-22 11:23:11 +0100132 /** Static function to check if given info will return the convolution called by @ref CpuConv2d
133 *
134 * @param[in] src Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
135 * while every optional dimension from 4 and above represent a batch of inputs.
136 * Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
137 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
138 * Data type supported:Same as @p src, also could be QSYMM8_PER_CHANNEL if input is QASYMM8/QASYMM8_SIGNED.
139 * @param[in] dst Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
140 * Data types supported: Same as @p src.
141 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
142 * @param[in] weights_info Specifies if the weights tensor has been reshaped with NEWeightsReshapeKernel. If this is not part of the fully connected layer the weights
143 * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input.
144 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
145 * @param[in] act_info (Optional) Activation layer information in case of a fused activation.
146 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
147 * available which may introduce a drop of accuracy as well. Default is false
148 *
149 * @return the Convolution Method Hint
150 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 static ConvolutionMethod get_convolution_method(const ITensorInfo *src,
152 const ITensorInfo *weights,
153 const ITensorInfo *dst,
154 const PadStrideInfo &conv_info,
155 const WeightsInfo &weights_info = WeightsInfo(),
156 const Size2D &dilation = Size2D(1U, 1U),
157 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
158 bool enable_fast_math = false);
Michalis Spyroub55f8e82021-07-22 11:23:11 +0100159 // Inherited methods overridden:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 void run(ITensorPack &tensors) override;
161 void prepare(ITensorPack &constants) override;
Michalis Spyroub55f8e82021-07-22 11:23:11 +0100162 experimental::MemoryRequirements workspace() const override;
163
164private:
165 std::unique_ptr<ICpuOperator> _function;
166 experimental::MemoryRequirements _aux_mem{};
167};
168} // namespace cpu
169} // namespace arm_compute