MCPcopy Create free account
hub / github.com/Kitware/COAT / reduce_dict

Function reduce_dict

utils/utils.py:242–267  ·  view source on GitHub ↗

Reduce the values in the dictionary from all processes so that all processes have the averaged results. Returns a dict with the same fields as input_dict, after reduction. Args: input_dict (dict): all the values will be reduced average (bool): whether to do average

(input_dict, average=True)

Source from the content-addressed store, hash-verified

240
241
242def reduce_dict(input_dict, average=True):
243 """
244 Reduce the values in the dictionary from all processes so that all processes
245 have the averaged results. Returns a dict with the same fields as
246 input_dict, after reduction.
247
248 Args:
249 input_dict (dict): all the values will be reduced
250 average (bool): whether to do average or sum
251 """
252 world_size = get_world_size()
253 if world_size < 2:
254 return input_dict
255 with torch.no_grad():
256 names = []
257 values = []
258 # sort the keys so that they are consistent across processes
259 for k in sorted(input_dict.keys()):
260 names.append(k)
261 values.append(input_dict[k])
262 values = torch.stack(values, dim=0)
263 dist.all_reduce(values)
264 if average:
265 values /= world_size
266 reduced_dict = {k: v for k, v in zip(names, values)}
267 return reduced_dict
268
269
270def setup_for_distributed(is_master):

Callers 1

train_one_epochFunction · 0.90

Calls 1

get_world_sizeFunction · 0.85

Tested by

no test coverage detected