code: def intra_and_cross_node_pg(
      device: Optional[torch.device] = None,
      backend: Optional[str] = None,
  ) -> Tuple[Optional[dist.ProcessGroup], Optional[dist.ProcessGroup]]:
      """
      Creates sub process groups (intra and cross node)
      """
      if device is not None and device.type == "meta":
          return None, None
  
      global _INTRA_PG  # intra node process group
      global _CROSS_PG  # cross node process group
  
      my_size = dist.get_world_size()
      my_rank = dist.get_rank()
      my_local_rank = get_local_rank(my_size, my_rank)
      local_size = get_local_size(my_size)
      my_group_rank = get_group_rank(my_size, my_rank)
      group_count = get_num_groups(my_size)
      if backend is None:
          backend = dist.get_backend()
  
      logger.info(
          f"[{my_rank}] my_local_rank = {my_local_rank}, local_size = {local_size},"
          f"my_group_rank = {my_group_rank}, group_count = {group_count}, backend = {backend}"
      )
      if _INTRA_PG is None:
          for group_rank in range(group_count):
              peers = [
                  group_rank * local_size + r for r in range(local_size)
              ]
              curr_intra_group_pg = dist.new_group(
                  backend=backend, ranks=peers
              )
              if my_group_rank == group_rank:
                  logger.warning(
                      "[Connection] intra_group: [%d] -> [%s]"
                      % (my_rank, peers)
                  )
                  _INTRA_PG = curr_intra_group_pg
  
      dist.barrier()
  
      if _CROSS_PG is None:
          for l_rank in range(local_size):
              peers = [
                  l_rank + g * local_size for g in range(group_count)
              ]
              curr_cross_group_pg = dist.new_group(
                  backend=backend, ranks=peers
              )
              if l_rank == my_local_rank:
                  logger.warning(
                      "[Connection] cross_group: [%d] -> [%s]"
                      % (my_rank, peers)
                  )
                  _CROSS_PG = curr_cross_group_pg
  
      dist.barrier()
  
      return _INTRA_PG, _CROSS_PG
  
file_path: torchrec/torchrec/distributed/comm.py
imports: import logging
  import os
  from typing import List, Optional, Tuple
  import torch
  import torch.distributed
label_desc: Use image node_id 6 for calling a global function with example usage: intra_and_cross_node_pg(device, backend) and returns: _INTRA_PG,  _CROSS_PG, None,  None