DreamShaper 7 是 Lykon 发布的 Stable Diffusion 1.5 系列文生图模型,以高质量写实 与艺术风格著称,擅长风景、人像、概念艺术等题材,是社区广泛使用的 SD 1.5 微调模型。
本项目完成其在**华为昇腾 NPU(Ascend 910B)**上的推理适配与验证。
| 项目 | 说明 |
|---|---|
| 模型架构 | StableDiffusionPipeline(UNet2DConditionModel + AutoencoderKL + CLIPTextModel) |
| 基础模型 | Stable Diffusion 1.5 |
| 文本编码器 | CLIP Text(768 维) |
| 权重格式 | safetensors(FP16) |
| 支持任务 | 文本生成图片(text-to-image) |
| 默认输出 | 512×512 RGB 图片 |
| 项目 | 要求 |
|---|---|
| 硬件 | Ascend 910B 系列(≥1 卡) |
| CANN | ≥ 8.0(source /usr/local/Ascend/ascend-toolkit/set_env.sh) |
| Python | 3.10 – 3.11 |
| torch / torch_npu | 2.9.0 / 2.9.0.post1 |
| diffusers | ≥ 0.39.0 |
source /usr/local/Ascend/ascend-toolkit/set_env.sh
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install torch_npu==2.9.0.post1 diffusers transformers pillow safetensors \
-i https://repo.huaweicloud.com/repository/pypi/simple/权重通过 HuggingFace 镜像获取:
git clone --depth 1 https://hf-mirror.com/Lykon/dreamshaper-7 model_src权重目录为 diffusers 格式:unet/ / vae/ / text_encoder/ / tokenizer/ /
scheduler/ / safety_checker/ + model_index.json。
cd /workspace/dreamshaper-7
python3 inference.py --model_path ./model_src \
--prompt "a majestic mountain landscape at sunset, golden light, ultra detailed" \
--output output.png --device npu:0| 参数 | 说明 | 默认值 |
|---|---|---|
--model_path | 模型权重目录 | ./model_src |
--prompt | 文本提示词(必选) | - |
--negative_prompt | 负面提示词 | lowres, bad anatomy, ... |
--device | 推理设备(npu:0 / cpu) | npu:0 |
--steps | 采样步数 | 25 |
--guidance_scale | CFG 引导强度 | 7.5 |
--height/--width | 输出尺寸 | 512×512 |
--seed | 随机种子 | 42 |
--output | 输出图片路径 | output.png |
设备解析:NPU 优先,自动回退 CPU。
输入 prompt:a majestic mountain landscape at sunset, golden light, ultra detailed, 4k
mean=98.0 std=50.7 min=0 max=255(正常自然图像分布)StableDiffusionPipeline 为纯 PyTorch 架构,无 CUDA 特有 API,
直接 .to(npu) 迁移即可,无需算子替换。torch.no_grad()。resolve_device():NPU 可用性检测,失败自动回退 CPU。*.safetensors / *.bin 已加入 .gitignore,
仓库仅包含推理脚本、模型卡片与截图素材。.
├── README.md # 模型卡片(本文件)
├── inference.py # NPU 推理脚本
└── assets/ # 适配过程截图素材