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