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 #ifndef TENSORFLOW_CORE_FRAMEWORK_GRAPH_DEF_UTIL_H_
17 #define TENSORFLOW_CORE_FRAMEWORK_GRAPH_DEF_UTIL_H_
18 
19 #include <set>
20 #include "tensorflow/core/framework/op.h"
21 #include "tensorflow/core/lib/core/status.h"
22 
23 namespace tensorflow {
24 
25 // Forward declare proto so that it's symbols can be removed from .so exports
26 class GraphDef;
27 
28 // Produce a human-readable version of a GraphDef that is more concise
29 // than a text-format proto.
30 string SummarizeGraphDef(const GraphDef& graph_def);
31 
32 // Validates the syntax of a GraphDef provided externally.
33 //
34 // The following is an EBNF-style syntax for GraphDef objects. Note that
35 // Node objects are actually specified as tensorflow::NodeDef protocol buffers,
36 // which contain many other fields that are not (currently) validated.
37 //
38 // Graph        = Node *
39 // Node         = NodeName, Inputs
40 // Inputs       = ( DataInput * ), ( ControlInput * )
41 // DataInput    = NodeName, ( ":", [1-9], [0-9] * ) ?
42 // ControlInput = "^", NodeName
43 // NodeName     = [A-Za-z0-9.], [A-Za-z0-9_./] *
44 Status ValidateExternalGraphDefSyntax(const GraphDef& graph_def);
45 
46 // Adds default attributes to NodeDefs in 'graph_def' starting
47 // from the 'node_offset' node in 'graph_def'.
48 //
49 // Default attributes are defined by 'op_registry'.
50 //
51 // Returns OK on success, an error if 'graph_def' has a NodeDef
52 // that cannot be found in 'op_registry'.
53 //
54 // REQUIRES: 'graph_def' and 'op_registry' are not nullptr.
55 Status AddDefaultAttrsToGraphDef(GraphDef* graph_def,
56                                  const OpRegistryInterface& op_registry,
57                                  int node_offset);
58 
59 // Same as above, except for the fact that it skips nodes that aren't found in
60 // op_registry if skip_unknown_ops is true.
61 Status AddDefaultAttrsToGraphDef(GraphDef* graph_def,
62                                  const OpRegistryInterface& op_registry,
63                                  int node_offset, bool skip_unknown_ops);
64 
65 // Remove attrs from 'graph_def' that have the default value according
66 // to 'producer_op_registry', but don't exist according to
67 // 'consumer_op_registry'. This can allow 'graph_def' to run on the
68 // consumer even if consumer was built at an earlier CL (before an
69 // attr with a default was added). Note that this will not affect
70 // attrs with non-default values, so you must run a
71 // ValidateGraphDef...() function to see if the result is in fact
72 // compatible. If not nullptr, the op/attr pairs that were removed
73 // are added to '*op_attr_removed'.
74 //
75 // Expected usage, for a producer that wants to prepare a graph for
76 // a consumer:
77 // // For each consumer, update 'graph_def':
78 //   OpListOpRegistry consumer_op_registry(consumer_server_op_list);
79 //   std::unordered_set<std::pair<string, string>> op_attr_removed;
80 //   TF_RETURN_IF_ERROR(RemoveNewDefaultAttrsFromGraphDef(
81 //       &graph_def, consumer_op_registry, *OpRegistry::Global(),
82 //       &op_attr_removed));
83 // // Validate that each consumer can understand the resulting 'graph_def'
84 //   TF_RETURN_IF_ERROR(graph::ValidateGraphDefAgainstOpRegistry(
85 //       graph_def, consumer_op_registry));
86 // // Consumer can use 'graph_def', and 'op_attr_removed' summarizes
87 // // what changes had to be made to 'graph_def' for it to work.
88 //
89 // Expected usage, for a consumer that has a graph and a
90 // (optionally-stripped) op_list from a producer (say from a call to
91 // StrippedOpListForGraph(), or in the MetaGraphDef):
92 //   OpListOpRegistry producer_op_registry(producer_stripped_op_list);
93 //   TF_RETURN_IF_ERROR(RemoveNewDefaultAttrsFromGraphDef(
94 //       &graph_def, *OpRegistry::Global(), producer_op_registry, nullptr));
95 Status RemoveNewDefaultAttrsFromGraphDef(
96     GraphDef* graph_def, const OpRegistryInterface& consumer_op_registry,
97     const OpRegistryInterface& producer_op_registry,
98     std::set<std::pair<string, string>>* op_attr_removed);
99 
100 // Two functions that collect the ops used by a graph.
101 //
102 // This returns the ops used as a set of strings.
103 void OpsUsedByGraph(const GraphDef& graph_def,
104                     std::set<string>* ops_used_in_graph);
105 
106 // This function computes the stripped_op_list field of MetaGraphDef
107 // and similar protos.  The op_registry should contain the ops used to
108 // produce graph_def.  The resulting stripped_op_list can be
109 // communicated from the producer to the consumer, which can use
110 // RemoveNewDefaultAttrsFromGraphDef() to improve forwards compatibility
111 // (using an OpListOpRegistry as indicated in the example above).
112 //
113 // Most users will pass *OpRegistry::Global() for op_registry to strip against
114 // the list of ops registered in this process.
115 Status StrippedOpListForGraph(const GraphDef& graph_def,
116                               const OpRegistryInterface& op_registry,
117                               OpList* stripped_op_list);
118 
119 }  // namespace tensorflow
120 
121 #endif  // TENSORFLOW_CORE_FRAMEWORK_GRAPH_DEF_UTIL_H_
122