
      i#                     Z    d dl Z d dlZd dlmZ d dlmZ d dlmZ  G d de      Zde	fdZ
y)	    N)override)ClusterEnvironment)rank_zero_onlyc                   B    e Zd ZdZd fdZeedefd              Zeede	fd              Z
eedefd              Zeedefd              Zedefd	       Zed
eddfd       Zedefd       Zededdfd       Zedefd       Zedefd       Zedd       Z xZS )LightningEnvironmentaX  The default environment used by Lightning for a single node or free cluster (not managed).

    There are two modes the Lightning environment can operate with:

    1.  The user only launches the main process by :code:`python train.py ...` with no additional environment variables
        set. Lightning will spawn new worker processes for distributed training in the current node.
    2.  The user launches all processes manually or with utilities like :code:`torch.distributed.launch`.
        The appropriate environment variables need to be set, and at minimum :code:`LOCAL_RANK`.

    If the main address and port are not provided, the default environment will choose them
    automatically. It is recommended to use this default environment for single-node distributed
    training as it provides a convenient way to launch the training script.

    returnNc                 L    t         |           d| _        d| _        d| _        y )Nr      )super__init__
_main_port_global_rank_world_size)self	__class__s    /Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/fabric/plugins/environments/lightning.pyr   zLightningEnvironment.__init__(   s%    !!" !    c                 &    dt         j                  v S )a  Returns whether the cluster creates the processes or not.

        If at least :code:`LOCAL_RANK` is available as environment variable, Lightning assumes the user acts as the
        process launcher/job scheduler and Lightning will not launch new processes.

        
LOCAL_RANKosenvironr   s    r   creates_processes_externallyz1LightningEnvironment.creates_processes_externally.   s     rzz))r   c                 B    t         j                  j                  dd      S )NMASTER_ADDRz	127.0.0.1)r   r   getr   s    r   main_addressz!LightningEnvironment.main_address9   s     zz~~m[99r   c                     | j                   dk(  r=dt        j                  v rt        t        j                  d         n	t	               | _         | j                   S )Nr
   MASTER_PORT)r   r   r   intfind_free_network_portr   s    r   	main_portzLightningEnvironment.main_port>   sE     ??b 2?2::2MBJJ}-.SiSk O r   c                       y)NT r&   r   r   detectzLightningEnvironment.detectG   s     r   c                     | j                   S Nr   r   s    r   
world_sizezLightningEnvironment.world_sizeL   s    r   sizec                     || _         y r)   r*   )r   r,   s     r   set_world_sizez#LightningEnvironment.set_world_sizeP   s
    r   c                     | j                   S r)   )r   r   s    r   global_rankz LightningEnvironment.global_rankT   s       r   rankc                 (    || _         |t        _        y r)   )r   r   r1   )r   r1   s     r   set_global_rankz$LightningEnvironment.set_global_rankX   s     "r   c                 T    t        t        j                  j                  dd            S )Nr   r   )r"   r   r   r   r   s    r   
local_rankzLightningEnvironment.local_rank]   s    2::>>,233r   c                     t         j                  j                  dd      }t        t         j                  j                  d|            S )N
GROUP_RANKr   	NODE_RANK)r   r   r   r"   )r   
group_ranks     r   	node_rankzLightningEnvironment.node_ranka   s0    ZZ^^L!4
2::>>+z:;;r   c                 L    dt         j                  v rt         j                  d= y y )N
WORLD_SIZEr   r   s    r   teardownzLightningEnvironment.teardownf   s    2::%

<( &r   )r   N)__name__
__module____qualname____doc__r   propertyr   boolr   strr   r"   r$   staticmethodr'   r+   r.   r0   r3   r5   r:   r=   __classcell__)r   s   @r   r   r      s]   " *d *  * :c :  : 3    D     C      3  4     !S ! ! #C #D # # 4C 4 4 <3 < < ) )r   r   r   c                  2   dt         j                  v rt         j                  d   } t        |       S t        j                  t        j                  t        j
                        }|j                  d       |j                         d   }|j                          |S )a  Finds a free port on localhost.

    If the environment variable `STANDALONE_PORT` is set, its value is used as the port number.

    It is useful in single-node training when we don't want to connect to a real main node but have to set the
    `MASTER_PORT` environment variable.

    STANDALONE_PORT) r   r   )	r   r   r"   socketAF_INETSOCK_STREAMbindgetsocknameclose)_portsports      r   r#   r#   l   sl     BJJ&

,-5zfnnf&8&89AFF7O==?1DGGIKr   )r   rJ   typing_extensionsr   9lightning.fabric.plugins.environments.cluster_environmentr   $lightning.fabric.utilities.rank_zeror   r   r"   r#   r&   r   r   <module>rV      s2    
  & X ?Q)- Q)h r   