1 /* Copyright 2018 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/framework/graph.pb.h"
17 #include "tensorflow/core/framework/node_def.pb.h"
18 #include "tensorflow/core/grappler/graph_analyzer/graph_analyzer.h"
19 #include "tensorflow/core/grappler/utils/transitive_fanin.h"
20 #include "tensorflow/core/lib/core/status.h"
21 #include "tensorflow/core/platform/env.h"
22 #include "tensorflow/core/platform/init_main.h"
23 #include "tensorflow/core/protobuf/meta_graph.pb.h"
24
25 namespace tensorflow {
26 namespace grappler {
27 namespace graph_analyzer {
28
29 // Dies on failure.
LoadModel(const string & filename,tensorflow::MetaGraphDef * metagraph)30 static void LoadModel(const string& filename,
31 tensorflow::MetaGraphDef* metagraph) {
32 LOG(INFO) << "Loading model from " << filename;
33 Status st;
34 st = ReadBinaryProto(Env::Default(), filename, metagraph);
35 if (!st.ok()) {
36 LOG(WARNING) << "Failed to read a binary metagraph: " << st;
37 st = ReadTextProto(Env::Default(), filename, metagraph);
38 if (!st.ok()) {
39 LOG(FATAL) << "Failed to read a text metagraph: " << st;
40 }
41 }
42 }
43
44 // Prune the graph to only keep the transitive fanin part with respect to a set
45 // of train ops (if provided).
MaybePruneGraph(const tensorflow::MetaGraphDef & metagraph,tensorflow::GraphDef * graph)46 void MaybePruneGraph(const tensorflow::MetaGraphDef& metagraph,
47 tensorflow::GraphDef* graph) {
48 std::vector<string> fetch_nodes;
49 for (const auto& fetch :
50 metagraph.collection_def().at("train_op").node_list().value()) {
51 LOG(INFO) << "Fetch node: " << fetch;
52 fetch_nodes.push_back(fetch);
53 }
54 if (fetch_nodes.empty()) {
55 *graph = metagraph.graph_def();
56 } else {
57 std::vector<const tensorflow::NodeDef*> fanin_nodes;
58 TF_CHECK_OK(tensorflow::grappler::ComputeTransitiveFanin(
59 metagraph.graph_def(), fetch_nodes, &fanin_nodes));
60 for (const tensorflow::NodeDef* node : fanin_nodes) {
61 *(graph->add_node()) = *node;
62 }
63 LOG(INFO) << "Pruned "
64 << metagraph.graph_def().node_size() - graph->node_size()
65 << " nodes. Original graph size: "
66 << metagraph.graph_def().node_size()
67 << ". New graph size: " << graph->node_size() << ".";
68 }
69 }
70
GraphAnalyzerTool(const string & file_name,int n)71 void GraphAnalyzerTool(const string& file_name, int n) {
72 if (n < 1) {
73 LOG(FATAL) << "Invalid subgraph size " << n << ", must be at least 1";
74 }
75
76 tensorflow::MetaGraphDef metagraph;
77 LoadModel(file_name, &metagraph);
78 tensorflow::GraphDef graph;
79 MaybePruneGraph(metagraph, &graph);
80 tensorflow::grappler::graph_analyzer::GraphAnalyzer analyzer(graph, n);
81 LOG(INFO) << "Running the analysis";
82 tensorflow::Status st = analyzer.Run();
83 if (!st.ok()) {
84 LOG(FATAL) << "Analysis failed: " << st;
85 }
86
87 LOG(INFO) << "Printing the result";
88 st = analyzer.OutputSubgraphs();
89 if (!st.ok()) {
90 LOG(FATAL) << "Failed to print the result: " << st;
91 }
92
93 LOG(INFO) << "Completed";
94 }
95
96 } // end namespace graph_analyzer
97 } // end namespace grappler
98 } // end namespace tensorflow
99