From 940473d47956ba2211442a98409f8fd585c200ba Mon Sep 17 00:00:00 2001 From: Andy Ye <89891424+zhihengy@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:41:41 +0000 Subject: [PATCH] fix(qwen_image21): cut each CFG branch's image tail before combining them The joint outputs of the two branches differ in length whenever the negative prompt and the prompt do, so the combine failed on a shape mismatch. --- .../fsdp_utils/configs/qwen_image21.py | 14 +++++++---- .../fsdp_utils/configs/test_qwen_image21.py | 24 +++++++++++++++++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/miles/backends/fsdp_utils/configs/qwen_image21.py b/miles/backends/fsdp_utils/configs/qwen_image21.py index 1dc55755a..12ea03feb 100644 --- a/miles/backends/fsdp_utils/configs/qwen_image21.py +++ b/miles/backends/fsdp_utils/configs/qwen_image21.py @@ -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, @@ -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, diff --git a/tests/fast/backends/fsdp_utils/configs/test_qwen_image21.py b/tests/fast/backends/fsdp_utils/configs/test_qwen_image21.py index 9920be40f..0b199cbda 100644 --- a/tests/fast/backends/fsdp_utils/configs/test_qwen_image21.py +++ b/tests/fast/backends/fsdp_utils/configs/test_qwen_image21.py @@ -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),) @@ -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)