blob: ba85c6140c8c4a22a10db4ad5841744c23aab421 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLLOCALLYCONNECTEDLAYER_H
25#define ARM_COMPUTE_CLLOCALLYCONNECTEDLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/runtime/IFunction.h"
28
29#include "arm_compute/core/CL/kernels/CLCol2ImKernel.h"
30#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
31#include "arm_compute/core/CL/kernels/CLLocallyConnectedMatrixMultiplyKernel.h"
32#include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/runtime/CL/CLTensor.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010035#include "arm_compute/runtime/IMemoryManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010036#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010037
38#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40namespace arm_compute
41{
42class ICLTensor;
43
44/** Basic function to compute the locally connected layer. This function calls the following OpenCL kernels:
45 *
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010046 * -# @ref CLWeightsReshapeKernel (executed only once for each configuration)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 * -# @ref CLIm2ColKernel
48 * -# @ref CLLocallyConnectedMatrixMultiplyKernel
49 * -# @ref CLCol2ImKernel
50 */
51class CLLocallyConnectedLayer : public IFunction
52{
53public:
54 /** Default constructor */
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010055 CLLocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Georgios Pinitas1562be32018-03-08 19:09:19 +000056 /** Prevent instances of this class from being copied (As this class contains pointers) */
57 CLLocallyConnectedLayer(const CLLocallyConnectedLayer &) = delete;
58 /** Default move constructor */
59 CLLocallyConnectedLayer(CLLocallyConnectedLayer &&) = default;
60 /** Prevent instances of this class from being copied (As this class contains pointers) */
61 CLLocallyConnectedLayer &operator=(const CLLocallyConnectedLayer &) = delete;
62 /** Default move assignment operator */
63 CLLocallyConnectedLayer &operator=(CLLocallyConnectedLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 /** Set the input and output tensors.
65 *
66 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
67 * while every optional dimension from 4 and above represent a batch of inputs.
68 * Data types supported: F32.
69 * @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.
70 * @param[in] biases Biases tensor. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as @p input.
71 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
72 * Data types supported: Same as @p input.
73 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
74 */
Georgios Pinitas2d221392020-09-03 15:16:37 +010075 ARM_COMPUTE_DEPRECATED_REL(20.11)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info);
Manuel Bottini2b84be52020-04-08 10:15:51 +010077 /** Set the input and output tensors.
78 *
79 * @param[in] compile_context The compile context to be used.
80 * @param[in] input Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
81 * while every optional dimension from 4 and above represent a batch of inputs.
82 * Data types supported: F32.
83 * @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.
84 * @param[in] biases Biases tensor. Shared biases supported. Biases are 2D tensor with dimensions [OFM, num_patches]. Data type supported:Same as @p input.
85 * @param[out] output Destination tensor. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
86 * Data types supported: Same as @p input.
87 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
88 */
Georgios Pinitas2d221392020-09-03 15:16:37 +010089 ARM_COMPUTE_DEPRECATED_REL(20.11)
Manuel Bottini2b84be52020-04-08 10:15:51 +010090 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info);
Alex Gilday27c08ab2018-02-22 11:36:16 +000091 /** Static function to check if given info will lead to a valid configuration of @ref CLLocallyConnectedLayer
92 *
93 * @param[in] input Input tensor info. 3 lower dimensions represent a single input [width, height, IFM],
94 * while every optional dimension from 4 and above represent a batch of inputs.
95 * Data types supported: F32.
96 * @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.
97 * @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.
98 * @param[in] output Output tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
99 * Data types supported: Same as @p input.
100 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
101 *
102 * @return a status
103 */
104 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
106 // Inherited methods overridden:
107 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +0100108 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
110private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100111 MemoryGroup _memory_group;
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +0100112 CLIm2ColKernel _input_im2col_kernel;
113 CLWeightsReshapeKernel _weights_reshape_kernel;
114 CLLocallyConnectedMatrixMultiplyKernel _mm_kernel;
115 CLCol2ImKernel _output_col2im_kernel;
116 CLTensor _input_im2col_reshaped;
117 CLTensor _weights_reshaped;
118 CLTensor _gemm_output;
Georgios Pinitas72219332018-06-05 14:56:06 +0100119 bool _is_prepared;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000120 const ICLTensor *_original_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121};
122}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000123#endif /* ARM_COMPUTE_CLLOCALLYCONNECTEDLAYER_H */