aboutsummaryrefslogtreecommitdiff
path: root/src/graph/INode.cpp
blob: b0c31372aa78f7ec5bc9a7f042f57dbd11614ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (c) 2018 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "arm_compute/graph/INode.h"

#include "arm_compute/core/Error.h"
#include "arm_compute/graph/Edge.h"
#include "arm_compute/graph/Graph.h"
#include "arm_compute/graph/Tensor.h"

namespace arm_compute
{
namespace graph
{
// *INDENT-OFF*
// clang-format off
INode::INode()
    : _graph(nullptr), _id(EmptyNodeID), _common_params({ "", Target::UNSPECIFIED}),
      _outputs(), _input_edges(), _output_edges(), _assigned_target(Target::UNSPECIFIED)
{
}
// clang-format on
// *INDENT-ON*

Status INode::validate() const
{
    return Status{};
}

void INode::set_graph(Graph *g)
{
    ARM_COMPUTE_ERROR_ON(g == nullptr);
    _graph = g;
}

void INode::set_id(NodeID id)
{
    _id = id;
}

void INode::set_common_node_parameters(NodeParams common_params)
{
    _common_params = std::move(common_params);
}

void INode::set_requested_target(Target target)
{
    _common_params.target = target;
}

void INode::set_assigned_target(Target target)
{
    _assigned_target = target;
}

void INode::set_output_tensor(TensorID tid, size_t idx)
{
    if(tid != NullTensorID && (idx < _outputs.size()) && (_graph->tensor(tid) != nullptr))
    {
        ARM_COMPUTE_ERROR_ON(_graph == nullptr);
        Tensor *updated_tensor = _graph->tensor(tid);
        _outputs[idx]          = tid;

        // Set tensor to all output edges of the node
        for(auto &output_edge_id : _output_edges)
        {
            auto output_edge = _graph->edge(output_edge_id);
            if(output_edge != nullptr)
            {
                // Unbind edge from current tensor
                auto current_output_tensor = output_edge->tensor();
                current_output_tensor->unbind_edge(output_edge->id());

                // Update tensor to edge and rebind tensor
                output_edge->update_bound_tensor(updated_tensor);
                updated_tensor->bind_edge(output_edge->id());
            }
        }
    }
}

NodeID INode::id() const
{
    return _id;
}

std::string INode::name() const
{
    return _common_params.name;
}

const Graph *INode::graph() const
{
    return _graph;
}

Graph *INode::graph()
{
    return _graph;
}

const std::vector<TensorID> &INode::outputs() const
{
    return _outputs;
}

const std::vector<EdgeID> &INode::input_edges() const
{
    return _input_edges;
}

const std::set<EdgeID> &INode::output_edges() const
{
    return _output_edges;
}

TensorID INode::input_id(size_t idx) const
{
    ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
    Edge *e = _graph->edge(_input_edges[idx]);
    return (e != nullptr) ? e->tensor_id() : NullTensorID;
}

TensorID INode::output_id(size_t idx) const
{
    ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
    return _outputs[idx];
}

Tensor *INode::input(size_t idx) const
{
    ARM_COMPUTE_ERROR_ON(_graph == nullptr);
    ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
    Edge *e = _graph->edge(_input_edges[idx]);
    return (e != nullptr) ? e->tensor() : nullptr;
}

Tensor *INode::output(size_t idx) const
{
    ARM_COMPUTE_ERROR_ON(_graph == nullptr);
    ARM_COMPUTE_ERROR_ON(idx >= _outputs.size());
    return _graph->tensor(_outputs[idx]);
}

EdgeID INode::input_edge_id(size_t idx) const
{
    ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
    return _input_edges[idx];
}

Edge *INode::input_edge(size_t idx) const
{
    ARM_COMPUTE_ERROR_ON(_graph == nullptr);
    ARM_COMPUTE_ERROR_ON(idx >= _input_edges.size());
    return _graph->edge(_input_edges[idx]);
}

size_t INode::num_inputs() const
{
    return _input_edges.size();
}

size_t INode::num_outputs() const
{
    return _outputs.size();
}

NodeParams INode::common_node_params() const
{
    return _common_params;
}

Target INode::requested_target() const
{
    return _common_params.target;
}

Target INode::assigned_target() const
{
    return _assigned_target;
}
} // namespace graph
} // namespace arm_compute