autocast_mode.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # mypy: allow-untyped-defs
  2. from typing import Any
  3. from typing_extensions import deprecated
  4. import torch
  5. __all__ = ["autocast"]
  6. class autocast(torch.amp.autocast_mode.autocast):
  7. r"""
  8. See :class:`torch.autocast`.
  9. ``torch.cpu.amp.autocast(args...)`` is deprecated. Please use ``torch.amp.autocast("cpu", args...)`` instead.
  10. """
  11. @deprecated(
  12. "`torch.cpu.amp.autocast(args...)` is deprecated. "
  13. "Please use `torch.amp.autocast('cpu', args...)` instead.",
  14. category=FutureWarning,
  15. )
  16. def __init__(
  17. self,
  18. enabled: bool = True,
  19. dtype: torch.dtype = torch.bfloat16,
  20. cache_enabled: bool = True,
  21. ):
  22. if torch._jit_internal.is_scripting():
  23. self._enabled = enabled
  24. self.device = "cpu"
  25. self.fast_dtype = dtype
  26. return
  27. super().__init__(
  28. "cpu", 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)