blob: 1186a449d58b47170c07ea125e9e2bc86c377f16 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas26014cf2019-09-09 19:00:57 +01002 * Copyright (c) 2017-2019 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 */
75 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info);
Alex Gilday27c08ab2018-02-22 11:36:16 +000076 /** Static function to check if given info will lead to a valid configuration of @ref CLLocallyConnectedLayer
77 *
78 * @param[in] input Input tensor info. 3 lower dimensions represent a single input [width, height, IFM],
79 * while every optional dimension from 4 and above represent a batch of inputs.
80 * Data types supported: F32.
81 * @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.
82 * @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.
83 * @param[in] output Output tensor info. 3 lower dimensions represent a single output [width, height, OFM], while the rest represent batch of outputs.
84 * Data types supported: Same as @p input.
85 * @param[in] conv_info Contains padding and stride information described in @ref PadStrideInfo.
86 *
87 * @return a status
88 */
89 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 +010090
91 // Inherited methods overridden:
92 void run() override;
Georgios Pinitas72219332018-06-05 14:56:06 +010093 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
95private:
Georgios Pinitas26014cf2019-09-09 19:00:57 +010096 MemoryGroup _memory_group;
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010097 CLIm2ColKernel _input_im2col_kernel;
98 CLWeightsReshapeKernel _weights_reshape_kernel;
99 CLLocallyConnectedMatrixMultiplyKernel _mm_kernel;
100 CLCol2ImKernel _output_col2im_kernel;
101 CLTensor _input_im2col_reshaped;
102 CLTensor _weights_reshaped;
103 CLTensor _gemm_output;
Georgios Pinitas72219332018-06-05 14:56:06 +0100104 bool _is_prepared;
Georgios Pinitas1562be32018-03-08 19:09:19 +0000105 const ICLTensor *_original_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106};
107}
Michalis Spyrouf4643372019-11-29 16:17:13 +0000108#endif /* ARM_COMPUTE_CLLOCALLYCONNECTEDLAYER_H */