blob: b420121c422ecc94dacfd67430d389219e7bcf47 [file] [log] [blame]
Georgios Pinitas2a2db592018-08-15 12:14:46 +01001/*
2 * Copyright (c) 2018 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 "arm_compute/core/utils/misc/Cast.h"
33
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
76void NodeExecutionMethodMutator::mutate(Graph &g)
77{
78 // Convolution Layer
79 set_default_on_invalid_method(g, NodeType::ConvolutionLayer, [](INode * n)
80 {
81 ARM_COMPUTE_LOG_GRAPH_INFO("Switched ConvolutionLayer method of node with ID : "
82 << n->id() << " and Name: " << n->name() << std::endl);
83 auto *casted_node = arm_compute::utils::cast::polymorphic_downcast<ConvolutionLayerNode *>(n);
84 casted_node->set_convolution_method(ConvolutionMethod::Default);
85 });
86
87 // Depthwise Convolution Layer
88 set_default_on_invalid_method(g, NodeType::DepthwiseConvolutionLayer, [](INode * n)
89 {
90 ARM_COMPUTE_LOG_GRAPH_INFO("Switched Depthwise ConvolutionLayer method of node with ID : "
91 << n->id() << " and Name: " << n->name() << std::endl);
92 auto *casted_node = arm_compute::utils::cast::polymorphic_downcast<DepthwiseConvolutionLayerNode *>(n);
93 casted_node->set_depthwise_convolution_method(DepthwiseConvolutionMethod::Default);
94 });
95}
96} // namespace graph
97} // namespace arm_compute