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)本仓已完成在 Ascend 910B (npu:0) 上的真实推理适配,dtype=fp16,全组件运行在 NPU。
workspace/dreamshaper-7_model (通过 hf-mirror 同步) + NPU真实推理torch.npu.is_available()=True, npu-smi info OK, NPU memory 62740 MB totalpip 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的导入问题 (patchautoencoder_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。
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 异步执行完成。
本次真实 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, 本地生成, 示例见仓库)
图像为固定 seed=33 的可复现结果,NPU 与 CPU 不要求逐像素相等,以上为 NPU 真实单样本 smoke consistency 验证,非完整数据集评测。
本模型为生成式扩散模型,采用分层验证 + 单样本 sanity 策略 (符合 diffusion-generation.md 要求):
| 阶段 | 检查项 | NPU 结果 | 判定 |
|---|---|---|---|
| 1 | 文本编码器 shape/有限值 | (1,77,768) finite True | PASS |
| 2 | 去噪 latent shape | (1,4,64,64) 预期 | PASS (通过端到端隐式验证) |
| 3 | VAE 解码范围/有限值 | [0,255] finite True | PASS |
| 4 | 端到端图像统计 | mean 63.96 std 57.03 min 0 max 255 | PASS |
npu:0 (fp16)测试条件: 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.921s | 0.226s |
| Min / Max | 0.921s / 0.922s | 0.226s / 0.226s |
| p50 / p90 / p95 | 0.921s / 0.922s / 0.922s | 0.226s / 0.226s / 0.226s |
| Steps/s | 27.14 | 22.14 |
| Seconds/image | 0.921 | 0.226 |
| NPU 峰值显存占用 | ~3694 MB (used) / 62740 MB total | ~3694 MB |
| Batch | 1 | 1 |
| dtype | fp16 | fp16 |
预热后稳定推理数据来自
python inference.py --steps 25 --benchmark-runs 3的 3 次正式测试; 完整日志见assets/agent_workflow.png与assets/model_result.png。
assets/agent_workflow.png: 完整工作流 (下载/侦察/加载/NPU推理/验证/性能)assets/npu_device_call.png: npu-smi info + torch.npu.is_available() + 设备名 + 模型参数 device/dtypeassets/model_result.png: 默认 python inference.py 真实输出 + PASS/FAIL三张图均由
scripts/render_xterm_evidence.mjs --style raw从本次真实日志渲染,含命令、退出码、UTC 时间、源日志 SHA-256。
npu:0 fp16 路径; bf16 未测试-- 参数替换safety_checker=None) 以保证开源验证; 生产环境请启用diffusers/models/autoencoders/autoencoder_rae.py 的 Dinov2WithRegisters 导入 (不影响 SD 功能)Hardware: NPU NPU Ascend Ascend910 Ascend910B StableDiffusion Text-to-Image Diffusion DreamShaper