xref: /aosp_15_r20/external/ComputeLibrary/src/graph/mutators/NodeExecutionMethodMutator.cpp (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 #include "arm_compute/graph/mutators/NodeExecutionMethodMutator.h"
25 
26 #include "arm_compute/graph/Graph.h"
27 #include "arm_compute/graph/Logger.h"
28 #include "arm_compute/graph/Utils.h"
29 #include "arm_compute/graph/backends/BackendRegistry.h"
30 #include "arm_compute/graph/nodes/Nodes.h"
31 
32 #include "support/Cast.h"
33 
34 namespace arm_compute
35 {
36 namespace graph
37 {
38 namespace
39 {
40 /** Runs a default setter function on a given types of nodes
41  *
42  * @tparam Setter Setter function to run
43  *
44  * @param[in, out] g         Graph to extract the nodes from
45  * @param[in]      node_type Node type
46  * @param[in]      setter    Setter function
47  */
48 template <typename Setter>
set_default_on_invalid_method(Graph & g,NodeType node_type,Setter && setter)49 void set_default_on_invalid_method(Graph &g, NodeType node_type, Setter &&setter)
50 {
51     const std::vector<NodeID> &node_ids = g.nodes(node_type);
52     for(auto &node_id : node_ids)
53     {
54         INode *node = g.node(node_id);
55         if(node != nullptr)
56         {
57             // Validate node
58             backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(node->assigned_target());
59             Status                    status  = backend.validate_node(*node);
60 
61             // Set default execution method in case of failure
62             if(!bool(status))
63             {
64                 setter(node);
65             }
66         }
67     }
68 }
69 } // namespace
70 
name()71 const char *NodeExecutionMethodMutator::name()
72 {
73     return "NodeExecutionMethodMutator";
74 }
75 
type() const76 IGraphMutator::MutationType NodeExecutionMethodMutator::type() const
77 {
78     return IGraphMutator::MutationType::Backend;
79 }
80 
mutate(Graph & g)81 void NodeExecutionMethodMutator::mutate(Graph &g)
82 {
83     // Convolution Layer
84     set_default_on_invalid_method(g, NodeType::ConvolutionLayer, [](INode * n)
85     {
86         ARM_COMPUTE_LOG_GRAPH_INFO("Switched ConvolutionLayer method of node with ID : "
87                                    << n->id() << " and Name: " << n->name() << std::endl);
88         auto *casted_node = arm_compute::utils::cast::polymorphic_downcast<ConvolutionLayerNode *>(n);
89         casted_node->set_convolution_method(ConvolutionMethod::Default);
90     });
91 
92     // Depthwise Convolution Layer
93     set_default_on_invalid_method(g, NodeType::DepthwiseConvolutionLayer, [](INode * n)
94     {
95         ARM_COMPUTE_LOG_GRAPH_INFO("Switched Depthwise ConvolutionLayer method of node with ID : "
96                                    << n->id() << " and Name: " << n->name() << std::endl);
97         auto *casted_node = arm_compute::utils::cast::polymorphic_downcast<DepthwiseConvolutionLayerNode *>(n);
98         casted_node->set_depthwise_convolution_method(DepthwiseConvolutionMethod::Default);
99     });
100 }
101 } // namespace graph
102 } // namespace arm_compute
103