-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodiff.cpp
441 lines (383 loc) · 11.6 KB
/
autodiff.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include<bits/stdc++.h>
#include<vector>
#include "autodiff.hpp"
using namespace std;
template<typename T>
class Node{
public:
vector<Node<T>> inputs;
//
// do we need a gradient
//
T const_var;
string name;
//Op<T> *operation;
int operation_id;
Node(){
operation_id = -1;
};
//Node(){
// operation = new Op<T>(this);
//};
~Node(){
//if(operation != NULL){
// delete operation;
// operation = NULL;
//}
}
Node<T> operator +(const Node &x) const{
return __add__(x);
}
/*
Node<T> operator *(const Node &x) const{
return __mul__(x);
}
*/
Node<T> operator *(const Node<T> x) const{
if(typeid(x) == typeid(Node))
return __mul__(x);
//else
// return __mul__const(x);
}
/*
Node<T> & operator =(const Node &x){
if(this != &x){
inputs.clear();
for(auto newinput: x.inputs)
inputs.push_back(newinput);
operation_id = x.operation_id;
}
return *this;
}
*/
bool operator <(const Node &x) const{
return name < x.name;
}
Node<T> __add__(const Node<T> &x) const{
Op<T> *temp_op = new Add_Op<T>();
auto ret = temp_op->__call__(*this, x);
delete temp_op;
return ret;
}
Node<T> __mul__(const Node<T> &x) const{
Op<T> *temp_op = new Mul_Op<T>();
auto ret = temp_op->__call__(*this, x);
delete temp_op;
return ret;
}
Op<T>* get_operation() const{
Op<T> *temp_op = NULL;
switch(operation_id){
case ZEROSLIKE_ID:
temp_op = new Zeroslike_Op<T>();
break;
case ONESLIKE_ID:
temp_op = new Oneslike_Op<T>();
break;
case ADD_ID:
temp_op = new Add_Op<T>();
break;
case MUL_ID:
temp_op = new Mul_Op<T>();
break;
default:
temp_op = NULL;
break;
}
return temp_op;
}
T compute(Node<T> x, vector<T> inputs){
Op<T> *temp_op = get_operation();
T ret;
if(temp_op != NULL){
ret = temp_op->compute(x, inputs);
delete temp_op;
}
return ret;
};
vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){
Op<T> *temp_op = get_operation();
vector<Node<T>> ret;
if(temp_op != NULL){
ret = temp_op->gradient(x, output_grad);
delete temp_op;
}
return ret;
}
};
template<typename T>
ostream& operator<<(ostream& out,const Node<T> &n){
return out<<n.name;
}
template<typename T>
class Op{
public:
Op(){};
Op(Node<T>* px){
px->operation = this;
}
virtual Node<T> __call__(Node<T> a, Node<T> b){};
virtual T compute(Node<T> x, vector<T> inputs){};
virtual vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){};
};
template<typename T>
class Add_Op: public Op<T>{
public:
Add_Op(){};
//Add_Op(Node<T>* px){
// px->operation = this;
//}
Node<T> __call__(Node<T> a, Node<T> b){
Node<T> newnode;
newnode.inputs.push_back(a);
newnode.inputs.push_back(b);
newnode.name = "(" + a.name + "+" + b.name + ")";
//newnode.operation = this;
newnode.operation_id = ADD_ID;
return newnode;
};
//template<typename T>
T compute(Node<T> x, vector<T> inputs){
int len = inputs.size();
assert(len == 2);
return inputs[0] + inputs[1];
};
vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){
vector<Node<T>> grads;
grads.push_back(output_grad);
grads.push_back(output_grad);
return grads;
}
};
template<typename T>
class Mul_Op: public Op<T>{
public:
Mul_Op(){};
Node<T> __call__(Node<T> a, Node<T> b){
Node<T> newnode;
newnode.inputs.push_back(a);
newnode.inputs.push_back(b);
newnode.name = "(" + a.name + "*" + b.name + ")";
//newnode.operation = this;
newnode.operation_id = MUL_ID;
return newnode;
}
T compute(Node<T> x, vector<T> inputs){
int len = inputs.size();
assert(len == 2);
return inputs[0] * inputs[1];
};
vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){
vector<Node<T>> grads;
grads.push_back(x.inputs[1] * output_grad);
grads.push_back(x.inputs[0] * output_grad);
return grads;
}
};
/*
template<typename T>
class Mul_const_Op: public Op<T>{
public:
Mul_const_Op(){};
Node<T> __call__(Node<T> a, T b){
Node<T> newnode;
newnode.inputs.push_back(a);
newnode.inputs.push_back(b);
newnode.name = "(" + a.name + "*" + b.name + ")";
//newnode.operation = this;
newnode.operation_id = 3;
return newnode;
}
T compute(Node<T> x, vector<T> inputs){
int len = inputs.size();
assert(len == 2);
return inputs[0] * inputs[1];
};
vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){
vector<Node<T>> grads;
grads.push_back(x.inputs[1] * output_grad);
grads.push_back(x.inputs[0] * output_grad);
return grads;
}
};
*/
template<typename T>
class Placeholder_Op: public Op<T>{
public:
Placeholder_Op(){};
Node<T> __call__(string name){
Node<T> newnode;
//newnode.operation = this;
newnode.name = name;
return newnode;
}
T compute(Node<T> x, vector<T> inputs){};
vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){};
};
template<typename T>
Node<T> Variable(string name){
return Placeholder_Op<T>().__call__(name);
}
template<typename T>
class Zeroslike_Op: public Op<T>{
public:
Zeroslike_Op(){};
Node<T> __call__(Node<T> x){
Node<T> newnode;
newnode.inputs.push_back(x);
newnode.operation_id = ZEROSLIKE_ID;
//newnode.operation = this;
newnode.name = "Zeroslike(" + x.name + ")";
return newnode;
}
T compute(Node<T> x, vector<T> inputs){
return T(0);
}
vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){
vector<Node<T>> grads;
grads.push_back(__call__(x));
return grads;
}
};
template<typename T>
class Oneslike_Op: public Op<T>{
public:
Oneslike_Op(){};
Node<T> __call__(Node<T> x){
Node<T> newnode;
newnode.inputs.push_back(x);
newnode.operation_id = ONESLIKE_ID;
//newnode.operation = this;
newnode.name = "Oneslike(" + x.name + ")";
cout<<"in call: "<<newnode<<endl;
return newnode;
}
T compute(Node<T> x, vector<T> inputs){
//cout<<"suan ni hen"<<endl;
return T(1);
};
vector<Node<T>> gradient(Node<T> x, Node<T> output_grad){
//cout<<"gradient in ones_like"<<endl;
vector<Node<T>> grads;
grads.push_back(Zeroslike_Op<T>().__call__(x));
return grads;
};
};
template<typename T>
class Executor{
public:
//注意:需要这样一个一直存在的实例,不能在gradient里面创建临时实例
//否则出来后,输出梯度Oneslike(y) 的算子指针就野了, 虽然这样不太elegent
//Oneslike_Op<T> oppo;
Executor(){};
vector<T> forward_run(vector<Node<T>> node_list, map<Node<T>, T> &mp){
cout<<"begin forward"<<endl;
vector<Node<T>> topo_rank = topo_sort(node_list);
for(auto x: topo_rank){
vector<T> cur_inputs;
if(x.inputs.size() == 1){
cur_inputs.push_back(mp[x.inputs[0]]);
//cout<<"cur caling: "<<x<<" from inputs: "<<mp[x.inputs[0]]<<endl;
}
else if(x.inputs.size() == 2){
cur_inputs.push_back(mp[x.inputs[0]]);
cur_inputs.push_back(mp[x.inputs[1]]);
//cout<<"cur caling: "<<x<<" from inputs: "<<mp[x.inputs[0]]<<" and "<<mp[x.inputs[1]]<<endl;
}
else{
//leaf node
continue;
}
//mp[x] = x.operation->compute(x, cur_inputs);
mp[x] = x.compute(x, cur_inputs);
//cout<<"res: "<<mp[x]<<endl;
}
vector<T> ret;
for(auto x: node_list)
ret.push_back(mp[x]);
return ret;
}
vector<Node<T>> build_graph(Node<T> output_node, vector<Node<T>> raw_nodes){
//compute the gradient graph for raw_nodes
vector<Node<T>> grad_nodes;
map<Node<T>, Node<T>> mp;
//这里很关键???
Oneslike_Op<T> op;
//mp[output_node] = oppo.__call__(output_node);
mp[output_node] = op.__call__(output_node);
//only single node supported, can be extended by passing vector<Node<T>> output_nodes to build graph
vector<Node<T>> topo_rank_rev = topo_sort(vector<Node<T>> {output_node});
reverse(topo_rank_rev.begin(), topo_rank_rev.end());
cout<<topo_rank_rev.size()<<endl;
//cout<<"Begin backprop"<<endl;
for(auto x: topo_rank_rev){
//Perform 3 steps:
//1) sum all the gradients from adjacent nodes
//2) cal sub-gradients for the inputs
//3) add current node to result
//leaf node have no children
if(x.inputs.size() == 0)
continue;
Node<T> x_grads = mp[x];
vector<Node<T>> child_grads = x.gradient(x, x_grads);
for(int i = 0; i < x.inputs.size(); i++){
auto child = x.inputs[i];
if(mp.find(child) == mp.end()){
mp[child] = child_grads[i];
}
else{
mp[child] = mp[child] + child_grads[i];
}
}
}
for(auto x: raw_nodes){
grad_nodes.push_back(mp[x]);
}
return grad_nodes;
}
vector<Node<T>> topo_sort(vector<Node<T>> topo_nodes){
vector<Node<T>> topo_rank;
//currently, the node_list begins with the outputs
set<Node<T>> vis;
for(auto x: topo_nodes){
topo_dfs(x, vis, topo_rank);
}
return topo_rank;
}
void topo_dfs(Node<T> x, set<Node<T>> & vis, vector<Node<T>> &topo_rank){
if(vis.find(x) != vis.end()) return ;
vis.insert(x);
for(auto childs: x.inputs){
topo_dfs(childs, vis, topo_rank);
}
topo_rank.push_back(x);
}
};
int main(){
Node<int> x1 = Variable<int>("x1");
Node<int> x2 = Variable<int>("x2");
Node<int> y = x1 * x1 + x1 * x2;
//x2.operation->compute(x2, vector<int>{});
//Node z = y + y;
//vector<Node<int>> v={y, x1, x2};
//Executor<int> e(v);
cout<<"Ready to build graph"<<endl;
Executor<int> e;
vector<Node<int>> grads = e.build_graph(y, vector<Node<int>> {x1, x2});
//cout<<"check grads"<<endl;
//for(auto x : grads)
// cout<<x<<endl;
vector<Node<int>> v_cal;
v_cal.push_back(y);
for(auto x: grads)
v_cal.push_back(x);
map<Node<int>, int> mp;
mp[x1] = 30;
mp[x2] = 10;
vector<int> grad_results = e.forward_run(v_cal, mp);
for(int i = 0; i < v_cal.size(); i++){
cout<<v_cal[i]<<' '<<grad_results[i]<<endl;
}
cout<<"Done"<<endl;
return 0;
}