xref: /aosp_15_r20/external/tensorflow/tensorflow/python/grappler/tf_optimizer.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Copyright 2017 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"""Provides a proper python API for the symbols exported through swig."""
16
17import threading
18
19from tensorflow.core.framework import graph_pb2
20from tensorflow.core.protobuf import config_pb2
21from tensorflow.python.grappler import _pywrap_tf_optimizer as tf_opt
22from tensorflow.python.grappler import cluster as gcluster
23
24_OPTIMIZE_GRAPH_CLUSTER_LOCK = threading.Lock()
25
26
27def OptimizeGraph(config_proto,
28                  metagraph,
29                  verbose=True,
30                  graph_id=b'graph_to_optimize',
31                  cluster=None,
32                  strip_default_attributes=False):
33  """Optimize the provided metagraph.
34
35  For best results, the signature_def field in `metagraph` should be populated
36  with information about input (feed) and output (fetch) tensors.
37
38  Args:
39    config_proto: a ConfigProto protobuf.
40    metagraph: a MetagraphDef protobuf.
41    verbose: whether to log optimization results.
42    graph_id: a string identifying this graph.
43    cluster: a grappler cluster object representing hardware resources
44        available to run this graph.
45    strip_default_attributes: whether graph node attributes having default
46        values should be removed after all the optimization passes. This
47        option is useful if the resulting graph will be executed by an older
48        process that might not know some of the recently added attributes.
49  """
50  if not isinstance(config_proto, config_pb2.ConfigProto):
51    raise TypeError('Argument `config_proto` should be a tf.ConfigProto, '
52                    f'received type: {type(config_proto).__name__}')
53  if cluster is not None:
54    out_graph = tf_opt.TF_OptimizeGraph(cluster.tf_cluster,
55                                        config_proto.SerializeToString(),
56                                        metagraph.SerializeToString(), verbose,
57                                        graph_id, strip_default_attributes)
58  else:
59    # Currently Grappler assumes no more than 1 sessions alive globally.
60    # See comments on SingleMachine::Provision(), hence we use the following
61    # lock to prevent concurrent access to the following code.
62    with _OPTIMIZE_GRAPH_CLUSTER_LOCK:
63      cluster = gcluster.Cluster()
64      try:
65        out_graph = tf_opt.TF_OptimizeGraph(cluster.tf_cluster,
66                                            config_proto.SerializeToString(),
67                                            metagraph.SerializeToString(),
68                                            verbose, graph_id,
69                                            strip_default_attributes)
70      finally:
71        # Force the cleanup instead of waiting on python GC to cleanup the
72        # temporary cluster we've created. Otherwise subsequent calls might
73        # not have a clean slate because GC may not have run yet.
74        cluster.Shutdown()
75  return graph_pb2.GraphDef().FromString(out_graph)
76