blob: 5927a597bb661da7b429b942d6fa5b4441ab9dcb [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
giuros01acce5042019-02-21 17:32:34 +00002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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
giuros01acce5042019-02-21 17:32:34 +000026#include "arm_compute/graph/GraphBuilder.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010027#include "arm_compute/graph/Logger.h"
Georgios Pinitas2a2db592018-08-15 12:14:46 +010028#include "arm_compute/graph/Utils.h"
giuros01acce5042019-02-21 17:32:34 +000029#include "arm_compute/graph/backends/BackendRegistry.h"
30#include "arm_compute/graph/nodes/FusedConvolutionBatchNormalizationNode.h"
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031#include "arm_compute/graph/nodes/Nodes.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032
33#include "arm_compute/core/utils/misc/Cast.h"
34
Georgios Pinitas6f109bd2018-07-16 12:57:42 +010035#include <set>
36
Georgios Pinitasd8734b52017-12-22 15:27:52 +000037namespace arm_compute
38{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010039namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000040{
41namespace detail
42{
giuros01acce5042019-02-21 17:32:34 +000043void fuse_convolution_with_batch_normalization(Graph &g, const Edge *output_edge)
44{
45 ARM_COMPUTE_ERROR_ON(output_edge == nullptr);
46
47 auto *conv_node = arm_compute::utils::cast::polymorphic_downcast<ConvolutionLayerNode *>(output_edge->producer());
48 auto *bn_node = arm_compute::utils::cast::polymorphic_downcast<BatchNormalizationLayerNode *>(output_edge->consumer());
49
50 // Not fusing if number of groups is greater than 1
51 if(conv_node->num_groups() > 1)
52 {
53 return;
54 }
55
56 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Fusing convolution node with ID : " << output_edge->producer_id()
57 << " with BatchNormalization Layer node with ID : " << output_edge->consumer_id() << std::endl);
58
59 // Prevent fusion if fused node has an output accessor
60 if(conv_node->output(0)->accessor() == nullptr)
61 {
62 const Target assigned_target = conv_node->assigned_target();
63
64 // Extract conv inputs
65 const auto conv_input_id = conv_node->input_edge(0)->producer_id();
66 const auto conv_weights_id = conv_node->input_edge(1)->producer_id();
67 const auto out_quant_info = conv_node->output(0)->desc().quant_info;
68 const auto conv_info = conv_node->convolution_info();
69 const auto conv_method = conv_node->convolution_method();
70 const auto num_groups = conv_node->num_groups();
71 const auto act_info = bn_node->fused_activation();
72 FastMathHint fast_math_hint = conv_node->fast_math_hint();
73
74 // Extract bn inputs
75 const auto bn_mean_id = bn_node->input_edge(1)->producer_id();
76 const auto bn_var_id = bn_node->input_edge(2)->producer_id();
77 const auto bn_beta_id = bn_node->input_edge(3)->producer_id();
78 const auto bn_gamma_id = bn_node->input_edge(4)->producer_id();
79 const auto epsilon = bn_node->epsilon();
80
81 // Create the fused node
82 const NodeID fused_id = g.add_node<FusedConvolutionBatchNormalizationNode>(epsilon, conv_info, num_groups, conv_method, fast_math_hint, out_quant_info, act_info);
83
84 if(conv_node->input_edge(2) != nullptr)
85 {
86 auto conv_bias_id = conv_node->input_edge(2)->producer_id();
87 g.add_connection(conv_bias_id, 0, fused_id, 2);
88 }
89
90 // Add connections from the conv/batch_norm inputs to the fused node
91 g.add_connection(conv_input_id, 0, fused_id, 0);
92 g.add_connection(conv_weights_id, 0, fused_id, 1);
93 g.add_connection(bn_mean_id, 0, fused_id, 3);
94 g.add_connection(bn_var_id, 0, fused_id, 4);
95 g.add_connection(bn_beta_id, 0, fused_id, 5);
96 g.add_connection(bn_gamma_id, 0, fused_id, 6);
97
98 auto fused_node = g.node(fused_id);
99 std::vector<NodeIdxPair> bn_driving_nodes = get_driving_nodes(*bn_node);
100
101 // Extract batch normalization node accessor if any
102 auto bn_node_accessor = bn_node->output(0)->extract_accessor();
103 auto bn_node_name = bn_node->name();
104
105 // Remove batch normalization node
106 g.remove_node(bn_node->id());
107
108 // Get driving nodes of batch normalization node
109 for(auto &driving_node : bn_driving_nodes)
110 {
111 g.add_connection(fused_id, 0, driving_node.node_id, driving_node.index);
112 configure_tensor(fused_node->output(0));
113 }
114 // Update fused node outputs
115 fused_node->output(0)->set_accessor(std::move(bn_node_accessor));
116 fused_node->set_assigned_target(assigned_target);
117 fused_node->set_common_node_parameters(NodeParams{ conv_node->name() + "+" + bn_node_name, assigned_target });
118
119 // Remove convolution node
120 g.remove_node(conv_node->id());
121 }
122 else
123 {
124 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented fusion of convolution with batch normalization due to the presence of an output accessor\n");
125 }
126}
127
Georgios Pinitas08346e92018-10-16 19:10:46 +0100128template <typename N>
giuros01acce5042019-02-21 17:32:34 +0000129void fuse_node_with_activation(Graph &g, const Edge *output_edge, const std::set<Activation> &supported_fused_activations)
130{
131 ARM_COMPUTE_ERROR_ON(output_edge == nullptr);
132
133 auto *n_node = arm_compute::utils::cast::polymorphic_downcast<N *>(output_edge->producer());
134 auto *act_node = arm_compute::utils::cast::polymorphic_downcast<ActivationLayerNode *>(output_edge->consumer());
135
136 ARM_COMPUTE_ERROR_ON(act_node->output(0) == nullptr || n_node->output(0) == nullptr);
137
138 // Check if activation is supported for fusion
139 if(supported_fused_activations.count(act_node->activation_info().activation()) == 0)
140 {
141 return;
142 }
143
144 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Fusing node with ID : " << output_edge->producer_id()
145 << " with Activation Layer node with ID : " << output_edge->consumer_id() << std::endl);
146
147 // Prevent fusion if fused node has an output accessor
148 if(n_node->output(0)->accessor() == nullptr)
149 {
150 // Get driving nodes of activation node
151 std::vector<NodeIdxPair> act_driving_nodes = get_driving_nodes(*act_node);
152
153 // Set activation info to fused node
154 n_node->set_fused_activation(act_node->activation_info());
155
156 // Extract activation node accessor if any
157 auto act_node_accessor = act_node->output(0)->extract_accessor();
158
159 // Remove activation node
160 g.remove_node(act_node->id());
161
162 // Update fused node outputs
163 for(auto &driving_node : act_driving_nodes)
164 {
165 g.add_connection(n_node->id(), 0, driving_node.node_id, driving_node.index);
166 }
167
168 // Update accessor to fused node
169 n_node->output(0)->set_accessor(std::move(act_node_accessor));
170 }
171 else
172 {
173 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented fusion of node with activation due to the presence of an output accessor\n");
174 }
175}
176
177template <typename N1, typename N2, typename F, typename... Args>
178void fuse_layer(Graph &g, std::function<bool(INode &)> const &prec, const F fuse_fcn, Args &&... optional_arguments)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000179{
180 // Not interested in the order of nodes
181 for(auto &node : g.nodes())
182 {
Georgios Pinitas60e98252018-10-22 16:17:20 +0100183 // Check if the node is of type N and not a branching node
giuros01acce5042019-02-21 17:32:34 +0000184 if(node && node->type() == N1::node_type && node->output_edges().size() == 1)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000185 {
giuros01acce5042019-02-21 17:32:34 +0000186 const auto output_edge_id = *node->output_edges().begin();
187 const auto output_edge = g.edge(output_edge_id);
188
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000189 // Check if following node is an activation layer node
giuros01acce5042019-02-21 17:32:34 +0000190 if((output_edge != nullptr) && (output_edge->consumer() != nullptr) && (output_edge->consumer()->type() == N2::node_type) && prec(*output_edge->producer()))
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000191 {
giuros01acce5042019-02-21 17:32:34 +0000192 fuse_fcn(g, output_edge, optional_arguments...);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000193 }
194 }
195 }
196}
197} // namespace detail
198
199const char *NodeFusionMutator::name()
200{
201 return "NodeFusionMutator";
202}
203
204void NodeFusionMutator::mutate(Graph &g)
205{
Georgios Pinitas08346e92018-10-16 19:10:46 +0100206 // Supported activations when fusing
207 const std::set<Activation> supported_fused_activations = { Activation::RELU, Activation::BOUNDED_RELU, Activation::LU_BOUNDED_RELU };
208
Georgios Pinitas60e98252018-10-22 16:17:20 +0100209 // Preconditions
210 auto empty_prec = [](INode & n)
211 {
212 return true;
213 };
214 auto qs8_prec = [](INode & n)
215 {
216 ARM_COMPUTE_ERROR_ON(n.output(0) == nullptr);
217 return n.output(0)->desc().data_type == DataType::QASYMM8;
218 };
219
220 // Fusion mutations
giuros01acce5042019-02-21 17:32:34 +0000221 detail::fuse_layer<BatchNormalizationLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<BatchNormalizationLayerNode>, supported_fused_activations);
222 detail::fuse_layer<ConvolutionLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<ConvolutionLayerNode>, supported_fused_activations);
223 detail::fuse_layer<DepthwiseConvolutionLayerNode, ActivationLayerNode>(g, qs8_prec, detail::fuse_node_with_activation<DepthwiseConvolutionLayerNode>, supported_fused_activations);
giuros01749021a2019-03-14 16:19:41 +0000224
225 // TODO (COMPMID-2055): re-enable once we fuse bias and activations to convolution
226 // detail::fuse_layer<ConvolutionLayerNode, BatchNormalizationLayerNode>(g, empty_prec, detail::fuse_convolution_with_batch_normalization);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000227}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100228} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000229} // namespace arm_compute