| 3 | from typing import List |
| 4 | |
| 5 | class TorchCommunicator: |
| 6 | |
| 7 | def __init__(self, |
| 8 | process_group, |
| 9 | to_global_rank=lambda rank: rank, |
| 10 | dp_rank=None, |
| 11 | comm_group_size=None,): |
| 12 | self.process_group = process_group |
| 13 | self.to_global_rank = to_global_rank |
| 14 | self.dp_rank = dp_rank |
| 15 | self.comm_group_size = comm_group_size |
| 16 | |
| 17 | # @staticmethod |
| 18 | def barrier(self): |
| 19 | dist.barrier(group=self.process_group) |
| 20 | |
| 21 | def send(self, |
| 22 | tensor: torch.Tensor, |
| 23 | dst: int, |
| 24 | stream=None): |
| 25 | # print("Send tensor of size:", torch.numel(tensor)) |
| 26 | if tensor.device == torch.device('cpu'): |
| 27 | dist.send(tensor, self.to_global_rank(dst), group=self.process_group) |
| 28 | else: |
| 29 | dist.send(tensor.cpu(), self.to_global_rank(dst), group=self.process_group) |
| 30 | |
| 31 | def recv(self, |
| 32 | tensor: torch.Tensor, |
| 33 | src: int, |
| 34 | stream=None): |
| 35 | |
| 36 | if tensor.device == torch.device('cpu'): |
| 37 | dist.recv(tensor, self.to_global_rank(src), group=self.process_group) |
| 38 | else: |
| 39 | buffer = tensor.cpu() |
| 40 | dist.recv(buffer, self.to_global_rank(src), group=self.process_group) |
| 41 | tensor[:] = buffer.to(tensor.device) |
| 42 | |
| 43 | def isend(self, |
| 44 | tensor: torch.Tensor, |
| 45 | dst: int, |
| 46 | stream=None): |
| 47 | # print("Send tensor of size:", torch.numel(tensor)) |
| 48 | if tensor.device == torch.device('cpu'): |
| 49 | handler = dist.isend(tensor, self.to_global_rank(dst), group=self.process_group) |
| 50 | else: |
| 51 | handler = dist.isend(tensor.cpu(), self.to_global_rank(dst), group=self.process_group) |
| 52 | return handler |
| 53 | |
| 54 | def irecv(self, |
| 55 | tensor: torch.Tensor, |
| 56 | src: int, |
| 57 | stream=None): |
| 58 | if tensor.device == torch.device('cpu'): |
| 59 | handler = dist.irecv(tensor, self.to_global_rank(src), group=self.process_group) |
| 60 | else: |
| 61 | assert False |
| 62 | buffer = tensor.cpu() |
no outgoing calls
no test coverage detected