mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
[AMP] Add deprecated decorator for torch.xxx.amp.autocast class (#163654)
As the title stated. **Changes:** - torch.cuda.amp.autocast - torch.cpu.amp.autocast - add explicit `__new__` and `__init_subclass__` for those class above for inspect.signature to retrieve correct signature Pull Request resolved: https://github.com/pytorch/pytorch/pull/163654 Approved by: https://github.com/Skylion007
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
# mypy: allow-untyped-defs
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing_extensions import deprecated
|
||||
|
||||
@ -8,17 +9,36 @@ import torch
|
||||
__all__ = ["autocast"]
|
||||
|
||||
|
||||
@deprecated(
|
||||
"`torch.cpu.amp.autocast(args...)` is deprecated. "
|
||||
"Please use `torch.amp.autocast('cpu', args...)` instead.",
|
||||
category=FutureWarning,
|
||||
)
|
||||
class autocast(torch.amp.autocast_mode.autocast):
|
||||
r"""
|
||||
See :class:`torch.autocast`.
|
||||
``torch.cpu.amp.autocast(args...)`` is deprecated. Please use ``torch.amp.autocast("cpu", args...)`` instead.
|
||||
"""
|
||||
|
||||
@deprecated(
|
||||
"`torch.cpu.amp.autocast(args...)` is deprecated. "
|
||||
"Please use `torch.amp.autocast('cpu', args...)` instead.",
|
||||
category=FutureWarning,
|
||||
)
|
||||
# TODO: remove this conditional once we stop supporting Python < 3.13
|
||||
# Prior to Python 3.13, inspect.signature could not retrieve the correct
|
||||
# signature information for classes decorated with @deprecated (unless
|
||||
# the __new__ static method was explicitly defined);
|
||||
#
|
||||
# However, this issue has been fixed in Python 3.13 and later versions.
|
||||
if sys.version_info < (3, 13):
|
||||
|
||||
def __new__(
|
||||
cls,
|
||||
enabled: bool = True,
|
||||
dtype: torch.dtype = torch.bfloat16,
|
||||
cache_enabled: bool = True,
|
||||
):
|
||||
return super().__new__(cls)
|
||||
|
||||
def __init_subclass__(cls):
|
||||
pass
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
enabled: bool = True,
|
||||
|
@ -1,5 +1,6 @@
|
||||
# mypy: allow-untyped-defs
|
||||
import functools
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing_extensions import deprecated
|
||||
|
||||
@ -9,17 +10,36 @@ import torch
|
||||
__all__ = ["autocast", "custom_fwd", "custom_bwd"]
|
||||
|
||||
|
||||
@deprecated(
|
||||
"`torch.cuda.amp.autocast(args...)` is deprecated. "
|
||||
"Please use `torch.amp.autocast('cuda', args...)` instead.",
|
||||
category=FutureWarning,
|
||||
)
|
||||
class autocast(torch.amp.autocast_mode.autocast):
|
||||
r"""See :class:`torch.autocast`.
|
||||
|
||||
``torch.cuda.amp.autocast(args...)`` is deprecated. Please use ``torch.amp.autocast("cuda", args...)`` instead.
|
||||
"""
|
||||
|
||||
@deprecated(
|
||||
"`torch.cuda.amp.autocast(args...)` is deprecated. "
|
||||
"Please use `torch.amp.autocast('cuda', args...)` instead.",
|
||||
category=FutureWarning,
|
||||
)
|
||||
# TODO: remove this conditional once we stop supporting Python < 3.13
|
||||
# Prior to Python 3.13, inspect.signature could not retrieve the correct
|
||||
# signature information for classes decorated with @deprecated (unless
|
||||
# the __new__ static method was explicitly defined);
|
||||
#
|
||||
# However, this issue has been fixed in Python 3.13 and later versions.
|
||||
if sys.version_info < (3, 13):
|
||||
|
||||
def __new__(
|
||||
cls,
|
||||
enabled: bool = True,
|
||||
dtype: torch.dtype = torch.float16,
|
||||
cache_enabled: bool = True,
|
||||
):
|
||||
return super().__new__(cls)
|
||||
|
||||
def __init_subclass__(cls):
|
||||
pass
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
enabled: bool = True,
|
||||
|
Reference in New Issue
Block a user