Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, 2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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_CLFULLYCONNECTEDLAYER_H |
| 25 | #define ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 27 | #include "arm_compute/function_info/FullyConnectedLayerInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLTensor.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/IFunction.h" |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/IWeightsManager.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | /** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels: |
| 36 | * |
Manuel Bottini | d844c08 | 2021-07-14 12:58:54 +0100 | [diff] [blame] | 37 | * -# @ref opencl::kernels::ClIm2ColKernel (called when the input comes from a convolutional layer) |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 38 | * -# @ref CLTranspose (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once) |
Gian Marco Iodice | c9cecc0 | 2021-10-15 10:23:24 +0100 | [diff] [blame] | 39 | * -# @ref opencl::ClGemm or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | * |
| 41 | * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. |
| 42 | */ |
| 43 | class CLFullyConnectedLayer : public IFunction |
| 44 | { |
| 45 | public: |
| 46 | /** Constructor */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, |
| 48 | IWeightsManager *weights_manager = nullptr); |
Georgios Pinitas | 529b5a2 | 2021-07-27 15:55:30 +0100 | [diff] [blame] | 49 | /** Default destructor */ |
| 50 | ~CLFullyConnectedLayer(); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 51 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 52 | CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete; |
| 53 | /** Default move constructor */ |
| 54 | CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default; |
| 55 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 56 | CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete; |
| 57 | /** Default move assignment operator */ |
| 58 | CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | /** Set the input and output tensors. |
| 60 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 61 | * Valid data layouts: |
| 62 | * - NHWC |
| 63 | * - NCHW |
| 64 | * |
| 65 | * Valid data type configurations: |
| 66 | * |src0 |src1 |src2 |dst | |
| 67 | * |:--------------|:------------------|:------|:--------------| |
| 68 | * |F16 |F16 |F16 |F16 | |
| 69 | * |F32 |F32 |F32 |F32 | |
| 70 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 71 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 72 | * |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 73 | * @param[in] compile_context The compile context to be used. |
| 74 | * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 75 | * @param[in] weights Weights tensor. The weights must be 2 dimensional. |
| 76 | * If this function is called after a Convolution Layer, the (transposed) weights will have as many rows as the product of the first 3 input's dimensions. |
| 77 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 78 | * Data type supported: Same as @p input. |
| 79 | * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input. |
| 80 | * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between: |
| 81 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 82 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 83 | * Data type supported: Same as @p input. |
| 84 | * @param[in] fc_info (Optional) Fully connected layer additional info |
| 85 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | void configure(const CLCompileContext &compile_context, |
| 87 | const ICLTensor *input, |
| 88 | const ICLTensor *weights, |
| 89 | const ICLTensor *biases, |
| 90 | ICLTensor *output, |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 91 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Georgios Pinitas | 529b5a2 | 2021-07-27 15:55:30 +0100 | [diff] [blame] | 92 | /** Set the input and output tensors. |
| 93 | * |
| 94 | * Similar to @ref CLFullyConnectedLayer |
| 95 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | void configure(const ICLTensor *input, |
| 97 | const ICLTensor *weights, |
| 98 | const ICLTensor *biases, |
| 99 | ICLTensor *output, |
Georgios Pinitas | 529b5a2 | 2021-07-27 15:55:30 +0100 | [diff] [blame] | 100 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 101 | /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer |
| 102 | * |
Georgios Pinitas | 529b5a2 | 2021-07-27 15:55:30 +0100 | [diff] [blame] | 103 | * Similar to @ref CLFullyConnectedLayer |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 104 | * |
| 105 | * @return a status |
| 106 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | static Status validate(const ITensorInfo *input, |
| 108 | const ITensorInfo *weights, |
| 109 | const ITensorInfo *biases, |
| 110 | const ITensorInfo *output, |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 111 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | |
| 113 | //Inherited methods override |
| 114 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 115 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
| 117 | private: |
Georgios Pinitas | 529b5a2 | 2021-07-27 15:55:30 +0100 | [diff] [blame] | 118 | struct Impl; |
| 119 | std::unique_ptr<Impl> _impl; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | }; |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 121 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 122 | #endif /* ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H */ |