Repository navigation
Fix autograd.Function + NJT when an output grad is None - #136875
jbschlosser wants to merge 5 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/136875
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 4835ca9 with merge base f8a5b71 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Maybe we could do this under a flag, since I'd consider it a relatively rare case that can be worked around by the user in most situations. |
Which part would be under the flag? Would the maintainer of e.g. Note that I ran into this in practice during integration with FlexAttention ( |
|
I think its an unfortunate situation either way, but...probably fine and we shouldn't stress too much since we'll have factory functions hopefully soon :P Cons: Pros: One I would say is that we can further decrease the blast radius of this PR by limiting to only save when there are at least two differentiable outputs, and one of them is the NJT. We could add a flag to disable this for the cases where the user is certain that they won't run into this, but I'm also not sure where to advertise that. |
sorry I'm confused about this - why would an opt-in approach serve people who weren't using NJT before? this PR doesn't affect them since it only makes changes conditional on nested tensors.
By "more memory" here, do you mean for the case where an autograd.Function utilizes NJT but doesn't hit the "output grad is None" case?
sure, I think this should cover the prior case pretty well. updating the PR with this logic. |
For `autograd.Function`, the engine will try to allocate correctly-shaped zeros for `None` grads (i.e. in the case where the output isn't used downstream). It determines the shape of these zeros from the `VariableInfo` entry, which is derived from the forward output shape. For the NJT forward output case, the size info stored will contain a nested int, and calling `zeros()` with this size throws: ``` RuntimeError: .../build/aten/src/ATen/RegisterCPU.cpp:5260: SymIntArrayRef expected to contain only concrete integers ``` This PR fixes this by storing the full tensor in the `VariableInfo` for the nested case and calling `zeros_like()` to allocate correctly-shaped zeros. This is pretty inefficient; ideally we would want to save just the NJT shape and be able to construct zeros from it, but this requires factory function support for nested ints (WIP). So this is a short-term fix until we have that. [ghstack-poisoned]
Ah whoops typo, I meant "opt out" lol
Yeah |
For `autograd.Function`, the engine will try to allocate correctly-shaped zeros for `None` grads (i.e. in the case where the output isn't used downstream). It determines the shape of these zeros from the `VariableInfo` entry, which is derived from the forward output shape. For the NJT forward output case, the size info stored will contain a nested int, and calling `zeros()` with this size throws: ``` RuntimeError: .../build/aten/src/ATen/RegisterCPU.cpp:5260: SymIntArrayRef expected to contain only concrete integers ``` This PR fixes this by storing the full tensor in the `VariableInfo` for the nested case and calling `zeros_like()` to allocate correctly-shaped zeros. This is pretty inefficient; ideally we would want to save just the NJT shape and be able to construct zeros from it, but this requires factory function support for nested ints (WIP). So this is a short-term fix until we have that. [ghstack-poisoned]
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 jobs have failed, first few of them are: Check mergeability of ghstack PR / ghstack-mergeability-check Details for Dev Infra teamRaised by workflow job |
For `autograd.Function`, the engine will try to allocate correctly-shaped zeros for `None` grads (i.e. in the case where the output isn't used downstream). It determines the shape of these zeros from the `VariableInfo` entry, which is derived from the forward output shape. For the NJT forward output case, the size info stored will contain a nested int, and calling `zeros()` with this size throws: ``` RuntimeError: .../build/aten/src/ATen/RegisterCPU.cpp:5260: SymIntArrayRef expected to contain only concrete integers ``` This PR fixes this by storing the full tensor in the `VariableInfo` for the nested case and calling `zeros_like()` to allocate correctly-shaped zeros. This is pretty inefficient; ideally we would want to save just the NJT shape and be able to construct zeros from it, but this requires factory function support for nested ints (WIP). So this is a short-term fix until we have that. [ghstack-poisoned]
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
Fix is in to detach properly @soulitzer . I verified that it fixes the broken test. |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…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
…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
…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: 19cff54 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: d31ac40 Pull-Request: #173295
… output (#173295) 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. Pull Request resolved: #173295 Approved by: https://github.com/soulitzer
…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
… 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
Stack from ghstack (oldest at bottom):
For
autograd.Function, the engine will try to allocate correctly-shaped zeros forNonegrads (i.e. in the case where the output isn't used downstream). It determines the shape of these zeros from theVariableInfoentry, which is derived from the forward output shape. For the NJT forward output case, the size info stored will contain a nested int, and callingzeros()with this size throws:This PR fixes this by storing the full tensor in the
VariableInfofor the nested case and callingzeros_like()to allocate correctly-shaped zeros. This is pretty inefficient; ideally we would want to save just the NJT shape and be able to construct zeros from it, but this requires factory function support for nested ints (WIP). So this is a short-term fix until we have that.