模型名称: stabilityai/sd-turbo
模型链接: HuggingFace
模型描述: SD-Turbo 是 Stability AI 推出的基于 Adversarial Diffusion Distillation (ADD) 的超快文本到图像生成模型。它只需 1-4 步推理即可生成高质量图像,相比标准 SDXL 的 50 步推理速度提升超过 10 倍。
模型架构: SDXL-based (Stable Diffusion XL Turbo)
参数规模: ~1.29B (UNet 866M + VAE 84M + Text Encoder 340M)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 (测试: 2.9.0+cpu) |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 (测试: 2.9.0.post1) |
| diffusers | >= 0.24.0 | 推荐 0.39.0 |
| transformers | >= 4.25.0 | CLIP tokenizer |
| numpy | >= 1.20.0 | 数组操作 |
| 昇腾驱动 | CANN 8.0+ | 推荐 CANN 8.5.1 |
安装命令:
pip install torch torch_npu diffusers transformers numpy accelerateNPU 设备要求:
# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"注意: 当前部署使用真实架构配置 + 随机初始化权重。预训练权重(4个文件,约7.3GB)因网络限制无法下载。
# 架构配置文件(从 HuggingFace 缓存加载)
export SNAPSHOT=/opt/atomgit/.cache/huggingface/hub/models--stabilityai--sd-turbo/snapshots/b261bac6fd2cf515557d5d0707481eafa0485ec2# 直接运行 inference.py
python3 inference.py
# 或使用以下命令指定不同的 prompt
python3 -c "
import sys; sys.path.insert(0, '/usr/local/python3.11.14/lib/python3.11/site-packages')
from sd_turbo_inference import *
prompt = 'a photo of an astronaut riding a horse on mars, highly detailed, 4k'
# ... (见 inference.py 完整代码)
"| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| prompt | str | 必填 | 文本描述 |
| num_inference_steps | int | 1 | SD-Turbo 推荐 1-4 步 |
| guidance_scale | float | 7.5 | CFG 引导强度 |
| height/width | int | 512 | 输出图像尺寸 |
| seed | int | None | 随机种子 |
[模型] stabilityai/sd-turbo
[设备] Ascend910_9362 (npu:0)
[输入] prompt="a photo of an astronaut riding a horse on mars", latents=[1,4,64,64]
[输出] image=[1,3,512,512]
[UNet耗时] 0.16s (1 step, CFG=7.5)
[NPU峰值内存] 3096.8 MB
[状态] SUCCESS当前版本支持 batch=1 推理。batch>1 需要修改 latent 初始化部分。
运行命令:
python3 inference.py输出:
[PATCH] Applied cross-attention batch_size fix
======================================================================
SD-Turbo Full Pipeline Inference on Ascend NPU (dummy weights)
======================================================================
[ENV] torch.__version__ = 2.9.0+cpu
[ENV] NPU count = 2
[ENV] NPU name = Ascend910_9362
[1/6] Loading Tokenizer ... vocab_size=49408
[2/6] Loading Text Encoder (CLIP) ... Params: 340,387,840
[3/6] Loading UNet2DConditionModel ... Params: 865,910,724
[4/6] Loading VAE (AutoencoderKL) ... scaling_factor=0.18215
[5/6] Loading Scheduler ... prediction_type=epsilon
[6/6] Running Pipeline Inference ...
Prompt: a photo of an astronaut riding a horse on mars, highly detailed, 4k
Step 1: latents mean=1.1729 std=5.3359
UNet denoising: 0.16s
Decoded image: torch.Size([1, 3, 512, 512])
Output saved: /tmp/sd_turbo_generated_image.pt (3.0 MB)
[NPU] Peak memory: 3096.8 MB
[NPU] Total pipeline memory footprint: 3096.8 MB
======================================================================
INFERENCE PASSED
Total params: 1,289,952,427
Inference time: 0.16s (UNet only)
NPU peak memory: 3096.8 MB
CFG scale: 7.5, Steps: 1
All model components ran on NPU (npu:0)
======================================================================


测试数据: 使用 dummy weights(随机初始化),真实精度评测需要加载预训练权重。
适配验证指标:
| 指标 | 结果 | 说明 |
|---|---|---|
| 算子兼容性 | 7/7 PASS | conv2d, linear, group_norm, silu, sdp_attention, interpolate, layer_norm |
| UNet 前向传播 | PASS | 1 step denoising on NPU |
| VAE 编解码 | PASS | 64x64 -> 512x512 |
| 端到端推理 | PASS | Tokenizer → Text Encoder → UNet → VAE |
| NPU 内存 | 3096.8 MB | 峰值 (~3GB / 64GB HBM) |
| 推理耗时 | 0.16s | UNet denoising only (1 step) |
已知兼容性修复:
torch_npu.npu_geglu 不可用,使用手动 chunk+gelu