Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 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 Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 31 | #include "arm_compute/runtime/GLES_COMPUTE/GCMemoryGroup.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
| 33 | #include "arm_compute/runtime/GLES_COMPUTE/IGCSimpleFunction.h" |
| 34 | |
| 35 | namespace 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 | */ |
| 43 | class GCFullyConnectedLayerReshapeWeights : public IGCSimpleFunction |
| 44 | { |
| 45 | public: |
| 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 | */ |
| 63 | class GCFullyConnectedLayer : public IFunction |
| 64 | { |
| 65 | public: |
| 66 | /** Constructor */ |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 67 | GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 68 | /** 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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 76 | /** Set the input and output tensors. |
| 77 | * |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 78 | * @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 Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 83 | */ |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 84 | 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] | 85 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 86 | |
| 87 | //Inherited methods override |
| 88 | void run() override; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 89 | void prepare() override; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 90 | |
| 91 | private: |
| 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 Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 95 | GCMemoryGroup _memory_group; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 96 | 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 Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 102 | const IGCTensor *_original_weights; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 103 | bool _are_weights_reshaped; |
| 104 | bool _is_fc_after_conv; |
| 105 | bool _accumulate_biases; |
| 106 | }; |
| 107 | } |
| 108 | #endif /* __ARM_COMPUTE_GCFULLYCONNECTEDLAYER_H__ */ |