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