/
dataset.cc
956 lines (862 loc) · 34.3 KB
/
dataset.cc
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
/* Copyright 2017 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/framework/dataset.h"
#include <unordered_map>
#include "tensorflow/core/framework/device_base.h"
#include "tensorflow/core/framework/function.h"
#include "tensorflow/core/framework/resource_mgr.h"
#include "tensorflow/core/framework/variant_encode_decode.h"
#include "tensorflow/core/framework/variant_op_registry.h"
#include "tensorflow/core/graph/graph_def_builder.h"
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/platform/errors.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/resource.h"
#include "tensorflow/core/platform/strcat.h"
#include "tensorflow/core/profiler/lib/traceme.h"
// On Windows, disable some macros that would break compile
#if defined(PLATFORM_WINDOWS)
#undef GetMessage
#endif
namespace tensorflow {
namespace data {
namespace {
static mutex* get_dataset_op_registry_lock() {
static mutex dataset_op_registry_lock(LINKER_INITIALIZED);
return &dataset_op_registry_lock;
}
static std::unordered_set<string>* get_dataset_op_registry() {
static std::unordered_set<string>* names = new std::unordered_set<string>;
return names;
}
// A wrapper class for storing a `DatasetBase` instance in a DT_VARIANT tensor.
// Objects of the wrapper class own a reference on an instance of `DatasetBase`,
// and the wrapper's copy constructor and destructor take care of managing the
// reference count.
//
// NOTE(mrry): This is not a feature-complete implementation of the DT_VARIANT
// specification. In particular, we cannot currently serialize an arbitrary
// `DatasetBase` object, so the `Encode()` and `Decode()` methods are not
// implemented.
class DatasetVariantWrapper {
public:
DatasetVariantWrapper() : dataset_(nullptr) {}
// Transfers ownership of `dataset` to `*this`.
explicit DatasetVariantWrapper(DatasetBase* dataset) : dataset_(dataset) {}
DatasetVariantWrapper(const DatasetVariantWrapper& other)
: dataset_(other.dataset_) {
if (dataset_) dataset_->Ref();
}
DatasetVariantWrapper& operator=(DatasetVariantWrapper&& other) {
if (&other == this) return *this;
std::swap(dataset_, other.dataset_);
return *this;
}
DatasetVariantWrapper& operator=(const DatasetVariantWrapper& other) = delete;
~DatasetVariantWrapper() {
if (dataset_) dataset_->Unref();
}
DatasetBase* get() const { return dataset_; }
string TypeName() const { return "tensorflow::DatasetVariantWrapper"; }
string DebugString() const {
if (dataset_) {
return dataset_->DebugString();
} else {
return "<Uninitialized DatasetVariantWrapper>";
}
}
void Encode(VariantTensorData* data) const {
LOG(ERROR) << "The Encode() method is not implemented for "
"DatasetVariantWrapper objects.";
}
bool Decode(const VariantTensorData& data) {
LOG(ERROR) << "The Decode() method is not implemented for "
"DatasetVariantWrapper objects.";
return false;
}
private:
DatasetBase* dataset_; // Owns one reference.
};
const char kWrappedDatasetVariantTypeName[] =
"tensorflow::data::WrappedDatasetVariant";
class WrappedDatasetVariantWrapper {
public:
WrappedDatasetVariantWrapper() {}
explicit WrappedDatasetVariantWrapper(const Tensor& ds_tensor)
: ds_tensor_(ds_tensor) {}
Tensor get() const { return ds_tensor_; }
string TypeName() const { return "tensorflow::WrappedDatasetVariantWrapper"; }
string DebugString() const {
return "tensorflow::WrappedDatasetVariantWrapper::DebugString";
}
void Encode(VariantTensorData* data) const {
*(data->add_tensors()) = ds_tensor_;
}
bool Decode(const VariantTensorData& data) {
ds_tensor_ = data.tensors(0);
return true;
}
private:
Tensor ds_tensor_;
};
class WrapDatasetVariantOp : public OpKernel {
public:
explicit WrapDatasetVariantOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
void Compute(OpKernelContext* ctx) override {
const Tensor& tensor = ctx->input(0);
OP_REQUIRES(ctx,
tensor.dtype() == DT_VARIANT &&
TensorShapeUtils::IsScalar(tensor.shape()),
errors::InvalidArgument(
"Dataset tensor must be a scalar of dtype DT_VARIANT."));
DatasetBase* unused;
OP_REQUIRES_OK(ctx, GetDatasetFromVariantTensor(tensor, &unused));
Tensor* output = nullptr;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &output));
output->scalar<Variant>()() = WrappedDatasetVariantWrapper(tensor);
}
};
REGISTER_KERNEL_BUILDER(Name("WrapDatasetVariant").Device(DEVICE_CPU),
WrapDatasetVariantOp);
REGISTER_KERNEL_BUILDER(Name("WrapDatasetVariant")
.HostMemory("input_handle")
.HostMemory("output_handle")
.Device(DEVICE_GPU),
WrapDatasetVariantOp);
class UnwrapDatasetVariantOp : public OpKernel {
public:
explicit UnwrapDatasetVariantOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
void Compute(OpKernelContext* ctx) override {
const Tensor& tensor = ctx->input(0);
OP_REQUIRES(ctx,
tensor.dtype() == DT_VARIANT &&
TensorShapeUtils::IsScalar(tensor.shape()),
errors::InvalidArgument(
"Dataset tensor must be a scalar of dtype DT_VARIANT."));
Variant variant = tensor.scalar<Variant>()();
const WrappedDatasetVariantWrapper* wrapper =
variant.get<WrappedDatasetVariantWrapper>();
OP_REQUIRES(ctx, wrapper != nullptr,
errors::InvalidArgument(
"Tensor must be a WrappedDataset variant object."));
Tensor ds_tensor = wrapper->get();
OP_REQUIRES_OK(ctx, ctx->set_output("output_handle", ds_tensor));
}
};
REGISTER_KERNEL_BUILDER(Name("UnwrapDatasetVariant").Device(DEVICE_CPU),
UnwrapDatasetVariantOp);
REGISTER_KERNEL_BUILDER(Name("UnwrapDatasetVariant")
.HostMemory("input_handle")
.HostMemory("output_handle")
.Device(DEVICE_GPU),
UnwrapDatasetVariantOp);
static Status WrappedDatasetVariantDeviceCopy(
const WrappedDatasetVariantWrapper& from, WrappedDatasetVariantWrapper* to,
const UnaryVariantOpRegistry::AsyncTensorDeviceCopyFn& copy) {
*to = WrappedDatasetVariantWrapper(from);
return Status::OK();
}
#define REGISTER_OPTIONAL_COPY(DIRECTION) \
INTERNAL_REGISTER_UNARY_VARIANT_DEVICE_COPY_FUNCTION( \
WrappedDatasetVariantWrapper, DIRECTION, \
WrappedDatasetVariantDeviceCopy)
REGISTER_OPTIONAL_COPY(VariantDeviceCopyDirection::HOST_TO_DEVICE);
REGISTER_OPTIONAL_COPY(VariantDeviceCopyDirection::DEVICE_TO_HOST);
REGISTER_OPTIONAL_COPY(VariantDeviceCopyDirection::DEVICE_TO_DEVICE);
REGISTER_UNARY_VARIANT_DECODE_FUNCTION(WrappedDatasetVariantWrapper,
kWrappedDatasetVariantTypeName);
} // namespace
Status GraphDefBuilderWrapper::AddDataset(
const DatasetBase* dataset,
const std::vector<std::pair<size_t, Node*>>& inputs,
const std::vector<std::pair<size_t, gtl::ArraySlice<Node*>>>& list_inputs,
const std::vector<std::pair<StringPiece, AttrValue>>& attrs,
Node** output) {
const string& type_string = dataset->type_string();
std::unique_ptr<const GraphDefBuilder::Options> opts(
new GraphDefBuilder::Options(b_->opts()));
// TODO(srbs|mrry): Not all datasets have output_types and output_shapes
// attributes defined. It will be nice to have a consistent pattern.
bool has_output_types_attr = HasAttr(type_string, "output_types");
bool has_output_shapes_attr = HasAttr(type_string, "output_shapes");
if (has_output_shapes_attr) {
opts.reset(new GraphDefBuilder::Options(
opts->WithAttr("output_shapes", dataset->output_shapes())));
}
if (has_output_types_attr) {
opts.reset(new GraphDefBuilder::Options(
opts->WithAttr("output_types", dataset->output_dtypes())));
}
for (const auto& attr : attrs) {
opts.reset(
new GraphDefBuilder::Options(opts->WithAttr(attr.first, attr.second)));
}
if (opts->HaveError()) {
return errors::Internal("AddDataset: Failed to build Options with error ",
opts->StatusToString());
}
NodeBuilder node_builder(opts->GetNameForOp(type_string), type_string,
opts->op_registry());
{
size_t total_size = inputs.size() + list_inputs.size();
auto inputs_iter = inputs.begin();
auto list_inputs_iter = list_inputs.begin();
for (int i = 0; i < total_size; i++) {
if (inputs_iter != inputs.end() && inputs_iter->first == i) {
node_builder.Input(NodeBuilder::NodeOut(inputs_iter->second));
inputs_iter++;
} else if (list_inputs_iter != list_inputs.end() &&
list_inputs_iter->first == i) {
std::vector<NodeBuilder::NodeOut> nodeout_inputs;
nodeout_inputs.reserve(list_inputs_iter->second.size());
for (Node* n : list_inputs_iter->second) {
nodeout_inputs.emplace_back(n);
}
node_builder.Input(nodeout_inputs);
list_inputs_iter++;
} else {
return errors::InvalidArgument("No input found for index ", i);
}
}
}
*output = opts->FinalizeBuilder(&node_builder);
if (*output == nullptr) {
return errors::Internal("AddDataset: Failed to build ", type_string,
" op with error ", opts->StatusToString());
}
return Status::OK();
}
Status GraphDefBuilderWrapper::AddFunction(
SerializationContext* ctx, const string& function_name,
const FunctionLibraryDefinition& lib_def) {
if (b_->HasFunction(function_name)) {
VLOG(1) << "Function with name " << function_name << "already exists in"
<< " the graph. It will not be added again.";
return Status::OK();
}
const FunctionDef* f_def = lib_def.Find(function_name);
if (f_def == nullptr) {
return errors::InvalidArgument("Unable to find FunctionDef for ",
function_name, " in the registry.");
}
FunctionDefLibrary def;
*def.add_function() = *f_def;
const string gradient_func = lib_def.FindGradient(function_name);
if (!gradient_func.empty()) {
GradientDef* g_def = def.add_gradient();
g_def->set_function_name(function_name);
g_def->set_gradient_func(gradient_func);
}
TF_RETURN_IF_ERROR(b_->AddFunctionLibrary(def));
// Recursively add functions in inputs of function_name.
for (const NodeDef& node_def : f_def->node_def()) {
const OpRegistrationData* op_reg_data = nullptr;
TF_RETURN_IF_ERROR(lib_def.LookUp(node_def.op(), &op_reg_data));
if (op_reg_data->is_function_op) {
TF_RETURN_IF_ERROR(AddFunction(ctx, op_reg_data->op_def.name(), lib_def));
}
// Recursively add functions in attrs of this NodeDef.
for (const auto& pair : node_def.attr()) {
TF_RETURN_IF_ERROR(AddAttrFunctions(ctx, pair.second, lib_def));
}
}
// Recursively add functions in attrs of function_name.
for (auto iter = f_def->attr().begin(); iter != f_def->attr().end(); iter++) {
TF_RETURN_IF_ERROR(AddAttrFunctions(ctx, iter->second, lib_def));
}
return Status::OK();
}
void GraphDefBuilderWrapper::AddPlaceholderInternal(const Tensor& val,
Node** output) {
*output = ops::SourceOp(
"Placeholder",
b_->opts().WithAttr("dtype", val.dtype()).WithAttr("shape", val.shape()));
}
void GraphDefBuilderWrapper::AddTensorInternal(const Tensor& val,
Node** output) {
*output = ops::SourceOp(
"Const",
b_->opts().WithAttr("dtype", val.dtype()).WithAttr("value", val));
}
bool GraphDefBuilderWrapper::HasAttr(const string& name,
const string& attr_name) const {
const OpDef* op_def = nullptr;
Status s = b_->opts().op_registry()->LookUpOpDef(name, &op_def);
if (!s.ok() || op_def == nullptr) {
return false;
}
return HasAttr(op_def, attr_name);
}
Status IteratorBase::InitializeBase(IteratorContext* ctx,
const IteratorBase* parent) {
parent_ = parent;
id_ =
Hash64CombineUnordered(Hash64(prefix()), reinterpret_cast<uint64>(this));
if (parent_) {
parent_id_ = Hash64CombineUnordered(Hash64(parent_->prefix()),
reinterpret_cast<uint64>(parent_));
}
if (const auto& model = ctx->model()) {
auto factory = [ctx, this](model::Node::Args args) {
return CreateNode(ctx, std::move(args));
};
model->AddNode(std::move(factory), prefix(), parent->model_node(), &node_);
cleanup_fns_.push_back([this, model]() { model->RemoveNode(node_); });
}
return Status::OK();
}
int64 GetAllocatedBytes(const std::vector<Tensor>& element) {
int64 allocated_bytes = 0;
DatasetBase* dataset;
for (auto& tensor : element) {
if (tensor.dtype() == DT_VARIANT &&
GetDatasetFromVariantTensor(tensor, &dataset).ok()) {
allocated_bytes += dataset->AllocatedBytes();
} else {
allocated_bytes += tensor.AllocatedBytes();
}
}
return allocated_bytes;
}
int64 GetTotalBytes(const std::vector<Tensor>& element) {
int64 total_bytes = 0;
DatasetBase* dataset;
for (auto& tensor : element) {
if (tensor.dtype() == DT_VARIANT &&
GetDatasetFromVariantTensor(tensor, &dataset).ok()) {
total_bytes += dataset->TotalBytes();
} else {
total_bytes += tensor.TotalBytes();
}
}
return total_bytes;
}
std::string FullName(const std::string& prefix, const std::string& name) {
if (str_util::StrContains(name, kColon)) {
LOG(ERROR) << name << " should not contain " << kColon;
}
return strings::StrCat(kFullNameRandomHex, kPipe, prefix, kColon, name);
}
Status GetDatasetFromVariantTensor(const Tensor& tensor,
DatasetBase** out_dataset) {
if (!(tensor.dtype() == DT_VARIANT &&
TensorShapeUtils::IsScalar(tensor.shape()))) {
return errors::InvalidArgument(
"Dataset tensor must be a scalar of dtype DT_VARIANT.");
}
const Variant& variant = tensor.scalar<Variant>()();
const DatasetVariantWrapper* wrapper = variant.get<DatasetVariantWrapper>();
if (wrapper == nullptr) {
return errors::InvalidArgument("Tensor must be a Dataset object.");
}
*out_dataset = wrapper->get();
if (*out_dataset == nullptr) {
return errors::Internal("Read uninitialized Dataset variant.");
}
return Status::OK();
}
Status StoreDatasetInVariantTensor(DatasetBase* dataset, Tensor* tensor) {
if (!(tensor->dtype() == DT_VARIANT &&
TensorShapeUtils::IsScalar(tensor->shape()))) {
return errors::InvalidArgument(
"Dataset tensor must be a scalar of dtype DT_VARIANT.");
}
tensor->scalar<Variant>()() = DatasetVariantWrapper(dataset);
return Status::OK();
}
namespace internal {
#define WARN_PROTO_FIELD_CONFLICT(reflection, field, field_type, src, dst) \
{ \
auto source_value = reflection->Get##field_type(src, field); \
auto destination_value = reflection->Get##field_type(*dst, field); \
if (source_value != destination_value) { \
LOG(WARNING) << "Changing the value of option field " << field->name() \
<< " from " << destination_value << " to " << source_value; \
} \
}
#define WARN_PROTO_ENUM_FIELD_CONFLICT(reflection, field, src, dst) \
{ \
auto source_value = reflection->GetEnum(src, field); \
auto destination_value = reflection->GetEnum(*dst, field); \
if (source_value != destination_value) { \
LOG(WARNING) << "Changing the value of option enum field " \
<< field->name() << " from " \
<< destination_value->full_name() << " to " \
<< source_value->full_name(); \
} \
}
void WarnProtoConflicts(const protobuf::Message& src, protobuf::Message* dst) {
std::vector<const protobuf::FieldDescriptor*> set_src;
std::vector<const protobuf::FieldDescriptor*> set_dst;
const protobuf::Reflection* reflection = src.GetReflection();
reflection->ListFields(src, &set_src);
reflection->ListFields(*dst, &set_dst);
std::sort(set_src.begin(), set_src.end());
std::sort(set_dst.begin(), set_dst.end());
std::vector<const protobuf::FieldDescriptor*> in_both;
std::set_intersection(set_src.begin(), set_src.end(), set_dst.begin(),
set_dst.end(), std::back_inserter(in_both));
for (auto field : in_both) {
if (field->type() == protobuf::FieldDescriptor::TYPE_MESSAGE) {
WarnProtoConflicts(reflection->GetMessage(src, field),
reflection->MutableMessage(dst, field));
} else {
switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_INT32:
WARN_PROTO_FIELD_CONFLICT(reflection, field, Int32, src, dst);
break;
case protobuf::FieldDescriptor::CPPTYPE_INT64:
WARN_PROTO_FIELD_CONFLICT(reflection, field, Int64, src, dst);
break;
case protobuf::FieldDescriptor::CPPTYPE_UINT32:
WARN_PROTO_FIELD_CONFLICT(reflection, field, UInt32, src, dst);
break;
case protobuf::FieldDescriptor::CPPTYPE_UINT64:
WARN_PROTO_FIELD_CONFLICT(reflection, field, UInt64, src, dst);
break;
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
WARN_PROTO_FIELD_CONFLICT(reflection, field, Double, src, dst);
break;
case protobuf::FieldDescriptor::CPPTYPE_FLOAT:
WARN_PROTO_FIELD_CONFLICT(reflection, field, Float, src, dst);
break;
case protobuf::FieldDescriptor::CPPTYPE_BOOL:
WARN_PROTO_FIELD_CONFLICT(reflection, field, Bool, src, dst);
break;
case protobuf::FieldDescriptor::CPPTYPE_ENUM:
WARN_PROTO_ENUM_FIELD_CONFLICT(reflection, field, src, dst);
break;
default: {
LOG(ERROR) << "Unrecognized proto type for field "
<< field->full_name();
}
}
}
}
}
#undef WARN_PROTO_ENUM_FIELD_CONFLICT
#undef WARN_PROTO_FIELD_CONFLICT
void MergeOptions(const protobuf::Message& source,
protobuf::Message* destination) {
WarnProtoConflicts(source, destination);
destination->MergeFrom(source);
}
void MergeOptions(const protobuf::MessageLite& source,
protobuf::MessageLite* destination) {
destination->CheckTypeAndMergeFrom(source);
}
} // namespace internal
Status DatasetBase::MergeOptionsFromInputs() {
std::vector<const DatasetBase*> inputs;
Status s = InputDatasets(&inputs);
if (errors::IsUnimplemented(s)) {
return errors::Unimplemented(
"Cannot merge options for dataset of type ", type_string(),
", because the dataset does not implement `InputDatasets`.");
}
if (inputs.empty()) {
return Status::OK();
}
// Merge options from inputs sequentially before merging options from dataset.
// Since the last options merged takes precedence, the options that may be set
// for the current dataset through OptionsDataset takes precedence over those
// set on the input datasets.
Options merged_options = inputs[0]->options_;
for (int i = 1; i < inputs.size(); ++i) {
internal::MergeOptions(inputs[i]->options_, &merged_options);
}
internal::MergeOptions(options_, &merged_options);
options_ = merged_options;
return Status::OK();
}
Status DatasetBase::MakeIterator(
IteratorContext* ctx, const IteratorBase* parent,
const string& output_prefix,
std::unique_ptr<IteratorBase>* iterator) const {
if (type_string() == "OptionsDataset" || type_string() == "FinalizeDataset") {
std::vector<const DatasetBase*> inputs;
Status s = InputDatasets(&inputs);
return inputs[0]->MakeIterator(ctx, parent, output_prefix, iterator);
}
profiler::TraceMe traceme(
[&] {
return profiler::TraceMeEncode(
strings::StrCat("MakeIterator::", type_string()), {});
},
profiler::TraceMeLevel::kInfo);
*iterator = MakeIteratorInternal(output_prefix);
Status s = (*iterator)->InitializeBase(ctx, parent);
if (s.ok()) {
s.Update((*iterator)->Initialize(ctx));
}
if (!s.ok()) {
// Reset the iterator to avoid returning an uninitialized iterator.
iterator->reset();
}
return s;
}
Status DatasetBase::MakeSplitProvider(
std::unique_ptr<SplitProvider>* split_provider) const {
std::vector<const DatasetBase*> inputs;
Status s = InputDatasets(&inputs);
if (errors::IsUnimplemented(s)) {
return errors::Unimplemented(
"Cannot create a split provider for dataset of type ", type_string(),
", because the dataset implements neither `InputDatasets` nor "
"`MakeSplitProvider`.");
}
if (inputs.size() != 1) {
return errors::Unimplemented(
"Cannot create a split provider for dataset of type ", type_string(),
", because the dataset is not unary (having arity ", inputs.size(),
"), and no custom implementation of `MakeSplitProvider` is defined.");
}
return inputs[0]->MakeSplitProvider(split_provider);
}
Status DatasetBase::InputDatasets(
std::vector<const DatasetBase*>* inputs) const {
return errors::Unimplemented("InputDatasets not implemented for ",
type_string());
}
Status DatasetBase::DatasetGraphDefBuilder::AddInputDataset(
SerializationContext* ctx, const DatasetBase* dataset, Node** output) {
Status status = dataset->AsGraphDefInternal(ctx, this, output);
if (errors::IsUnimplemented(status) && !ctx->fail_if_unimplemented()) {
Tensor t(DT_VARIANT, TensorShape({}));
// `StoreDatasetInVariantTensor` will transfer ownership of `dataset`. We
// increment the refcount of `dataset` here to retain ownership.
dataset->Ref();
TF_RETURN_IF_ERROR(
StoreDatasetInVariantTensor(const_cast<DatasetBase*>(dataset), &t));
TF_RETURN_IF_ERROR(AddPlaceholder(t, output));
DCHECK_NE(ctx->input_list(), nullptr);
ctx->input_list()->emplace_back((*output)->name(), std::move(t));
LOG_EVERY_N_SEC(WARNING, 30)
<< "Input of " << dataset->DebugString()
<< " will not be optimized because the dataset does not implement the "
"AsGraphDefInternal() method needed to apply optimizations.";
return Status::OK();
}
return status;
}
Status DatasetBase::DatasetGraphDefBuilder::AddDatasetOrTensor(
SerializationContext* ctx, const Tensor& t, Node** output) {
if (t.dtype() == DT_VARIANT) {
// If the input tensor is a variant, it may represent a multi-dimensional
// array of datasets. We attempt to decode each dataset so that we can use
// their custom serialization logic and combine the result of their
// individual serializations using the `Pack` operation.
//
// If this fails, we fallback to using its Variant::Encode() based
// serialization.
Status s = AddDatasetOrTensorHelper(ctx, t, output);
if (s.ok()) {
return s;
}
}
if (t.dtype() == DT_RESOURCE && ctx->serialize_data_tensors()) {
Status s = AddResourceHelper(ctx, t, output);
if (!errors::IsUnimplemented(s)) {
// Fall through to AddTensor if AsGraphDef is not implemented for this
// resource.
return s;
}
}
return AddTensor(t, output);
}
Status DatasetBase::DatasetGraphDefBuilder::AddDatasetOrTensorHelper(
SerializationContext* ctx, const Tensor& t, Node** output) {
if (t.dims() == 0) {
DatasetBase* dataset;
TF_RETURN_IF_ERROR(GetDatasetFromVariantTensor(t, &dataset));
return AddInputDataset(ctx, dataset, output);
}
std::vector<NodeBuilder::NodeOut> nodes;
for (int i = 0; i < t.dim_size(0); ++i) {
Node* node;
TF_RETURN_IF_ERROR(AddDatasetOrTensorHelper(ctx, t.SubSlice(i), &node));
nodes.emplace_back(node);
}
auto op_name = "Pack";
auto opts = builder()->opts();
NodeBuilder node_builder(opts.GetNameForOp(op_name), op_name,
opts.op_registry());
node_builder.Input(std::move(nodes));
*output = opts.FinalizeBuilder(&node_builder);
return Status::OK();
}
Status DatasetBase::DatasetGraphDefBuilder::AddResourceHelper(
SerializationContext* ctx, const Tensor& t, Node** output) {
const ResourceHandle& handle = t.flat<ResourceHandle>()(0);
ResourceBase* resource;
TF_RETURN_IF_ERROR(ctx->resource_mgr()->Lookup(handle, &resource));
core::ScopedUnref unref(resource);
return resource->AsGraphDef(builder(), output);
}
DatasetBaseIterator::DatasetBaseIterator(const BaseParams& params)
: params_(params) {
params_.dataset->Ref();
VLOG(2) << prefix() << " constructor";
strings::StrAppend(&traceme_metadata_, "shapes=");
auto& shapes = output_shapes();
for (int i = 0; i < shapes.size(); ++i) {
if (i > 0) {
strings::StrAppend(&traceme_metadata_, " ");
}
strings::StrAppend(&traceme_metadata_, shapes.at(i).DebugString());
}
strings::StrAppend(&traceme_metadata_, ",types=");
auto& types = output_dtypes();
for (int i = 0; i < types.size(); ++i) {
if (i > 0) {
strings::StrAppend(&traceme_metadata_, " ");
}
strings::StrAppend(&traceme_metadata_, DataTypeString(types.at(i)));
}
}
DatasetBaseIterator::~DatasetBaseIterator() {
VLOG(2) << prefix() << " destructor";
params_.dataset->Unref();
}
string DatasetBaseIterator::BuildTraceMeName() {
string result =
strings::StrCat(params_.prefix, "#", traceme_metadata_, ",id=", id_);
if (parent_) {
strings::StrAppend(&result, ",parent_id=", parent_id_);
}
TraceMeMetadata metadata = GetTraceMeMetadata();
for (const auto& pair : metadata) {
strings::StrAppend(&result, ",", pair.first, "=", pair.second);
}
strings::StrAppend(&result, "#");
return result;
}
Status DatasetBaseIterator::GetNext(IteratorContext* ctx,
std::vector<Tensor>* out_tensors,
bool* end_of_sequence) {
profiler::TraceMe activity([&] { return BuildTraceMeName(); },
profiler::TraceMeLevel::kInfo);
DVLOG(3) << prefix() << " GetNext enter";
auto model = ctx->model();
if (model && model->collect_resource_usage() && node_) {
int64 now_nanos = EnvTime::NowNanos();
auto output = node_->output();
if (output) {
output->record_stop(now_nanos);
}
node_->record_start(now_nanos);
}
Status s = GetNextInternal(ctx, out_tensors, end_of_sequence);
if (TF_PREDICT_TRUE(s.ok() && !*end_of_sequence)) {
DCHECK_EQ(out_tensors->size(), dataset()->output_dtypes().size());
RecordElement(ctx, out_tensors);
}
if (model && model->collect_resource_usage() && node_) {
int64 now_nanos = EnvTime::NowNanos();
node_->record_stop(now_nanos);
auto output = node_->output();
if (output) {
output->record_start(now_nanos);
}
}
if (TF_PREDICT_FALSE(errors::IsOutOfRange(s))) {
s = errors::Internal("Iterator \"", params_.prefix,
"\" returned `OutOfRange`. This indicates an "
"implementation error as `OutOfRange` errors are not "
"expected to be returned here. Original message: ",
s.error_message());
LOG(ERROR) << s;
}
DVLOG(3) << prefix() << " GetNext exit";
return s;
}
Status DatasetBaseIterator::Skip(IteratorContext* ctx, int num_to_skip,
bool* end_of_sequence, int* num_skipped) {
profiler::TraceMe activity([&] { return BuildTraceMeName(); },
profiler::TraceMeLevel::kInfo);
DVLOG(3) << prefix() << " Skip enter";
auto model = ctx->model();
if (model && model->collect_resource_usage() && node_) {
int64 now_nanos = EnvTime::NowNanos();
auto output = node_->output();
if (output) {
output->record_stop(now_nanos);
}
node_->record_start(now_nanos);
}
Status s = SkipInternal(ctx, num_to_skip, end_of_sequence, num_skipped);
if (model && model->collect_resource_usage() && node_) {
int64 now_nanos = EnvTime::NowNanos();
node_->record_stop(now_nanos);
auto output = node_->output();
if (output) {
output->record_start(now_nanos);
}
}
if (TF_PREDICT_FALSE(errors::IsOutOfRange(s))) {
s = errors::Internal("Iterator \"", params_.prefix,
"\" returned `OutOfRange`. This indicates an "
"implementation error as `OutOfRange` errors are not "
"expected to be returned here. Original message: ",
s.error_message());
LOG(ERROR) << s;
}
DVLOG(3) << prefix() << " Skip exit";
return s;
}
Status DatasetBaseIterator::SkipInternal(IteratorContext* ctx, int num_to_skip,
bool* end_of_sequence,
int* num_skipped) {
*num_skipped = 0;
for (int i = 0; i < num_to_skip; ++i) {
std::vector<Tensor> out_tensors;
TF_RETURN_IF_ERROR(GetNextInternal(ctx, &out_tensors, end_of_sequence));
if (*end_of_sequence) {
return Status::OK();
}
// RecordElement is used to count the number of element computed and
// help calculate the CPU time spent on a given iterator to do the
// autotuning.
// Here we only call RecordElement in the default implementation of
// SkipInternal (which trivially calls GetNextInternal) and assume
// that the overriden SkipInternal in the derived class will have
// negligible cost compare to its GetNextInternal.
RecordElement(ctx, &out_tensors);
(*num_skipped)++;
}
return Status::OK();
}
void DatasetOpKernel::Compute(OpKernelContext* ctx) {
DatasetBase* dataset = nullptr;
MakeDataset(ctx, &dataset);
if (ctx->status().ok()) {
Tensor* output = nullptr;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &output));
OP_REQUIRES_OK(ctx, StoreDatasetInVariantTensor(dataset, output));
auto status = dataset->MergeOptionsFromInputs();
if (!status.ok()) {
LOG(ERROR) << status;
}
}
}
string DatasetOpKernel::TraceString(const OpKernelContext& ctx,
bool verbose) const {
return profiler::TraceMeOp(name_view(), type_string_view());
}
// static
bool DatasetOpKernel::IsDatasetOp(const OpDef* op_def) {
if (DatasetOpRegistry::IsRegistered(op_def->name())) {
return true;
}
return (op_def->output_arg_size() == 1 &&
op_def->output_arg(0).type() == DT_VARIANT &&
(absl::EndsWith(op_def->name(), "Dataset") ||
absl::EndsWith(op_def->name(), "DatasetV2")));
}
void UnaryDatasetOpKernel::MakeDataset(OpKernelContext* ctx,
DatasetBase** output) {
DatasetBase* input;
OP_REQUIRES_OK(ctx, GetDatasetFromVariantTensor(ctx->input(0), &input));
MakeDataset(ctx, input, output);
}
void BinaryDatasetOpKernel::MakeDataset(OpKernelContext* ctx,
DatasetBase** output) {
DatasetBase* input;
OP_REQUIRES_OK(ctx, GetDatasetFromVariantTensor(ctx->input(0), &input));
DatasetBase* another_input;
OP_REQUIRES_OK(ctx,
GetDatasetFromVariantTensor(ctx->input(1), &another_input));
MakeDataset(ctx, input, another_input, output);
}
const char DatasetBase::kDatasetGraphKey[] = "_DATASET_GRAPH";
const char DatasetBase::kDatasetGraphOutputNodeKey[] =
"_DATASET_GRAPH_OUTPUT_NODE";
BackgroundWorker::BackgroundWorker(Env* env, const char* name)
: env_(env), name_(name) {}
BackgroundWorker::~BackgroundWorker() {
{
mutex_lock l(mu_);
cancelled_ = true;
}
cond_var_.notify_one();
// Block until the background thread has terminated.
//
// NOTE(mrry): We explicitly free and join the thread here because
// `WorkerLoop()` uses other members of this object, and so we must join
// the thread before destroying them.
thread_.reset();
}
void BackgroundWorker::Schedule(std::function<void()> work_item) {
{
mutex_lock l(mu_);
if (!thread_) {
thread_ = absl::WrapUnique(env_->StartThread(
{} /* thread_options */, name_, [this]() { WorkerLoop(); }));
}
work_queue_.push_back(std::move(work_item));
}
cond_var_.notify_one();
}
void BackgroundWorker::WorkerLoop() {
tensorflow::ResourceTagger tag(kTFDataResourceTag, "Background");
while (true) {
std::function<void()> work_item = nullptr;
{
mutex_lock l(mu_);
while (!cancelled_ && work_queue_.empty()) {
cond_var_.wait(l);
}
if (cancelled_) {
return;
}
DCHECK(!work_queue_.empty());
work_item = std::move(work_queue_.front());
work_queue_.pop_front();
}
DCHECK(work_item != nullptr);
work_item();
}
}
// static
void DatasetOpRegistry::Register(const string& op_name) {
mutex_lock l(*get_dataset_op_registry_lock());
get_dataset_op_registry()->insert(op_name);
}
// static
bool DatasetOpRegistry::IsRegistered(const string& op_name) {
mutex_lock l(*get_dataset_op_registry_lock());
std::unordered_set<string>* op_names = get_dataset_op_registry();
return op_names->find(op_name) != op_names->end();
}
namespace {
class RunnerImpl : public Runner {
public:
void Run(const std::function<void()>& f) override {
tensorflow::ResourceTagger tag(kTFDataResourceTag, "Runner");
f();
// NOTE: We invoke a virtual function to prevent `f` being tail-called, and
// thus ensure that this function remains on the stack until after `f`
// returns.
PreventTailCall();
}
private:
virtual void PreventTailCall() {}
};
} // namespace
/* static */
Runner* Runner::get() {
static Runner* singleton = new RunnerImpl;
return singleton;
}
} // namespace data
} // namespace tensorflow