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/步的性能不受影响)。
# 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 输入完全一致。
| 输出 | cos_sim | max_abs_err | mean_abs_err |
|---|---|---|---|
| eps_pred(UNet 噪声预测) | 0.99999997 | 1.490e-03 | 1.490e-04 |
| x0_latent(一步去噪潜变量) | 0.99999988 | 1.151e-03 | 1.333e-04 |
| text_emb(CLIP 文本嵌入) | 0.99999998 | 6.672e-03 | 1.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.