在 Windows 上启用 PyTorch 与 DirectML

使用 DirectML 的 PyTorch 可以在支持 DirectX 12 的 GPU 上进行训练和推理。 搭载 DirectML 的 PyTorch 现已推出公共预览版,并且自 Windows 10 版本 1709 起可在原生 Windows 上运行。

检查Windows的版本

若要检查Windows版本和版本号,请选择Windows徽标键 + R,输入winver并选择“确定”。 如果内部版本较旧,请更新到 Windows 10 版本 1709 或更高版本。

检查 GPU 驱动程序更新

通过Windows 更新或硬件制造商的网站安装 GPU 可用的最新驱动程序。

配置 Python

安装Python环境。 如果使用 Miniconda,请下载并运行体系结构Windows安装程序。

然后创建并激活一个名为 pytorch-directml 的环境:

conda create --name pytorch-directml python=3.10
conda activate pytorch-directml

使用 DirectML 安装 PyTorch

安装 torch-directml 包:

pip install torch-directml

验证安装

启动 Python 并运行以下代码,以在 DirectML 设备上将两个张量相加:

import torch
import torch_directml

dml = torch_directml.device()
tensor1 = torch.tensor([1]).to(dml)
tensor2 = torch.tensor([2]).to(dml)
dml_algebra = tensor1 + tensor2
print(dml_algebra.item())

预期输出:

3

示例和反馈

有关示例,请参阅 DirectML PyTorch 示例 。 若要报告包问题或请求功能,请使用 DirectML 问题跟踪器。