Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 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 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 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 26 | #include "arm_compute/graph/Graph.h" |
| 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" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 29 | #include "arm_compute/graph/nodes/Nodes.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 30 | |
| 31 | #include "arm_compute/core/utils/misc/Cast.h" |
| 32 | |
Georgios Pinitas | 6f109bd | 2018-07-16 12:57:42 +0100 | [diff] [blame] | 33 | #include <set> |
| 34 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 37 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 38 | { |
| 39 | namespace detail |
| 40 | { |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 41 | template <typename N> |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 42 | void fuse_node_with_activation(Graph &g, |
| 43 | const std::set<Activation> &supported_fused_activations, |
| 44 | std::function<bool(INode &)> const &prec) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 45 | { |
| 46 | // Not interested in the order of nodes |
| 47 | for(auto &node : g.nodes()) |
| 48 | { |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 49 | // Check if the node is of type N and not a branching node |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 50 | if(node && node->type() == N::node_type && node->output_edges().size() == 1) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 51 | { |
| 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 Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 57 | auto *n_node = arm_compute::utils::cast::polymorphic_downcast<N *>(output_edge->producer()); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 58 | auto *act_node = arm_compute::utils::cast::polymorphic_downcast<ActivationLayerNode *>(output_edge->consumer()); |
| 59 | |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_ERROR_ON(act_node->output(0) == nullptr || n_node->output(0) == nullptr); |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 61 | |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 62 | // Check given precondition |
| 63 | if(!prec(*n_node)) |
| 64 | { |
| 65 | continue; |
| 66 | } |
Georgios Pinitas | 6f109bd | 2018-07-16 12:57:42 +0100 | [diff] [blame] | 67 | // 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 Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Fusing node with ID : " << output_edge->producer_id() |
Georgios Pinitas | 6f109bd | 2018-07-16 12:57:42 +0100 | [diff] [blame] | 74 | << " with Activation Layer node with ID : " << output_edge->consumer_id() << std::endl); |
| 75 | |
Georgios Pinitas | 1c32bf396 | 2018-11-12 18:36:19 +0000 | [diff] [blame] | 76 | // Prevent fusion if fused node has an output accessor |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 77 | if(n_node->output(0)->accessor() == nullptr) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 78 | { |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 79 | // Get driving nodes of activation node |
Georgios Pinitas | 2a2db59 | 2018-08-15 12:14:46 +0100 | [diff] [blame] | 80 | std::vector<NodeIdxPair> act_driving_nodes = get_driving_nodes(*act_node); |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 81 | |
Georgios Pinitas | 1c32bf396 | 2018-11-12 18:36:19 +0000 | [diff] [blame] | 82 | // Set activation info to fused node |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 83 | n_node->set_fused_activation(act_node->activation_info()); |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 84 | |
| 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 Pinitas | 1c32bf396 | 2018-11-12 18:36:19 +0000 | [diff] [blame] | 91 | // Update fused node outputs |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 92 | for(auto &driving_node : act_driving_nodes) |
| 93 | { |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 94 | g.add_connection(n_node->id(), 0, driving_node.node_id, driving_node.index); |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Georgios Pinitas | 1c32bf396 | 2018-11-12 18:36:19 +0000 | [diff] [blame] | 97 | // Update accessor to fused node |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 98 | n_node->output(0)->set_accessor(std::move(act_node_accessor)); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 99 | } |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 100 | else |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 101 | { |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 102 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented fusion of node with activation due to the presence of an output accessor\n"); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } // namespace detail |
| 109 | |
| 110 | const char *NodeFusionMutator::name() |
| 111 | { |
| 112 | return "NodeFusionMutator"; |
| 113 | } |
| 114 | |
| 115 | void NodeFusionMutator::mutate(Graph &g) |
| 116 | { |
Georgios Pinitas | 08346e9 | 2018-10-16 19:10:46 +0100 | [diff] [blame] | 117 | // Supported activations when fusing |
| 118 | const std::set<Activation> supported_fused_activations = { Activation::RELU, Activation::BOUNDED_RELU, Activation::LU_BOUNDED_RELU }; |
| 119 | |
Georgios Pinitas | 60e9825 | 2018-10-22 16:17:20 +0100 | [diff] [blame] | 120 | // 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 Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 135 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 136 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 137 | } // namespace arm_compute |