class_docstr: Collective operations using `torch.distributed <https://pytorch.org/docs/stable/distributed.html>`__.
  
  .. warning:: This is an :ref:`experimental <versioning:Experimental API>` feature which is still in development.
class_docstr_tok: ["Collective","operations","using","`","torch.distributed","<","https",":","\/\/pytorch.org\/docs\/stable\/distributed.html",">","`","__",".","..","warning",":",":","This","is","an",":","ref",":","`","experimental","<","versioning",":","Experimental","API",">","`","feature","which","is","still","in","development","."]
code: def setup(
      self,
      main_address: Optional[str] = None,
      main_port: Optional[str] = None,
      **kwargs: Any,
  ) -> Self:
      if self.is_initialized():
          return self
      # maybe set addr
      set_addr = False
      addr_key = "MASTER_ADDR"
      if main_address is not None and addr_key not in os.environ:
          os.environ[addr_key] = main_address
          set_addr = True
      # maybe set port
      set_port = False
      port_key = "MASTER_PORT"
      if main_port is not None and port_key not in os.environ:
          os.environ[port_key] = str(main_port)
          set_port = True
      # this will `init_group`
      super().setup(**kwargs)
      # set as a class attribute so any instance can know whether we initialized the default process group
      TorchCollective.manages_default_group = True
      # cleanup
      if set_addr:
          os.environ.pop("MASTER_ADDR", None)
      if set_port:
          os.environ.pop("MASTER_PORT", None)
      return self
  
imports: import datetime
  import os
  from typing import Any, List, Optional, Union
  import torch
  import torch.distributed
  from torch import Tensor
  from typing_extensions import Self, override
  from lightning.fabric.plugins.collectives.collective import Collective
  from lightning.fabric.utilities.imports import _TORCH_GREATER_EQUAL_1_13
  from lightning.fabric.utilities.types import CollectibleGroup, RedOpType, ReduceOp
label_desc: Use image node_id 21 for calling the TorchCollective obj's underlying member method code with example usage: obj.setup(main_address, main_port) and returns: self, self