blob: e800dd7cbb92f1c8b789b3e7884b60c49e40c7a0 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgioebc3a902018-11-16 16:04:25 +00002 * Copyright (c) 2017-2019 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 */
24#ifndef __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__
25#define __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__
26
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"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010031#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010033#include "arm_compute/runtime/CL/functions/CLConvertFullyConnectedWeights.h"
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010034#include "arm_compute/runtime/CL/functions/CLFlattenLayer.h"
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +010035#include "arm_compute/runtime/CL/functions/CLGEMM.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000036#include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
37#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39namespace arm_compute
40{
41/** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls the following kernels:
42 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010043 * -# @ref CLTransposeKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044 *
45 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
46 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010047class CLFullyConnectedLayerReshapeWeights : public ICLSimpleFunction
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
49public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050 /** Set the input and output tensors.
51 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010052 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/F16/F32.
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010053 * @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 +010054 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010055 void configure(const ICLTensor *input, ICLTensor *output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000056 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayerReshapeWeights
57 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010058 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/F16/F32.
Georgios Pinitas358ca202017-12-07 16:47:52 +000059 * @param[in] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
60 *
61 * @return a status
62 */
63 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064};
65
66/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
67 *
68 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010069 * -# @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 +000070 * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
Isabella Gottardia1667fb2018-04-11 11:53:49 +010071 * -# @ref CLGEMMMatrixAccumulateBiasesKernel or @ref CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 *
73 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
74 */
75class CLFullyConnectedLayer : public IFunction
76{
77public:
78 /** Constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010079 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +000080 /** Prevent instances of this class from being copied (As this class contains pointers) */
81 CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete;
82 /** Default move constructor */
83 CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default;
84 /** Prevent instances of this class from being copied (As this class contains pointers) */
85 CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete;
86 /** Default move assignment operator */
87 CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 /** Set the input and output tensors.
89 *
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010090 * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32.
Giorgio Arenaa855af12018-07-16 17:20:38 +010091 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
92 * 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.
93 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
94 * Data type supported: Same as @p input.
95 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
96 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
97 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
98 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
99 * Data type supported: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100100 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100102 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
103 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +0000104 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer
105 *
Giorgio Arenaa855af12018-07-16 17:20:38 +0100106 * @param[in] input Source tensor info. Data type supported: QASYMM8/F16/F32.
107 * @param[in] weights Weights tensor info. The weights must be 2 dimensional.
108 * 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.
109 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
110 * Data type supported: Same as @p input.
111 * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input.
112 * @param[out] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between:
113 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
114 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
115 * Data type supported: Same as @p input.
116 * @param[in] fc_info (Optional) Fully connected layer additional info
Georgios Pinitas358ca202017-12-07 16:47:52 +0000117 *
118 * @return a status
119 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100120 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
121 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
123 //Inherited methods override
124 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100125 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126
127private:
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +0100128 void configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool retain_internal_weights);
129 void configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool retain_internal_weights);
130 void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool retain_internal_weights);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100131
Gian Marco58c57942017-11-28 09:10:03 +0000132 CLMemoryGroup _memory_group;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100133 CLConvertFullyConnectedWeights _convert_weights;
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100134 CLFlattenLayer _flatten_layer;
Gian Marco58c57942017-11-28 09:10:03 +0000135 CLFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
Gian Marco Iodicec9c62c22018-04-06 10:00:10 +0100136 CLGEMM _mm_gemm;
Gian Marco58c57942017-11-28 09:10:03 +0000137 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
138 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
Michele Di Giorgioebc3a902018-11-16 16:04:25 +0000139 CLGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel; // TODO(COMPMID-1889): Use CLGEMM to add bias in CLFullyConnectedLayer
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +0100140 CLTensor _flatten_output;
Gian Marco58c57942017-11-28 09:10:03 +0000141 CLTensor _gemmlowp_output;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100142 CLTensor _converted_weights_output;
Gian Marco58c57942017-11-28 09:10:03 +0000143 CLTensor _reshape_weights_output;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100144 bool _are_weights_converted;
Gian Marco58c57942017-11-28 09:10:03 +0000145 bool _are_weights_reshaped;
146 bool _is_fc_after_conv;
147 bool _accumulate_biases;
148 bool _is_quantized;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100149 bool _is_prepared;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000150 const ICLTensor *_original_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151};
152}
153#endif /* __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__ */