aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/data/kernels/indexed_dataset.cc
blob: ced8ab0d608cddc51ede38490bb8d9aecbe7da92 (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
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
/* 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/framework/resource_mgr.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/gtl/cleanup.h"

namespace tensorflow {
namespace data {
namespace {

Status VerifyTypesMatch(const DataTypeVector& expected,
                        const DataTypeVector& received) {
  if (expected.size() != received.size()) {
    return errors::InvalidArgument(
        "Number of components does not match: expected ", expected.size(),
        " types but got ", received.size(), ".");
  }
  for (size_t i = 0; i < expected.size(); ++i) {
    if (expected[i] != received[i]) {
      return errors::InvalidArgument("Data type mismatch at component ", i,
                                     ": expected ", DataTypeString(expected[i]),
                                     " but got ", DataTypeString(received[i]),
                                     ".");
    }
  }
  return Status::OK();
}

Status VerifyShapesCompatible(const std::vector<PartialTensorShape>& expected,
                              const std::vector<PartialTensorShape>& received) {
  if (expected.size() != received.size()) {
    return errors::InvalidArgument(
        "Number of components does not match: expected ", expected.size(),
        " shapes but got ", received.size(), ".");
  }
  for (size_t i = 0; i < expected.size(); ++i) {
    if (!expected[i].IsCompatibleWith(received[i])) {
      return errors::InvalidArgument("Incompatible shapes at component ", i,
                                     ": expected ", expected[i].DebugString(),
                                     " but got ", received[i].DebugString(),
                                     ".");
    }
  }

  return Status::OK();
}

class MaterializedDatasetResource : public ResourceBase {
 public:
  MaterializedDatasetResource(
      const DataTypeVector& output_dtypes,
      const std::vector<PartialTensorShape>& output_shapes)
      : output_dtypes_(output_dtypes), output_shapes_(output_shapes) {}

  string DebugString() override {
    return "Materialized IndexedDataset resource";
  }

  Status Get(IteratorContext&& ctx, uint64 index,
             std::vector<Tensor>* out_tensors) {
    std::shared_ptr<MaterializedIndexedDataset> captured(materialized_);
    if (captured) {
      return captured->Get(std::move(ctx), index, out_tensors);
    } else {
      return errors::FailedPrecondition(
          "Get() failed because the MaterializedIndexedDataset has not been "
          "initialized. Ensure that you have run the materialization operation "
          "for this MaterializedIndexedDataset before retrieving elements.");
    }
  }

  // TODO(saeta): Implement Save and Restore

  const DataTypeVector& output_dtypes() const { return output_dtypes_; }
  const std::vector<PartialTensorShape>& output_shapes() const {
    return output_shapes_;
  }

  Status set_materialized_dataset(
      const std::shared_ptr<MaterializedIndexedDataset>& dataset) {
    if (dataset) {
      TF_RETURN_IF_ERROR(
          VerifyTypesMatch(output_dtypes_, dataset->output_dtypes()));
      TF_RETURN_IF_ERROR(
          VerifyShapesCompatible(output_shapes_, dataset->output_shapes()));
    }
    materialized_ = dataset;
    return Status::OK();
  }

 private:
  std::shared_ptr<MaterializedIndexedDataset> materialized_;
  const DataTypeVector output_dtypes_;
  const std::vector<PartialTensorShape> output_shapes_;
};

// A wrapper class for storing an `IndexedDataset` instance in a DT_VARIANT
// tensor. Objects of the wrapper class own a reference on an instance of an
// `IndexedTensor` and the wrapper's copy constructor and desctructor take care
// of managing the reference count.
//
// NOTE: This is not a feature-complete implementation of the DT_VARIANT
// specification. In particular, we cannot currently serialize an arbitrary
// `IndexedDataset` object, so the `Encode()` and `Decode()` methods are not
// implemented.
//
// NOTE(saeta): When `IndexedDataset`s get merged into core, we can instead just
// use `tensorflow::DatasetVariantWrapper`.
class IndexedDatasetVariantWrapper {
 public:
  IndexedDatasetVariantWrapper() : dataset_(nullptr) {}

  // Transfers ownership of `dataset` to `*this`.
  explicit IndexedDatasetVariantWrapper(IndexedDataset* dataset)
      : dataset_(dataset) {}

  IndexedDatasetVariantWrapper(const IndexedDatasetVariantWrapper& other)
      : dataset_(other.dataset_) {
    if (dataset_) dataset_->Ref();
  }

  ~IndexedDatasetVariantWrapper() {
    if (dataset_) dataset_->Unref();
  }

  IndexedDataset* get() const { return dataset_; }

  string TypeName() const { return "tensorflow::IndexedDatasetVariantWrapper"; }
  string DebugString() const {
    if (dataset_) {
      return dataset_->DebugString();
    } else {
      return "<Uninitialized IndexedDatasetVariantWrapper>";
    }
  }

  void Encode(VariantTensorData* data) const {
    LOG(ERROR) << "The Encode() method is not implemented for "
                  "IndexedDatasetVariantWrapper objects.";
  }

  bool Decode(const VariantTensorData& data) {
    LOG(ERROR) << "The Decode() method is not implemented for "
                  "IndexedDatasetVariantWrapper objects.";
    return false;
  }

 private:
  IndexedDataset* const dataset_;  // Owns one reference.
};

}  // namespace

Status GetIndexedDatasetFromVariantTensor(const Tensor& tensor,
                                          IndexedDataset** out_dataset) {
  if (!(tensor.dtype() == DT_VARIANT ||
        TensorShapeUtils::IsScalar(tensor.shape()))) {
    return errors::InvalidArgument(
        "IndexedDataset tensor must be a scalar of dtype DT_VARIANT.");
  }
  const Variant& variant = tensor.scalar<Variant>()();
  const IndexedDatasetVariantWrapper* wrapper =
      variant.get<IndexedDatasetVariantWrapper>();
  if (wrapper == nullptr) {
    return errors::InvalidArgument("Tensor must be an IndexedDataset object.");
  }
  *out_dataset = wrapper->get();
  if (*out_dataset == nullptr) {
    return errors::Internal("Read uninitialized IndexedDataset variant.");
  }
  return Status::OK();
}

Status StoreIndexedDatasetInVariantTensor(IndexedDataset* 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>()() = IndexedDatasetVariantWrapper(dataset);
  return Status::OK();
}

void IndexedDatasetOpKernel::Compute(OpKernelContext* ctx) {
  IndexedDataset* dataset = nullptr;
  MakeIndexedDataset(ctx, &dataset);

  if (ctx->status().ok()) {
    OP_REQUIRES(ctx, dataset != nullptr,
                errors::Internal("MakeIndexedDataset did not correctly "
                                 "construct the IndexedDataset"));
    Tensor* output = nullptr;
    OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &output));
    OP_REQUIRES_OK(ctx, StoreIndexedDatasetInVariantTensor(dataset, output));
  }
}

namespace {

class MaterializedHandleOp : public OpKernel {
 public:
  explicit MaterializedHandleOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
    OP_REQUIRES_OK(ctx, ctx->GetAttr("output_types", &output_dtypes_));
    OP_REQUIRES_OK(ctx, ctx->GetAttr("output_shapes", &output_shapes_));
  }

  ~MaterializedHandleOp() override {
    if (resource_ != nullptr) {
      resource_->Unref();
      if (cinfo_.resource_is_private_to_kernel()) {
        if (!cinfo_.resource_manager()
                 ->template Delete<MaterializedDatasetResource>(
                     cinfo_.container(), cinfo_.name())
                 .ok()) {
          // Do nothing; the resource can have been deleted by session resets.
          // Note: cargo-culted from $tf/core/framework/resource_op_kernel.h
        }
      }
    }
  }

  void Compute(OpKernelContext* context) override LOCKS_EXCLUDED(mu_) {
    {
      mutex_lock l(mu_);
      if (resource_ == nullptr) {
        ResourceMgr* mgr = context->resource_manager();
        OP_REQUIRES_OK(context, cinfo_.Init(mgr, def()));

        MaterializedDatasetResource* resource;
        OP_REQUIRES_OK(context,
                       mgr->LookupOrCreate<MaterializedDatasetResource>(
                           cinfo_.container(), cinfo_.name(), &resource,
                           [this](MaterializedDatasetResource** ret)
                               EXCLUSIVE_LOCKS_REQUIRED(mu_) {
                                 *ret = new MaterializedDatasetResource(
                                     output_dtypes_, output_shapes_);
                                 return Status::OK();
                               }));
        Status s = VerifyResource(resource);
        if (TF_PREDICT_FALSE(!s.ok())) {
          resource->Unref();
          context->SetStatus(s);
          return;
        }

        resource_ = resource;
      }
    }
    OP_REQUIRES_OK(context, MakeResourceHandleToOutput(
                                context, 0, cinfo_.container(), cinfo_.name(),
                                MakeTypeIndex<MaterializedDatasetResource>()));
  }

 private:
  // During the first Compute(), resource is either created or looked up using
  // shared_name. In the latter case, the resource found should be verified if
  // it is compatible with this op's configuration. The verification may fail in
  // cases such as two graphs asking queues of the same shared name to have
  // inconsistent capacities.
  Status VerifyResource(MaterializedDatasetResource* resource) {
    TF_RETURN_IF_ERROR(
        VerifyTypesMatch(output_dtypes_, resource->output_dtypes()));
    TF_RETURN_IF_ERROR(
        VerifyShapesCompatible(output_shapes_, resource->output_shapes()));
    return Status::OK();
  }

  mutex mu_;
  ContainerInfo cinfo_;  // Written once under mu_ then constant afterwards.
  MaterializedDatasetResource* resource_ GUARDED_BY(mu_) = nullptr;
  DataTypeVector output_dtypes_;
  std::vector<PartialTensorShape> output_shapes_;
};

// TODO(saeta): Make async.
class MaterializeDatasetOp : public OpKernel {
 public:
  explicit MaterializeDatasetOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}

  void Compute(OpKernelContext* ctx) override {
    IndexedDataset* dataset;
    OP_REQUIRES_OK(ctx,
                   GetIndexedDatasetFromVariantTensor(ctx->input(0), &dataset));

    MaterializedDatasetResource* materialized_resource;
    OP_REQUIRES_OK(ctx, LookupResource(ctx, HandleFromInput(ctx, 1),
                                       &materialized_resource));
    core::ScopedUnref unref(materialized_resource);
    std::shared_ptr<MaterializedIndexedDataset> materialized;
    OP_REQUIRES_OK(ctx, dataset->MaterializeDataset(&materialized));
    OP_REQUIRES_OK(
        ctx, materialized_resource->set_materialized_dataset(materialized));
  }
};

// TODO(saeta): Make async
class IndexedDatasetGet : public OpKernel {
 public:
  explicit IndexedDatasetGet(OpKernelConstruction* ctx) : OpKernel(ctx) {}

  void Compute(OpKernelContext* ctx) override {
    MaterializedDatasetResource* materialized_resource;
    OP_REQUIRES_OK(ctx, LookupResource(ctx, HandleFromInput(ctx, 0),
                                       &materialized_resource));
    auto cleanup = gtl::MakeCleanup([materialized_resource] {
      materialized_resource->Unref();  // Note: can't use core::ScopedUnref.
    });

    const Tensor* index_t;
    OP_REQUIRES_OK(ctx, ctx->input("index", &index_t));
    // TODO(saeta): Support batch reads (indexes should be non-scalar!)
    OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(index_t->shape()),
                errors::InvalidArgument("index must be a scalar"));
    const uint64 index = index_t->scalar<uint64>()();

    std::vector<Tensor> out_tensors;
    Status s =
        materialized_resource->Get(IteratorContext(ctx), index, &out_tensors);

    // Note: Unref materialized_resource to avoid destruction races. (Important
    // in a [future] async op implementation.)
    cleanup.release()();

    if (!s.ok()) {
      ctx->SetStatus(s);
    } else {
      auto expected_shapes = materialized_resource->output_shapes();
      auto expected_types = materialized_resource->output_dtypes();
      for (size_t i = 0; i < out_tensors.size(); ++i) {
        OP_REQUIRES(
            ctx, expected_shapes[i].IsCompatibleWith(out_tensors[i].shape()),
            errors::Internal(
                "Materialized dataset output at index ", i,
                " is incompatible with the expected shape. (Expected: ",
                expected_shapes[i], ", got: ", out_tensors[i].shape(), ")"));
        OP_REQUIRES(ctx, out_tensors[i].dtype() == expected_types[i],
                    errors::Internal("Materialized dataset output at index ", i,
                                     " was not the expected dtype. (Expected: ",
                                     expected_types[i],
                                     ", got: ", out_tensors[i].dtype(), ")"));
        ctx->set_output(i, out_tensors[i]);
      }
    }
  }
};

REGISTER_KERNEL_BUILDER(
    Name("MaterializedIndexDatasetHandle").Device(DEVICE_CPU),
    MaterializedHandleOp);
REGISTER_KERNEL_BUILDER(Name("IndexedDatasetMaterialize").Device(DEVICE_CPU),
                        MaterializeDatasetOp);
REGISTER_KERNEL_BUILDER(Name("IndexedDatasetGet").Device(DEVICE_CPU),
                        IndexedDatasetGet);

}  // namespace
}  // namespace data
}  // namespace tensorflow