autocast_mode.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # mypy: allow-untyped-defs
  2. import functools
  3. from typing import Any
  4. from typing_extensions import deprecated
  5. import torch
  6. __all__ = ["autocast", "custom_fwd", "custom_bwd"]
  7. class autocast(torch.amp.autocast_mode.autocast):
  8. r"""See :class:`torch.autocast`.
  9. ``torch.cuda.amp.autocast(args...)`` is deprecated. Please use ``torch.amp.autocast("cuda", args...)`` instead.
  10. """
  11. @deprecated(
  12. "`torch.cuda.amp.autocast(args...)` is deprecated. "
  13. "Please use `torch.amp.autocast('cuda', args...)` instead.",
  14. category=FutureWarning,
  15. )
  16. def __init__(
  17. self,
  18. enabled: bool = True,
  19. dtype: torch.dtype = torch.float16,
  20. cache_enabled: bool = True,
  21. ):
  22. if torch._jit_internal.is_scripting():
  23. self._enabled = enabled
  24. self.device = "cuda"
  25. self.fast_dtype = dtype
  26. return
  27. super().__init__(
  28. "cuda", enabled=enabled, dtype=dtype, cache_enabled=cache_enabled
  29. )
  30. def __enter__(self):
  31. if torch._jit_internal.is_scripting():
  32. return self
  33. return super().__enter__()
  34. # TODO: discuss a unified TorchScript-friendly API for autocast
  35. def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any): # type: ignore[override]
  36. if torch._jit_internal.is_scripting():
  37. return
  38. return super().__exit__(exc_type, exc_val, exc_tb)
  39. def __call__(self, func):
  40. if torch._jit_internal.is_scripting():
  41. return func
  42. return super().__call__(func)
  43. # Preserved only for BC reasons
  44. @deprecated(
  45. "`torch.cuda.amp.autocast_mode._cast(value, dtype)` is deprecated. "
  46. "Please use `torch.amp.autocast_mode._cast(value, 'cuda', dtype)` instead.",
  47. category=FutureWarning,
  48. )
  49. def _cast(value, dtype):
  50. return torch.amp.autocast_mode._cast(value, "cuda", dtype)
  51. @deprecated(
  52. "`torch.cuda.amp.custom_fwd(args...)` is deprecated. "
  53. "Please use `torch.amp.custom_fwd(args..., device_type='cuda')` instead.",
  54. category=FutureWarning,
  55. )
  56. def custom_fwd(fwd=None, *, cast_inputs=None):
  57. """
  58. ``torch.cuda.amp.custom_fwd(args...)`` is deprecated. Please use
  59. ``torch.amp.custom_fwd(args..., device_type='cuda')`` instead.
  60. """
  61. return functools.partial(torch.amp.custom_fwd, device_type="cuda")(
  62. fwd=fwd, cast_inputs=cast_inputs
  63. )
  64. @deprecated(
  65. "`torch.cuda.amp.custom_bwd(args...)` is deprecated. "
  66. "Please use `torch.amp.custom_bwd(args..., device_type='cuda')` instead.",
  67. category=FutureWarning,
  68. )
  69. def custom_bwd(bwd):
  70. """
  71. ``torch.cuda.amp.custom_bwd(args...)`` is deprecated. Please use
  72. ``torch.amp.custom_bwd(args..., device_type='cuda')`` instead.
  73. """
  74. return functools.partial(torch.amp.custom_bwd, device_type="cuda")(bwd)