torch.cpu.amp.autocast 模式源代码
# mypy: 允许未类型化定义
from 打字
导入
任何
from typing_extensions 导入
已弃用
导入
火炬
全部 = [
"自动转换"]
[文档]类 autocast(torch.amp.autocast_mode.autocast):
r"""
查看 :class:`torch.autocast`。
``torch.cpu.amp.autocast(args...)`` 已弃用。请使用 ``torch.amp.autocast("cpu", args...)`` 代替。
"""
@deprecated(
"``torch.cpu.amp.autocast(args...)`` 已弃用。"
"请使用 ``torch.amp.autocast('cpu', args...)`` 代替。"
category=FutureWarning
)
def __init__(
self,
enabled: bool = True,
dtype: torch.dtype = torch.bfloat16,
cache_enabled: 布尔型 = True,
):
if torch._jit_internal.is_scripting():
self._enabled = enabled
self.device = "cpu"
self.fast_dtype = dtype
return
super().__init__(
"cpu", 启用=enabled, 数据类型=dtype, 缓存启用=cache_enabled
)
def __enter__(self):
if torch._jit_internal.is_scripting():
返回自身
返回父类的 __enter__ 方法
# TODO: 讨论一个统一的适用于 TorchScript 的 autocast API
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any): # 忽略[覆盖]
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():
返回 func
返回 super().__call__(func)