本文档记录 ERNIE-Image-Turbo 在华为昇腾 Ascend 910B3 NPU 上的适配部署与精度性能评测结果。
ERNIE-Image-Turbo 是百度 ERNIE-Image 团队开发的文本到图像生成模型,基于单流 Diffusion Transformer (DiT) 架构,仅需 8 步推理即可生成高质量图像。
适配要点:
cuda -> npu[B,1,1,Skv] -> [B,1,Sq,Skv])相关获取地址:
| 组件 | 版本 |
|---|---|
| CANN | 8.5.1 |
| PyTorch | 2.10.0+cpu |
| torch_npu | 2.10.0 |
| diffusers | 0.38.0 |
| transformers | 5.8.0 |
| accelerate | 1.13.0 |
| huggingface_hub | 1.14.0 |
/data/zkx/ERNIE-Image-Turbopipe.to("npu") # 原为 pipe.to("cuda")diffusers 的 ErnieImageSingleStreamAttnProcessor 将 attention mask 展开为 [B, 1, 1, Skv] 格式,但昇腾 FlashAttention kernel 要求 [B, 1, Sq, Skv] 或 [Sq, Skv] 格式。通过自定义 ErnieImageSingleStreamAttnProcessorNPU 处理器修复。
适配代码(npu_adapter.py 核心修复):
if attention_mask.ndim == 4 and attention_mask.shape[-2] == 1 and query.shape[1] > 1:
attention_mask = attention_mask.expand(-1, -1, query.shape[1], -1)推理使用 torch.bfloat16 以获得最佳性能与精度平衡。
| 文件 | 说明 |
|---|---|
| npu_adapter.py | NPU Attention Processor 适配模块 |
| inference.py | NPU 推理脚本(支持命令行参数) |
| eval_ernie.py | 精度与性能自动化评测脚本 |
pip install diffusers transformers accelerate huggingface_hub Pillow \
--index-url https://mirrors.huaweicloud.com/repository/pypi/simple/ \
--trusted-host mirrors.huaweicloud.com# 从 AtomGit 下载
git clone https://atomgit.com/paddlepaddle/ERNIE-Image-Turbo.git
cd ERNIE-Image-Turbo && git lfs pull
# 或从 ModelScope(推荐国内)
export HF_ENDPOINT=https://hf-mirror.com# 基础推理
python3 inference.py \
--prompt "A beautiful landscape with mountains and a lake at sunset" \
--height 1024 --width 1024 \
--num-steps 8 --seed 42 \
--output output.png
# 运行完整评测
python3 eval_ernie.py \
--height 512 --width 512 \
--num-steps 8测试条件:Ascend 910B3 x 1,bf16,8 steps,连续运行 3 次取平均。
| 指标 | 数值 |
|---|---|
| Run 1 (含编译预热) | 28.82 s |
| Run 2 | 22.62 s |
| Run 3 | 22.33 s |
| 平均时间 | 24.59 s |
| 每步耗时 | 3.07 s |
| 每秒步数 | 0.33 steps/s |
| 模型加载时间 | ~180 s |
| 指标 | 数值 |
|---|---|
| 推理时间 | ~28-33 s |
| 每步耗时 | ~3.5-4.1 s |
| 峰值显存 | ~55 GB |
| dtype | 推理时间 |
|---|---|
| bf16 | 0.96 s |
| fp32 | 1.06 s |
在相同种子(seed=42)下,分别运行 bf16 和 fp32 推理,比较输出图像的像素级差异(关闭 Prompt Enhancer 以排除 LLM 差异干扰)。
| 指标 | 数值 |
|---|---|
| 归一化 MSE | 0.007657% |
| PSNR | 41.16 dB |
| SSIM | 0.9985 |
| 结论 | PASSED(误差 < 1%) |
bf16 与 fp32 的输出差异极小(MSE 仅 0.008%),远低于 1% 阈值,说明 NPU 上 bf16 精度满足部署要求。
在使用 Prompt Enhancer(PE)时,由于 PE 模型(Ministral3ForCausalLM)在 bf16 和 fp32 下的采样结果存在差异,会导致最终图像有较大差异(归一化 MSE ~5.9%)。这是 LLM 推理的固有现象,并非 DiT 主模型的精度问题。实际部署时应固定 PE 输出或关闭 PE 以确保可复现性。
ERNIE-Image-Turbo-ascend/
├── inference.py # NPU 推理脚本
├── npu_adapter.py # NPU Attention 适配模块
├── eval_ernie.py # 精度与性能评测脚本
├── README.md # 本文档
├── log.txt # 评测运行日志
└── eval_report.json # 评测数据报告