Skip to content
Merged
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
14 changes: 9 additions & 5 deletions miles/backends/fsdp_utils/configs/qwen_image21.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,15 @@ def compute_noise_pred(
guidance_scale: float,
true_cfg_scale: float | None,
) -> dict[str, torch.Tensor]:
# The 2.1 DiT projects the joint text+image sequence; keep the target image tail.
pred = super().compute_noise_pred(
model=model,
# The 2.1 DiT projects the joint text+image sequence; keep the target image tail of each CFG branch, whose
# text lengths differ.
target_tokens = latents_input["visual"].shape[1]

def target_tail(**kwargs):
return (model(**kwargs)[0][:, -target_tokens:],)

return super().compute_noise_pred(
model=target_tail,
latents_input=latents_input,
timesteps_input=timesteps_input,
pos_cond=pos_cond,
Expand All @@ -190,8 +196,6 @@ def compute_noise_pred(
guidance_scale=guidance_scale,
true_cfg_scale=true_cfg_scale,
)
target_tokens = latents_input["visual"].shape[1]
return {"visual": pred["visual"][:, -target_tokens:]}

def cfg_combine(
self,
Expand Down
24 changes: 22 additions & 2 deletions tests/fast/backends/fsdp_utils/configs/test_qwen_image21.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def test_no_legacy_window_pad(self):


class _TailDiT(torch.nn.Module):
def forward(self, hidden_states, timestep, return_dict=False, **kwargs):
extra = hidden_states.new_ones(hidden_states.shape[0], 3, hidden_states.shape[-1])
def forward(self, hidden_states, timestep, return_dict=False, text_len=3, **kwargs):
extra = hidden_states.new_ones(hidden_states.shape[0], text_len, hidden_states.shape[-1])
return (torch.cat([extra, hidden_states * 2], dim=1),)


Expand All @@ -97,3 +97,23 @@ def test_compute_noise_pred_keeps_target_tail():
true_cfg_scale=1.0,
)
torch.testing.assert_close(pred["visual"], latents * 2)


def test_cfg_combines_the_target_tails_of_branches_with_different_text_lengths():
"""A negative prompt is rarely as long as the prompt; combined before the tail is cut, the joint outputs of the
two branches do not even have the same length."""
cfg = QwenImage21TrainPipelineConfig()
latents = torch.arange(8.0).reshape(1, 4, 2)
pred = cfg.compute_noise_pred(
model=_TailDiT(),
latents_input={"visual": latents},
timesteps_input={"visual": torch.tensor([0.5])},
pos_cond={"text_len": 3},
neg_cond={"text_len": 5},
joint_cond=None,
use_cfg=True,
cfg_batching=False,
guidance_scale=4.0,
true_cfg_scale=None,
)
torch.testing.assert_close(pred["visual"], latents * 2)
Loading