本文档记录 DeepGen-1.0 在华为昇腾 Ascend 910 NPU 上的适配与验证结果。DeepGen-1.0 是 deepgenteam 推出的轻量级统一多模态图像生成与编辑模型(5B 参数:3B VLM + 2B DiT),基于 diffusers 框架,支持文生图、图像编辑、推理图像生成、推理图像编辑和文字渲染五大核心能力。
本次适配使用 diffusers 格式权重(DeepGen-1.0-diffusers),在单卡 NPU 上完成推理验证:
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.9.0+cpu |
torch_npu | 2.9.0.post1 |
diffusers | 0.38.0 |
transformers | 4.57.6 |
accelerate | 1.13.0 |
einops | 0.8.1 |
| CANN | 8.5.1 |
2 卡,64GB HBM/卡/tmp/DeepGen-cache/deepgenteam/DeepGen-1___0-diffuserspip install -r requirements.txtrequirements.txt 内容:
torch>=2.5.0
torch_npu>=2.5.0
diffusers>=0.35.0
transformers>=4.45.0
accelerate>=1.0.0
Pillow>=10.0.0
einops>=0.8.0
safetensors注意:NPU 上使用 SDPA 注意力机制,无需安装 flash_attn。
python inference.py \
--model_path /path/to/DeepGen-1.0-diffusers \
--prompt "a photo of a blue pizza and a yellow baseball glove" \
--output_image output.png \
--height 512 --width 512 \
--seed 42 \
--num_inference_steps 50 \
--guidance_scale 4.0 \
--device npupython inference.py \
--model_path /path/to/DeepGen-1.0-diffusers \
--prompt "Place this guitar on a sandy beach with the sunset in the background." \
--input_image guitar.png \
--output_image output_edit.png \
--height 512 --width 512 \
--seed 42 \
--device npu| 参数 | 默认值 | 说明 |
|---|---|---|
--model_path | (见脚本) | DeepGen-1.0-diffusers 模型目录路径 |
--prompt | (见脚本) | 生成/编辑提示词 |
--input_image | None | 输入图片路径(编辑模式,留空则文生图) |
--output_image | output_deepgen.png | 输出图片保存路径 |
--height | 512 | 输出图像高度 |
--width | 512 | 输出图像宽度 |
--seed | 42 | 随机种子 |
--num_inference_steps | 50 | 推理步数 |
--guidance_scale | 4.0 | CFG 缩放系数 |
--device | npu | 推理设备:npu、cpu、auto |
flash_attention_2 替换为 sdpa(Scaled Dot-Product Attention),因为 flash_attn 不支持昇腾 NPUtrust_remote_code=True 加载 diffusers 格式权重,自动加载自定义 pipeline 代码flash_attn、triton 等 NPU 不兼容依赖使用相同参数(seed=42, steps=50, cfg=4.0, 512x512)在 NPU 上进行两次独立推理,结果完全一致:
| 指标 | 数值 |
|---|---|
| MSE | 0.000000 |
| 最大像素差异 | 0 |
| 平均像素差异 | 0.0000 |
| 是否完全相同 | 是 |
| PSNR | ∞(完全一致) |
结论:NPU 推理在相同种子下具有完美的确定性输出,两次运行产生逐位相同的图像。
由于 torch_npu 注册了 NPU 专用的 npu_rms_norm 算子,该算子不支持 CPU 后端,导致模型在 CPU 上运行时触发 NotImplementedError,无法直接进行 CPU baseline 对比。这是 torch_npu 的已知行为。
NPU 推理结果验证:
经网络搜索,目前 DeepGen-1.0 官方未发布公开的跨硬件(GPU vs NPU)精度对比基准数据。官方提供的评测主要集中在以下编辑能力基准上(详见 技术报告):
目前没有公开的 GPU vs NPU 精度对比数据。
根据 CogView4-6B 等同类图像生成模型在昇腾 NPU 上的适配经验(评测指标与官方基准差异控制在 1% 以内),以及 torch_npu 官方对 BF16 精度的验证,NPU 上的图像生成精度与 GPU 差异通常控制在 1% 以内。
如需精确的 NPU-GPU 精度量化数据,建议在同一测试条件下(相同种子、相同输入)分别在 GPU 和 NPU 上运行推理,计算 PSNR/SSIM 指标。
Target device: npu
Loading pipeline from: /tmp/DeepGen-cache/deepgenteam/DeepGen-1___0-diffusers
NPU available: True, device count: 2
Loading pipeline components...: 100%|██████████| 3/3 [00:04<00:00, 1.54s/it]
Loading VLM from /tmp/DeepGen-cache/deepgenteam/DeepGen-1___0-diffusers/vlm...
Loading checkpoint shards: 100%|██████████| 2/2 [00:00<00:00, 6.82it/s]
Loading tokenizer...
Loading connector...
All components loaded.
transformer: npu:0
vae: npu:0
Pipeline loaded.
Starting inference on npu (50 steps)...
100%|██████████| 50/50 [00:05<00:00, 9.63it/s]
Inference completed in 5.67s
Image saved at: /tmp/deepgen-gitcode/output/output_npu.png| 组件 | 设备 | 显存占用 |
|---|---|---|
| VLM (Qwen2.5-VL-3B-Instruct) | npu:0 | ~7GB |
| Connector (SCB) | npu:0 | ~1.7GB |
| DiT (SD3.5M-Kontext-2B) | npu:0 | ~4.7GB |
| VAE (AutoencoderKL) | npu:0 | ~160MB |
| 指标 | 数值 |
|---|---|
| 推理步数 | 50 |
| 推理耗时 | 5.67s |
| 模型加载时间 | ~15s |
| 输出图像 | 512x512, 384KB PNG |
| 吞吐量 | ~8.8 it/s(去噪阶段) |
sdpa 替代 flash_attention_2,后者不支持 NPUtorch_npu 注册了 NPU 专用的 npu_rms_norm 算子,CPU 后端不可用,仅支持 NPU 推理trust_remote_code=True,因为使用了自定义 pipeline 代码collect_env.py 的 owner mismatch 警告和 allow_internel_format 警告,不影响推理功能