blob: 29788742d76a0b921c7fda61c872198fbca7e1d6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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/core/CL/kernels/CLTransposeKernel.h"
30#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010031#include "arm_compute/runtime/CL/functions/CLConvertFullyConnectedWeights.h"
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010032#include "arm_compute/runtime/CL/functions/CLFlattenLayer.h"
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +010033#include "arm_compute/runtime/CL/functions/CLGEMM.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000034#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.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{
40/** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls the following kernels:
41 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010042 * -# @ref CLTransposeKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 *
44 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
45 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010046class CLFullyConnectedLayerReshapeWeights : public ICLSimpleFunction
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047{
48public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 /** Set the input and output tensors.
50 *
Manuel Bottini8481d832019-12-10 15:28:40 +000051 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010052 * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010054 void configure(const ICLTensor *input, ICLTensor *output);
Manuel Bottini2b84be52020-04-08 10:15:51 +010055 /** Set the input and output tensors.
56 *
57 * @param[in] compile_context The compile context to be used.
58 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
59 * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
60 */
61 void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000062 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayerReshapeWeights
63 *
Manuel Bottini8481d832019-12-10 15:28:40 +000064 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Georgios Pinitas358ca202017-12-07 16:47:52 +000065 * @param[in] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
66 *
67 * @return a status
68 */
69 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070};
71
Michalis Spyroub27e13a2019-09-27 11:04:27 +010072namespace weights_transformations
73{
74/** Basic function to manage the reshape weights generated from @ref CLFullyConnectedLayerReshapeWeights */
75class CLFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights
76{
77public:
78 //Inherited method override
79 void run() override
80 {
81 _output.allocator()->allocate();
82 _func.run();
83 _reshape_run = true;
84 }
85
86 //Inherited method override
87 void release() override
88 {
89 _output.allocator()->free();
90 }
91
92 //Inherited method override
93 ICLTensor *get_weights() override
94 {
95 return &_output;
96 }
97
98 //Inherited method override
99 uint32_t uid() override
100 {
101 return _uid;
102 }
103
104 /** Configures the @ref CLFullyConnectedLayerReshapeWeights function
105 *
Manuel Bottini8481d832019-12-10 15:28:40 +0000106 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100107 */
108 void configure(const ICLTensor *input)
109 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100110 configure(CLKernelLibrary::get().get_compile_context(), input);
111 }
112 /** Configures the @ref CLFullyConnectedLayerReshapeWeights function
113 *
114 * @param[in] compile_context The compile context to be used.
115 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
116 */
117 void configure(const CLCompileContext &compile_context, const ICLTensor *input)
118 {
119 _func.configure(compile_context, input, &_output);
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100120 }
121
122private:
123 static constexpr uint32_t _uid = 0x0;
124 CLTensor _output{};
125 CLFullyConnectedLayerReshapeWeights _func{};
126};
127} // namespace weights_transformations
128
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
130 *
131 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100132 * -# @ref CLFullyConnectedLayerReshapeWeights (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 +0000133 * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 *
135 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
136 */
137class CLFullyConnectedLayer : public IFunction
138{
139public:
140 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100141 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000142 /** Prevent instances of this class from being copied (As this class contains pointers) */
143 CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete;
144 /** Default move constructor */
145 CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default;
146 /** Prevent instances of this class from being copied (As this class contains pointers) */
147 CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete;
148 /** Default move assignment operator */
149 CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 /** Set the input and output tensors.
151 *
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000152 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100153 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
154 * 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.
155 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
156 * Data type supported: Same as @p input.
157 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
158 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
159 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
160 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
161 * Data type supported: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100162 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100164 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
165 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Manuel Bottini2b84be52020-04-08 10:15:51 +0100166 /** Set the input and output tensors.
167 *
168 * @param[in] compile_context The compile context to be used.
169 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
170 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
171 * 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.
172 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
173 * Data type supported: Same as @p input.
174 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
175 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
176 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
177 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
178 * Data type supported: Same as @p input.
179 * @param[in] fc_info (Optional) Fully connected layer additional info
180 */
181 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
182 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000183 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer
184 *
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000185 * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100186 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
187 * 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.
188 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
189 * Data type supported: Same as @p input.
190 * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input.
191 * @param[out] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
192 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
193 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
194 * Data type supported: Same as @p input.
195 * @param[in] fc_info (Optional) Fully connected layer additional info
Georgios Pinitas358ca202017-12-07 16:47:52 +0000196 *
197 * @return a status
198 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100199 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
200 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201
202 //Inherited methods override
203 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100204 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205
206private:
Manuel Bottini2b84be52020-04-08 10:15:51 +0100207 void configure_fc_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
208 void configure_conv_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
209 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 +0100210
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100211 MemoryGroup _memory_group;
212 IWeightsManager *_weights_manager;
213 CLConvertFullyConnectedWeights _convert_weights;
214 weights_transformations::CLConvertFullyConnectedWeightsManaged _convert_weights_managed;
215 weights_transformations::CLFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function;
216 CLFlattenLayer _flatten_layer;
217 CLFullyConnectedLayerReshapeWeights _reshape_weights_function;
218 CLGEMM _mm_gemm;
219 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100220 CLTensor _flatten_output;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100221 CLTensor _converted_weights_output;
222 CLTensor _reshape_weights_output;
223 bool _are_weights_converted;
224 bool _are_weights_reshaped;
225 bool _is_fc_after_conv;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100226 bool _is_quantized;
227 bool _is_prepared;
228 const ICLTensor *_original_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229};
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100230} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000231#endif /* ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H */