-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
212 lines (174 loc) · 4.8 KB
/
graph.cpp
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
204
205
206
207
208
209
210
211
212
#include "node.h"
#include "graph.h"
#include "utils.h"
#include <iostream>
#include <fstream>
#include <string>
#include <set>
Graph::Graph()
{
}
Graph::~Graph()
{
for(int i=0; i<_nodes.size(); i++)
{
SAFE_DELETE(_nodes[i]);
}
_nodes.clear();
}
int Graph::load_model(string file_name)
{
string param_file_name = file_name + ".ncnn.param";
string bin_file_name = file_name + ".ncnn.bin";
ifstream fin(param_file_name.c_str());
FILE *fp = fopen(bin_file_name.c_str(), "rb");
int index = 0;
string strline;
while(getline(fin, strline))
{
if(index > 1)
{
vector<string> params = split(strline, " ");
Node* node = new Node(this, params);
node->load_model(params, fp);
_nodes.push_back(node);
}
index++;
}
fin.close();
fclose(fp);
return 0;
}
void Graph::register_operand(string operand_name)
{
if(_operand_map.find(operand_name) == _operand_map.end())
{
_operand_map[operand_name] = nullptr;
}
}
void Graph::register_operand(string operand_name, Tensor* operand_tensor)
{
if(_operand_map.find(operand_name) == _operand_map.end())
{
_operand_map[operand_name] = operand_tensor;
return;
}
if(_operand_map[operand_name] == nullptr)
{
_operand_map[operand_name] = operand_tensor;
return;
}
if(_operand_map[operand_name] == operand_tensor)
{
return;
}
SAFE_DELETE(_operand_map[operand_name]);
_operand_map[operand_name] = operand_tensor;
}
void Graph::register_output(string operand_name, Node* node)
{
if(_output_owner_map.find(operand_name) == _output_owner_map.end())
{
_output_owner_map[operand_name] = node;
}
}
void Graph::input(string operand_name, Tensor* input_tensor)
{
if(_operand_map.find(operand_name) != _operand_map.end())
{
if(_operand_map[operand_name] != nullptr)
{
//SAFE_DELETE(_operand_map[operand_name]);
}
}
_operand_map[operand_name] = input_tensor;
}
int Graph::extract(string operand_name, Tensor* &output_tensor)
{
if(_operand_map.find(operand_name) == _operand_map.end())
{
return -1;
}
//检查有无该输出的执行列表
if(_exec_order_map.find(operand_name) == _exec_order_map.end())
{
//没有则执行拓扑排序得到节点列表
vector<Node*> execOrder;
if(0 != topo_sort(operand_name, execOrder))
{
return -1;
}
_exec_order_map[operand_name] = execOrder;
}
//执行列表
vector<Node*> execList = _exec_order_map[operand_name];
for(int i=0; i<execList.size(); i++)
{
execList[i]->forward();
}
output_tensor = _operand_map[operand_name];
return 0;
}
int Graph::get_result(string operand_name, Tensor* &output_tensor)
{
if(_operand_map.find(operand_name) == _operand_map.end())
{
return -1;
}
output_tensor = _operand_map[operand_name];
return 0;
}
Tensor* Graph::find_operand(string operand_name)
{
return _operand_map[operand_name];
}
int Graph::topo_sort(string operand_name, vector<Node*> &exec_list)
{
Node* outputNode = _output_owner_map[operand_name];
map<Node*, set<Node*>> nodeIndegrees;
for(int i=0; i<_nodes.size(); i++)
{
set<Node*> indegree;
vector<string> inputNames = _nodes[i]->get_input_names();
for(int j=0; j<inputNames.size(); j++)
{
Node* inputNode = _output_owner_map[inputNames[j]];
indegree.insert(inputNode);
}
nodeIndegrees[_nodes[i]] = indegree;
}
while(1)
{
set<Node*> nodesToRemove;
map<Node*, set<Node*>>::iterator iterCheck;
for(iterCheck=nodeIndegrees.begin(); iterCheck != nodeIndegrees.end(); iterCheck++)
{
if(iterCheck->second.size() == 0)
{
Node* nodeRemove = iterCheck->first;
exec_list.push_back(nodeRemove);
nodesToRemove.insert(nodeRemove);
}
}
if(nodesToRemove.size()==0 && nodeIndegrees.find(outputNode)!=nodeIndegrees.end())
{
//没有找到执行到该节点的路径
return -1;
}
set<Node*>::iterator iterRemove;
for(iterRemove=nodesToRemove.begin(); iterRemove != nodesToRemove.end(); iterRemove++)
{
map<Node*, set<Node*>>::iterator iterNode;
for(iterNode=nodeIndegrees.begin(); iterNode != nodeIndegrees.end(); iterNode++)
{
iterNode->second.erase(*iterRemove);
}
nodeIndegrees.erase(*iterRemove);
}
if(nodeIndegrees.find(outputNode) == nodeIndegrees.end())
{
return 0;
}
}
return -1;
}