autocast_mode.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # mypy: allow-untyped-defs
  2. import sys
  3. from typing import Any
  4. from typing_extensions import deprecated
  5. import torch
  6. __all__ = ["autocast"]
  7. @deprecated(
  8. "`torch.cpu.amp.autocast(args...)` is deprecated. "
  9. "Please use `torch.amp.autocast('cpu', args...)` instead.",
  10. category=FutureWarning,
  11. )
  12. class autocast(torch.amp.autocast_mode.autocast):
  13. r"""
  14. See :class:`torch.autocast`.
  15. ``torch.cpu.amp.autocast(args...)`` is deprecated. Please use ``torch.amp.autocast("cpu", args...)`` instead.
  16. """
  17. # TODO: remove this conditional once we stop supporting Python < 3.13
  18. # Prior to Python 3.13, inspect.signature could not retrieve the correct
  19. # signature information for classes decorated with @deprecated (unless
  20. # the __new__ static method was explicitly defined);
  21. #
  22. # However, this issue has been fixed in Python 3.13 and later versions.
  23. if sys.version_info < (3, 13):
  24. def __new__(
  25. cls,
  26. enabled: bool = True,
  27. dtype: torch.dtype = torch.bfloat16,
  28. cache_enabled: bool = True,
  29. ):
  30. return super().__new__(cls)
  31. def __init_subclass__(cls):
  32. pass
  33. def __init__(
  34. self,
  35. enabled: bool = True,
  36. dtype: torch.dtype = torch.bfloat16,
  37. cache_enabled: bool = True,
  38. ):
  39. if torch._jit_internal.is_scripting():
  40. self._enabled = enabled
  41. self.device = "cpu"
  42. self.fast_dtype = dtype
  43. return
  44. super().__init__(
  45. "cpu", enabled=enabled, dtype=dtype, cache_enabled=cache_enabled
  46. )
  47. def __enter__(self):
  48. if torch._jit_internal.is_scripting():
  49. return self
  50. return super().__enter__()
  51. # TODO: discuss a unified TorchScript-friendly API for autocast
  52. def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any): # type: ignore[override]
  53. if torch._jit_internal.is_scripting():
  54. return
  55. return super().__exit__(exc_type, exc_val, exc_tb)
  56. def __call__(self, func):
  57. if torch._jit_internal.is_scripting():
  58. return func
  59. return super().__call__(func)