Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Arm Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H |
| 25 | #define ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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" |
| 31 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
| 32 | #include "arm_compute/runtime/GLES_COMPUTE/IGCSimpleFunction.h" |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/IWeightsManager.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 35 | |
| 36 | namespace 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 | */ |
| 44 | class GCFullyConnectedLayerReshapeWeights : public IGCSimpleFunction |
| 45 | { |
| 46 | public: |
| 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 | */ |
| 64 | class GCFullyConnectedLayer : public IFunction |
| 65 | { |
| 66 | public: |
| 67 | /** Constructor */ |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 68 | GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 69 | /** 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 77 | /** Set the input and output tensors. |
| 78 | * |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 79 | * @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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 84 | */ |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 85 | void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 86 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 87 | |
| 88 | //Inherited methods override |
| 89 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 90 | void prepare() override; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 91 | |
| 92 | private: |
| 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 Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 96 | MemoryGroup _memory_group; |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 97 | IWeightsManager *_weights_manager; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 98 | 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 Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 104 | const IGCTensor *_original_weights; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 105 | bool _are_weights_reshaped; |
| 106 | bool _is_fc_after_conv; |
| 107 | bool _accumulate_biases; |
| 108 | }; |
| 109 | } |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 110 | #endif /* ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H */ |