blob: 75cb2dc1fabf66e4946c90a47b6ed01c09638b09 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Teresa Charlin27886092021-02-25 20:15:01 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H
25#define ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010027#include "arm_compute/runtime/CL/ICLSimpleFunction.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010030#include "arm_compute/runtime/CL/functions/CLConvertFullyConnectedWeights.h"
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010031#include "arm_compute/runtime/CL/functions/CLFlattenLayer.h"
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +010032#include "arm_compute/runtime/CL/functions/CLGEMM.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000033#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
Teresa Charlin68508892021-04-07 19:18:08 +010034#include "arm_compute/runtime/CL/functions/CLTranspose.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010035#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010036#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38namespace arm_compute
39{
Michalis Spyroub27e13a2019-09-27 11:04:27 +010040namespace weights_transformations
41{
Teresa Charlin68508892021-04-07 19:18:08 +010042/** Basic function to manage the reshape weights generated from @ref CLTranspose */
Michalis Spyroub27e13a2019-09-27 11:04:27 +010043class CLFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights
44{
45public:
46 //Inherited method override
47 void run() override
48 {
49 _output.allocator()->allocate();
50 _func.run();
51 _reshape_run = true;
52 }
53
54 //Inherited method override
55 void release() override
56 {
57 _output.allocator()->free();
58 }
59
60 //Inherited method override
61 ICLTensor *get_weights() override
62 {
63 return &_output;
64 }
65
66 //Inherited method override
67 uint32_t uid() override
68 {
69 return _uid;
70 }
71
Teresa Charlin68508892021-04-07 19:18:08 +010072 /** Configures the @ref CLTranspose function
Michalis Spyroub27e13a2019-09-27 11:04:27 +010073 *
Manuel Bottini8481d832019-12-10 15:28:40 +000074 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michalis Spyroub27e13a2019-09-27 11:04:27 +010075 */
76 void configure(const ICLTensor *input)
77 {
Manuel Bottini2b84be52020-04-08 10:15:51 +010078 configure(CLKernelLibrary::get().get_compile_context(), input);
79 }
Teresa Charlin68508892021-04-07 19:18:08 +010080 /** Configures the @ref CLTranspose function
Manuel Bottini2b84be52020-04-08 10:15:51 +010081 *
82 * @param[in] compile_context The compile context to be used.
83 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
84 */
85 void configure(const CLCompileContext &compile_context, const ICLTensor *input)
86 {
87 _func.configure(compile_context, input, &_output);
Michalis Spyroub27e13a2019-09-27 11:04:27 +010088 }
89
90private:
Teresa Charlin68508892021-04-07 19:18:08 +010091 static constexpr uint32_t _uid = 0x0;
92 CLTensor _output{};
93 CLTranspose _func{};
Michalis Spyroub27e13a2019-09-27 11:04:27 +010094};
95} // namespace weights_transformations
96
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
98 *
99 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
Teresa Charlin68508892021-04-07 19:18:08 +0100100 * -# @ref CLTranspose (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000101 * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 *
103 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
104 */
105class CLFullyConnectedLayer : public IFunction
106{
107public:
108 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100109 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000110 /** Prevent instances of this class from being copied (As this class contains pointers) */
111 CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete;
112 /** Default move constructor */
113 CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default;
114 /** Prevent instances of this class from being copied (As this class contains pointers) */
115 CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete;
116 /** Default move assignment operator */
117 CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 /** Set the input and output tensors.
119 *
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000120 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100121 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
122 * 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.
123 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
124 * Data type supported: Same as @p input.
125 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
126 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
127 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
128 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
129 * Data type supported: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100130 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100132 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
133 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Manuel Bottini2b84be52020-04-08 10:15:51 +0100134 /** Set the input and output tensors.
135 *
136 * @param[in] compile_context The compile context to be used.
137 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
138 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
139 * 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.
140 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
141 * Data type supported: Same as @p input.
142 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
143 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
144 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
145 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
146 * Data type supported: Same as @p input.
147 * @param[in] fc_info (Optional) Fully connected layer additional info
148 */
149 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
150 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000151 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer
152 *
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000153 * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100154 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
155 * 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.
156 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
157 * Data type supported: Same as @p input.
158 * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input.
159 * @param[out] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
160 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
161 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
162 * Data type supported: Same as @p input.
163 * @param[in] fc_info (Optional) Fully connected layer additional info
Georgios Pinitas358ca202017-12-07 16:47:52 +0000164 *
165 * @return a status
166 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100167 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
168 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169
170 //Inherited methods override
171 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100172 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173
174private:
Manuel Bottini2b84be52020-04-08 10:15:51 +0100175 void configure_fc_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
176 void configure_conv_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
177 void configure_mm(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100178
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100179 MemoryGroup _memory_group;
180 IWeightsManager *_weights_manager;
181 CLConvertFullyConnectedWeights _convert_weights;
182 weights_transformations::CLConvertFullyConnectedWeightsManaged _convert_weights_managed;
183 weights_transformations::CLFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function;
184 CLFlattenLayer _flatten_layer;
Teresa Charlin68508892021-04-07 19:18:08 +0100185 CLTranspose _reshape_weights_function;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100186 CLGEMM _mm_gemm;
187 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100188 CLTensor _flatten_output;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100189 CLTensor _converted_weights_output;
190 CLTensor _reshape_weights_output;
191 bool _are_weights_converted;
192 bool _are_weights_reshaped;
193 bool _is_fc_after_conv;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100194 bool _is_quantized;
195 bool _is_prepared;
196 const ICLTensor *_original_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197};
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100198} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000199#endif /* ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H */