blob: e076f51b26d70ea01e088780c2c4e1ca547580ca [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"
33#include "arm_compute/runtime/CL/CLTensor.h"
34
35namespace arm_compute
36{
37/** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls the following kernels:
38 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010039 * -# @ref CLTransposeKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 *
41 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
42 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010043class CLFullyConnectedLayerReshapeWeights : public ICLSimpleFunction
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044{
45public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 /** Set the input and output tensors.
47 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010048 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QS8/QS16/F16/F32.
49 * @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 +010050 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010051 void configure(const ICLTensor *input, ICLTensor *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052};
53
54/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
55 *
56 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010057 * -# @ref CLFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 * -# @ref CLGEMMMatrixMultiplyKernel
59 * -# @ref CLGEMMMatrixAccumulateBiasesKernel (if @p biases is not equal to nullptr)
60 *
61 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
62 */
63class CLFullyConnectedLayer : public IFunction
64{
65public:
66 /** Constructor */
67 CLFullyConnectedLayer();
68 /** Set the input and output tensors.
69 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010070 * @param[in] input Source tensor. Data type supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input
72 * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input.
73 * @param[out] output Destination tensor. Data type supported: Same as @p input.
74 * @param[in] transpose_weights (Optional) Transpose weights if true. Defaults to true.
75 * @param[in] are_weights_reshaped (Optional) Reshape the weights tensor if false. Defaults to false.
76 */
77 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights = true, bool are_weights_reshaped = false);
78
79 //Inherited methods override
80 void run() override;
81
82private:
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010083 void configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
84 void configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
85
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 CLIm2ColKernel _im2col_kernel;
87 CLFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 CLGEMMMatrixMultiplyKernel _mm_kernel;
89 CLGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel;
90 CLTensor _im2col_output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 CLTensor _reshape_weights_output;
92 bool _are_weights_reshaped;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010093 bool _is_fc_after_conv;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 bool _accumulate_biases;
95};
96}
97#endif /* __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__ */