-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_object_iterator.cpp
165 lines (97 loc) · 5.66 KB
/
function_object_iterator.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
#include "function_object_iterator.h"
#include "tensor.h"
#include "generator.h"
namespace NeuralNetwork {
namespace Computation {
namespace Graph {
void ReadParameterPolicy::process_head(TensorID tid) {}
void ReadParameterPolicy::apply_to_children(std::stack<TensorID>& nodeStack, TensorID tid) {
nodeStack.emplace(tid);
}
ReadParameterPolicy::ReturnType ReadParameterPolicy::dereference(TensorID tid) {
ComputationalGraphMap& map = ComputationalGraphMap::get();
return map._get_tensor(tid)->release_matrix();
}
ReadParameterPolicy::Matrix_t ReadParameterPolicy::grad(TensorID tid) {
ComputationalGraphMap& map = ComputationalGraphMap::get();
auto gradient = Matrix_t{map._get_tensor(tid)->get_grad()};
return Matrix_t{gradient};
}
void ComputeGradientPolicy::process_head(TensorID tid) {
// std::cout << "Backpropigating TID: " << _t.get() << std::endl;
ComputationalGraphMap& map = ComputationalGraphMap::get();
auto operation = map._get_operation(tid);
// auto _r = map._get_tensor(current)->release_matrix().num_rows();
// auto _w = map._get_tensor(current)->release_matrix().num_cols();
// auto matrix = Matrix_t{
// Matrix::Rows(_r),
// Matrix::Columns(_w)
// };
// Matrix::Generation::Tester<1> unit_gen;
// matrix = unit_gen(matrix);
// Events::Differentiate backpropigate_grad(matrix);
Events::Differentiate backpropigate_grad(Matrix_t{});
operation.stringify_type();
std::cout << "Computing Leaf Derivative" << std::endl;
operation.process_event(backpropigate_grad);
operation.stringify_type();
}
void ComputeGradientPolicy::apply_to_children(std::stack<TensorID>& nodeStack, TensorID tid) {
ComputationalGraphMap& map = ComputationalGraphMap::get();
auto df = Matrix_t{map._get_tensor(tid)->get_grad()};
assert(df.num_rows() && df.num_cols() && "Invalid Derivative.");
std::cout << "Gradient DIM: [" << df.num_rows() << "," << df.num_cols() << "]" << std::endl;
Events::Differentiate backpropigate_grad(df);
auto operation = map._get_operation(tid);
operation.stringify_type();
std::cout << "Processing event:" << std::endl;
operation.process_event(backpropigate_grad);
operation.stringify_type();
nodeStack.emplace(tid);
}
ComputeGradientPolicy::ReturnType ComputeGradientPolicy::dereference(TensorID tid) {
ComputationalGraphMap& map = ComputationalGraphMap::get();
FunctionObject fn_obj = map._get_operation(tid);
return fn_obj;
}
template <TraversalPolicy TP>
LevelOrderIterator<TP>::LevelOrderIterator(const TensorID _t) noexcept : current(_t) {
if (_t.get()) {
TP::process_head(_t);
this->_stack_children();
}
}
template <TraversalPolicy TP>
LevelOrderIterator<TP>& LevelOrderIterator<TP>::operator++(void) noexcept {
if (!nodeStack.empty()) {
current = nodeStack.top();
nodeStack.pop();
this->_stack_children();
}
else current = TensorID(0);
return *this;
}
template <TraversalPolicy TP>
typename LevelOrderIterator<TP>::IterReturnType LevelOrderIterator<TP>::operator*() const noexcept {
return TP::dereference(current);
}
template <TraversalPolicy TP>
void LevelOrderIterator<TP>::_stack_children(void) noexcept {
ComputationalGraphMap& map = ComputationalGraphMap::get();
FunctionObject fn_obj = map._get_operation(current);
fn_obj.stringify_type();
for (std::size_t i = 0; const auto tid: fn_obj.serialize()) {
if (tid) {
std::cout << "Backpropigating TID " << i << ": " << tid->get() << std::endl;
if (i++) {
TP::apply_to_children(nodeStack, TensorID(tid->get()));
}
}
}
return;
}
template class LevelOrderIterator<ReadParameterPolicy>;
template class LevelOrderIterator<ComputeGradientPolicy>;
} // Graph
} // Computation
} // NN