blob: 218877d4211350a4bf52fc339328ab697156fbcf [file] [log] [blame]
Giorgio Arena657bdb32018-04-26 18:52:01 +01001/*
Teresa Charlin562bee52021-04-13 17:44:15 +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_NECONVERTFULLYCONNECTEDWEIGHTS_H
25#define ARM_COMPUTE_NECONVERTFULLYCONNECTEDWEIGHTS_H
Giorgio Arena657bdb32018-04-26 18:52:01 +010026
Giorgio Arena657bdb32018-04-26 18:52:01 +010027#include "arm_compute/runtime/IFunction.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010028#include "arm_compute/runtime/ITransformWeights.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010029#include "arm_compute/runtime/Tensor.h"
Giorgio Arena657bdb32018-04-26 18:52:01 +010030
31namespace arm_compute
32{
Georgios Pinitas33843562019-12-10 13:33:18 +000033// Forward declarations
Giorgio Arena657bdb32018-04-26 18:52:01 +010034class ITensor;
35
Teresa Charlin562bee52021-04-13 17:44:15 +010036/** Basic function to run @ref cpu::kernels::CpuConvertFullyConnectedWeightsKernel. */
Giorgio Arena657bdb32018-04-26 18:52:01 +010037class NEConvertFullyConnectedWeights : public IFunction
38{
39public:
40 /** Default constructor */
41 NEConvertFullyConnectedWeights();
Michalis Spyrouebcebf12020-10-21 00:04:14 +010042 /** Prevent instances of this class from being copied (As this class contains pointers) */
43 NEConvertFullyConnectedWeights(const NEConvertFullyConnectedWeights &) = delete;
44 /** Prevent instances of this class from being copied (As this class contains pointers) */
45 NEConvertFullyConnectedWeights &operator=(const NEConvertFullyConnectedWeights &) = delete;
46 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
47 NEConvertFullyConnectedWeights(NEConvertFullyConnectedWeights &&) = delete;
48 /** Prevent instances of this class from being moved (As this class contains non movable objects) */
49 NEConvertFullyConnectedWeights &operator=(NEConvertFullyConnectedWeights &&) = delete;
50 /** Default destructor */
51 ~NEConvertFullyConnectedWeights();
Giorgio Arena657bdb32018-04-26 18:52:01 +010052 /** Initialize the function.
53 *
Sheri Zhanga47dcc22021-04-22 14:41:12 +010054 * Valid data layouts:
55 * - NHWC
56 * - NCHW
57 *
58 * Valid data type configurations:
59 * |src |dst |
60 * |:--------------|:--------------|
61 * |All |All |
62 *
Georgios Pinitas33843562019-12-10 13:33:18 +000063 * @param[in] input Source weights tensor to convert. Must be 2 dimensional. Data types supported: All.
Giorgio Arena657bdb32018-04-26 18:52:01 +010064 * @param[out] output The converted weights tensor. 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 void configure(const ITensor *input, ITensor *output, const TensorShape &original_input_shape, DataLayout data_layout);
69 /** Static function to check if given info will lead to a valid configuration of @ref NEConvertFullyConnectedWeights
70 *
Georgios Pinitas33843562019-12-10 13:33:18 +000071 * @param[in] input Source weights tensor info to convert. Must be 2 dimensional. Data types supported: All.
Giorgio Arena657bdb32018-04-26 18:52:01 +010072 * @param[in] output The converted weights tensor info. Shape and Data Type: Same as @p input.
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010073 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer).
Giorgio Arena657bdb32018-04-26 18:52:01 +010074 * @param[in] data_layout The data layout the weights have been trained in.
Michalis Spyrou1a569a32019-09-10 17:20:34 +010075 *
76 * @return A Status
Giorgio Arena657bdb32018-04-26 18:52:01 +010077 */
78 static Status validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout);
79
80 // Inherited methods overriden:
81 void run() override;
82
83private:
Teresa Charlin562bee52021-04-13 17:44:15 +010084 struct Impl;
85 std::unique_ptr<Impl> _impl;
Giorgio Arena657bdb32018-04-26 18:52:01 +010086};
Michalis Spyrou1a569a32019-09-10 17:20:34 +010087
88namespace weights_transformations
89{
Teresa Charlin562bee52021-04-13 17:44:15 +010090/** Basic function to manage @ref NEConvertFullyConnectedWeights. */
Michalis Spyrou1a569a32019-09-10 17:20:34 +010091class NEConvertFullyConnectedWeightsManaged : public ITransformWeights
92{
93public:
94 void run() override
95 {
96 _output.allocator()->allocate();
97 _func.run();
98 _reshape_run = true;
99 }
100
101 void release() override
102 {
103 _output.allocator()->free();
104 }
105
106 ITensor *get_weights() override
107 {
108 return &_output;
109 }
110
111 uint32_t uid() override
112 {
113 return _uid;
114 }
115
116 void configure(const ITensor *input, const TensorShape &original_input_shape, DataLayout data_layout)
117 {
118 _func.configure(input, &_output, original_input_shape, data_layout);
119 }
120
121private:
122 static constexpr uint32_t _uid = 0x4;
123 Tensor _output{};
124 NEConvertFullyConnectedWeights _func{};
125};
126} // namespace weights_transformations
127} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000128#endif /* ARM_COMPUTE_NECONVERTFULLYCONNECTEDWEIGHTS_H */