w
weasonlee/dreamshaper-7
模型介绍
文件和版本
Pull Requests
讨论
分析

DreamShaper-7 (SD1.5) — Ascend NPU Adaptation

模型简介

DreamShaper-7 是 Lykon 基于 Stable Diffusion 1.5 微调的文生图检查点(diffusers pipeline 布局:unet + vae + text_encoder + tokenizer + scheduler),以提示词跟随能力强、风格化出图质量高著称。总参数量约 983M(UNet 860M + CLIP 文本编码器)。

原始模型:Lykon/dreamshaper-7(OpenRAIL++ 许可)

适配方案(更稳的确定性对比路径):完整多步采样在双端浮点误差会逐步放大,本仓库采用 UNet 单步 denoise 对比:固定 prompt 经 CLIP 文本编码器得到 (1,77,768) 嵌入,固定种子(42)的 (1,4,64,64) 潜变量在 t=500 加噪后过一次 UNet,对比 epsilon 预测、一步重建潜变量 x0 与文本嵌入三方 cos_sim——输入完全一致、结果可复现。另提供 --generate 选项跑 10 步 256×256 端到端生成存图供目视检查。

昇腾环境注意:本容器的 torch_npu 会自动给 diffusers 的 GEGLU 打 npu_geglu 融合算子补丁,导致 CPU 张量报错。inference.py 中 restore_native_geglu() 恢复了纯 torch 实现,对 CPU/NPU 双端均安全(NPU 上 56ms/步的性能不受影响)。

环境要求

  • 昇腾 Ascend 910(本仓库在 910 ×2 上验证,单卡即可推理)
  • Python 3.11
  • torch 2.9.0 + torch-npu 2.9.0.post1
  • diffusers ≥ 0.32 + transformers 4.57.6

部署步骤

# 1. 克隆仓库
git clone https://gitcode.com/weasonlee/dreamshaper-7.git
cd dreamshaper-7

# 2. 安装依赖
pip install -r requirements.txt

# 3. 下载权重到 models/(已 .gitignore 排除;fp32 全量约 5GB)
mkdir -p models
HF_ENDPOINT=https://hf-mirror.com HF_HUB_DOWNLOAD_TIMEOUT=120 python -c "
from huggingface_hub import snapshot_download
snapshot_download('Lykon/dreamshaper-7', local_dir='models',
                  ignore_patterns=['*safety_checker*'])"

# 4. 推理(NPU,UNet 单步对比路径)
python inference.py --device npu
# 或 CPU
python inference.py --device cpu
# 附加 10 步 256x256 端到端生成(存 assets/gen_<device>.png)
python inference.py --device npu --generate

推理示例

$ python inference.py --device npu
torch.npu.is_available() = True
torch.npu.device_count() = 2
[info] device = npu:0, torch = 2.9.0+cpu
[load] SD1.5 components: unet 860M + text_encoder -> 983M params total
[info] prompt = 'a photo of an astronaut riding a horse on mars, hi...'
[info] latents = (1, 4, 64, 64), t = 500 (alpha_bar=0.2763)
[time] run 0: 56 ms
[time] avg: 56 ms (UNet 64x64 single step)
[out ] eps_pred: mean=0.0043 std=0.9590
[out ] x0_latent: mean=0.0105 std=0.3953

===== CPU vs NPU comparison (UNet single step, t=500) =====
  eps_pred   cos_sim=0.99999997 max_abs_err=1.490e-03 mean_abs_err=1.490e-04
  x0_latent  cos_sim=0.99999988 max_abs_err=1.151e-03 mean_abs_err=1.333e-04
  text_emb   cos_sim=0.99999998 max_abs_err=6.672e-03 mean_abs_err=1.352e-04

输入:固定 prompt("a photo of an astronaut riding a horse on mars, highly detailed")+ 固定种子(42)潜变量 + 固定时间步 t=500;CPU 与 NPU 输入完全一致。

CPU vs NPU 精度对比

输出cos_simmax_abs_errmean_abs_err
eps_pred(UNet 噪声预测)0.999999971.490e-031.490e-04
x0_latent(一步去噪潜变量)0.999999881.151e-031.333e-04
text_emb(CLIP 文本嵌入)0.999999986.672e-031.352e-04
单步耗时(含同步,3 次均值)NPU ~56 ms vs CPU ~16655 ms(~297× 加速)——

结论:NPU(Ascend 910, torch-npu 2.9.0.post1)与 CPU 的 UNet 单步输出一致性达 0.9999999(fp32 正常差异水平),文本编码器与去噪路径双端一致,适配成功。

仓库结构

├── inference.py        # 推理脚本(UNet 单步对比 + 可选端到端生成)
├── readme.md
├── requirements.txt
├── models/             # 权重目录(需自行下载,已 gitignore)
├── assets/             # 运行截图(--generate 另存 gen_<device>.png)
│   ├── agent_workflow.png
│   ├── npu_device_call.png
│   └── model_result.png
└── results/            # 每次运行的 latent/嵌入/耗时产物(gitignore)

许可与引用

权重遵循 OpenRAIL++(CreativeML Open RAIL++-M)许可。

Rombach R. et al. "High-Resolution Image Synthesis with Latent Diffusion Models." CVPR 2022.