z
zkx_/ERNIE-Image-Turbo-Ascend
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

ERNIE-Image-Turbo on Ascend NPU

1. 简介

本文档记录 ERNIE-Image-Turbo 在华为昇腾 Ascend 910B3 NPU 上的适配部署与精度性能评测结果。

ERNIE-Image-Turbo 是百度 ERNIE-Image 团队开发的文本到图像生成模型,基于单流 Diffusion Transformer (DiT) 架构,仅需 8 步推理即可生成高质量图像。

适配要点:

  • 设备切换:cuda -> npu
  • FlashAttention Mask 兼容性修复([B,1,1,Skv] -> [B,1,Sq,Skv])
  • bfloat16 混合精度

相关获取地址:

  • 原始权重(HuggingFace):https://huggingface.co/Baidu/ERNIE-Image-Turbo
  • 原始权重(ModelScope):https://modelscope.cn/models/PaddlePaddle/ERNIE-Image-Turbo
  • 原始权重(AtomGit):https://atomgit.com/paddlepaddle/ERNIE-Image-Turbo

2. 验证环境

组件版本
CANN8.5.1
PyTorch2.10.0+cpu
torch_npu2.10.0
diffusers0.38.0
transformers5.8.0
accelerate1.13.0
huggingface_hub1.14.0
  • NPU:1 张 Ascend 910B3(64GB HBM),共 8 卡可用
  • 模型路径:/data/zkx/ERNIE-Image-Turbo

3. 适配说明

3.1 设备切换

pipe.to("npu")  # 原为 pipe.to("cuda")

3.2 FlashAttention Mask 兼容性

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)

3.3 数据类型

推理使用 torch.bfloat16 以获得最佳性能与精度平衡。

3.4 适配文件

文件说明
npu_adapter.pyNPU Attention Processor 适配模块
inference.pyNPU 推理脚本(支持命令行参数)
eval_ernie.py精度与性能自动化评测脚本

4. 快速开始

安装依赖

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

5. 推理性能

测试条件:Ascend 910B3 x 1,bf16,8 steps,连续运行 3 次取平均。

512 x 512

指标数值
Run 1 (含编译预热)28.82 s
Run 222.62 s
Run 322.33 s
平均时间24.59 s
每步耗时3.07 s
每秒步数0.33 steps/s
模型加载时间~180 s

1024 x 1024(参考)

指标数值
推理时间~28-33 s
每步耗时~3.5-4.1 s
峰值显存~55 GB

256 x 256(精度对比)

dtype推理时间
bf160.96 s
fp321.06 s

6. 精度评测

评测方法

在相同种子(seed=42)下,分别运行 bf16 和 fp32 推理,比较输出图像的像素级差异(关闭 Prompt Enhancer 以排除 LLM 差异干扰)。

  • 分辨率:256 x 256(fp32 显存限制)
  • 步数:8
  • 指标:归一化 MSE、PSNR、SSIM
  • 阈值:归一化 MSE < 1%

评测结果

指标数值
归一化 MSE0.007657%
PSNR41.16 dB
SSIM0.9985
结论PASSED(误差 < 1%)

bf16 与 fp32 的输出差异极小(MSE 仅 0.008%),远低于 1% 阈值,说明 NPU 上 bf16 精度满足部署要求。

PE 影响说明

在使用 Prompt Enhancer(PE)时,由于 PE 模型(Ministral3ForCausalLM)在 bf16 和 fp32 下的采样结果存在差异,会导致最终图像有较大差异(归一化 MSE ~5.9%)。这是 LLM 推理的固有现象,并非 DiT 主模型的精度问题。实际部署时应固定 PE 输出或关闭 PE 以确保可复现性。

7. 项目结构

ERNIE-Image-Turbo-ascend/
├── inference.py          # NPU 推理脚本
├── npu_adapter.py        # NPU Attention 适配模块
├── eval_ernie.py         # 精度与性能评测脚本
├── README.md             # 本文档
├── log.txt               # 评测运行日志
└── eval_report.json      # 评测数据报告

8. 注意事项

  1. 首次加载较慢:模型加载约需 3 分钟(bf16),fp32 加载因权重缓存仅需 ~30 秒
  2. 显存:1024x1024 推理峰值显存 ~55GB(bf16),fp32 需降低分辨率
  3. PE 可复现性:Prompt Enhancer 为 LLM,不同 dtype 下输出可能不同,精度对比时建议关闭
  4. 容器环境:需设置 HF_HUB_OFFLINE=1 离线加载本地权重
  5. 首次推理预热:首次推理包含图编译,后续同分辨率推理更快(20-23s vs 28s)