Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [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 | */ |
| 24 | #include "arm_compute/graph/SubGraph.h" |
| 25 | |
| 26 | #include "arm_compute/graph/Graph.h" |
| 27 | #include "arm_compute/graph/INode.h" |
| 28 | #include "arm_compute/graph/Tensor.h" |
| 29 | |
| 30 | using namespace arm_compute::graph; |
| 31 | |
| 32 | SubGraph::SubGraph() |
| 33 | : _nodes(), _input(nullptr), _output(nullptr) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void SubGraph::add_node(std::unique_ptr<INode> node) |
| 38 | { |
| 39 | _nodes.push_back(std::move(node)); |
| 40 | } |
| 41 | |
| 42 | void SubGraph::add_tensor_object(std::unique_ptr<ITensorObject> tensor) |
| 43 | { |
| 44 | // If it's the first Tensor added then it will be the input of the Graph. |
| 45 | if(_input == nullptr) |
| 46 | { |
| 47 | _input = std::move(tensor); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | _output = std::move(tensor); |
| 52 | } |
| 53 | } |
| 54 | |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 55 | std::unique_ptr<Graph> SubGraph::construct(const GraphContext &ctx, std::unique_ptr<ITensorObject> input, std::unique_ptr<ITensorObject> output) |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 56 | { |
| 57 | auto graph = arm_compute::support::cpp14::make_unique<Graph>(); |
| 58 | |
| 59 | // Set hint |
| 60 | // TODO(geopin01): store hints of sub-graph |
Georgios Pinitas | 1250a5a | 2018-01-02 13:27:37 +0000 | [diff] [blame] | 61 | graph->hints() = ctx.hints(); |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 62 | |
| 63 | // Configure input |
| 64 | if(_input == nullptr) |
| 65 | { |
| 66 | _input = std::move(input); |
| 67 | } |
| 68 | graph->add_tensor_object(std::move(_input)); |
| 69 | |
Michele Di Giorgio | dde9ec9 | 2018-02-13 15:24:04 +0000 | [diff] [blame] | 70 | // Make sure first and last nodes of the subgraph always do operations out-of-place |
| 71 | _nodes.front()->set_supports_in_place(false); |
| 72 | _nodes.back()->set_supports_in_place(false); |
| 73 | |
Georgios Pinitas | e2c82fe | 2017-10-02 18:51:47 +0100 | [diff] [blame] | 74 | // Construct nodes |
| 75 | for(auto &node : _nodes) |
| 76 | { |
| 77 | graph->add_node(std::move(node)); |
| 78 | } |
| 79 | |
| 80 | // Configure output |
| 81 | if(_output == nullptr) |
| 82 | { |
| 83 | _output = std::move(output); |
| 84 | } |
| 85 | graph->add_tensor_object(std::move(_output)); |
| 86 | |
| 87 | return graph; |
| 88 | } |
| 89 | |
| 90 | bool SubGraph::has_input() const |
| 91 | { |
| 92 | return _input != nullptr; |
| 93 | } |
| 94 | |
| 95 | bool SubGraph::has_output() const |
| 96 | { |
| 97 | return _output != nullptr; |
| 98 | } |
| 99 | |
| 100 | SubGraph &arm_compute::graph::operator<<(SubGraph &graph, Tensor &&tensor) |
| 101 | { |
| 102 | graph.add_tensor_object(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tensor))); |
| 103 | return graph; |
| 104 | } |
| 105 | |
| 106 | SubGraph &arm_compute::graph::operator<<(SubGraph &graph, SubTensor &&sub_tensor) |
| 107 | { |
| 108 | graph.add_tensor_object(arm_compute::support::cpp14::make_unique<SubTensor>(std::move(sub_tensor))); |
| 109 | return graph; |
| 110 | } |