xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/graph/backends/Utils.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
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_GRAPH_BACKENDS_UTILS_H
25 #define ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H
26 
27 #include "arm_compute/graph/GraphContext.h"
28 #include "arm_compute/runtime/IMemoryManager.h"
29 #include "arm_compute/runtime/IWeightsManager.h"
30 
31 namespace arm_compute
32 {
33 namespace graph
34 {
35 namespace backends
36 {
37 /** Creates and configures a named function
38  *
39  * @param[in] name Name of the function
40  * @param[in] args Function arguments
41  *
42  * @return  A configured backend function
43  */
44 template <typename FunctionType, typename FunctionNameType, typename... ParameterType>
create_named_function(FunctionNameType name,ParameterType...args)45 std::tuple<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_function(FunctionNameType name, ParameterType... args)
46 {
47     auto f = std::make_unique<FunctionType>();
48     f->configure(std::forward<ParameterType>(args)...);
49     return std::make_pair(std::move(f), name);
50 }
51 
52 /** Creates and configures a named function
53  *
54  * @param[in] name Name of the function
55  * @param[in] mm   Memory manager to use
56  * @param[in] args Function arguments
57  *
58  * @return  A configured backend function
59  */
60 template <typename FunctionType, typename FunctionNameType, typename MemoryManagerType, typename... ParameterType>
create_named_memory_managed_function(FunctionNameType name,MemoryManagerType mm,ParameterType...args)61 std::tuple<std::unique_ptr<arm_compute::IFunction>, FunctionNameType> create_named_memory_managed_function(FunctionNameType name,
62                                                                                                            MemoryManagerType mm,
63                                                                                                            ParameterType... args)
64 {
65     auto f = std::make_unique<FunctionType>(mm);
66     f->configure(std::forward<ParameterType>(args)...);
67     return std::make_pair(std::move(f), name);
68 }
69 
70 /** Checks if an operation is in place
71  *
72  * @param[in] input  Pointer to input
73  * @param[in] output Pointer to output
74  *
75  * @return True if output is nullptr or input is equal to the output, else false
76  */
is_in_place_operation(void * input,void * output)77 inline bool is_in_place_operation(void *input, void *output)
78 {
79     return (output == nullptr) || (input == output);
80 }
81 
82 /** Returns the memory manager for a given target
83  *
84  * @param[in] ctx    Graph context containing memory management metadata
85  * @param[in] target Target to retrieve the memory manager from
86  *
87  * @return The memory manager for the given target else false
88  */
get_memory_manager(GraphContext & ctx,Target target)89 inline std::shared_ptr<IMemoryManager> get_memory_manager(GraphContext &ctx, Target target)
90 {
91     bool enabled = ctx.config().use_function_memory_manager && (ctx.memory_management_ctx(target) != nullptr);
92     return enabled ? ctx.memory_management_ctx(target)->intra_mm : nullptr;
93 }
94 
95 /** Returns the weights manager for a given target
96  *
97  * @param[in] ctx    Graph context containing weight management metadata
98  * @param[in] target Target to retrieve the weights manager from
99  *
100  * @return The weights manager for the given target else false
101  */
get_weights_manager(GraphContext & ctx,Target target)102 inline std::shared_ptr<IWeightsManager> get_weights_manager(GraphContext &ctx, Target target)
103 {
104     bool enabled = ctx.config().use_function_weights_manager && (ctx.weights_management_ctx(target) != nullptr);
105     return enabled ? ctx.weights_management_ctx(target)->wm : nullptr;
106 }
107 } // namespace backends
108 } // namespace graph
109 } // namespace arm_compute
110 
111 #endif /* ARM_COMPUTE_GRAPH_BACKENDS_UTILS_H */
112