Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/169393
Note: Links to docs will display an error until the docs builds have been completed. ❌ 3 New Failures, 3 Unrelated FailuresAs of commit 6abd26e with merge base aea70db ( NEW FAILURES - The following jobs have failed:
BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
| # TODO: we asume the grad_logsumexp are all zeros if it is not a DTensor, | ||
| # but I'm not sure if we can safely assume this. | ||
| for i, s in enumerate(logsumexp._local_tensor.shape): | ||
| grad_logsumexp = grad_logsumexp.narrow(i, 0, s) | ||
| grad_logsumexp = DTensor.from_local( | ||
| grad_logsumexp, | ||
| device_mesh=out.device_mesh, | ||
| placements=out.placements, | ||
| ) |
There was a problem hiding this comment.
Confirming; the behavior of autograd funcs for outputs that dont end up have a back propping grad is to allocate a zeros_like of the output, so should be good if it is not used in downstream ops
|
|
||
| 1. **Modified operator signature**: We modify the op signature to only | ||
| include the necessary mask tensors for sharding validation, since mask | ||
| tensors cannot be resharded. This simplification is needed because |
There was a problem hiding this comment.
could you say more about "mask tensors cannot be resharded"?
I saw that needs_redistribute is still used normally to redistribute args (including mask tensors?)
| Shard(2), # kv_num_blocks, | ||
| Shard(2), # kv_indices, |
There was a problem hiding this comment.
Is this intended? I forgot what they mean, but it sounds they should be Replicate.
|
|
||
|
|
||
| def create_distributed_block_mask( | ||
| mask_mod: _mask_mod_signature, |
There was a problem hiding this comment.
I guess with CP (+ load balancing), users are still responsible for modifying the mask_mod to do (1) local to global q idx mapping and (2) position_ids / load balancer based idx permutation.
| ) | ||
|
|
||
|
|
||
| def shard_mask_mod( |
There was a problem hiding this comment.
I think these APIs should live in the flex attention module instead of the dtensor module, as it's very specific to flex attention
There was a problem hiding this comment.
Yes, we should probably move it out. But not flex attention but under distributed. Otherwise, there will be endless circular dependencies.
| from .._trace_wrapped_higher_order_op import TransformGetItemToIndex | ||
|
|
||
| def create_scalar(): | ||
| def create_scalar(query): |
There was a problem hiding this comment.
nit: for me -> we should be calling torch.empty here I remember this not working from ages ago and I dont know why lets re confirm.
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo wanchaol tianyu-l wz337 XilunWu d4l3k pragupta SherlockNoMad [ghstack-poisoned]
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo wanchaol tianyu-l wz337 XilunWu d4l3k pragupta SherlockNoMad [ghstack-poisoned]
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo wanchaol tianyu-l wz337 XilunWu d4l3k pragupta SherlockNoMad [ghstack-poisoned]
| mask_mod_other_buffers, | ||
| ) | ||
|
|
||
| if not isinstance(grad_logsumexp, DTensor): |
There was a problem hiding this comment.
@fegin I'm trying to run a repro to understand what's going on better. But running the tests in this PR errors for me with the below. Is there a repro somewhere I can go off of?
python test/distributed/tensor/test_flex_attention.py -k test_flex_attention_replicate
...
File "/data/users/hirsheybar/new2/pytorch/torch/_dynamo/variables/higher_order_ops.py", line 4145, in _call_function
score_mod_node, score_mod_lifted_args = self.create_wrapped_node(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/users/hirsheybar/new2/pytorch/torch/_dynamo/variables/higher_order_ops.py", line 4079, in create_wrapped_node
bhmn = [create_scalar() for _ in range(4)]
^^^^^^^^^^^^^^^
File "/data/users/hirsheybar/new2/pytorch/torch/_dynamo/variables/higher_order_ops.py", line 4065, in create_scalar
if query.call_obj_hasattr(tx, "_local_tensor").value:
There was a problem hiding this comment.
Sorry, forgot to upstream the latest version. Just updated the PR. You can reproduce the issue with
pytest test/distributed/tensor/test_flex_attention.py -k test_flex_attention_replicate
…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
|
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):
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @Lucaskabela @wanchaol @tianyu-l @wz337 @XilunWu @d4l3k @pragupta @SherlockNoMad @ppwwyyxx