code: def all_gather_object(object_list, obj, group=None):
      """
      复制 pytorch 的代码，使得可以版本兼容低版本的 pytorch 。
  
      Example::
          >>> # Note: Process group initialization omitted on each rank.
          >>> # Assumes world_size of 3.
          >>> gather_objects = ["foo", 12, {1: 2}] # any picklable object
          >>> output = [None for _ in gather_objects]
          >>> all_gather_object(output, gather_objects[dist.get_rank()])
          >>> output
          ['foo', 12, {1: 2}]
  
      :param object_list:
      :param obj:
      :param group:
      :return:
      """
      if int(os.environ.get(FASTNLP_NO_SYNC, "0")) == 2:
          return [obj]
  
      if dist.distributed_c10d._rank_not_in_group(group):
          return
      if _TORCH_GREATER_EQUAL_1_8:
          current_device = torch.device("cpu")
          is_nccl_backend = _check_for_nccl_backend(group)
          if is_nccl_backend:
              # See note about using torch.cuda.current_device() here in docstring.
              # We cannot simply use my_rank since rank == device is not necessarily
              # true.
              current_device = torch.device(
                  "cuda", torch.cuda.current_device()
              )
      else:
          current_device = torch.cuda.current_device()
  
      input_tensor, local_size = _object_to_tensor(
          obj, device=current_device
      )
  
      # Gather all local sizes. This is so that we can find the max size, and index
      # until the correct size when deserializing the tensors.
      group_size = dist.get_world_size(group=group)
      object_sizes_tensor = torch.zeros(
          group_size, dtype=torch.long, device=current_device
      )
      object_size_list = [
          object_sizes_tensor[i].unsqueeze(dim=0)
          for i in range(group_size)
      ]
      # Allgather tensor sizes
      dist.all_gather(object_size_list, local_size, group=group)
      max_object_size = int(max(object_size_list).item())  # type: ignore[type-var]
      # Resize tensor to max size across all ranks.
      input_tensor.resize_(max_object_size)
      coalesced_output_tensor = torch.empty(
          max_object_size * group_size,
          dtype=torch.uint8,
          device=current_device,
      )
      # Output tensors are nonoverlapping views of coalesced_output_tensor
      output_tensors = [
          coalesced_output_tensor[
              max_object_size * i : max_object_size * (i + 1)
          ]
          for i in range(group_size)
      ]
      dist.all_gather(output_tensors, input_tensor, group=group)
      # Deserialize outputs back to object.
      for i, tensor in enumerate(output_tensors):
          tensor = tensor.type(torch.uint8)
          if tensor.device != torch.device("cpu"):
              tensor = tensor.cpu()
          tensor_size = object_size_list[i]
          object_list[i] = _tensor_to_object(tensor, tensor_size)
      return object_list
  
file_path: fastNLP/fastNLP/core/drivers/torch_driver/dist_utils.py
imports: import io
  import pickle
  import os
  from typing import Any, List
  from fastNLP.envs.imports import _TORCH_GREATER_EQUAL_1_8
  from fastNLP.core.utils.torch_utils import DEFAULT_TORCH_GROUP
  from fastNLP.envs.imports import _NEED_IMPORT_TORCH
  from fastNLP.envs.env import FASTNLP_NO_SYNC
  from fastNLP.core.utils import apply_to_collection
label_desc: Use image node_id 10 for calling a global function with example usage: all_gather_object(object_list, obj, group) and returns: object_list, list