Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 45fbf9f | 2021-08-13 12:07:59 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019, 2021 Arm Limited. |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +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 | */ |
| 24 | |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 25 | #ifndef ARM_COMPUTE_IWEIGHTSMANAGER_H |
| 26 | #define ARM_COMPUTE_IWEIGHTSMANAGER_H |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 27 | |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/runtime/ITransformWeights.h" |
| 30 | |
| 31 | #include <map> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | /** Weights manager interface to handle weights transformations */ |
| 36 | class IWeightsManager |
| 37 | { |
| 38 | public: |
| 39 | /** Constructor */ |
| 40 | IWeightsManager(); |
| 41 | /** Default Destructor */ |
| 42 | virtual ~IWeightsManager() = default; |
| 43 | /** Prevent instances of this class to be copy constructed */ |
| 44 | IWeightsManager(const IWeightsManager &) = delete; |
| 45 | /** Prevent instances of this class to be copied */ |
| 46 | IWeightsManager &operator=(const IWeightsManager &) = delete; |
| 47 | /** Allow instances of this class to be move constructed */ |
| 48 | IWeightsManager(IWeightsManager &&) = default; |
| 49 | /** Allow instances of this class to be moved */ |
| 50 | IWeightsManager &operator=(IWeightsManager &&) = default; |
| 51 | |
| 52 | /** Start managing a weights tensor |
| 53 | * |
| 54 | * @param[in] weights Pointer to the weights tensor to be managed |
| 55 | * @param[in] parent Parent node in case where the weights are coming from a previous reshape function |
| 56 | */ |
| 57 | void manage(const ITensor *weights, ITransformWeights *parent = nullptr); |
| 58 | /** Run the reshape function. |
| 59 | * |
| 60 | * @param[in] weights Pointer to the weights tensor we want to reshape |
| 61 | * @param[in] weights_transform Weights transformation object |
| 62 | * |
| 63 | * @return The reshaped tensor |
| 64 | */ |
| 65 | ITensor *run(const ITensor *weights, ITransformWeights *weights_transform); |
| 66 | /** Acquire the requested reshape tensor of the selected weights |
| 67 | * |
| 68 | * @param[in] weights Pointer to the weights tensor to be managed |
| 69 | * @param[in] weights_transform Weights transformation object |
| 70 | */ |
| 71 | ITensor *acquire(const ITensor *weights, ITransformWeights *weights_transform); |
| 72 | /** Check if the weights are managed |
| 73 | * |
| 74 | * @param[in] weights Pointer to the weights tensor we want to check if managed |
| 75 | * |
| 76 | * @return True if the weights tensor is managed else false |
| 77 | */ |
| 78 | bool are_weights_managed(const ITensor *weights); |
Georgios Pinitas | 45fbf9f | 2021-08-13 12:07:59 +0100 | [diff] [blame] | 79 | /** Release weights refcount and mark as unused if reaches 0 |
| 80 | * |
| 81 | * @param[in] weights Weights to release |
| 82 | */ |
| 83 | void release(const ITensor *weights); |
Giorgio Arena | 73df931 | 2021-08-16 12:29:27 +0100 | [diff] [blame] | 84 | /** Pre-mark the weights as unused. The weights tensor will get marked as unused only when the counter goes to 0 |
Georgios Pinitas | 45fbf9f | 2021-08-13 12:07:59 +0100 | [diff] [blame] | 85 | * |
| 86 | * @param weights Weights to mark unused |
| 87 | */ |
Giorgio Arena | 73df931 | 2021-08-16 12:29:27 +0100 | [diff] [blame] | 88 | void pre_mark_as_unused(const ITensor *weights); |
Georgios Pinitas | 45fbf9f | 2021-08-13 12:07:59 +0100 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | struct CounterElement |
| 92 | { |
| 93 | bool is_unused{ false }; |
| 94 | std::atomic<int> counter{ 1 }; |
| 95 | }; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | std::map<const ITensor *, std::vector<ITransformWeights *>> _managed_weights; |
Georgios Pinitas | 45fbf9f | 2021-08-13 12:07:59 +0100 | [diff] [blame] | 99 | std::map<const ITensor *, CounterElement> _managed_counter; |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 100 | std::map<const ITensor *, ITransformWeights *> _managed_weights_parents; |
| 101 | }; |
| 102 | } // arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 103 | #endif /*ARM_COMPUTE_IWEIGHTSMANAGER_H */ |