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_CLLOCALLYCONNECTEDLAYER_H 25 #define ARM_COMPUTE_CLLOCALLYCONNECTEDLAYER_H 26 27 #include "arm_compute/runtime/IFunction.h" 28 29 #include "arm_compute/core/Types.h" 30 #include "arm_compute/runtime/CL/CLTensor.h" 31 #include "arm_compute/runtime/IMemoryManager.h" 32 #include "arm_compute/runtime/MemoryGroup.h" 33 34 #include <memory> 35 36 namespace arm_compute 37 { 38 class CLCompileContext; 39 class CLCol2ImKernel; 40 class CLIm2ColKernel; 41 class CLWeightsReshapeKernel; 42 class CLLocallyConnectedMatrixMultiplyKernel; 43 class ICLTensor; 44 class ITensorInfo; 45 46 /** Basic function to compute the locally connected layer. This function calls the following OpenCL kernels: 47 * 48 * -# @ref CLWeightsReshapeKernel (executed only once for each configuration) 49 * -# @ref CLIm2ColKernel 50 * -# @ref CLLocallyConnectedMatrixMultiplyKernel 51 * -# @ref CLCol2ImKernel 52 */ 53 class CLLocallyConnectedLayer : public IFunction 54 { 55 public: 56 /** Default constructor */ 57 CLLocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr); 58 /** Prevent instances of this class from being copied (As this class contains pointers) */ 59 CLLocallyConnectedLayer(const CLLocallyConnectedLayer &) = delete; 60 /** Default move constructor */ 61 CLLocallyConnectedLayer(CLLocallyConnectedLayer &&) = default; 62 /** Prevent instances of this class from being copied (As this class contains pointers) */ 63 CLLocallyConnectedLayer &operator=(const CLLocallyConnectedLayer &) = delete; 64 /** Default move assignment operator */ 65 CLLocallyConnectedLayer &operator=(CLLocallyConnectedLayer &&) = default; 66 /** Set the input and output tensors. 67 * 68 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 69 * while every optional dimension from 4 and above represent a batch of inputs. 70 * Data types supported: F32. 71 * @param[in] weights Weights tensor. Weights are 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches]. Data type supported:Same as @p input. 72 * @param[in] biases Biases tensor. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as @p input. 73 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 74 * Data types supported: Same as @p input. 75 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 76 */ 77 ARM_COMPUTE_DEPRECATED_REL(20.11) 78 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info); 79 /** Set the input and output tensors. 80 * 81 * @param[in] compile_context The compile context to be used. 82 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM], 83 * while every optional dimension from 4 and above represent a batch of inputs. 84 * Data types supported: F32. 85 * @param[in] weights Weights tensor. Weights are 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches]. Data type supported:Same as @p input. 86 * @param[in] biases Biases tensor. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as @p input. 87 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 88 * Data types supported: Same as @p input. 89 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 90 */ 91 ARM_COMPUTE_DEPRECATED_REL(20.11) 92 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info); 93 /** Static function to check if given info will lead to a valid configuration of @ref CLLocallyConnectedLayer 94 * 95 * @param[in] input Input tensor info. 3 lower dimensions represent a single input [width, height, IFM], 96 * while every optional dimension from 4 and above represent a batch of inputs. 97 * Data types supported: F32. 98 * @param[in] weights Weights tensor info. Weights are 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, num_patches]. Data type supported:Same as @p input. 99 * @param[in] biases Biases tensor info. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as @p input. 100 * @param[in] output Output tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs. 101 * Data types supported: Same as @p input. 102 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo. 103 * 104 * @return a status 105 */ 106 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info); 107 108 // Inherited methods overridden: 109 void run() override; 110 void prepare() override; 111 112 private: 113 MemoryGroup _memory_group; 114 std::unique_ptr<CLIm2ColKernel> _input_im2col_kernel; 115 std::unique_ptr<CLWeightsReshapeKernel> _weights_reshape_kernel; 116 std::unique_ptr<CLLocallyConnectedMatrixMultiplyKernel> _mm_kernel; 117 std::unique_ptr<CLCol2ImKernel> _output_col2im_kernel; 118 CLTensor _input_im2col_reshaped; 119 CLTensor _weights_reshaped; 120 CLTensor _gemm_output; 121 bool _is_prepared; 122 const ICLTensor *_original_weights; 123 }; 124 } 125 #endif /* ARM_COMPUTE_CLLOCALLYCONNECTEDLAYER_H */ 126