Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | 1d35927 | 2021-06-10 13:56:11 +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/InPlaceOperationMutator.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
Sheri Zhang | 1d35927 | 2021-06-10 13:56:11 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/Graph.h" |
| 28 | #include "arm_compute/graph/Logger.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 33 | { |
Michele Di Giorgio | 294f6ff | 2020-06-19 12:11:06 +0100 | [diff] [blame] | 34 | namespace |
| 35 | { |
| 36 | // Check if the output edges of the parent node are separate tensors. If not, |
| 37 | // it means the same output is connected to multiple nodes and computations on |
| 38 | // these nodes cannot be done in-place. |
| 39 | bool output_edges_are_separate_tensors(Graph &g, const Edge *input_edge) |
| 40 | { |
| 41 | const auto parent_node = input_edge->producer(); |
| 42 | const auto input_tensor = input_edge->tensor(); |
| 43 | const auto input_edge_id = input_edge->id(); |
| 44 | |
| 45 | if(parent_node == nullptr) |
| 46 | { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | const auto output_edges = parent_node->output_edges(); |
| 51 | |
| 52 | // If the output is connected to only one edge, then computations can |
| 53 | // be done in-place. |
| 54 | if(output_edges.size() == 1) |
| 55 | { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | return std::all_of(output_edges.begin(), |
| 60 | output_edges.end(), |
| 61 | [&](const EdgeID & edge_id) |
| 62 | { |
| 63 | // Skip check on current input edge |
| 64 | if(edge_id == input_edge_id) |
| 65 | { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | auto edge = g.edge(edge_id); |
| 70 | return edge->tensor() != input_tensor; |
| 71 | }); |
| 72 | } |
Sheri Zhang | 1d35927 | 2021-06-10 13:56:11 +0100 | [diff] [blame] | 73 | |
| 74 | // If do in-place calculation, then need to use the new output and inherit original output's accessor |
| 75 | void set_new_output_and_inherit_accessor(std::unique_ptr<INode> &node, Tensor *orig_output, Tensor *new_output) |
| 76 | { |
| 77 | ARM_COMPUTE_LOG_GRAPH_INFO("Switching to in-place computation for the node with ID : " |
| 78 | << node->id() << " and name : " << node->name() << std::endl); |
| 79 | // Update accessor |
| 80 | new_output->set_accessor(orig_output->extract_accessor()); |
| 81 | // Update output |
| 82 | node->set_output_tensor(new_output->id(), 0); |
| 83 | } |
| 84 | |
| 85 | // Try to mutate the node to perform the elementwise in-place calculation |
| 86 | void try_in_place_elementwise(std::unique_ptr<INode> &node) |
| 87 | { |
| 88 | // Get input edge |
| 89 | Edge *input0_edge = node->input_edge(0); |
| 90 | Edge *input1_edge = node->input_edge(1); |
| 91 | ARM_COMPUTE_ERROR_ON(input0_edge == nullptr || input1_edge == nullptr); |
| 92 | |
| 93 | auto input0_tensor = input0_edge->tensor(); |
| 94 | auto input1_tensor = input1_edge->tensor(); |
| 95 | ARM_COMPUTE_ERROR_ON(input0_tensor == nullptr || input1_tensor == nullptr); |
| 96 | |
| 97 | const auto shape0 = input0_tensor->desc().shape; |
| 98 | const auto shape1 = input1_tensor->desc().shape; |
| 99 | const auto qinfo0 = input0_tensor->desc().quant_info; |
| 100 | const auto qinfo1 = input1_tensor->desc().quant_info; |
| 101 | |
| 102 | const TensorShape out_shape = TensorShape::broadcast_shape(shape0, shape1); |
| 103 | // Inputs are not broadcast compatible |
| 104 | if(out_shape.total_size() == 0) |
| 105 | { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | // Get current output tensor |
| 110 | auto current_output_tensor = node->output(0); |
| 111 | ARM_COMPUTE_ERROR_ON(current_output_tensor == nullptr); |
| 112 | const auto qinfo_out = current_output_tensor->desc().quant_info; |
| 113 | |
| 114 | // Can do in place, if the input has same shape as output, has same quntisation info as output, and input doesn't have accessor. |
| 115 | bool input0_can_in_place = !arm_compute::detail::have_different_dimensions(out_shape, shape0, 0) && (qinfo0 == qinfo_out) && (input0_tensor->accessor() == nullptr); |
| 116 | bool input1_can_in_place = !arm_compute::detail::have_different_dimensions(out_shape, shape1, 0) && (qinfo1 == qinfo_out) && (input1_tensor->accessor() == nullptr); |
| 117 | |
| 118 | if(input0_can_in_place) |
| 119 | { |
| 120 | set_new_output_and_inherit_accessor(node, current_output_tensor, input0_tensor); |
| 121 | } |
| 122 | else if(input1_can_in_place) |
| 123 | { |
| 124 | set_new_output_and_inherit_accessor(node, current_output_tensor, input1_tensor); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented in-place operation as there is an accessor bound to the input tensor or the quantization info are different.\n"); |
| 129 | } |
| 130 | } |
Michele Di Giorgio | 294f6ff | 2020-06-19 12:11:06 +0100 | [diff] [blame] | 131 | } // namespace |
| 132 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 133 | const char *InPlaceOperationMutator::name() |
| 134 | { |
| 135 | return "InPlaceOperationMutator"; |
| 136 | } |
| 137 | |
Georgios Pinitas | f4261ad | 2019-12-02 11:58:19 +0000 | [diff] [blame] | 138 | IGraphMutator::MutationType InPlaceOperationMutator::type() const |
| 139 | { |
| 140 | return IGraphMutator::MutationType::Backend; |
| 141 | } |
| 142 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 143 | void InPlaceOperationMutator::mutate(Graph &g) |
| 144 | { |
Michele Di Giorgio | 3be0b8c | 2020-06-18 15:28:54 +0100 | [diff] [blame] | 145 | std::set<NodeType> in_place_nodes = |
| 146 | { |
| 147 | NodeType::ActivationLayer, |
| 148 | NodeType::BatchNormalizationLayer, |
| 149 | NodeType::EltwiseLayer, |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 150 | NodeType::UnaryEltwiseLayer, |
Michele Di Giorgio | 3be0b8c | 2020-06-18 15:28:54 +0100 | [diff] [blame] | 151 | NodeType::PrintLayer |
| 152 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 153 | |
| 154 | // Not interested in the order of nodes |
| 155 | for(auto &node : g.nodes()) |
| 156 | { |
| 157 | if(node && in_place_nodes.find(node->type()) != std::end(in_place_nodes)) |
| 158 | { |
| 159 | // Get input edge |
| 160 | Edge *input_edge = node->input_edge(0); |
| 161 | |
| 162 | // Check if parent has a single output if yes then force in place calculation else not |
Michele Di Giorgio | 294f6ff | 2020-06-19 12:11:06 +0100 | [diff] [blame] | 163 | if((input_edge != nullptr) && output_edges_are_separate_tensors(g, input_edge)) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 164 | { |
Sheri Zhang | 1d35927 | 2021-06-10 13:56:11 +0100 | [diff] [blame] | 165 | if(node->type() == NodeType::EltwiseLayer) |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 166 | { |
Sheri Zhang | 1d35927 | 2021-06-10 13:56:11 +0100 | [diff] [blame] | 167 | try_in_place_elementwise(node); |
Isabella Gottardi | 2ea3761 | 2019-07-16 11:48:51 +0100 | [diff] [blame] | 168 | } |
| 169 | else |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 170 | { |
Sheri Zhang | 1d35927 | 2021-06-10 13:56:11 +0100 | [diff] [blame] | 171 | // Get current and new output tensors |
| 172 | auto current_output_tensor = node->output(0); |
| 173 | auto new_output_tensor = input_edge->tensor(); |
| 174 | |
| 175 | ARM_COMPUTE_ERROR_ON(current_output_tensor == nullptr || new_output_tensor == nullptr); |
| 176 | |
| 177 | // Prevent in-place operation if there is an accessor bound to the in-place tensor or quantization info are different |
| 178 | if(new_output_tensor->accessor() != nullptr || current_output_tensor->desc().quant_info != new_output_tensor->desc().quant_info) |
| 179 | { |
| 180 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented in-place operation as there is an accessor bound to the input tensor or the quantization info are different.\n"); |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | set_new_output_and_inherit_accessor(node, current_output_tensor, new_output_tensor); |
| 185 | } |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 186 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 191 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 192 | } // namespace arm_compute |