blob: 09a3cf50c0b40f6557197d890b1a42bd98fb4b54 [file] [log] [blame]
Georgios Pinitas2a2db592018-08-15 12:14:46 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitas2a2db592018-08-15 12:14:46 +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#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
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "support/Cast.h"
Georgios Pinitas2a2db592018-08-15 12:14:46 +010033
34namespace arm_compute
35{
36namespace graph
37{
38namespace
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 */
48template <typename Setter>
49void 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
Anthony Barbier890ad1b2018-08-22 13:44:36 +010058 backends::IDeviceBackend &backend = backends::BackendRegistry::get().get_backend(node->assigned_target());
59 Status status = backend.validate_node(*node);
Georgios Pinitas2a2db592018-08-15 12:14:46 +010060
61 // Set default execution method in case of failure
62 if(!bool(status))
63 {
64 setter(node);
65 }
66 }
67 }
68}
69} // namespace
70
71const char *NodeExecutionMethodMutator::name()
72{
73 return "NodeExecutionMethodMutator";
74}
75
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000076IGraphMutator::MutationType NodeExecutionMethodMutator::type() const
77{
78 return IGraphMutator::MutationType::Backend;
79}
80
Georgios Pinitas2a2db592018-08-15 12:14:46 +010081void 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