Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Teresa Charlin | 2788609 | 2021-02-25 20:15:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/ICLSimpleFunction.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLTensor.h" |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/functions/CLConvertFullyConnectedWeights.h" |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/CL/functions/CLFlattenLayer.h" |
Gian Marco Iodice | c9c62c2 | 2018-04-06 10:00:10 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/functions/CLGEMM.h" |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 34 | #include "arm_compute/runtime/CL/functions/CLTranspose.h" |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/IWeightsManager.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 36 | #include "arm_compute/runtime/MemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 40 | namespace weights_transformations |
| 41 | { |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 42 | /** Basic function to manage the reshape weights generated from @ref CLTranspose */ |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 43 | class CLFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights |
| 44 | { |
| 45 | public: |
| 46 | //Inherited method override |
| 47 | void run() override |
| 48 | { |
| 49 | _output.allocator()->allocate(); |
| 50 | _func.run(); |
| 51 | _reshape_run = true; |
| 52 | } |
| 53 | |
| 54 | //Inherited method override |
| 55 | void release() override |
| 56 | { |
| 57 | _output.allocator()->free(); |
| 58 | } |
| 59 | |
| 60 | //Inherited method override |
| 61 | ICLTensor *get_weights() override |
| 62 | { |
| 63 | return &_output; |
| 64 | } |
| 65 | |
| 66 | //Inherited method override |
| 67 | uint32_t uid() override |
| 68 | { |
| 69 | return _uid; |
| 70 | } |
| 71 | |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 72 | /** Configures the @ref CLTranspose function |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 73 | * |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 74 | * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 75 | */ |
| 76 | void configure(const ICLTensor *input) |
| 77 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 78 | configure(CLKernelLibrary::get().get_compile_context(), input); |
| 79 | } |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 80 | /** Configures the @ref CLTranspose function |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 81 | * |
| 82 | * @param[in] compile_context The compile context to be used. |
| 83 | * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 84 | */ |
| 85 | void configure(const CLCompileContext &compile_context, const ICLTensor *input) |
| 86 | { |
| 87 | _func.configure(compile_context, input, &_output); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 91 | static constexpr uint32_t _uid = 0x0; |
| 92 | CLTensor _output{}; |
| 93 | CLTranspose _func{}; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 94 | }; |
| 95 | } // namespace weights_transformations |
| 96 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | /** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels: |
| 98 | * |
| 99 | * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer) |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 100 | * -# @ref CLTranspose (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once) |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 101 | * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | * |
| 103 | * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. |
| 104 | */ |
| 105 | class CLFullyConnectedLayer : public IFunction |
| 106 | { |
| 107 | public: |
| 108 | /** Constructor */ |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 109 | CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 110 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 111 | CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete; |
| 112 | /** Default move constructor */ |
| 113 | CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default; |
| 114 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 115 | CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete; |
| 116 | /** Default move assignment operator */ |
| 117 | CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | /** Set the input and output tensors. |
| 119 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 120 | * Valid data layouts: |
| 121 | * - NHWC |
| 122 | * - NCHW |
| 123 | * |
| 124 | * Valid data type configurations: |
| 125 | * |src0 |src1 |src2 |dst | |
| 126 | * |:--------------|:------------------|:------|:--------------| |
| 127 | * |F16 |F16 |F16 |F16 | |
| 128 | * |F32 |F32 |F32 |F32 | |
| 129 | * |QASYMM8 |QASYMM8 |S32 |QASYMM8 | |
| 130 | * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED | |
| 131 | * |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 132 | * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 133 | * @param[in] weights Weights tensor. The weights must be 2 dimensional. |
| 134 | * 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. |
| 135 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 136 | * Data type supported: Same as @p input. |
| 137 | * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input. |
| 138 | * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between: |
| 139 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 140 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 141 | * Data type supported: Same as @p input. |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 142 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 144 | void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 145 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 146 | /** Set the input and output tensors. |
| 147 | * |
| 148 | * @param[in] compile_context The compile context to be used. |
| 149 | * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
| 150 | * @param[in] weights Weights tensor. The weights must be 2 dimensional. |
| 151 | * 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. |
| 152 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 153 | * Data type supported: Same as @p input. |
| 154 | * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input. |
| 155 | * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between: |
| 156 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 157 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 158 | * Data type supported: Same as @p input. |
| 159 | * @param[in] fc_info (Optional) Fully connected layer additional info |
| 160 | */ |
| 161 | void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 162 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 163 | /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer |
| 164 | * |
Sang-Hoon Park | b66aa3b | 2020-01-10 14:44:13 +0000 | [diff] [blame] | 165 | * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. |
Giorgio Arena | a855af1 | 2018-07-16 17:20:38 +0100 | [diff] [blame] | 166 | * @param[in] weights Weights tensor info. The weights must be 2 dimensional. |
| 167 | * 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. |
| 168 | * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. |
| 169 | * Data type supported: Same as @p input. |
| 170 | * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input. |
| 171 | * @param[out] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between: |
| 172 | * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer |
| 173 | * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. |
| 174 | * Data type supported: Same as @p input. |
| 175 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 176 | * |
| 177 | * @return a status |
| 178 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 179 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 180 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | |
| 182 | //Inherited methods override |
| 183 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 184 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | |
| 186 | private: |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 187 | void configure_fc_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info); |
| 188 | void configure_conv_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info); |
| 189 | void configure_mm(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 190 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 191 | MemoryGroup _memory_group; |
| 192 | IWeightsManager *_weights_manager; |
| 193 | CLConvertFullyConnectedWeights _convert_weights; |
| 194 | weights_transformations::CLConvertFullyConnectedWeightsManaged _convert_weights_managed; |
| 195 | weights_transformations::CLFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function; |
| 196 | CLFlattenLayer _flatten_layer; |
Teresa Charlin | 6850889 | 2021-04-07 19:18:08 +0100 | [diff] [blame] | 197 | CLTranspose _reshape_weights_function; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 198 | CLGEMM _mm_gemm; |
| 199 | CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 200 | CLTensor _flatten_output; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 201 | CLTensor _converted_weights_output; |
| 202 | CLTensor _reshape_weights_output; |
| 203 | bool _are_weights_converted; |
| 204 | bool _are_weights_reshaped; |
| 205 | bool _is_fc_after_conv; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 206 | bool _is_quantized; |
| 207 | bool _is_prepared; |
| 208 | const ICLTensor *_original_weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | }; |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 210 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 211 | #endif /* ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H */ |