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/InPlaceOperationMutator.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 | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 28 | |
| 29 | namespace arm_compute |
| 30 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 31 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 32 | { |
| 33 | const char *InPlaceOperationMutator::name() |
| 34 | { |
| 35 | return "InPlaceOperationMutator"; |
| 36 | } |
| 37 | |
| 38 | void InPlaceOperationMutator::mutate(Graph &g) |
| 39 | { |
| 40 | std::set<NodeType> in_place_nodes = { NodeType::BatchNormalizationLayer, NodeType::ActivationLayer }; |
| 41 | |
| 42 | // Not interested in the order of nodes |
| 43 | for(auto &node : g.nodes()) |
| 44 | { |
| 45 | if(node && in_place_nodes.find(node->type()) != std::end(in_place_nodes)) |
| 46 | { |
| 47 | // Get input edge |
| 48 | Edge *input_edge = node->input_edge(0); |
| 49 | |
| 50 | // Check if parent has a single output if yes then force in place calculation else not |
| 51 | if((input_edge != nullptr) && (input_edge->producer() != nullptr) && (input_edge->producer()->output_edges().size() == 1)) |
| 52 | { |
Georgios Pinitas | d3a78ab | 2018-06-18 15:35:09 +0100 | [diff] [blame] | 53 | // Get current and new output tensors |
| 54 | auto current_output_tensor = node->output(0); |
| 55 | auto new_output_tensor = input_edge->tensor(); |
| 56 | |
| 57 | ARM_COMPUTE_ERROR_ON(current_output_tensor == nullptr || new_output_tensor == nullptr); |
| 58 | |
| 59 | // Prevent in-place operation if there is an accessor bound to the in-place tensor |
| 60 | if(new_output_tensor->accessor() == nullptr) |
| 61 | { |
| 62 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Switching to in-place computation for the node with ID : " |
| 63 | << node->id() << " and name : " << node->name() << std::endl); |
| 64 | // Update accessor |
| 65 | new_output_tensor->set_accessor(current_output_tensor->extract_accessor()); |
| 66 | // Update output |
| 67 | node->set_output_tensor(new_output_tensor->id(), 0); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Prevented in-place operation as there is an accessor bound to the input tensor\n"); |
| 72 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 77 | } // namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 78 | } // namespace arm_compute |