blob: f62b2617c502167bec5692c9500dd7d7b9416ebc [file] [log] [blame]
Georgios Pinitase2c82fe2017-10-02 18:51:47 +01001/*
2 * Copyright (c) 2017 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 */
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
70 // Construct nodes
71 for(auto &node : _nodes)
72 {
73 graph->add_node(std::move(node));
74 }
75
76 // Configure output
77 if(_output == nullptr)
78 {
79 _output = std::move(output);
80 }
81 graph->add_tensor_object(std::move(_output));
82
83 return graph;
84}
85
86bool SubGraph::has_input() const
87{
88 return _input != nullptr;
89}
90
91bool SubGraph::has_output() const
92{
93 return _output != nullptr;
94}
95
96SubGraph &arm_compute::graph::operator<<(SubGraph &graph, Tensor &&tensor)
97{
98 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<Tensor>(std::move(tensor)));
99 return graph;
100}
101
102SubGraph &arm_compute::graph::operator<<(SubGraph &graph, SubTensor &&sub_tensor)
103{
104 graph.add_tensor_object(arm_compute::support::cpp14::make_unique<SubTensor>(std::move(sub_tensor)));
105 return graph;
106}