autocast_mode.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # mypy: allow-untyped-defs
  2. import functools
  3. import sys
  4. from typing import Any
  5. from typing_extensions import deprecated
  6. import torch
  7. __all__ = ["autocast", "custom_fwd", "custom_bwd"]
  8. @deprecated(
  9. "`torch.cuda.amp.autocast(args...)` is deprecated. "
  10. "Please use `torch.amp.autocast('cuda', args...)` instead.",
  11. category=FutureWarning,
  12. )
  13. class autocast(torch.amp.autocast_mode.autocast):
  14. r"""See :class:`torch.autocast`.
  15. ``torch.cuda.amp.autocast(args...)`` is deprecated. Please use ``torch.amp.autocast("cuda", 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.float16,
  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.float16,
  37. cache_enabled: bool = True,
  38. ):
  39. if torch._jit_internal.is_scripting():
  40. self._enabled = enabled
  41. self.device = "cuda"
  42. self.fast_dtype = dtype
  43. return
  44. super().__init__(
  45. "cuda", 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)
  60. # Preserved only for BC reasons
  61. @deprecated(
  62. "`torch.cuda.amp.autocast_mode._cast(value, dtype)` is deprecated. "
  63. "Please use `torch.amp.autocast_mode._cast(value, 'cuda', dtype)` instead.",
  64. category=FutureWarning,
  65. )
  66. def _cast(value, dtype):
  67. return torch.amp.autocast_mode._cast(value, "cuda", dtype)
  68. @deprecated(
  69. "`torch.cuda.amp.custom_fwd(args...)` is deprecated. "
  70. "Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.",
  71. category=FutureWarning,
  72. )
  73. def custom_fwd(fwd=None, *, cast_inputs=None):
  74. """
  75. ``torch.cuda.amp.custom_fwd(args...)`` is deprecated. Please use
  76. ``torch.amp.custom_fwd(args..., device_type='cuda')`` instead.
  77. """
  78. return functools.partial(torch.amp.custom_fwd, device_type="cuda")(
  79. fwd=fwd, cast_inputs=cast_inputs
  80. )
  81. @deprecated(
  82. "`torch.cuda.amp.custom_bwd(args...)` is deprecated. "
  83. "Please use `torch.amp.custom_bwd(args..., device_type='cuda')` instead.",
  84. category=FutureWarning,
  85. )
  86. def custom_bwd(bwd):
  87. """
  88. ``torch.cuda.amp.custom_bwd(args...)`` is deprecated. Please use
  89. ``torch.amp.custom_bwd(args..., device_type='cuda')`` instead.
  90. """
  91. return functools.partial(torch.amp.custom_bwd, device_type="cuda")(bwd)