code: def _gather(input_, pg: ProcessGroup, dim=-1):
      # skip if only one rank involved
      world_size = pg.tp_world_size()
      if world_size == 1:
          return input_
  
      # all gather
      rank = pg.tp_local_rank()
      tensor_list = [
          torch.empty_like(input_) for _ in range(world_size)
      ]
      tensor_list[rank] = input_
      assert input_.device.type == "cuda"
      group = pg.tp_process_group()
      torch.distributed.all_gather(tensor_list, input_, group=group)
  
      # concat
      output = torch.cat(tensor_list, dim=dim).contiguous()
  
      return output
  
imports: from typing import List, Optional, Union
  import torch
  import torch.distributed
  from colossalai.legacy.global_variables import tensor_parallel_env
  from colossalai.legacy.nn.layer.utils import divide
  from colossalai.legacy.tensor import ColoTensorSpec, ProcessGroup
  from colossalai.tensor import ColoTensor
label_desc: Use image node_id 8 for calling a global function with example usage: _gather(input_, pg, dim) and returns: output, input_