Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/173295
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 4 Cancelled Jobs, 23 Unrelated FailuresAs of commit c6c5767 with merge base babfa88 ( NEW FAILURE - The following job has failed:
CANCELLED JOBS - The following jobs were cancelled. Please retry:
FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
…radients This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a tensor subclass is detected, e.g., DTensor, zeros_like() will be used to create the zero gradients. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. However, the blast area is quite big as all tensor subclasses will be affected. An alternative is that we detect whether the tensor subclass is DTensor and only perform this fix for DTensor. However, even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: 5cf0443 Pull-Request: #173295
|
Just noticed that #173175 was also created to fix. We can use either one. |
aaron-seq
left a comment
There was a problem hiding this comment.
The implementation correctly identifies the need to preserve tensor subclass types for unused gradients, but the detection mechanism is too broad, causing 19 test failures.
Root cause:
Using is_python_dispatch() (line 783) catches all Python dispatch tensors, including functorch's internal tensors (FakeTensor, AOTAutograd wrappers). When zeros_like() is applied to these tensors instead of creating plain zero tensors, it alters gradient materialization behavior and breaks functorch's input mutation tracking.
Failing tests:
- 18 failures in
test/functorch/test_aotdispatch.py(input mutation tests) - 1 failure in
test/test_autograd.py::test_undefined_grad_with_tensor_subclass(the new test added in this PR)
Recommendation:
Replace the broad is_python_dispatch() check with more specific detection for DTensor and other tensor subclasses that require zeros_like() preservation. Consider checking for specific subclass types or adding a trait that indicates whether a tensor subclass needs preserved zero gradient behavior, rather than applying this to all Python dispatch tensors.
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: d1eebc9 Pull-Request: #173295
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: b2e053e Pull-Request: #173295
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: 55ea9c0 Pull-Request: #173295
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: 620c374 Pull-Request: #173295
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: d076810 Pull-Request: #173295
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: 9b7d176 Pull-Request: #173295
soulitzer
left a comment
There was a problem hiding this comment.
Thanks, add some small comments.
| cdata->name(), | ||
| ". This preserves the DTensor type but may have performance " | ||
| "implications. To avoid this: (1) call " | ||
| "ctx.set_materialize_grads(False) in forward() (only works when " |
There was a problem hiding this comment.
Suggesting a different wording here:
To avoid this:
- If you are in eager (e.g. the name of the autograd.Function is not CompiledFunction) it means that one of the outputs of your autograd.Function is unused. You can call ctx.set_materialize_grads(False) in the forward() and update the backward() to handle potential None gradients.
- If you are in compile, it means that one of the outputs to the compiled region is unused. You should detach() any outputs that are unused and explicitly or implicitly (i.e., saved as a global) returned from the compiled region.
…ckward functions (#173865)" This reverts commit 90ea4db. Reverted #173865 on behalf of https://github.com/izaitsevfb due to breaks internal signals ([comment](#173295 (comment)))
…f unused output (#173295)" This reverts commit 64d74c2. Reverted #173295 on behalf of https://github.com/izaitsevfb due to breaks internal signals ([comment](#173295 (comment)))
|
@fegin your PR has been successfully reverted. |
| def test_multi_layer_memory_usage_with_zeros_like(self): | ||
| """ | ||
| Test memory usage when multiple layers each use MultiOutputFunc with | ||
| an unused DTensor output. Selective activation checkpointing (SAC) is |
There was a problem hiding this comment.
The SAC interaction is pretty orthogonal imo, to make the intent of the test clearer, just test w/o SAC, and check that the output is not saved?
| self->output_info.emplace_back(wrapped_output.value(), use_zeros_like); | ||
| bool output_is_dtensor = is_dtensor(obj); | ||
| bool output_is_nested = wrapped_output->is_nested(); | ||
| bool use_zeros_like = is_differentiable && num_outputs > 1 && |
There was a problem hiding this comment.
Do we want to combine the next PR (num_outputs = 1 case) into this one?
soulitzer
left a comment
There was a problem hiding this comment.
Thanks! Added minor comments
| // For DTensor, this preserves the mesh and placements while using minimal | ||
| // memory (just a single element instead of the full tensor). | ||
| // We use new_zeros({}) to create a scalar with the same type/subclass. | ||
| template_var = var.new_zeros({}).detach(); |
There was a problem hiding this comment.
Detach before new_zeros
| template_var = var.new_zeros({}).detach(); | |
| template_var = var.detach().new_zeros({}); |
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: 6e071e0 Pull-Request: #173295
…d DTensor outputs This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. Even for DTensor, we need to verify the implication when the sharding of the unused output is partial. ghstack-source-id: 0d5548d Pull-Request: #173295
…unctions (#173865) When an autograd function has multiple outputs but only some are used, the gradients for unused outputs are None. By default, autograd materializes these as zero tensors. #173295 improved this for DTensor outputs by using zeros_like() to preserve the DTensor type. However, #173295 doesn't work with from_local(). When autograd materializes a zero gradient for from_local, it creates a plain torch.Tensor, which causes a type mismatch error when mixed with DTensor in subsequent computations. This PR updates both from_local and to_local to explicitly handle None gradients in their backward functions, avoiding reliance on autograd's default materialization behavior. Pull Request resolved: #173865 Approved by: https://github.com/soulitzer, https://github.com/wconstab
… output (pytorch#173295) This is a tentative fix to pytorch#173123 and the issue encountered in pytorch#169393, following the fix to NJT, pytorch#136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type. When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode. In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch. Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior. **Conclusion** Based on the discussion with @drisspg, @bdhirsh, @soulitzer, @wconstab, we can land this PR as we limit the PR to DTensor only. But we also need to add a warning to indicate the potential performance issue and point out the solution to remove the zeros_like(). The warning will also print out the backward name so that users can debug. Pull Request resolved: pytorch#173295 Approved by: https://github.com/soulitzer
…unctions (pytorch#173865) When an autograd function has multiple outputs but only some are used, the gradients for unused outputs are None. By default, autograd materializes these as zero tensors. pytorch#173295 improved this for DTensor outputs by using zeros_like() to preserve the DTensor type. However, pytorch#173295 doesn't work with from_local(). When autograd materializes a zero gradient for from_local, it creates a plain torch.Tensor, which causes a type mismatch error when mixed with DTensor in subsequent computations. This PR updates both from_local and to_local to explicitly handle None gradients in their backward functions, avoiding reliance on autograd's default materialization behavior. Pull Request resolved: pytorch#173865 Approved by: https://github.com/soulitzer, https://github.com/wconstab ghstack dependencies: pytorch#173295
This reverts commit 909f129. Reverted pytorch#174466 on behalf of https://github.com/izaitsevfb due to blocks revert of pytorch#173295 ([comment](pytorch#174466 (comment)))
…ckward functions (pytorch#173865)" This reverts commit 90ea4db. Reverted pytorch#173865 on behalf of https://github.com/izaitsevfb due to breaks internal signals ([comment](pytorch#173295 (comment)))
…f unused output (pytorch#173295)" This reverts commit 64d74c2. Reverted pytorch#173295 on behalf of https://github.com/izaitsevfb due to breaks internal signals ([comment](pytorch#173295 (comment)))
…unctions (#173865) When an autograd function has multiple outputs but only some are used, the gradients for unused outputs are None. By default, autograd materializes these as zero tensors. #173295 improved this for DTensor outputs by using zeros_like() to preserve the DTensor type. However, #173295 doesn't work with from_local(). When autograd materializes a zero gradient for from_local, it creates a plain torch.Tensor, which causes a type mismatch error when mixed with DTensor in subsequent computations. This PR updates both from_local and to_local to explicitly handle None gradients in their backward functions, avoiding reliance on autograd's default materialization behavior. Pull Request resolved: #173865 Approved by: https://github.com/soulitzer, https://github.com/wconstab
…unctions (pytorch#173865) When an autograd function has multiple outputs but only some are used, the gradients for unused outputs are None. By default, autograd materializes these as zero tensors. pytorch#173295 improved this for DTensor outputs by using zeros_like() to preserve the DTensor type. However, pytorch#173295 doesn't work with from_local(). When autograd materializes a zero gradient for from_local, it creates a plain torch.Tensor, which causes a type mismatch error when mixed with DTensor in subsequent computations. This PR updates both from_local and to_local to explicitly handle None gradients in their backward functions, avoiding reliance on autograd's default materialization behavior. Pull Request resolved: pytorch#173865 Approved by: https://github.com/soulitzer, https://github.com/wconstab
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Stack from ghstack (oldest at bottom):
This is a tentative fix to #173123 and the issue encountered in #169393, following the fix to NJT, #136875. When a DTensor is detected, zeros_like() will be used to create the zero gradients to preserve the tensor subclass type.
When an op has multiple outputs but not all are used (e.g., LSE for FlexAttention), autograd will materialize the gradient of the unused output as a zero tensor. But autograd doesn't respect the tensor subclass. In our case, the q, k, v to FlexAttention are all DTensors, hence the outputs of FlexAttention, including LSE, are all DTensors. But LSE is not always used (depending on the model), which results in errors in both eager mode and compiler mode.
In eager mode, DTensor complains users mix torch.Tensor with DTensor in some computation. In compiler mode, compiler raises an exception due to an expected metadata mismatch.
Semantically this fix is correct since we should not upcast a tensor back to a plain tensor. To limit the blast area, we specifically detect DTensor rather than all tensor subclasses, to avoid affecting other subclasses that may not need this behavior.
Conclusion
Based on the discussion with @drisspg, @bdhirsh, @soulitzer, @wconstab, we can land this PR as we limit the PR to DTensor only. But we also need to add a warning to indicate the potential performance issue and point out the solution to remove the zeros_like(). The warning will also print out the backward name so that users can debug.
cc @wanchaol @tianyu-l @wz337 @XilunWu @d4l3k @pragupta @SherlockNoMad @ppwwyyxx @weifengpy