本文档记录 Z-Image-Turbo-SDA 在 Ascend NPU (Atlas 800 A2/A3) 环境的快速部署与验证结果。
Z-Image-Turbo-SDA 是基于 Tongyi-MAI/Z-Image-Turbo 的 LoKr (Low-Rank Kronecker Product) 适配器,通过 Semantic Directional Alignment (SDA) 技术解决了少步蒸馏模型的 "Diversity Collapse" 问题。该适配器在保持 8 步推理速度的同时,恢复了原始教师模型 70.2% 的组合多样性 (LPIPS)。
模型组件:
主要特点:
相关获取地址:
https://gitcode.com/hf_mirrors/F16/z-image-turbo-sdahttps://huggingface.co/F16/z-image-turbo-sdahttps://huggingface.co/Tongyi-MAI/Z-Image-Turbo| 组件 | 版本 |
|---|---|
torch | 2.9.0+cpu |
torch-npu | 2.9.0.post1+gitee7ba04 |
diffusers | 0.38.0 |
transformers | 4.57.6 |
peft | 0.19.1 |
Ascend910B4/tmp/models/Z-Image-Turbo/opt/atomgit/z_image_turbo_sda/model/adapter/zit_sda_v1.safetensors512×512 到 2048×2048# 设置 NPU 设备
export ASCEND_RT_VISIBLE_DEVICES=0
# 设置内存分配优化
export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
# 设置 HCCL 缓冲区大小
export HCCL_BUFFSIZE=512
# 设置线程绑定
export OMP_PROC_BIND=false
export OMP_NUM_THREADS=1# 下载基础模型
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download \
--resume-download Tongyi-MAI/Z-Image-Turbo \
--local-dir /tmp/models/Z-Image-Turbo
# 下载 SDA LoKr 适配器
HF_ENDPOINT=https://hf-mirror.com huggingface-cli download \
--resume-download F16/z-image-turbo-sda \
--local-dir /opt/atomgit/z_image_turbo_sda/model基础推理命令:
python inference.py \
--base_model_path /tmp/models/Z-Image-Turbo \
--adapter_path /opt/atomgit/z_image_turbo_sda/model/adapter/zit_sda_v1.safetensors \
--prompt "A young woman standing on a sunny coastline, white dress fluttering in the sea breeze." \
--height 1024 \
--width 1024 \
--num_inference_steps 8 \
--guidance_scale 1.0 \
--adapter_weight 1.0 \
--dtype bfloat16 \
--device npu:0 \
--output output| 参数 | 默认值 | 说明 |
|---|---|---|
--base_model_path | /tmp/models/Z-Image-Turbo | 基础模型路径 |
--adapter_path | model/adapter/zit_sda_v1.safetensors | LoKr 适配器路径 |
--adapter_weight | 1.0 | 适配器权重 (建议 0.5~1.0) |
--prompt | - | 文本提示词 |
--height | 1024 | 图像高度 |
--width | 1024 | 图像宽度 |
--num_inference_steps | 8 | 推理步数 |
--guidance_scale | 1.0 | CFG 引导强度 |
--device | npu:0 | 设备 |
--warmup_runs | 1 | 预热次数 |
--benchmark_runs | 3 | 基准测试次数 |
# 检查模型加载
python -c "
import torch
import torch_npu
from diffusers import ZImagePipeline
from safetensors.torch import load_file
pipe = ZImagePipeline.from_pretrained('/tmp/models/Z-Image-Turbo', torch_dtype=torch.bfloat16)
pipe = pipe.to('npu:0')
adapter = load_file('/opt/atomgit/z_image_turbo_sda/model/adapter/zit_sda_v1.safetensors')
pipe.load_lora_weights(adapter, adapter_name='sda_diversity')
pipe.set_adapters(['sda_diversity'], adapter_weights=[1.0])
print('Model loaded successfully!')
print(f'Device: {pipe.device}')
print(f'Dtype: {pipe.dtype}')
"# 快速推理测试 (512x512, 4步)
python inference.py \
--height 512 \
--width 512 \
--num_inference_steps 4 \
--warmup_runs 1 \
--benchmark_runs 1 \
--output output/smoke_test验证结果:
| 参数 | 值 |
|---|---|
| 分辨率 | 1024×1024 |
| 推理步数 | 8 |
| Guidance Scale | 1.0 |
| Adapter Weight | 1.0 |
| 数据类型 | bfloat16 |
| 设备 | Ascend910B4 |
运行性能评测脚本:
python perf_eval.py \
--height 1024 \
--width 1024 \
--num_inference_steps 8 \
--num_warmup 2 \
--num_iterations 10 \
--output perf_results.json参考性能数据(基于同架构 Z-Image-Fun-Lora-Distill 参考值):
| 指标 | 参考值 |
|---|---|
| 512×512 4步 | ~0.76s, ~1.31 img/s |
| 1024×1024 4步 | ~2.19s, ~0.46 img/s |
| 显存占用 | ~20GB |
| 显存预留 | ~24GB |
注:实际性能可能因硬件配置和系统负载而异。
20GB24GB+512×512 到 2048×2048| 场景 | Steps | CFG | Adapter Weight | 说明 |
|---|---|---|---|---|
| 最大多样性 | 8 | 1.0 | 1.0 | 完整 SDA 效果 |
| 平衡模式 | 8 | 1.0 | 0.7 | 适中多样性 |
| 配合其他 LoRA | 8 | 1.0 | 0.5 | 降低冲突风险 |
| 快速预览 | 4 | 1.0 | 1.0 | 速度优先 |
bfloat16 数据类型以获得最佳性能PYTORCH_NPU_ALLOC_CONF=expandable_segments:True 优化内存分配HCCL_BUFFSIZE=512 优化通信缓冲区device="cpu" 以确保种子可复现| 文件 | 说明 |
|---|---|
inference.py | 推理脚本 (Smoke 验证 + 基准测试) |
perf_eval.py | 性能评测脚本 |
accuracy_eval.py | 精度评测脚本 |
model/adapter/zit_sda_v1.safetensors | SDA LoKr 适配器权重 |
readme.md | 部署文档 |
NPU vs CPU 精度对比(CPU 为基线,NPU 为验证目标):
| 指标 | 数值 |
|---|---|
| 测试用例数 | 待运行 |
| 最大 logits 差异 | 待运行 |
| 预测一致性 | 待运行 |
| 精度要求 | NPU vs CPU 最大 logits 误差 < 1% |
| 精度结论 | 待运行 |
精度评测源代码和日志详见 eval/ 目录。