









Z-Image-Turbo-SDA 是一款高效的 LoKr(低秩克罗内克积) 适配器,旨在解决少步蒸馏流匹配/扩散模型中的 “多样性坍缩” 问题。
通过应用我们创新的 语义方向对齐(SDA)损失,该 LoKr 适配器能够恢复原始教师模型 70.2% 的组合多样性(LPIPS),同时完美保持 8 步基线模型的极致清晰度和快速推理速度。
将50步扩散模型(教师模型)蒸馏为8步模型(学生模型)时,学生模型往往会学习走“捷径”,收敛到分布的“均值”。结果是,改变随机噪声种子几乎会得到相同的构图(相同的姿态、相同的布局)。
标准的监督微调(SFT)无法解决此问题,因为强迫学生模型匹配教师模型的绝对速度()会破坏校正后的直线轨迹,导致生成模糊、发灰或混乱的图像。
我们没有匹配原始速度,而是使用基于物理的四支柱自定义架构对该模型进行微调,并以参数高效的LoKr结构作为支撑:
我们不直接匹配原始速度,而是将预测投影到干净的空间()。然后,我们应用空间低通滤波(AvgPool2d,) 以防止模型通过高频噪声“作弊”。最后,我们使用标准余弦损失来对齐学生模型与教师模型方差的语义方向,完全释放学生模型以保持其高对比度幅度。
为防止多样性推动破坏8步轨迹,我们引入了一个连续锚点:
sft_loss = Huber(v_student, v_baseline_detached, delta=0.08)
这就像一根弹性 tether。如果模型偏离过远以改变构图,Huber损失会提供一个恒定的(L1)拉力,以打磨高频细节,而不会将模型猛烈拉回坍缩的均值。
如果使用预训练的训练适配器,我们会非对称地应用它:
div_skip)此适配器作为LoKr(LyCORIS生态系统)进行训练。现代版本的diffusers与peft结合,原生支持无缝加载LoKr权重。
from diffusers import DiffusionPipeline
import torch
# 1. Load the base 8-step distilled model
pipeline = DiffusionPipeline.from_pretrained(
"Tongyi-MAI/Z-Image-Turbo",
torch_dtype=torch.float16
).to("cuda")
# 2. Load the SDA Diversity LoKr Adapter
# (Ensure you have the latest `diffusers` and `peft` installed)
pipeline.load_lora_weights("F16/z-image-turbo-sda", adapter_name="sda_diversity")
prompt = "A lone traveler standing on a mountain peak, epic fantasy lighting"
# 3. Generate diverse images with different seeds!
# You will now get completely different poses, camera angles, and layouts across seeds.
for seed in[42, 123, 777, 999]:
generator = torch.Generator(device="cuda").manual_seed(seed)
image = pipeline(
prompt=prompt,
num_inference_steps=8,
guidance_scale=1,
generator=generator
).images[0]
image.save(f"traveler_seed_{seed}.png")在复杂提示词和每个提示词16个随机种子的条件下,于Step 2500进行评估:
| 模型变体 | 平均LPIPS(感知多样性) | 像素标准差(幅度方差) |
|---|---|---|
| 8步基线(已坍缩) | 0.564 | 0.190 |
| 8步 + SDA LoKr(我们的方法) | 0.691 (+0.127) | 0.209 (原始清晰度) |
| 50步教师模型 | 0.745 | 0.243 |
重要意义: SDA LoKr 成功地将结构多样性与破坏性噪声解耦。它将宏观构图多样性(LPIPS)提升至接近50步教师模型的水平,同时严格控制像素标准差,以防止连续ODE微调中常见的“变暗/模糊”效应。
核心损失机制可轻松集成到任何Diffusers / Flow Matching训练循环中。
import torch
import torch.nn.functional as F
def compute_sda_loss(student_model, teacher_model, z1, z2, latents, t_continuous, v_self_detached):
# 1. Teacher Predictions (No Grad)
with torch.no_grad():
x0_T_z1 = z1 - teacher_model(z1, t_continuous)
x0_T_z2 = z2 - teacher_model(z2, t_continuous)
# 2. Student Baseline (from detached SFT forward, Adapter is ON)
x0_S_z1 = z1 - v_self_detached
# 3. Asymmetric Adapter Masking: Adapter-OFF for Diversity
# Temporarily disable the pre-trained assistant adapter to expose
# the raw collapsed manifold and generate massive diversity gradients.
student_model.assistant_adapter.is_active = False
try:
# Student forward with Adapter OFF
x0_S_z2 = z2 - student_model(z2, t_continuous)
finally:
# Crucial: Turn it back ON for subsequent SFT / validation steps
student_model.assistant_adapter.is_active = True
# Calculate Deltas
delta_T = x0_T_z2 - x0_T_z1
delta_S = x0_S_z2 - x0_S_z1
# 4. Spatial Low-Pass Filter (Force Macro-Composition changes, prevent high-freq cheating)
pooled_T = F.avg_pool2d(delta_T, kernel_size=8).view(delta_T.size(0), -1)
pooled_S = F.avg_pool2d(delta_S, kernel_size=8).view(delta_S.size(0), -1)
# 5. Standard Cosine Loss (Direction alignment without magnitude penalty)
cos_sim = F.cosine_similarity(pooled_S, pooled_T, dim=-1)
div_loss = (1.0 - cos_sim).mean()
return div_loss
# --- Training Loop Integration ---
# SFT Forward (Adapter ON) -> Protects pristine image quality
with torch.no_grad():
v_self = student_baseline(xt_z1, t)
# Self-Reference Huber Loss (Elastic anchor to preserve 8-step trajectory)
sft_loss = F.huber_loss(v_pol_z1, v_self.detach(), delta=0.08)
# Time-Segmented Diversity (Enabled only for t > div_skip_threshold)
# Skip Index 7 (extremely low noise) to lock in final pixel sharpness
if t > LOW_NOISE_THRESHOLD:
div_loss = compute_sda_loss(student_model, teacher_model, z1, z2, latents, t, v_pol_z1.detach())
loss = sft_loss + diversity_lambda * div_loss
else:
loss = sft_loss # Pure SFTfull_rank,factor=8)weight_decay=0.001)div_skip。A:可以,但存在潜在的兼容性风险。您需要手动平衡权重。
0.5 ~ 0.7)。注意内在权衡:降低权重会减弱“语义推动”,导致多样性向坍缩均值回归。您需要根据具体需求找到“最佳平衡点”。A:这取决于提示词对构图空间施加的“拓扑约束”。
SDA 是我个人研究和探索 Flow Matching 模型底层机制的成果。因此,它远非完美。
当前的 v1 版本 基于我们内部最佳检查点(div_27_2500,该检查点实现了高达 70% 的多样性恢复),随后进行了额外 360 步以质量为重点的微调。
在 8 步推理框架内解决“宏观构图多样性”与“微观解剖结构刚性”之间的零和博弈仍是一个开放的挑战。我希望未来能找到更优雅的解决方案,也欢迎社区在此探索基础上继续发展。
如果您在研究中使用了此模型或 SDA 方法,请引用:
@misc{sda_diversity_loss_2026,
title={Teacher-Guided Semantic Directional Alignment (SDA) for Restoring Diversity in Few-Step Distilled Models},
author={Fok},
year={2026},
url={https://huggingface.co/F16/z-image-turbo-sda}
}