a
    þdN  ã                   @   sJ   d dl Z d dlZd dlmZ d dlmZ G dd„ deƒZedœdd„ZdS )	é    N)ÚClusterEnvironment)Úrank_zero_onlyc                       sÔ   e Zd ZdZddœ‡ fdd„Zeedœdd„ƒZeedœdd	„ƒZ	ee
dœd
d„ƒZeedœdd„ƒZe
dœdd„Ze
ddœdd„Ze
dœdd„Ze
ddœdd„Ze
dœdd„Ze
dœdd„Zddœdd„Z‡  ZS )ÚLightningEnvironmentaW  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.
    N©Úreturnc                    s    t ƒ  ¡  d| _d| _d| _d S )Néÿÿÿÿr   é   )ÚsuperÚ__init__Ú
_main_portÚ_global_rankÚ_world_size©Úself©Ú	__class__© úx/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/plugins/environments/lightning.pyr
   %   s    
zLightningEnvironment.__init__c                 C   s
   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Úenvironr   r   r   r   Úcreates_processes_externally+   s    z1LightningEnvironment.creates_processes_externallyc                 C   s   t j dd¡S )NZMASTER_ADDRz	127.0.0.1)r   r   Úgetr   r   r   r   Úmain_address4   s    z!LightningEnvironment.main_addressc                 C   s&   | j dkr ttj dtƒ ¡ƒ| _ | j S )Nr   ZMASTER_PORT)r   Úintr   r   r   Úfind_free_network_portr   r   r   r   Ú	main_port8   s    
zLightningEnvironment.main_portc                   C   s   dS )NTr   r   r   r   r   Údetect>   s    zLightningEnvironment.detectc                 C   s   | j S ©N©r   r   r   r   r   Ú
world_sizeB   s    zLightningEnvironment.world_size)Úsizer   c                 C   s
   || _ d S r   r    )r   r"   r   r   r   Úset_world_sizeE   s    z#LightningEnvironment.set_world_sizec                 C   s   | j S r   )r   r   r   r   r   Úglobal_rankH   s    z LightningEnvironment.global_rank)Úrankr   c                 C   s   || _ |t_d S r   )r   r   r%   )r   r%   r   r   r   Úset_global_rankK   s    z$LightningEnvironment.set_global_rankc                 C   s   t tj dd¡ƒS )Nr   r   )r   r   r   r   r   r   r   r   Ú
local_rankO   s    zLightningEnvironment.local_rankc                 C   s    t j dd¡}tt j d|¡ƒS )NZ
GROUP_RANKr   Z	NODE_RANK)r   r   r   r   )r   Z
group_rankr   r   r   Ú	node_rankR   s    zLightningEnvironment.node_rankc                 C   s   dt jv rt jd= d S )NZ
WORLD_SIZEr   r   r   r   r   ÚteardownV   s    
zLightningEnvironment.teardown)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   ÚpropertyÚboolr   Ústrr   r   r   Ústaticmethodr   r!   r#   r$   r&   r'   r(   r)   Ú__classcell__r   r   r   r   r      s"   r   r   c                  C   s2   t   t jt j¡} |  d¡ |  ¡ d }|  ¡  |S )z¼Finds a free port on localhost.

    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.
    )Ú r   r   )ÚsocketÚAF_INETÚSOCK_STREAMÚbindÚgetsocknameÚclose)ÚsÚportr   r   r   r   [   s
    
r   )	r   r4   Z9lightning_fabric.plugins.environments.cluster_environmentr   Z$lightning_fabric.utilities.rank_zeror   r   r   r   r   r   r   r   Ú<module>   s
   E