Skip to content

Forward the inputs DSUNet._forward accepts to the UNet - #8408

Open
vineethsaivs wants to merge 1 commit into
deepspeedai:masterfrom
vineethsaivs:fix/dsunet-forward-kwargs
Open

vineethsaivs wants to merge 1 commit into
deepspeedai:masterfrom
vineethsaivs:fix/dsunet-forward-kwargs

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

What is wrong

DSUNet._forward accepts timestep_cond and added_cond_kwargs and forwards neither, and it passes return_dict positionally:

return self.unet(sample, timestamp, encoder_hidden_states, return_dict)

UNet2DConditionModel.forward's fourth positional parameter is class_labels, not return_dict:

def forward(self, sample, timestep, encoder_hidden_states,
            class_labels=None, timestep_cond=None, attention_mask=None,
            cross_attention_kwargs=None, added_cond_kwargs=None, ..., return_dict=True)

Driving the wrapper with a stub that mirrors that signature, with enable_cuda_graph=False so no accelerator is needed:

caller passes UNet receives on master UNet receives with this PR
return_dict=False class_labels=False, return_dict=True class_labels=None, return_dict=False
timestep_cond=<tensor> None the tensor
added_cond_kwargs={...} None the dict

SDXL's UNet requires added_cond_kwargs (addition_embed_type == "text_time"), and timestep_cond is what a guidance-distilled model reads, so both are dropped exactly where they matter.

What changed

Pass everything after encoder_hidden_states by keyword. The if cross_attention_kwargs: branch goes with it: cross_attention_kwargs=None is what omitting the argument already meant, and both of its arms carried the same positional mistake.

Test

test_ds_unet_forwards_the_inputs_it_accepts in tests/unit/inference/test_stable_diffusion.py, a plain function alongside the existing TestStableDiffusion class. It drives DSUNet with a recording stub, needs no accelerator and downloads no model.

$ pytest tests/unit/inference/test_stable_diffusion.py -k ds_unet -q
master      1 failed    assert True is False   (return_dict never reached the UNet)
this PR     1 passed

yapf --style .style.yapf --diff is clean on both files.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5a030136ea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

encoder_hidden_states,
timestep_cond=timestep_cond,
cross_attention_kwargs=cross_attention_kwargs,
added_cond_kwargs=added_cond_kwargs,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Refresh nested added conditioning before graph replay

With enable_cuda_graph=True (the default), SDXL passes added_cond_kwargs as a dictionary containing tensors such as text_embeds and time_ids. _graph_replay only copies top-level tensor kwargs, so the captured dictionary from the first invocation is never refreshed; a later pipeline call with a different prompt replays using the first call's added conditioning and produces silently incorrect images. Flatten or explicitly copy the dictionary's tensor values into static buffers before replay; the new test disables CUDA graphs and therefore does not cover this path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real, and split out into #8488.

You are right that forwarding added_cond_kwargs is what makes the staleness reachable: before this PR it was dropped entirely, so there was nothing to go stale. _graph_replay matches on torch.is_tensor, so a dict of tensors is skipped and the capture-time conditioning is replayed for every later prompt.

It is not specific to this file. The same six lines sit in DSVAE (three times), DSClipEncoder and InferenceEngine, all with the same gap. #8488 adds one refresh_static_tensors() helper next to CUDAGraph and uses it at all six sites, with a CPU test that fails on master.

Keeping it out of this PR so this one stays about the argument list.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

CUDA graph replay does not refresh tensors nested within added_cond_kwargs, causing stale conditioning on later calls.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Fixes DSUNet argument forwarding to the wrapped Diffusers UNet.

Changes:

  • Forwards optional inputs by keyword.
  • Adds a regression test for argument forwarding.
File Description
deepspeed/​model_implementations/​diffusers/​unet.py Corrects wrapped UNet invocation.
tests/​unit/​inference/​test_stable_diffusion.py Tests forwarded argument values.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

encoder_hidden_states,
timestep_cond=timestep_cond,
cross_attention_kwargs=cross_attention_kwargs,
added_cond_kwargs=added_cond_kwargs,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handled in #8488, which refreshes tensors nested in containers at every replay site. It builds on this PR, so the two should land together.

@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Friendly bump: still relevant, and it merges cleanly with the base branch. Happy to adjust anything.

@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Friendly ping: this is still relevant and merges cleanly with main. Happy to rebase or change anything.

DSUNet._forward takes timestep_cond and added_cond_kwargs and passes neither
on, and it hands return_dict to the UNet positionally, where
UNet2DConditionModel.forward reads class_labels. So a caller asking for
return_dict=False gets class_labels=True and the UNet's own default instead,
SDXL's added_cond_kwargs never arrives, and a guidance-distilled model's
timestep_cond is dropped.

Pass everything after encoder_hidden_states by keyword. The cross_attention
branch goes away with it: cross_attention_kwargs=None is what omitting it
already meant.

Signed-off-by: Vineeth Sai <vineethsai4444@gmail.com>
@vineethsaivs
vineethsaivs force-pushed the fix/dsunet-forward-kwargs branch from 5a03013 to 5a0fb26 Compare November 29, 2026 08:40
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants