aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/data/kernels/identity_indexed_dataset.cc
blob: 4718c1c8b9d77b5dbac2a8caf11d9a0604af94c2 (plain)
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
/* 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/contrib/data/kernels/indexed_dataset.h"
#include "tensorflow/core/lib/core/errors.h"

namespace tensorflow {
namespace {

class IdentityIndexedDatasetOp : public IndexedDatasetOpKernel {
 public:
  using IndexedDatasetOpKernel::IndexedDatasetOpKernel;

  void MakeIndexedDataset(OpKernelContext* ctx,
                          IndexedDataset** output) override {
    uint64 size = -1;
    OP_REQUIRES_OK(ctx, ParseScalarArgument<uint64>(ctx, "size", &size));
    OP_REQUIRES(ctx, size > 0, errors::InvalidArgument("`size` must be > 0"));
    *output = new Dataset(ctx, size);
  }

  class Dataset : public IndexedDataset {
   public:
    Dataset(OpKernelContext* ctx, uint64 size)
        : IndexedDataset(DatasetContext(ctx)), size_(size) {}

    Status MaterializeDataset(
        std::shared_ptr<MaterializedIndexedDataset>* materialized) override {
      materialized->reset(new Materialized(this));
      return Status::OK();
    }

    const DataTypeVector& output_dtypes() const override {
      static DataTypeVector* dtypes = new DataTypeVector({DT_UINT64});
      return *dtypes;
    }

    const std::vector<PartialTensorShape>& output_shapes() const override {
      static std::vector<PartialTensorShape>* shapes =
          new std::vector<PartialTensorShape>({{}});
      return *shapes;
    }

    std::unique_ptr<IteratorBase> MakeIteratorInternal(
        const string& prefix) const override {
      return std::unique_ptr<IteratorBase>(new Iterator(
          {this, strings::StrCat(prefix, "::IdentityIndexedDataset")}));
    }

    string DebugString() const override {
      return "IdentityIndexedDataset::Dataset";
    }

    Status AsGraphDefInternal(SerializationContext* ctx,
                              DatasetGraphDefBuilder* b,
                              Node** node) const override {
      return errors::Unimplemented(
          "identity_indexed_dataset.AsGraphDefInternal");
    }

   private:
    class Iterator : public DatasetIterator<Dataset> {
     public:
      explicit Iterator(const Params& params)
          : DatasetIterator<Dataset>(params) {}
      Status GetNextInternal(IteratorContext* ctx,
                             std::vector<Tensor>* out_tensors,
                             bool* end_of_sequence) override {
        mutex_lock l(mu_);
        if (cur_ < dataset()->size_) {
          Tensor result_tensor(ctx->allocator({}), DT_UINT64, {});
          result_tensor.scalar<uint64>()() = cur_++;
          out_tensors->emplace_back(std::move(result_tensor));
          *end_of_sequence = false;
          return Status::OK();
        }
        *end_of_sequence = true;
        return Status::OK();
      }

     private:
      mutex mu_;
      uint64 cur_ GUARDED_BY(mu_);
    };

    class Materialized : public MaterializedIndexedDataset {
     public:
      explicit Materialized(Dataset* dataset) : dataset_(dataset) {
        dataset->Ref();
      }

      ~Materialized() override {
        // TODO(saeta): Pull this into MaterializedIndexedDataset
        dataset_->Unref();
      }

      const DataTypeVector& output_dtypes() const override {
        return dataset_->output_dtypes();
      }

      const std::vector<PartialTensorShape>& output_shapes() const override {
        return dataset_->output_shapes();
      }

      Status Get(IteratorContext&& ctx, uint64 index,
                 std::vector<Tensor>* out_tensors) const override {
        LOG(INFO) << "Materialized(" << dataset_->size_ << ")::Get(" << index
                  << ")";
        if (index >= dataset_->size_) {
          // Note: use InvalidArgument instead of OutOfRange error because many
          // things consider OutOfRange to be a "clean termination" error.
          return errors::InvalidArgument(
              "Index ", index,
              " is out of range for this dataset. (Size is: ", dataset_->size_,
              ".)");
        }
        Tensor result_tensor(ctx.allocator({}), DT_UINT64, {});
        result_tensor.scalar<uint64>()() = index;
        out_tensors->emplace_back(std::move(result_tensor));
        return Status::OK();
      }

      Status Size(uint64* size) const override {
        *size = dataset_->size_;
        return Status::OK();
      }

     private:
      const Dataset* const dataset_;  // Not owned.
    };

    const uint64 size_;
    std::shared_ptr<Materialized> materialized_;
  };
};

REGISTER_KERNEL_BUILDER(Name("IdentityIndexedDataset").Device(DEVICE_CPU),
                        IdentityIndexedDatasetOp);

}  // namespace
}  // namespace tensorflow