blob: 1aca1df8eb88fba2b8173f75ba136cda69da9ffb [file] [log] [blame]
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00001/*
Matthew Benthamf1aeab92023-05-30 13:35:34 +00002 * Copyright (c) 2017-2023 Arm Limited.
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +00003 *
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_NEGEMMCONVOLUTIONLAYER_H
25#define ARM_COMPUTE_NEGEMMCONVOLUTIONLAYER_H
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000026
27#include "arm_compute/runtime/IFunction.h"
28
Matthew Benthamf1aeab92023-05-30 13:35:34 +000029#include "arm_compute/core/ActivationLayerInfo.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000030#include "arm_compute/core/Types.h"
Manuel Bottini29599d02021-07-06 15:01:35 +010031#include "arm_compute/runtime/IFunction.h"
32#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010033#include "arm_compute/runtime/IWeightsManager.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000034#include "arm_compute/runtime/MemoryGroup.h"
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000035
36#include <memory>
37
38namespace arm_compute
39{
40class ITensor;
Manuel Bottini29599d02021-07-06 15:01:35 +010041class ITensorInfo;
Michalis Spyrou1a569a32019-09-10 17:20:34 +010042
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000043/** Basic function to compute the convolution layer. This function calls the following kernels/functions:
Gian Marco Iodice597a8562018-08-01 15:06:06 +010044 *
Georgios Pinitas19884632021-08-16 12:38:54 +010045 * -# @ref cpu::CpuGemmConv2d
Gian Marco Iodice597a8562018-08-01 15:06:06 +010046 *
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000047 */
48class NEGEMMConvolutionLayer : public IFunction
49{
50public:
51 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +010052 NEGEMMConvolutionLayer(const std::shared_ptr<IMemoryManager> &memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +000053 /** Prevent instances of this class from being copied (As this class contains pointers) */
54 NEGEMMConvolutionLayer(const NEGEMMConvolutionLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000055 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
56 NEGEMMConvolutionLayer(NEGEMMConvolutionLayer &&) = delete;
Georgios Pinitas1562be32018-03-08 19:09:19 +000057 /** Prevent instances of this class from being copied (As this class contains pointers) */
58 NEGEMMConvolutionLayer &operator=(const NEGEMMConvolutionLayer &) = delete;
Michalis Spyrou770dfeb2020-11-04 18:55:34 +000059 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
60 NEGEMMConvolutionLayer &operator=(NEGEMMConvolutionLayer &&) = delete;
Michalis Spyrouebcebf12020-10-21 00:04:14 +010061 /** Default destructor */
62 ~NEGEMMConvolutionLayer();
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000063 /** Set the input and output tensors.
64 *
Teresa Charlin62687422021-04-28 10:58:49 +010065 * Valid data layouts:
66 * - NHWC
67 * - NCHW
68 *
69 * Valid data type configurations:
70 * |src0 |src1 |src2 |dst |
71 * |:--------------|:------------------|:--------|:--------------|
72 * |F16 |F16 |F16 |F16 |
73 * |F32 |F32 |F32 |F32 |
74 * |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 |
75 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
76 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
77 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
78 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
79 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010080 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
81 * while every optional dimension from 4 and above represent a batch of inputs.
82 * Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
83 * @param[in] weights Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
84 * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
85 * @param[in] biases Biases tensor. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
86 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
87 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
88 * Data types supported: Same as @p input.
89 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
90 * @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
91 * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input.
92 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
93 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
94 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
95 * available which may introduce a drop of accuracy as well. Default is false
96 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +000097 */
Alex Gilday7da29b62018-03-23 14:16:00 +000098 void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info = WeightsInfo(),
Georgios Pinitas69a9ac42021-07-22 13:30:13 +010099 const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(), bool enable_fast_math = false, unsigned int num_groups = 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000100 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMConvolutionLayer
101 *
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100102 * @param[in] input Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
103 * while every optional dimension from 4 and above represent a batch of inputs.
104 * Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
105 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
106 * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
107 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 1D tensor with dimensions [OFM].
108 * Data type supported: Should match @p input data type, except for input of QASYMM8/QASYMM8_SIGNED type where biases should be of S32 type.
109 * @param[in] output Destination tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
110 * Data types supported: Same as @p input.
111 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
112 * @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
113 * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input.
114 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
115 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
116 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
117 * available which may introduce a drop of accuracy as well. Default is false
118 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000119 *
120 * @return a status
121 */
122 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
Georgios Pinitas69a9ac42021-07-22 13:30:13 +0100123 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(),
124 bool enable_fast_math = false, unsigned int num_groups = 1);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000125
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000126 /** Static function to check if there is an optimized version of
127 * GEMM available for the input parameters.
128 *
129 * The method is intended to be used to find out the optimal
130 * memory layout to be used for the weights tensor when running
131 * variable weights execution.
132 *
133 * The user can query the database of optimised kernels in
134 * arm_gemm by specifying one of the enumerations of
Ramy Elgammal91780022022-07-20 14:57:37 +0100135 * arm_compute::WeightFormat in the weight_format field of the input
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000136 * parameter weights_info. In case of success, the method
137 * writes the expected format in the output parameter
138 * expected_weight_format. The expected_weight_format can than be
139 * used in the configure method of the class for retrieving the
140 * best optimal kernel.
141 *
142 * Use case one - query for a specific format:
143 *
Ramy Elgammal91780022022-07-20 14:57:37 +0100144 * WeightInfo weights_info(..., arm_compute::WeightFormat::OHWIo4, ...); // Set the value of the input query.
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000145 * if (NEGEMMConvolutionlayer::has_opt_impl(WeightFormat(), ...., weights_info, ...))
146 * {
147 * auto conv = std::unique_ptr<NEGEMMConvolutionlayer>();
148 * conv->configure(..., weights_info, ...); // uses the same WeightFormat the user wanted originally, OHWYo4.
149 * conv->run(...);
150 * }
151 *
152 * Use case two - query for any format that would be optimal for the GEMM to execute:
153 *
Ramy Elgammal91780022022-07-20 14:57:37 +0100154 * WeightInfo weights_info(..., arm_compute::WeightFormat::ANY, ...); // Set the value of the input query.
155 * arm_compute::WeightFormat expected_wf;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000156 * if (NEGEMMConvolutionlayer::has_opt_impl(expected_wf, ...., weights_info, ...))
157 * {
158 * auto conv = std::unique_ptr<NEGEMMConvolutionlayer>();
159 * // ... code to convert the layout of the weights tensor to the layout returned by has_opt_impl
160 * WeightInfo new_weights_info(..., expected_wf, ...); // Set the value of the WeightFormat returned by has_opt_impl.
161 * conv->configure(..., new_weights_info, ...);
162 * conv->run(...);
163 * }
164 *
165 * Notice that a GEMM configured with a WeightFormat other than
166 * UNSPECIFIED will run GEMM with variable weights mode.
167 *
168 * @param[out] expected_weight_format The arm_compute::WeightFormat expected by the kernel.
169 * @param[in] src Source tensor info.
170 * @param[in] weights Weights tensor info.
171 * @param[in] biases Biases tensor info. Shared biases supported.
172 * @param[in] dst Destination tensor info.
173 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
174 * @param[in] weights_info (optional) Specifies additional configuration parameters for the weights of the GEMM computation.
175 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
176 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported. And no activation (i.e. Linear) which is the default value.
177 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
178 *
179 * @return a Status
180 */
Ramy Elgammal91780022022-07-20 14:57:37 +0100181 static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst,
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000182 const PadStrideInfo &conv_info,
183 const WeightsInfo &weights_info = WeightsInfo(), const Size2D &dilation = Size2D(1U, 1U), const ActivationLayerInfo &act_info = ActivationLayerInfo(),
184 bool enable_fast_math = false);
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000185 // Inherited methods overridden:
186 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100187 void prepare() override;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000188
189private:
Manuel Bottini29599d02021-07-06 15:01:35 +0100190 struct Impl;
191 std::unique_ptr<Impl> _impl;
Isabella Gottardi6acc6ad2018-02-02 17:19:18 +0000192};
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100193} // namespace arm_compute
Manuel Bottini29599d02021-07-06 15:01:35 +0100194#endif /* ARM_COMPUTE_NEGEMMCONVOLUTIONLAYER_H */