资讯详情

资讯详情

如何在 Intel GPU 上首次安装并运行 PyTorch(XPU)?

如何在 Intel GPU 上首次安装并运行 PyTorchXPU【免费下载链接】pytorchTensors and Dynamic neural networks in Python with strong GPU acceleration项目地址: https://gitcode.com/GitHub_Trending/py/pytorch你的任务是在装有 Intel GPU 的机器上第一次装好 PyTorch并让一个真实模型跑在 XPU 设备上。按 XPU 入门文档 的路径整个流程是确认硬件与系统受支持 → 安装 Intel GPU 驱动 → 用 pip 安装带xpu支持的 PyTorch 二进制包 → 用torch.xpu.is_available()验证 → 运行示例程序。Intel GPU 支持从 PyTorch 2.5 起以 Prototype 形式提供覆盖 Linux 和 Windows训练与推理工作流都支持。1. 先确认硬件和操作系统在支持范围内入门文档给出了两类硬件的支持矩阵安装前先核对Intel 数据中心 GPUDeviceRHEL 9.2SLES 15 SP5Ubuntu Server 22.04 ( 5.15 LTS kernel)Intel® Data Center GPU Max Series (CodeName: Ponte Vecchio)yesyesyesIntel 客户端 GPUSupported OSValidated HardwareWindows 11 Ubuntu 24.04/26.04 WSL2 Ubuntu 24.04/26.04Intel® Arc™ A-Series Graphics (CodeName: Alchemist)、Intel® Arc™ B-Series Graphics (CodeName: Battlemage)、Intel® Core™ Ultra Processors with Intel® Arc™ Graphics (CodeName: Meteor Lake-H)、Intel® Core™ Ultra Processors (Series 2) with Intel® Arc™ Graphics (CodeName: Arrow Lake-H)、Intel® Core™ Ultra Mobile Processors (Series 2) with Intel® Arc™ Graphics (CodeName: Lunar Lake)Windows 11 Ubuntu 26.04 WSL2 Ubuntu 24.04/26.04Intel® Core™ Ultra Mobile Processors (Series 3) with Intel® Arc™ Graphics (CodeName: Panther Lake)如果你的显卡或操作系统不在上表中文档没有给出对应支持结论不建议继续。2. 安装 Intel GPU 驱动必做前置使用 PyTorch 跑 Intel GPU 前必须先安装 Intel GPU 驱动文档指向 Intel 官方的 PyTorch Prerequisites for Intel GPUs 页面获取驱动安装指引。注意两种安装方式对依赖的要求不同从二进制包安装只需要驱动可以跳过 Intel® Deep Learning Essentials 的安装部分从源码构建驱动和 Intel® Deep Learning Essentials 都要安装。3. 用 pip 安装 XPU 版 PyTorch驱动装好后安装最新的稳定版 XPU 轮子包含torch、torchvision、torchaudio三个包pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/xpu以下两条是可选分支不是首次安装必须执行的内容Nightly 预览版pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/xpu历史版本命令模板为pip3 install torchTORCH_VERSION torchvisionTORCHVISION_VERSION torchaudioTORCHAUDIO_VERSION --index-url https://download.pytorch.org/whl/xpu其中TORCH_VERSION等三个占位符要替换成你要的具体版本号可用版本见文档指引的 previous versions 页面。4. 验证 XPU 设备是否可用安装完成后用torch.xpuAPI 检查设备import torch print(torch.xpu.is_available()) # torch.xpu is the API for Intel GPU support输出为True说明 PyTorch 已识别到你的 Intel GPU。文档给出的排查方法只有一条如果输出是False重新检查 Intel GPU 驱动的安装。5. 运行第一个 XPU 程序FP32 推理入门文档给出的最短可运行示例是一个 ResNet-50 推理程序模型和输入都通过.to(xpu)移到 Intel GPUimport torch import torchvision.models as models model models.resnet50(weightsResNet50_Weights.DEFAULT) model.eval() data torch.rand(1, 3, 224, 224) model model.to(xpu) data data.to(xpu) with torch.no_grad(): model(data) print(Execution finished)程序正常结束时打印Execution finished即表示模型已完整跑过一次 XPU 前向推理。6. 已有 CUDA 代码怎么切到 XPU如果你是从cuda迁移代码文档给出的最小改动是把设备引用从cuda换成xpu# CUDA CODE tensor torch.tensor([1.0, 2.0]).to(cuda) # CODE for Intel GPU tensor torch.tensor([1.0, 2.0]).to(xpu)文档同时明确了当前支持范围和限制训练和推理工作流都支持eager 模式和torch.compile都支持torch.compile在 Windows 上从 PyTorch 2.7 起支持 Intel GPUFP32、BF16、FP16 数据类型和 Automatic Mixed Precision (AMP) 都支持。7. 可选分支AMP 推理需要混合精度推理时在torch.autocast中指定device_typexpu默认示例用torch.float16注释里说明想跑 BF16 就把dtype改成torch.bfloat16import torch import torchvision.models as models model models.resnet50(weightsResNet50_Weights.DEFAULT) model.eval() data torch.rand(1, 3, 224, 224) model model.to(xpu) data data.to(xpu) with torch.no_grad(): d torch.rand(1, 3, 224, 224) d d.to(xpu) # set dtypetorch.bfloat16 for BF16 with torch.autocast(device_typexpu, dtypetorch.float16, enabledTrue): model(data) print(Execution finished)这里有一个文档明确标注的训练侧限制训练中使用GradScaler需要硬件支持FP64而 Intel® Arc™ A-Series Graphics 不原生支持FP64。如果你的工作负载跑在 Arc A-Series 上请禁用GradScaler。8. 可选分支从源码构建带 Intel GPU 支持的 PyTorchpip 二进制包满足不了需求时例如需要跟进最新改动可以按 README 的 From Source 流程构建。与 XPU 直接相关的要求是先按 README Intel GPU Support 一节完成 Intel GPU 驱动和 Intel® Deep Learning Essentials 的安装Intel GPU 目前支持 Linux 和 Windows想禁用 Intel GPU 支持时导出USE_XPU0若要在 Linux 上用make triton安装配套 triton配合 torch.compile 使用需先显式export USE_XPU1。通用的源码构建前置条件与命令来自 README From Source需要 Python 3.10 及以上、完全支持 C20 的编译器Linux 上 gcc 11.3.0 或更新版本、至少 10 GB 空闲磁盘首次构建约 30–60 分钟。git clone https://github.com/pytorch/pytorch cd pytorch git submodule sync git submodule update --init --recursive# 在 PyTorch 源码目录下 pip install --group devLinux 下再执行pip install mkl-static mkl-include # (可选) 若使用 torch.compile 的 inductor/triton先 export USE_XPU1再运行 make triton最后安装 PyTorch 本身# conda 环境 export CMAKE_PREFIX_PATH${CONDA_PREFIX:-$(dirname $(which conda))/../}:${CMAKE_PREFIX_PATH} python -m pip install --no-build-isolation -v -e . # 非 conda 的 venv 环境激活 venv 后改用 # export CMAKE_PREFIX_PATH${VIRTUAL_ENV}:${CMAKE_PREFIX_PATH}构建完成后同样用第 4 步的torch.xpu.is_available()验证 XPU 是否生效。边界与限制Intel GPU 支持在文档中定位为 Prototype自 PyTorch 2.5 起API 行为后续版本可能仍有变化硬件/OS 支持以第 1 节的两张表为准表外组合文档未给出结论Arc A-Series 上训练用 AMP 时须禁用GradScaler缺少原生 FP64 支持torch.compile在 Windows 上要求 PyTorch 2.7 及以上。完整示例代码AMP 训练、torch.compile训练与推理等都在 XPU 入门文档 的 Examples 一节中可以对照本文的命令逐步执行。【免费下载链接】pytorchTensors and Dynamic neural networks in Python with strong GPU acceleration项目地址: https://gitcode.com/GitHub_Trending/py/pytorch创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
觉得有用,分享给同行:

为您的企业打造数字门面

稳重轻奢商务风格,端正雅致视觉,长效耐看不易过时。

立即咨询 →