Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 1 | /* |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H |
| 25 | #define ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 26 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLTensor.h" |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/IFunction.h" |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/ITransformWeights.h" |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 31 | |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 32 | #include <memory> |
| 33 | |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 36 | class CLCompileContext; |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 37 | class ICLTensor; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 38 | class ITensorInfo; |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 39 | |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 40 | /** Basic function to run an @ref opencl::kernels::ClConvertFullyConnectedWeightsKernel. */ |
| 41 | class CLConvertFullyConnectedWeights : public IFunction |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 42 | { |
| 43 | public: |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 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; |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 56 | /** Initialize the function. |
| 57 | * |
Sheri Zhang | a47dcc2 | 2021-04-22 14:41:12 +0100 | [diff] [blame] | 58 | * Valid data layouts: |
| 59 | * - NHWC |
| 60 | * - NCHW |
| 61 | * |
| 62 | * Valid data type configurations: |
| 63 | * |src |dst | |
| 64 | * |:--------------|:--------------| |
| 65 | * |All |All | |
| 66 | * |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 67 | * @param[in] input Source weights tensor to convert. Must be 2 dimensional. Data types supported: All. |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 68 | * @param[out] output The converted weights tensor. Shape and Data Type: Same as @p input. |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 69 | * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer). |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 70 | * @param[in] data_layout The data layout the weights have been trained in. |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 71 | * |
| 72 | * @return A status |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 73 | */ |
| 74 | void configure(const ICLTensor *input, ICLTensor *output, const TensorShape &original_input_shape, DataLayout data_layout); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 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); |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 86 | /** Static function to check if given info will lead to a valid configuration of @ref CLConvertFullyConnectedWeights |
| 87 | * |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 88 | * @param[in] input Source weights tensor info to convert. Must be 2 dimensional. Data types supported: All. |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 89 | * @param[in] output The converted weights tensor info. Shape and Data Type: Same as @p input. |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 90 | * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer). |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 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); |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 94 | |
| 95 | // Inherited methods overridden: |
| 96 | void run() override; |
| 97 | |
| 98 | private: |
| 99 | struct Impl; |
| 100 | std::unique_ptr<Impl> _impl; |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 101 | }; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 102 | |
| 103 | namespace weights_transformations |
| 104 | { |
Teresa Charlin | 91b7f74 | 2021-04-12 13:57:00 +0100 | [diff] [blame] | 105 | /** Basic function to manage @ref CLConvertFullyConnectedWeights. */ |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 106 | class CLConvertFullyConnectedWeightsManaged : public ITransformWeights |
| 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 | * |
Michele Di Giorgio | cbbed28 | 2019-12-20 13:26:08 +0000 | [diff] [blame] | 136 | * @param[in] input Source weights tensor info to convert. Data type supported: All. |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 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 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 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); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
| 157 | static constexpr uint32_t _uid = 0x5; |
| 158 | CLTensor _output{}; |
| 159 | CLConvertFullyConnectedWeights _func{}; |
| 160 | }; |
| 161 | } // namespace weights_transformations |
Giorgio Arena | 657bdb3 | 2018-04-26 18:52:01 +0100 | [diff] [blame] | 162 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 163 | #endif /* ARM_COMPUTE_CLCONVERTFULLYCONNECTEDWEIGHTS_H */ |