Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__ |
| 25 | #define __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__ |
| 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/core/CL/kernels/CLGEMMMatrixAccumulateBiasesKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/kernels/CLIm2ColKernel.h" |
| 31 | #include "arm_compute/core/CL/kernels/CLTransposeKernel.h" |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/CLMemoryGroup.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/CL/CLTensor.h" |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 34 | #include "arm_compute/runtime/CL/functions/CLConvertFullyConnectedWeights.h" |
Gian Marco Iodice | c9c62c2 | 2018-04-06 10:00:10 +0100 | [diff] [blame] | 35 | #include "arm_compute/runtime/CL/functions/CLGEMM.h" |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" |
| 37 | #include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | /** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls the following kernels: |
| 42 | * |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 43 | * -# @ref CLTransposeKernel |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | * |
| 45 | * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. |
| 46 | */ |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 47 | class CLFullyConnectedLayerReshapeWeights : public ICLSimpleFunction |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
| 49 | public: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | /** Set the input and output tensors. |
| 51 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 52 | * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/F16/F32. |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 53 | * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | */ |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 55 | void configure(const ICLTensor *input, ICLTensor *output); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 56 | /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayerReshapeWeights |
| 57 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 58 | * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/F16/F32. |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 59 | * @param[in] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input. |
| 60 | * |
| 61 | * @return a status |
| 62 | */ |
| 63 | static Status validate(const ITensorInfo *input, const ITensorInfo *output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels: |
| 67 | * |
| 68 | * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer) |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 69 | * -# @ref CLFullyConnectedLayerReshapeWeights (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] | 70 | * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric) |
Isabella Gottardi | a1667fb | 2018-04-11 11:53:49 +0100 | [diff] [blame] | 71 | * -# @ref CLGEMMMatrixAccumulateBiasesKernel or @ref CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint (if quantized asymmetric) (if @p biases is not equal to nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | * |
| 73 | * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. |
| 74 | */ |
| 75 | class CLFullyConnectedLayer : public IFunction |
| 76 | { |
| 77 | public: |
| 78 | /** Constructor */ |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 79 | CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 80 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 81 | CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete; |
| 82 | /** Default move constructor */ |
| 83 | CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default; |
| 84 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 85 | CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete; |
| 86 | /** Default move assignment operator */ |
| 87 | CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | /** Set the input and output tensors. |
| 89 | * |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 90 | * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32. |
| 91 | * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input |
| 92 | * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input. |
| 93 | * @param[out] output Destination tensor. Data type supported: Same as @p input. |
| 94 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 96 | void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, |
| 97 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 98 | /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer |
| 99 | * |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 100 | * @param[in] input Source tensor. Data type supported: QASYMM8/F16/F32. |
| 101 | * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input |
| 102 | * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input. |
| 103 | * @param[in] output Destination tensor. Data type supported: Same as @p input. |
| 104 | * @param[in] fc_info (Optional) Fully connected layer additional info |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 105 | * |
| 106 | * @return a status |
| 107 | */ |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 108 | static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, |
| 109 | FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | |
| 111 | //Inherited methods override |
| 112 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 113 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | |
| 115 | private: |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 116 | void configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output); |
| 117 | void configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output); |
Gian Marco Iodice | c9c62c2 | 2018-04-06 10:00:10 +0100 | [diff] [blame] | 118 | void configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 119 | |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 120 | CLMemoryGroup _memory_group; |
| 121 | CLIm2ColKernel _im2col_kernel; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 122 | CLConvertFullyConnectedWeights _convert_weights; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 123 | CLFullyConnectedLayerReshapeWeights _reshape_weights_kernel; |
Gian Marco Iodice | c9c62c2 | 2018-04-06 10:00:10 +0100 | [diff] [blame] | 124 | CLGEMM _mm_gemm; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 125 | CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp; |
| 126 | CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint _gemmlowp_output_stage; |
| 127 | CLGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel; |
| 128 | CLTensor _im2col_output; |
| 129 | CLTensor _gemmlowp_output; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 130 | CLTensor _converted_weights_output; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 131 | CLTensor _reshape_weights_output; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 132 | bool _are_weights_converted; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 133 | bool _are_weights_reshaped; |
| 134 | bool _is_fc_after_conv; |
| 135 | bool _accumulate_biases; |
| 136 | bool _is_quantized; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame^] | 137 | bool _is_prepared; |
Georgios Pinitas | 1562be3 | 2018-03-08 19:09:19 +0000 | [diff] [blame] | 138 | const ICLTensor *_original_weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | }; |
| 140 | } |
| 141 | #endif /* __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__ */ |