Debian Codegraph @

arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h

raw
1 /*
2 * Copyright (c) 2017-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_NEON_FUNCTIONS_NEFULLYCONNECTEDLAYER_H
25 #define ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEFULLYCONNECTEDLAYER_H
26
27 #include "arm_compute/function_info/FullyConnectedLayerInfo.h"
28 #include "arm_compute/runtime/IFunction.h"
29 #include "arm_compute/runtime/IMemoryManager.h"
30 #include "arm_compute/runtime/IWeightsManager.h"
31 #include "arm_compute/runtime/MemoryManagerOnDemand.h"
32 #include "arm_compute/runtime/NEON/functions/NETranspose.h"
33 #include "arm_compute/runtime/Tensor.h"
34
35 #include <memory>
36
37 namespace arm_compute
38 {
39 namespace weights_transformations
40 {
41 /** Basic function to manage the reshape weights generated from @ref NETranspose */
42 class NEFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights
43 {
44 public:
45 void run() override
46 {
47 _output.allocator()->allocate();
48 _func.run();
49 _reshape_run = true;
50 }
51
52 void release() override
53 {
54 _output.allocator()->free();
55 }
56
57 ITensor *get_weights() override
58 {
59 return &_output;
60 }
61
62 uint32_t uid() override
63 {
64 return _uid;
65 }
66
67 void configure(const ITensor *input)
68 {
69 _func.configure(input, &_output);
70 }
71
72 private:
73 static constexpr uint32_t _uid = 0x0;
74 Tensor _output{};
75 NETranspose _func{};
76 };
77 } // namespace weights_transformations
78
79 /** Basic function to compute a Fully Connected layer. This function calls the following kernels:
80 * -# cpu::kernels::CpuIm2ColKernel (called when the input comes from a convolutional layer)
81 * -# @ref NETranspose (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
82 * -# @ref NEGEMM or @ref NEGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
83 * -# cpu::kernels::CpuGemmMatrixAdditionKernel or @ref NEGEMMLowpOutputStage (if quantized asymmetric) (if @p biases is not equal to nullptr)
84 *
85 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
86 */
87 class NEFullyConnectedLayer : public IFunction
88 {
89 public:
90 /** Constructor */
91 NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager = nullptr);
92 NEFullyConnectedLayer() : NEFullyConnectedLayer(MemoryManagerOnDemand::make_default())
93 {
94 }
95 /** Prevent instances of this class from being copied (As this class contains pointers) */
96 NEFullyConnectedLayer(const NEFullyConnectedLayer &) = delete;
97 /** Prevent instances of this class from being moved (As this class contains pointers) */
98 NEFullyConnectedLayer(NEFullyConnectedLayer &&) = delete;
99 /** Prevent instances of this class from being copied (As this class contains pointers) */
100 NEFullyConnectedLayer &operator=(const NEFullyConnectedLayer &) = delete;
101 /** Prevent instances of this class from being moved (As this class contains pointers) */
102 NEFullyConnectedLayer &operator=(NEFullyConnectedLayer &&) = delete;
103 /** Default destructor */
104 ~NEFullyConnectedLayer();
105 /** Set the input and output tensors.
106 *
107 * Valid data layouts:
108 * - NHWC
109 * - NCHW
110 *
111 * Valid data type configurations:
112 * |src0 |src1 |src2 |dst |
113 * |:--------------|:------------------|:------|:--------------|
114 * |F16 |F16 |F16 |F16 |
115 * |F32 |F32 |F32 |F32 |
116 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
117 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
118 *
119 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
120 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
121 * If this function is called after a Convolution Layer, the (transposed) weights will have as many rows as the product of the first 3 input's dimensions.
122 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
123 * Data type supported: Same as @p input.
124 * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
125 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
126 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
127 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
128 * Data type supported: Same as @p input.
129 * @param[in] fc_info (Optional) Fully connected layer additional info
130 * @param[in] weights_info (Optional) Stores neccessary compute information when weights are already reshaped
131 */
132 void configure(const ITensor *input,
133 const ITensor *weights,
134 const ITensor *biases,
135 ITensor *output,
136 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(),
137 const WeightsInfo &weights_info = WeightsInfo());
138 /** Static function to check if given info will lead to a valid configuration of @ref NEFullyConnectedLayer
139 *
140 * Similar to @ref NEFullyConnectedLayer::configure()
141 *
142 * @return a status
143 */
144 static Status validate(const ITensorInfo *input,
145 const ITensorInfo *weights,
146 const ITensorInfo *biases,
147 const ITensorInfo *output,
148 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo(),
149 const WeightsInfo &weights_info = WeightsInfo());
150
151 /** Static function that queries whether fixed-format kernel exists for a given problem description
152 *
153 * @param[out] expected_weight_format Format in which weights should be for found fixed format kernel
154 * @param[in] input Source tensor
155 * @param[in] weights Weights tensor.
156 * @param[in] biases Bias tensor. Can be nullptr. Data type supported: Same as @p weights, S32 if @p weights is QASYMM8/QASYMM8_SIGNED.
157 * @param[in] output Destination tensor
158 * @param[in] fc_info Fully connected layer additional info
159 * @param[in] weights_info Describes weights shape
160 *
161 * @return a status
162 */
163 static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format,
164 const ITensorInfo *input,
165 const ITensorInfo *weights,
166 const ITensorInfo *biases,
167 const ITensorInfo *output,
168 const FullyConnectedLayerInfo &fc_info,
169 const WeightsInfo &weights_info);
170
171 //Inherited methods override
172 void run() override;
173 void prepare() override;
174
175 private:
176 struct Impl;
177 std::unique_ptr<Impl> _impl;
178 };
179 } // namespace arm_compute
180 #endif // ACL_ARM_COMPUTE_RUNTIME_NEON_FUNCTIONS_NEFULLYCONNECTEDLAYER_H