blob: 2e893c2e07ec342c2b0952499d2701bb116acb47 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/mutators/NodeFusionMutator.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/graph/Graph.h"
27#include "arm_compute/graph/Logger.h"
28#include "arm_compute/graph/nodes/Nodes.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000029
30#include "arm_compute/core/utils/misc/Cast.h"
31
32namespace arm_compute
33{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010034namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000035{
36namespace detail
37{
38void fuse_batch_norm_with_activation(Graph &g)
39{
40 // Not interested in the order of nodes
41 for(auto &node : g.nodes())
42 {
43 // Check if the node is batch norm and not a branching node
44 if(node && node->type() == NodeType::BatchNormalizationLayer && node->output_edges().size() == 1)
45 {
46 auto output_edge_id = *node->output_edges().begin();
47 auto output_edge = g.edge(output_edge_id);
48 // Check if following node is an activation layer node
49 if((output_edge != nullptr) && (output_edge->consumer() != nullptr) && (output_edge->consumer()->type() == NodeType::ActivationLayer))
50 {
51 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Fusing Batch Normalization node with ID : " << output_edge->producer_id()
52 << " with Activation Layer node with ID : " << output_edge->consumer_id() << std::endl);
53
54 auto *bn_node = arm_compute::utils::cast::polymorphic_downcast<BatchNormalizationLayerNode *>(output_edge->producer());
55 auto *act_node = arm_compute::utils::cast::polymorphic_downcast<ActivationLayerNode *>(output_edge->consumer());
56
57 // Get driving nodes of activation node
58 std::vector<NodeIdxPair> act_driving_nodes;
59 for(auto &act_output_edge_id : act_node->output_edges())
60 {
61 auto act_output_edge = g.edge(act_output_edge_id);
62 if(act_output_edge != nullptr)
63 {
64 ARM_COMPUTE_ERROR_ON(act_output_edge->consumer() == nullptr);
65 act_driving_nodes.push_back({ act_output_edge->consumer_id(), act_output_edge->consumer_idx() });
66 }
67 }
68
69 // Set activation info to batch normalization
70 bn_node->set_fused_activation(act_node->activation_info());
71
72 // Remove activation node
73 g.remove_node(act_node->id());
74
75 // Update batch normalization node outputs
76 for(auto &driving_node : act_driving_nodes)
77 {
78 g.add_connection(bn_node->id(), 0, driving_node.node_id, driving_node.index);
79 }
80 }
81 }
82 }
83}
84} // namespace detail
85
86const char *NodeFusionMutator::name()
87{
88 return "NodeFusionMutator";
89}
90
91void NodeFusionMutator::mutate(Graph &g)
92{
93 detail::fuse_batch_norm_with_activation(g);
94}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010095} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000096} // namespace arm_compute