超
gcw_g59KKkes/Lykon-dreamshaper-7
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Lykon/dreamshaper-7 · 昇腾 NPU 文生图

dreamshaper-7 是一个基于 Stable Diffusion 1.5 的文生图模型,可将文本提示词生成高质量图像。本仓库将其适配至 Ascend NPU(昇腾 910B)运行。

NPU 适配

  • 零 CUDA 代码:所有 .cuda() / torch.cuda 调用已移除,模型和所有张量运行在 npu:0 设备上
  • NPU 初始化异常捕获:失败时输出 JSON 错误信息后退出
  • 使用 torch.float16 半精度推理,开启 attention slicing 优化内存

双模式推理

CLI 模式

# 使用提示词文件
ASCEND_RT_VISIBLE_DEVICES=1 python3 inference.py --img examples/prompt.txt

# 使用 --prompt 参数
ASCEND_RT_VISIBLE_DEVICES=1 python3 inference.py --prompt "a cat wearing a hat, digital art"

FastAPI 服务模式

ASCEND_RT_VISIBLE_DEVICES=1 python3 inference.py --mode server --port 8782
# 测试
curl -s -F prompt="a cat" http://127.0.0.1:8782/predict

权重缓存

  • 自动下载到 model_weights/Lykon/dreamshaper-7/
  • 优先加载本地缓存,避免重复下载
  • 镜像源:https://hf-mirror.com/Lykon/dreamshaper-7/

输出 JSON 格式

{
  "model": "Lykon/dreamshaper-7",
  "device": "npu:0",
  "status": "success",
  "elapsed_ms": 12345.67,
  "prompt": "a majestic castle...",
  "image_path": "results/output_1234567890.png",
  "image_size": "512x512",
  "num_inference_steps": 25,
  "seed": 42
}

环境要求

  • Python 3.11
  • torch + torch_npu (Ascend CANN)
  • diffusers >= 0.37
  • transformers >= 4.57
  • Pillow, fastapi, uvicorn, python-multipart