Datasets:
File size: 898 Bytes
453129d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from typing import List, Optional, Tuple
import torch
import torch.distributed as dist
def solution(
x: torch.Tensor,
gather_dim: int,
group: Optional[dist.ProcessGroup] = None,
) -> torch.Tensor:
group = group or dist.group.WORLD
world_size = dist.get_world_size(group)
if world_size == 1:
return x.contiguous()
device = x.device
dtype = x.dtype
x = x.contiguous()
x_size = torch.tensor(x.size(), dtype=torch.int64, device=device)
size_list = [torch.zeros(x_size.size(), dtype=torch.int64, device=device) for _ in range(world_size)]
dist.all_gather(size_list, x_size, group=group)
tensor_list = [
torch.empty(torch.Size(size_list[i].tolist()), dtype=dtype, device=device)
for i in range(world_size)
]
dist.all_gather(tensor_list, x, group=group)
return torch.cat(tensor_list, dim=gather_dim).contiguous()
|