pytorch
b00a4b7c - [torch.fx.wrap] Use callable / function.__name__ instead of function.__code__.co_name (#84373)

Commit
2 years ago
[torch.fx.wrap] Use callable / function.__name__ instead of function.__code__.co_name (#84373) Ran across this issue while using torch.fx.wrap on a decorated function: it triggered a KeyError: 'wrapper_inside_decorator'. torch.fx.wrap stores function.__code__.co_name, but that isn't set correctly (and doesn't match it's name in the global namespace) for decorators; function.__name__ is set correctly. Also adjusted to checking for callable instead of checking for the existing of __code__ to allow for a broader variety of functions that can be passed in. Eg. using functools.cache returns a callable that won't have a __code__ attribute. I added a unit test (that incidentally fails every test in the suite before the fix commit -- because it affects the global state), and then a fix that addresses it. ``` In [1]: import functools In [2]: def decorator(f): ...: @functools.wraps(f) ...: def wrapper(*args, **kwargs): ...: return f(*args, **kwargs) ...: return wrapper ...: In [3]: @decorator ...: def some_function(x): ...: return x ...: In [4]: some_function.__name__ Out[4]: 'some_function' In [5]: some_function.__code__.co_name Out[5]: 'wrapper' ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/84373 Approved by: https://github.com/jamesr66a, https://github.com/SherlockNoMad
Author
Committer
Parents
Loading