mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
This reverts commit 749a132fb0.
Reverted https://github.com/pytorch/pytorch/pull/126898 on behalf of https://github.com/fbgheith due to switching typing-extensions=4.3.0 to 4.9.0 causes internal failure ([comment](https://github.com/pytorch/pytorch/pull/126898#issuecomment-2142884456))
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
import warnings
|
|
from typing import Any
|
|
|
|
import torch
|
|
|
|
__all__ = ["autocast"]
|
|
|
|
|
|
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.
|
|
"""
|
|
|
|
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
|
|
warnings.warn(
|
|
"torch.cpu.amp.autocast(args...) is deprecated. Please use torch.amp.autocast('cpu', args...) instead.",
|
|
DeprecationWarning,
|
|
)
|
|
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)
|