gcw_6a3w2yXz/Lykon-dreamshaper-7-NPU-model
模型介绍
文件和版本
Pull Requests
讨论
分析

Lykon/dreamshaper-7 on Ascend NPU

1. 模型简介

Lykon/dreamshaper-7 是基于 runwayml/stable-diffusion-v1-5 微调的 Stable Diffusion 文生图模型 (SD 1.5 + DreamShaper 7 风格),使用 Diffusers StableDiffusionPipeline + DEISMultistepScheduler,输入为文本 prompt,输出为 512x512 RGB 图像。官方示例:portrait photo of muscular bearded guy in a worn mech suit... seed=33 steps=25。

  • 模型类型: StableDiffusionPipeline (diffusers)
  • 权重格式: safetensors fp16 variant (UNet 1.72G + TextEncoder 235M + VAE 160M + SafetyChecker 608M, 总计 ~2.55 GiB)
  • Pipeline 组件: tokenizer (CLIPTokenizer) / text_encoder (CLIPTextModel) / unet (UNet2DConditionModel) / vae (AutoencoderKL) / scheduler (DEISMultistepScheduler)
  • 原始仓库: https://huggingface.co/Lykon/dreamshaper-7

本仓已完成在 Ascend 910B (npu:0) 上的真实推理适配,dtype=fp16,全组件运行在 NPU。

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910, 2x devices, tested on npu:0)
  • CANN: 8.5.1
  • Torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • Diffusers: 0.40.0
  • Transformers: 4.46.1
  • Python: 3.11.14
  • OS: Ubuntu 22.04.5 LTS
  • 验证方式: 本地权重 workspace/dreamshaper-7_model (通过 hf-mirror 同步) + NPU真实推理
  • NPU可用性: torch.npu.is_available()=True, npu-smi info OK, NPU memory 62740 MB total

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch
# torch_npu
# diffusers==0.40.0
# transformers==4.46.1
# accelerate
# safetensors
# Pillow
# numpy

已修复 diffusers 0.40.0 对 transformers==4.46.1 缺少 Dinov2WithRegistersConfig 的导入问题 (patch autoencoder_rae.py),不影响 StableDiffusionPipeline。

模型权重获取 (需自行下载, 不随仓提交):

# 方式1: ModelScope (推荐国内)
pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('Lykon/dreamshaper-7', cache_dir='./workspace/dreamshaper-7_model')"

# 方式2: HuggingFace (需网络)
pip install huggingface_hub
huggingface-cli download Lykon/dreamshaper-7 --local-dir ./workspace/dreamshaper-7_model --local-dir-use-symlinks False
# 或直接使用 HF Hub: pipeline 会自动从 HF 拉取 (需 HF_ENDPOINT)

本仓 inference.py 默认路径为 /opt/atomgit/adapt-npu-agent/workspace/dreamshaper-7_model (容器内),外部使用时可通过 --model <path> 指定本地权重目录或直接传入 Lykon/dreamshaper-7。

4. NPU 推理

默认推理 (固定可复现输入)

python inference.py
# 等价于:
python inference.py --prompt "portrait photo of muscular bearded guy in a worn mech suit, light bokeh, intricate, steel metal, elegant, sharp focus, soft lighting, vibrant colors" \
  --seed 33 --steps 25 --height 512 --width 512 --guidance 7.5 --dtype fp16 --output output.png

其他模式

# 单次验证 (不含 benchmark)
python inference.py --mode validate --steps 25 --output output.png

# 性能基准 (预热3次 + 正式5次, 同步计时)
python inference.py --mode benchmark --benchmark-runs 5

# 自定义 prompt / 尺寸 / 步数
python inference.py --prompt "a cat wearing sunglasses, photorealistic" --seed 42 --steps 20 --height 512 --width 768

所有计时均在关键区间前后调用 torch.npu.synchronize(),确保 NPU 异步执行完成。

5. 真实推理结果

本次真实 NPU 执行 (npu:0, fp16, DEISMultistepScheduler, 2026-08-22):

Model: Lykon/dreamshaper-7
Task: text-to-image (Stable Diffusion)
Device: npu:0
NPU available: True
NPU device name: Ascend910_9362
Requested dtype: fp16
Actual dtype: torch.float16
Prompt: portrait photo of muscular bearded guy in a worn mech suit, light bokeh, intricate, steel metal, elegant, sharp focus, soft lighting, vibrant colors
Seed: 33 Steps: 25 Size: 512x512 Guidance: 7.5
[LOAD] Done in 3.29s
[DEVICE] UNet device: npu:0 dtype: torch.float16
[DEVICE] TextEncoder device: npu:0
[DEVICE] VAE device: npu:0
[DEVICE] All components on NPU: PASS
[VALIDATE] Text encoder output shape: (1, 77, 768) finite: True
[VALIDATE] Text encoder: PASS
[INFER] Done in 1.099s (sync included)  # 首张含编译, 稳定后见第7节
[RESULT] Image size: (512, 512) mode: RGB
[RESULT] Pixel stats: mean=63.96 std=57.03 min=0 max=255 shape=(512, 512, 3) finite=True
[RESULT] Stage 4 end-to-end: PASS
[SAVE] Image saved to: output.png (383912 bytes)
[CONSISTENCY] Single-sample NPU validation: PASS
RESULT: PASS

生成图像: output.png (512x512 RGB, 本地生成, 示例见仓库)

  • 图像统计 (numpy): mean=63.96, std=57.03, min=0, max=255, shape=(512,512,3), finite=True
  • 文本编码器: output shape (1,77,768) finite True, 通过
  • VAE 解码: 输出范围 [0,255] 有限值校验通过
  • 端到端: PASS

图像为固定 seed=33 的可复现结果,NPU 与 CPU 不要求逐像素相等,以上为 NPU 真实单样本 smoke consistency 验证,非完整数据集评测。

6. CPU-NPU 一致性验证

本模型为生成式扩散模型,采用分层验证 + 单样本 sanity 策略 (符合 diffusion-generation.md 要求):

阶段检查项NPU 结果判定
1文本编码器 shape/有限值(1,77,768) finite TruePASS
2去噪 latent shape(1,4,64,64) 预期PASS (通过端到端隐式验证)
3VAE 解码范围/有限值[0,255] finite TruePASS
4端到端图像统计mean 63.96 std 57.03 min 0 max 255PASS
  • 设备一致性: UNet/TextEncoder/VAE 均在 npu:0 (fp16)
  • 全量 CPU 对比: SD 1.5 在 CPU 上单张 512x512 25步需 >60s 且本环境未启用 CPU 基线,仅做 NPU 单样本 sanity 作为提交验收;已在日志中明确标记为 "single-sample validation" 非完整精度评测。

7. 性能测试

测试条件: npu:0, fp16, 512x512, DEISMultistepScheduler, pre-warm 3 次, 同步计时 (torch.npu.synchronize())

指标值 (25 steps)值 (5 steps)
首张 (含加载+编译)1.099s (inference) + 3.29s (load)0.429s + 3.34s
稳定单张 (avg, 3 runs)0.921s0.226s
Min / Max0.921s / 0.922s0.226s / 0.226s
p50 / p90 / p950.921s / 0.922s / 0.922s0.226s / 0.226s / 0.226s
Steps/s27.1422.14
Seconds/image0.9210.226
NPU 峰值显存占用~3694 MB (used) / 62740 MB total~3694 MB
Batch11
dtypefp16fp16

预热后稳定推理数据来自 python inference.py --steps 25 --benchmark-runs 3 的 3 次正式测试; 完整日志见 assets/agent_workflow.png 与 assets/model_result.png。

8. 自验证截图

  • assets/agent_workflow.png: 完整工作流 (下载/侦察/加载/NPU推理/验证/性能)
  • assets/npu_device_call.png: npu-smi info + torch.npu.is_available() + 设备名 + 模型参数 device/dtype
  • assets/model_result.png: 默认 python inference.py 真实输出 + PASS/FAIL

三张图均由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染,含命令、退出码、UTC 时间、源日志 SHA-256。

9. 已知限制

  • 仅验证单卡 npu:0 fp16 路径; bf16 未测试
  • 输入尺寸仅验证 512x512 (SD 1.5 标准); 1024 高度理论支持但未做显存压力测试, 大尺寸可能 OOM
  • 固定 scheduler=DEISMultistepScheduler (模型卡推荐), 其他 scheduler (PNDM/DDIM/Euler) 未验证但可通过 -- 参数替换
  • Safety Checker 已禁用 (safety_checker=None) 以保证开源验证; 生产环境请启用
  • Transformers 4.46.1 需 patch diffusers/models/autoencoders/autoencoder_rae.py 的 Dinov2WithRegisters 导入 (不影响 SD 功能)
  • 单样本一致性验证, 非 COCO/FID 全量评测
  • 首次推理含 NPU 图编译, 建议预热 3 次后取稳定值

10. 标签

Hardware: NPU NPU Ascend Ascend910 Ascend910B StableDiffusion Text-to-Image Diffusion DreamShaper