blob: 9298be2e535ad33b61cd8a27d06ea3b7dcc0a063 [file] [log] [blame]
Giorgio Arena657bdb32018-04-26 18:52:01 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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
27#include "arm_compute/core/CL/kernels/CLConvertFullyConnectedWeightsKernel.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010028#include "arm_compute/runtime/CL/CLTensor.h"
Giorgio Arena657bdb32018-04-26 18:52:01 +010029#include "arm_compute/runtime/CL/ICLSimpleFunction.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010030#include "arm_compute/runtime/ITransformWeights.h"
Giorgio Arena657bdb32018-04-26 18:52:01 +010031
32namespace arm_compute
33{
34class ICLTensor;
35
36/** Basic function to run @ref CLConvertFullyConnectedWeightsKernel. */
37class CLConvertFullyConnectedWeights : public ICLSimpleFunction
38{
39public:
40 /** Initialize the function.
41 *
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000042 * @param[in] input Source weights tensor to convert. Must be 2 dimensional. Data types supported: All.
Giorgio Arena657bdb32018-04-26 18:52:01 +010043 * @param[out] output The converted weights tensor. Shape and Data Type: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010044 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
Giorgio Arena657bdb32018-04-26 18:52:01 +010045 * @param[in] data_layout The data layout the weights have been trained in.
Michalis Spyrou1a569a32019-09-10 17:20:34 +010046 *
47 * @return A status
Giorgio Arena657bdb32018-04-26 18:52:01 +010048 */
49 void configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape, DataLayout data_layout);
Manuel Bottini2b84be52020-04-08 10:15:51 +010050 /** Initialize the function.
51 *
52 * @param[in] compile_context The compile context to be used.
53 * @param[in] input Source weights tensor to convert. Must be 2 dimensional. Data types supported: All.
54 * @param[out] output The converted weights tensor. Shape and Data Type: Same as @p input.
55 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
56 * @param[in] data_layout The data layout the weights have been trained in.
57 *
58 * @return A status
59 */
60 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 +010061 /** Static function to check if given info will lead to a valid configuration of @ref CLConvertFullyConnectedWeights
62 *
Michele Di Giorgiocbbed282019-12-20 13:26:08 +000063 * @param[in] input Source weights tensor info to convert. Must be 2 dimensional. Data types supported: All.
Giorgio Arena657bdb32018-04-26 18:52:01 +010064 * @param[in] output The converted weights tensor info. Shape and Data Type: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010065 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
Giorgio Arena657bdb32018-04-26 18:52:01 +010066 * @param[in] data_layout The data layout the weights have been trained in.
67 */
68 static Status validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout);
69};
Michalis Spyroub27e13a2019-09-27 11:04:27 +010070
71namespace weights_transformations
72{
73/** Basic function to run @ref CLConvertFullyConnectedWeightsKernel. */
74class CLConvertFullyConnectedWeightsManaged : public ITransformWeights
75{
76public:
77 //Inherited method override
78 void run() override
79 {
80 _output.allocator()->allocate();
81 _func.run();
82 _reshape_run = true;
83 }
84
85 //Inherited method override
86 void release() override
87 {
88 _output.allocator()->free();
89 }
90
91 //Inherited method override
92 ICLTensor *get_weights() override
93 {
94 return &_output;
95 }
96
97 //Inherited method override
98 uint32_t uid() override
99 {
100 return _uid;
101 }
102 /** Configures the @ref CLConvertFullyConnectedWeights function
103 *
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000104 * @param[in] input Source weights tensor info to convert. Data type supported: All.
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100105 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
106 * @param[in] data_layout The data layout the weights have been trained in.
107 */
108 void configure(const ICLTensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
109 {
Manuel Bottini2b84be52020-04-08 10:15:51 +0100110 configure(CLKernelLibrary::get().get_compile_context(), input, original_input_shape, data_layout);
111 }
112 /** Configures the @ref CLConvertFullyConnectedWeights function
113 *
114 * @param[in] compile_context The compile context to be used.
115 * @param[in] input Source weights tensor info to convert. Data type supported: All.
116 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
117 * @param[in] data_layout The data layout the weights have been trained in.
118 */
119 void configure(const CLCompileContext &compile_context, const ICLTensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
120 {
121 _func.configure(compile_context, input, &_output, original_input_shape, data_layout);
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100122 }
123
124private:
125 static constexpr uint32_t _uid = 0x5;
126 CLTensor _output{};
127 CLConvertFullyConnectedWeights _func{};
128};
129} // namespace weights_transformations
Giorgio Arena657bdb32018-04-26 18:52:01 +0100130} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000131#endif /* ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H */