blob: a13c74a6838c8cdf0fc9b24874c0388f6115777a [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H
25#define ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H
Anthony Barbier7068f992017-10-26 15:23:08 +010026
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"
31#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
32#include "arm_compute/runtime/GLES_COMPUTE/IGCSimpleFunction.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010033#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010034#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010035
36namespace arm_compute
37{
38/** Basic function to reshape the weights of Fully Connected layer with OpenGL ES. This function calls the following kernels:
39 *
40 * -# @ref GCTransposeKernel
41 *
42 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
43 */
44class GCFullyConnectedLayerReshapeWeights : public IGCSimpleFunction
45{
46public:
47 /** Set the input and output tensors.
48 *
49 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: F16/F32.
50 * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
51 */
52 void configure(const IGCTensor *input, IGCTensor *output);
53};
54
55/** Basic function to compute a Fully Connected layer on OpenGL ES. This function calls the following OpenGL ES kernels:
56 *
57 * -# @ref GCIm2ColKernel (called when the input comes from a convolutional layer)
58 * -# @ref GCFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
59 * -# @ref GCGEMMMatrixMultiplyKernel
60 * -# @ref GCGEMMMatrixAccumulateBiasesKernel (if @p biases is not equal to nullptr)
61 *
62 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
63 */
64class GCFullyConnectedLayer : public IFunction
65{
66public:
67 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +010068 GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas72219332018-06-05 14:56:06 +010069 /** Prevent instances of this class from being copied (As this class contains pointers) */
70 GCFullyConnectedLayer(const GCFullyConnectedLayer &) = delete;
71 /** Default move constructor */
72 GCFullyConnectedLayer(GCFullyConnectedLayer &&) = default;
73 /** Prevent instances of this class from being copied (As this class contains pointers) */
74 GCFullyConnectedLayer &operator=(const GCFullyConnectedLayer &) = delete;
75 /** Default move assignment operator */
76 GCFullyConnectedLayer &operator=(GCFullyConnectedLayer &&) = default;
Anthony Barbier7068f992017-10-26 15:23:08 +010077 /** Set the input and output tensors.
78 *
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010079 * @param[in] input Source tensor. Data type supported: F16/F32.
80 * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input
81 * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input.
82 * @param[out] output Destination tensor. Data type supported: Same as @p input.
83 * @param[in] fc_info (Optional) Fully connected layer additional info
Anthony Barbier7068f992017-10-26 15:23:08 +010084 */
Georgios Pinitasceff0f92018-03-19 19:57:01 +000085 void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output,
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010086 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier7068f992017-10-26 15:23:08 +010087
88 //Inherited methods override
89 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +010090 void prepare() override;
Anthony Barbier7068f992017-10-26 15:23:08 +010091
92private:
93 void configure_fc_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
94 void configure_conv_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
95
Georgios Pinitas26014cf2019-09-09 19:00:57 +010096 MemoryGroup _memory_group;
Michalis Spyrou6bff1952019-10-02 17:22:11 +010097 IWeightsManager *_weights_manager;
Anthony Barbier7068f992017-10-26 15:23:08 +010098 GCIm2ColKernel _im2col_kernel;
99 GCFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
100 GCGEMMMatrixMultiplyKernel _mm_kernel;
101 GCGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel;
102 GCTensor _im2col_output;
103 GCTensor _reshape_weights_output;
Georgios Pinitas72219332018-06-05 14:56:06 +0100104 const IGCTensor *_original_weights;
Anthony Barbier7068f992017-10-26 15:23:08 +0100105 bool _are_weights_reshaped;
106 bool _is_fc_after_conv;
107 bool _accumulate_biases;
108};
109}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000110#endif /* ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H */