JeffDing/DeepGen-1.0
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

DeepGen-1.0 昇腾NPU适配

1. 简介

本文档记录 DeepGen-1.0 在华为昇腾 Ascend 910 NPU 上的适配与验证结果。DeepGen-1.0 是 deepgenteam 推出的轻量级统一多模态图像生成与编辑模型(5B 参数:3B VLM + 2B DiT),基于 diffusers 框架,支持文生图、图像编辑、推理图像生成、推理图像编辑和文字渲染五大核心能力。

本次适配使用 diffusers 格式权重(DeepGen-1.0-diffusers),在单卡 NPU 上完成推理验证:

  • NPU:0 → VLM (Qwen2.5-VL-3B-Instruct, ~7GB) + Connector (~1.7GB) + DiT (SD3.5M-Kontext-2B, ~4.7GB) + VAE (~160MB)

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/deepgenteam/DeepGen-1.0-diffusers
  • 权重下载地址(ModelScope):https://modelscope.cn/models/deepgenteam/DeepGen-1.0-diffusers
  • 原始检查点(HuggingFace):https://huggingface.co/deepgenteam/DeepGen-1.0
  • 官方代码仓库:https://github.com/deepgenteam/deepgen
  • 技术报告:https://arxiv.org/abs/2602.12205
  • 项目主页:https://deepgenteam.github.io/

2. 验证环境

组件版本
torch2.9.0+cpu
torch_npu2.9.0.post1
diffusers0.38.0
transformers4.57.6
accelerate1.13.0
einops0.8.1
CANN8.5.1
  • NPU:Ascend 910,2 卡,64GB HBM/卡
  • 内存:229GB
  • 模型路径:/tmp/DeepGen-cache/deepgenteam/DeepGen-1___0-diffusers

3. NPU 推理指南

3.1 环境准备

pip install -r requirements.txt

requirements.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。

3.2 运行推理

文生图(Text-to-Image)

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 npu

图像编辑(Image Editing)

python 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

3.3 参数说明

参数默认值说明
--model_path(见脚本)DeepGen-1.0-diffusers 模型目录路径
--prompt(见脚本)生成/编辑提示词
--input_imageNone输入图片路径(编辑模式,留空则文生图)
--output_imageoutput_deepgen.png输出图片保存路径
--height512输出图像高度
--width512输出图像宽度
--seed42随机种子
--num_inference_steps50推理步数
--guidance_scale4.0CFG 缩放系数
--devicenpu推理设备:npu、cpu、auto

3.4 关键适配说明

  1. 注意力机制替换:将 flash_attention_2 替换为 sdpa(Scaled Dot-Product Attention),因为 flash_attn 不支持昇腾 NPU
  2. 模型加载:使用 trust_remote_code=True 加载 diffusers 格式权重,自动加载自定义 pipeline 代码
  3. 显存管理:模型总大小约 14GB(BF16),单卡 64GB HBM 完全容纳
  4. 不兼容组件:去除了 flash_attn、triton 等 NPU 不兼容依赖

4. 精度对比(NPU 一致性验证)

4.1 NPU 多次运行一致性

使用相同参数(seed=42, steps=50, cfg=4.0, 512x512)在 NPU 上进行两次独立推理,结果完全一致:

指标数值
MSE0.000000
最大像素差异0
平均像素差异0.0000
是否完全相同是
PSNR∞(完全一致)

结论:NPU 推理在相同种子下具有完美的确定性输出,两次运行产生逐位相同的图像。

4.2 NPU vs CPU 精度对比

由于 torch_npu 注册了 NPU 专用的 npu_rms_norm 算子,该算子不支持 CPU 后端,导致模型在 CPU 上运行时触发 NotImplementedError,无法直接进行 CPU baseline 对比。这是 torch_npu 的已知行为。

NPU 推理结果验证:

  • NPU 输出图像正常,内容语义正确("蓝色披萨和黄色棒球手套"的图像)
  • 输出图像尺寸 512x512,文件大小 384KB
  • 两次运行输出完全一致(确定性验证通过)

4.3 GPU 精度数据

经网络搜索,目前 DeepGen-1.0 官方未发布公开的跨硬件(GPU vs NPU)精度对比基准数据。官方提供的评测主要集中在以下编辑能力基准上(详见 技术报告):

  • 通用文生图(T2I)
  • 通用图像编辑(Image Editing)
  • 推理图像生成(Reasoning Generation)
  • 文字渲染(Text Rendering)

目前没有公开的 GPU vs NPU 精度对比数据。

根据 CogView4-6B 等同类图像生成模型在昇腾 NPU 上的适配经验(评测指标与官方基准差异控制在 1% 以内),以及 torch_npu 官方对 BF16 精度的验证,NPU 上的图像生成精度与 GPU 差异通常控制在 1% 以内。

如需精确的 NPU-GPU 精度量化数据,建议在同一测试条件下(相同种子、相同输入)分别在 GPU 和 NPU 上运行推理,计算 PSNR/SSIM 指标。

5. 推理输出证据

5.1 NPU 推理日志

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

5.2 设备分配验证

组件设备显存占用
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

5.3 推理性能

指标数值
推理步数50
推理耗时5.67s
模型加载时间~15s
输出图像512x512, 384KB PNG
吞吐量~8.8 it/s(去噪阶段)

6. 注意事项

  1. 显存要求:模型总大小约 14GB(BF16),需单张 16GB+ HBM 的 NPU 卡
  2. CANN 版本:需 CANN 8.5.0+,当前验证环境为 CANN 8.5.1
  3. 注意力机制:必须使用 sdpa 替代 flash_attention_2,后者不支持 NPU
  4. CPU 推理:由于 torch_npu 注册了 NPU 专用的 npu_rms_norm 算子,CPU 后端不可用,仅支持 NPU 推理
  5. diffusers 版本:需 diffusers 0.35.0+,当前验证环境为 0.38.0
  6. trust_remote_code:加载模型时必须设置 trust_remote_code=True,因为使用了自定义 pipeline 代码
  7. 警告信息:运行时可能出现 collect_env.py 的 owner mismatch 警告和 allow_internel_format 警告,不影响推理功能

7. 参考资源

  • DeepGen GitHub
  • DeepGen-1.0-diffusers HuggingFace
  • DeepGen 技术报告
  • torch_npu 文档
  • Diffusers 文档