1 /* Copyright 2017 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 #include "tensorflow/core/grappler/inputs/file_input_yielder.h"
17
18 #include <memory>
19 #include <unordered_set>
20 #include <utility>
21
22 #include "tensorflow/core/framework/node_def.pb.h"
23 #include "tensorflow/core/grappler/grappler_item.h"
24 #include "tensorflow/core/grappler/grappler_item_builder.h"
25 #include "tensorflow/core/grappler/utils.h"
26 #include "tensorflow/core/lib/strings/strcat.h"
27 #include "tensorflow/core/platform/env.h"
28 #include "tensorflow/core/platform/fingerprint.h"
29 #include "tensorflow/core/platform/types.h"
30 #include "tensorflow/core/protobuf/meta_graph.pb.h"
31
32 namespace tensorflow {
33 namespace grappler {
34
FileInputYielder(const std::vector<string> & filenames,size_t max_iterations)35 FileInputYielder::FileInputYielder(const std::vector<string>& filenames,
36 size_t max_iterations)
37 : filenames_(filenames),
38 current_file_(0),
39 current_iteration_(0),
40 max_iterations_(max_iterations),
41 bad_inputs_(0) {
42 CHECK_GT(filenames.size(), 0) << "List of filenames is empty.";
43 }
44
NextItem(GrapplerItem * item)45 bool FileInputYielder::NextItem(GrapplerItem* item) {
46 if (filenames_.size() == bad_inputs_) {
47 // All the input files are bad, give up.
48 return false;
49 }
50
51 if (current_file_ >= filenames_.size()) {
52 if (current_iteration_ >= max_iterations_) {
53 return false;
54 } else {
55 ++current_iteration_;
56 current_file_ = 0;
57 bad_inputs_ = 0;
58 }
59 }
60
61 const string& filename = filenames_[current_file_];
62 ++current_file_;
63
64 if (!Env::Default()->FileExists(filename).ok()) {
65 LOG(WARNING) << "Skipping non existent file " << filename;
66 // Attempt to process the next item on the list
67 bad_inputs_ += 1;
68 return NextItem(item);
69 }
70
71 LOG(INFO) << "Loading model from " << filename;
72
73 MetaGraphDef metagraph;
74 Status s = ReadBinaryProto(Env::Default(), filename, &metagraph);
75 if (!s.ok()) {
76 s = ReadTextProto(Env::Default(), filename, &metagraph);
77 }
78 if (!s.ok()) {
79 LOG(WARNING) << "Failed to read MetaGraphDef from " << filename << ": "
80 << s.ToString();
81 // Attempt to process the next item on the list
82 bad_inputs_ += 1;
83 return NextItem(item);
84 }
85
86 if (metagraph.collection_def().count("train_op") == 0 ||
87 !metagraph.collection_def().at("train_op").has_node_list() ||
88 metagraph.collection_def().at("train_op").node_list().value_size() == 0) {
89 LOG(ERROR) << "No train op specified";
90 bad_inputs_ += 1;
91 metagraph = MetaGraphDef();
92 return NextItem(item);
93 } else {
94 std::unordered_set<string> train_ops;
95 for (const string& val :
96 metagraph.collection_def().at("train_op").node_list().value()) {
97 train_ops.insert(NodeName(val));
98 }
99 std::unordered_set<string> train_ops_found;
100 for (auto& node : metagraph.graph_def().node()) {
101 if (train_ops.find(node.name()) != train_ops.end()) {
102 train_ops_found.insert(node.name());
103 }
104 }
105 if (train_ops_found.size() != train_ops.size()) {
106 for (const auto& train_op : train_ops) {
107 if (train_ops_found.find(train_op) != train_ops_found.end()) {
108 LOG(ERROR) << "Non existent train op specified: " << train_op;
109 }
110 }
111 bad_inputs_ += 1;
112 metagraph = MetaGraphDef();
113 return NextItem(item);
114 }
115 }
116
117 const string id =
118 strings::StrCat(Fingerprint64(metagraph.SerializeAsString()));
119
120 ItemConfig cfg;
121 std::unique_ptr<GrapplerItem> new_item =
122 GrapplerItemFromMetaGraphDef(id, metagraph, cfg);
123 if (new_item == nullptr) {
124 bad_inputs_ += 1;
125 metagraph = MetaGraphDef();
126 return NextItem(item);
127 }
128
129 *item = std::move(*new_item);
130 return true;
131 }
132
133 } // end namespace grappler
134 } // end namespace tensorflow
135