blob: 6c7d9e52e8e71d820230d41ca08a511c971b9f07 [file] [log] [blame]
Giorgio Arena657bdb32018-04-26 18:52:01 +01001/*
Teresa Charlin91b7f742021-04-12 13:57:00 +01002 * Copyright (c) 2018-2021 Arm Limited.
Giorgio Arena657bdb32018-04-26 18:52:01 +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_CLCONVERTFULLYCONNECTEDWEIGHTS_H
25#define ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H
Giorgio Arena657bdb32018-04-26 18:52:01 +010026
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010027#include "arm_compute/core/CL/CLKernelLibrary.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010028#include "arm_compute/runtime/CL/CLTensor.h"
Teresa Charlin91b7f742021-04-12 13:57:00 +010029#include "arm_compute/runtime/IFunction.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010030#include "arm_compute/runtime/ITransformWeights.h"
Giorgio Arena657bdb32018-04-26 18:52:01 +010031
Teresa Charlin91b7f742021-04-12 13:57:00 +010032#include <memory>
33
Giorgio Arena657bdb32018-04-26 18:52:01 +010034namespace arm_compute
35{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036class CLCompileContext;
Giorgio Arena657bdb32018-04-26 18:52:01 +010037class ICLTensor;
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010038class ITensorInfo;
Giorgio Arena657bdb32018-04-26 18:52:01 +010039
Teresa Charlin91b7f742021-04-12 13:57:00 +010040/** Basic function to run an @ref opencl::kernels::ClConvertFullyConnectedWeightsKernel. */
41class CLConvertFullyConnectedWeights : public IFunction
Giorgio Arena657bdb32018-04-26 18:52:01 +010042{
43public:
Teresa Charlin91b7f742021-04-12 13:57:00 +010044 /** Constructor */
45 CLConvertFullyConnectedWeights();
46 /** Destructor */
47 ~CLConvertFullyConnectedWeights();
48 /** Prevent instances of this class from being copied (As this class contains pointers) */
49 CLConvertFullyConnectedWeights(const CLConvertFullyConnectedWeights &) = delete;
50 /** Default move constructor */
51 CLConvertFullyConnectedWeights(CLConvertFullyConnectedWeights &&) = default;
52 /** Prevent instances of this class from being copied (As this class contains pointers) */
53 CLConvertFullyConnectedWeights &operator=(const CLConvertFullyConnectedWeights &) = delete;
54 /** Default move assignment operator */
55 CLConvertFullyConnectedWeights &operator=(CLConvertFullyConnectedWeights &&) = default;
Giorgio Arena657bdb32018-04-26 18:52:01 +010056 /** Initialize the function.
57 *
Sheri Zhanga47dcc22021-04-22 14:41:12 +010058 * Valid data layouts:
59 * - NHWC
60 * - NCHW
61 *
62 * Valid data type configurations:
63 * |src |dst |
64 * |:--------------|:--------------|
65 * |All |All |
66 *
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000067 * @param[in] input Source weights tensor to convert. Must be 2 dimensional. Data types supported: All.
Giorgio Arena657bdb32018-04-26 18:52:01 +010068 * @param[out] output The converted weights tensor. Shape and Data Type: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010069 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
Giorgio Arena657bdb32018-04-26 18:52:01 +010070 * @param[in] data_layout The data layout the weights have been trained in.
Michalis Spyrou1a569a32019-09-10 17:20:34 +010071 *
72 * @return A status
Giorgio Arena657bdb32018-04-26 18:52:01 +010073 */
74 void configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape, DataLayout data_layout);
Manuel Bottini2b84be52020-04-08 10:15:51 +010075 /** Initialize the function.
76 *
77 * @param[in] compile_context The compile context to be used.
78 * @param[in] input Source weights tensor to convert. Must be 2 dimensional. Data types supported: All.
79 * @param[out] output The converted weights tensor. Shape and Data Type: Same as @p input.
80 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
81 * @param[in] data_layout The data layout the weights have been trained in.
82 *
83 * @return A status
84 */
85 void configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape, DataLayout data_layout);
Giorgio Arena657bdb32018-04-26 18:52:01 +010086 /** Static function to check if given info will lead to a valid configuration of @ref CLConvertFullyConnectedWeights
87 *
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000088 * @param[in] input Source weights tensor info to convert. Must be 2 dimensional. Data types supported: All.
Giorgio Arena657bdb32018-04-26 18:52:01 +010089 * @param[in] output The converted weights tensor info. Shape and Data Type: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010090 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
Giorgio Arena657bdb32018-04-26 18:52:01 +010091 * @param[in] data_layout The data layout the weights have been trained in.
92 */
93 static Status validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout);
Teresa Charlin91b7f742021-04-12 13:57:00 +010094
95 // Inherited methods overridden:
96 void run() override;
97
98private:
99 struct Impl;
100 std::unique_ptr<Impl> _impl;
Giorgio Arena657bdb32018-04-26 18:52:01 +0100101};
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100102
103namespace weights_transformations
104{
Teresa Charlin91b7f742021-04-12 13:57:00 +0100105/** Basic function to manage @ref CLConvertFullyConnectedWeights. */
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100106class CLConvertFullyConnectedWeightsManaged : public ITransformWeights
107{
108public:
109 //Inherited method override
110 void run() override
111 {
112 _output.allocator()->allocate();
113 _func.run();
114 _reshape_run = true;
115 }
116
117 //Inherited method override
118 void release() override
119 {
120 _output.allocator()->free();
121 }
122
123 //Inherited method override
124 ICLTensor *get_weights() override
125 {
126 return &_output;
127 }
128
129 //Inherited method override
130 uint32_t uid() override
131 {
132 return _uid;
133 }
134 /** Configures the @ref CLConvertFullyConnectedWeights function
135 *
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000136 * @param[in] input Source weights tensor info to convert. Data type supported: All.
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100137 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
138 * @param[in] data_layout The data layout the weights have been trained in.
139 */
140 void configure(const ICLTensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
141 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100142 configure(CLKernelLibrary::get().get_compile_context(), input, original_input_shape, data_layout);
143 }
144 /** Configures the @ref CLConvertFullyConnectedWeights function
145 *
146 * @param[in] compile_context The compile context to be used.
147 * @param[in] input Source weights tensor info to convert. Data type supported: All.
148 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
149 * @param[in] data_layout The data layout the weights have been trained in.
150 */
151 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
152 {
153 _func.configure(compile_context, input, &_output, original_input_shape, data_layout);
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100154 }
155
156private:
157 static constexpr uint32_t _uid = 0x5;
158 CLTensor _output{};
159 CLConvertFullyConnectedWeights _func{};
160};
161} // namespace weights_transformations
Giorgio Arena657bdb32018-04-26 18:52:01 +0100162} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000163#endif /* ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H */