aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/runtime/GLES_COMPUTE/functions/GCFullyConnectedLayer.h
blob: 64e98e2f4d2a1dffce51f4bad7a16ebbb064c9d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2017-2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef __ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H__
#define __ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H__

#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixAccumulateBiasesKernel.h"
#include "arm_compute/core/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h"
#include "arm_compute/core/GLES_COMPUTE/kernels/GCIm2ColKernel.h"
#include "arm_compute/core/GLES_COMPUTE/kernels/GCTransposeKernel.h"
#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
#include "arm_compute/runtime/GLES_COMPUTE/IGCSimpleFunction.h"
#include "arm_compute/runtime/IWeightsManager.h"
#include "arm_compute/runtime/MemoryGroup.h"

namespace arm_compute
{
/** Basic function to reshape the weights of Fully Connected layer with OpenGL ES. This function calls the following kernels:
 *
 *  -# @ref GCTransposeKernel
 *
 * @note  The fully connected layer accepts "weights" tensors only with 2 dimensions.
 */
class GCFullyConnectedLayerReshapeWeights : public IGCSimpleFunction
{
public:
    /** Set the input and output tensors.
     *
     * @param[in]  input  Weights tensor. The weights must be 2 dimensional. Data types supported: F16/F32.
     * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
     */
    void configure(const IGCTensor *input, IGCTensor *output);
};

/** Basic function to compute a Fully Connected layer on OpenGL ES. This function calls the following OpenGL ES kernels:
 *
 *  -# @ref GCIm2ColKernel (called when the input comes from a convolutional layer)
 *  -# @ref GCFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
 *  -# @ref GCGEMMMatrixMultiplyKernel
 *  -# @ref GCGEMMMatrixAccumulateBiasesKernel (if @p biases is not equal to nullptr)
 *
 * @note  The fully connected layer accepts "weights" tensors only with 2 dimensions.
 */
class GCFullyConnectedLayer : public IFunction
{
public:
    /** Constructor */
    GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    GCFullyConnectedLayer(const GCFullyConnectedLayer &) = delete;
    /** Default move constructor */
    GCFullyConnectedLayer(GCFullyConnectedLayer &&) = default;
    /** Prevent instances of this class from being copied (As this class contains pointers) */
    GCFullyConnectedLayer &operator=(const GCFullyConnectedLayer &) = delete;
    /** Default move assignment operator */
    GCFullyConnectedLayer &operator=(GCFullyConnectedLayer &&) = default;
    /** Set the input and output tensors.
     *
     * @param[in]  input   Source tensor. Data type supported: F16/F32.
     * @param[in]  weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input
     * @param[in]  biases  Bias tensor. It can be nullptr. Data type supported:Same as @p input.
     * @param[out] output  Destination tensor. Data type supported: Same as @p input.
     * @param[in]  fc_info (Optional) Fully connected layer additional info
     */
    void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output,
                   FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());

    //Inherited methods override
    void run() override;
    void prepare() override;

private:
    void configure_fc_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);
    void configure_conv_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output);

    MemoryGroup                         _memory_group;
    IWeightsManager                    *_weights_manager;
    GCIm2ColKernel                      _im2col_kernel;
    GCFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
    GCGEMMMatrixMultiplyKernel          _mm_kernel;
    GCGEMMMatrixAccumulateBiasesKernel  _accumulate_biases_kernel;
    GCTensor                            _im2col_output;
    GCTensor                            _reshape_weights_output;
    const IGCTensor                    *_original_weights;
    bool                                _are_weights_reshaped;
    bool                                _is_fc_after_conv;
    bool                                _accumulate_biases;
};
}
#endif /* __ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H__ */