blob: 63565df1a71d0529cde06aa116aa2d723198d09b [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Stephen Lie855c232018-01-04 14:13:22 +08002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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_GCFULLYCONNECTEDLAYER_H__
25#define __ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H__
26
27#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixAccumulateBiasesKernel.h"
28#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
29#include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h"
30#include "arm_compute/core/GLES_COMPUTE/kernels/GCTransposeKernel.h"
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000031#include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010032#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
33#include "arm_compute/runtime/GLES_COMPUTE/IGCSimpleFunction.h"
34
35namespace arm_compute
36{
37/** Basic function to reshape the weights of Fully Connected layer with OpenGL ES. This function calls the following kernels:
38 *
39 * -# @ref GCTransposeKernel
40 *
41 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
42 */
43class GCFullyConnectedLayerReshapeWeights : public IGCSimpleFunction
44{
45public:
46 /** Set the input and output tensors.
47 *
48 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: F16/F32.
49 * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
50 */
51 void configure(const IGCTensor *input, IGCTensor *output);
52};
53
54/** Basic function to compute a Fully Connected layer on OpenGL ES. This function calls the following OpenGL ES kernels:
55 *
56 * -# @ref GCIm2ColKernel (called when the input comes from a convolutional layer)
57 * -# @ref GCFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
58 * -# @ref GCGEMMMatrixMultiplyKernel
59 * -# @ref GCGEMMMatrixAccumulateBiasesKernel (if @p biases is not equal to nullptr)
60 *
61 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
62 */
63class GCFullyConnectedLayer : public IFunction
64{
65public:
66 /** Constructor */
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000067 GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010068 /** Prevent instances of this class from being copied (As this class contains pointers) */
69 GCFullyConnectedLayer(const GCFullyConnectedLayer &) = delete;
70 /** Default move constructor */
71 GCFullyConnectedLayer(GCFullyConnectedLayer &&) = default;
72 /** Prevent instances of this class from being copied (As this class contains pointers) */
73 GCFullyConnectedLayer &operator=(const GCFullyConnectedLayer &) = delete;
74 /** Default move assignment operator */
75 GCFullyConnectedLayer &operator=(GCFullyConnectedLayer &&) = default;
Anthony Barbier7068f992017-10-26 15:23:08 +010076 /** Set the input and output tensors.
77 *
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010078 * @param[in] input Source tensor. Data type supported: F16/F32.
79 * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input
80 * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input.
81 * @param[out] output Destination tensor. Data type supported: Same as @p input.
82 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier7068f992017-10-26 15:23:08 +010083 */
Georgios Pinitasceff0f92018-03-19 19:57:01 +000084 void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010085 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier7068f992017-10-26 15:23:08 +010086
87 //Inherited methods override
88 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +010089 void prepare() override;
Anthony Barbier7068f992017-10-26 15:23:08 +010090
91private:
92 void configure_fc_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
93 void configure_conv_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
94
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000095 GCMemoryGroup _memory_group;
Anthony Barbier7068f992017-10-26 15:23:08 +010096 GCIm2ColKernel _im2col_kernel;
97 GCFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
98 GCGEMMMatrixMultiplyKernel _mm_kernel;
99 GCGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel;
100 GCTensor _im2col_output;
101 GCTensor _reshape_weights_output;
Georgios Pinitas72219332018-06-05 14:56:06 +0100102 const IGCTensor *_original_weights;
Anthony Barbier7068f992017-10-26 15:23:08 +0100103 bool _are_weights_reshaped;
104 bool _is_fc_after_conv;
105 bool _accumulate_biases;
106};
107}
108#endif /* __ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H__ */