torch 加速器.current_accelerator ¶
- torch 加速器.current_accelerator(check_available=False)[source][source] ¶
返回编译时可用加速器的设备。如果编译时没有可用加速器,则返回 None。请参阅加速器获取详细信息。
- 参数:
check_available (布尔值,可选) – 如果为 True,将在编译时检查的基础上也进行运行时检查,以查看设备
torch.accelerator.is_available()
是否可用。默认:False
- 返回值:
返回当前加速器为
torch.device
。- 返回类型:
注意
返回的
torch.device
的索引将是None
,请使用torch.accelerator.current_device_index()
了解当前使用的索引。示例:
>>> # If an accelerator is available, sent the model to it >>> model = torch.nn.Linear(2, 2) >>> if (current_device := current_accelerator(check_available=True)) is not None: >>> model.to(current_device)