找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1429|回复: 15

英特尔(Intel)集成显卡也能使用GPU为模型训练加速(以Intel(R) iris(R) Xe Graphi

[复制链接]

93

主题

29

回帖

546

积分

高级会员

积分
546
发表于 2025-5-3 09:05:22 | 显示全部楼层 |阅读模式
本帖最后由 rafavi 于 2025-5-8 10:24 编辑

一,首先需要安装虚拟环境(miniconda),方便管理:

如果不知到怎么安装虚拟环境可以点这:安装虚拟环境

安装好虚拟环境后创建虚拟环境:在命令提示符(CMD)输入:

  1. conda create -n Py310 python=3.10
复制代码


Py310是你的虚拟环境的名字,当让也可以换成你喜欢的,python版本也可以是其他的,我个人觉得3.10到3.12比较好。执行后输入"y"确认即可,如图:


二、下载 Intel® oneAPI Base Toolkit

访问 Intel® oneAPI 下载页面Get the Intel® oneAPI Base ToolkitIntel® oneAPI 下载页面,选择适合你操作系统的版本(Windows/Linux/macOS)。

运行安装程序,选择 Custom Install(自定义安装)。

在组件列表中,只勾选以下内容:

Intel® oneAPI DPC++/C++ Compiler

Intel® oneAPI Level Zero

Intel® oneAPI AI Analytics Toolkit

Intel® VTune Profiler(可选)

点击 Next,选择安装路径(例如 D:\intel\oneAPI)。

完成安装。

配置环境变量
安装完成后,确保正确配置环境变量:打开命令提示符或终端。运行以下命令加载 oneAPI 环境变量:

call "D:\intel\oneAPI\setvars.bat" (注意确认好setvars.bat的实际位置,一般在C盘"C:\Program Files (x86)\Intel\oneAPI\setvars.bat")

三、安装Intel显卡驱动及extension for pytorch
1、打开Intel官方网址:https://pytorch-extension.intel.com/installation?platform=gpu&version=v2.7.10+xpu&os=windows&package=pip

按照下图中选:



2、安装intel显卡驱动:

往下滑,我的显卡是Intel(R) iris(R) Xe Graphics,随便安装了第一个


3、安装extension for pytorch及其他依赖库

激活刚刚创建的虚拟环境:

conda activate Py310
安装过程中我发现torch不支持numpy>=2,但是在安装完成后把numpy改为1.xx时出现以下错误:

OSError: [WinError 127] 找不到指定的程序。 Error loading "D:\ProgramData\miniconda3\envs\Py311\Lib\site-packages\torch\lib\aoti_custom_ops.dll" or one of its dependencies.(可能是链接出现错误),但是安装torch是系统自动检索numpy>=2的版本,所以为了解决这个问题,在创建完虚拟环境是第一步先安装numpy<2版本,我选择了numpy=1.26版本,因为matplotlib只支持1.23版本以上的。

  1. pip install numpy==1.26
复制代码

为了防止以后安装其他库时numpy自动更新,需要做一下步骤:

使用 pip freeze 锁定版本:

  1. pip freeze > requirements.txt
复制代码

然后在未来的安装中,使用以下命令来安装与当前环境中相同的依赖版本:

  1. pip install -r requirements.txt
复制代码

你要安装的库
因为第三个是核显,核显与集显的区别是有无独立内存。两者都是集成在处理器中。所以我就选择了第三个。
Intel ARC A 系列(有IPEX功能)
  1. conda install libuv
复制代码
  1. python -m pip install torch==2.5.1+cxx11.abi torchvision==0.20.1+cxx11.abi torchaudio==2.5.1+cxx11.abi intel-extension-for-pytorch==2.5.10+xpu --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/lnl/us/
复制代码


或者:

  1. conda install libuv
  2. pip install torch==2.3.1.post0+cxx11.abi torchvision==0.18.1.post0+cxx11.abi torchaudio==2.3.1.post0+cxx11.abi intel-extension-for-pytorch==2.3.110.post0+xpu --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/cn
复制代码

/

Intel ARC
  1. pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/xpu
复制代码
注意:按上述方法都不对(版本不对),错误提示:
ERROR! Intel® Extension for PyTorch needs to work with PyTorch 2.3., but PyTorch 2.8.0.dev20250503+xpu is found. Please switch to the matching version and run again.

正确的安装:

安装 PyTorch 2.3 + IPEX (XPU)

  1. pip install torch==2.3.1+cxx11.abi --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
  2. pip install intel_extension_for_pytorch==2.3.110+xpu --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
复制代码


Intel 提供了 针对 XPU 优化的 torchvision 和 torchaudio,但需要从 Intel 的官方仓库 安装,而不是 PyPI。

  1. pip install torchvision==0.18.1+cxx11.abi --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
  2. pip install torchaudio==2.3.1+cxx11.abi --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
复制代码
安装2.5.1版本:
  1. pip install torch==2.5.1+cxx11.abi torchvision==0.20.1+cxx11.abi torchaudio==2.5.1+cxx11.abi intel-extension-for-pytorch==2.5.10+xpu --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
复制代码
安装2.3.1版本:
  1. pip install torch==2.3.1+cxx11.abi torchvision==0.18.1.post0+cxx11.abi torchaudio==2.3.1.post0+cxx11.abi intel-extension-for-pytorch==2.3.110.post0+xpu --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
复制代码
以下汇总:(用intel的官方扩展,然后在Pytorch xpu官网(https://download.pytorch.org/whl/xpu)找对应的版本,比如 torch==2.7.0+xpu torchvision==0.22.0+xpu torchaudio==2.7.0+xpu
1.在Intel官网安装GPU for windows PIP  v2.7.10+xpu
2.https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ 安装intel-extension-for-pytorch的版本2.7.10+xpu
  1. pip install intel_extension_for_pytorch==2.7.10+xpu --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
复制代码
3.在在Pytorch xpu官网(https://download.pytorch.org/whl/xpu)找对应的版本,比如 torch==2.7.0+xpu torchvision==0.22.0+xpu torchaudio==2.7.0+xpu
  1. pip install torch==2.7.0+xpu torchvision==0.22.0+xpu torchaudio==2.7.0+xpu --index-url https://download.pytorch.org/whl/xpu
复制代码


等待安装完成,检验可用性:关掉命令窗口,从新激活虚拟环境,输入:

  1. python -c "import torch; import intel_extension_for_pytorch; print(torch.xpu.is_available());"
复制代码

可能会有警告,可以忽略掉,结尾出现True即可。


当然可能也会出现报错,如依赖包不匹配,比如我遇到numpy不匹配,需要降到1.xx版本。

三、激动人心的时刻。在你的项目中切换到刚创建的虚拟环境中
添加以下代码即可用GPU加速,其实和使用CUDA的代码区别不大,无非是把cuda改成xpu


  1. import torch
复制代码


# 检查 GPU 是否可用
  1. device = torch.device("xpu" if torch.xpu.is_available() else "cpu")
复制代码


# 假设你有一个模型
  1. model = MyModel().to(device)
复制代码


# 假设你有一些输入数据
  1. inputs = inputs.to(device)
复制代码


# 训练时将模型和数据移到 GPU
  1. outputs = model(inputs)
复制代码

最后,提醒一下,注意每次训练的batch_size不要太大,从小往大试,不让会把显存撑爆,导致闪屏(因为我试过,如果真的这样,建议关机重启,或者重装显卡驱动)

按照以下步骤可以查看显存占用情况:


                        
原文链接













回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 19:10:54 | 显示全部楼层
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 19:27:05 | 显示全部楼层
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 19:47:44 | 显示全部楼层
sympy 1.13.1
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 19:49:49 | 显示全部楼层
本帖最后由 rafavi 于 2025-5-4 20:08 编辑

Successfully installed sympy-1.13.1 torch-2.5.1+cxx11.abi torchaudio-2.5.1+cxx11.abi torchvision-0.20.1+cxx11.abi
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 20:09:13 | 显示全部楼层
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Could not find a version that satisfies the requirement torch==2.5.1+cxx11.abi (from versions: 1.11.0, 1.12.0, 1.12.1, 1.13.0, 1.13.1, 2.0.0, 2.0.1, 2.1.0, 2.1.1, 2.1.2, 2.2.0, 2.2.1, 2.2.2, 2.3.0, 2.3.1, 2.4.0, 2.4.1, 2.5.0, 2.5.1, 2.6.0, 2.7.0)
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 20:26:21 | 显示全部楼层
在conda虚拟环境下安装的,免得插件冲突。conda activate py310
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 21:38:12 | 显示全部楼层
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-4 22:15:01 | 显示全部楼层
pip install torch==2.5.1+cxx11.abi torchvision==0.20.1+cxx11.abi torchaudio==2.5.1+cxx11.abi intel-extension-for-pytorch==2.7.10+xpu --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
回复

使用道具 举报

93

主题

29

回帖

546

积分

高级会员

积分
546
 楼主| 发表于 2025-5-5 12:11:00 | 显示全部楼层
ComfyUI For Intel GPUs (Windows and Linux) https://github.com/comfyanonymou ... -windows-linux]LINK[/url]
(Option 1) Intel Arc GPU users can install native PyTorch with torch.xpu support using pip (currently available in PyTorch nightly builds). More information can be found here

To install PyTorch nightly, use the following command:
pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/xpu

Launch ComfyUI by running python main.py
(Option 2) Alternatively, Intel GPUs supported by Intel Extension for PyTorch (IPEX) can leverage IPEX for improved performance.

For Intel® Arc™ A-Series Graphics utilizing IPEX, create a conda environment and use the commands below:
conda install libuv
pip install torch==2.3.1.post0+cxx11.abi torchvision==0.18.1.post0+cxx11.abi torchaudio==2.3.1.post0+cxx11.abi intel-extension-for-pytorch==2.3.110.post0+xpu --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/cn/
For other supported Intel GPUs with IPEX, visit Installation for more information.

Additional discussion and help can be found here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


QQ|Archiver|手机版|小黑屋|Bluetooth-UWB 联盟论坛 ( 京ICP备19003900号-5 )

GMT+8, 2025-12-5 04:36 , Processed in 0.020226 second(s), 35 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表