Debian Codegraph @

arm_compute/runtime/experimental/operators/CpuGemmConv2d.h

raw
1 /*
2 * Copyright (c) 2021-2024 Arm Limited.
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 ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_OPERATORS_CPUGEMMCONV2D_H
25 #define ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_OPERATORS_CPUGEMMCONV2D_H
26
27 #include "arm_compute/core/ITensorPack.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/core/Types.h"
30 #include "arm_compute/runtime/IOperator.h"
31
32 #include <memory>
33
34 namespace arm_compute
35 {
36 namespace experimental
37 {
38 namespace op
39 {
40 /*
41 * A shallow wrapper for arm_compute::cpu::CpuGemmConv2d.
42 * Any new features should be added to arm_compute::cpu::CpuGemmConv2d and
43 * arm_compute::experimental::op::CpuGemmConv2d should remain a shallow wrapper.
44 */
45 class CpuGemmConv2d : public IOperator
46 {
47 public:
48 /** Constructor */
49 CpuGemmConv2d();
50 /** Prevent instances of this class from being copied (As this class contains pointers) */
51 CpuGemmConv2d(const CpuGemmConv2d &) = delete;
52 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
53 CpuGemmConv2d(CpuGemmConv2d &&) = delete;
54 /** Prevent instances of this class from being copied (As this class contains pointers) */
55 CpuGemmConv2d &operator=(const CpuGemmConv2d &) = delete;
56 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
57 CpuGemmConv2d &operator=(CpuGemmConv2d &&) = delete;
58 /** Destructor */
59 ~CpuGemmConv2d();
60 /** Set the input and output tensors.
61 *
62 * Valid data layouts:
63 * - NHWC
64 * - NCHW
65 *
66 * Valid data type configurations:
67 * |src0 |src1 |src2 |dst |
68 * |:--------------|:------------------|:--------|:--------------|
69 * |F16 |F16 |F16 |F16 |
70 * |F32 |F32 |F32 |F32 |
71 * |BFLOAT16 |BFLOAT16 |BFLOAT16 |BFLOAT16 |
72 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
73 * |QASYMM8 |QASYMM8_SIGNED |S32 |QASYMM8 |
74 * |QASYMM8 |QSYMM8_PER_CHANNEL |S32 |QASYMM8 |
75 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
76 * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32 |QASYMM8_SIGNED |
77 *
78 * @param[in] src Source tensor info. 3 lower dimensions represent a single input [width, height, IFM],
79 * while every optional dimension from 4 and above represent a batch of inputs.
80 * Data types supported: QASYMM8/QASYMM8_SIGNED/BFLOAT16/F16/F32.
81 * @param[in] weights Weights tensor info. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM].
82 * Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL/BFLOAT16/F16/F32.
83 * @param[in] biases Biases tensor info. 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/QASYMM8_SIGNED type where biases should be of S32 type.
85 * @param[out] dst Destination tensor info. 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 CpuWeightsReshapeKernel. If this is not part of the fully connected layer the weights
89 * tensor has also been transposed with cpu::kernels::CpuGemmTranspose1xWKernel. Data type supported: Same as @p input.
90 * @param[in] dilation (Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
91 * @param[in] act_info (Optional) Activation layer information in case of a fused activation. Only RELU, BOUNDED_RELU and LU_BOUNDED_RELU supported.
92 * @param[in] enable_fast_math (Optional) Enable fast math computation. In case this flag were set, the function could dispatch the fastest implementation
93 * available which may introduce a drop of accuracy as well. Default is false
94 * @param[in] num_groups (Optional) Number of groups when performing a grouped convolution. num_groups != 1 is not supported
95 */
96 void configure(const ITensorInfo *src,
97 const ITensorInfo *weights,
98 const ITensorInfo *biases,
99 ITensorInfo *dst,
100 const PadStrideInfo &conv_info,
101 const WeightsInfo &weights_info = WeightsInfo(),
102 const Size2D &dilation = Size2D(1U, 1U),
103 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
104 bool enable_fast_math = false,
105 unsigned int num_groups = 1);
106 /** Static function to check if given info will lead to a valid configuration
107 *
108 * Similar to CpuGemmConvolution::configure()
109 *
110 * @return a status
111 */
112 static Status validate(const ITensorInfo *src,
113 const ITensorInfo *weights,
114 const ITensorInfo *biases,
115 const ITensorInfo *output,
116 const PadStrideInfo &conv_info,
117 const WeightsInfo &weights_info = WeightsInfo(),
118 const Size2D &dilation = Size2D(1U, 1U),
119 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
120 bool enable_fast_math = false,
121 unsigned int num_groups = 1);
122
123 /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters.
124 *
125 * The parameter list is the same as @ref NEGEMMConvolutionLayer::has_opt_impl
126 *
127 * @return a status.
128 */
129 static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
130 const ITensorInfo *src,
131 const ITensorInfo *weights,
132 const ITensorInfo *biases,
133 const ITensorInfo *output,
134 const PadStrideInfo &conv_info,
135 const WeightsInfo &weights_info = WeightsInfo(),
136 const Size2D &dilation = Size2D(1U, 1U),
137 const ActivationLayerInfo &act_info = ActivationLayerInfo(),
138 bool enable_fast_math = false);
139
140 /** Update of quantization information at the run stage for convolution so that the quantization multipliers can be properly calculated.
141 * Please @ref NEGEMMConvolutionLayer for a more in-depth explanation and example.
142 *
143 * @param[in] tensors Vector that contains the tensors to operate on.
144 */
145 void update_quantization_parameters(ITensorPack &tensors);
146
147 void run(ITensorPack &tensors) override;
148 void prepare(ITensorPack &tensors) override;
149 experimental::MemoryRequirements workspace() const override;
150
151 private:
152 struct Impl;
153 std::unique_ptr<Impl> _impl;
154 };
155 } // namespace op
156 } // namespace experimental
157 } // namespace arm_compute
158 #endif // ACL_ARM_COMPUTE_RUNTIME_EXPERIMENTAL_OPERATORS_CPUGEMMCONV2D_H