Compute Library
 21.11
NodeExecutionMethodMutator.cpp
Go to the documentation of this file.
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  */
25 
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>
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 
72 {
73  return "NodeExecutionMethodMutator";
74 }
75 
77 {
79 }
80 
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
std::string name() const
Returns node&#39;s name.
Definition: INode.cpp:108
const char * name() override
Returns mutator name.
IDeviceBackend & get_backend(Target target)
Get a backend from the registry.
#define ARM_COMPUTE_LOG_GRAPH_INFO(x)
Definition: Logger.h:54
Copyright (c) 2017-2021 Arm Limited.
MutationType type() const override
Returns mutation type.
Node interface.
Definition: INode.h:46
virtual void mutate(Graph &g) override
Walk the graph and perform a specific mutation.
NodeID id() const
Returns node&#39;s ID.
Definition: INode.cpp:103
NodeType
Supported nodes.
Definition: Types.h:199
static BackendRegistry & get()
Gets backend registry instance.
virtual Status validate_node(INode &node)=0
Validate a node.
Graph class.
Definition: Graph.h:53
Default approach using internal heuristics.
Default approach using internal heuristics.