Run all_gather on arbitrary picklable data (not necessarily tensors) Args: data: any picklable object Returns: list[data]: list of data gathered from each rank
(data, device)
| 112 | |
| 113 | # Function from https://github.com/facebookresearch/detr/blob/master/util/misc.py |
| 114 | def all_gather_pickle(data, device): |
| 115 | """ |
| 116 | Run all_gather on arbitrary picklable data (not necessarily tensors) |
| 117 | Args: |
| 118 | data: any picklable object |
| 119 | Returns: |
| 120 | list[data]: list of data gathered from each rank |
| 121 | """ |
| 122 | world_size = get_world_size() |
| 123 | if world_size == 1: |
| 124 | return [data] |
| 125 | |
| 126 | # serialized to a Tensor |
| 127 | buffer = pickle.dumps(data) |
| 128 | storage = torch.ByteStorage.from_buffer(buffer) |
| 129 | tensor = torch.ByteTensor(storage).to(device) |
| 130 | |
| 131 | # obtain Tensor size of each rank |
| 132 | local_size = torch.tensor([tensor.numel()], device=device) |
| 133 | size_list = [torch.tensor([0], device=device) for _ in range(world_size)] |
| 134 | dist.all_gather(size_list, local_size) |
| 135 | size_list = [int(size.item()) for size in size_list] |
| 136 | max_size = max(size_list) |
| 137 | |
| 138 | # receiving Tensor from all ranks |
| 139 | # we pad the tensor because torch all_gather does not support |
| 140 | # gathering tensors of different shapes |
| 141 | tensor_list = [] |
| 142 | for _ in size_list: |
| 143 | tensor_list.append(torch.empty((max_size,), dtype=torch.uint8, device=device)) |
| 144 | if local_size != max_size: |
| 145 | padding = torch.empty( |
| 146 | size=(max_size - local_size,), dtype=torch.uint8, device=device |
| 147 | ) |
| 148 | tensor = torch.cat((tensor, padding), dim=0) |
| 149 | dist.all_gather(tensor_list, tensor) |
| 150 | |
| 151 | data_list = [] |
| 152 | for size, tensor in zip(size_list, tensor_list): |
| 153 | buffer = tensor.cpu().numpy().tobytes()[:size] |
| 154 | data_list.append(pickle.loads(buffer)) |
| 155 | |
| 156 | return data_list |
| 157 | |
| 158 | |
| 159 | def all_gather_dict(data): |
nothing calls this directly
no test coverage detected