forked from marian-nmt/marian-dev
/
node.h
261 lines (195 loc) · 7.42 KB
/
node.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
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
#pragma once
#include <iostream>
#include <memory>
#include <thread>
#include "common/hash.h"
#include "tensors/backend.h"
#include "tensors/tensor.h"
#include "graph/chainable.h"
namespace marian {
/**
* Main node class for computation graph,
* implements most common functions demanded by Chainable.
* Each operation in a computation graph is a node.
*/
class Node : public Chainable<Tensor> {
protected:
size_t id_{0};
size_t edges_{0};
bool trainable_{true};
bool destroy_{true};
bool memoize_{false};
std::vector<Expr> children_;
Weak<ExpressionGraph> graph_;
Shape shape_{1, 1, 1, 1};
Type valueType_{Type::float32};
std::string name_{"none"};
Tensor val_{nullptr};
Tensor adj_{nullptr};
bool markedForDebug_{false};
std::string debugMessage_;
Ptr<std::list<Expr>> subtape_; // a subtape is used to keep track of nodes that need to be freed and recomputed with gradient-checkpointing.
bool isCheckpoint_{false}; // true if this node has been selected to be a checkpoint, currently only done manually.
Ptr<AutoTunerRecorder> recorder_;
size_t recorderHash_;
bool recorderStop_;
public:
Node(Ptr<ExpressionGraph> graph, const Shape& shape, const Type& valueType = Type::float32)
: graph_(graph), shape_(shape), valueType_(valueType) {}
virtual ~Node() {
free();
}
virtual float scalar() override;
virtual NodeOps forwardOps() override { return {}; };
virtual NodeOps backwardOps() override { return {}; };
virtual void runForward(const NodeOps& ops) {
for(auto&& op : ops)
op();
}
virtual void runBackward(const NodeOps& ops) {
size_t i = 0;
for(auto&& op : ops)
if(child(i++)->trainable())
op();
}
virtual void forward() override;
virtual void backward() override;
virtual bool trainable() override { return trainable_; }
virtual void setTrainable(bool trainable) override { trainable_ = trainable; }
virtual bool memoize() override { return memoize_; };
virtual void setMemoize(bool memoize) override { memoize_ = memoize; };
virtual void setId(size_t id) override { id_ = id; }
virtual size_t getId() override { return id_; }
virtual void increaseEdges(size_t edges = 1) { edges_ += edges; };
virtual void decreaseEdges(size_t edges = 1) { edges_ -= edges; };
virtual size_t edges() { return edges_; };
virtual Ptr<ExpressionGraph> graph() override { return graph_.lock(); }
virtual void debug(const std::string& message) override {
debugMessage_ = message;
markedForDebug_ = true;
}
virtual bool marked_for_debug() override { return markedForDebug_; }
virtual const std::string& debug_message() override { return debugMessage_; }
virtual void allocate() override;
virtual void free() override;
virtual void init() override {};
virtual void init_dependent() override;
virtual void set_zero_adjoint() override;
virtual Tensor& val() override { return val_; };
virtual Tensor& grad() override { return adj_; };
virtual const Shape& shape() override { return shape_; }
virtual const Type& value_type() override { return valueType_; }
void set_name(const std::string& name) override { name_ = name; }
const std::string& name() const override { return name_; }
virtual const std::string form() override { return "box"; }
virtual const std::string color() override { return "orange"; }
virtual const std::string label() override {
std::stringstream label;
label << "<" << type();
if(name_ != "none") {
label << "<br/>"
<< "\"" << name_ << "\"";
}
label << " (" << getId() << "/" << trainable() << ")>";
return label.str();
}
virtual std::string graphviz() override {
std::stringstream ss;
ss << "\"" << this << "\" ["
<< "shape=\"" << form() << "\", "
<< "label=" << label() << ", "
<< "style=\"filled\", "
<< (isCheckpoint_ ? "penwidth=3, " : "penwidth=1, ")
<< "fillcolor=\"" << color() << "\"];" << std::endl;
for(auto&& child : children())
ss << "\"" << child << "\" -> \"" << this << "\";" << std::endl;
if(subtape_) {
for(auto&& dep : *subtape_)
ss << "\"" << dep << "\" -> \"" << this << "\" [style=dotted];" << std::endl;
}
ss << std::endl;
return ss.str();
}
virtual std::vector<Expr>& children() override { return children_; }
virtual Expr child(size_t i) override { return children_[i]; }
Ptr<Backend> getBackend();
void record(Ptr<AutoTunerRecorder>, size_t, bool) override;
// this is currently only called manually by checkpoint(Expr). In the future we will figure out a general algorithm
virtual void markCheckpoint() override {
isCheckpoint_ = true;
}
virtual bool isCheckpoint() const override {
return (children_.empty() || isCheckpoint_); // this node is a checkPoint if it's a leaf or if it has been marked.
}
virtual void setSubtape(Ptr<std::list<Expr>> subtape) override {
subtape_ = subtape;
}
virtual Ptr<std::list<Expr>> getSubtape() override {
return subtape_;
};
};
struct NaryNodeOp : public Node {
size_t hash_{0};
// Deduce type automatically, but then all types must be the same
// this is called automatically when no output type is specified.
// If the input types are mixed, the output type needs to be specified
// in the constructor.
static Type commonType(const std::vector<Expr>& nodes) {
ABORT_IF(nodes.size() == 0, "NaryNodeOp has no children");
Type type = nodes[0]->value_type();
for(int i = 1; i < nodes.size(); ++i)
ABORT_IF(nodes[i]->value_type() != type,
"Child {} has different type (first: {} != child: {})",
i, type, nodes[i]->value_type());
return type;
}
NaryNodeOp(const std::vector<Expr>& nodes)
: NaryNodeOp(nodes, nodes[0]->shape()) {}
// this contructor will try to deduce the node type automatically
NaryNodeOp(const std::vector<Expr>& nodes, Shape shape)
: NaryNodeOp(nodes, shape, commonType(nodes)) {}
// this contructor will takes a node type
NaryNodeOp(const std::vector<Expr>& nodes,
Shape shape,
Type value_type)
: Node(nodes.front()->graph(), shape, value_type) {
children_.resize(nodes.size());
for(size_t i = 0; i < nodes.size(); ++i)
children_[i] = nodes[i];
setTrainable(std::any_of(
nodes.begin(), nodes.end(), [](Expr a) { return a->trainable(); }));
// Node is to be memoized if all children are to be memoized.
setMemoize(std::all_of(
nodes.begin(), nodes.end(), [](Expr a) { return a->memoize(); }));
}
virtual ~NaryNodeOp() {}
std::vector<Expr>& children() override { return children_; }
virtual size_t hash() override {
if(!hash_) {
std::size_t seed = util::hash<std::string>()(name());
util::hash_combine(seed, type());
util::hash_combine(seed, (size_t)value_type());
for(size_t i = 0; i < children_.size(); ++i)
util::hash_combine(seed, child(i)->hash());
hash_ = seed;
}
return hash_;
}
virtual bool equal(Expr node) override {
if(type() != node->type())
return false;
else if(name() != node->name())
return false;
else if(value_type() != node->value_type())
return false;
else if(children().size() != node->children().size())
return false;
else {
for(size_t i = 0; i < children().size(); ++i)
if(children()[i]->getId() != node->children()[i]->getId())
return false;
return true;
}
}
};
} // namespace marian