torch.accelerator.current_accelerator¶
- torch.accelerator.current_accelerator()[原始碼][原始碼]¶
傳回目前加速器的裝置。
- 傳回
以
torch.device
傳回目前的加速器。- 傳回類型
注意
返回的
torch.device
的索引將會是None
,請使用torch.accelerator.current_device_index()
來得知目前正在使用的索引。並且請確保使用torch.accelerator.is_available()
來檢查是否有可用的加速器。如果沒有可用的加速器,此函數將會拋出例外。範例
>>> if torch.accelerator.is_available(): >>> current_device = torch.accelerator.current_accelerator() >>> else: >>> current_device = torch.device("cpu") >>> if current_device.type == 'cuda': >>> is_half_supported = torch.cuda.has_half >>> elif current_device.type == 'xpu': >>> is_half_supported = torch.xpu.get_device_properties().has_fp16 >>> elif current_device.type == 'cpu': >>> is_half_supported = True