1 /*
2  * Copyright (c) 2018-2021 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_CLCONVERTFULLYCONNECTEDWEIGHTS_H
25 #define ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H
26 
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/runtime/CL/CLTensor.h"
29 #include "arm_compute/runtime/IFunction.h"
30 #include "arm_compute/runtime/ITransformWeights.h"
31 
32 #include <memory>
33 
34 namespace arm_compute
35 {
36 class CLCompileContext;
37 class ICLTensor;
38 class ITensorInfo;
39 
40 /** Basic function to run an @ref opencl::kernels::ClConvertFullyConnectedWeightsKernel. */
41 class CLConvertFullyConnectedWeights : public IFunction
42 {
43 public:
44     /** 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;
56     /** Initialize the function.
57      *
58      * Valid data layouts:
59      * - NHWC
60      * - NCHW
61      *
62      * Valid data type configurations:
63      * |src            |dst            |
64      * |:--------------|:--------------|
65      * |All            |All            |
66      *
67      * @param[in]  input                Source weights tensor to convert. Must be 2 dimensional. Data types supported: All.
68      * @param[out] output               The converted weights tensor. Shape and Data Type: Same as @p input.
69      * @param[in]  original_input_shape Shape of the original input tensor (the one entering fully connected layer).
70      * @param[in]  data_layout          The data layout the weights have been trained in.
71      *
72      * @return A status
73      */
74     void configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape, DataLayout data_layout);
75     /** 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);
86     /** Static function to check if given info will lead to a valid configuration of @ref CLConvertFullyConnectedWeights
87      *
88      * @param[in] input                Source weights tensor info to convert. Must be 2 dimensional. Data types supported: All.
89      * @param[in] output               The converted weights tensor info. Shape and Data Type: Same as @p input.
90      * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
91      * @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);
94 
95     // Inherited methods overridden:
96     void run() override;
97 
98 private:
99     struct Impl;
100     std::unique_ptr<Impl> _impl;
101 };
102 
103 namespace weights_transformations
104 {
105 /** Basic function to manage @ref CLConvertFullyConnectedWeights. */
106 class CLConvertFullyConnectedWeightsManaged : public ITransformWeights
107 {
108 public:
109     //Inherited method override
run()110     void run() override
111     {
112         _output.allocator()->allocate();
113         _func.run();
114         _reshape_run = true;
115     }
116 
117     //Inherited method override
release()118     void release() override
119     {
120         _output.allocator()->free();
121     }
122 
123     //Inherited method override
get_weights()124     ICLTensor *get_weights() override
125     {
126         return &_output;
127     }
128 
129     //Inherited method override
uid()130     uint32_t uid() override
131     {
132         return _uid;
133     }
134     /** Configures the @ref CLConvertFullyConnectedWeights function
135      *
136      * @param[in] input                Source weights tensor info to convert.  Data type supported: All.
137      * @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      */
configure(const ICLTensor * input,const TensorShape & original_input_shape,DataLayout data_layout)140     void configure(const ICLTensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
141     {
142         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      */
configure(const CLCompileContext & compile_context,const ICLTensor * input,const TensorShape & original_input_shape,DataLayout data_layout)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);
154     }
155 
156 private:
157     static constexpr uint32_t      _uid = 0x5;
158     CLTensor                       _output{};
159     CLConvertFullyConnectedWeights _func{};
160 };
161 } // namespace weights_transformations
162 } // namespace arm_compute
163 #endif /* ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H */
164