Compute Library
 21.05
CLConvertFullyConnectedWeights.h
Go to the documentation of this file.
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 
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. */
42 {
43 public:
44  /** Constructor */
46  /** Destructor */
48  /** Prevent instances of this class from being copied (As this class contains pointers) */
50  /** Default move constructor */
52  /** Prevent instances of this class from being copied (As this class contains pointers) */
54  /** Default move assignment operator */
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. */
107 {
108 public:
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  *
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  */
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  */
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{};
160 };
161 } // namespace weights_transformations
162 } // namespace arm_compute
163 #endif /* ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H */
Shape of a tensor.
Definition: TensorShape.h:39
uint32_t uid() override
Function that returns a unique id of the reshape function.
Base class for all functions.
Definition: IFunction.h:30
CLConvertFullyConnectedWeights & operator=(const CLConvertFullyConnectedWeights &)=delete
Prevent instances of this class from being copied (As this class contains pointers)
const DataLayout data_layout
Definition: Im2Col.cpp:151
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor's metadata.
Definition: ITensorInfo.h:40
CLTensorAllocator * allocator()
Return a pointer to the tensor's allocator.
Definition: CLTensor.cpp:61
Status class.
Definition: Error.h:52
void configure(const CLCompileContext &compile_context, const ICLTensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
Configures the CLConvertFullyConnectedWeights function.
Copyright (c) 2017-2021 Arm Limited.
Basic function to run an opencl::kernels::ClConvertFullyConnectedWeightsKernel.
void run() override
Run the kernels contained in the function.
ICLTensor * get_weights() override
Get a pointer to the transformed weights.
CLCompileContext class.
static Status validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout)
Static function to check if given info will lead to a valid configuration of CLConvertFullyConnectedW...
void allocate() override
Allocate size specified by TensorInfo of OpenCL memory.
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context.
void configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape, DataLayout data_layout)
Initialize the function.
void free() override
Free allocated OpenCL memory.
Weights tensor transform interface In order to identify the different reshape functions,...
void configure(const ICLTensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
Configures the CLConvertFullyConnectedWeights function.
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41