blob: 1e9ee492addfecc62bf4cd090d153290970e16cc [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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 __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"
30#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
32#include "arm_compute/core/CL/kernels/CLTransposeKernel.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010033#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/CL/CLTensor.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"
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 *
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000051 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QS8/QASYMM8/QS16/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);
Georgios Pinitas358ca202017-12-07 16:47:52 +000055 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayerReshapeWeights
56 *
57 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QS8/QASYMM8/QS16/F16/F32.
58 * @param[in] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
59 *
60 * @return a status
61 */
62 static Status validate(const ITensorInfo *input, const ITensorInfo *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063};
64
65/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
66 *
67 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010068 * -# @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 +000069 * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
70 * -# @ref CLGEMMMatrixAccumulateBiasesKernel or @ref CLGEMMLowpQuantizeDownInt32ToUint8Scale (if quantized asymmetric) (if @p biases is not equal to nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 *
72 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
73 */
74class CLFullyConnectedLayer : public IFunction
75{
76public:
77 /** Constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010078 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 /** Set the input and output tensors.
80 *
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000081 * @param[in] input Source tensor. Data type supported: QS8/QASYMM8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input
83 * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input.
84 * @param[out] output Destination tensor. Data type supported: Same as @p input.
85 * @param[in] transpose_weights (Optional) Transpose weights if true. Defaults to true.
86 * @param[in] are_weights_reshaped (Optional) Reshape the weights tensor if false. Defaults to false.
87 */
88 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights = true, bool are_weights_reshaped = false);
Georgios Pinitas358ca202017-12-07 16:47:52 +000089 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer
90 *
91 * @param[in] input Source tensor. Data type supported: QS8/QASYMM8/QS16/F16/F32.
92 * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input
93 * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input.
94 * @param[in] output Destination tensor. Data type supported: Same as @p input.
95 * @param[in] transpose_weights (Optional) Transpose weights if true. Defaults to true.
96 * @param[in] are_weights_reshaped (Optional) Reshape the weights tensor if false. Defaults to false.
97 *
98 * @return a status
99 */
100 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose_weights = true, bool are_weights_reshaped = false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
102 //Inherited methods override
103 void run() override;
104
105private:
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000106 void configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
107 void configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
108 void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool is_interleaved_transposed = true);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100109
Gian Marco58c57942017-11-28 09:10:03 +0000110 CLMemoryGroup _memory_group;
111 CLIm2ColKernel _im2col_kernel;
112 CLFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
113 CLGEMMMatrixMultiplyKernel _mm_kernel;
114 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp;
115 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage;
116 CLGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel;
117 CLTensor _im2col_output;
118 CLTensor _gemmlowp_output;
119 CLTensor _reshape_weights_output;
120 bool _are_weights_reshaped;
121 bool _is_fc_after_conv;
122 bool _accumulate_biases;
123 bool _is_quantized;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124};
125}
126#endif /* __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__ */