class_docstr: Represents a device on a remote worker.
  
  Args:
      remote_device (str or torch.device): Represents a device on a remote worker.
          The string format should be one of the following:
  
              1. "<workername>/<device>", where the device field can be parsed as torch.device type.
                 E.g., "trainer0/cpu", "trainer0", "ps0/cuda:0".
                 In addition, the device field can be optional and the default value is "cpu".
              2. "rank:<rank>/<device>", where <rank> is the rank of the
                 process and device can be parsed as torch.device type.
                 E.g., "rank:0/cpu", "rank:0", "rank:0/cuda:0"
              3. <workername> and <rank> are optional and formats like "cpu"
                  and "cuda:1", just represent local devices.
class_docstr_tok: ["Represents","a","device","on","a","remote","worker",".","Args",":","remote_device","(","str","or","torch.device",")",":","Represents","a","device","on","a","remote","worker",".","The","string","format","should","be","one","of","the","following",":","1",".","``","<","workername",">","\/","<","device",">","''",",","where","the","device","field","can","be","parsed","as","torch.device","type",".","E.g.",",","``","trainer0\/cpu","''",",","``","trainer0","''",",","``","ps0\/cuda:0","''",".","In","addition",",","the","device","field","can","be","optional","and","the","default","value","is","``","cpu","''",".","2",".","``","rank",":","<","rank",">","\/","<","device",">","''",",","where","<","rank",">","is","the","rank","of","the","process","and","device","can","be","parsed","as","torch.device","type",".","E.g.",",","``","rank:0\/cpu","''",",","``","rank:0","''",",","``","rank:0\/cuda:0","''","3",".","<","workername",">","and","<","rank",">","are","optional","and","formats","like","``","cpu","''","and","``","cuda:1","''",",","just","represent","local","devices","."]
code: def __init__(self, remote_device: Union[str, torch.device]):
      PARSE_ERROR = (
          f"Could not parse remote_device: {remote_device}. The valid format is "
          "'<workername>/<device>' or 'rank:<rank>/<device>' or '<device>'"
      )
      self._worker_name = None
      self._rank = None
      self._device: Optional[Union[str, int, torch.device]] = None
  
      if isinstance(remote_device, torch.device):
          self._device = remote_device
      elif isinstance(remote_device, str):
          fields = remote_device.split("/")
          if len(fields) == 2:
              self._worker_name, self._device = fields
          elif len(fields) == 1:
              # Check if this is a valid device.
              if _remote_device._is_valid_local_device(fields[0]):
                  self._device = fields[0]
              else:
                  self._worker_name = fields[0]
                  self._device = "cpu"
          else:
              raise ValueError(PARSE_ERROR)
      else:
          raise TypeError(
              f"Invalid type for remote_device: {type(remote_device)}"
          )
  
      # Do some basic sanity check (no empty string)
      if self._worker_name is not None and not self._worker_name:
          raise ValueError(PARSE_ERROR)
  
      # Validate the device.
      self._device = torch.device(self._device)
  
      # Check for rank based format.
      if self._worker_name is not None:
          fields = self._worker_name.split(":")
          if len(fields) == 2:
              # rank:<rank>/device format, extract rank
              if fields[0] == "rank" and fields[1].isdigit():
                  self._rank = int(fields[1])  # type: ignore[assignment]
                  self._worker_name = None
              else:
                  raise ValueError(PARSE_ERROR)
          elif len(fields) > 2:
              raise ValueError(PARSE_ERROR)
  
file_path: pytorch/torch/distributed/remote_device.py
imports: from typing import Optional, Union
  import torch
label_desc: Use image node_id 1 to create a new _remote_device object  with example: obj = _remote_device(remote_device)