tf_1.8_xla_doc
graph_def_util.h
Go to the documentation of this file.
1 
3 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7  http://www.apache.org/licenses/LICENSE-2.0
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13 ==============================================================================*/
14 #ifndef TENSORFLOW_FRAMEWORK_GRAPH_DEF_UTIL_H_
15 #define TENSORFLOW_FRAMEWORK_GRAPH_DEF_UTIL_H_
16 #include <set>
17 #include "tensorflow/core/framework/op.h"
18 #include "tensorflow/core/lib/core/status.h"
19 namespace tensorflow {
20 // Forward declare proto so that it's symbols can be removed from .so exports
21 class GraphDef;
22 // Produce a human-readable version of a GraphDef that is more concise
23 // than a text-format proto.
24 string SummarizeGraphDef(const GraphDef& graph_def);
25 // Validates the syntax of a GraphDef provided externally.
26 //
27 // The following is an EBNF-style syntax for GraphDef objects. Note that
28 // Node objects are actually specified as tensorflow::NodeDef protocol buffers,
29 // which contain many other fields that are not (currently) validated.
30 //
31 // Graph = Node *
32 // Node = NodeName, Inputs
33 // Inputs = ( DataInput * ), ( ControlInput * )
34 // DataInput = NodeName, ( ":", [1-9], [0-9] * ) ?
35 // ControlInput = "^", NodeName
36 // NodeName = [A-Za-z0-9.], [A-Za-z0-9_./] *
37 Status ValidateExternalGraphDefSyntax(const GraphDef& graph_def);
38 // Adds default attributes to NodeDefs in 'graph_def' starting
39 // from the 'node_offset' node in 'graph_def'.
40 //
41 // Default attributes are defined by 'op_registry'.
42 //
43 // Returns OK on success, an error if 'graph_def' has a NodeDef
44 // that cannot be found in 'op_registry'.
45 //
46 // REQUIRES: 'graph_def' and 'op_registry' are not nullptr.
47 Status AddDefaultAttrsToGraphDef(GraphDef* graph_def,
48  const OpRegistryInterface& op_registry,
49  int node_offset);
50 // Same as above, except for the fact that it skips nodes that aren't found in
51 // op_registry if skip_unknown_ops is true.
52 Status AddDefaultAttrsToGraphDef(GraphDef* graph_def,
53  const OpRegistryInterface& op_registry,
54  int node_offset, bool skip_unknown_ops);
55 // Remove attrs from 'graph_def' that have the default value according
56 // to 'producer_op_registry', but don't exist according to
57 // 'consumer_op_registry'. This can allow 'graph_def' to run on the
58 // consumer even if consumer was built at an earlier CL (before an
59 // attr with a default was added). Note that this will not affect
60 // attrs with non-default values, so you must run a
61 // ValidateGraphDef...() function to see if the result is in fact
62 // compatible. If not nullptr, the op/attr pairs that were removed
63 // are added to '*op_attr_removed'.
64 //
65 // Expected usage, for a producer that wants to prepare a graph for
66 // a consumer:
67 // // For each consumer, update 'graph_def':
68 // OpListOpRegistry consumer_op_registry(consumer_server_op_list);
69 // std::unordered_set<std::pair<string, string>> op_attr_removed;
70 // TF_RETURN_IF_ERROR(RemoveNewDefaultAttrsFromGraphDef(
71 // &graph_def, consumer_op_registry, *OpRegistry::Global(),
72 // &op_attr_removed));
73 // // Validate that each consumer can understand the resulting 'graph_def'
74 // TF_RETURN_IF_ERROR(graph::ValidateGraphDefAgainstOpRegistry(
75 // graph_def, consumer_op_registry));
76 // // Consumer can use 'graph_def', and 'op_attr_removed' summarizes
77 // // what changes had to be made to 'graph_def' for it to work.
78 //
79 // Expected usage, for a consumer that has a graph and a
80 // (optionally-stripped) op_list from a producer (say from a call to
81 // StrippedOpListForGraph(), or in the MetaGraphDef):
82 // OpListOpRegistry producer_op_registry(producer_stripped_op_list);
83 // TF_RETURN_IF_ERROR(RemoveNewDefaultAttrsFromGraphDef(
84 // &graph_def, *OpRegistry::Global(), producer_op_registry, nullptr));
85 Status RemoveNewDefaultAttrsFromGraphDef(
86  GraphDef* graph_def, const OpRegistryInterface& consumer_op_registry,
87  const OpRegistryInterface& producer_op_registry,
88  std::set<std::pair<string, string>>* op_attr_removed);
89 // Two functions that collect the ops used by a graph.
90 //
91 // This returns the ops used as a set of strings.
92 void OpsUsedByGraph(const GraphDef& graph_def,
93  std::set<string>* ops_used_in_graph);
94 // This function computes the stripped_op_list field of MetaGraphDef
95 // and similar protos. The op_registry should contain the ops used to
96 // produce graph_def. The resulting stripped_op_list can be
97 // communicated from the producer to the consumer, which can use
98 // RemoveNewDefaultAttrsFromGraphDef() to improve forwards compatibility
99 // (using an OpListOpRegistry as indicated in the example above).
100 //
101 // Most users will pass *OpRegistry::Global() for op_registry to strip against
102 // the list of ops registered in this process.
103 Status StrippedOpListForGraph(const GraphDef& graph_def,
104  const OpRegistryInterface& op_registry,
105  OpList* stripped_op_list);
106 } // namespace tensorflow
107 #endif // TENSORFLOW_FRAMEWORK_GRAPH_DEF_UTIL_H_
Status AddDefaultAttrsToGraphDef(GraphDef *graph_def, const OpRegistryInterface &op_registry, int node_offset)
Assign each NodeDefs of GraphDef default values.
Definition: graph_def_util.cc:61
Definition: compile.cc:35