chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
load("@xla//third_party/rules_python/python:py_library.bzl", "py_library")
load("//tensorflow:tensorflow.default.bzl", "tf_py_strict_test")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
licenses = ["notice"],
)
py_library(
name = "v2_compat",
srcs = ["v2_compat.py"],
strict_deps = True,
visibility = ["//tensorflow:internal"],
deps = [
"//tensorflow/python:tf2",
"//tensorflow/python/eager:monitoring",
"//tensorflow/python/framework:ops",
"//tensorflow/python/framework:registry",
"//tensorflow/python/framework:tensor",
"//tensorflow/python/framework:tensor_shape",
"//tensorflow/python/ops:control_flow_v2_toggles",
"//tensorflow/python/ops:resource_variables_toggle",
"//tensorflow/python/util:tf_export",
],
)
py_library(
name = "compat",
srcs = ["compat.py"],
strict_deps = True,
visibility = ["//tensorflow:internal"],
deps = [
"//tensorflow/python/platform:tf_logging",
"//tensorflow/python/util:tf_contextlib",
"//tensorflow/python/util:tf_export",
],
)
tf_py_strict_test(
name = "compat_test",
size = "small",
srcs = ["compat_test.py"],
tags = ["nofwdcompat"],
deps = [
":compat",
"//tensorflow/python/platform:client_testlib",
],
)
tf_py_strict_test(
name = "disable_v2_behavior_test",
size = "small",
srcs = ["disable_v2_behavior_test.py"],
deps = [
":v2_compat",
"//tensorflow/python/framework:constant_op",
"//tensorflow/python/framework:ops",
"//tensorflow/python/platform:_pywrap_tf2",
"//tensorflow/python/platform:client_testlib",
],
)
+168
View File
@@ -0,0 +1,168 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Utilities for API compatibility between TensorFlow release versions.
See [Version
Compatibility](https://tensorflow.org/guide/version_compat#backward_forward)
"""
import datetime
import os
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.util import tf_contextlib
from tensorflow.python.util.tf_export import tf_export
# This value changes every day with an automatic CL. It can be modified in code
# via `forward_compatibility_horizon()` or with the environment variable
# TF_FORWARD_COMPATIBILITY_DELTA_DAYS, which is added to the compatibility date.
_FORWARD_COMPATIBILITY_HORIZON = datetime.date(2026, 1, 16)
_FORWARD_COMPATIBILITY_DELTA_DAYS_VAR_NAME = "TF_FORWARD_COMPATIBILITY_DELTA_DAYS"
_FORWARD_COMPATIBILITY_DATE_NUMBER = None
def _date_to_date_number(year, month, day):
return (year << 9) | (month << 5) | day
def _update_forward_compatibility_date_number(date_to_override=None):
"""Update the base date to compare in forward_compatible function."""
global _FORWARD_COMPATIBILITY_DATE_NUMBER
if date_to_override:
date = date_to_override
else:
date = _FORWARD_COMPATIBILITY_HORIZON
delta_days = os.getenv(_FORWARD_COMPATIBILITY_DELTA_DAYS_VAR_NAME)
if delta_days:
date += datetime.timedelta(days=int(delta_days))
if date < _FORWARD_COMPATIBILITY_HORIZON:
logging.warning("Trying to set the forward compatibility date to the past"
" date %s. This will be ignored by TensorFlow." % (date))
return
_FORWARD_COMPATIBILITY_DATE_NUMBER = _date_to_date_number(
date.year, date.month, date.day)
_update_forward_compatibility_date_number()
@tf_export("compat.forward_compatible")
def forward_compatible(year, month, day):
"""Return true if the forward compatibility window has expired.
See [Version
compatibility](https://www.tensorflow.org/guide/versions#backward_and_partial_forward_compatibility).
Forward-compatibility refers to scenarios where the producer of a TensorFlow
model (a GraphDef or SavedModel) is compiled against a version of the
TensorFlow library newer than what the consumer was compiled against. The
"producer" is typically a Python program that constructs and trains a model
while the "consumer" is typically another program that loads and serves the
model.
TensorFlow has been supporting a 3 week forward-compatibility window for
programs compiled from source at HEAD.
For example, consider the case where a new operation `MyNewAwesomeAdd` is
created with the intent of replacing the implementation of an existing Python
wrapper - `tf.add`. The Python wrapper implementation should change from
something like:
```python
def add(inputs, name=None):
return gen_math_ops.add(inputs, name)
```
to:
```python
from tensorflow.python.compat import compat
def add(inputs, name=None):
if compat.forward_compatible(year, month, day):
# Can use the awesome new implementation.
return gen_math_ops.my_new_awesome_add(inputs, name)
# To maintain forward compatibility, use the old implementation.
return gen_math_ops.add(inputs, name)
```
Where `year`, `month`, and `day` specify the date beyond which binaries
that consume a model are expected to have been updated to include the
new operations. This date is typically at least 3 weeks beyond the date
the code that adds the new operation is committed.
Args:
year: A year (e.g., 2018). Must be an `int`.
month: A month (1 <= month <= 12) in year. Must be an `int`.
day: A day (1 <= day <= 31, or 30, or 29, or 28) in month. Must be an
`int`.
Returns:
True if the caller can expect that serialized TensorFlow graphs produced
can be consumed by programs that are compiled with the TensorFlow library
source code after (year, month, day).
"""
return _FORWARD_COMPATIBILITY_DATE_NUMBER > _date_to_date_number(
year, month, day)
@tf_export("compat.forward_compatibility_horizon")
@tf_contextlib.contextmanager
def forward_compatibility_horizon(year, month, day):
"""Context manager for testing forward compatibility of generated graphs.
See [Version
compatibility](https://www.tensorflow.org/guide/versions#backward_and_partial_forward_compatibility).
To ensure forward compatibility of generated graphs (see `forward_compatible`)
with older binaries, new features can be gated with:
```python
if compat.forward_compatible(year=2018, month=08, day=01):
generate_graph_with_new_features()
else:
generate_graph_so_older_binaries_can_consume_it()
```
However, when adding new features, one may want to unittest it before
the forward compatibility window expires. This context manager enables
such tests. For example:
```python
from tensorflow.python.compat import compat
def testMyNewFeature(self):
with compat.forward_compatibility_horizon(2018, 08, 02):
# Test that generate_graph_with_new_features() has an effect
```
Args:
year: A year (e.g., 2018). Must be an `int`.
month: A month (1 <= month <= 12) in year. Must be an `int`.
day: A day (1 <= day <= 31, or 30, or 29, or 28) in month. Must be an
`int`.
Yields:
Nothing.
"""
try:
_update_forward_compatibility_date_number(datetime.date(year, month, day))
yield
finally:
_update_forward_compatibility_date_number()
+122
View File
@@ -0,0 +1,122 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for forward and backwards compatibility utilities."""
import datetime
import os
from tensorflow.python.compat import compat
from tensorflow.python.platform import test
class CompatTest(test.TestCase):
def _compatibility_date(self):
date = compat._FORWARD_COMPATIBILITY_HORIZON # pylint: disable=protected-access
return (date.year, date.month, date.day)
def _n_days_after(self, n):
date = compat._FORWARD_COMPATIBILITY_HORIZON + datetime.timedelta(days=n) # pylint: disable=protected-access
return (date.year, date.month, date.day)
def test_basic(self):
compatibility_date = self._compatibility_date()
one_day_before = self._n_days_after(-1)
self.assertTrue(compat.forward_compatible(*one_day_before))
self.assertFalse(compat.forward_compatible(*compatibility_date))
def test_past(self):
with compat.forward_compatibility_horizon(2018, 9, 18):
self.assertTrue(compat.forward_compatible(2020, 4, 4))
def test_decorator(self):
compatibility_date = self._compatibility_date()
one_day_after = self._n_days_after(1)
with compat.forward_compatibility_horizon(*one_day_after):
self.assertTrue(compat.forward_compatible(*compatibility_date))
self.assertFalse(compat.forward_compatible(*one_day_after))
# After exiting context manager, value should be reset.
self.assertFalse(compat.forward_compatible(*compatibility_date))
def test_decorator_with_failure(self):
compatibility_date = self._compatibility_date()
one_day_after = self._n_days_after(1)
class DummyError(Exception):
pass
try:
with compat.forward_compatibility_horizon(*one_day_after):
raise DummyError()
except DummyError:
pass # silence DummyError
# After exiting context manager, value should be reset.
self.assertFalse(compat.forward_compatible(*compatibility_date))
def test_environment_override(self):
var_name = 'TF_FORWARD_COMPATIBILITY_DELTA_DAYS'
def remove_os_environment_var():
try:
del os.environ[var_name]
except KeyError:
pass
self.addCleanup(remove_os_environment_var)
compatibility_date = self._compatibility_date()
one_day_before = self._n_days_after(-1)
one_day_after = self._n_days_after(1)
ten_days_after = self._n_days_after(10)
nine_days_after = self._n_days_after(9)
self.assertTrue(compat.forward_compatible(*one_day_before))
self.assertFalse(compat.forward_compatible(*compatibility_date))
self.assertFalse(compat.forward_compatible(*one_day_after))
self.assertFalse(compat.forward_compatible(*nine_days_after))
self.assertFalse(compat.forward_compatible(*ten_days_after))
os.environ[var_name] = '10'
compat._update_forward_compatibility_date_number()
self.assertTrue(compat.forward_compatible(*one_day_before))
self.assertTrue(compat.forward_compatible(*compatibility_date))
self.assertTrue(compat.forward_compatible(*one_day_after))
self.assertTrue(compat.forward_compatible(*nine_days_after))
self.assertFalse(compat.forward_compatible(*ten_days_after))
del os.environ[var_name]
compat._update_forward_compatibility_date_number()
self.assertTrue(compat.forward_compatible(*one_day_before))
self.assertFalse(compat.forward_compatible(*compatibility_date))
self.assertFalse(compat.forward_compatible(*one_day_after))
self.assertFalse(compat.forward_compatible(*nine_days_after))
self.assertFalse(compat.forward_compatible(*ten_days_after))
# Now test interaction between environment variable and context func.
os.environ[var_name] = '10'
compat._update_forward_compatibility_date_number()
self.assertTrue(compat.forward_compatible(*one_day_after))
with compat.forward_compatibility_horizon(*one_day_after):
self.assertTrue(compat.forward_compatible(*one_day_before))
self.assertTrue(compat.forward_compatible(*compatibility_date))
self.assertFalse(compat.forward_compatible(*one_day_after))
self.assertFalse(compat.forward_compatible(*nine_days_after))
self.assertFalse(compat.forward_compatible(*ten_days_after))
self.assertTrue(compat.forward_compatible(*one_day_after))
if __name__ == '__main__':
test.main()
@@ -0,0 +1,40 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for forward and backwards compatibility utilities."""
from tensorflow.python.compat import v2_compat
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import ops
from tensorflow.python.platform import _pywrap_tf2
from tensorflow.python.platform import test
class DisableV2BehaviorTest(test.TestCase):
def test_basic(self):
t = constant_op.constant([1, 2, 3]) # creates a hidden context
self.assertTrue(isinstance(t, ops.EagerTensor))
t = _pywrap_tf2.is_enabled()
self.assertTrue(t)
v2_compat.disable_v2_behavior()
t = constant_op.constant([1, 2, 3])
self.assertFalse(isinstance(t, ops.EagerTensor))
t = _pywrap_tf2.is_enabled()
self.assertFalse(t)
if __name__ == '__main__':
v2_compat.enable_v2_behavior()
test.main()
+105
View File
@@ -0,0 +1,105 @@
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Switching v2 features on and off."""
from tensorflow.python import tf2
from tensorflow.python.eager import monitoring
from tensorflow.python.framework import ops
from tensorflow.python.framework import registry
from tensorflow.python.framework import tensor
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import control_flow_v2_toggles
from tensorflow.python.ops import resource_variables_toggle
from tensorflow.python.util.tf_export import tf_export
# Metrics to track the status of v2_behavior
_v2_behavior_usage_gauge = monitoring.BoolGauge(
"/tensorflow/version/v2_behavior",
"whether v2_behavior is enabled or disabled", "status")
_DATA_V2_CALLBACKS = registry.Registry("data_v2_callbacks")
def register_data_v2_callback(data_v2_func):
_DATA_V2_CALLBACKS.register(data_v2_func, data_v2_func.__module__)
@tf_export(v1=["enable_v2_behavior"])
def enable_v2_behavior():
"""Enables TensorFlow 2.x behaviors.
This function can be called at the beginning of the program (before `Tensors`,
`Graphs` or other structures have been created, and before devices have been
initialized. It switches all global behaviors that are different between
TensorFlow 1.x and 2.x to behave as intended for 2.x.
This function is called in the main TensorFlow `__init__.py` file, user should
not need to call it, except during complex migrations.
@compatibility(TF2)
This function is not necessary if you are using TF2. V2 behavior is enabled by
default.
@end_compatibility
"""
_v2_behavior_usage_gauge.get_cell("enable").set(True)
# TF2 behavior is enabled if either 1) enable_v2_behavior() is called or
# 2) the TF2_BEHAVIOR=1 environment variable is set. In the latter case,
# the modules below independently check if tf2.enabled().
tf2.enable()
ops.enable_eager_execution()
tensor_shape.enable_v2_tensorshape() # Also switched by tf2
resource_variables_toggle.enable_resource_variables()
tensor.enable_tensor_equality()
# Enables TensorArrayV2 and control flow V2.
control_flow_v2_toggles.enable_control_flow_v2()
# Make sure internal uses of tf.data symbols map to V2 versions.
for v2_enabler_name in _DATA_V2_CALLBACKS.list():
v2_enabler = _DATA_V2_CALLBACKS.lookup(v2_enabler_name)
v2_enabler()
@tf_export(v1=["disable_v2_behavior"])
def disable_v2_behavior():
"""Disables TensorFlow 2.x behaviors.
This function can be called at the beginning of the program (before `Tensors`,
`Graphs` or other structures have been created, and before devices have been
initialized. It switches all global behaviors that are different between
TensorFlow 1.x and 2.x to behave as intended for 1.x.
User can call this function to disable 2.x behavior during complex migrations.
@compatibility(TF2)
Using this function indicates that your software is not compatible
with eager execution and `tf.function` in TF2.
To migrate to TF2, rewrite your code to be compatible with eager execution.
Please refer to the [migration guide]
(https://www.tensorflow.org/guide/migrate) for additional resource on the
topic.
@end_compatibility
"""
_v2_behavior_usage_gauge.get_cell("disable").set(True)
tf2.disable()
ops.disable_eager_execution()
tensor_shape.disable_v2_tensorshape() # Also switched by tf2
resource_variables_toggle.disable_resource_variables()
tensor.disable_tensor_equality()
# Disables TensorArrayV2 and control flow V2.
control_flow_v2_toggles.disable_control_flow_v2()
# Make sure internal uses of tf.data symbols map to V1 versions.
for v2_disabler_name in _DATA_V2_CALLBACKS.list():
v2_disabler = _DATA_V2_CALLBACKS.lookup(v2_disabler_name)
v2_disabler()