/* 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/partial_tensor_shape.h" #include "tensorflow/core/framework/tensor.h" #include "tensorflow/core/kernels/data/dataset.h" namespace tensorflow { namespace data { namespace { // See documentation in ../ops/dataset_ops.cc for a high-level // description of the following op. class SkipDatasetOp : public UnaryDatasetOpKernel { public: explicit SkipDatasetOp(OpKernelConstruction* ctx) : UnaryDatasetOpKernel(ctx) {} void MakeDataset(OpKernelContext* ctx, DatasetBase* input, DatasetBase** output) override { // Create a new RepeatDatasetOp::Dataset, and return it as the output. int64 count; OP_REQUIRES_OK(ctx, ParseScalarArgument(ctx, "count", &count)); *output = new Dataset(ctx, count, input); } private: class Dataset : public DatasetBase { public: Dataset(OpKernelContext* ctx, int64 count, const DatasetBase* input) : DatasetBase(DatasetContext(ctx)), count_(count), input_(input) { input_->Ref(); } ~Dataset() override { input_->Unref(); } std::unique_ptr MakeIteratorInternal( const string& prefix) const override { if (count_ < 0) { return std::unique_ptr( new EmptyIterator({this, strings::StrCat(prefix, "::EmptySkip")})); } else { return std::unique_ptr(new FiniteIterator( {this, strings::StrCat(prefix, "::FiniteSkip")})); } } const DataTypeVector& output_dtypes() const override { return input_->output_dtypes(); } const std::vector& output_shapes() const override { return input_->output_shapes(); } string DebugString() const override { return "SkipDatasetOp::Dataset"; } protected: Status AsGraphDefInternal(SerializationContext* ctx, DatasetGraphDefBuilder* b, Node** output) const override { Node* input_graph_node = nullptr; TF_RETURN_IF_ERROR(b->AddInputDataset(ctx, input_, &input_graph_node)); Node* count = nullptr; TF_RETURN_IF_ERROR(b->AddScalar(count_, &count)); TF_RETURN_IF_ERROR( b->AddDataset(this, {input_graph_node, count}, output)); return Status::OK(); } private: class EmptyIterator : public DatasetIterator { public: explicit EmptyIterator(const Params& params) : DatasetIterator(params) {} Status GetNextInternal(IteratorContext* ctx, std::vector* out_tensors, bool* end_of_sequence) override { *end_of_sequence = true; return Status::OK(); } protected: Status SaveInternal(IteratorStateWriter* writer) override { return Status::OK(); } Status RestoreInternal(IteratorContext* ctx, IteratorStateReader* reader) override { return Status::OK(); } }; class FiniteIterator : public DatasetIterator { public: explicit FiniteIterator(const Params& params) : DatasetIterator(params), i_(0) {} Status Initialize(IteratorContext* ctx) override { return dataset()->input_->MakeIterator(ctx, prefix(), &input_impl_); } Status GetNextInternal(IteratorContext* ctx, std::vector* out_tensors, bool* end_of_sequence) override { mutex_lock l(mu_); // TODO(mrry): Make locking less conservative. if (!input_impl_) { *end_of_sequence = true; return Status::OK(); } // Keep calling GetNext(). TODO(vrv): Figure out a way to // skip records without reading, perhaps by adding an // interface to iterator. while (i_ < dataset()->count_) { // Fetch and throw away Tensors. std::vector dummy_out_tensors; TF_RETURN_IF_ERROR( input_impl_->GetNext(ctx, &dummy_out_tensors, end_of_sequence)); if (*end_of_sequence) { // We reached the end before the count was reached. input_impl_.reset(); return Status::OK(); } ++i_; } // Return GetNext() on the underlying iterator. TF_RETURN_IF_ERROR( input_impl_->GetNext(ctx, out_tensors, end_of_sequence)); if (*end_of_sequence) { input_impl_.reset(); } return Status::OK(); } protected: Status SaveInternal(IteratorStateWriter* writer) override { mutex_lock l(mu_); TF_RETURN_IF_ERROR(writer->WriteScalar(full_name("i"), i_)); if (input_impl_) { TF_RETURN_IF_ERROR(SaveInput(writer, input_impl_)); } else { TF_RETURN_IF_ERROR( writer->WriteScalar(full_name("input_impl_empty"), "")); } return Status::OK(); } Status RestoreInternal(IteratorContext* ctx, IteratorStateReader* reader) override { mutex_lock l(mu_); TF_RETURN_IF_ERROR(reader->ReadScalar(full_name("i"), &i_)); if (!reader->Contains(full_name("input_impl_empty"))) { TF_RETURN_IF_ERROR(RestoreInput(ctx, reader, input_impl_)); } else { input_impl_.reset(); } return Status::OK(); } private: mutex mu_; int64 i_ GUARDED_BY(mu_); std::unique_ptr input_impl_ GUARDED_BY(mu_); }; const int64 count_; const DatasetBase* const input_; }; }; REGISTER_KERNEL_BUILDER(Name("SkipDataset").Device(DEVICE_CPU), SkipDatasetOp); } // namespace } // namespace data } // namespace tensorflow