Dreamshaper-7 是基于 Stable Diffusion 1.5 架构的精调文生图模型,由 Lykon 在 Hugging Face 上发布,参数量约 7.36 亿。该模型在 SD 1.5 的基础上进行了精细调优,能够生成高质量、高细节的图片,特别擅长艺术风格和写实风格图像生成。
本适配项目将 Dreamshaper-7 部署到华为昇腾 NPU(Ascend910)上,使用 diffusers + torch_npu 框架进行推理,并通过 FastAPI 提供 OpenAI 兼容的 /v1/images/generations 接口服务。由于 Dreamshaper-7 是扩散模型(非 LLM 架构),vLLM-Ascend 不支持加载,因此采用 torch_npu 原生推理路径。
| 组件 | 版本 |
|---|---|
| 操作系统 | Linux (aarch64) |
| Python | 3.11.14 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| diffusers | 0.35.2 |
| transformers | 4.57.6 |
| vLLM | 0.18.0 |
| vLLM-Ascend | 0.18.0 |
| CANN | 8.5.1 |
| NPU | Ascend 910 (64GB HBM) |
| npu-smi | 25.5.5 |
# 从 GitCode 克隆(推荐)
git clone https://gitcode.com/hf_mirrors/Lykon/dreamshaper-7.git /path/to/dreamshaper-7
# 若 GitCode 拉取失败,从 ModelScope 拉取
# 若仍失败,从 Hugging Face 拉取
# git clone https://huggingface.co/Lykon/dreamshaper-7模型权重使用 Git LFS 存储,需下载实际 safetensors 文件:
# 进入模型目录
cd /path/to/dreamshaper-7
# 安装 git-lfs 后拉取
git lfs pullpip install diffusers transformers torch torch_npu fastapi uvicorn Pillow# 启动推理服务(默认端口 8300)
python3 inference.py --model-path /path/to/dreamshaper-7 --port 8300服务启动后会自动加载模型并监听指定端口,日志输出如下:
[startup] Loading dreamshaper-7 for NPU inference...
[load] Loading model from /path/to/dreamshaper-7
[load] NPU device: npu:0, total memory: 61.27GB
[load] Model loaded in 6.46s, NPU memory: 2.01GB
[startup] Starting server on port 8300...
INFO: Uvicorn running on http://0.0.0.0:8300服务启动后,可通过以下方式验证:
curl http://127.0.0.1:8300/health预期响应:
{"status":"ok","model":"dreamshaper-7","device":"npu:0"}curl -X POST http://127.0.0.1:8300/v1/images/generations \
-H "Content-Type: application/json" \
-d '{
"prompt": "a beautiful mountain landscape with sunset, highly detailed, digital art",
"negative_prompt": "blurry, low quality, distorted",
"num_inference_steps": 20,
"guidance_scale": 7.5,
"seed": 42,
"n": 1,
"size": "512x512"
}'预期响应包含 base64 编码的 PNG 图像数据和推理耗时信息。
| 指标 | 数值 |
|---|---|
| 模型加载时间 | 6.46s |
| 推理耗时(单图,20步) | 1.96s |
| 推理耗时(单图,25步) | 约 2.4s |
| NPU 显存占用(加载后) | 2.01 GB |
| NPU 显存占用(推理时) | 3.6 GB |
| NPU AICore 利用率 | 峰值约 0-27% |
| 输入图像分辨率 | 512x512 |
| 输出图像格式 | PNG (base64) |
| 服务框架 | FastAPI + uvicorn |
| 测试项 | 结果 |
|---|---|
| 同 seed 两次推理一致性 | 图像完全一致(seed 确定性) |
| 推理精度类型 | FP16 |
| 图像质量 | 与官方 SD 1.5 输出风格一致,细节丰富 |
| 色彩表现 | 正常,无偏色或伪影 |
| 构图完整性 | 符合 prompt 描述 |
variant="fp16"。--port 参数。dl_dreamshaper7_lfs.py 参考脚本。