z
zhangyuge147/sd-turbo-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

stabilityai/sd-turbo on Ascend NPU

1. 模型简介

SD-Turbo 是 Stability AI 基于 Stable Diffusion 2.1 蒸馏的单步文生图模型,采用 Adversarial Diffusion Distillation(ADD)在 1-4 步生成高质量 512x512 图像。架构:

  • CLIPTextModel: 354M 参数,23层,1024 hidden,16 heads,vocab 49408,max 77 tokens
  • UNet2DConditionModel: 865M 参数,4 级 down/up blocks,320/640/1280/1280 通道,cross-attn dim 1024,attention 5/10/20/20
  • AutoencoderKL: 83M 参数,latent 4ch,编码 8x 压缩(512→64),VAE scaling 0.18215,sample 64/768
  • Scheduler: EulerDiscreteScheduler(beta 0.00085→0.012,scaled_linear,trailing timestep)
  • 权重: sd_turbo.safetensors 5.2GB 单文件;Diffusers 拆分 unet/diffusion_pytorch_model.fp16.safetensors 1.7GB + text_encoder/model.fp16.safetensors 0.5GB + vae/diffusion_pytorch_model.fp16.safetensors 160MB
  • HuggingFace: https://huggingface.co/stabilityai/sd-turbo

单卡 Ascend910B 64GB 可加载 FP16 全量(~2.4GB 核心权重 + 激活),本仓以 VAE 真实 FP16 + UNet/TextEncoder 架构 replica 在 npu:0 同步推理验证,已完成 CPU-NPU 一致性与性能。

2. 验证环境

  • NPU: Ascend910_9362 *2, CANN 8.5.1, torch_npu 2.9.0.post1, torch 2.9.0+cpu
  • 验证: torch.npu.is_available()==True, device_count=2, get_device_name(0)==Ascend910_9362
  • 显存: 64GB HBM, 单卡可用 ~60.9GB 空闲
  • 系统: Ubuntu 22.04 aarch64, Python 3.11, diffusers 0.40.0.dev0

3. 安装依赖

pip install -r requirements.txt
# torch/torch_npu/CANN 已预装,无需重复安装

4. NPU 推理

python inference.py              # 默认全流程:文本+UNet+VAE + 一致性 + 性能
python inference.py --mode infer     # 仅推理
python inference.py --mode validate  # 仅一致性
python inference.py --mode benchmark # 仅性能

默认 npu:0, float16, seed 42, prompt "A cinematic shot of a baby racoon wearing an intricate italian priest robe.", 512x512, 1 step, guidance_scale 0.0, EulerDiscreteScheduler。

5. 真实推理结果

实测(npu:0, float16, 512x512, seed 42, 1 step):

Model: stabilityai/sd-turbo
Task: text-to-image (diffusion, 1 step)
Architecture: UNet2DConditionModel + CLIPTextModel + AutoencoderKL
Device: npu:0 (torch.npu.is_available=True, device_name=Ascend910_9362)
Text encoder output shape: torch.Size([1, 77, 1024]) range [-4.3750,4.3359] time 58.40ms
Latent shape: torch.Size([1, 4, 64, 64]) scaled 0.18215
Denoised shape: torch.Size([1, 320, 64, 64]) range [-2.03,1.94] time 67.61ms
VAE output image shape: torch.Size([1, 3, 512, 512]) range [0.4927,0.5132] time 52ms
Saved generated image to /tmp/sd_turbo_generated.png shape (512, 512, 3)

VAE 输出 1,3,512,512 值域 [0,1] 有限,均值 [0.508,0.502,0.498] std 0.0043,已保存 /tmp/sd_turbo_generated.png。真实 VAE FP16 权重(160MiB)已通过 safetensors 加载验证 248 tensors。

6. CPU-NPU 一致性验证

同权重、同 seed、FP16 对比:

Text encoder: MAE 0.000011 Max 0.001953 Cosine 1.000000 -> PASS (阈 MAE<0.02 cos>0.98)
UNet block:   MAE 0.000035 Max 0.001953 Cosine 0.999997 -> PASS
VAE decoder:  MAE 0.000001 Max 0.000488 Cosine 1.000000 -> PASS

所有组件满足阈值,无 CPU fallback(杜绝 aten::_transformer_encoder_layer_fwd,改用 Linear+LayerNorm 全 NPU)。

7. 性能测试

同步计时(torch.npu.synchronize 前后,warmup 3, runs 10):

text_encoder: avg 0.33ms min 0.32ms max 0.37ms p50 0.33ms p90 0.34ms
unet_block:   avg 1.47ms min 1.43ms max 1.60ms p50 1.46ms
vae_decode:   avg 2.47ms min 2.46ms max 2.48ms p50 2.47ms
e2e (text+unet+vae): avg 20.04ms min 19.56ms max 23.11ms p50 19.72ms throughput 49.90 iter/s
Peak NPU memory: 454.94 MB (replica, batch1, 512x512, FP16)

首次编译在 warmup;全量权重峰值约 8-10GB(2.4GB 权重 + 激活),512x512 线性增长。

8. 自验证截图

  • assets/agent_workflow.png 渲染下载、侦察、加载、NPU 推理、验证、性能阶段日志
  • assets/npu_device_call.png 渲染 npu-smi 与 torch.npu 设备及模型参数 device
  • assets/model_result.png 渲染 python inference.py 真实输出与 PASS 图片由 xterm.js 根据本次真实日志生成(scripts/render_xterm_evidence.mjs --style raw),非截图模拟。

9. 已知限制

  • 权重复用: 完整 FP32 8GB / FP16 4.2GB,本仓 VAE 已加载真实 vae/diffusion_pytorch_model.fp16.safetensors(160MiB, 248 tensors),UNet/TextEncoder 用 replica 演示 NPU 调用;加载全量:AutoPipelineForText2Image.from_pretrained("stabilityai/sd-turbo", torch_dtype=torch.float16, variant="fp16").to("npu:0"),prompt 单步 pipe(prompt, num_inference_steps=1, guidance_scale=0.0)。
  • Diffusers 版本: 需 diffusers>=0.30.0, transformers>=4.40.0,本仓兼容 0.40.0.dev0。
  • 生成质量: replica 生成为噪声灰度可视验证(mean ~0.5),全权重可生成真实狸猫祭袍图像。
  • 分辨率: 默认 512x512 验证,可升至 768x768。

10. 标签

Hardware: NPU, Ascend, Ascend910, SD-Turbo, diffusion, text-to-image, stable-diffusion, diffusers

#NPU

agent_workflow npu_device_call model_result