-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGraph.h
231 lines (194 loc) · 6.15 KB
/
Graph.h
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef BasicGraph
#define BasicGraph
/*
* Graph.h:
* manage nodes in a neural network model
*
* Created on: Apr 21, 2017
* Author: mszhang
*/
//#include "Eigen/Dense"
#include "Node.h"
#include "MyLib.h"
//using namespace Eigen;
// one Node means a vector
// the col should be 1, because we aimed for NLP only
class Graph {
protected:
vector<PExecute> execs; //backward
vector<PNode> nodes; //forward
vector<PNode> free_nodes;
vector<PNode> finish_nodes;
vector<PNode> all_nodes;
public:
bool train;
public:
Graph() {
execs.clear();
execs.clear();
nodes.clear();
free_nodes.clear();
}
virtual ~Graph() {
int count = execs.size();
for (int idx = 0; idx < count; idx++) {
delete execs[idx];
}
execs.clear();
execs.clear();
nodes.clear();
free_nodes.clear();
}
public:
inline void clearValue(const bool& bTrain = false) {
int count = execs.size();
for (int idx = 0; idx < count; idx++) {
delete execs[idx];
}
execs.clear();
count = nodes.size();
vector<LDG::PTensor> vec_val, vec_loss;
for (int idx = 0; idx < count; idx++) {
nodes[idx]->clearValue();
if(nodes[idx]->node_type != "bucket")
vec_val.push_back(&nodes[idx]->val);
vec_loss.push_back(&nodes[idx]->loss);
//if(nodes[idx]->drop_value > 0)
//vec_drop_mask.push_back(&nodes[idx]->drop_mask);
}
DEV->set(vec_val, 0);
if(bTrain)
DEV->set(vec_loss, 0);
//DEV->set(vec_drop_mask, 1);
nodes.clear();
free_nodes.clear();
finish_nodes.clear();
all_nodes.clear();
train = bTrain;
}
inline void backward() {
int count = execs.size();
for (int idx = count - 1; idx >= 0; idx--) {
execs[idx]->backward();
}
}
inline void addNode(PNode x) {
nodes.push_back(x);
if (x->degree == 0) {
free_nodes.push_back(x);
}
all_nodes.push_back(x);
}
//real executation
inline void compute() {
int free_count = free_nodes.size();
while (free_count > 0) {
vector<PExecute> cur_execs;
int cur_execs_size = 0;
for (int idx = 0; idx < free_count; idx++) {
bool find = false;
for (int idy = 0; idy < cur_execs_size; idy++) {
if (cur_execs[idy]->addNode(free_nodes[idx])) {
find = true;
break;
}
}
if (!find) {
PExecute new_exec = free_nodes[idx]->generate(train);
cur_execs.push_back(new_exec);
cur_execs_size++;
}
}
//execute
//#pragma omp parallel for
for (int idy = 0; idy < cur_execs_size; idy++) {
cur_execs[idy]->forward();
}
for (int idy = 0; idy < cur_execs_size; idy++) {
execs.push_back(cur_execs[idy]);
}
//finished nodes
vector<PNode> new_free_nodes;
for (int idx = 0; idx < free_count; idx++) {
finish_nodes.push_back(free_nodes[idx]);
int parent_count = free_nodes[idx]->parents.size();
for (int idy = 0; idy < parent_count; idy++) {
free_nodes[idx]->parents[idy]->degree--;
if (free_nodes[idx]->parents[idy]->degree == 0) {
new_free_nodes.push_back(free_nodes[idx]->parents[idy]);
}
}
}
// update free nodes
free_nodes.clear();
free_count = new_free_nodes.size();
for (int idx = 0; idx < free_count; idx++) {
free_nodes.push_back(new_free_nodes[idx]);
}
}
if (finish_nodes.size() != all_nodes.size()) {
std::cout << "error: several nodes are not executed, finished: " << finish_nodes.size() << ", all: " << all_nodes.size() << std::endl;
int total_node_num = all_nodes.size();
int unprocessed = 0;
for (int idx = 0; idx < total_node_num; idx++) {
PNode curNode = all_nodes[idx];
if (curNode->degree >= 0) {
curNode->typeEqual(all_nodes[0]);
unprocessed++;
}
}
std::cout << "unprocessed: " << unprocessed << std::endl;
}
}
};
// one very useful function to collect pointers of derived nodes
template<typename DerivedNode>
inline vector<PNode> getPNodes(vector<DerivedNode>& inputs, int size) {
int usedSize = inputs.size();
if (size >= 0 && size < usedSize) usedSize = size;
vector<PNode> pnodes;
for (int idx = 0; idx < usedSize; idx++) {
pnodes.push_back(&(inputs[idx]));
}
return pnodes;
}
template<typename DerivedNode>
inline vector<PNode> getPNodes(DerivedNode inputs[], int size) {
//int usedSize = inputs.;
//if (size >= 0 && size < usedSize) usedSize = size;
int usedSize = size;
vector<PNode> pnodes;
for (int idx = 0; idx < usedSize; idx++) {
pnodes.push_back(&(inputs[idx]));
}
return pnodes;
}
template<typename DerivedNode>
inline vector<PNode> getPNodes(vector<DerivedNode>& inputs, int start, int length) {
int end, tmp_end = start + length;
if (tmp_end > inputs.size())
end = inputs.size();
else
end = tmp_end;
//if (size >= 0 && size < usedSize) usedSize = size;
vector<PNode> pnodes;
for (int idx = start; idx < end; idx++) {
pnodes.push_back(&(inputs[idx]));
}
return pnodes;
}
template<typename DerivedNode>
inline vector<PNode> getPNodes(DerivedNode inputs[], int size, int start, int length) {
int end, tmp_end = start + length;
if (tmp_end > size)
end = size;
else
end = tmp_end;
//if (size >= 0 && size < usedSize) usedSize = size;
vector<PNode> pnodes;
for (int idx = start; idx < end; idx++) {
pnodes.push_back(&(inputs[idx]));
}
return pnodes;
}
#endif