模型名称: Lykon/dreamshaper-7
模型链接: HuggingFace | HF 镜像
模型描述: Dreamshaper-7 是基于 Stable Diffusion 1.5 微调的高质量文生图模型,擅长生成艺术风格的图像。由 Lykon 团队维护,广泛用于创意图像生成、概念艺术和设计辅助场景。
模型架构: Stable Diffusion 1.5 (CLIP Text Encoder + UNet2DConditionModel + AutoencoderKL),基于 Diffusers 框架
参数规模: 约 860M(fp32)/ 约 430M(fp16)
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch / torch_npu | 2.10.0 | 昇腾 NPU 运行时 |
| diffusers | >= 0.30.0 | HuggingFace Diffusers 库 |
| transformers | >= 4.30.0 | HuggingFace 库 |
| numpy | >= 1.26.0 | 数值计算 |
| fastapi | >= 0.100.0 | HTTP 服务框架 |
| uvicorn | >= 0.20.0 | ASGI 服务器 |
| Pillow | >= 9.0.0 | 图像处理库 |
| 昇腾驱动 | CANN 8.5.1 | 推荐最新版 |
安装命令:
# 华为镜像源
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
pip install -r requirements.txt# 加载 CANN 环境
source /usr/local/Ascend/ascend-toolkit/set_env.sh
# 检查 NPU 设备
npu-smi info# 通过 hf-mirror 国内镜像下载(fp16 版本)
mkdir -p /data2/drh/models/Lykon/dreamshaper-7
cd /data2/drh/models/Lykon/dreamshaper-7
# 下载配置文件
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/model_index.json"
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/text_encoder/config.json" -P text_encoder/
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/unet/config.json" -P unet/
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/vae/config.json" -P vae/
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/scheduler/scheduler_config.json" -P scheduler/
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/tokenizer/*" -P tokenizer/
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/feature_extractor/preprocessor_config.json" -P feature_extractor/
# 下载 fp16 权重文件
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/text_encoder/model.fp16.safetensors" -P text_encoder/
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/unet/diffusion_pytorch_model.fp16.safetensors" -P unet/
wget -nc "https://hf-mirror.com/Lykon/dreamshaper-7/resolve/main/vae/diffusion_pytorch_model.fp16.safetensors" -P vae/
# 创建符号链接(Diffusers 需要标准文件名)
cd /data2/drh/models/Lykon/dreamshaper-7
ln -sf model.fp16.safetensors text_encoder/model.safetensors
ln -sf diffusion_pytorch_model.fp16.safetensors unet/diffusion_pytorch_model.safetensors
ln -sf diffusion_pytorch_model.fp16.safetensors vae/diffusion_pytorch_model.safetensors# 单次推理
python3 inference.py --prompt "a cat sitting on a wooden table, highly detailed" --steps 15 --seed 42
# 自定义推理参数
python3 inference.py --prompt "一只猫坐在桌子上" --steps 20 --guidance-scale 7.5 --width 512 --height 512
# 启动 FastAPI 服务化推理
python3 service.py --host 0.0.0.0 --port 8080
# 端到端测试
python3 test_case.py --port 8081| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --prompt | str | "a beautiful landscape..." | 正向提示词(inference.py) |
| --negative-prompt | str | "blurry, low quality..." | 负向提示词 |
| --model-path | str | 自动检测 | 模型权重目录 |
| --device | str | npu:0 | 推理设备 |
| --steps | int | 25 | 推理步数 |
| --guidance-scale | float | 7.5 | CFG 引导系数 |
| --seed | int | 随机 | 随机种子 |
| --width | int | 512 | 图像宽度 |
| --height | int | 512 | 图像高度 |
提示词:
"a cat sitting on a wooden table, highly detailed, 8k"参数: 15步, 512×512, seed=42
输出:
【推理设备】: npu:0
【图像尺寸】: (512, 512)
【推理步数】: 15
【单次推理耗时】: 2.50 秒 (2498 ms)请求:
POST /generate
{
"prompt": "a beautiful landscape with mountains and a lake, highly detailed, 8k",
"steps": 15,
"seed": 42,
"width": 512,
"height": 512
}输出:
推理耗时: 2.44 秒 (2437 ms)
设备: npu:0
生成图像尺寸: 512x512输入:
prompt: "a cat sitting on a wooden table, highly detailed"
steps: 10输出:
推理耗时: 1.56 秒 (1564 ms)


| 指标 | 耗时 |
|---|---|
| Pipeline 加载 | 1536 ms |
| 单次推理(15步,512×512,含warmup) | 2.50 秒 (2498 ms) |
| 服务化推理(15步,512×512) | 2.44 秒 (2437 ms) |
| 简单推理(10步,512×512) | 1.56 秒 (1564 ms) |
| 接口 | 方法 | 说明 |
|---|---|---|
/health | GET | 健康检查,返回 NPU 状态信息 |
/generate | POST | 文生图接口,请求体 {"prompt": "...", "steps": 25, "seed": 42} |
/generate 请求参数:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| prompt | string | (必填) | 正向提示词 |
| negative_prompt | string | "" | 负向提示词 |
| steps | int | 25 | 推理步数 |
| guidance_scale | float | 7.5 | CFG 引导系数 |
| seed | int | -1 | 随机种子(-1 为自动) |
| width | int | 512 | 图像宽度 |
| height | int | 512 | 图像高度 |
/generate 返回字段:
| 字段 | 类型 | 说明 |
|---|---|---|
| image_base64 | string | Base64 编码的 PNG 图像 |
| latency_s | float | 推理耗时(秒) |
| latency_ms | float | 推理耗时(毫秒) |
| device | string | 推理设备 |
| model | string | 模型名称 |
| prompt | string | 使用的提示词 |
| seed | int | 实际使用的随机种子 |
dreamshaper-7-npu/
├── inference.py # NPU 推理脚本
├── service.py # FastAPI 服务
├── test_case.py # 端到端测试
├── requirements.txt # 依赖包列表
├── README.md # 本文档
├── adaptation_report.md # 适配报告
├── assets/
│ ├── agent_workflow.png # Agent适配全过程截图
│ ├── npu_device_call.png # NPU设备调用截图
│ ├── model_result.png # 模型适配结果截图
│ ├── npu_screenshot.txt # NPU 设备文本截图(原始)
│ ├── test_result.txt # 测试结果文本截图(原始)
│ └── adaptation_process.txt # 适配过程文本截图(原始)
└── results/ # 推理结果
├── dreamshaper_npu_output.png
├── dreamshaper_npu_result.json
├── test_generated.png
├── test_simple_generated.png
└── test_case_result.jsontorch_npu + transfer_to_npu 自动迁移调用 NPU,无需修改上游 Diffusers 代码