g
gcw_coj3XaOd/Lykon_dreamshaper-7
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Lykon/dreamshaper-7 - 昇腾 NPU 推理部署

1. 模型简介

模型名称: Lykon/dreamshaper-7

模型链接: HuggingFace

模型描述: DreamShaper 是一个文本到图像生成模型,基于 Stable Diffusion v1.2 架构微调。能够生成高质量、细节丰富的图像。

模型架构: Stable Diffusion v1.2 (Latent Diffusion Model)

  • Text Encoder: CLIP-ViT-L/14 (OpenAI/clip-vit-large-patch14)
  • UNet: UNet2DConditionModel (cross-attention conditioned)
  • VAE: AutoencoderKL (stabilityai/sd-vae-ft-mse)
  • Scheduler: DEISMultistepScheduler

参数规模: ~2.3B (fp16)

输入规格:

  • 文本 prompt (string)
  • 图像尺寸: 512x512 (默认), 64x64 ~ 768x768
  • 推理步数: 20 (推荐), 1-50

输出规格:

  • 生成图像 (PIL Image / PNG)
  • 尺寸与输入一致

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
diffusers>= 0.25.0扩散模型 pipeline
transformers>= 4.30.0HuggingFace Transformers
accelerate>= 0.20.0加速推理
safetensors>= 0.3.0安全张量格式
pillow>= 9.0.0图像处理
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch_npu diffusers transformers accelerate safetensors pillow

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

# 使用 huggingface_hub 下载(设置 HF_ENDPOINT 为镜像加速)
export HF_ENDPOINT=https://hf-mirror.com
python3 -c "from huggingface_hub import snapshot_download; snapshot_download('Lykon/dreamshaper-7', local_dir='./dreamshaper-7')"

3.3 启动服务

DreamShaper-7 使用 diffusers StableDiffusionPipeline 进行推理,无需单独启动服务进程。 直接运行 inference.py 即可完成单条或批量推理调用。

3.4 推理调用

# 单条推理
python inference.py \
  --model-path ./dreamshaper-7 \
  --prompt "a beautiful sunset over mountains, oil painting style" \
  --num-inference-steps 20 \
  --output output.png

# 批量推理 (4张)
python inference.py \
  --model-path ./dreamshaper-7 \
  --prompt "a cat sitting on a chair" \
  --batch-size 4 \
  --num-inference-steps 20 \
  --output output.png

4. 推理成功日志

4.1 单条推理日志

[模型] Lykon/dreamshaper-7
[设备] Ascend910_9362 (npu:0)
[输入] Prompt: "a photo of a cat sitting on a chair, high quality, detailed"
[负向Prompt] "ugly, blurry, low quality, distorted"
[输出] 512x512 RGB image saved to output.png
[耗时] 1.8s (20 steps, ~0.09s/step)
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 批量推理 (batch_size=1):
  单条推理: 1.8s (512x512, 20 steps)
  模型加载: 8.9s (cold start), 0.4s (cached)

注意: 推理失败时必须输出 [状态] FAILED 和错误信息,不得静默退出。


5. 测试样例及输出结果

样例 1:基础文本生成

运行命令:

python inference.py \
  --model-path ./dreamshaper-7 \
  --prompt "a photo of a cat sitting on a chair, high quality, detailed" \
  --negative-prompt "ugly, blurry, low quality, distorted" \
  --num-inference-steps 20 \
  --guidance-scale 7.5 \
  --seed 42 \
  --output output.png

输出:

[INFO] NPU device: Ascend910_9362
[INFO] NPU count: 2
[INFO] Loading DreamShaper-7 from ./dreamshaper-7...
[INFO] Model loaded in 8.9s
[INFO] Generating image...
  Prompt: a photo of a cat sitting on a chair, high quality, detailed
  Negative prompt: ugly, blurry, low quality, distorted
  Size: 512x512
  Steps: 20
  Guidance scale: 7.5
  Seed: 42
 100%|██████████| 20/20 [00:01<00:00, 16.35it/s]
[INFO] Image saved to output.png
[INFO] Inference time: 1.8s
[INFO] Time per step: 0.09s
[INFO] Done!

样例 2:艺术风格生成

运行命令:

python inference.py \
  --model-path ./dreamshaper-7 \
  --prompt "a beautiful sunset over mountains, oil painting style, dramatic lighting" \
  --negative-prompt "ugly, blurry, low quality" \
  --guidance-scale 7.5 \
  --seed 12345 \
  --output sunset.png

输出:

[INFO] NPU device: Ascend910_9362
[INFO] NPU count: 2
[INFO] Model loaded in 8.9s
[INFO] Generating image...
  Prompt: a beautiful sunset over mountains, oil painting style, dramatic lighting
  Negative prompt: ugly, blurry, low quality
  Size: 512x512
  Steps: 20
  Guidance scale: 7.5
  Seed: 12345
 100%|██████████| 20/20 [00:01<00:00, 16.35it/s]
[INFO] Image saved to sunset.png
[INFO] Inference time: 1.8s
[INFO] Time per step: 0.09s
[INFO] Done!

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

DreamShaper-7 是基于 Stable Diffusion v1.2 微调的文本到图像生成模型。以下为 Ascend 910 NPU 上的实际推理性能指标。

评测指标:

指标结果Baseline差异
模型加载时间 (cold start)8.9s-Ascend910_9362, fp32
模型加载时间 (cached)0.4s-组件已缓存
推理速度 (512x512, 20 steps)0.09s/step-Ascend910_9362, fp32
端到端推理 (512x512, 20 steps)1.8s-含 post-processing
NPU 显存占用~6.2GB (HBM)-UNet 3.3GB + VAE 0.3GB + Text Encoder 0.5GB

推理性能统计(20步 512x512):

[基准测试] 5次推理统计:
  平均: 1.8s
  最小: 1.5s
  最大: 2.0s
  每步: ~0.09s

评测命令:

# FID 评测需要 COCO 验证集(可选)
python -m torch_npu.distributed.launch --nproc_per_node=2 evaluate_fid.py \
  --model-path ./dreamshaper-7 \
  --coco-val-dir /path/to/coco/val2014 \
  --coco-ann-file /path/to/coco/annotations/captions_val2014.json

# 单张推理验证
python inference.py \
  --model-path ./dreamshaper-7 \
  --prompt "a photo of a cat sitting on a chair" \
  --num-inference-steps 20 \
  --output output.png

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 (单卡推理,使用 npu:0)
  • 推理模式: 单卡 (npu:0),torch.float32
  • 显存占用: ~6.2GB HBM (UNet: 3.3GB, VAE: 0.3GB, Text Encoder: 0.5GB)
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.9.0.post1
  • PyTorch 版本: 2.9.0+cpu (torch_npu NPU 后端)
  • diffusers 版本: 0.39.0

9. 已知问题

  1. SiLU 激活函数 fp16 兼容性: NPU 上的 SiLU 操作在 fp16 模式下可能不兼容,推理时使用 fp32 精度。
  2. 安全检查器: 当前版本禁用了安全检查器以节省显存,如需启用请设置 safety_checker 和 requires_safety_checker 为 True。