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 _v2_behavior_usage_gauge.get_cell("enable").set(True) 55 # TF2 behavior is enabled if either 1) enable_v2_behavior() is called or 56 # 2) the TF2_BEHAVIOR=1 environment variable is set. In the latter case, 57 # the modules below independently check if tf2.enabled(). 58 tf2.enable() 59 ops.enable_eager_execution() 60 tensor_shape.enable_v2_tensorshape() # Also switched by tf2 61 variable_scope.enable_resource_variables() 62 ops.enable_tensor_equality() 63 # Enables TensorArrayV2 and control flow V2. 64 control_flow_v2_toggles.enable_control_flow_v2() 65 # Make sure internal uses of tf.data symbols map to V2 versions. 66 dataset_ops.Dataset = dataset_ops.DatasetV2 67 readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV2 68 readers.TFRecordDataset = readers.TFRecordDatasetV2 69 readers.TextLineDataset = readers.TextLineDatasetV2 70 counter.Counter = counter.CounterV2 71 interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v2 72 interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v2 73 random_ops.RandomDataset = random_ops.RandomDatasetV2 74 exp_readers.CsvDataset = exp_readers.CsvDatasetV2 75 exp_readers.SqlDataset = exp_readers.SqlDatasetV2 76 exp_readers.make_batched_features_dataset = ( 77 exp_readers.make_batched_features_dataset_v2) 78 exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v2 79 80 81@tf_export(v1=["disable_v2_behavior"]) 82def disable_v2_behavior(): 83 """Disables TensorFlow 2.x behaviors. 84 85 This function can be called at the beginning of the program (before `Tensors`, 86 `Graphs` or other structures have been created, and before devices have been 87 initialized. It switches all global behaviors that are different between 88 TensorFlow 1.x and 2.x to behave as intended for 1.x. 89 90 User can call this function to disable 2.x behavior during complex migrations. 91 """ 92 _v2_behavior_usage_gauge.get_cell("disable").set(True) 93 tf2.disable() 94 ops.disable_eager_execution() 95 tensor_shape.disable_v2_tensorshape() # Also switched by tf2 96 variable_scope.disable_resource_variables() 97 ops.disable_tensor_equality() 98 # Disables TensorArrayV2 and control flow V2. 99 control_flow_v2_toggles.disable_control_flow_v2() 100 # Make sure internal uses of tf.data symbols map to V1 versions. 101 dataset_ops.Dataset = dataset_ops.DatasetV1 102 readers.FixedLengthRecordDataset = readers.FixedLengthRecordDatasetV1 103 readers.TFRecordDataset = readers.TFRecordDatasetV1 104 readers.TextLineDataset = readers.TextLineDatasetV1 105 counter.Counter = counter.CounterV1 106 interleave_ops.choose_from_datasets = interleave_ops.choose_from_datasets_v1 107 interleave_ops.sample_from_datasets = interleave_ops.sample_from_datasets_v1 108 random_ops.RandomDataset = random_ops.RandomDatasetV1 109 exp_readers.CsvDataset = exp_readers.CsvDatasetV1 110 exp_readers.SqlDataset = exp_readers.SqlDatasetV1 111 exp_readers.make_batched_features_dataset = ( 112 exp_readers.make_batched_features_dataset_v1) 113 exp_readers.make_csv_dataset = exp_readers.make_csv_dataset_v1 114