code: def state_dict_all_gather_keys(
      state_dict: Dict[str, Union[torch.Tensor, ShardedTensor]],
      pg: ProcessGroup,
  ) -> List[str]:
      """
      Gathers all the keys of the state_dict from all ranks. Can handle ShardedTensors in the state_dict.
  
      Args:
          state_dict (Dict[str, Union[torch.Tensor, ShardedTensor]]): keys of this state_dict will be gathered
          pg (ProcessGroup): Process Group used for comms
      """
      names = list(state_dict.keys())
      all_names = [None] * dist.get_world_size(pg)
      dist.all_gather_object(all_names, names, pg)
      deduped_names = set()
      for local_names in all_names:
          # pyre-ignore[16]
          for name in local_names:
              deduped_names.add(name)
      return sorted(deduped_names)
  
imports: from typing import Dict, List, Union
  import torch
  from torch import distributed
  from torch.distributed import ProcessGroup
  from torch.distributed._shard.sharded_tensor import Shard, ShardedTensor
label_desc: Use image node_id 2 for calling a global function with example usage: state_dict_all_gather_keys(state_dict, pg) and returns: sorted