# mypy: allow-untyped-defs import sys from typing import Any from typing_extensions import deprecated 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. """ # 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, dtype: torch.dtype = torch.bfloat16, cache_enabled: bool = True, ): if torch._jit_internal.is_scripting(): self._enabled = enabled self.device = "cpu" self.fast_dtype = dtype return super().__init__( "cpu", enabled=enabled, dtype=dtype, cache_enabled=cache_enabled ) def __enter__(self): if torch._jit_internal.is_scripting(): return self return super().__enter__() # TODO: discuss a unified TorchScript-friendly API for autocast def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any): # type: ignore[override] if torch._jit_internal.is_scripting(): return return super().__exit__(exc_type, exc_val, exc_tb) def __call__(self, func): if torch._jit_internal.is_scripting(): return func return super().__call__(func)