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