1 /* 2 * Copyright (c) 2018-2020 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_NECONVERTFULLYCONNECTEDWEIGHTS_H 25 #define ARM_COMPUTE_NECONVERTFULLYCONNECTEDWEIGHTS_H 26 27 #include "arm_compute/runtime/IFunction.h" 28 #include "arm_compute/runtime/ITransformWeights.h" 29 #include "arm_compute/runtime/NEON/NEScheduler.h" 30 #include "arm_compute/runtime/Tensor.h" 31 #include <memory> 32 33 namespace arm_compute 34 { 35 // Forward declarations 36 class ITensor; 37 class NEConvertFullyConnectedWeightsKernel; 38 39 /** Basic function to run @ref NEConvertFullyConnectedWeightsKernel. */ 40 class NEConvertFullyConnectedWeights : public IFunction 41 { 42 public: 43 /** Default constructor */ 44 NEConvertFullyConnectedWeights(); 45 /** Prevent instances of this class from being copied (As this class contains pointers) */ 46 NEConvertFullyConnectedWeights(const NEConvertFullyConnectedWeights &) = delete; 47 /** Prevent instances of this class from being copied (As this class contains pointers) */ 48 NEConvertFullyConnectedWeights &operator=(const NEConvertFullyConnectedWeights &) = delete; 49 /** Prevent instances of this class from being moved (As this class contains non movable objects) */ 50 NEConvertFullyConnectedWeights(NEConvertFullyConnectedWeights &&) = delete; 51 /** Prevent instances of this class from being moved (As this class contains non movable objects) */ 52 NEConvertFullyConnectedWeights &operator=(NEConvertFullyConnectedWeights &&) = delete; 53 /** Default destructor */ 54 ~NEConvertFullyConnectedWeights(); 55 /** Initialize the function. 56 * 57 * @param[in] input Source weights tensor to convert. Must be 2 dimensional. Data types supported: All. 58 * @param[out] output The converted weights tensor. Shape and Data Type: Same as @p input. 59 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer). 60 * @param[in] data_layout The data layout the weights have been trained in. 61 */ 62 void configure(const ITensor *input, ITensor *output, const TensorShape &original_input_shape, DataLayout data_layout); 63 /** Static function to check if given info will lead to a valid configuration of @ref NEConvertFullyConnectedWeights 64 * 65 * @param[in] input Source weights tensor info to convert. Must be 2 dimensional. Data types supported: All. 66 * @param[in] output The converted weights tensor info. Shape and Data Type: Same as @p input. 67 * @param[in] original_input_shape Shape of the original input tensor (the one entering fully connected layer). 68 * @param[in] data_layout The data layout the weights have been trained in. 69 * 70 * @return A Status 71 */ 72 static Status validate(const ITensorInfo *input, const ITensorInfo *output, const TensorShape &original_input_shape, DataLayout data_layout); 73 74 // Inherited methods overriden: 75 void run() override; 76 77 private: 78 std::unique_ptr<NEConvertFullyConnectedWeightsKernel> _kernel; 79 }; 80 81 namespace weights_transformations 82 { 83 /** Basic function to run @ref NEConvertFullyConnectedWeightsKernel. */ 84 class NEConvertFullyConnectedWeightsManaged : public ITransformWeights 85 { 86 public: run()87 void run() override 88 { 89 _output.allocator()->allocate(); 90 _func.run(); 91 _reshape_run = true; 92 } 93 release()94 void release() override 95 { 96 _output.allocator()->free(); 97 } 98 get_weights()99 ITensor *get_weights() override 100 { 101 return &_output; 102 } 103 uid()104 uint32_t uid() override 105 { 106 return _uid; 107 } 108 configure(const ITensor * input,const TensorShape & original_input_shape,DataLayout data_layout)109 void configure(const ITensor *input, const TensorShape &original_input_shape, DataLayout data_layout) 110 { 111 _func.configure(input, &_output, original_input_shape, data_layout); 112 } 113 114 private: 115 static constexpr uint32_t _uid = 0x4; 116 Tensor _output{}; 117 NEConvertFullyConnectedWeights _func{}; 118 }; 119 } // namespace weights_transformations 120 } // namespace arm_compute 121 #endif /* ARM_COMPUTE_NECONVERTFULLYCONNECTEDWEIGHTS_H */ 122