Repository navigation
Make autograd use zeros_like for Python dispatch of DTensor - #173175
arkadip-maitra wants to merge 2 commits into
Conversation
🔗 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 FailuresAs of commit 5821c54 with merge base 6d90af3 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
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 |
|
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. |
|
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. |
|
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 |
|
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. |
Yeah, but it was nested tensor specific, so it won't help generally |
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_likewhich preserves tensor type andzeros_symintwhich creates plain tensor.Previously, only nested tensors got
zeros_like.This fix adds
DTensorto that condition. Now the mismatch does not happen.Added a test too.
cc @wanchaol @tianyu-l @wz337 @XilunWu @d4l3k @pragupta @SherlockNoMad @ppwwyyxx