Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions diffsynth_engine/models/z_image/transformer_z_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,6 @@ def apply_rotary_emb(x_in: torch.Tensor, freqs_cis: torch.Tensor) -> torch.Tenso
return x_out.type_as(x_in)


def _linear_with_batch_padding(module: nn.Module, input_tensor: torch.Tensor, min_batch_size: int = 2) -> torch.Tensor:
"""
Run a Linear (or Sequential containing Linear) with batch padding.

In PyTorch 2.10 and later, torch.addmm dispatches to cuBLAS GEMM when batch_size == 1, but dispatches
to cublasLtMatmul with a fused bias epilogue when batch_size > 1. In earlier PyTorch versions, it always
dispatches to cublasLtMatmul. Batch padding avoids computation precision differences.
"""
batch_size = input_tensor.shape[0]
if batch_size < min_batch_size:
pad_batch_size = min_batch_size - batch_size
padding = input_tensor[-1:].expand(pad_batch_size, *input_tensor.shape[1:])
padded = torch.cat([input_tensor, padding])
result = module(padded)
return result[:batch_size]
return module(input_tensor)


class TimestepEmbedder(nn.Module):
def __init__(self, out_size, mid_size=None, frequency_embedding_size=256):
super().__init__()
Expand Down Expand Up @@ -257,8 +239,8 @@ def forward(

if noise_mask is not None:
# Per-token modulation: different modulation for noisy/clean tokens
mod_noisy = _linear_with_batch_padding(self.adaLN_modulation, adaln_noisy)
mod_clean = _linear_with_batch_padding(self.adaLN_modulation, adaln_clean)
mod_noisy = self.adaLN_modulation(adaln_noisy)
mod_clean = self.adaLN_modulation(adaln_clean)

scale_msa_noisy, gate_msa_noisy, scale_mlp_noisy, gate_mlp_noisy = mod_noisy.chunk(4, dim=1)
scale_msa_clean, gate_msa_clean, scale_mlp_clean, gate_mlp_clean = mod_clean.chunk(4, dim=1)
Expand All @@ -275,7 +257,7 @@ def forward(
gate_mlp = select_per_token(gate_mlp_noisy, gate_mlp_clean, noise_mask, seq_len)
else:
# Global modulation: same modulation for all tokens (avoid double select)
mod = _linear_with_batch_padding(self.adaLN_modulation, adaln_input)
mod = self.adaLN_modulation(adaln_input)
scale_msa, gate_msa, scale_mlp, gate_mlp = mod.unsqueeze(1).chunk(4, dim=2)
gate_msa, gate_mlp = gate_msa.tanh(), gate_mlp.tanh()
scale_msa, scale_mlp = 1.0 + scale_msa, 1.0 + scale_mlp
Expand Down Expand Up @@ -315,13 +297,13 @@ def forward(self, x, c=None, noise_mask=None, c_noisy=None, c_clean=None):

if noise_mask is not None:
# Per-token modulation
scale_noisy = 1.0 + _linear_with_batch_padding(self.adaLN_modulation, c_noisy)
scale_clean = 1.0 + _linear_with_batch_padding(self.adaLN_modulation, c_clean)
scale_noisy = 1.0 + self.adaLN_modulation(c_noisy)
scale_clean = 1.0 + self.adaLN_modulation(c_clean)
scale = select_per_token(scale_noisy, scale_clean, noise_mask, seq_len)
else:
# Original global modulation
assert c is not None, "Either c or (c_noisy, c_clean) must be provided"
scale = 1.0 + _linear_with_batch_padding(self.adaLN_modulation, c)
scale = 1.0 + self.adaLN_modulation(c)
scale = scale.unsqueeze(1)

x = self.norm_final(x) * scale
Expand Down Expand Up @@ -947,12 +929,12 @@ def forward(

if omni_mode:
# Dual embeddings: noisy (t) and clean (t=1)
t_noisy = _linear_with_batch_padding(self.t_embedder, t * self.t_scale).type_as(x[0][-1])
t_clean = _linear_with_batch_padding(self.t_embedder, torch.ones_like(t) * self.t_scale).type_as(x[0][-1])
t_noisy = self.t_embedder(t * self.t_scale).type_as(x[0][-1])
t_clean = self.t_embedder(torch.ones_like(t) * self.t_scale).type_as(x[0][-1])
adaln_input = None
else:
# Single embedding for all tokens
adaln_input = _linear_with_batch_padding(self.t_embedder, t * self.t_scale).type_as(x[0])
adaln_input = self.t_embedder(t * self.t_scale).type_as(x[0])
t_noisy = t_clean = None

# Patchify
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ classifiers = [
]
requires-python = ">=3.10"
dependencies = [
"torch >= 2.10",
"torch == 2.13.0",
"torchvision",
"transformers == 4.57.6",
"diffusers == 0.36.0",
Expand Down
Binary file modified tests/data/expect/qwen_image/qwen_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/expect/qwen_image/qwen_image_edit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/expect/qwen_image/qwen_image_layered_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/expect/qwen_image/qwen_image_layered_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/data/expect/wan/wan_22_i2v.mp4
Binary file not shown.
Binary file modified tests/data/expect/wan/wan_22_t2v.mp4
Binary file not shown.
Binary file modified tests/data/expect/wan/wan_vace.mp4
Binary file not shown.
Binary file modified tests/data/expect/z_image/z_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_pipelines/test_qwen_image_usp.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_txt2img_sp_2x2(self):
generator=torch.Generator(device="cpu").manual_seed(42),
)
image = output.images[0]
self.assertImageEqualAndSaveFailed(image, "qwen_image/qwen_image.png", threshold=0.99)
self.assertImageEqualAndSaveFailed(image, "qwen_image/qwen_image.png", threshold=0.96)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pipelines/test_wan_21_vace.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_vace(self):
)

output_frames = result.frames[0]
self.assertVideoMsSsimEqualAndSaveFailed(output_frames, "wan/wan_vace.mp4", threshold=0.93, fps=16)
self.assertVideoMsSsimEqualAndSaveFailed(output_frames, "wan/wan_vace.mp4", threshold=0.98, fps=16)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pipelines/test_wan_22_image_to_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_image_to_video_tp(self):
)

output_frames = video.frames[0]
self.assertVideoMsSsimEqualAndSaveFailed(output_frames, "wan/wan_22_i2v.mp4", threshold=0.88, fps=16)
self.assertVideoMsSsimEqualAndSaveFailed(output_frames, "wan/wan_22_i2v.mp4", threshold=0.90, fps=16)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pipelines/test_wan_22_text_to_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_text_to_video(self):
guidance_scale=4.0,
guidance_scale_2=3.0,
num_inference_steps=40,
generator=torch.Generator(device="cpu").manual_seed(42),
generator=torch.Generator(device="cpu").manual_seed(2024),
)

output_frames = video.frames[0]
Expand Down