code: def reduce_dict(input_dict, average=True):
      """
      Args:
          input_dict (dict): all the values will be reduced
          average (bool): whether to do average or sum
      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.
      """
      world_size = get_world_size()
      if world_size < 2:  # 单GPU的情况
          return input_dict
      with torch.no_grad():  # 多GPU的情况
          names = []
          values = []
          # sort the keys so that they are consistent across processes
          for k in sorted(input_dict.keys()):
              names.append(k)
              values.append(input_dict[k])
          values = torch.stack(values, dim=0)
          dist.all_reduce(values)
          if average:
              values /= world_size
  
          reduced_dict = {k: v for k, v in zip(names, values)}
          return reduced_dict
  
imports: from collections import defaultdict, deque
  import datetime
  import pickle
  import time
  import errno
  import os
  from contextlib import contextmanager
  import torch
  import torch.distributed
label_desc: Use image node_id 2 for calling a global function with example usage: reduce_dict(input_dict, average) and returns: input_dict, reduced_dict