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
|
/*
* Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "model/operation/SubgraphContext.h"
#include "util/logging.h"
#include "cpp14/memory.h"
#include <cassert>
#include <string>
#include <stdint.h>
namespace neurun
{
namespace model
{
namespace operation
{
OperationIndex SubgraphContext::generateIndex()
{
assert((_index_count) <= UINT32_MAX);
return OperationIndex{_index_count++};
}
OperationIndex SubgraphContext::append(std::unique_ptr<Subgraph> &&subgraph)
{
auto index = generateIndex();
_subgs[index] = std::move(subgraph);
return index;
}
OperationIndex SubgraphContext::append(const OperationIndex &index, const Node &node)
{
std::unique_ptr<Subgraph> subg = nnfw::cpp14::make_unique<model::operation::Subgraph>();
subg->appendOperation(index, node);
return append(std::move(subg));
}
const Subgraph &SubgraphContext::at(const OperationIndex &index) const
{
return *(_subgs.at(index));
}
Subgraph &SubgraphContext::at(const OperationIndex &index) { return *(_subgs.at(index)); }
bool SubgraphContext::exist(const OperationIndex &index) const
{
return _subgs.find(index) != _subgs.end();
}
bool SubgraphContext::hasNode(const OperationIndex &node_index) const
{
for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
{
const auto &subg = *it->second;
for (const auto &elem : subg.operations())
{
if (elem.index == node_index)
return true;
}
}
return false;
}
OperationIndex SubgraphContext::findNode(const OperationIndex &node_index) const
{
for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
{
auto &subg_index = it->first;
const auto &subg = *it->second;
for (const auto &elem : subg.operations())
{
if (elem.index == node_index)
{
return subg_index;
}
}
}
assert(true && "DO NOT ENTER HERE");
return OperationIndex(UINT32_MAX); // CAN'T ENTER HERE
}
void SubgraphContext::iterate(
const std::function<void(const OperationIndex &, const Subgraph &)> &fn) const
{
for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
{
fn(it->first, *it->second);
}
}
void SubgraphContext::iterate(const std::function<void(const OperationIndex &, Subgraph &)> &fn)
{
for (auto it = _subgs.begin(); it != _subgs.end(); ++it)
{
fn(it->first, *it->second);
}
}
// TODO: Extract this into external helper function
void SubgraphContext::dump(const std::string &msg) const
{
VERBOSE(SubgraphContext) << "SubgraphContext(" << msg << ")" << std::endl;
iterate([&](const OperationIndex &idx, const model::operation::Subgraph &subg) {
VERBOSE(SubgraphContext) << idx.value() << "] " << subg.getStr() << std::endl;
});
}
} // namespace operation
} // namespace model
} // namespace neurun
|