• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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"""API for enabling v2 control flow."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
22from tensorflow.python.framework import ops
23from tensorflow.python.ops import control_flow_util
24from tensorflow.python.ops import control_flow_util_v2
25from tensorflow.python.platform import tf_logging as logging
26from tensorflow.python.util.tf_export import tf_export
27
28
29@tf_export(v1=["enable_control_flow_v2"])
30def enable_control_flow_v2():  # pylint: disable=invalid-name
31  """Use control flow v2.
32
33  control flow v2 (cfv2) is an improved version of control flow in TensorFlow
34  with support for higher order derivatives. Enabling cfv2 will change the
35  graph/function representation of control flow, e.g., `tf.while_loop` and
36  `tf.cond` will generate functional `While` and `If` ops instead of low-level
37  `Switch`, `Merge` etc. ops. Note: Importing and running graphs exported
38  with old control flow will still be supported.
39
40  Calling tf.enable_control_flow_v2() lets you opt-in to this TensorFlow 2.0
41  feature.
42
43  Note: v2 control flow is always enabled inside of tf.function. Calling this
44  function is not required.
45  """
46  # pylint: disable=protected-access
47  logging.vlog(1, "Enabling control flow v2")
48  ops._control_flow_api_gauge.get_cell().set(True)
49  control_flow_util.ENABLE_CONTROL_FLOW_V2 = True
50
51
52@tf_export(v1=["disable_control_flow_v2"])
53def disable_control_flow_v2():  # pylint: disable=invalid-name
54  """Opts out of control flow v2.
55
56  Note: v2 control flow is always enabled inside of tf.function. Calling this
57  function has no effect in that case.
58
59  If your code needs tf.disable_control_flow_v2() to be called to work
60  properly please file a bug.
61  """
62  # pylint: disable=protected-access
63  logging.vlog(1, "Disabling control flow v2")
64  ops._control_flow_api_gauge.get_cell().set(False)
65  control_flow_util.ENABLE_CONTROL_FLOW_V2 = False
66
67
68@tf_export(v1=["control_flow_v2_enabled"])
69def control_flow_v2_enabled():  # pylint: disable=invalid-name
70  """Returns `True` if v2 control flow is enabled.
71
72  Note: v2 control flow is always enabled inside of tf.function.
73  """
74  return control_flow_util.EnableControlFlowV2(ops.get_default_graph())
75
76
77@tf_export(v1=["experimental.output_all_intermediates"])
78def output_all_intermediates(state):  # pylint: disable=invalid-name
79  """Whether to output all intermediates from functional control flow ops.
80
81  The "default" behavior to is to output all intermediates when using v2 control
82  flow inside Keras models in graph mode (possibly inside Estimators). This is
83  needed to support taking gradients of v2 control flow. In graph mode, Keras
84  can sometimes freeze the forward graph before the gradient computation which
85  does not work for v2 control flow since it requires updating the forward ops
86  to output the needed intermediates. We work around this by proactively
87  outputting the needed intermediates when building the forward pass itself.
88  Ideally any such extra tensors should be pruned out at runtime. However, if
89  for any reason this doesn't work for you or if you have an inference-only
90  model you can turn this behavior off using
91  `tf.compat.v1.experimental.output_all_intermediates(False)`.
92
93  If with the default behavior you are still seeing errors of the form
94  "Connecting to invalid output X of source node Y which has Z outputs" try
95  setting `tf.compat.v1.experimental.output_all_intermediates(True)` and
96  please file an issue at https://github.com/tensorflow/tensorflow/issues.
97
98  Args:
99    state: True, False or None. None restores the default behavior.
100  """
101  control_flow_util_v2._EXPERIMENTAL_OUTPUT_ALL_INTERMEDIATES_OVERRIDE = state  # pylint: disable=protected-access
102