blob: b1cbb9cc95bc69e7021a83870badf0f13474193f [file] [log] [blame]
Georgios Pinitase2c82fe2017-10-02 18:51:47 +01001/*
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +00002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitase2c82fe2017-10-02 18:51:47 +01003 *
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
30using namespace arm_compute::graph;
31
32SubGraph::SubGraph()
33 : _nodes(), _input(nullptr), _output(nullptr)
34{
35}
36
37void SubGraph::add_node(std::unique_ptr<INode> node)
38{
39 _nodes.push_back(std::move(node));
40}
41
42void 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 Pinitas1250a5a2018-01-02 13:27:37 +000055std::unique_ptr<Graph> SubGraph::construct(const GraphContext &ctx, std::unique_ptr<ITensorObject> input, std::unique_ptr<ITensorObject> output)
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010056{
57 auto graph = arm_compute::support::cpp14::make_unique<Graph>();
58
59 // Set hint
60 // TODO(geopin01): store hints of sub-graph
Georgios Pinitas1250a5a2018-01-02 13:27:37 +000061 graph->hints() = ctx.hints();
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010062
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 Giorgiodde9ec92018-02-13 15:24:04 +000070 // 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 Pinitase2c82fe2017-10-02 18:51:47 +010074 // 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
90bool SubGraph::has_input() const
91{
92 return _input != nullptr;
93}
94
95bool SubGraph::has_output() const
96{
97 return _output != nullptr;
98}
99
100SubGraph &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
106SubGraph &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}