code: def init_from_env(
      *,
      device_type: Optional[str] = None,
      dist_init_method_type: Literal["env", "tcp", "file"] = "env",
      pg_backend: Optional[str] = None,
      pg_timeout: timedelta = default_pg_timeout,
      float32_matmul_precision: str = "high",
  ) -> torch.device:
      """Utility function that initializes the device and process group, if applicable.
  
      The global process group is initialized only if:
          - torch.distributed is available is not already initialized
          - the program has been launched on multiple processes
  
      This is intended as a convenience to include at the beginning of scripts that follow
      a SPMD-style execution model.
  
  
      Args:
          device_type (str, optional): Device type to initialize. If None, device will be initialized
                                    based on environment
          dist_init_method_type (str, optional): Method to initialize the process group. Must be one of "env", "tcp", or "file".
              For more information, see here: https://pytorch.org/docs/stable/distributed.html#initialization
          pg_backend (str, optional): The process group backend to use. If None, it will use the
                                      default process group backend from the device
          pg_timeout (timedelta, optional): Timeout for operations executed against the process
                                            group. Default value equals 30 minutes
          float32_matmul_precision (str, optional): The setting for torch's precision of matrix multiplications.
  
      Returns:
          The current device.
      """
      device = (
          torch.device("cpu")
          if device_type == "cpu"
          else get_device_from_env()
      )
  
      if device_type is not None and device.type != device_type:
          raise RuntimeError(
              f"Device type is specified to {device_type} but got {device.type} from env"
          )
  
      if _check_dist_env():
          if not torch.distributed.is_available():
              _log.warning(
                  "torch.distributed is not available. Skipping initializing the process group."
              )
              return device
          if torch.distributed.is_initialized():
              _log.warning(
                  "torch.distributed is already initialized. Skipping initializing the process group."
              )
              return device
          pg_backend = (
              pg_backend
              if pg_backend is not None
              else get_process_group_backend_from_device(device)
          )
          init_method: Optional[str] = None
          if dist_init_method_type == "tcp":
              init_method = get_tcp_init_method()
          elif dist_init_method_type == "file":
              init_method = get_file_init_method()
          torch.distributed.init_process_group(
              init_method=init_method,
              backend=pg_backend,
              timeout=pg_timeout,
          )
      set_float32_precision(float32_matmul_precision)
      return device
  
file_path: tnt/torchtnt/utils/env.py
imports: import logging
  import os
  import random
  from datetime import timedelta
  from typing import Optional, Union
  import numpy
  import torch
  from torch.distributed.constants import default_pg_timeout
  from torchtnt.utils.device import get_device_from_env, set_float32_precision
  from torchtnt.utils.distributed import get_file_init_method, get_process_group_backend_from_device, get_tcp_init_method
  from typing_extensions import Literal
label_desc: Use image node_id 2 for calling a global function with example usage: init_from_env() and returns: device, device, device