Skip to content

Commit

Permalink
[BE] [c10d][send] Improve error message on dist.send() with destinati…
Browse files Browse the repository at this point in the history
…on rank as itself (#89004)

This improves error msg on dist.send() and add corresponding test in test_c10d_common.py(https://github.com/pytorch/pytorch/blob/master/test/distributed/test_c10d_common.py).
Context in issue#83912: #83912

Pull Request resolved: #89004
Approved by: https://github.com/H-Huang
  • Loading branch information
wz337 authored and pytorchmergebot committed Nov 15, 2022
1 parent 21dd311 commit 68fd8f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions test/distributed/test_c10d_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,9 @@ def test_send_recv(self):
dist.send(input_tensor, (self.rank + 1) % self.world_size)
self.assertEqual(input_tensor, torch.zeros(2, 2) + 1)

with self.assertRaises(ValueError):
dist.send(input_tensor, dist.get_rank())

# test recv
input_tensor = torch.zeros(2, 2)
dist.recv(input_tensor, (self.rank + 1) % self.world_size)
Expand Down
9 changes: 8 additions & 1 deletion torch/distributed/distributed_c10d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,12 +1179,19 @@ def send(tensor: torch.Tensor, dst: int, group: Optional[ProcessGroup] = None, t
Args:
tensor (Tensor): Tensor to send.
dst (int): Destination rank.
dst (int): Destination rank. Destination rank should not be the same
as the rank of the current process.
group (ProcessGroup, optional): The process group to work on. If None,
the default process group will be used.
tag (int, optional): Tag to match send with remote recv
"""
if get_rank() == dst:
raise ValueError(
"Invalid destination rank: destination rank should not be the same as "
"the rank of the current process."
)

_check_single_tensor(tensor, "tensor")
if _rank_not_in_group(group):
_warn_not_in_group("send")
Expand Down

0 comments on commit 68fd8f3

Please sign in to comment.