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
|
#pragma once
#include "common/config.h"
#include "common/definitions.h"
#include "tensors/backend.h"
#include "tensors/tensor_allocator.h"
#include "graph/chainable.h"
#include "graph/node_initializers.h"
#include "graph/node_operators.h"
#include "graph/parameters.h"
#include <map>
#include <unordered_set>
namespace marian {
template <class T, typename... Args>
Expr Expression(Args&&... args);
class Tensors {
private:
Ptr<TensorAllocator> tensors_;
Ptr<TensorAllocator> cache_;
typedef std::unordered_map<size_t, std::vector<WExpr>> WeakMemory;
typedef std::unordered_map<size_t, std::vector<Expr>> Memory;
Ptr<WeakMemory> shortterm_;
Ptr<Memory> longterm_;
public:
Tensors(Ptr<Backend> backend)
: tensors_(New<TensorAllocator>(backend)),
cache_(New<TensorAllocator>(backend)),
shortterm_(New<WeakMemory>()),
longterm_(New<Memory>()) {}
Tensors(Ptr<Backend> backend, Ptr<Device> device)
: tensors_(New<TensorAllocator>(backend, device)),
cache_(New<TensorAllocator>(backend)),
shortterm_(New<WeakMemory>()),
longterm_(New<Memory>()) {}
void reserve(size_t bytes) { tensors_->reserve(bytes); }
void throwAtReallocation(bool throwAtRealloc) {
tensors_->throwAtReallocation(throwAtRealloc);
}
void allocateForward(Expr node) {
if(!node->val()) {
if(node->memoize())
cache_->allocate(node->val(), node->shape(), node->value_type());
else
tensors_->allocate(node->val(), node->shape(), node->value_type());
}
}
void allocateBackward(Expr node) {
if(!node->grad())
tensors_->allocate(node->grad(), node->shape(), node->value_type());
}
void free(const Tensor& tensor) { tensors_->free(tensor); }
// @TODO: get rid of this, not really used or can be done better
Ptr<Allocator> allocator() { return tensors_->allocator(); }
Expr findOrRemember(Expr node) {
size_t hash = node->hash();
// memoize constant nodes that are not parameters
// parameters are already memoized in the graph itself
if(node->type() != "param" && node->memoize()) {
auto it = longterm_->find(hash);
if(it != longterm_->end()) {
for(auto found : it->second) {
return found;
// @TODO: check why below code does not work for certain nodes and
// autotuning.
// if(node->equal(found)) {
// std::cerr << "found memoized" << std::endl;
// return found;
//}
}
}
(*longterm_)[hash].push_back(node);
}
auto it = shortterm_->find(hash);
if(it != shortterm_->end()) {
for(auto foundWeak : it->second) {
auto found = foundWeak.lock();
if(node->equal(found)) {
return found;
}
}
}
(*shortterm_)[hash].push_back(node);
return nullptr;
}
void clear() {
tensors_->clear();
shortterm_->clear();
}
void clearShorttermMemory() { shortterm_->clear(); }
void clearLongtermMemory() { longterm_->clear(); }
};
class ExpressionGraph : public std::enable_shared_from_this<ExpressionGraph> {
private:
size_t count_{0};
std::list<Expr> nodesForward_;
std::list<Expr> nodesBackward_;
std::unordered_set<Expr> topNodes_; // current set of roots. In the end, all but one must have been consumed.
// Holds memory and expressions that correspond to graph parameters
Ptr<Parameters> params_;
// Holds memory and expressions that correspond to temporary expressions.
// This gets cleared before a new graph is built.
Ptr<Tensors> tensors_;
std::unordered_map<size_t, std::vector<Expr>> memoized_;
bool inferenceOnly_{false};
bool optimized_{false};
Ptr<Backend> backend_;
bool reloaded_{false};
std::string namespace_;
bool throwNaN_{false};
protected:
// Delete, copy and move constructors
ExpressionGraph(const ExpressionGraph&) = delete;
ExpressionGraph(ExpressionGraph&&) = delete;
public:
/** @brief Constructs a new expression graph
*
* Constructor should be used as New<ExpressionGraph>()
*/
ExpressionGraph(bool inference = false, bool optimized = false);
void setInference(bool inference) { inferenceOnly_ = inference; }
bool isInference() { return inferenceOnly_; }
~ExpressionGraph() {
clear();
params_->clear();
}
void setDevice(DeviceId deviceId = {0, DeviceType::gpu},
Ptr<Device> device = nullptr);
DeviceId getDeviceId() { return backend_->getDeviceId(); }
Ptr<Backend> getBackend() { return backend_; }
void setOptimized(bool optimized) { optimized_ = optimized; }
bool isOptimized() { return (optimized_ && inferenceOnly_); }
void switchParams(const std::string& newNamespace) {
namespace_ = newNamespace;
}
void copyParams(Ptr<ExpressionGraph> graph) {
for(auto p : *graph->params())
param(p->name(), p->shape(), inits::dummy);
params()->allocateForward();
params()->vals()->copyFrom(graph->params()->vals());
}
void reserveWorkspaceMB(size_t num) {
size_t bytes = num * 1024 * 1024 - 1;
tensors_->reserve(bytes);
}
void reuseWorkspace(Ptr<ExpressionGraph> graph) {
tensors_ = graph->tensors_;
}
/**
* @brief Performs backpropogation on this expression graph.
*
* Backpropogation is implemented by performing first the forward pass and
* then the backward pass of algorithmic differentiation (AD) on the nodes of
* the graph.
*/
void backprop() {
forward();
backward();
}
bool fits() {
try {
tensors_->throwAtReallocation(true);
backprop();
tensors_->throwAtReallocation(false);
} catch(AllocationException&) {
tensors_->throwAtReallocation(false);
return false;
}
return true;
}
void forward() {
params_->allocateForward();
forwardNext();
}
void checkNan(Tensor t);
void forwardNext() {
// @TODO: check if allocation works properly
tensors_->clearShorttermMemory();
while(!nodesForward_.empty()) {
auto v = nodesForward_.front();
v->allocate();
v->init();
v->forward();
checkNan(v->val());
if(v->marked_for_debug()) {
std::cerr << "Debug: " << v->debug_message() << " op=" << v->type()
<< std::endl;
std::cerr << v->val()->debug() << std::endl;
}
if(inferenceOnly_)
v->children().clear();
nodesForward_.pop_front();
}
}
void backward(bool zero = true) {
if(topNodes_.size() > 1) {
LOG(critical, "There are more ({}) than one top most node for backward step:", topNodes_.size());
for(auto node : topNodes_) {
LOG(critical,
"\tType: {}, Shape: {}, Name: {}, Id: {}, Hash: {}",
node->type(),
node->shape(),
node->name(),
node->getId(),
node->hash());
}
ABORT("Aborting");
}
params_->allocateBackward();
if(zero)
params_->set_zero_adjoint();
for(auto&& v : topNodes_)
v->init_dependent();
// named_.clear();
topNodes_.clear();
tensors_->clearShorttermMemory();
while(!nodesBackward_.empty()) {
auto v = nodesBackward_.back();
nodesBackward_.pop_back();
for(auto&& child : v->children()) {
if(child->trainable() && child->type() != "param")
child->set_zero_adjoint();
}
if(v->trainable())
v->backward();
checkNan(v->grad());
if(v->trainable() && v->marked_for_debug()) {
std::cerr << "Debug Grad: " << v->debug_message() << std::endl;
std::cerr << v->grad()->debug() << std::endl;
}
v->children().clear();
}
}
std::string graphviz() {
std::stringstream ss;
ss << "digraph ExpressionGraph {" << std::endl;
// ss << "graph[splines=ortho]" << std::endl;
ss << "rankdir=LR" << std::endl;
auto it = nodesForward_.rbegin();
while(it != nodesForward_.rend()) {
auto v = *it;
ss << v->graphviz();
it++;
}
ss << "}" << std::endl;
return ss.str();
}
void graphviz(const std::string& filename) {
std::ofstream dot(filename);
dot << graphviz();
dot.close();
}
Expr param(const std::string& pname,
const Shape& shape,
const NodeInitializer& init,
bool fixed = false) {
std::string name = pname;
if(!namespace_.empty())
name = namespace_ + "::" + name;
// check first if parameter already exists
auto p = params_->get(name);
if(p) {
// if yes add to tape and return
ABORT_IF(shape != p->shape(),
"Requested shape {} for existing parameter '{}' does not match "
"original shape {}",
shape,
name,
p->shape());
p->setTrainable(!fixed);
add(p);
return p;
}
// if graph was reloaded do not allow creation of new parameters
ABORT_IF(reloaded_,
"Graph was reloaded and parameter '{}' is newly created",
name);
// if not check if name is not taken by other node
ABORT_IF(get(name), "Non-parameter with name '{}' already exists", name);
// create parameter node (adds to tape)
p = Expression<ParamNode>(shared_from_this(), shape, init, fixed);
// set name and id and add to list of parameters
p->set_name(name);
params_->add(p, name);
return p;
}
Expr constant(const Shape& shape, const NodeInitializer& init, Type value_type = Type::float32) {
return Expression<ConstantNode>(shared_from_this(), shape, init, value_type);
}
// @TODO: add version with iterators
// shortcut to turn vector of indices to integer tensor, to be used with operators
// like rows or select
Expr indices(const std::vector<IndexType>& indicesVector) {
return constant({(int)indicesVector.size()},
inits::from_vector(indicesVector),
Type::uint32);
}
// this version sets up the shape such that the indices are in a given axis
// Use this if you want to pass these indices to gather().
// indexee shape = (3, 2, 5, 2); axis = 1 -> resulting shape = (1, size of indicesVector, 1, 1)
Expr indices(const std::vector<IndexType>& indicesVector, Expr indexee, int axis = -1) {
Shape shape;
shape.resize(indexee->shape().size());
shape.set(axis, indicesVector.size());
return constant(Shape(shape),
inits::from_vector(indicesVector),
Type::uint32);
}
Expr ones(const Shape& shape) {
return constant(shape, inits::ones);
}
Expr zeros(const Shape& shape) {
return constant(shape, inits::zeros);
}
// prob = dropProb, e.g. 0.1 means 90% of values are kept
Expr dropout(float dropProb, const Shape& shape);
Expr get(std::string name) {
if(!namespace_.empty())
name = namespace_ + "::" + name;
auto e = params_->get(name);
if(e)
return e;
return Expr();
}
Ptr<Parameters>& params() { return params_; }
Expr add(Expr node) {
auto found = tensors_->findOrRemember(node);
if(found) {
return found;
} else {
node->setId(count_++);
// record in foward graph
nodesForward_.push_back(node);
// record in backward graph if training, and keep track of roots
if(!inferenceOnly_ && node->trainable()) {
nodesBackward_.push_back(node);
topNodes_.insert(node); // opportunistically record all new nodes as roots (gets removed once consumed)
}
for(auto child : node->children())
topNodes_.erase(child); // this child is consumed and therefore not a root
return node;
}
}
void allocateForward(Expr node) {
if(tensors_)
tensors_->allocateForward(node);
}
void allocateBackward(Expr node) {
if(tensors_)
tensors_->allocateBackward(node);
}
void free(const Tensor& tensor) {
if(tensors_)
tensors_->free(tensor);
}
// @TODO: get rid of this, not really used or can be done better
Ptr<Allocator> allocator() { return tensors_->allocator(); }
void clear() {
// clear everything apart from parameters and memoized nodes
count_ = 0;
nodesForward_.clear();
nodesBackward_.clear();
topNodes_.clear();
tensors_->clear();
}
void clearParameters() { params_->clear(); }
void setReloaded(bool reloaded) { reloaded_ = reloaded; }
void setThrowNaN(bool throwNaN) { throwNaN_ = throwNaN; }
public:
// convert all parameters into an array of IoItem elements, for loading
void load(const std::vector<io::Item>& ioItems, bool markReloaded = true) {
setReloaded(false);
for(auto& item : ioItems) {
std::string pName = item.name;
// skip over special parameters starting with "special:"
if(pName.substr(0, 8) == "special:")
continue;
param(pName, item.shape, inits::from_item(item));
}
if(markReloaded)
setReloaded(true);
}
void load(const std::string& name, bool markReloaded = true) {
LOG(info, "Loading model from {}", name);
load(io::loadItems(name), markReloaded);
}
void load(const void* ptr, bool markReloaded = true) {
LOG(info, "Loading model from buffer at {}", ptr);
load(io::loadItems(ptr), markReloaded);
}
void mmap(const void* ptr, bool markReloaded = true) {
ABORT_IF(backend_->getDeviceId().type != DeviceType::cpu || !inferenceOnly_,
"Memory mapping only supported for CPU inference mode");
params_ = New<MappedParameters>();
params_->init(backend_);
LOG(info, "Memory mapping model at {}", ptr);
load(io::mmapItems(ptr), markReloaded);
}
public:
// convert all parameters into an array of io::Item elements, for saving
void save(std::vector<io::Item>& ioItems);
void save(const std::string& name, const std::string& meta = "") {
// LOG(info, "Saving model to {}", name);
std::vector<io::Item> ioItems;
save(ioItems);
if(!meta.empty())
io::addMetaToItems(meta, "special:model.yml", ioItems);
io::saveItems(name, ioItems);
// LOG(info, "Saved {} items.", ioItems.size());
}
};
template <class T, typename... Args>
Expr Expression(Args&&... args) {
auto e = Expr(new T(std::forward<Args>(args)...));
return e->graph()->add(e);
}
} // namespace marian
|