blob: 83c3ef7e37f5ff542a0b3ddfe3ffa10337928be2 [file] [log] [blame]
Anthony Barbier2a07e182017-08-04 18:20:27 +01001/*
Jakub Sujak0d27b2e2023-08-24 14:01:20 +01002 * Copyright (c) 2018,2021,2023 Arm Limited.
Anthony Barbier2a07e182017-08-04 18:20:27 +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 */
Anthony Barbier2a07e182017-08-04 18:20:27 +010024#include "arm_compute/graph/INode.h"
25
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#include "arm_compute/core/Error.h"
27#include "arm_compute/graph/Edge.h"
28#include "arm_compute/graph/Graph.h"
29#include "arm_compute/graph/Tensor.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010030
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010031namespace arm_compute
Anthony Barbier2a07e182017-08-04 18:20:27 +010032{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010033namespace graph
34{
35// *INDENT-OFF*
36// clang-format off
37INode::INode()
38 : _graph(nullptr), _id(EmptyNodeID), _common_params({ "", Target::UNSPECIFIED}),
39 _outputs(), _input_edges(), _output_edges(), _assigned_target(Target::UNSPECIFIED)
40{
41}
42// clang-format on
43// *INDENT-ON*
44
Georgios Pinitascac13b12018-04-27 19:07:19 +010045Status INode::validate() const
46{
47 return Status{};
48}
49
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010050void INode::set_graph(Graph *g)
51{
52 ARM_COMPUTE_ERROR_ON(g == nullptr);
53 _graph = g;
54}
55
56void INode::set_id(NodeID id)
57{
58 _id = id;
59}
60
61void INode::set_common_node_parameters(NodeParams common_params)
62{
63 _common_params = std::move(common_params);
64}
65
66void INode::set_requested_target(Target target)
67{
68 _common_params.target = target;
69}
70
71void INode::set_assigned_target(Target target)
72{
73 _assigned_target = target;
74}
75
76void INode::set_output_tensor(TensorID tid, size_t idx)
77{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (tid != NullTensorID && (idx < _outputs.size()) && (_graph->tensor(tid) != nullptr))
Anthony Barbier2a07e182017-08-04 18:20:27 +010079 {
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010080 ARM_COMPUTE_ERROR_ON(_graph == nullptr);
81 Tensor *updated_tensor = _graph->tensor(tid);
82 _outputs[idx] = tid;
83
84 // Set tensor to all output edges of the node
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010085 for (auto &output_edge_id : _output_edges)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010086 {
87 auto output_edge = _graph->edge(output_edge_id);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 if (output_edge != nullptr)
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010089 {
90 // Unbind edge from current tensor
91 auto current_output_tensor = output_edge->tensor();
92 current_output_tensor->unbind_edge(output_edge->id());
93
94 // Update tensor to edge and rebind tensor
95 output_edge->update_bound_tensor(updated_tensor);
96 updated_tensor->bind_edge(output_edge->id());
97 }
98 }
Anthony Barbier2a07e182017-08-04 18:20:27 +010099 }
Anthony Barbier2a07e182017-08-04 18:20:27 +0100100}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100101
102NodeID INode::id() const
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000103{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100104 return _id;
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000105}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100106
107std::string INode::name() const
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000108{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100109 return _common_params.name;
Michele Di Giorgiodde9ec92018-02-13 15:24:04 +0000110}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100111
112const Graph *INode::graph() const
Anthony Barbier2a07e182017-08-04 18:20:27 +0100113{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100114 return _graph;
Anthony Barbier2a07e182017-08-04 18:20:27 +0100115}
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100116
117Graph *INode::graph()
118{
119 return _graph;
120}
121
122const std::vector<TensorID> &INode::outputs() const
123{
124 return _outputs;
125}
126
127const std::vector<EdgeID> &INode::input_edges() const
128{
129 return _input_edges;
130}
131
132const std::set<EdgeID> &INode::output_edges() const
133{
134 return _output_edges;
135}
136
137TensorID INode::input_id(size_t idx) const
138{
139 ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
140 Edge *e = _graph->edge(_input_edges[idx]);
141 return (e != nullptr) ? e->tensor_id() : NullTensorID;
142}
143
144TensorID INode::output_id(size_t idx) const
145{
146 ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
147 return _outputs[idx];
148}
149
150Tensor *INode::input(size_t idx) const
151{
152 ARM_COMPUTE_ERROR_ON(_graph == nullptr);
153 ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
154 Edge *e = _graph->edge(_input_edges[idx]);
155 return (e != nullptr) ? e->tensor() : nullptr;
156}
157
158Tensor *INode::output(size_t idx) const
159{
160 ARM_COMPUTE_ERROR_ON(_graph == nullptr);
161 ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
162 return _graph->tensor(_outputs[idx]);
163}
164
165EdgeID INode::input_edge_id(size_t idx) const
166{
167 ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
168 return _input_edges[idx];
169}
170
171Edge *INode::input_edge(size_t idx) const
172{
173 ARM_COMPUTE_ERROR_ON(_graph == nullptr);
174 ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
175 return _graph->edge(_input_edges[idx]);
176}
177
178size_t INode::num_inputs() const
179{
180 return _input_edges.size();
181}
182
183size_t INode::num_outputs() const
184{
185 return _outputs.size();
186}
187
Georgios Pinitas2a2db592018-08-15 12:14:46 +0100188NodeParams INode::common_node_params() const
189{
190 return _common_params;
191}
192
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100193Target INode::requested_target() const
194{
195 return _common_params.target;
196}
197
198Target INode::assigned_target() const
199{
200 return _assigned_target;
201}
202} // namespace graph
Jakub Sujak0d27b2e2023-08-24 14:01:20 +0100203} // namespace arm_compute