• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Helpers to manipulate a tensor graph in python.
16"""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21import copy
22import re
23
24import six
25
26from tensorflow.core.framework import graph_pb2
27from tensorflow.core.framework import node_def_pb2
28from tensorflow.python.framework import _proto_comparators
29from tensorflow.python.framework import dtypes
30from tensorflow.python.framework import ops
31from tensorflow.python.util import deprecation
32from tensorflow.python.util import lazy_loader
33from tensorflow.python.util.tf_export import tf_export
34
35tf_export(v1=["GraphDef"])(graph_pb2.GraphDef)
36
37# A normal import here would generate circular dependencies.
38convert_to_constants = lazy_loader.LazyLoader(
39    "convert_to_constants", globals(),
40    "tensorflow.python.framework.convert_to_constants")
41
42_VARIABLE_OPS = {
43    "Assign",
44    "AssignAdd",
45    "AssignSub",
46    "Queue",
47    "ScatterAdd",
48    "ScatterSub",
49    "ScatterUpdate",
50    "TruncatedNormal",
51    "Variable",
52    "VariableV2",
53}
54
55_CONTROL_FLOW_OP_NAMES_OR_IDENTITY = [
56    "Switch",
57    "Enter",
58    "Exit",
59    "Identity",
60    "Merge",
61    "NextIteration",
62]
63
64
65def _is_variable_op(op):
66  """Returns true if 'op' refers to a Variable node."""
67  return op in _VARIABLE_OPS
68
69# GraphDef protobuf docstring.
70graph_pb2.GraphDef.__doc__ = """\
71A protobuf containing the graph of operations.
72
73@compatibility(TF2)
74This API is not available in TensorFlow 2.x.
75
76You should not need to use `GraphDef`s directly in TF2. To load `GraphDef`s in
77TF2, use SavedModel. The SavedModel contains the `GraphDef`.
78
79Before:
80
81```python
82with tf.io.gfile.GFile('/tmp/graph.pb', 'rb') as f:
83  graph_def = tf.compat.v1.GraphDef()
84  graph_def.ParseFromString(f.read())
85```
86
87After:
88
89```python
90tf.saved_model.load('/tmp/saved_model')
91```
92
93If you would like to create a `GraphDef` in TF2, use `tf.function` and
94`get_concrete_function`.
95
96>>> @tf.function
97>>> def f(x):
98>>>   return x
99>>>
100>>> graph_def = f.get_concrete_function(1.).graph.as_graph_def()
101>>> print(graph_def)
102
103@end_compatibility
104
105"""
106
107
108@deprecation.deprecated(
109    date=None,
110    instructions="Use `tf.compat.v1.graph_util.must_run_on_cpu`")
111@tf_export(v1=["graph_util.must_run_on_cpu"])
112def must_run_on_cpu(node, pin_variables_on_cpu=False):
113  """Returns True if the given node_def must run on CPU, otherwise False.
114
115  Args:
116    node: The node to be assigned to a device. Could be either an ops.Operation
117      or NodeDef.
118    pin_variables_on_cpu: If True, this function will return False if node_def
119      represents a variable-related op.
120
121  Returns:
122    True if the given node must run on CPU, otherwise False.
123  """
124
125  if isinstance(node, ops.Operation):
126    node_def = node.node_def
127  else:
128    assert isinstance(node, node_def_pb2.NodeDef)
129    node_def = node
130
131  # If the op is a variable-related op, should we pin it on CPU?
132  if pin_variables_on_cpu and _is_variable_op(node_def.op):
133    return True
134
135  # Constant operations producing a string or int32 must run on CPU.
136  if node_def.op == "Const":
137    # Get the value of the 'dtype' attr
138    dtype = node_def.attr["dtype"].type
139    if dtype == dtypes.string or dtype == dtypes.int32:
140      return True
141
142  if node_def.op in ["DynamicStitch", "ParallelDynamicStitch"]:
143    dtype = node_def.attr["T"].type
144    if dtype == dtypes.int32:
145      # DynamicStitch on GPU only works for int32 values.
146      return True
147
148  if node_def.op in ["Cast"]:
149    dtype = node_def.attr["SrcT"].type
150    if dtype == dtypes.int32:
151      # Cast on GPU does not works for int32 values.
152      return True
153  return False
154
155
156################################################################################
157#
158# device functions for use in with g.device(...)
159#
160################################################################################
161
162
163def _node_name(n):
164  if n.startswith("^"):
165    return n[1:]
166  else:
167    return n.split(":")[0]
168
169
170def _get_colocated_node_name(colocated_node_name):
171  """Decodes colocated node name and returns it without loc:@ prepended."""
172  colocated_node_decoded = colocated_node_name.decode("utf-8")
173  if colocated_node_decoded.startswith("loc:@"):
174    return colocated_node_decoded[5:]
175  return colocated_node_decoded
176
177
178def _extract_graph_summary(graph_def):
179  """Extracts useful information from the graph and returns them."""
180  name_to_input_name = {}  # Keyed by the dest node name.
181  name_to_node = {}  # Keyed by node name.
182
183  # Keeps track of node sequences. It is important to still output the
184  # operations in the original order.
185  name_to_seq_num = {}  # Keyed by node name.
186  seq = 0
187  for node in graph_def.node:
188    n = _node_name(node.name)
189    name_to_node[n] = node
190    name_to_input_name[n] = [_node_name(x) for x in node.input]
191    # Prevent colocated nodes from being lost.
192    if "_class" in node.attr:
193      for colocated_node_name in node.attr["_class"].list.s:
194        name_to_input_name[n].append(
195            _get_colocated_node_name(colocated_node_name))
196    name_to_seq_num[n] = seq
197    seq += 1
198  return name_to_input_name, name_to_node, name_to_seq_num
199
200
201def _assert_nodes_are_present(name_to_node, nodes):
202  """Assert that nodes are present in the graph."""
203  for d in nodes:
204    assert d in name_to_node, "%s is not in graph" % d
205
206
207def _bfs_for_reachable_nodes(target_nodes, name_to_input_name):
208  """Breadth first search for reachable nodes from target nodes."""
209  nodes_to_keep = set()
210  # Breadth first search to find all the nodes that we should keep.
211  next_to_visit = list(target_nodes)
212  while next_to_visit:
213    node = next_to_visit[0]
214    del next_to_visit[0]
215    if node in nodes_to_keep:
216      # Already visited this node.
217      continue
218    nodes_to_keep.add(node)
219    if node in name_to_input_name:
220      next_to_visit += name_to_input_name[node]
221  return nodes_to_keep
222
223
224@deprecation.deprecated(
225    date=None,
226    instructions="Use `tf.compat.v1.graph_util.extract_sub_graph`")
227@tf_export(v1=["graph_util.extract_sub_graph"])
228def extract_sub_graph(graph_def, dest_nodes):
229  """Extract the subgraph that can reach any of the nodes in 'dest_nodes'.
230
231  Args:
232    graph_def: A graph_pb2.GraphDef proto.
233    dest_nodes: An iterable of strings specifying the destination node names.
234  Returns:
235    The GraphDef of the sub-graph.
236
237  Raises:
238    TypeError: If 'graph_def' is not a graph_pb2.GraphDef proto.
239  """
240
241  if not isinstance(graph_def, graph_pb2.GraphDef):
242    raise TypeError("graph_def must be a graph_pb2.GraphDef proto.")
243
244  if isinstance(dest_nodes, six.string_types):
245    raise TypeError("dest_nodes must be an iterable of strings.")
246
247  name_to_input_name, name_to_node, name_to_seq_num = _extract_graph_summary(
248      graph_def)
249  _assert_nodes_are_present(name_to_node, dest_nodes)
250
251  nodes_to_keep = _bfs_for_reachable_nodes(dest_nodes, name_to_input_name)
252
253  nodes_to_keep_list = sorted(
254      list(nodes_to_keep), key=lambda n: name_to_seq_num[n])
255  # Now construct the output GraphDef
256  out = graph_pb2.GraphDef()
257  for n in nodes_to_keep_list:
258    out.node.extend([copy.deepcopy(name_to_node[n])])
259  out.library.CopyFrom(graph_def.library)
260  out.versions.CopyFrom(graph_def.versions)
261
262  return out
263
264
265@deprecation.deprecated(
266    date=None,
267    instructions="Use `tf.compat.v1.graph_util.tensor_shape_from_node_def_name`"
268)
269@tf_export(v1=["graph_util.tensor_shape_from_node_def_name"])
270def tensor_shape_from_node_def_name(graph, input_name):
271  """Convenience function to get a shape from a NodeDef's input string."""
272  # To get a tensor, the name must be in the form <input>:<port>, for example
273  # 'Mul:0'. The GraphDef input strings don't always have the port specified
274  # though, so if there isn't a colon we need to add a default ':0' to the end.
275  if ":" not in input_name:
276    canonical_name = input_name + ":0"
277  else:
278    canonical_name = input_name
279  tensor = graph.get_tensor_by_name(canonical_name)
280  shape = tensor.get_shape()
281  return shape
282
283
284@deprecation.deprecated(
285    date=None,
286    instructions="Use `tf.compat.v1.graph_util.convert_variables_to_constants`")
287@tf_export(v1=["graph_util.convert_variables_to_constants"])
288def convert_variables_to_constants(sess,
289                                   input_graph_def,
290                                   output_node_names,
291                                   variable_names_whitelist=None,
292                                   variable_names_blacklist=None):
293  """Replaces all the variables in a graph with constants of the same values.
294
295  If you have a trained graph containing Variable ops, it can be convenient to
296  convert them all to Const ops holding the same values. This makes it possible
297  to describe the network fully with a single GraphDef file, and allows the
298  removal of a lot of ops related to loading and saving the variables.
299
300  Args:
301    sess: Active TensorFlow session containing the variables.
302    input_graph_def: GraphDef object holding the network.
303    output_node_names: List of name strings for the result nodes of the graph.
304    variable_names_whitelist: The set of variable names to convert (by default,
305                              all variables are converted).
306    variable_names_blacklist: The set of variable names to omit converting
307                              to constants.
308
309  Returns:
310    GraphDef containing a simplified version of the original.
311
312  Raises:
313    RuntimeError: if a DT_RESOURCE op is found whose ancestor Variables are both
314      denylisted AND whitelisted for freezing.
315  """
316  ret = convert_to_constants.convert_variables_to_constants_from_session_graph(
317      session=sess,
318      graph_def=input_graph_def,
319      output_node_names=output_node_names,
320      variable_names_allowlist=variable_names_whitelist,
321      variable_names_denylist=variable_names_blacklist)
322  # The previous code logic generated an empty versions field, we clear it here
323  # to maintain backwards compatibility.
324  ret.versions.Clear()
325  return ret
326
327
328@deprecation.deprecated(
329    date=None,
330    instructions="Use `tf.compat.v1.graph_util.remove_training_nodes`")
331@tf_export(v1=["graph_util.remove_training_nodes"])
332def remove_training_nodes(input_graph, protected_nodes=None):
333  """Prunes out nodes that aren't needed for inference.
334
335  There are nodes like Identity and CheckNumerics that are only useful
336  during training, and can be removed in graphs that will be used for
337  nothing but inference. Here we identify and remove them, returning an
338  equivalent graph. To be specific, CheckNumerics nodes are always removed, and
339  Identity nodes that aren't involved in control edges are spliced out so that
340  their input and outputs are directly connected.
341
342  Args:
343    input_graph: Model to analyze and prune.
344    protected_nodes: An optional list of names of nodes to be kept
345      unconditionally. This is for example useful to preserve Identity output
346      nodes.
347
348  Returns:
349    A list of nodes with the unnecessary ones removed.
350  """
351  if not protected_nodes:
352    protected_nodes = []
353
354  types_to_remove = {"CheckNumerics": True}
355
356  input_nodes = input_graph.node
357  names_to_remove = {}
358  for node in input_nodes:
359    if node.op in types_to_remove and node.name not in protected_nodes:
360      names_to_remove[node.name] = True
361
362  nodes_after_removal = []
363  for node in input_nodes:
364    if node.name in names_to_remove:
365      continue
366    new_node = node_def_pb2.NodeDef()
367    new_node.CopyFrom(node)
368    input_before_removal = node.input
369    del new_node.input[:]
370    for full_input_name in input_before_removal:
371      input_name = re.sub(r"^\^", "", full_input_name)
372      if input_name in names_to_remove:
373        continue
374      new_node.input.append(full_input_name)
375    nodes_after_removal.append(new_node)
376
377  types_to_splice = {"Identity": True}
378  control_input_names = set()
379  node_names_with_control_input = set()
380  for node in nodes_after_removal:
381    for node_input in node.input:
382      if "^" in node_input:
383        control_input_names.add(node_input.replace("^", ""))
384        node_names_with_control_input.add(node.name)
385
386  names_to_splice = {}
387  for node in nodes_after_removal:
388    if node.op in types_to_splice and node.name not in protected_nodes:
389      # We don't want to remove nodes that have control edge inputs, because
390      # they might be involved in subtle dependency issues that removing them
391      # will jeopardize.
392      if node.name not in node_names_with_control_input:
393        names_to_splice[node.name] = node.input[0]
394
395  # We also don't want to remove nodes which are used as control edge inputs.
396  names_to_splice = {name: value for name, value in names_to_splice.items()
397                     if name not in control_input_names}
398
399  nodes_after_splicing = []
400  for node in nodes_after_removal:
401    if node.name in names_to_splice:
402      continue
403    new_node = node_def_pb2.NodeDef()
404    new_node.CopyFrom(node)
405    input_before_removal = node.input
406    del new_node.input[:]
407    for full_input_name in input_before_removal:
408      input_name = re.sub(r"^\^", "", full_input_name)
409      while input_name in names_to_splice:
410        full_input_name = names_to_splice[input_name]
411        input_name = re.sub(r"^\^", "", full_input_name)
412      new_node.input.append(full_input_name)
413    nodes_after_splicing.append(new_node)
414
415  output_graph = graph_pb2.GraphDef()
416  output_graph.node.extend(nodes_after_splicing)
417  return output_graph
418
419
420@tf_export("__internal__.graph_util.graph_defs_equal", v1=[])
421def graph_defs_equal(graph_def_1: graph_pb2.GraphDef,
422                     graph_def_2: graph_pb2.GraphDef,
423                     treat_nan_as_equal: bool = False) -> bool:
424  """Returns True iff the graph def arguments are structurally equivalent.
425
426  The notion of equivalence encoded here checks that the set of NodeDefs in
427  the GraphDef's function library and main graph body are identical.
428  Additionally, it checks that the functions in the function library are equal
429  as sets.
430
431  Example usage:
432
433  ```
434  with tf.Graph().as_default() as g1:
435    tf.constant(1)
436
437  with tf.Graph().as_default() as g2:
438    tf.constant(2)
439
440  with tf.Graph().as_default() as g3:
441    tf.constant(1)
442
443  assert tf.__internal__.graph_util.graph_defs_equal(g1.as_graph_def(),
444                                                     g3.as_graph_def())
445
446  assert not tf.__internal__.graph_util.graph_defs_equal(g1.as_graph_def(),
447                                                         g2.as_graph_def())
448  ```
449
450  Args:
451    graph_def_1: Instance of `graph_pb2.GraphDef` to compare.
452    graph_def_2: Instance of `graph_pb2.GraphDef` to compare.
453    treat_nan_as_equal: Boolean indicating whether or not to treat nan
454      floating-point values as equal. This is crucial for any equivalence
455      relation defined over GraphDefs, to ensure symmetry.
456
457  Returns:
458    Boolean indicating structural equivalence as described above.
459
460  Raises:
461    TypeError: If either of the GraphDefs are not instances of
462      `graph_pb2.GraphDef`.
463  """
464  if not isinstance(graph_def_1, graph_pb2.GraphDef):
465    raise TypeError("graph_def_1 must be a graph_pb2.GraphDef proto.")
466  if not isinstance(graph_def_2, graph_pb2.GraphDef):
467    raise TypeError("graph_def_2 must be a graph_pb2.GraphDef proto.")
468  options = _proto_comparators.ProtoComparisonOptions(treat_nan_as_equal)
469  return _proto_comparators.EqualsGraphDef(graph_def_1.SerializeToString(),
470                                           graph_def_2.SerializeToString(),
471                                           options)
472