1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/core/kernels/data/window_dataset.h"
16 #include "tensorflow/core/lib/core/errors.h"
17
18 namespace tensorflow {
19 namespace data {
20 namespace {
21
22 class WindowDataset : public DatasetBase {
23 public:
WindowDataset(std::vector<std::vector<Tensor>> elements,DataTypeVector output_types,std::vector<PartialTensorShape> output_shapes)24 WindowDataset(std::vector<std::vector<Tensor>> elements,
25 DataTypeVector output_types,
26 std::vector<PartialTensorShape> output_shapes)
27 : DatasetBase(DatasetContext({"Window"})),
28 elements_(std::move(elements)),
29 output_types_(std::move(output_types)),
30 output_shapes_(std::move(output_shapes)) {}
31
MakeIteratorInternal(const string & prefix) const32 std::unique_ptr<IteratorBase> MakeIteratorInternal(
33 const string& prefix) const override {
34 return absl::make_unique<Iterator>(
35 Iterator::Params{this, strings::StrCat(prefix, "::Window")});
36 }
37
output_dtypes() const38 const DataTypeVector& output_dtypes() const override { return output_types_; }
39
output_shapes() const40 const std::vector<PartialTensorShape>& output_shapes() const override {
41 return output_shapes_;
42 }
43
AllocatedBytes() const44 int64 AllocatedBytes() const override {
45 int64 allocated_bytes = 0;
46 for (auto& element : elements_) {
47 allocated_bytes += GetAllocatedBytes(element);
48 }
49 return allocated_bytes;
50 }
51
Cardinality() const52 int64 Cardinality() const override { return elements_.size(); }
53
DebugString() const54 string DebugString() const override { return "WindowDataset"; }
55
56 protected:
57 // TODO(b/110981596): Support checkpointing.
AsGraphDefInternal(SerializationContext * ctx,DatasetGraphDefBuilder * b,Node ** output) const58 Status AsGraphDefInternal(SerializationContext* ctx,
59 DatasetGraphDefBuilder* b,
60 Node** output) const override {
61 return errors::Unimplemented("%s does not support serialization",
62 DebugString());
63 }
64
65 private:
66 class Iterator : public DatasetIterator<WindowDataset> {
67 public:
Iterator(const Params & params)68 explicit Iterator(const Params& params)
69 : DatasetIterator<WindowDataset>(params) {}
70
GetNextInternal(IteratorContext * ctx,std::vector<Tensor> * out_tensors,bool * end_of_sequence)71 Status GetNextInternal(IteratorContext* ctx,
72 std::vector<Tensor>* out_tensors,
73 bool* end_of_sequence) override {
74 mutex_lock l(mu_);
75 if (i_ == dataset()->elements_.size()) {
76 *end_of_sequence = true;
77 } else {
78 *end_of_sequence = false;
79 *out_tensors = dataset()->elements_[i_++];
80 }
81 return Status::OK();
82 }
83
SaveInternal(IteratorStateWriter * writer)84 Status SaveInternal(IteratorStateWriter* writer) override {
85 mutex_lock l(mu_);
86 TF_RETURN_IF_ERROR(writer->WriteScalar(full_name("i"), i_));
87 return Status::OK();
88 }
89
RestoreInternal(IteratorContext * ctx,IteratorStateReader * reader)90 Status RestoreInternal(IteratorContext* ctx,
91 IteratorStateReader* reader) override {
92 mutex_lock l(mu_);
93 int64 i;
94 TF_RETURN_IF_ERROR(reader->ReadScalar(full_name("i"), &i));
95 i_ = size_t(i);
96 return Status::OK();
97 }
98
99 mutex mu_;
100 size_t i_ GUARDED_BY(mu_) = 0;
101 };
102
103 const std::vector<std::vector<Tensor>> elements_;
104 const DataTypeVector output_types_;
105 const std::vector<PartialTensorShape> output_shapes_;
106 };
107
108 } // namespace
109
NewWindowDataset(std::vector<std::vector<Tensor>> elements,DataTypeVector output_types,std::vector<PartialTensorShape> output_shapes,DatasetBase ** out_dataset)110 Status NewWindowDataset(std::vector<std::vector<Tensor>> elements,
111 DataTypeVector output_types,
112 std::vector<PartialTensorShape> output_shapes,
113 DatasetBase** out_dataset) {
114 // TODO(mrry): If this becomes more public, we must validate that
115 // the elements match the output_types and output_shapes.
116 *out_dataset = new WindowDataset(std::move(elements), std::move(output_types),
117 std::move(output_shapes));
118 return Status::OK();
119 }
120
121 } // namespace data
122 } // namespace tensorflow
123