Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[r1.15-CherryPick]:forward_compatible env variable caching perf optimization. #32632

Merged
merged 1 commit into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions tensorflow/python/compat/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,32 @@
# 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(2019, 8, 21)

_FORWARD_COMPATIBILITY_HORIZON_OVERRIDDEN = False
_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."""

def _get_forward_compatibility_date():
date = _FORWARD_COMPATIBILITY_HORIZON
delta_days = os.getenv(_FORWARD_COMPATIBILITY_DELTA_DAYS_VAR_NAME)
if delta_days is not None and not _FORWARD_COMPATIBILITY_HORIZON_OVERRIDDEN:
return date + datetime.timedelta(days=int(delta_days))
global _FORWARD_COMPATIBILITY_DATE_NUMBER

if date_to_override:
date = date_to_override
else:
return date
date = _FORWARD_COMPATIBILITY_HORIZON
delta_days = os.getenv(_FORWARD_COMPATIBILITY_DELTA_DAYS_VAR_NAME)
if delta_days:
date += datetime.timedelta(days=int(delta_days))

_FORWARD_COMPATIBILITY_DATE_NUMBER = _date_to_date_number(
date.year, date.month, date.day)


_update_forward_compatibility_date_number()


@tf_export("compat.forward_compatible")
Expand Down Expand Up @@ -102,7 +116,8 @@ def add(inputs, name=None):
can be consumed by programs that are compiled with the TensorFlow library
source code after (year, month, day).
"""
return _get_forward_compatibility_date() > datetime.date(year, month, day)
return _FORWARD_COMPATIBILITY_DATE_NUMBER > _date_to_date_number(
year, month, day)


@tf_export("compat.forward_compatibility_horizon")
Expand Down Expand Up @@ -144,13 +159,8 @@ def testMyNewFeature(self):
Yields:
Nothing.
"""
global _FORWARD_COMPATIBILITY_HORIZON
global _FORWARD_COMPATIBILITY_HORIZON_OVERRIDDEN
try:
old_compat_date = _FORWARD_COMPATIBILITY_HORIZON
_FORWARD_COMPATIBILITY_HORIZON_OVERRIDDEN = True
_FORWARD_COMPATIBILITY_HORIZON = datetime.date(year, month, day)
_update_forward_compatibility_date_number(datetime.date(year, month, day))
yield
finally:
_FORWARD_COMPATIBILITY_HORIZON_OVERRIDDEN = False
_FORWARD_COMPATIBILITY_HORIZON = old_compat_date
_update_forward_compatibility_date_number()
3 changes: 3 additions & 0 deletions tensorflow/python/compat/compat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ def remove_os_environment_var():
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))
Expand All @@ -105,6 +107,7 @@ def remove_os_environment_var():

# 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))
Expand Down