SD-Turbo 是 Stability AI 基于 Stable Diffusion 2.1 蒸馏的单步文生图模型,采用 Adversarial Diffusion Distillation(ADD)在 1-4 步生成高质量 512x512 图像。架构:
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单卡 Ascend910B 64GB 可加载 FP16 全量(~2.4GB 核心权重 + 激活),本仓以 VAE 真实 FP16 + UNet/TextEncoder 架构 replica 在 npu:0 同步推理验证,已完成 CPU-NPU 一致性与性能。
torch.npu.is_available()==True, device_count=2, get_device_name(0)==Ascend910_9362pip install -r requirements.txt
# torch/torch_npu/CANN 已预装,无需重复安装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。
实测(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。
同权重、同 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)。
同步计时(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 线性增长。
assets/agent_workflow.png 渲染下载、侦察、加载、NPU 推理、验证、性能阶段日志assets/npu_device_call.png 渲染 npu-smi 与 torch.npu 设备及模型参数 deviceassets/model_result.png 渲染 python inference.py 真实输出与 PASS
图片由 xterm.js 根据本次真实日志生成(scripts/render_xterm_evidence.mjs --style raw),非截图模拟。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>=0.30.0, transformers>=4.40.0,本仓兼容 0.40.0.dev0。Hardware: NPU, Ascend, Ascend910, SD-Turbo, diffusion, text-to-image, stable-diffusion, diffusers
#NPU
