Skip to content

Make autograd use zeros_like for Python dispatch of DTensor - #173175

Closed
arkadip-maitra wants to merge 2 commits into
pytorch:mainfrom
arkadip-maitra:fix_173123
Closed

arkadip-maitra wants to merge 2 commits into
pytorch:mainfrom
arkadip-maitra:fix_173123

Conversation

@arkadip-maitra

@arkadip-maitra arkadip-maitra commented Jan 23, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #173123

When a multi output autograd.Function has an unused output then autograd should materialize a zero gradient for it.
It chooses between zeros_like which preserves tensor type and zeros_symint which creates plain tensor.

Previously, only nested tensors got zeros_like.

This fix adds DTensor to that condition. Now the mismatch does not happen.

Added a test too.

cc @wanchaol @tianyu-l @wz337 @XilunWu @d4l3k @pragupta @SherlockNoMad @ppwwyyxx

@pytorch-bot

pytorch-bot Bot commented Jan 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/173175

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 5821c54 with merge base 6d90af3 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@arkadip-maitra
arkadip-maitra requested review from bdhirsh, cyyever, fegin, soulitzer and wconstab and removed request for albanD and soulitzer January 23, 2026 12:24
@arkadip-maitra arkadip-maitra added topic: not user facing topic category module: dtensor distributed tensor tag labels Jan 23, 2026
@bdhirsh

bdhirsh commented Jan 23, 2026

Copy link
Copy Markdown
Collaborator

So this is fair (and maybe @soulitzer has thoughts). But my worry (described in #173123 (comment)) is that silently converting "gradients of zeros" into DTensors will lead to compile perf cliffs, when it would have been better to just fix the error

@arkadip-maitra

arkadip-maitra commented Jan 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

So this is fair (and maybe @soulitzer has thoughts). But my worry (described in #173123 (comment)) is that silently converting "gradients of zeros" into DTensors will lead to compile perf cliffs, when it would have been better to just fix the error

🤔 ok, just pushing another commit to fix failing tests. ig i will wait for @soulitzer comments

@soulitzer

Copy link
Copy Markdown
Contributor

Yeah this is tricky, for this to work you also have to always save a copy of the outputs for backward. My vote would also be for just having a nice error.

@soulitzer

soulitzer commented Jan 23, 2026 •

Copy link
Copy Markdown
Contributor

To add to this the nested tensor path was meant to be something temporary, and the plan was that we'd eventually replace it with something more sophisticated that didn't require saving an output for bw.

@fegin

fegin commented Jan 24, 2026 •

Copy link
Copy Markdown
Contributor

I just noticed this PR was open to fix the issue. I also created one in #173295. Either PR should be fine.

But I want to emphasize that the issue is more than just a compiler issue, if torch.compile is not used users can still encounter an error, mixing torch.Tensor and DTensor, in the backward. Please see the unittest in #173295. While the unittest may be a fake one, we do encounter this issue in eager when adding FlexAttention with DTensor support. I suspect we will have the same issuewhen doing varlen attention with DTensor.

For FlexAttention + DTensor, we have to workaround this issue in the eager mode. But our workaround is to silently conver the torch.Tensor to DTensor in the HOP dispatcher, not that different from doing this in autograd. Moreover, FlexAttention is going to be compiled and there is no way to ensure full graph because FlexAttention alone has to be compiled but not its caller. Detaching the output is also not very realistic for some cases as users may just disable the return of LSE but the LSE is still an output of the lower level API. So users don't have a chance to detach it. @drisspg do you think we can do this if users disable LSE we just detach() LSE automatically? cc., @bdhirsh

@soulitzer is there any plan to actually fix the issue as you mentioned?

If such a fix is not acceptable, we should have a workaround for all attention APIs (SDPA, varlen, FlexAttention) + DTensor.

cc., @wconstab

@soulitzer

Copy link
Copy Markdown
Contributor

btw this is a bit different from the other case where the compile's runtime autograd Function is the multi-output node. In this case the multi-output node had already existed in eager, so detaching is too late. Fixing this would require the multi-output op backward to be able to handle non-materialized grad_outs.

@bdhirsh bdhirsh assigned ge0405 and unassigned ge0405 Jan 26, 2026
@bdhirsh bdhirsh added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jan 26, 2026
@soulitzer

Copy link
Copy Markdown
Contributor

@soulitzer is there any plan to actually fix the issue as you mentioned?

Yeah, but it was nested tensor specific, so it won't help generally

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

Labels

module: dtensor distributed tensor tag open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DTensor] Gradient of unused output becomes plain torch.Tensor in backward, causing type mismatch with torch.compile

6 participants