张芷铭的个人博客

DeepSpeed XPU 模块缺失导致 ModuleNotFoundError,需安装 XPU 兼容版本或源码编译启用 XPU 支持。

问题根源

层面表现解决方案
XPU 支持未启用DeepSpeed 缺少 XPU 编译选项源码编译时添加 DS_BUILD_XPU=1
版本冲突Transformers 与 DeepSpeed 不兼容锁定版本 ds0.14.0 + tf4.41.0
依赖链断裂CLIP 模型触发 DeepSpeed 异常临时屏蔽缺失模块导入

快速修复

1
2
3
4
5
6
7
8
# 卸载冲突版本
pip uninstall -y deepspeed transformers

# 安装 XPU 兼容版本
pip install deepspeed==0.14.0 --no-build-isolation

# 安装兼容的 Transformers
pip install transformers==4.41.0 flash-attn==2.5.0

验证 PyTorch XPU 支持

1
2
3
import torch
assert hasattr(torch, "xpu") and torch.xpu.is_available(), "XPU 未启用!"
print(f"XPU 设备数量: {torch.xpu.device_count()}")

应急方案

临时屏蔽缺失模块:

1
2
import sys
sys.modules['deepspeed.ops.op_builder.xpu'] = None

源码编译 DeepSpeed(启用 XPU)

1
2
3
git clone https://github.com/microsoft/DeepSpeed
cd DeepSpeed
DS_BUILD_OPS=1 DS_BUILD_XPU=1 pip install . --verbose

使用 Intel 官方 Docker

1
2
FROM intel/intel-extension-for-pytorch:2.3.10-xpu
RUN pip install deepspeed==0.14.0 transformers==4.41.0

验证修复

1
2
3
4
from deepspeed.ops.op_builder import XPU_OpBuilder
from transformers import CLIPVisionModelWithProjection
model = CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-base-patch32")
print("所有模块加载成功")

技术背景

XPU 与 Flash Attention:Intel XPU 通过 op_builder.xpu.flash_attn 实现 Flash Attention 算子加速,需编译时显式启用。

DeepSpeed 依赖链:Transformers 初始化时自动加载 DeepSpeed,任何安装问题都会导致级联崩溃。

Comments