-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperators.cpp
645 lines (552 loc) · 15.6 KB
/
operators.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include <functional>
#include <vector>
// #include "constant.h"
#include "fixed.hpp"
// #include "global.h"
#include "ios.hpp"
#include "math.hpp"
// #include "variable.h"
#include <typeinfo>
#include <ostream>
#include <stdlib.h>
#include <iostream>
#include <stack>
#include <set>
#include <map>
#include <queue>
template<typename T>
class Node{
private:
inline static int idx;
int node_idx;
public:
bool isPlaceholder;
// virtual void compute(){};
virtual T getValue() = 0;
virtual T diff(int) =0;
virtual void register_parent(Node<T> *) =0;
virtual std::vector<Node<T> *> const getParents() =0;
Node(){
node_idx = idx;
idx++;
std::cout << "ID Allotted: " << node_idx << "\n";
}
int get_unq_node_idx() {
return node_idx;
}
};
// int Node::idx
class DummyNode{
public:
DummyNode() {}
};
// template<typename T>
// class Placeholder : public Node<T> {
// template<typename T>
//
// void * entry;
// bool isConstant;
// public:
// Placeholder(Constant<T> &c){
// this->isPlaceholder = true;
// Constant<T> entry(c.getValue()) ;
// entry = &c;
// isConstant = true;
// std::cout << "Placeholder recieved value: " << entry <<"\n";
// }
//
// Placeholder(Variable<T> &c){
// this->isPlaceholder = true;
// Variable<T> entry(c.getValue()) ;
// entry = &c;
// isConstant = false;
// std::cout << "Placeholder recieved value: " << entry <<"\n";
// }
//
// T getValue()
// {
// if(isConstant)
// {
// return (static_cast<Constant<T> *>(entry))->getValue();
// }
// else{
// return (static_cast<Variable<T> *>(entry))->getValue();
// }
// }
//
// };
template <typename T>
class Placeholder : public Node<T>,public DummyNode {
// private:
// static int placeholder_index;
// protected:
// int placeholder_ID;
public:
Placeholder() {
// placeholder_ID = placeholder_index;
// placeholder_index++;
this->isPlaceholder = true;
}
// int get_ID(){
// return placeholder_ID;
// }
// virtual int getPlaceholderType() =0;
};
template <typename TYPE>
class Constant: public Placeholder<TYPE>{
private:
TYPE value;
std::string name;
int grad =0;
std::vector<Node<TYPE> *> parents;
public:
// Constant(TYPE val){
// value = val;
// }
Constant(auto val){
TYPE num{val};
value = num;
std::cout << "Constant Node allotted\n";
}
Constant(TYPE val, std::string n){
value = val;
name = n;
}
TYPE getValue() {
return value;
}
int getGrad(){
return grad;
}
TYPE diff(int wrt_id){
return 0;
}
void register_parent(Node<TYPE> * parent) {
parents.push_back(parent);
}
std::vector<Node<TYPE> *> const getParents() {
return parents;
}
// int getPlaceholderType(){ // {0 -> constant, 1-> variable}
// return 0;
// }
};
template<typename T>
std::ostream& operator<<(std::ostream& os, Constant<T> &obj) {
os << obj.getValue();
return os;
}
template <typename TYPE>
class Variable: public Placeholder<TYPE>{
private:
TYPE value;
std::string name;
TYPE gradient;
std::vector<Node<TYPE> *> parents;
public:
inline static int count;
Variable(auto val){
TYPE num{val};
value = num;
name = "x_"+ std::to_string(count);
count++;
}
std::string getName_str() const{
return name;
}
TYPE getValue() {
return value;
}
void setValue(TYPE input) {
value = input;
}
TYPE diff(int wrt_id){
if(wrt_id == this->get_unq_node_idx()){
return 1;
}
else{
return 0;
}
}
void register_parent(Node<TYPE> * parent) {
parents.push_back(parent);
}
std::vector<Node<TYPE> *> const getParents() {
return parents;
}
// int getPlaceholderType(){ // {0 -> constant, 1-> variable}
// return 0;
// }
};
// template<typename T>
// int Variable<T>::count;
template<typename T>
std::ostream& operator<<(std::ostream& os, Variable<T> &obj) {
os << obj.getName_str() << ": " << obj.getValue();
return os;
}
template<typename T>
class Operator : public Node<T>,public DummyNode {
private:
std::vector<Node<T> *> inputs; //it's node * because you can have input as either operator or placeholder
std::vector<Node<T> *> parents;
// Placeholder<T> * output;
T output;
T diff;
// static int operator_index; // Unique identifier of Operator instances, there to make future passes easier.
protected:
int OP_ID;
// int unq_op_id;
public:
bool isComputed=false;
bool isDiffComputed = false;
Operator()
{
std::cout <<"Default C\n";
this->isPlaceholder = false;
// operator_index++;
}
Operator(Node<T> &a, Node<T> &b){
// this->isPlaceholder = false;
std::cout << "New: Node operator\n";
inputs.push_back(&a);
inputs.push_back(&b);
// setOP_ID();
this->isPlaceholder = false;
a.register_parent(this);
b.register_parent(this);
// unq_op_id = operator_index;
// operator_index++;
}
std::vector<Node<T> *> &getInputs(){
return inputs;
}
// void setOpID(int id) {op_id = id;}
T getOutput() {return output;}
T getDiff() {return diff;}
void setOutput(T out){output = out;}
void setDiff(T d){diff = d;}
virtual void setOP_ID() = 0;
int getOP_ID(){
return OP_ID;
}
void register_parent(Node<T> * parent) {
parents.push_back(parent);
}
std::vector<Node<T> *> const getParents() {
return parents;
}
// int get_unq_id(){
// return unq_op_id;
// }
};
template <typename T>
class add: public Operator<T> {
int CHILD_ID = 1;
public:
add(Node<T> &a, Node<T> &b) : Operator<T>(a, b){setOP_ID();std::cout<<"OP:ADD\n";}
T getValue() {
if(this->isComputed){
return this->getOutput();
}
T temp =0;
for(auto curr_input: this->getInputs()) // could be optimized further - the casts would not be needed from the second iteration of the graph
{
temp+= curr_input->getValue();
}
this->isComputed = true;
this->setOutput(temp);
return temp;
}
T diff(int wrt_v){
getValue();
if(this->isDiffComputed) {
return this->getDiff();
}
else{
T temp =0;
for(auto curr_input: this->getInputs()) // could be optimized further - the casts would not be needed from the second iteration of the graph
{
temp+= curr_input->diff(wrt_v);
}
this->isDiffComputed = true;
this->setDiff(temp);
return temp;
}
}
void setOP_ID(){
this->OP_ID = CHILD_ID;
}
};
template <typename T>
class sub: public Operator<T> {
int CHILD_ID = 2;
public:
sub(Node<T> &a, Node<T> &b) : Operator<T>(a, b){setOP_ID();std::cout<<"OP:SUB\n";}
T getValue() {
if(this->isComputed){
return this->getOutput();
}
// T temp =this->getInputs()->at(0)->getValue() - this->getInputs()->at(1)->getValue();
T temp =(this->getInputs()).at(0)->getValue() - (this->getInputs()).at(1)->getValue();
this->isComputed = true;
this->setOutput(temp);
return temp;
}
void setOP_ID(){
this->OP_ID = CHILD_ID;
}
T diff(int wrt_v){
getValue();
if(this->isDiffComputed) {
return this->getDiff();
}
else{
T temp =(this->getInputs()).at(0)->diff(wrt_v) - (this->getInputs()).at(1)->diff(wrt_v);
this->isDiffComputed = true;
this->setDiff(temp);
return temp;
}
}
};
template <typename T>
class mul: public Operator<T> {
int CHILD_ID = 3;
public:
mul(Node<T> &a, Node<T> &b) : Operator<T>(a, b){setOP_ID();std::cout<<"OP:MUL\n";}
T getValue() {
if(this->isComputed){
return this->getOutput();
}
T temp =(this->getInputs()).at(0)->getValue() * (this->getInputs()).at(1)->getValue();
this->isComputed = true;
this->setOutput(temp);
return temp;
}
void setOP_ID(){
this->OP_ID = CHILD_ID;
}
T diff(int wrt_v) {
T curr_val = getValue();
if(this->isDiffComputed) {
return this->getDiff();
}
else{
T temp =(this->getInputs()).at(0)->getValue() * (this->getInputs()).at(1)->diff(wrt_v) + (this->getInputs()).at(1)->getValue() * (this->getInputs()).at(0)->diff(wrt_v) ;
this->isDiffComputed = true;
this->setDiff(temp);
return temp;
}
}
};
template <typename T>
class division: public Operator<T> {
int CHILD_ID = 4;
public:
division(Node<T> &a, Node<T> &b) : Operator<T>(a, b){setOP_ID();std::cout<<"OP:DIV\n";}
T getValue() {
if(this->isComputed){
return this->getOutput();
}
// T temp =this->getInputs()->at(0)->getValue() / this->getInputs()->at(1)->getValue();
T temp =(this->getInputs()).at(0)->getValue() / (this->getInputs()).at(1)->getValue();
this->isComputed = true;
this->setOutput(temp);
return temp;
}
void setOP_ID(){
this->OP_ID = CHILD_ID;
}
T diff(int wrt_v) {
T curr_val = getValue();
if(this->isDiffComputed) {
return this->getDiff();
}
else{
T temp =(this->getInputs()).at(1)->getValue() * (this->getInputs()).at(0)->diff(wrt_v) - (this->getInputs()).at(0)->getValue() * (this->getInputs()).at(1)->diff(wrt_v) ;
temp = temp/((this->getInputs()).at(1)->getValue() * (this->getInputs()).at(1)->getValue());
this->isDiffComputed = true;
this->setDiff(temp);
return temp;
}
}
};
//Operator overloading
// Change this -> this should be lazy evaluation and not eager evaluation
// template <typename T>
// Constant<T> operator-(const Placeholder<T> &a, const Placeholder<T> &b){
// return Constant<T>(a.getValue() - b.getValue());
// }
// template <typename T>
// Constant<T> operator*(const Placeholder<T> &a, const Placeholder<T> &b){
// return Constant<T>(a.getValue() * b.getValue());
//static
// template <typename T>
// Constant<T> operator/(const Placeholder<T> &a, const Placeholder<T> &b){
// return Constant<T>(a.getValue() / b.getValue());
// }
template<typename T>
T DiffEngine(std::vector<Node<T> *>&order, Variable<T> &wrt_v){
std::map<int, T> NodeIDtoDiff;
int idx = wrt_v.get_unq_node_idx();
for(auto curr_node: order) {
curr_node->diff(idx);
}
return (order.back())->diff(idx);
}
template< typename T>
class Expression{
//TODO: The expression class should number the nodes not the node class. This will create confusion between multiple expressions running, or dangling Placeholders.
std::vector<Node<T> *> ordering; // ordering is a topological sorting of the nodes.
std::set<int> visited_nodes;
std::map<int, T> NodeIDtoDiff;
public:
Node<T> *output_node;
Expression () {};
Expression<T> * operator<<=( Node<T> &rhs_node){
std::cout<< " Assignment operator called\n";
output_node = &rhs_node;
topological_sort(*output_node);
return this;
}
void topological_sort(Node<T> &curr_node){
// assert(curr_node.isPlaceholder == false); // Currently output node needs to be an operator -> z = c or z = v do not work. Will handle this seperately.
// std::stack<Node<T> *> op_stack;
if(visited_nodes.find(curr_node.get_unq_node_idx()) == visited_nodes.end()) // If curr_node is not visited
{
visited_nodes.insert(curr_node.get_unq_node_idx());
if(curr_node.isPlaceholder == false){
Operator<T> * curr_op_parent =static_cast<Operator<T> *>(&curr_node);
for(Node<T> *curr_child: curr_op_parent->getInputs())
{
topological_sort(*curr_child);
}
}
ordering.push_back(&curr_node);
}
}
T getValue(){
output_node->getValue();
}
Node<T> &getOutput()
{
return output_node;
}
std::vector<Node<T> *> get_Nodes_Topologically()
{
return ordering;
}
// T diff(Variable<T> &wrt_v) {
// DiffEngine(ordering,wrt_v);
// }
T diff(Variable<T> &wrt_v, std::map<Variable<T> *, T> &new_values) {
//Update the values of Variables.
// struct MapWrapper {
// typedef std::map<Variable<T> &, T>::iterator it;
// }
std::queue<Node<T> *> q;
typename std::template map<Variable<T> *, T>::iterator it;
for( it = new_values.begin() ; it != new_values.end(); it++) {
it->first->setValue(it->second);
q.push(it->first);
}
bool isChanged = true;
while(!q.empty() || isChanged)
{
Node<T> *curr_node = q.front();
q.pop();
for(Node<T> * parent: curr_node->getParents()) {
Operator<T> * temp = static_cast<Operator<T> *>(parent);
bool old_status = temp->isComputed;
if(old_status == true) {
isChanged = true;
temp->isComputed = false;
q.push(parent);
}
else{
isChanged = false;
}
}
if(q.empty() && curr_node->getParents().size() == 0) {
break;
}
}
// }
return DiffEngine(ordering,wrt_v);
std::cout << " Value of wrt_v: " << wrt_v.getValue() << std::endl;
}
void print_top_ordered_dbg() {
std::cout << " Topological ordering of CG nodes\n";
for(auto elem: ordering){
std::cout << elem->get_unq_node_idx() << "\t";
}
std::cout << "\n";
}
};
template <typename T>
Node<T> &operator+( Node<T> &a, Node<T> &b){
add<T> *temp = new add<T>(a,b);
return *temp;
// return Constant<T>(a.getValue() + b.getValue());
}
template <typename T>
Node<T> &operator*( Node<T> &a, Node<T> &b){
mul<T> *temp = new mul<T>(a,b);
return *temp;
// return Constant<T>(a.getValue() + b.getValue());
}
template <typename T>
Node<T> &operator-( Node<T> &a, Node<T> &b){
sub<T> *temp = new sub<T>(a,b);
return *temp;
// return Constant<T>(a.getValue() + b.getValue());
}
template <typename T>
Node<T> &operator/( Node<T> &a, Node<T> &b){
division<T> *temp = new division<T>(a,b);
return *temp;
// return Constant<T>(a.getValue() + b.getValue());
}
// PASSES
//
//
class AD_PASS {
public:
AD_PASS(){}
virtual void runPass() =0;
};
class CSE_PASS: public AD_PASS{
public:
template<typename T>
CSE_PASS(Expression<T> &E){}
};
class FORWARD_PASS: public AD_PASS{
public:
template<typename T>
FORWARD_PASS(Expression<T> &E){}
};
int main(){
Constant<float> c(1);
// Constant<float> d(8);
Variable<float> v(9);
std::cout << "Constant: " << c <<"\n";
std::cout << "Variable: " << v << "\n";
// std::cout << "Addition C+V: " << c+v << "\n";
Expression<float> z;
z <<= c/v;
std::cout << "Expression evaluation: "<< z.getValue() << "\n";
z.print_top_ordered_dbg();
std::map<Variable<float> *, float> m1= {{&v,5}};
std::cout << "Diff testing: " << z.diff(v,m1) << std::endl;
// Placeholder<float> p(c);
// Placeholder<float> x(d);
// std::cout << x.getValue() ;
// sub<float> op_1(p,x);
//
// Operator<float> o_1(x,p);
// std::cout << "Answer of add: " << add<float>(p,x).getValue();
// std::cout << "Subtract of add: " << sub<float>(p,x).getValue();
// std::cout << "Multiplication of add: " << mul<float>(p,x).getValue();
// std::cout << "Division of add: " << division<float>(p,x).getValue();
// std::cout << "Recursive combination: " << add<float>(p,op_1).getValue();
}