blob: 3b06537cd9357e42e0d228ac0f65fe67542611cd [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Giorgio Arena6e9d0e02020-01-03 15:02:04 +00002 * 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/InPlaceOperationMutator.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/graph/Graph.h"
27#include "arm_compute/graph/Logger.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000028
29namespace arm_compute
30{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000032{
33const char *InPlaceOperationMutator::name()
34{
35 return "InPlaceOperationMutator";
36}
37
Georgios Pinitasf4261ad2019-12-02 11:58:19 +000038IGraphMutator::MutationType InPlaceOperationMutator::type() const
39{
40 return IGraphMutator::MutationType::Backend;
41}
42
Georgios Pinitasd8734b52017-12-22 15:27:52 +000043void InPlaceOperationMutator::mutate(Graph &g)
44{
Giorgio Arena6e9d0e02020-01-03 15:02:04 +000045 std::set<NodeType> in_place_nodes = { NodeType::BatchNormalizationLayer, NodeType::ActivationLayer, NodeType::PrintLayer };
Georgios Pinitasd8734b52017-12-22 15:27:52 +000046
47 // Not interested in the order of nodes
48 for(auto &node : g.nodes())
49 {
50 if(node && in_place_nodes.find(node->type()) != std::end(in_place_nodes))
51 {
52 // Get input edge
53 Edge *input_edge = node->input_edge(0);
54
55 // Check if parent has a single output if yes then force in place calculation else not
56 if((input_edge != nullptr) && (input_edge->producer() != nullptr) && (input_edge->producer()->output_edges().size() == 1))
57 {
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010058 // Get current and new output tensors
59 auto current_output_tensor = node->output(0);
60 auto new_output_tensor = input_edge->tensor();
61
62 ARM_COMPUTE_ERROR_ON(current_output_tensor == nullptr || new_output_tensor == nullptr);
63
Isabella Gottardi0ae5de92019-03-14 10:32:11 +000064 // Prevent in-place operation if there is an accessor bound to the in-place tensor or quantization info are different
Isabella Gottardi2ea37612019-07-16 11:48:51 +010065 if(new_output_tensor->accessor() != nullptr || current_output_tensor->desc().quant_info != new_output_tensor->desc().quant_info)
66 {
67 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");
68 }
69 else
Georgios Pinitasd3a78ab2018-06-18 15:35:09 +010070 {
71 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Switching to in-place computation for the node with ID : "
72 << node->id() << " and name : " << node->name() << std::endl);
73 // Update accessor
74 new_output_tensor->set_accessor(current_output_tensor->extract_accessor());
75 // Update output
76 node->set_output_tensor(new_output_tensor->id(), 0);
77 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +000078 }
79 }
80 }
81}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010082} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000083} // namespace arm_compute