forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.cc
More file actions
2572 lines (2278 loc) · 93 KB
/
Copy pathexecutor.cc
File metadata and controls
2572 lines (2278 loc) · 93 KB
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/core/common_runtime/executor.h"
#include <atomic>
#include <deque>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "tensorflow/core/common_runtime/costmodel_manager.h"
#include "tensorflow/core/common_runtime/pending_counts.h"
#include "tensorflow/core/common_runtime/step_stats_collector.h"
#include "tensorflow/core/framework/allocation_description.pb.h"
#include "tensorflow/core/framework/allocator.h"
#include "tensorflow/core/framework/cancellation.h"
#include "tensorflow/core/framework/control_flow.h"
#include "tensorflow/core/framework/device_attributes.pb.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/log_memory.h"
#include "tensorflow/core/framework/node_def_util.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/op_segment.h"
#include "tensorflow/core/framework/step_stats.pb.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_reference.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/framework/types.pb.h"
#include "tensorflow/core/graph/edgeset.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/notification.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/gtl/flatmap.h"
#include "tensorflow/core/lib/gtl/flatset.h"
#include "tensorflow/core/lib/gtl/inlined_vector.h"
#include "tensorflow/core/lib/gtl/manual_constructor.h"
#include "tensorflow/core/lib/gtl/stl_util.h"
#include "tensorflow/core/lib/hash/hash.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/thread_annotations.h"
#include "tensorflow/core/platform/tracing.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/util/tensor_slice_reader_cache.h"
namespace tensorflow {
namespace {
// 1-D, 0 element tensor.
static const Tensor* const kEmptyTensor = new Tensor;
bool IsInitializationOp(const Node* node) {
return node->op_def().allows_uninitialized_input();
}
// Sets the timeline_label field of *node_stats, using data from *node.
// Returns true iff the node is a transfer node.
// TODO(tucker): merge with the DetailText function in session.cc
// in a common location.
bool SetTimelineLabel(const Node* node, NodeExecStats* node_stats) {
bool is_transfer_node = false;
string memory;
for (auto& all : node_stats->memory()) {
int64 tot = all.total_bytes();
if (tot >= 0.1 * 1048576.0) {
int64 peak = all.peak_bytes();
if (peak > 0) {
memory =
strings::StrCat(memory, "[", all.allocator_name(),
strings::Printf(" %.1fMB %.1fMB] ", tot / 1048576.0,
peak / 1048576.0));
} else {
memory = strings::StrCat(memory, "[", all.allocator_name(),
strings::Printf(" %.1fMB] ", tot / 1048576.0));
}
}
}
const NodeDef& def = node->def();
string text = "";
if (IsSend(node)) {
string tensor_name;
TF_CHECK_OK(GetNodeAttr(def, "tensor_name", &tensor_name));
string recv_device;
TF_CHECK_OK(GetNodeAttr(def, "recv_device", &recv_device));
text = strings::StrCat(memory, def.name(), " = ", def.op(), "(",
tensor_name, " @", recv_device);
is_transfer_node = true;
} else if (IsRecv(node)) {
string tensor_name;
TF_CHECK_OK(GetNodeAttr(def, "tensor_name", &tensor_name));
string send_device;
TF_CHECK_OK(GetNodeAttr(def, "send_device", &send_device));
text = strings::StrCat(memory, def.name(), " = ", def.op(), "(",
tensor_name, " @", send_device);
is_transfer_node = true;
} else {
text = strings::StrCat(
memory, def.name(), " = ", def.op(), "(",
str_util::Join(
std::vector<StringPiece>(def.input().begin(), def.input().end()),
", "),
")");
}
node_stats->set_timeline_label(text);
return is_transfer_node;
}
// Helper routines for collecting step stats.
namespace nodestats {
inline int64 NowInUsec() { return Env::Default()->NowMicros(); }
void SetScheduled(NodeExecStats* nt, int64 t) { nt->set_scheduled_micros(t); }
void SetAllStart(NodeExecStats* nt) { nt->set_all_start_micros(NowInUsec()); }
void SetOpStart(NodeExecStats* nt) {
DCHECK_NE(nt->all_start_micros(), 0);
nt->set_op_start_rel_micros(NowInUsec() - nt->all_start_micros());
}
void SetOpEnd(NodeExecStats* nt) {
DCHECK_NE(nt->all_start_micros(), 0);
nt->set_op_end_rel_micros(NowInUsec() - nt->all_start_micros());
}
void SetAllEnd(NodeExecStats* nt) {
DCHECK_NE(nt->all_start_micros(), 0);
nt->set_all_end_rel_micros(NowInUsec() - nt->all_start_micros());
}
void SetOutput(NodeExecStats* nt, int slot, const Tensor* v) {
DCHECK(v);
NodeOutput* no = nt->add_output();
no->set_slot(slot);
v->FillDescription(no->mutable_tensor_description());
}
void SetMemory(NodeExecStats* nt, OpKernelContext* ctx) {
for (const auto& allocator_pair : ctx->wrapped_allocators()) {
AllocatorMemoryUsed* memory = nt->add_memory();
// retrieving the sizes from the wrapped allocator removes the
// executor's reference to it, so allocator_pair.second must not
// be dereferenced again after this statement
auto sizes = allocator_pair.second->GetSizesAndUnRef();
memory->set_allocator_name(allocator_pair.first->Name());
memory->set_total_bytes(std::get<0>(sizes));
if (allocator_pair.first->TracksAllocationSizes()) {
memory->set_peak_bytes(std::get<1>(sizes));
memory->set_live_bytes(std::get<2>(sizes));
}
}
}
void SetReferencedTensors(NodeExecStats* nt,
const TensorReferenceVector& tensors) {
// be careful not to increment the reference count on any tensor
// while recording the information
for (size_t i = 0; i < tensors.size(); ++i) {
AllocationDescription* description = nt->add_referenced_tensor();
tensors.at(i).FillDescription(description);
}
}
} // namespace nodestats
class ExecutorImpl;
class GraphView;
struct EdgeInfo {
int dst_id;
int16 output_slot;
// true if this is the last info for output_slot in the EdgeInfo list.
bool is_last;
int input_slot;
};
struct NodeItem {
NodeItem() {}
// A graph node.
const Node* node = nullptr;
// The kernel for this node.
OpKernel* kernel = nullptr;
bool kernel_is_expensive : 1; // True iff kernel->IsExpensive()
bool kernel_is_async : 1; // True iff kernel->AsAsync() != nullptr
bool is_merge : 1; // True iff IsMerge(node)
bool is_enter : 1; // True iff IsEnter(node)
bool is_exit : 1; // True iff IsExit(node)
bool is_control_trigger : 1; // True iff IsControlTrigger(node)
bool is_sink : 1; // True iff IsSink(node)
// True iff IsEnter(node) || IsExit(node) || IsNextIteration(node)
bool is_enter_exit_or_next_iter : 1;
// Cached values of node->num_inputs() and node->num_outputs(), to
// avoid levels of indirection.
int num_inputs;
int num_outputs;
// ExecutorImpl::tensors_[input_start] is the 1st positional input
// for this node.
int input_start = 0;
// Number of output edges.
int num_output_edges;
PendingCounts::Handle pending_id;
const EdgeInfo* output_edge_list() const { return output_edge_base(); }
// ith output edge.
const EdgeInfo& output_edge(int i) const {
DCHECK_GE(i, 0);
DCHECK_LT(i, num_output_edges);
return output_edge_base()[i];
}
DataType input_type(int i) const {
DCHECK_LT(i, num_inputs);
return static_cast<DataType>(input_type_base()[i]);
}
DataType output_type(int i) const {
DCHECK_LT(i, num_outputs);
return static_cast<DataType>(output_type_base()[i]);
}
// Return array of per-output allocator attributes.
const AllocatorAttributes* output_attrs() const { return output_attr_base(); }
private:
friend class GraphView;
// Variable length section starts immediately after *this
// (uint8 is enough for DataType).
// EdgeInfo out_edges[num_out_edges];
// AllocatorAttributes output_attr[num_outputs];
// uint8 input_type[num_inputs];
// uint8 output_type[num_outputs];
// Return pointer to variable length section.
char* var() const {
return const_cast<char*>(reinterpret_cast<const char*>(this) +
sizeof(NodeItem));
}
EdgeInfo* output_edge_base() const {
return reinterpret_cast<EdgeInfo*>(var());
}
AllocatorAttributes* output_attr_base() const {
return reinterpret_cast<AllocatorAttributes*>(var() + sizeof(EdgeInfo) *
num_output_edges);
}
uint8* input_type_base() const {
return reinterpret_cast<uint8*>(var() +
sizeof(EdgeInfo) * num_output_edges +
sizeof(AllocatorAttributes) * num_outputs);
}
uint8* output_type_base() const {
return reinterpret_cast<uint8*>(
var() + sizeof(EdgeInfo) * num_output_edges +
sizeof(AllocatorAttributes) * num_outputs + sizeof(uint8) * num_inputs);
}
TF_DISALLOW_COPY_AND_ASSIGN(NodeItem);
};
typedef gtl::InlinedVector<TensorValue, 4> TensorValueVec;
typedef gtl::InlinedVector<DeviceContext*, 4> DeviceContextVec;
typedef gtl::InlinedVector<AllocatorAttributes, 4> AllocatorAttributeVec;
// Immutable view of a Graph organized for efficient execution.
class GraphView {
public:
GraphView() : space_(nullptr) {}
~GraphView();
void Initialize(const Graph* g);
Status SetAllocAttrs(const Graph* g, const Device* device);
NodeItem* node(int id) const {
DCHECK_GE(id, 0);
DCHECK_LT(id, num_nodes_);
uint32 offset = node_offsets_[id];
return ((offset == kuint32max)
? nullptr
: reinterpret_cast<NodeItem*>(space_ + node_offsets_[id]));
}
private:
char* InitializeNode(char* ptr, const Node* n);
size_t NodeItemBytes(const Node* n);
int32 num_nodes_ = 0;
uint32* node_offsets_ = nullptr; // array of size "graph_.num_node_ids()"
// node_offsets_[id] holds the byte offset for node w/ "id" in space_
char* space_; // NodeItem objects are allocated here
TF_DISALLOW_COPY_AND_ASSIGN(GraphView);
};
class ExecutorImpl : public Executor {
public:
ExecutorImpl(const LocalExecutorParams& p, const Graph* g)
: params_(p), graph_(g), gview_() {
CHECK(p.create_kernel != nullptr);
CHECK(p.delete_kernel != nullptr);
}
~ExecutorImpl() override {
for (int i = 0; i < graph_->num_node_ids(); i++) {
NodeItem* item = gview_.node(i);
if (item != nullptr) {
params_.delete_kernel(item->kernel);
}
}
for (auto fiter : frame_info_) {
delete fiter.second;
}
delete graph_;
}
Status Initialize();
// Process all Nodes in the current graph, attempting to infer the
// memory allocation attributes to be used wherever they may allocate
// a tensor buffer.
Status SetAllocAttrs();
void RunAsync(const Args& args, DoneCallback done) override;
private:
friend class ExecutorState;
struct ControlFlowInfo {
gtl::FlatSet<string, HashStr> unique_frame_names;
std::vector<string> frame_names;
};
struct FrameInfo {
FrameInfo()
: input_count(0),
total_inputs(0),
pending_counts(nullptr),
nodes(nullptr) {}
// The total number of inputs to a frame.
int input_count;
// The total number of input tensors of a frame.
// == sum(nodes[*].num_inputs()) where nodes are the nodes in the frame.
int total_inputs;
// Used to determine the next place to allocate space in the
// pending_counts data structure we'll eventually construct
PendingCounts::Layout pending_counts_layout;
// Each frame has its own PendingCounts only for the nodes in the frame.
PendingCounts* pending_counts; // Owned
// The nodes in a frame. Used only for debugging.
std::vector<const Node*>* nodes; // Owned
~FrameInfo() {
delete pending_counts;
delete nodes;
}
};
static Status BuildControlFlowInfo(const Graph* graph,
ControlFlowInfo* cf_info);
void InitializePending(const Graph* graph, const ControlFlowInfo& cf_info);
FrameInfo* EnsureFrameInfo(const string& fname) {
auto slot = &frame_info_[fname];
if (*slot == nullptr) {
*slot = new FrameInfo;
}
return *slot;
}
// Owned.
LocalExecutorParams params_;
const Graph* graph_;
GraphView gview_;
// A cached value of params_
bool device_record_tensor_accesses_ = false;
// Root nodes (with no in edges) that should form the initial ready queue
std::vector<const Node*> root_nodes_;
// Mapping from frame name to static information about the frame.
// TODO(yuanbyu): We could cache it along with the graph so to avoid
// the overhead of constructing it for each executor instance.
gtl::FlatMap<string, FrameInfo*, HashStr> frame_info_;
TF_DISALLOW_COPY_AND_ASSIGN(ExecutorImpl);
};
// Infer memory allocation attributes of a node n's output,
// based on its use node dst. Note that dst might not be directly
// connected to n by a single edge, but might be a downstream
// consumer of n's output by reference. *attr is updated with any
// necessary attributes.
static Status InferAllocAttr(const Node* n, const Node* dst,
const DeviceNameUtils::ParsedName& local_dev_name,
AllocatorAttributes* attr);
GraphView::~GraphView() {
static_assert(std::is_trivially_destructible<AllocatorAttributes>::value,
"Update code if AllocatorAttributes gains a destructor");
static_assert(std::is_trivially_destructible<EdgeInfo>::value,
"Update code if EdgeInfo gains a destructor");
for (int i = 0; i < num_nodes_; i++) {
NodeItem* n = node(i);
if (n != nullptr) {
n->NodeItem::~NodeItem();
// Memory for "n" itself is held in space_ & gets cleaned up below
}
}
delete[] node_offsets_;
delete[] space_;
}
size_t GraphView::NodeItemBytes(const Node* n) {
const int num_output_edges = n->out_edges().size();
const int num_inputs = n->num_inputs();
const int num_outputs = n->num_outputs();
// Compute number of bytes needed for NodeItem and variable length data.
// We do not subtract sizeof(var) since num_inputs/num_outputs might
// both be zero.
const size_t raw_bytes =
sizeof(NodeItem) // Fixed
+ num_output_edges * sizeof(EdgeInfo) // output_edges[...]
+ num_outputs * sizeof(AllocatorAttributes) // output_attr[...]
+ num_inputs * sizeof(uint8) // input_type[num_inputs]
+ num_outputs * sizeof(uint8); // output_type[num_outputs]
static constexpr size_t kItemAlignment = sizeof(NodeItem*);
static_assert(kItemAlignment % alignof(NodeItem) == 0,
"NodeItem must be aligned with kItemAlignment");
static_assert(kItemAlignment % alignof(EdgeInfo) == 0,
"EdgeInfo must be aligned with kItemAlignment");
static_assert(kItemAlignment % alignof(AllocatorAttributes) == 0,
"AllocatorAttributes must be aligned with kItemAlignment");
static_assert(sizeof(NodeItem) % alignof(EdgeInfo) == 0,
"NodeItem must be aligned with EdgeInfo");
static_assert(sizeof(NodeItem) % alignof(AllocatorAttributes) == 0,
"NodeItem must be aligned with AllocatorAttributes");
static_assert(sizeof(EdgeInfo) % alignof(AllocatorAttributes) == 0,
"EdgeInfo must be aligned with AllocatorAttributes");
const size_t bytes =
((raw_bytes + kItemAlignment - 1) / kItemAlignment) * kItemAlignment;
return bytes;
}
char* GraphView::InitializeNode(char* ptr, const Node* n) {
const int id = n->id();
CHECK(node_offsets_[id] == kuint32max); // Initial value in constructor
const size_t bytes = NodeItemBytes(n);
static constexpr size_t kItemAlignment = sizeof(NodeItem*);
CHECK_EQ(reinterpret_cast<uintptr_t>(ptr) % kItemAlignment, 0);
NodeItem* item = reinterpret_cast<NodeItem*>(ptr);
// We store a 32-bit offset relative to the beginning of space_, so that we
// only need an array of 32-bit values to map from node id to the NodeItem*,
// (versus 64 bits on most machines if we just stored an array of NodeItem*
// pointers). Casting to int64 is needed on 32bit CPU to avoid comparing
// values as "int" vs "size_t" in CHECK_LE.
CHECK_LE(static_cast<int64>(ptr - space_), kuint32max);
const uint32 offset = ptr - space_;
node_offsets_[id] = offset;
ptr += bytes;
const int num_output_edges = n->out_edges().size();
const int num_inputs = n->num_inputs();
const int num_outputs = n->num_outputs();
new (item) NodeItem();
item->num_inputs = num_inputs;
item->num_outputs = num_outputs;
item->num_output_edges = num_output_edges;
// Fill output edges.
// Keep track of the last EdgeInfo in the EdngeInfo array that references
// a given output slot. For all but the last, we need to do a copy of the
// Tensor when propagating results downstream in the graph, but for the
// last one, we can just do a move of the Tensor object to propagate it.
gtl::InlinedVector<EdgeInfo*, 4> last_indices(num_outputs, nullptr);
EdgeInfo* dst_edge = item->output_edge_base();
for (auto e : n->out_edges()) {
dst_edge->dst_id = e->dst()->id();
CHECK_LT(e->src_output(), 32768); // Must fit in int16
dst_edge->output_slot = e->src_output();
dst_edge->is_last = false;
if (dst_edge->output_slot >= 0) {
last_indices[dst_edge->output_slot] = dst_edge;
}
dst_edge->input_slot = e->dst_input();
dst_edge++;
}
for (EdgeInfo* edge_info : last_indices) {
if (edge_info != nullptr) {
edge_info->is_last = true;
}
}
AllocatorAttributes* output_attrs = item->output_attr_base();
for (int i = 0; i < num_outputs; i++) {
new (&output_attrs[i]) AllocatorAttributes();
}
DCHECK_LT(DataType_MAX, 255); // Must fit in uint8
uint8* input_types = item->input_type_base();
for (int i = 0; i < num_inputs; i++) {
input_types[i] = static_cast<uint8>(n->input_type(i));
DCHECK_EQ(item->input_type(i), n->input_type(i));
}
uint8* output_types = item->output_type_base();
for (int i = 0; i < num_outputs; i++) {
output_types[i] = static_cast<uint8>(n->output_type(i));
DCHECK_EQ(item->output_type(i), n->output_type(i));
}
return ptr;
}
void GraphView::Initialize(const Graph* g) {
CHECK(node_offsets_ == nullptr);
const int num_nodes = g->num_node_ids();
num_nodes_ = num_nodes;
size_t total_bytes = 0;
for (const Node* n : g->nodes()) {
total_bytes += NodeItemBytes(n);
}
node_offsets_ = new uint32[num_nodes];
for (int i = 0; i < num_nodes; i++) {
node_offsets_[i] = kuint32max;
}
space_ = new char[total_bytes]; // NodeItem objects are allocated here
char* ptr = space_;
for (const Node* n : g->nodes()) {
ptr = InitializeNode(ptr, n);
}
CHECK_EQ(ptr, space_ + total_bytes);
}
static void GetMaxPendingCounts(const Node* n, int* max_pending,
int* max_dead_count) {
const int num_in_edges = n->in_edges().size();
int initial_count;
if (IsMerge(n)) {
// merge waits all control inputs so we initialize the pending
// count to be the number of control edges.
int32 num_control_edges = 0;
for (const Edge* edge : n->in_edges()) {
if (edge->IsControlEdge()) {
num_control_edges++;
}
}
// Use bit 0 to indicate if we are waiting for a ready live data input.
initial_count = 1 + (num_control_edges << 1);
} else {
initial_count = num_in_edges;
}
*max_pending = initial_count;
*max_dead_count = num_in_edges;
}
Status ExecutorImpl::Initialize() {
gview_.Initialize(graph_);
// Build the information about frames in this subgraph.
ControlFlowInfo cf_info;
TF_RETURN_IF_ERROR(BuildControlFlowInfo(graph_, &cf_info));
// Cache this value so we make this virtual function call once, rather
// that O(# steps * # nodes per step) times.
device_record_tensor_accesses_ =
params_.device->RequiresRecordingAccessedTensors();
for (auto& it : cf_info.unique_frame_names) {
EnsureFrameInfo(it)->nodes = new std::vector<const Node*>;
}
// Preprocess every node in the graph to create an instance of op
// kernel for each node.
for (const Node* n : graph_->nodes()) {
const int id = n->id();
const string& frame_name = cf_info.frame_names[id];
FrameInfo* frame_info = EnsureFrameInfo(frame_name);
// See if this node is a root node, and if so, add to root_nodes_.
const int num_in_edges = n->in_edges().size();
if (num_in_edges == 0) {
root_nodes_.push_back(n);
}
NodeItem* item = gview_.node(id);
item->node = n;
item->input_start = frame_info->total_inputs;
frame_info->total_inputs += n->num_inputs();
Status s = params_.create_kernel(n->def(), &item->kernel);
if (!s.ok()) {
item->kernel = nullptr;
s = AttachDef(s, n->def());
LOG(ERROR) << "Executor failed to create kernel. " << s;
return s;
}
CHECK(item->kernel);
item->kernel_is_expensive = item->kernel->IsExpensive();
item->kernel_is_async = (item->kernel->AsAsync() != nullptr);
item->is_merge = IsMerge(n);
item->is_enter = IsEnter(n);
item->is_exit = IsExit(n);
item->is_control_trigger = IsControlTrigger(n);
item->is_sink = IsSink(n);
item->is_enter_exit_or_next_iter =
(IsEnter(n) || IsExit(n) || IsNextIteration(n));
// Compute the maximum values we'll store for this node in the
// pending counts data structure, and allocate a handle in
// that frame's pending counts data structure that has enough
// space to store these maximal count values.
int max_pending, max_dead;
GetMaxPendingCounts(n, &max_pending, &max_dead);
item->pending_id =
frame_info->pending_counts_layout.CreateHandle(max_pending, max_dead);
// Initialize static information about the frames in the graph.
frame_info->nodes->push_back(n);
if (IsEnter(n)) {
string enter_name;
TF_RETURN_IF_ERROR(GetNodeAttr(n->def(), "frame_name", &enter_name));
EnsureFrameInfo(enter_name)->input_count++;
}
}
// Initialize PendingCounts only after item->pending_id is initialized for
// all nodes.
InitializePending(graph_, cf_info);
return gview_.SetAllocAttrs(graph_, params_.device);
}
Status GraphView::SetAllocAttrs(const Graph* g, const Device* device) {
Status s;
DeviceNameUtils::ParsedName local_dev_name = device->parsed_name();
for (const Node* n : g->nodes()) {
NodeItem* item = node(n->id());
AllocatorAttributes* attrs = item->output_attr_base();
// Examine the out edges of each node looking for special use
// cases that may affect memory allocation attributes.
for (auto e : n->out_edges()) {
AllocatorAttributes attr;
s = InferAllocAttr(n, e->dst(), local_dev_name, &attr);
if (!s.ok()) return s;
if (attr.value != 0) {
if (!e->IsControlEdge()) {
attrs[e->src_output()].Merge(attr);
}
}
}
for (int out = 0; out < n->num_outputs(); out++) {
OpKernel* op_kernel = item->kernel;
DCHECK_LT(out, op_kernel->output_memory_types().size());
bool on_host = op_kernel->output_memory_types()[out] == HOST_MEMORY;
AllocatorAttributes h;
h.set_on_host(on_host);
attrs[out].Merge(h);
}
}
return s;
}
static Status InferAllocAttr(const Node* n, const Node* dst,
const DeviceNameUtils::ParsedName& local_dev_name,
AllocatorAttributes* attr) {
Status s;
// Note that it's possible for *n to be a Recv and *dst to be a Send,
// so these two cases are not mutually exclusive.
if (IsRecv(n)) {
string src_name;
s = GetNodeAttr(n->def(), "send_device", &src_name);
if (!s.ok()) return s;
DeviceNameUtils::ParsedName parsed_src_name;
if (!DeviceNameUtils::ParseFullName(src_name, &parsed_src_name)) {
s = errors::Internal("Bad send_device attr '", src_name, "' in node ",
n->name());
return s;
}
if (!DeviceNameUtils::IsSameAddressSpace(parsed_src_name, local_dev_name)) {
// Value is going to be the sink of an RPC.
attr->set_nic_compatible(true);
VLOG(2) << "node " << n->name() << " is the sink of an RPC in";
} else if ((local_dev_name.type == "CPU" || n->IsHostRecv()) &&
parsed_src_name.type != "CPU") {
// Value is going to be the sink of a local DMA from GPU to CPU (or other
// types of accelerators).
attr->set_gpu_compatible(true);
VLOG(2) << "node " << n->name() << " is the sink of a gpu->cpu copy";
} else {
VLOG(2) << "default alloc case local type " << local_dev_name.type
<< " remote type " << parsed_src_name.type;
}
}
if (IsSend(dst)) {
string dst_name;
s = GetNodeAttr(dst->def(), "recv_device", &dst_name);
if (!s.ok()) return s;
DeviceNameUtils::ParsedName parsed_dst_name;
if (!DeviceNameUtils::ParseFullName(dst_name, &parsed_dst_name)) {
s = errors::Internal("Bad recv_device attr '", dst_name, "' in node ",
n->name());
return s;
}
if (!DeviceNameUtils::IsSameAddressSpace(parsed_dst_name, local_dev_name)) {
// Value is going to be the source of an RPC.
attr->set_nic_compatible(true);
VLOG(2) << "node " << n->name() << " is the source of an RPC out";
} else if ((local_dev_name.type == "CPU" || dst->IsHostSend()) &&
parsed_dst_name.type != "CPU") {
// Value is going to be the source of a local DMA from CPU to GPU (or
// other types of accelerators).
// Note that this does not cover the case where the allocation of the
// output tensor is not generated by the src: n.
attr->set_gpu_compatible(true);
VLOG(2) << "node " << n->name() << " is the source of a cpu->gpu copy";
} else {
VLOG(2) << "default alloc case local type " << local_dev_name.type
<< " remote type " << parsed_dst_name.type;
}
} else if (dst->type_string() == "ToFloat") {
for (auto e : dst->out_edges()) {
s = InferAllocAttr(n, e->dst(), local_dev_name, attr);
if (!s.ok()) return s;
}
}
return s;
}
// The state associated with one invocation of ExecutorImpl::Run.
// ExecutorState dispatches nodes when they become ready and keeps
// track of how many predecessors of a node have not done (pending_).
class ExecutorState {
public:
ExecutorState(const Executor::Args& args, ExecutorImpl* impl);
~ExecutorState();
void RunAsync(Executor::DoneCallback done);
private:
// Either a tensor pointer (pass-by-reference) or a tensor (pass-by-value).
// TODO(yuanbyu): A better way to do "has_value"?
struct Entry {
Entry() {}
Entry(const Entry& other)
: ref(other.ref),
ref_mu(other.ref_mu),
has_value(other.has_value),
val_field_is_set(other.val_field_is_set),
alloc_attr(other.alloc_attr),
device_context(other.device_context) {
if (val_field_is_set) {
val.Init(*other.val);
}
}
~Entry() {
if (val_field_is_set) val.Destroy();
}
Entry& operator=(const Entry& other) {
if (val_field_is_set) {
val.Destroy();
}
ref = other.ref;
ref_mu = other.ref_mu;
has_value = other.has_value;
val_field_is_set = other.val_field_is_set;
alloc_attr = other.alloc_attr;
device_context = other.device_context;
if (val_field_is_set) {
val.Init(*other.val);
}
return *this;
}
Entry& operator=(Entry&& other) {
if (val_field_is_set) {
val.Destroy();
}
ref = other.ref;
ref_mu = other.ref_mu;
has_value = other.has_value;
val_field_is_set = other.val_field_is_set;
alloc_attr = other.alloc_attr;
device_context = other.device_context;
if (val_field_is_set) {
val.Init(std::move(*other.val));
}
return *this;
}
// Clears the <val> field.
void ClearVal() {
if (val_field_is_set) {
val.Destroy();
val_field_is_set = false;
}
}
// A tensor value, if val_field_is_set.
ManualConstructor<Tensor> val;
Tensor* ref = nullptr; // A tensor reference.
mutex* ref_mu = nullptr; // mutex for *ref if ref is not nullptr.
// Whether the value exists, either in <val> or <ref>.
bool has_value = false;
bool val_field_is_set = false;
// The attributes of the allocator that creates the tensor.
AllocatorAttributes alloc_attr;
// Every entry carries an optional DeviceContext containing
// Device-specific information about how the Tensor was produced.
DeviceContext* device_context = nullptr;
};
// Contains a value for [node->id()] for the device context assigned by the
// device at the beginning of a step.
DeviceContextMap device_context_map_;
struct TaggedNode;
typedef gtl::InlinedVector<TaggedNode, 8> TaggedNodeSeq;
typedef gtl::InlinedVector<Entry, 4> EntryVector;
struct IterationState {
explicit IterationState(const PendingCounts* pending_counts,
int total_input_tensors)
: input_tensors(new Entry[total_input_tensors]),
outstanding_ops(0),
outstanding_frame_count(0),
counts_(*pending_counts) { // Initialize with copy of *pending_counts
}
// The state of an iteration.
// One copy per iteration. For iteration k, i-th node's j-th input is in
// input_tensors[k][impl_->nodes[i].input_start + j]. An entry is either
// a tensor pointer (pass-by-reference) or a tensor (pass-by-value).
//
// NOTE: No need to protect input_tensors[i] by any locks because it
// is resized once. Each element of tensors_ is written once by the
// source node of an edge and is cleared by the destination of the same
// edge. The latter node is never run concurrently with the former node.
Entry* input_tensors;
// The number of outstanding ops for each iteration.
int outstanding_ops;
// The number of outstanding frames for each iteration.
int outstanding_frame_count;
int pending(PendingCounts::Handle h) { return counts_.pending(h); }
int decrement_pending(PendingCounts::Handle h, int v) {
return counts_.decrement_pending(h, v);
}
// Mark a merge node as live
// REQUIRES: Node corresponding to "h" is a merge node
void mark_live(PendingCounts::Handle h) { counts_.mark_live(h); }
// Mark a node to show that processing has started.
void mark_started(PendingCounts::Handle h) { counts_.mark_started(h); }
// Mark a node to show that processing has completed.
void mark_completed(PendingCounts::Handle h) { counts_.mark_completed(h); }
PendingCounts::NodeState node_state(PendingCounts::Handle h) {
return counts_.node_state(h);
}
int dead_count(PendingCounts::Handle h) { return counts_.dead_count(h); }
void increment_dead_count(PendingCounts::Handle h) {
counts_.increment_dead_count(h);
}
void adjust_for_activation(PendingCounts::Handle h, bool increment_dead,
int* pending_result, int* dead_result) {
counts_.adjust_for_activation(h, increment_dead, pending_result,
dead_result);
}
~IterationState() { delete[] input_tensors; }
private:
PendingCounts counts_;
};
struct FrameState {
explicit FrameState(const ExecutorImpl* impl, int parallel_iters)
: executor(impl),
max_parallel_iterations(parallel_iters),
num_outstanding_iterations(1) {}
// A new frame is created for each loop. Execution starts at iteration 0.
// When a value at iteration 0 passes through a NextIteration node,
// iteration 1 is created and starts running. Note that iteration 0 may
// still be running so multiple iterations may run in parallel. The
// frame maintains the state of iterations in several data structures
// such as pending_count and input_tensors. When iteration 0 completes,
// we garbage collect the state of iteration 0.
//
// A frame instance is considered "done" and can be garbage collected
// if all its inputs have entered and all its iterations are "done".
//
// A frame manages the live iterations of an iterative computation.
// Iteration i is considered "done" when there are no outstanding ops,
// frames at iteration i are done, all recvs for this iteration are
// completed, and iteration i-1 is done. For iteration 0, we instead
// wait for there to be no more pending inputs of the frame.
//
// Frames and iterations are garbage collected once they are done.
// The state we need to keep around is highly dependent on the
// parallelism enabled by the scheduler. We may want to have the
// scheduler dynamically control the outstanding number of live
// parallel frames and iterations. To reduce the state space, the
// scheduler might want to schedule ops in inner frames first and
// lower iterations first.
//
// This frame state is mostly initialized lazily on demand so we
// don't introduce unnecessary overhead.
// The executor the frame is in.
const ExecutorImpl* executor = nullptr;
// The name of this frame, which is the concatenation of its parent
// frame name, the iteration of the parent frame when this frame was
// created, and the value of the attr 'frame_name'.
string frame_name;
// The unique id for this frame. Generated by fingerprinting
// frame_name.
uint64 frame_id;
// The iteration id of its parent frame when this frame is created.
// -1 if there is no parent frame. The frame_name/parent_iter pair
// uniquely identifies this FrameState.
int64 parent_iter = -1;
// The FrameState of its parent frame.
FrameState* parent_frame = nullptr;
// The maximum allowed number of parallel iterations.
const int max_parallel_iterations;
// The number of inputs this frame is still waiting.
int num_pending_inputs = 0;
// The highest iteration number we have reached so far in this frame.
int64 iteration_count GUARDED_BY(mu) = 0;
// The number of outstanding iterations.
int num_outstanding_iterations GUARDED_BY(mu) = 1;
// The active iteration states of this frame.
gtl::InlinedVector<IterationState*, 12> iterations;
// The NextIteration nodes to enter a new iteration. If the number of
// outstanding iterations reaches the limit, we will defer the start of
// the next iteration until the number of outstanding iterations falls
// below the limit.
std::vector<std::pair<const Node*, Entry>> next_iter_roots GUARDED_BY(mu);
// The values of the loop invariants for this loop. They are added into
// this list as they "enter" the frame. When a loop invariant enters,