xref: /aosp_15_r20/external/ComputeLibrary/src/graph/Utils.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "arm_compute/graph/Utils.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #include "arm_compute/graph/GraphContext.h"
27*c217d954SCole Faust #include "arm_compute/graph/backends/BackendRegistry.h"
28*c217d954SCole Faust #include "arm_compute/graph/mutators/GraphMutators.h"
29*c217d954SCole Faust 
30*c217d954SCole Faust namespace arm_compute
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace graph
33*c217d954SCole Faust {
is_target_supported(Target target)34*c217d954SCole Faust bool is_target_supported(Target target)
35*c217d954SCole Faust {
36*c217d954SCole Faust     return backends::BackendRegistry::get().contains(target) && backends::BackendRegistry::get().find_backend(target)->is_backend_supported();
37*c217d954SCole Faust }
38*c217d954SCole Faust 
get_default_target()39*c217d954SCole Faust Target get_default_target()
40*c217d954SCole Faust {
41*c217d954SCole Faust     if(is_target_supported(Target::NEON))
42*c217d954SCole Faust     {
43*c217d954SCole Faust         return Target::NEON;
44*c217d954SCole Faust     }
45*c217d954SCole Faust     if(is_target_supported(Target::CL))
46*c217d954SCole Faust     {
47*c217d954SCole Faust         return Target::CL;
48*c217d954SCole Faust     }
49*c217d954SCole Faust     ARM_COMPUTE_ERROR("No backend exists!");
50*c217d954SCole Faust }
51*c217d954SCole Faust 
force_target_to_graph(Graph & g,Target target)52*c217d954SCole Faust void force_target_to_graph(Graph &g, Target target)
53*c217d954SCole Faust {
54*c217d954SCole Faust     auto &nodes = g.nodes();
55*c217d954SCole Faust     for(auto &node : nodes)
56*c217d954SCole Faust     {
57*c217d954SCole Faust         if(node)
58*c217d954SCole Faust         {
59*c217d954SCole Faust             node->set_assigned_target(target);
60*c217d954SCole Faust         }
61*c217d954SCole Faust     }
62*c217d954SCole Faust 
63*c217d954SCole Faust     auto &tensors = g.tensors();
64*c217d954SCole Faust     for(auto &tensor : tensors)
65*c217d954SCole Faust     {
66*c217d954SCole Faust         if(tensor)
67*c217d954SCole Faust         {
68*c217d954SCole Faust             tensor->desc().target = target;
69*c217d954SCole Faust         }
70*c217d954SCole Faust     }
71*c217d954SCole Faust }
72*c217d954SCole Faust 
create_default_pass_manager(Target target,const GraphConfig & cfg)73*c217d954SCole Faust PassManager create_default_pass_manager(Target target, const GraphConfig &cfg)
74*c217d954SCole Faust {
75*c217d954SCole Faust     ARM_COMPUTE_UNUSED(target);
76*c217d954SCole Faust     PassManager pm;
77*c217d954SCole Faust 
78*c217d954SCole Faust     // Passes that mutate graph IR
79*c217d954SCole Faust     if(cfg.use_synthetic_type)
80*c217d954SCole Faust     {
81*c217d954SCole Faust         switch(cfg.synthetic_type)
82*c217d954SCole Faust         {
83*c217d954SCole Faust             case DataType::QASYMM8:
84*c217d954SCole Faust             case DataType::QASYMM8_SIGNED:
85*c217d954SCole Faust             {
86*c217d954SCole Faust                 pm.append(std::make_unique<SyntheticDataTypeMutator>(cfg.synthetic_type));
87*c217d954SCole Faust                 break;
88*c217d954SCole Faust             }
89*c217d954SCole Faust             default:
90*c217d954SCole Faust             {
91*c217d954SCole Faust                 ARM_COMPUTE_ERROR("Unsupported DataType for SyntheticDataTypeMutator");
92*c217d954SCole Faust                 break;
93*c217d954SCole Faust             }
94*c217d954SCole Faust         }
95*c217d954SCole Faust     }
96*c217d954SCole Faust     pm.append(std::make_unique<NodeFusionMutator>());
97*c217d954SCole Faust     pm.append(std::make_unique<GroupedConvolutionMutator>());
98*c217d954SCole Faust     pm.append(std::make_unique<InPlaceOperationMutator>());
99*c217d954SCole Faust 
100*c217d954SCole Faust     // Passes that mutate backend information
101*c217d954SCole Faust     pm.append(std::make_unique<DepthConcatSubTensorMutator>());
102*c217d954SCole Faust     pm.append(std::make_unique<SplitLayerSubTensorMutator>());
103*c217d954SCole Faust     pm.append(std::make_unique<NodeExecutionMethodMutator>());
104*c217d954SCole Faust 
105*c217d954SCole Faust     return pm;
106*c217d954SCole Faust }
107*c217d954SCole Faust 
release_default_graph_context(GraphContext & ctx)108*c217d954SCole Faust void release_default_graph_context(GraphContext &ctx)
109*c217d954SCole Faust {
110*c217d954SCole Faust     for(const auto &backend : backends::BackendRegistry::get().backends())
111*c217d954SCole Faust     {
112*c217d954SCole Faust         if(backend.second->is_backend_supported())
113*c217d954SCole Faust         {
114*c217d954SCole Faust             backend.second->release_backend_context(ctx);
115*c217d954SCole Faust         }
116*c217d954SCole Faust     }
117*c217d954SCole Faust }
118*c217d954SCole Faust 
sync_backends()119*c217d954SCole Faust void sync_backends()
120*c217d954SCole Faust {
121*c217d954SCole Faust     for(const auto &backend : backends::BackendRegistry::get().backends())
122*c217d954SCole Faust     {
123*c217d954SCole Faust         if(backend.second->backend_allocator())
124*c217d954SCole Faust         {
125*c217d954SCole Faust             backend.second->sync();
126*c217d954SCole Faust         }
127*c217d954SCole Faust     }
128*c217d954SCole Faust }
129*c217d954SCole Faust 
setup_requested_backend_context(GraphContext & ctx,Target target)130*c217d954SCole Faust void setup_requested_backend_context(GraphContext &ctx, Target target)
131*c217d954SCole Faust {
132*c217d954SCole Faust     if(backends::BackendRegistry::get().contains(target))
133*c217d954SCole Faust     {
134*c217d954SCole Faust         const auto &backend = backends::BackendRegistry::get().find_backend(target);
135*c217d954SCole Faust         if(backend->is_backend_supported())
136*c217d954SCole Faust         {
137*c217d954SCole Faust             backend->setup_backend_context(ctx);
138*c217d954SCole Faust         }
139*c217d954SCole Faust     }
140*c217d954SCole Faust }
141*c217d954SCole Faust 
get_dimension_size(const TensorDescriptor & descriptor,const DataLayoutDimension data_layout_dimension)142*c217d954SCole Faust size_t get_dimension_size(const TensorDescriptor &descriptor, const DataLayoutDimension data_layout_dimension)
143*c217d954SCole Faust {
144*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_MSG(descriptor.layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
145*c217d954SCole Faust     return descriptor.shape[get_dimension_idx(descriptor.layout, data_layout_dimension)];
146*c217d954SCole Faust }
147*c217d954SCole Faust 
get_dimension_idx(DataLayout data_layout,const DataLayoutDimension data_layout_dimension)148*c217d954SCole Faust size_t get_dimension_idx(DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
149*c217d954SCole Faust {
150*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON_MSG(data_layout == DataLayout::UNKNOWN, "Cannot retrieve the dimension index for an unknown layout!");
151*c217d954SCole Faust 
152*c217d954SCole Faust     /* Return the index based on the data layout
153*c217d954SCole Faust      * [N C H W]
154*c217d954SCole Faust      * [3 2 1 0]
155*c217d954SCole Faust      * [N H W C]
156*c217d954SCole Faust      */
157*c217d954SCole Faust     switch(data_layout_dimension)
158*c217d954SCole Faust     {
159*c217d954SCole Faust         case DataLayoutDimension::CHANNEL:
160*c217d954SCole Faust             return (data_layout == DataLayout::NCHW) ? 2 : 0;
161*c217d954SCole Faust             break;
162*c217d954SCole Faust         case DataLayoutDimension::HEIGHT:
163*c217d954SCole Faust             return (data_layout == DataLayout::NCHW) ? 1 : 2;
164*c217d954SCole Faust             break;
165*c217d954SCole Faust         case DataLayoutDimension::WIDTH:
166*c217d954SCole Faust             return (data_layout == DataLayout::NCHW) ? 0 : 1;
167*c217d954SCole Faust             break;
168*c217d954SCole Faust         case DataLayoutDimension::BATCHES:
169*c217d954SCole Faust             return 3;
170*c217d954SCole Faust             break;
171*c217d954SCole Faust         default:
172*c217d954SCole Faust             break;
173*c217d954SCole Faust     }
174*c217d954SCole Faust     ARM_COMPUTE_ERROR("Data layout index not supported!");
175*c217d954SCole Faust }
176*c217d954SCole Faust 
get_driving_nodes(const INode & node)177*c217d954SCole Faust std::vector<NodeIdxPair> get_driving_nodes(const INode &node)
178*c217d954SCole Faust {
179*c217d954SCole Faust     std::vector<NodeIdxPair> driving_nodes;
180*c217d954SCole Faust 
181*c217d954SCole Faust     const Graph *g = node.graph();
182*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(g == nullptr);
183*c217d954SCole Faust 
184*c217d954SCole Faust     for(auto &output_edge_id : node.output_edges())
185*c217d954SCole Faust     {
186*c217d954SCole Faust         auto output_edge = g->edge(output_edge_id);
187*c217d954SCole Faust         if(output_edge != nullptr)
188*c217d954SCole Faust         {
189*c217d954SCole Faust             ARM_COMPUTE_ERROR_ON(output_edge->consumer() == nullptr);
190*c217d954SCole Faust             driving_nodes.push_back({ output_edge->consumer_id(), output_edge->consumer_idx() });
191*c217d954SCole Faust         }
192*c217d954SCole Faust     }
193*c217d954SCole Faust 
194*c217d954SCole Faust     return driving_nodes;
195*c217d954SCole Faust }
196*c217d954SCole Faust 
get_driver_nodes(const INode & node)197*c217d954SCole Faust std::vector<NodeIdxPair> get_driver_nodes(const INode &node)
198*c217d954SCole Faust {
199*c217d954SCole Faust     std::vector<NodeIdxPair> driver_nodes;
200*c217d954SCole Faust 
201*c217d954SCole Faust     const Graph *g = node.graph();
202*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(g == nullptr);
203*c217d954SCole Faust 
204*c217d954SCole Faust     for(auto &input_edge_id : node.input_edges())
205*c217d954SCole Faust     {
206*c217d954SCole Faust         auto input_edge = g->edge(input_edge_id);
207*c217d954SCole Faust         if(input_edge != nullptr)
208*c217d954SCole Faust         {
209*c217d954SCole Faust             ARM_COMPUTE_ERROR_ON(input_edge->producer() == nullptr);
210*c217d954SCole Faust             driver_nodes.push_back({ input_edge->producer_id(), input_edge->producer_idx() });
211*c217d954SCole Faust         }
212*c217d954SCole Faust     }
213*c217d954SCole Faust 
214*c217d954SCole Faust     return driver_nodes;
215*c217d954SCole Faust }
216*c217d954SCole Faust 
configure_tensor(Tensor * tensor)217*c217d954SCole Faust void configure_tensor(Tensor *tensor)
218*c217d954SCole Faust {
219*c217d954SCole Faust     if(tensor != nullptr && tensor->handle() == nullptr)
220*c217d954SCole Faust     {
221*c217d954SCole Faust         Target                         target  = tensor->desc().target;
222*c217d954SCole Faust         backends::IDeviceBackend      &backend = backends::BackendRegistry::get().get_backend(target);
223*c217d954SCole Faust         std::unique_ptr<ITensorHandle> handle  = backend.create_tensor(*tensor);
224*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_MSG(!handle, "Couldn't create backend handle!");
225*c217d954SCole Faust         tensor->set_handle(std::move(handle));
226*c217d954SCole Faust     }
227*c217d954SCole Faust }
228*c217d954SCole Faust 
229*c217d954SCole Faust } // namespace graph
230*c217d954SCole Faust } // namespace arm_compute
231