blob: afc4452202e645dfa22c3c88ada1e18ee8b53a13 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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();
giuros01acce5042019-02-21 17:32:34 +000067 const auto conv_info = conv_node->convolution_info();
68 const auto conv_method = conv_node->convolution_method();
69 const auto num_groups = conv_node->num_groups();
70 const auto act_info = bn_node->fused_activation();
71 FastMathHint fast_math_hint = conv_node->fast_math_hint();
72
73 // Extract bn inputs
giuros01351bd132019-08-23 14:27:30 +010074 const auto bn_mean_id = bn_node->input_edge(1)->producer_id();
75 const auto bn_var_id = bn_node->input_edge(2)->producer_id();
76
77 const auto epsilon = bn_node->epsilon();
giuros01acce5042019-02-21 17:32:34 +000078
79 // Create the fused node
Manuel Bottinibffb41e2019-06-20 16:00:27 +010080 const NodeID fused_id = g.add_node<FusedConvolutionBatchNormalizationNode>(epsilon, conv_info, num_groups, conv_method, fast_math_hint, act_info);
giuros01acce5042019-02-21 17:32:34 +000081
82 if(conv_node->input_edge(2) != nullptr)
83 {
84 auto conv_bias_id = conv_node->input_edge(2)->producer_id();
85 g.add_connection(conv_bias_id, 0, fused_id, 2);
86 }
87
88 // Add connections from the conv/batch_norm inputs to the fused node
89 g.add_connection(conv_input_id, 0, fused_id, 0);
90 g.add_connection(conv_weights_id, 0, fused_id, 1);
91 g.add_connection(bn_mean_id, 0, fused_id, 3);
92 g.add_connection(bn_var_id, 0, fused_id, 4);
giuros01351bd132019-08-23 14:27:30 +010093
94 if(bn_node->input_edge(3) != nullptr)
95 {
96 const auto bn_beta_id = bn_node->input_edge(3)->producer_id();
97 g.add_connection(bn_beta_id, 0, fused_id, 5);
98 }
99
100 if(bn_node->input_edge(4) != nullptr)
101 {
102 const auto bn_gamma_id = bn_node->input_edge(4)->producer_id();
103 g.add_connection(bn_gamma_id, 0, fused_id, 6);
104 }
giuros01acce5042019-02-21 17:32:34 +0000105
106 auto fused_node = g.node(fused_id);
107 std::vector<NodeIdxPair> bn_driving_nodes = get_driving_nodes(*bn_node);
108
109 // Extract batch normalization node accessor if any
110 auto bn_node_accessor = bn_node->output(0)->extract_accessor();
111 auto bn_node_name = bn_node->name();
112
113 // Remove batch normalization node
114 g.remove_node(bn_node->id());
115
116 // Get driving nodes of batch normalization node
117 for(auto &driving_node : bn_driving_nodes)
118 {
119 g.add_connection(fused_id, 0, driving_node.node_id, driving_node.index);
120 configure_tensor(fused_node->output(0));
121 }
122 // Update fused node outputs
123 fused_node->output(0)->set_accessor(std::move(bn_node_accessor));
124 fused_node->set_assigned_target(assigned_target);
125 fused_node->set_common_node_parameters(NodeParams{ conv_node->name() + "+" + bn_node_name, assigned_target });
126
127 // Remove convolution node
128 g.remove_node(conv_node->id());
129 }
130 else
131 {
132 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented fusion of convolution with batch normalization due to the presence of an output accessor\n");
133 }
134}
135
Manuel Bottinibffb41e2019-06-20 16:00:27 +0100136void fuse_depthwise_convolution_with_batch_normalization(Graph &g, const Edge *output_edge)
137{
138 ARM_COMPUTE_ERROR_ON(output_edge == nullptr);
139
140 auto *depth_conv_node = arm_compute::utils::cast::polymorphic_downcast<DepthwiseConvolutionLayerNode *>(output_edge->producer());
141 auto *bn_node = arm_compute::utils::cast::polymorphic_downcast<BatchNormalizationLayerNode *>(output_edge->consumer());
142
143 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Fusing depthwise convolution node with ID : " << output_edge->producer_id()
144 << " with BatchNormalization Layer node with ID : " << output_edge->consumer_id() << std::endl);
145
146 // Prevent fusion if fused node has an output accessor
147 if(depth_conv_node->output(0)->accessor() == nullptr)
148 {
149 const Target assigned_target = depth_conv_node->assigned_target();
150
151 // Extract conv inputs
152 const auto depth_conv_input_id = depth_conv_node->input_edge(0)->producer_id();
153 const auto conv_weights_id = depth_conv_node->input_edge(1)->producer_id();
154 const auto conv_info = depth_conv_node->convolution_info();
155 const auto depth_conv_method = depth_conv_node->depthwise_convolution_method();
156 const auto depth_multiplier = depth_conv_node->depth_multiplier();
157 const auto act_info = bn_node->fused_activation();
158
159 // Extract bn inputs
160 const auto bn_mean_id = bn_node->input_edge(1)->producer_id();
161 const auto bn_var_id = bn_node->input_edge(2)->producer_id();
162 const auto bn_beta_id = bn_node->input_edge(3)->producer_id();
163 const auto bn_gamma_id = bn_node->input_edge(4)->producer_id();
164 const auto epsilon = bn_node->epsilon();
165
166 // Create the fused node
167 const NodeID fused_id = g.add_node<FusedDepthwiseConvolutionBatchNormalizationNode>(epsilon, conv_info, depth_multiplier, depth_conv_method, act_info);
168
169 if(depth_conv_node->input_edge(2) != nullptr)
170 {
171 const auto conv_bias_id = depth_conv_node->input_edge(2)->producer_id();
172 g.add_connection(conv_bias_id, 0, fused_id, 2);
173 }
174
175 // Add connections from the conv/batch_norm inputs to the fused node
176 g.add_connection(depth_conv_input_id, 0, fused_id, 0);
177 g.add_connection(conv_weights_id, 0, fused_id, 1);
178 g.add_connection(bn_mean_id, 0, fused_id, 3);
179 g.add_connection(bn_var_id, 0, fused_id, 4);
180 g.add_connection(bn_beta_id, 0, fused_id, 5);
181 g.add_connection(bn_gamma_id, 0, fused_id, 6);
182
183 auto fused_node = g.node(fused_id);
184 std::vector<NodeIdxPair> bn_driving_nodes = get_driving_nodes(*bn_node);
185
186 // Extract batch normalization node accessor if any
187 auto bn_node_accessor = bn_node->output(0)->extract_accessor();
188 auto bn_node_name = bn_node->name();
189
190 // Remove batch normalization node
191 g.remove_node(bn_node->id());
192
193 // Get driving nodes of batch normalization node
194 for(auto &driving_node : bn_driving_nodes)
195 {
196 g.add_connection(fused_id, 0, driving_node.node_id, driving_node.index);
197 configure_tensor(fused_node->output(0));
198 }
199 // Update fused node outputs
200 fused_node->output(0)->set_accessor(std::move(bn_node_accessor));
201 fused_node->set_assigned_target(assigned_target);
202 fused_node->set_common_node_parameters(NodeParams{ depth_conv_node->name() + "+" + bn_node_name, assigned_target });
203
204 // Remove convolution node
205 g.remove_node(depth_conv_node->id());
206 }
207 else
208 {
209 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented fusion of depthwise convolution with batch normalization due to the presence of an output accessor\n");
210 }
211}
212
Georgios Pinitas08346e92018-10-16 19:10:46 +0100213template <typename N>
giuros01acce5042019-02-21 17:32:34 +0000214void fuse_node_with_activation(Graph &g, const Edge *output_edge, const std::set<Activation> &supported_fused_activations)
215{
216 ARM_COMPUTE_ERROR_ON(output_edge == nullptr);
217
218 auto *n_node = arm_compute::utils::cast::polymorphic_downcast<N *>(output_edge->producer());
219 auto *act_node = arm_compute::utils::cast::polymorphic_downcast<ActivationLayerNode *>(output_edge->consumer());
220
221 ARM_COMPUTE_ERROR_ON(act_node->output(0) == nullptr || n_node->output(0) == nullptr);
222
223 // Check if activation is supported for fusion
224 if(supported_fused_activations.count(act_node->activation_info().activation()) == 0)
225 {
226 return;
227 }
228
Sheri Zhang16dddd22020-05-27 15:03:48 +0100229 // EltwiseLayerNode can only be fused when dataype is float
230 if(n_node->type() == NodeType::EltwiseLayer && !is_data_type_float(n_node->output(0)->desc().data_type))
231 {
232 return;
233 }
234
giuros01acce5042019-02-21 17:32:34 +0000235 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Fusing node with ID : " << output_edge->producer_id()
236 << " with Activation Layer node with ID : " << output_edge->consumer_id() << std::endl);
237
238 // Prevent fusion if fused node has an output accessor
239 if(n_node->output(0)->accessor() == nullptr)
240 {
241 // Get driving nodes of activation node
242 std::vector<NodeIdxPair> act_driving_nodes = get_driving_nodes(*act_node);
243
244 // Set activation info to fused node
245 n_node->set_fused_activation(act_node->activation_info());
246
247 // Extract activation node accessor if any
248 auto act_node_accessor = act_node->output(0)->extract_accessor();
249
250 // Remove activation node
251 g.remove_node(act_node->id());
252
253 // Update fused node outputs
254 for(auto &driving_node : act_driving_nodes)
255 {
256 g.add_connection(n_node->id(), 0, driving_node.node_id, driving_node.index);
257 }
258
259 // Update accessor to fused node
260 n_node->output(0)->set_accessor(std::move(act_node_accessor));
261 }
262 else
263 {
264 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented fusion of node with activation due to the presence of an output accessor\n");
265 }
266}
267
268template <typename N1, typename N2, typename F, typename... Args>
269void fuse_layer(Graph &g, std::function<bool(INode &)> const &prec, const F fuse_fcn, Args &&... optional_arguments)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000270{
271 // Not interested in the order of nodes
272 for(auto &node : g.nodes())
273 {
Georgios Pinitas60e98252018-10-22 16:17:20 +0100274 // Check if the node is of type N and not a branching node
giuros01acce5042019-02-21 17:32:34 +0000275 if(node && node->type() == N1::node_type && node->output_edges().size() == 1)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000276 {
giuros01acce5042019-02-21 17:32:34 +0000277 const auto output_edge_id = *node->output_edges().begin();
278 const auto output_edge = g.edge(output_edge_id);
279
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000280 // Check if following node is an activation layer node
giuros01acce5042019-02-21 17:32:34 +0000281 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 +0000282 {
giuros01acce5042019-02-21 17:32:34 +0000283 fuse_fcn(g, output_edge, optional_arguments...);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000284 }
285 }
286 }
287}
288} // namespace detail
289
290const char *NodeFusionMutator::name()
291{
292 return "NodeFusionMutator";
293}
294
Georgios Pinitasf4261ad2019-12-02 11:58:19 +0000295IGraphMutator::MutationType NodeFusionMutator::type() const
296{
297 return IGraphMutator::MutationType::Backend;
298}
299
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000300void NodeFusionMutator::mutate(Graph &g)
301{
Georgios Pinitas08346e92018-10-16 19:10:46 +0100302 // Supported activations when fusing
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000303 const std::set<Activation> supported_fused_activations_conv = { Activation::RELU, Activation::BOUNDED_RELU, Activation::LU_BOUNDED_RELU };
304 const std::set<Activation> supported_fused_activations_eltwise = { Activation::RELU, Activation::BOUNDED_RELU, Activation::LU_BOUNDED_RELU,
305 Activation::TANH, Activation::LOGISTIC
306 };
Georgios Pinitas08346e92018-10-16 19:10:46 +0100307
Georgios Pinitas60e98252018-10-22 16:17:20 +0100308 // Preconditions
Michalis Spyrou299fdd32019-05-01 13:03:59 +0100309 auto empty_prec = [](INode &)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100310 {
311 return true;
312 };
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000313 auto cl_target_prec = [](INode & n)
314 {
315 return n.assigned_target() == Target::CL;
316 };
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000317 auto qs8_prec = [&g](INode & n)
Georgios Pinitas60e98252018-10-22 16:17:20 +0100318 {
319 ARM_COMPUTE_ERROR_ON(n.output(0) == nullptr);
Isabella Gottardi0ae5de92019-03-14 10:32:11 +0000320
321 const auto output_edge_id = *n.output_edges().begin();
322 const auto output_edge = g.edge(output_edge_id);
323 // To perform fusion the two nodes must have same output quantization information
324 const bool same_qinfo = n.output(0)->desc().quant_info == output_edge->producer()->output(0)->desc().quant_info;
325 const bool output_qasymm8 = n.output(0)->desc().data_type == DataType::QASYMM8;
326
Georgios Pinitascadb3682019-03-29 10:54:36 +0000327 return (output_qasymm8 && same_qinfo) || !output_qasymm8;
Georgios Pinitas60e98252018-10-22 16:17:20 +0100328 };
329
330 // Fusion mutations
Giorgio Arena8b2a7d32020-02-11 17:21:31 +0000331 detail::fuse_layer<BatchNormalizationLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<BatchNormalizationLayerNode>, supported_fused_activations_conv);
332 detail::fuse_layer<ConvolutionLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<ConvolutionLayerNode>, supported_fused_activations_conv);
333 detail::fuse_layer<DepthwiseConvolutionLayerNode, ActivationLayerNode>(g, qs8_prec, detail::fuse_node_with_activation<DepthwiseConvolutionLayerNode>, supported_fused_activations_conv);
334 detail::fuse_layer<FullyConnectedLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<FullyConnectedLayerNode>, supported_fused_activations_conv);
335 detail::fuse_layer<EltwiseLayerNode, ActivationLayerNode>(g, cl_target_prec, detail::fuse_node_with_activation<EltwiseLayerNode>, supported_fused_activations_eltwise);
Gian Marco Iodice5dea19e2019-11-08 12:13:48 +0000336 detail::fuse_layer<ConvolutionLayerNode, BatchNormalizationLayerNode>(g, empty_prec, detail::fuse_convolution_with_batch_normalization);
337 detail::fuse_layer<DepthwiseConvolutionLayerNode, BatchNormalizationLayerNode>(g, empty_prec, detail::fuse_depthwise_convolution_with_batch_normalization);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000338}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100339} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000340} // namespace arm_compute