Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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 
     16 // See docs in ../ops/io_ops.cc.
     17 
     18 #include <memory>
     19 #include "tensorflow/core/framework/reader_base.h"
     20 #include "tensorflow/core/framework/reader_base.pb.h"
     21 #include "tensorflow/core/framework/reader_op_kernel.h"
     22 #include "tensorflow/core/lib/core/errors.h"
     23 #include "tensorflow/core/lib/strings/str_util.h"
     24 #include "tensorflow/core/lib/strings/strcat.h"
     25 #include "tensorflow/core/platform/protobuf.h"
     26 
     27 namespace tensorflow {
     28 
     29 class IdentityReader : public ReaderBase {
     30  public:
     31   explicit IdentityReader(const string& node_name)
     32       : ReaderBase(strings::StrCat("IdentityReader '", node_name, "'")) {}
     33 
     34   Status ReadLocked(string* key, string* value, bool* produced,
     35                     bool* at_end) override {
     36     *key = current_work();
     37     *value = current_work();
     38     *produced = true;
     39     *at_end = true;
     40     return Status::OK();
     41   }
     42 
     43   // Stores state in a ReaderBaseState proto, since IdentityReader has
     44   // no additional state beyond ReaderBase.
     45   Status SerializeStateLocked(string* state) override {
     46     ReaderBaseState base_state;
     47     SaveBaseState(&base_state);
     48     base_state.SerializeToString(state);
     49     return Status::OK();
     50   }
     51 
     52   Status RestoreStateLocked(const string& state) override {
     53     ReaderBaseState base_state;
     54     if (!ParseProtoUnlimited(&base_state, state)) {
     55       return errors::InvalidArgument("Could not parse state for ", name(), ": ",
     56                                      str_util::CEscape(state));
     57     }
     58     TF_RETURN_IF_ERROR(RestoreBaseState(base_state));
     59     return Status::OK();
     60   }
     61 };
     62 
     63 class IdentityReaderOp : public ReaderOpKernel {
     64  public:
     65   explicit IdentityReaderOp(OpKernelConstruction* context)
     66       : ReaderOpKernel(context) {
     67     SetReaderFactory([this]() { return new IdentityReader(name()); });
     68   }
     69 };
     70 
     71 REGISTER_KERNEL_BUILDER(Name("IdentityReader").Device(DEVICE_CPU),
     72                         IdentityReaderOp);
     73 REGISTER_KERNEL_BUILDER(Name("IdentityReaderV2").Device(DEVICE_CPU),
     74                         IdentityReaderOp);
     75 
     76 }  // namespace tensorflow
     77