forked from zhangmeishan/EGN3LDG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Concat.h
160 lines (124 loc) · 2.59 KB
/
Concat.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
#ifndef CONCAT
#define CONCAT
#include "MyLib.h"
#include "Node.h"
#include "Graph.h"
struct ConcatNode : Node{
public:
int nSize;
vector<int> inDims;
vector<PNode> ins;
public:
ConcatNode() : Node(){
nSize = 0;
inDims.clear();
ins.clear();
}
inline void clearValue(){
Node::clearValue();
}
inline void init(int dim, dtype dropOut, AlignedMemoryPool* mem = NULL){
Node::init(dim, -1, mem);
}
public:
void forward(Graph *cg, const vector<PNode>& x) {
if (x.size() == 0){
std::cout << "empty inputs for concat" << std::endl;
return;
}
ins.clear();
for (int i = 0; i < x.size(); i++){
ins.push_back(x[i]);
}
forward();
cg->addNode(this);
}
void forward(Graph *cg, PNode x1, PNode x2){
ins.clear();
ins.push_back(x1);
ins.push_back(x2);
forward();
cg->addNode(this);
}
void forward(Graph *cg, PNode x1, PNode x2, PNode x3){
ins.clear();
ins.push_back(x1);
ins.push_back(x2);
ins.push_back(x3);
forward();
cg->addNode(this);
}
void forward(Graph *cg, PNode x1, PNode x2, PNode x3, PNode x4){
ins.clear();
ins.push_back(x1);
ins.push_back(x2);
ins.push_back(x3);
ins.push_back(x4);
forward();
cg->addNode(this);
}
void forward(Graph *cg, PNode x1, PNode x2, PNode x3, PNode x4, PNode x5){
ins.clear();
ins.push_back(x1);
ins.push_back(x2);
ins.push_back(x3);
ins.push_back(x4);
ins.push_back(x5);
forward();
cg->addNode(this);
}
void forward(Graph *cg, PNode x1, PNode x2, PNode x3, PNode x4, PNode x5, PNode x6){
ins.clear();
ins.push_back(x1);
ins.push_back(x2);
ins.push_back(x3);
ins.push_back(x4);
ins.push_back(x5);
ins.push_back(x6);
forward();
cg->addNode(this);
}
void backward(){
int offset = 0;
for (int i = 0; i < nSize; ++i){
for (int idx = 0; idx < inDims[i]; idx++){
ins[i]->loss[idx] += loss[offset + idx];
}
offset += inDims[i];
}
}
inline void unlock(){
for (int i = 0; i < nSize; i++){
ins[i]->decrease_loc();
}
if(!lossed)return;
for (int i = 0; i < nSize; i++){
ins[i]->lossed = true;
}
}
protected:
inline void forward(){
nSize = ins.size();
inDims.clear();
int curDim = 0;
for (int i = 0; i < nSize; ++i){
inDims.push_back(ins[i]->val.dim);
curDim += inDims[i];
}
if(curDim != dim){
std::cout << "input dim size not match" << std::endl;
return;
}
int offset = 0;
for (int i = 0; i < nSize; ++i){
for (int idx = 0; idx < inDims[i]; idx++){
val[offset + idx] = ins[i]->val[idx];
}
offset += inDims[i];
}
for (int i = 0; i < nSize; ++i){
ins[i]->increase_loc();
}
}
};
#endif