Skip to content

[cp] dispatch flex_attention_backward to CP impl in TorchDispatchMode #152311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: gh/XilunWu/139/base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update on "[cp] dispatch flex_attention_backward to CP impl in TorchD…
…ispatchMode"

cc H-Huang awgu wanchaol fegin fduwjj wz337 wconstab d4l3k

[ghstack-poisoned]
  • Loading branch information
XilunWu committed May 27, 2025
commit ba1749efd7c1ce5f8616852e75037c9f58846d11
7 changes: 7 additions & 0 deletions test/distributed/tensor/test_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,10 @@ def causal_mask(b, h, q_idx, kv_idx):
)
cp_out.sum().backward()

cp_q.requires_grad = False
cp_k.requires_grad = False
cp_v.requires_grad = False

self.assertDictEqual(
comm_mode.get_comm_counts(),
{
Expand All @@ -589,6 +593,9 @@ def causal_mask(b, h, q_idx, kv_idx):
torch.testing.assert_close(cp_k_grad, k.grad, atol=1e-6, rtol=1e-2)
torch.testing.assert_close(cp_v_grad, v.grad, atol=1e-6, rtol=1e-2)

# reset to the default mode
_set_dispatch_mode("monkey_patch")


if __name__ == "__main__":
run_tests()
13 changes: 4 additions & 9 deletions torch/distributed/tensor/experimental/_attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -1620,12 +1620,6 @@ def context_parallel(

for buffer, original_buffer in zip(buffers, original_buffers):
if original_buffer is not None:
# tensor cannot resize if requires_grad is True
# key and value's requires_grad has been set to False in manual comm calls
# unless via DTensor.
if buffer.requires_grad:
buffer.requires_grad = False

buffer.resize_(original_buffer.shape)
buffer.copy_(original_buffer)

Expand Down Expand Up @@ -1830,7 +1824,8 @@ def cp_flex_attention_backward_dispatch_mode(

# TODO: save global KV in forward
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to save global KVs? Would it cause OOM?

# all-gather KV
sharding = Shard(2)
seq_dim = 2
sharding = Shard(seq_dim)
k_dist = DTensor.from_local(key, device_mesh, [sharding])
v_dist = DTensor.from_local(value, device_mesh, [sharding])
k_global = k_dist.full_tensor()
Expand Down Expand Up @@ -1862,10 +1857,10 @@ def cp_flex_attention_backward_dispatch_mode(

# reduce-scatter KV grads
grad_key = ft_c.reduce_scatter_tensor(
grad_key, reduceOp="sum", scatter_dim=2, group=device_mesh
grad_key, reduceOp="sum", scatter_dim=seq_dim, group=device_mesh
)
grad_value = ft_c.reduce_scatter_tensor(
grad_value, reduceOp="sum", scatter_dim=2, group=device_mesh
grad_value, reduceOp="sum", scatter_dim=seq_dim, group=device_mesh
)

return grad_query, grad_key, grad_value, grad_score_mod_captured
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy