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