1 /* 2 * Copyright (c) 2017-2020 Arm Limited. 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 27 #include "arm_compute/runtime/CL/ICLSimpleFunction.h" 28 29 #include "arm_compute/runtime/CL/CLTensor.h" 30 #include "arm_compute/runtime/CL/functions/CLConvertFullyConnectedWeights.h" 31 #include "arm_compute/runtime/CL/functions/CLFlattenLayer.h" 32 #include "arm_compute/runtime/CL/functions/CLGEMM.h" 33 #include "arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" 34 #include "arm_compute/runtime/IWeightsManager.h" 35 #include "arm_compute/runtime/MemoryGroup.h" 36 37 namespace arm_compute 38 { 39 /** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls the following kernels: 40 * 41 * -# @ref CLTransposeKernel 42 * 43 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. 44 */ 45 class CLFullyConnectedLayerReshapeWeights : public ICLSimpleFunction 46 { 47 public: 48 /** Set the input and output tensors. 49 * 50 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 51 * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input. 52 */ 53 void configure(const ICLTensor *input, ICLTensor *output); 54 /** Set the input and output tensors. 55 * 56 * @param[in] compile_context The compile context to be used. 57 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 58 * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input. 59 */ 60 void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output); 61 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayerReshapeWeights 62 * 63 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 64 * @param[in] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input. 65 * 66 * @return a status 67 */ 68 static Status validate(const ITensorInfo *input, const ITensorInfo *output); 69 }; 70 71 namespace weights_transformations 72 { 73 /** Basic function to manage the reshape weights generated from @ref CLFullyConnectedLayerReshapeWeights */ 74 class CLFullyConnectedLayerReshapeWeightsManaged : public ITransformWeights 75 { 76 public: 77 //Inherited method override run()78 void run() override 79 { 80 _output.allocator()->allocate(); 81 _func.run(); 82 _reshape_run = true; 83 } 84 85 //Inherited method override release()86 void release() override 87 { 88 _output.allocator()->free(); 89 } 90 91 //Inherited method override get_weights()92 ICLTensor *get_weights() override 93 { 94 return &_output; 95 } 96 97 //Inherited method override uid()98 uint32_t uid() override 99 { 100 return _uid; 101 } 102 103 /** Configures the @ref CLFullyConnectedLayerReshapeWeights function 104 * 105 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 106 */ configure(const ICLTensor * input)107 void configure(const ICLTensor *input) 108 { 109 configure(CLKernelLibrary::get().get_compile_context(), input); 110 } 111 /** Configures the @ref CLFullyConnectedLayerReshapeWeights function 112 * 113 * @param[in] compile_context The compile context to be used. 114 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 115 */ configure(const CLCompileContext & compile_context,const ICLTensor * input)116 void configure(const CLCompileContext &compile_context, const ICLTensor *input) 117 { 118 _func.configure(compile_context, input, &_output); 119 } 120 121 private: 122 static constexpr uint32_t _uid = 0x0; 123 CLTensor _output{}; 124 CLFullyConnectedLayerReshapeWeights _func{}; 125 }; 126 } // namespace weights_transformations 127 128 /** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels: 129 * 130 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer) 131 * -# @ref CLFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once) 132 * -# @ref CLGEMMMatrixMultiplyKernel or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric) 133 * 134 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions. 135 */ 136 class CLFullyConnectedLayer : public IFunction 137 { 138 public: 139 /** Constructor */ 140 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); 141 /** Prevent instances of this class from being copied (As this class contains pointers) */ 142 CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete; 143 /** Default move constructor */ 144 CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default; 145 /** Prevent instances of this class from being copied (As this class contains pointers) */ 146 CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete; 147 /** Default move assignment operator */ 148 CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default; 149 /** Set the input and output tensors. 150 * 151 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 152 * @param[in] weights Weights tensor. The weights must be 2 dimensional. 153 * 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. 154 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. 155 * Data type supported: Same as @p input. 156 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input. 157 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between: 158 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer 159 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. 160 * Data type supported: Same as @p input. 161 * @param[in] fc_info (Optional) Fully connected layer additional info 162 */ 163 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, 164 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); 165 /** Set the input and output tensors. 166 * 167 * @param[in] compile_context The compile context to be used. 168 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 169 * @param[in] weights Weights tensor. The weights must be 2 dimensional. 170 * 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. 171 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. 172 * Data type supported: Same as @p input. 173 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input. 174 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between: 175 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer 176 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. 177 * Data type supported: Same as @p input. 178 * @param[in] fc_info (Optional) Fully connected layer additional info 179 */ 180 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, 181 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); 182 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer 183 * 184 * @param[in] input Source tensor info. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32. 185 * @param[in] weights Weights tensor info. The weights must be 2 dimensional. 186 * 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. 187 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension. 188 * Data type supported: Same as @p input. 189 * @param[in] biases Bias tensor info. Can be nullptr. Data type supported:Same as @p input. 190 * @param[out] output Destination tensor info. Its shape should be equal to the output of a matrix multiplication between: 191 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer 192 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer. 193 * Data type supported: Same as @p input. 194 * @param[in] fc_info (Optional) Fully connected layer additional info 195 * 196 * @return a status 197 */ 198 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, 199 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo()); 200 201 //Inherited methods override 202 void run() override; 203 void prepare() override; 204 205 private: 206 void configure_fc_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info); 207 void configure_conv_fc(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info); 208 void configure_mm(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *bias, ICLTensor *output, const FullyConnectedLayerInfo &fc_info); 209 210 MemoryGroup _memory_group; 211 IWeightsManager *_weights_manager; 212 CLConvertFullyConnectedWeights _convert_weights; 213 weights_transformations::CLConvertFullyConnectedWeightsManaged _convert_weights_managed; 214 weights_transformations::CLFullyConnectedLayerReshapeWeightsManaged _reshape_weights_managed_function; 215 CLFlattenLayer _flatten_layer; 216 CLFullyConnectedLayerReshapeWeights _reshape_weights_function; 217 CLGEMM _mm_gemm; 218 CLGEMMLowpMatrixMultiplyCore _mm_gemmlowp; 219 CLTensor _flatten_output; 220 CLTensor _converted_weights_output; 221 CLTensor _reshape_weights_output; 222 bool _are_weights_converted; 223 bool _are_weights_reshaped; 224 bool _is_fc_after_conv; 225 bool _is_quantized; 226 bool _is_prepared; 227 const ICLTensor *_original_weights; 228 }; 229 } // namespace arm_compute 230 #endif /* ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H */ 231