blob: 2947b4890cce543f309e7e2a99e6297eaa49ab74 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Teresa Charlin27886092021-02-25 20:15:01 +00002 * Copyright (c) 2017-2021 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_CLFULLYCONNECTEDLAYER_H
25#define ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Georgios Pinitas529b5a22021-07-27 15:55:30 +010027#include "arm_compute/runtime/IFunction.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/runtime/CL/CLTensor.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010030#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010031#include "arm_compute/runtime/MemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
33namespace arm_compute
34{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
36 *
Manuel Bottinid844c082021-07-14 12:58:54 +010037 * -# @ref opencl::kernels::ClIm2ColKernel (called when the input comes from a convolutional layer)
Teresa Charlin68508892021-04-07 19:18:08 +010038 * -# @ref CLTranspose (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
Gian Marco Iodicec9cecc02021-10-15 10:23:24 +010039 * -# @ref opencl::ClGemm or @ref CLGEMMLowpMatrixMultiplyCore (if quantized asymmetric)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040 *
41 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
42 */
43class CLFullyConnectedLayer : public IFunction
44{
45public:
46 /** Constructor */
Michalis Spyrou1a569a32019-09-10 17:20:34 +010047 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas529b5a22021-07-27 15:55:30 +010048 /** Default destructor */
49 ~CLFullyConnectedLayer();
Georgios Pinitas1562be32018-03-08 19:09:19 +000050 /** Prevent instances of this class from being copied (As this class contains pointers) */
51 CLFullyConnectedLayer(const CLFullyConnectedLayer &) = delete;
52 /** Default move constructor */
53 CLFullyConnectedLayer(CLFullyConnectedLayer &&) = default;
54 /** Prevent instances of this class from being copied (As this class contains pointers) */
55 CLFullyConnectedLayer &operator=(const CLFullyConnectedLayer &) = delete;
56 /** Default move assignment operator */
57 CLFullyConnectedLayer &operator=(CLFullyConnectedLayer &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 /** Set the input and output tensors.
59 *
Teresa Charlin62687422021-04-28 10:58:49 +010060 * Valid data layouts:
61 * - NHWC
62 * - NCHW
63 *
64 * Valid data type configurations:
65 * |src0 |src1 |src2 |dst |
66 * |:--------------|:------------------|:------|:--------------|
67 * |F16 |F16 |F16 |F16 |
68 * |F32 |F32 |F32 |F32 |
69 * |QASYMM8 |QASYMM8 |S32 |QASYMM8 |
70 * |QASYMM8_SIGNED |QASYMM8_SIGNED |S32 |QASYMM8_SIGNED |
71 *
Manuel Bottini2b84be52020-04-08 10:15:51 +010072 * @param[in] compile_context The compile context to be used.
73 * @param[in] input Source tensor. Data type supported: QASYMM8/QASYMM8_SIGNED/F16/F32.
74 * @param[in] weights Weights tensor. The weights must be 2 dimensional.
75 * 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.
76 * If it is called after another FullyConnected Layer, the (transposed) weights will have as many rows as the input's first dimension.
77 * Data type supported: Same as @p input.
78 * @param[in] biases Bias tensor. Can be nullptr. Data type supported:Same as @p input.
79 * @param[out] output Destination tensor. Its shape should be equal to the output of a matrix multiplication between:
80 * - The output of im2col on the input and the (transposed) 2D weights, if the function is called after a Convolution Layer
81 * - The input tensor and the (transposed) 2D weights, if the function is called after another FullyConnected Layer.
82 * Data type supported: Same as @p input.
83 * @param[in] fc_info (Optional) Fully connected layer additional info
84 */
85 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
86 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Georgios Pinitas529b5a22021-07-27 15:55:30 +010087 /** Set the input and output tensors.
88 *
89 * Similar to @ref CLFullyConnectedLayer
90 */
91 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
92 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Georgios Pinitas358ca202017-12-07 16:47:52 +000093 /** Static function to check if given info will lead to a valid configuration of @ref CLFullyConnectedLayer
94 *
Georgios Pinitas529b5a22021-07-27 15:55:30 +010095 * Similar to @ref CLFullyConnectedLayer
Georgios Pinitas358ca202017-12-07 16:47:52 +000096 *
97 * @return a status
98 */
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010099 static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
100 FullyConnectedLayerInfo fc_info = FullyConnectedLayerInfo());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
102 //Inherited methods override
103 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100104 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
106private:
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100107 struct Impl;
108 std::unique_ptr<Impl> _impl;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109};
Georgios Pinitas26014cf2019-09-09 19:00:57 +0100110} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000111#endif /* ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H */