Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
SiCongLi | bc91297 | 2021-05-25 14:29:21 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/mutators/NodeFusionMutator.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 26 | #include "arm_compute/graph/GraphBuilder.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/Logger.h" |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/Utils.h" |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 29 | #include "arm_compute/graph/backends/BackendRegistry.h" |
| 30 | #include "arm_compute/graph/nodes/FusedConvolutionBatchNormalizationNode.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 31 | #include "arm_compute/graph/nodes/Nodes.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 32 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "support/Cast.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 34 | |
Georgios Pinitas | 6f109bd | 2018-07-16 12:57:42 +0100 | [diff] [blame] | 35 | #include <set> |
| 36 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 39 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 40 | { |
| 41 | namespace detail |
| 42 | { |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 43 | void 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(); |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 67 | 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 |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 74 | 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(); |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 78 | |
| 79 | // Create the fused node |
Manuel Bottini | bffb41e | 2019-06-20 16:00:27 +0100 | [diff] [blame] | 80 | const NodeID fused_id = g.add_node<FusedConvolutionBatchNormalizationNode>(epsilon, conv_info, num_groups, conv_method, fast_math_hint, act_info); |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 81 | |
| 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); |
giuros01 | 351bd13 | 2019-08-23 14:27:30 +0100 | [diff] [blame] | 93 | |
| 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 | } |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 105 | |
| 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 Bottini | bffb41e | 2019-06-20 16:00:27 +0100 | [diff] [blame] | 136 | void 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 Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 213 | template <typename N> |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 214 | void 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 Zhang | 16dddd2 | 2020-05-27 15:03:48 +0100 | [diff] [blame] | 229 | // 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 | |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 235 | 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 | |
Gunes Bayir | 814bddf | 2021-09-01 16:20:54 +0100 | [diff] [blame^] | 268 | bool check_padding_info(const DataLayout &layout, const PaddingList &padding_list, PaddingInfo &pad_w, PaddingInfo &pad_h) |
| 269 | { |
| 270 | if(layout == DataLayout::NCHW || layout == DataLayout::NHWC) |
| 271 | { |
| 272 | const PaddingInfo zero_padding(0, 0); |
| 273 | |
| 274 | const unsigned int height_index = get_dimension_idx(layout, DataLayoutDimension::HEIGHT); |
| 275 | const unsigned int width_index = get_dimension_idx(layout, DataLayoutDimension::WIDTH); |
| 276 | |
| 277 | pad_w = width_index < padding_list.size() ? padding_list[width_index] : zero_padding; |
| 278 | pad_h = height_index < padding_list.size() ? padding_list[height_index] : zero_padding; |
| 279 | |
| 280 | for(unsigned int i = 0; i < padding_list.size(); i++) |
| 281 | { |
| 282 | if(i != height_index && i != width_index && padding_list[i] != zero_padding) |
| 283 | { |
| 284 | // if the index is not either height or width, don't fuse |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | template <typename N> |
| 296 | void fuse_pad_with_convolution(Graph &g, const Edge *output_edge) |
| 297 | { |
| 298 | auto *pad_node = arm_compute::utils::cast::polymorphic_downcast<PadLayerNode *>(output_edge->producer()); |
| 299 | auto *conv_node = arm_compute::utils::cast::polymorphic_downcast<N *>(output_edge->consumer()); |
| 300 | |
| 301 | const Edge *input_edge = pad_node->input_edge(0); |
| 302 | if(input_edge != nullptr && input_edge->tensor() != nullptr && pad_node->output(0)->accessor() == nullptr |
| 303 | && pad_node->pad_value().get<float>() == 0.0) |
| 304 | { |
| 305 | const DataLayout layout = input_edge->tensor()->desc().layout; |
| 306 | const PaddingList padding_list = pad_node->padding(); |
| 307 | PaddingInfo pad_w, pad_h; |
| 308 | |
| 309 | if(check_padding_info(layout, padding_list, pad_w, pad_h)) |
| 310 | { |
| 311 | // Add paddings to the convolution node |
| 312 | const PadStrideInfo conv_info = conv_node->convolution_info(); |
| 313 | const PadStrideInfo new_conv_info( |
| 314 | conv_info.stride().first, |
| 315 | conv_info.stride().second, |
| 316 | conv_info.pad_left() + pad_w.first, |
| 317 | conv_info.pad_right() + pad_w.second, |
| 318 | conv_info.pad_top() + pad_h.first, |
| 319 | conv_info.pad_bottom() + pad_h.second, |
| 320 | conv_info.round()); |
| 321 | conv_node->set_convolution_info(new_conv_info); |
| 322 | |
| 323 | // Update drivers of the convolution node |
| 324 | std::vector<NodeIdxPair> pad_driver_nodes = get_driver_nodes(*pad_node); |
| 325 | g.remove_node(pad_node->id()); |
| 326 | |
| 327 | // Update fused node inputs |
| 328 | for(auto &driver_node : pad_driver_nodes) |
| 329 | { |
| 330 | g.add_connection(driver_node.node_id, driver_node.index, conv_node->id(), 0); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 336 | template <typename N1, typename N2, typename F, typename... Args> |
| 337 | void fuse_layer(Graph &g, std::function<bool(INode &)> const &prec, const F fuse_fcn, Args &&... optional_arguments) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 338 | { |
SiCongLi | bc91297 | 2021-05-25 14:29:21 +0100 | [diff] [blame] | 339 | // Note that fused nodes may be added to the end of the node list. |
| 340 | // Instead of only looping over the original list of nodes, we loop over the current node list which could be growing. |
| 341 | // This is intentional as it probes the newly added fused nodes for further fusing opportunities. |
| 342 | for(unsigned int i = 0; i < g.nodes().size(); ++i) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 343 | { |
SiCongLi | bc91297 | 2021-05-25 14:29:21 +0100 | [diff] [blame] | 344 | auto node = g.node(i); |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 345 | // Check if the node is of type N and not a branching node |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 346 | if(node && node->type() == N1::node_type && node->output_edges().size() == 1) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 347 | { |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 348 | const auto output_edge_id = *node->output_edges().begin(); |
| 349 | const auto output_edge = g.edge(output_edge_id); |
| 350 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 351 | // Check if following node is an activation layer node |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 352 | if((output_edge != nullptr) && (output_edge->consumer() != nullptr) && (output_edge->consumer()->type() == N2::node_type) && prec(*output_edge->producer())) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 353 | { |
giuros01 | acce504 | 2019-02-21 17:32:34 +0000 | [diff] [blame] | 354 | fuse_fcn(g, output_edge, optional_arguments...); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } // namespace detail |
| 360 | |
| 361 | const char *NodeFusionMutator::name() |
| 362 | { |
| 363 | return "NodeFusionMutator"; |
| 364 | } |
| 365 | |
Georgios Pinitas | f4261ad | 2019-12-02 11:58:19 +0000 | [diff] [blame] | 366 | IGraphMutator::MutationType NodeFusionMutator::type() const |
| 367 | { |
| 368 | return IGraphMutator::MutationType::Backend; |
| 369 | } |
| 370 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 371 | void NodeFusionMutator::mutate(Graph &g) |
| 372 | { |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 373 | // Supported activations when fusing |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 374 | const std::set<Activation> supported_fused_activations = { Activation::ABS, Activation::BOUNDED_RELU, Activation::ELU, |
| 375 | Activation::HARD_SWISH, Activation::IDENTITY, Activation::LEAKY_RELU, |
| 376 | Activation::LINEAR, Activation::LOGISTIC, Activation::LU_BOUNDED_RELU, |
| 377 | Activation::RELU, Activation::SOFT_RELU, Activation::SQRT, |
| 378 | Activation::SQUARE, Activation::TANH |
| 379 | }; |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 380 | |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 381 | // Preconditions |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 382 | auto empty_prec = [](INode &) |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 383 | { |
| 384 | return true; |
| 385 | }; |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 386 | auto cl_target_prec = [](INode & n) |
| 387 | { |
| 388 | return n.assigned_target() == Target::CL; |
| 389 | }; |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 390 | auto qs8_prec = [&g](INode & n) |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 391 | { |
| 392 | ARM_COMPUTE_ERROR_ON(n.output(0) == nullptr); |
Isabella Gottardi | 0ae5de9 | 2019-03-14 10:32:11 +0000 | [diff] [blame] | 393 | |
| 394 | const auto output_edge_id = *n.output_edges().begin(); |
| 395 | const auto output_edge = g.edge(output_edge_id); |
| 396 | // To perform fusion the two nodes must have same output quantization information |
| 397 | const bool same_qinfo = n.output(0)->desc().quant_info == output_edge->producer()->output(0)->desc().quant_info; |
| 398 | const bool output_qasymm8 = n.output(0)->desc().data_type == DataType::QASYMM8; |
| 399 | |
Georgios Pinitas | cadb368 | 2019-03-29 10:54:36 +0000 | [diff] [blame] | 400 | return (output_qasymm8 && same_qinfo) || !output_qasymm8; |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | // Fusion mutations |
Gunes Bayir | 814bddf | 2021-09-01 16:20:54 +0100 | [diff] [blame^] | 404 | detail::fuse_layer<PadLayerNode, ConvolutionLayerNode>(g, empty_prec, detail::fuse_pad_with_convolution<ConvolutionLayerNode>); |
| 405 | detail::fuse_layer<PadLayerNode, DepthwiseConvolutionLayerNode>(g, empty_prec, detail::fuse_pad_with_convolution<DepthwiseConvolutionLayerNode>); |
Gian Marco Iodice | 047c6fc | 2020-09-21 14:22:25 +0100 | [diff] [blame] | 406 | detail::fuse_layer<BatchNormalizationLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<BatchNormalizationLayerNode>, supported_fused_activations); |
| 407 | detail::fuse_layer<ConvolutionLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<ConvolutionLayerNode>, supported_fused_activations); |
| 408 | detail::fuse_layer<DepthwiseConvolutionLayerNode, ActivationLayerNode>(g, qs8_prec, detail::fuse_node_with_activation<DepthwiseConvolutionLayerNode>, supported_fused_activations); |
| 409 | detail::fuse_layer<FullyConnectedLayerNode, ActivationLayerNode>(g, empty_prec, detail::fuse_node_with_activation<FullyConnectedLayerNode>, supported_fused_activations); |
| 410 | detail::fuse_layer<EltwiseLayerNode, ActivationLayerNode>(g, cl_target_prec, detail::fuse_node_with_activation<EltwiseLayerNode>, supported_fused_activations); |
Gian Marco Iodice | 5dea19e | 2019-11-08 12:13:48 +0000 | [diff] [blame] | 411 | detail::fuse_layer<ConvolutionLayerNode, BatchNormalizationLayerNode>(g, empty_prec, detail::fuse_convolution_with_batch_normalization); |
| 412 | detail::fuse_layer<DepthwiseConvolutionLayerNode, BatchNormalizationLayerNode>(g, empty_prec, detail::fuse_depthwise_convolution_with_batch_normalization); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 413 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 414 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 415 | } // namespace arm_compute |