blob: 9dc02d1ad16fa9472f288083b72c67442b8b992c [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"
Georgios Pinitas2a2db592018-08-15 12:14:46 +010028#include "arm_compute/graph/Utils.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010029#include "arm_compute/graph/nodes/Nodes.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000030
31#include "arm_compute/core/utils/misc/Cast.h"
32
Georgios Pinitas6f109bd2018-07-16 12:57:42 +010033#include <set>
34
Georgios Pinitasd8734b52017-12-22 15:27:52 +000035namespace arm_compute
36{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010037namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000038{
39namespace detail
40{
Georgios Pinitas08346e92018-10-16 19:10:46 +010041template <typename N>
Georgios Pinitas60e98252018-10-22 16:17:20 +010042void fuse_node_with_activation(Graph &g,
43 const std::set<Activation> &supported_fused_activations,
44 std::function<bool(INode &)> const &prec)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000045{
46 // Not interested in the order of nodes
47 for(auto &node : g.nodes())
48 {
Georgios Pinitas60e98252018-10-22 16:17:20 +010049 // Check if the node is of type N and not a branching node
Georgios Pinitas08346e92018-10-16 19:10:46 +010050 if(node && node->type() == N::node_type && node->output_edges().size() == 1)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000051 {
52 auto output_edge_id = *node->output_edges().begin();
53 auto output_edge = g.edge(output_edge_id);
54 // Check if following node is an activation layer node
55 if((output_edge != nullptr) && (output_edge->consumer() != nullptr) && (output_edge->consumer()->type() == NodeType::ActivationLayer))
56 {
Georgios Pinitas08346e92018-10-16 19:10:46 +010057 auto *n_node = arm_compute::utils::cast::polymorphic_downcast<N *>(output_edge->producer());
Georgios Pinitasd8734b52017-12-22 15:27:52 +000058 auto *act_node = arm_compute::utils::cast::polymorphic_downcast<ActivationLayerNode *>(output_edge->consumer());
59
Georgios Pinitas08346e92018-10-16 19:10:46 +010060 ARM_COMPUTE_ERROR_ON(act_node->output(0) == nullptr || n_node->output(0) == nullptr);
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010061
Georgios Pinitas60e98252018-10-22 16:17:20 +010062 // Check given precondition
63 if(!prec(*n_node))
64 {
65 continue;
66 }
Georgios Pinitas6f109bd2018-07-16 12:57:42 +010067 // Check if activation is supported for fusion
68 if(supported_fused_activations.count(act_node->activation_info().activation()) == 0)
69 {
70 continue;
71 }
72
Georgios Pinitas08346e92018-10-16 19:10:46 +010073 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Fusing node with ID : " << output_edge->producer_id()
Georgios Pinitas6f109bd2018-07-16 12:57:42 +010074 << " with Activation Layer node with ID : " << output_edge->consumer_id() << std::endl);
75
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000076 // Prevent fusion if fused node has an output accessor
Georgios Pinitas08346e92018-10-16 19:10:46 +010077 if(n_node->output(0)->accessor() == nullptr)
Georgios Pinitasd8734b52017-12-22 15:27:52 +000078 {
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010079 // Get driving nodes of activation node
Georgios Pinitas2a2db592018-08-15 12:14:46 +010080 std::vector<NodeIdxPair> act_driving_nodes = get_driving_nodes(*act_node);
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010081
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000082 // Set activation info to fused node
Georgios Pinitas08346e92018-10-16 19:10:46 +010083 n_node->set_fused_activation(act_node->activation_info());
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010084
85 // Extract activation node accessor if any
86 auto act_node_accessor = act_node->output(0)->extract_accessor();
87
88 // Remove activation node
89 g.remove_node(act_node->id());
90
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000091 // Update fused node outputs
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010092 for(auto &driving_node : act_driving_nodes)
93 {
Georgios Pinitas08346e92018-10-16 19:10:46 +010094 g.add_connection(n_node->id(), 0, driving_node.node_id, driving_node.index);
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010095 }
96
Georgios Pinitas1c32bf3962018-11-12 18:36:19 +000097 // Update accessor to fused node
Georgios Pinitas08346e92018-10-16 19:10:46 +010098 n_node->output(0)->set_accessor(std::move(act_node_accessor));
Georgios Pinitasd8734b52017-12-22 15:27:52 +000099 }
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +0100100 else
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000101 {
Georgios Pinitas08346e92018-10-16 19:10:46 +0100102 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented fusion of node with activation due to the presence of an output accessor\n");
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000103 }
104 }
105 }
106 }
107}
108} // namespace detail
109
110const char *NodeFusionMutator::name()
111{
112 return "NodeFusionMutator";
113}
114
115void NodeFusionMutator::mutate(Graph &g)
116{
Georgios Pinitas08346e92018-10-16 19:10:46 +0100117 // Supported activations when fusing
118 const std::set<Activation> supported_fused_activations = { Activation::RELU, Activation::BOUNDED_RELU, Activation::LU_BOUNDED_RELU };
119
Georgios Pinitas60e98252018-10-22 16:17:20 +0100120 // Preconditions
121 auto empty_prec = [](INode & n)
122 {
123 return true;
124 };
125 auto qs8_prec = [](INode & n)
126 {
127 ARM_COMPUTE_ERROR_ON(n.output(0) == nullptr);
128 return n.output(0)->desc().data_type == DataType::QASYMM8;
129 };
130
131 // Fusion mutations
132 detail::fuse_node_with_activation<BatchNormalizationLayerNode>(g, supported_fused_activations, empty_prec);
133 detail::fuse_node_with_activation<ConvolutionLayerNode>(g, supported_fused_activations, empty_prec);
134 detail::fuse_node_with_activation<DepthwiseConvolutionLayerNode>(g, supported_fused_activations, qs8_prec);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000135}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100136} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000137} // namespace arm_compute