MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / all_gather

Function all_gather

util/misc.py:114–154  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

112
113
114def all_gather(data):
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('cuda')
130
131 # obtain Tensor size of each rank
132 local_size = torch.tensor([tensor.numel()], device='cuda')
133 size_list = [torch.tensor([0], device='cuda') 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 tensor_list = []
139 for _ in size_list:
140 tensor_list.append(
141 torch.empty((max_size, ), dtype=torch.uint8, device='cuda'))
142 if local_size != max_size:
143 padding = torch.empty(size=(max_size - local_size, ),
144 dtype=torch.uint8,
145 device='cuda')
146 tensor = torch.cat((tensor, padding), dim=0)
147 dist.all_gather(tensor_list, tensor)
148
149 data_list = []
150 for size, tensor in zip(size_list, tensor_list):
151 buffer = tensor.cpu().numpy().tobytes()[:size]
152 data_list.append(pickle.loads(buffer))
153
154 return data_list
155
156
157def reduce_dict(input_dict, average=True):

Callers

nothing calls this directly

Calls 2

get_world_sizeFunction · 0.70
toMethod · 0.45

Tested by

no test coverage detected