tf_1.8_xla_doc
graph_constructor.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_GRAPH_GRAPH_CONSTRUCTOR_H_
15 #define TENSORFLOW_GRAPH_GRAPH_CONSTRUCTOR_H_
16 #include "tensorflow/core/framework/graph.pb.h"
17 #include "tensorflow/core/graph/graph.h"
18 #include "tensorflow/core/graph/tensor_id.h"
19 #include "tensorflow/core/lib/core/status.h"
20 namespace tensorflow {
21 class ShapeRefiner;
22 // Construct a Graph *g out of a GraphDef gdef. Returns non-OK on
23 // error, in which case *g is left in an incomplete state.
24 //
25 // *g is expected to be an empty graph (with no more than a source and sink
26 // nodes) when provided to ConvertGraphDefToGraph. To enhance an existing Graph,
27 // see ImportGraphDef.
28 struct GraphConstructorOptions {
29  GraphConstructorOptions() {}
30  // If true, allows internal ops in the GraphDef.
31  bool allow_internal_ops = false;
32  // If true, the graph def is expected to have fully specified
33  // devices for all nodes. A node in the resulting graph "g" has the
34  // device name set accordingly.
35  //
36  // TODO(zhifengc): if possible, consider removing this option.
37  bool expect_device_spec = false;
38 };
39 extern Status ConvertGraphDefToGraph(const GraphConstructorOptions& opts,
40  const GraphDef& gdef, Graph* g);
41 // Same as ConvertGraphDefToGraph, but takes just nodes. Used by function
42 // instantiation.
43 // TODO(irving): This will turn into std::vector<NodeInfoPtr> soon.
44 extern Status ConvertNodeDefsToGraph(const GraphConstructorOptions& opts,
45  gtl::ArraySlice<NodeDef> nodes, Graph* g);
46 // Options for calling ImportGraphDef().
47 struct ImportGraphDefOptions {
48  ImportGraphDefOptions()
49  : uniquify_names(false),
50  uniquify_prefix(false),
51  skip_mapped_nodes(false),
52  validate_shape(true) {}
53  // Name prefix to use for nodes imported from the GraphDef. For example, if
54  // prefix="animals" and GraphDef contains a node "bunny" then the node will be
55  // named "animals/bunny" in *g. Must not be already used as a node name or
56  // prefix in the graph.
57  string prefix;
58  // If true, imported node names will be modified if their name already exists
59  // in the graph. If false, conflicting names will be treated as an error. Note
60  // that this option has no effect if `prefix` is specified, since `prefix`
61  // will guarantee all node names are unique.
62  bool uniquify_names;
63  // If true, `prefix` will be modified if it already exists as a node name or
64  // prefix in the graph. If false, a conflicting prefix will be treated as an
65  // error. This option has no effect if `prefix` isn't specified.
66  bool uniquify_prefix;
67  // Maps tensors in `gdef` to existing tensors in `g`. Inputs in `gdef`
68  // corresponding to `input_map` keys will be remapped to the nodes in `g`
69  // corresponding to the values.
70  //
71  // Keys should not include `prefix`, i.e., a key TensorId's name should be the
72  // name as it originally appears in `gdef`.
73  //
74  // If this is non-empty, ImportGraphDef must be called with the shape refiner
75  // used to create the existing nodes referenced in `input_map`.
76  // TODO(skyewm): can we remove this requirement? How do we access the original
77  // shape refiner?
78  std::map<TensorId, TensorId> input_map;
79  // If true, nodes that will have all output edges removed because of
80  // overrides in `input_map` will not be imported.
81  bool skip_mapped_nodes;
82  // The names of existing nodes in `g` that the imported graph should have
83  // control dependencies on.
84  //
85  // Note that to avoid creating many redundant control edges, ImportGraphDef()
86  // won't add control edges to nodes that will inherit the dependencies from
87  // other nodes in `gdef`.
88  std::vector<string> control_dependencies;
89  // Tensors in `gdef` that will be returned via the ImportGraphDefResults
90  // output parameter of `ImportGraphDef()`. If this list is non-empty, the
91  // caller must pass a results object to `ImportGraphDef()`. The
92  // `return_tensors` field will be populated with the imported nodes in `g`.
93  //
94  // Entries should not include `prefix`, i.e., each TensorId's name should be
95  // the name as it originally appears in `gdef`.
96  //
97  // If this contains a tensor that's also being remapped via `input_map`, the
98  // corresponding existing tensor in `g` will be returned.
99  std::vector<TensorId> return_tensors;
100  // The names of nodes in `gdef` that will be returned via the
101  // ImportGraphDefResults output parameter of `ImportGraphDef()`. If this list
102  // is non-empty, the caller must pass a results object to
103  // `ImportGraphDef()`. The `return_nodes` field will be populated with the
104  // imported nodes in `g`.
105  //
106  // Entries should not include `prefix`, i.e., each node's name should be the
107  // name as it originally appears in `gdef`.
108  //
109  // Unlike `return_tensors`, `input_map` has no effect on the nodes
110  // returned. `return_nodes` must be empty if `skip_mapped_nodes` is true.
111  // TODO(skyewm): make this work with `skip_mapped_nodes` if there's a need.
112  std::vector<string> return_nodes;
113  // If true, checks that all colocation constraints are nodes in the GraphDef.
114  bool validate_colocation_constraints = true;
115  // If false skips shape validation.
116  bool validate_shape;
117  // TODO(ashankar): Enable handling of GraphDefs produced by newer binaries
118  // with ops that are not defined in the binary calling ImportGraphDef.
119  // Similar to the producer_op_list argument to import_graph_def in the
120  // python API.
121 };
122 // Optional results that may be returned by ImportGraphDef.
123 struct ImportGraphDefResults {
124  // The requested tensors associated with
125  // ImportGraphDefOptions::return_tensors. Note that the index may be different
126  // than the requested index if the returned tensor has been remapped according
127  // to `input_map`.
128  typedef int Index;
129  std::vector<std::pair<Node*, Index>> return_tensors;
130  // The requested nodes associated with ImportGraphDefOptions::return_nodes.
131  std::vector<Node*> return_nodes;
132  // Keys in ImportGraphDefOptions::input_map that don't appear in `gdef` and
133  // weren't used as an input to any node in `gdef`. These keys are likely due
134  // to typos, and callers may wish to treat their existence as an error.
135  std::vector<TensorId> missing_unused_input_map_keys;
136 };
137 // Adds the graph in GraphDef `gdef` into an existing Graph `*g`.
138 //
139 // On error, returns non-OK and leaves `*g` unmodified.
140 //
141 // `refiner` can be null. It should be non-null if the caller
142 // intends to add additional nodes to the graph after the import. This
143 // allows the caller to validate shapes of those nodes (since
144 // ShapeRefiner::AddNode must be called in topological order).
145 //
146 // `results` must be non-null if `opts.return_tensors` or `opts.result_nodes` is
147 // non-empty. It can also be set to fetch the unused input map keys. If it's
148 // non-null, all the vector fields must be empty.
149 //
150 // TODO(ashankar): Push this mechanism and get rid of Session::Extend()
151 // as a means of enhancing an existing Graph.
152 extern Status ImportGraphDef(const ImportGraphDefOptions& opts,
153  const GraphDef& gdef, Graph* g,
154  ShapeRefiner* refiner,
155  ImportGraphDefResults* results = nullptr);
156 // Make a copy of "src" into "*dest".
157 //
158 // REQUIRES: "*dest" is a freshly allocated graph without any nodes or edges
159 // other than the implicit Source/Sink nodes.
160 extern void CopyGraph(const Graph& src, Graph* dest);
161 } // namespace tensorflow
162 #endif // TENSORFLOW_GRAPH_GRAPH_CONSTRUCTOR_H_
163 
Status ConvertGraphDefToGraph(const GraphConstructorOptions &opts, const GraphDef &gdef, Graph *g)
Convert GraphDef defined protobuf to type Graph
Definition: graph_constructor.cc:1079
Definition: compile.cc:35