torch.cuda.amp.autocast_mode 的源代码
# mypy: 允许未类型化定义
导入 functools
from 打字
导入
任何
from typing_extensions 导入
已弃用
导入
火炬
全部 = [
"自动转换", "custom_fwd", "custom_bwd"]
[文档]类 autocast(torch.amp.autocast_mode.autocast)
参见 :class:`torch.autocast`
`torch.cuda.amp.autocast(args...)` 已弃用。请使用 `torch.amp.autocast("cuda", args...)` 代替。
"""
@deprecated(
`torch.cuda.amp.autocast(args...)` 已弃用。
"请使用 `torch.amp.autocast('cuda', args...)` 代替。"
category=FutureWarning
)
def __init__(
self,
enabled: 布尔型 = True,
dtype: torch.dtype = torch.float16,
cache_enabled: 布尔型 = True,
):
if torch._jit_internal.is_scripting():
self._enabled = enabled
self.device = "cuda"
self.fast_dtype = dtype
return
super().__init__()
"cuda", enabled=enabled, dtype=dtype, cache_enabled=cache_enabled
)
def __enter__(self):
if torch._jit_internal.is_scripting():
return self
return super().__enter__()
# TODO: 讨论一个适用于 autocast 的统一 TorchScript API
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any): # type: ignore[override]
if torch._jit_internal.is_scripting():
返回
return super().__exit__(exc_type, exc_val, exc_tb)
def __call__(self, func):
if torch._jit_internal.is_scripting():
返回函数
返回 super().__call__(func)
仅保留以支持向后兼容
@deprecated(
"`torch.cuda.amp.autocast_mode._cast(value, dtype)` 已弃用。"
"请使用 `torch.amp.autocast_mode._cast(value, 'cuda', dtype)` 代替。",
分类=
未来警告,
)
def _投射(
值,
数据类型):
返回
火把.
转换.autocast_mode.
_投射(
值,
cuda,
数据类型)
[文档]@已弃用(
"`torch.cuda.amp.custom_fwd(args...)` 已弃用。"
请使用 `torch.amp.custom_fwd(args..., device_type='cuda')` 代替。
category=FutureWarning
)
def custom_fwd(fwd=None, *, cast_inputs=None):
```python
# 假设输入文本为:
input_text = '"""'
# 翻译函数(此处仅为示例,实际翻译功能需要调用真实的翻译 API)
def translate_to_simplified_chinese(text):
# 这里应该调用真实的翻译 API 进行翻译
# 由于示例中不使用真实的 API,以下为模拟翻译结果
return text
# 输出翻译结果
translated_text = translate_to_simplified_chinese(input_text)
print(translated_text)
```
torch.cuda.amp.custom_fwd(args...) 已弃用。请使用
torch.amp.custom_fwd(args..., device_type='cuda')
```python
# 假设输入文本为:
input_text = '"""'
# 翻译函数(此处仅为示例,实际翻译功能需要调用真实的翻译 API)
def translate_to_simplified_chinese(text):
# 这里应该调用真实的翻译 API 进行翻译
# 由于示例中不使用真实的 API,以下为模拟翻译结果
return text
# 输出翻译结果
translated_text = translate_to_simplified_chinese(input_text)
print(translated_text)
```
return functools.partial(torch.amp.custom_fwd, device_type="cuda")(
fwd=fwd, cast_inputs=cast_inputs
)
[文档]@已弃用(
"``torch.cuda.amp.custom_bwd(args...)`` 已弃用。"
"请使用 ``torch.amp.custom_bwd(args..., device_type='cuda')`` 代替。"
category=FutureWarning,
)
def custom_bwd(bwd):
"""
``torch.cuda.amp.custom_bwd(args...)`` 已弃用。请使用
``torch.amp.custom_bwd(args..., device_type='cuda')`` 代替。
"""
"""
return functools.partial(torch.amp.custom_bwd, device_type="cuda")(bwd)