1# Copyright 2018 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"""Switching v2 features on and off.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python import tf2 22from tensorflow.python.data.experimental.ops import counter 23from tensorflow.python.data.experimental.ops import interleave_ops 24from tensorflow.python.data.experimental.ops import random_ops 25from tensorflow.python.data.experimental.ops import readers as exp_readers 26from tensorflow.python.data.ops import dataset_ops 27from tensorflow.python.data.ops import readers 28from tensorflow.python.eager import monitoring 29from tensorflow.python.framework import ops 30from tensorflow.python.framework import tensor_shape 31from tensorflow.python.ops import control_flow_v2_toggles 32from tensorflow.python.ops import variable_scope 33 34from tensorflow.python.util.tf_export import tf_export 35 36# Metrics to track the status of v2_behavior 37_v2_behavior_usage_gauge = monitoring.BoolGauge( 38 "/tensorflow/version/v2_behavior", 39 "whether v2_behavior is enabled or disabled", "status") 40 41 42@tf_export(v1=["enable_v2_behavior"]) 43def enable_v2_behavior(): 44 """Enables TensorFlow 2.x behaviors. 45 46 This function can be called at the beginning of the program (before `Tensors`, 47 `Graphs` or other structures have been created, and before devices have been 48 initialized. It switches all global behaviors that are different between 49 TensorFlow 1.x and 2.x to behave as intended for 2.x. 50 51 This function is called in the main TensorFlow `__init__.py` file, user should 52 not need to call it, except during complex migrations. 53 54 @compatibility(TF2) 55 This function is not necessary if you are using TF2. V2 behavior is enabled by 56 default. 57 @end_compatibility 58 """ 59 _v2_behavior_usage_gauge.get_cell("enable").set(True) 60 # TF2 behavior is enabled if either 1) enable_v2_behavior() is called or 61 # 2) the TF2_BEHAVIOR=1 environment variable is set. In the latter case, 62 # the modules below independently check if tf2.enabled(). 63 tf2.enable() 64 ops.enable_eager_execution() 65 tensor_shape.enable_v2_tensorshape() # Also switched by tf2 66 variable_scope.enable_resource_variables() 67 ops.enable_tensor_equality() 68 # Enables TensorArrayV2 and control flow V2. 69 control_flow_v2_toggles.enable_control_flow_v2() 70 # Make sure internal uses of tf.data symbols map to V2 versions. 71 dataset_ops.Dataset = dataset_ops.DatasetV2 72 readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV2 73 readers.TFRecordDataset = readers.TFRecordDatasetV2 74 readers.TextLineDataset = readers.TextLineDatasetV2 75 counter.Counter = counter.CounterV2 76 interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v2 77 interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v2 78 random_ops.RandomDataset = random_ops.RandomDatasetV2 79 exp_readers.CsvDataset = exp_readers.CsvDatasetV2 80 exp_readers.SqlDataset = exp_readers.SqlDatasetV2 81 exp_readers.make_batched_features_dataset = ( 82 exp_readers.make_batched_features_dataset_v2) 83 exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v2 84 85 86@tf_export(v1=["disable_v2_behavior"]) 87def disable_v2_behavior(): 88 """Disables TensorFlow 2.x behaviors. 89 90 This function can be called at the beginning of the program (before `Tensors`, 91 `Graphs` or other structures have been created, and before devices have been 92 initialized. It switches all global behaviors that are different between 93 TensorFlow 1.x and 2.x to behave as intended for 1.x. 94 95 User can call this function to disable 2.x behavior during complex migrations. 96 97 @compatibility(TF2) 98 Using this function indicates that your software is not compatible 99 with eager execution and `tf.function` in TF2. 100 101 To migrate to TF2, rewrite your code to be compatible with eager execution. 102 Please refer to the [migration guide] 103 (https://www.tensorflow.org/guide/migrate) for additional resource on the 104 topic. 105 @end_compatibility 106 """ 107 _v2_behavior_usage_gauge.get_cell("disable").set(True) 108 tf2.disable() 109 ops.disable_eager_execution() 110 tensor_shape.disable_v2_tensorshape() # Also switched by tf2 111 variable_scope.disable_resource_variables() 112 ops.disable_tensor_equality() 113 # Disables TensorArrayV2 and control flow V2. 114 control_flow_v2_toggles.disable_control_flow_v2() 115 # Make sure internal uses of tf.data symbols map to V1 versions. 116 dataset_ops.Dataset = dataset_ops.DatasetV1 117 readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1 118 readers.TFRecordDataset = readers.TFRecordDatasetV1 119 readers.TextLineDataset = readers.TextLineDatasetV1 120 counter.Counter = counter.CounterV1 121 interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v1 122 interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v1 123 random_ops.RandomDataset = random_ops.RandomDatasetV1 124 exp_readers.CsvDataset = exp_readers.CsvDatasetV1 125 exp_readers.SqlDataset = exp_readers.SqlDatasetV1 126 exp_readers.make_batched_features_dataset = ( 127 exp_readers.make_batched_features_dataset_v1) 128 exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1 129