blob: f71e2a33f9441c7cca43f0889a84a3cde6c44d59 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010027#include "arm_compute/runtime/CL/ICLSimpleFunction.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/CL/kernels/CLGEMMMatrixAccumulateBiasesKernel.h"
30#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
32#include "arm_compute/core/CL/kernels/CLTransposeKernel.h"
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010033#include "arm_compute/runtime/CL/CLMemoryGroup.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/CL/CLTensor.h"
35
36namespace arm_compute
37{
38/** Basic function to reshape the weights of Fully Connected layer with OpenCL. This function calls the following kernels:
39 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010040 * -# @ref CLTransposeKernel
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041 *
42 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
43 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010044class CLFullyConnectedLayerReshapeWeights : public ICLSimpleFunction
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045{
46public:
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 /** Set the input and output tensors.
48 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010049 * @param[in] input Weights tensor. The weights must be 2 dimensional. Data types supported: QS8/QS16/F16/F32.
50 * @param[out] output Destination tensor which stores the transposed input tensor. Data type supported: Same as @p input.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 */
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010052 void configure(const ICLTensor *input, ICLTensor *output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053};
54
55/** Basic function to compute a Fully Connected layer on OpenCL. This function calls the following OpenCL kernels:
56 *
57 * -# @ref CLIm2ColKernel (called when the input comes from a convolutional layer)
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010058 * -# @ref CLFullyConnectedLayerReshapeWeights (if @p are_weights_reshaped is set to false and transpose_weights is set to true ) (called once)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 * -# @ref CLGEMMMatrixMultiplyKernel
60 * -# @ref CLGEMMMatrixAccumulateBiasesKernel (if @p biases is not equal to nullptr)
61 *
62 * @note The fully connected layer accepts "weights" tensors only with 2 dimensions.
63 */
64class CLFullyConnectedLayer : public IFunction
65{
66public:
67 /** Constructor */
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010068 CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 /** Set the input and output tensors.
70 *
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010071 * @param[in] input Source tensor. Data type supported: QS8/QS16/F16/F32.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 * @param[in] weights Weights tensor. The weights must be 2 dimensional. Data type supported: Same as @p input
73 * @param[in] biases Bias tensor. It can be nullptr. Data type supported:Same as @p input.
74 * @param[out] output Destination tensor. Data type supported: Same as @p input.
75 * @param[in] transpose_weights (Optional) Transpose weights if true. Defaults to true.
76 * @param[in] are_weights_reshaped (Optional) Reshape the weights tensor if false. Defaults to false.
77 */
78 void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights = true, bool are_weights_reshaped = false);
79
80 //Inherited methods override
81 void run() override;
82
83private:
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010084 void configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
85 void configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
86
Georgios Pinitasbaf174e2017-09-08 19:47:30 +010087 CLMemoryGroup _memory_group;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 CLIm2ColKernel _im2col_kernel;
89 CLFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 CLGEMMMatrixMultiplyKernel _mm_kernel;
91 CLGEMMMatrixAccumulateBiasesKernel _accumulate_biases_kernel;
92 CLTensor _im2col_output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 CLTensor _reshape_weights_output;
94 bool _are_weights_reshaped;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010095 bool _is_fc_after_conv;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 bool _accumulate_biases;
97};
98}
99#endif /* __ARM_COMPUTE_CLFULLYCONNECTEDLAYER_H__ */