blob: de8a92faa3a7c0509409a588dc451de77f4f9069 [file] [log] [blame]
Michalis Spyrou1a569a32019-09-10 17:20:34 +01001/*
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +01002 * Copyright (c) 2019, 2021 Arm Limited.
Michalis Spyrou1a569a32019-09-10 17:20:34 +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 */
24
Michalis Spyrouf4643372019-11-29 16:17:13 +000025#ifndef ARM_COMPUTE_IWEIGHTSMANAGER_H
26#define ARM_COMPUTE_IWEIGHTSMANAGER_H
Michalis Spyrou1a569a32019-09-10 17:20:34 +010027
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/runtime/ITransformWeights.h"
30
31#include <map>
32
33namespace arm_compute
34{
35/** Weights manager interface to handle weights transformations */
36class IWeightsManager
37{
38public:
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 Pinitas45fbf9f2021-08-13 12:07:59 +010079 /** 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 Arena73df9312021-08-16 12:29:27 +010084 /** Pre-mark the weights as unused. The weights tensor will get marked as unused only when the counter goes to 0
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010085 *
86 * @param weights Weights to mark unused
87 */
Giorgio Arena73df9312021-08-16 12:29:27 +010088 void pre_mark_as_unused(const ITensor *weights);
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010089
90private:
91 struct CounterElement
92 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093 bool is_unused{false};
94 std::atomic<int> counter{1};
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010095 };
Michalis Spyrou1a569a32019-09-10 17:20:34 +010096
97private:
98 std::map<const ITensor *, std::vector<ITransformWeights *>> _managed_weights;
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010099 std::map<const ITensor *, CounterElement> _managed_counter;
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100100 std::map<const ITensor *, ITransformWeights *> _managed_weights_parents;
101};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102} // namespace arm_compute
103#endif /*ARM_COMPUTE_IWEIGHTSMANAGER_H */