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

add depreacated decorator #7703

Merged
merged 6 commits into from
Jul 18, 2024
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
1 change: 1 addition & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ function run_xla_op_tests1 {
run_test "$CDIR/test_python_ops.py"
run_test "$CDIR/test_ops.py"
run_test "$CDIR/test_metrics.py"
run_test "$CDIR/test_deprecation.py"
run_test "$CDIR/dynamo/test_dynamo_integrations_util.py"
run_test "$CDIR/dynamo/test_dynamo_aliasing.py"
run_test "$CDIR/dynamo/test_dynamo.py"
Expand Down
48 changes: 48 additions & 0 deletions test/test_deprecation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
import logging
import io
import unittest
import importlib

from torch_xla.experimental.deprecation import deprecated, mark_deprecated


def old_function():
return False


def new_function():
return True


class TestDepecation(unittest.TestCase):

def test_map_to_new_func(self):
this_module = importlib.import_module(__name__)
old_function = deprecated(this_module, this_module.new_function,
"random_old_name")
with self.assertLogs(level='WARNING') as log:
result = old_function()
self.assertIn("random_old_name", log.output[0])
assert (result)

@unittest.mock.patch('__main__.new_function')
def test_decorator(self, mock_new_function):
mock_new_function.__name__ = "new_name"
mock_new_function.return_value = True
with self.assertLogs(level='WARNING') as log:

@mark_deprecated(new_function)
def function_to_deprecate():
return False

result = function_to_deprecate()
assert (result)
self.assertIn("function_to_deprecate", log.output[0])
self.assertIn(mock_new_function.__name__, log.output[0])
mock_new_function.assert_called_once()


if __name__ == '__main__':
test = unittest.main()
sys.exit(0 if test.result.wasSuccessful() else 1)
24 changes: 22 additions & 2 deletions torch_xla/experimental/deprecation.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import functools
import logging
import importlib
from typing import TypeVar

FN = TypeVar('FN')


def deprecated(module, new: FN) -> FN:
def deprecated(module, new: FN, old_name=None) -> FN:
already_warned = [False]
old_name = old_name or new.__name__

@functools.wraps(new)
def wrapped(*args, **kwargs):
if not already_warned[0]:
logging.warning(
f'{module.__name__}.{new.__name__} is deprecated. Use {new.__module__}.{new.__name__} instead.'
f'{module.__name__}.{old_name} is deprecated. Use {new.__module__}.{new.__name__} instead.'
)
already_warned[0] = True

Expand All @@ -21,5 +23,23 @@ def wrapped(*args, **kwargs):
return wrapped


def mark_deprecated(new: FN) -> FN:
"""Decorator to mark a function as deprecated and map to new function.

Args:
module: current module of the deprecated function that is in. Assume current module name is X, you can use `from . import X` and pass X here.
new: new function that we map to. Need to include the path the new function that is in.

Returns:
Wrapper of the new function.
"""

def decorator(func):
return deprecated(
importlib.import_module(func.__module__), new, old_name=func.__name__)

return decorator


def register_deprecated(module, new: FN):
setattr(module, new.__name__, deprecated(module, new))
Loading