blob: 39c3c2b85b4dff12208993415a6bfc6ee8e3f6b3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * 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/CLGEMMMatrixAccumulateBiasesKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/CL/kernels/CLTransposeKernel.h"
31#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010032#include "arm_compute/runtime/CL/functions/CLConvertFullyConnectedWeights.h"
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010033#include "arm_compute/runtime/CL/functions/CLFlattenLayer.h"
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +010034#include "arm_compute/runtime/CL/functions/CLGEMM.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000035#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
36#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010037#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010038#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40namespace arm_compute
41{
42/** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls the following kernels:
43 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010044 * -# @ref CLTransposeKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 *
46 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
47 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010048class CLFullyConnectedLayerReshapeWeights : public ICLSimpleFunction
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049{
50public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 /** Set the input and output tensors.
52 *
Manuel Bottini8481d832019-12-10 15:28:40 +000053 * @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 +010054 * @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 +010055 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010056 void configure(const ICLTensor *input, ICLTensor *output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000057 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayerReshapeWeights
58 *
Manuel Bottini8481d832019-12-10 15:28:40 +000059 * @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 +000060 * @param[in] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
61 *
62 * @return a status
63 */
64 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065};
66
Michalis Spyroub27e13a2019-09-27 11:04:27 +010067namespace weights_transformations
68{
69/** Basic function to manage the reshape weights generated from @ref CLFullyConnectedLayerReshapeWeights */
70class CLFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights
71{
72public:
73 //Inherited method override
74 void run() override
75 {
76 _output.allocator()->allocate();
77 _func.run();
78 _reshape_run = true;
79 }
80
81 //Inherited method override
82 void release() override
83 {
84 _output.allocator()->free();
85 }
86
87 //Inherited method override
88 ICLTensor *get_weights() override
89 {
90 return &_output;
91 }
92
93 //Inherited method override
94 uint32_t uid() override
95 {
96 return _uid;
97 }
98
99 /** Configures the @ref CLFullyConnectedLayerReshapeWeights function
100 *
Manuel Bottini8481d832019-12-10 15:28:40 +0000101 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100102 */
103 void configure(const ICLTensor *input)
104 {
105 _func.configure(input, &_output);
106 }
107
108private:
109 static constexpr uint32_t _uid = 0x0;
110 CLTensor _output{};
111 CLFullyConnectedLayerReshapeWeights _func{};
112};
113} // namespace weights_transformations
114
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
116 *
117 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100118 * -# @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 +0000119 * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
Isabella Gottardia1667fb2018-04-11 11:53:49 +0100120 * -# @ref CLGEMMMatrixAccumulateBiasesKernel or @ref CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 *
122 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
123 */
124class CLFullyConnectedLayer : public IFunction
125{
126public:
127 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100128 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +0000129 /** Prevent instances of this class from being copied (As this class contains pointers) */
130 CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete;
131 /** Default move constructor */
132 CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default;
133 /** Prevent instances of this class from being copied (As this class contains pointers) */
134 CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete;
135 /** Default move assignment operator */
136 CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 /** Set the input and output tensors.
138 *
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100139 * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100140 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
141 * 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.
142 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
143 * Data type supported: Same as @p input.
144 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
145 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
146 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
147 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
148 * Data type supported: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100149 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100151 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
152 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000153 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer
154 *
Giorgio Arenaa855af12018-07-16 17:20:38 +0100155 * @param[in] input Source tensor info. Data type supported: QASYMM8/F16/F32.
156 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
157 * 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.
158 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
159 * Data type supported: Same as @p input.
160 * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input.
161 * @param[out] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
162 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
163 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
164 * Data type supported: Same as @p input.
165 * @param[in] fc_info (Optional) Fully connected layer additional info
Georgios Pinitas358ca202017-12-07 16:47:52 +0000166 *
167 * @return a status
168 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100169 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
170 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171
172 //Inherited methods override
173 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100174 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175
176private:
Georgios Pinitas44bfc3f2019-10-28 14:16:31 +0000177 void configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
178 void configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
179 void configure_mm(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100180
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100181 MemoryGroup _memory_group;
182 IWeightsManager *_weights_manager;
183 CLConvertFullyConnectedWeights _convert_weights;
184 weights_transformations::CLConvertFullyConnectedWeightsManaged _convert_weights_managed;
185 weights_transformations::CLFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function;
186 CLFlattenLayer _flatten_layer;
187 CLFullyConnectedLayerReshapeWeights _reshape_weights_function;
188 CLGEMM _mm_gemm;
189 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100190 CLTensor _flatten_output;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100191 CLTensor _converted_weights_output;
192 CLTensor _reshape_weights_output;
193 bool _are_weights_converted;
194 bool _are_weights_reshaped;
195 bool _is_fc_after_conv;
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100196 bool _is_quantized;
197 bool _is_prepared;
198 const ICLTensor *_original_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199};
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100200} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000201#endif /* ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H */