能否在PyTorch中使用Intel Iris GPU运行深度学习代码?
问题描述
- 已通过Anaconda安装PyTorch,使用命令:
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia - 设备无CUDA环境,仅搭载Windows系统默认的Intel Iris GPU,希望了解能否利用该GPU运行深度学习代码
- 尝试运行自定义的设备检测代码时出现报错,错误信息显示
mkl不是合法的PyTorch设备类型
解决方案
1. 更换支持Intel GPU的PyTorch版本
你当前安装的是面向NVIDIA CUDA的PyTorch版本,无法直接适配Intel Iris GPU。需重新安装支持Intel GPU加速的版本:
- 先卸载现有PyTorch组件:
conda uninstall pytorch torchvision torchaudio pytorch-cuda - 安装基础CPU版PyTorch:
conda install pytorch torchvision torchaudio cpuonly -c pytorch - 安装Intel PyTorch扩展以启用GPU加速:
pip install intel-extension-for-pytorch
2. 修正设备检测代码
原代码错误在于将mkl当作设备类型——mkl是CPU的数值优化库,并非GPU设备标识。修改后的检测代码如下:
def get_hwacc_device(): device = torch.device('cpu') # 检测CUDA(保留原逻辑) if torch.cuda.is_available(): print(torch.cuda.get_device_name(0)) print('CUDA memory usage:') print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1),'GB') print('Cached: ', round(torch.cuda.memory_reserved(0)/1024**3,1),'GB') # 修正原代码拼写错误:reservrd→reserved device = torch.device('cuda') # 检测Intel GPU(需安装Intel PyTorch扩展) elif hasattr(torch, 'xpu') and torch.xpu.is_available(): print(torch.xpu.get_device_name(0)) device = torch.device('xpu') # 提示MKL加速状态(CPU层面优化) elif torch.backends.mkl.is_available(): print("MKL CPU acceleration enabled") print("Using device:", device) return device
3. 验证Intel GPU可用性
安装完成后运行上述代码,若输出Using device: xpu,则说明Intel Iris GPU已成功启用,可用于深度学习代码的加速运行。
内容的提问来源于stack exchange,提问作者Rainbow
相关产品推荐
相关产品推荐

