Ë
      çiõ"  ã                   óÎ   — d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ  e j$                  e«      Z G d„ d	e«      Zd
efd„Zd
efd„Zy)é    N)ÚOptional)Úoverride)ÚClusterEnvironment)Ú_IS_WINDOWS)Úrank_zero_warn)ÚPossibleUserWarningc                   óì  ‡ — e Zd ZdZddedeej                     ddfˆ 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e   fd„«       Zede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ededdfd„«       Zededefd„«       Zedd„«       Zedd„«       Zˆ xZS )ÚSLURMEnvironmenta'  Cluster environment for training on a cluster managed by SLURM.

    You can configure the `main_address` and `main_port` properties via the env variables `MASTER_ADDR` and
    `MASTER_PORT`, respectively.

    Args:
        auto_requeue: Whether automatic job resubmission is enabled or not. How and under which conditions a job gets
            rescheduled gets determined by the owner of this plugin.
        requeue_signal: The signal that SLURM will send to indicate that the job should be requeued. Defaults to
            SIGUSR1 on Unix.

    NÚauto_requeueÚrequeue_signalÚreturnc                 ó®   •— t         ‰| �  «        || _        |€t        st        j
                  }|| _        | j                  «        | j                  «        y )N)	ÚsuperÚ__init__r   r   ÚsignalÚSIGUSR1r   Ú_validate_srun_usedÚ_validate_srun_variables)Úselfr   r   Ú	__class__s      €ú€/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning_fabric/plugins/environments/slurm.pyr   zSLURMEnvironment.__init__/   sG   ø€ Ü‰ÑÔØ(ˆÔØÐ!­+Ü#Ÿ^™^ˆNØ,ˆÔØ× Ñ Ô"Ø×%Ñ%Õ'ó    c                  ó   — y)NT© ©r   s    r   Úcreates_processes_externallyz-SLURMEnvironment.creates_processes_externally8   s   € ð r   c                 ó"  — t         j                  j                  d«      }|€Dt         j                  j                  dd«      }| j                  |«      }|t         j                  d<   t        j                  dt         j                  d   › �«       |S )NÚMASTER_ADDRÚSLURM_NODELISTz	127.0.0.1zMASTER_ADDR: )ÚosÚenvironÚgetÚresolve_root_node_addressÚlogÚdebug)r   Ú	root_nodeÚnodelists      r   Úmain_addresszSLURMEnvironment.main_address=   sr   € ô —J‘J—N‘N =Ó1ˆ	ØÐÜ—z‘z—~‘~Ð&6¸ÓDˆHØ×6Ñ6°xÓ@ˆIØ(1ŒB�J‰J�}Ñ%ä�	‰	�M¤"§*¡*¨]Ñ";Ð!<Ð=Ô>ØÐr   c                 ó\  — t         j                  j                  d«      }|�|dd  }t        |«      dz   }nd}dt         j                  v rt        t         j                  d   «      }nt	        |«      t         j                  d<   t
        j                  dt         j                  d   › �«       |S )NÚSLURM_JOB_IDéüÿÿÿi˜:  in2  ÚMASTER_PORTzMASTER_PORT: )r    r!   r"   ÚintÚstrr$   r%   )r   Újob_idÚdefault_ports      r   Ú	main_portzSLURMEnvironment.main_portI   s’   € ô —‘—‘ Ó/ˆØÐà! " #˜;ˆLä˜|Ó,¨uÑ4‰Là ˆLð œBŸJ™JÑ&ÜœrŸz™z¨-Ñ8Ó9‰Lä(+¨LÓ(9ŒB�J‰J�}Ñ%ä�	‰	�M¤"§*¡*¨]Ñ";Ð!<Ð=Ô>ØÐr   c                  ó>   — t         j                  «        t        «       S )aÒ  Returns ``True`` if the current process was launched on a SLURM cluster.

        It is possible to use the SLURM scheduler to request resources and then launch processes manually using a
        different environment. For this, the user can set the job name in SLURM to 'bash' or 'interactive' (srun --job-
        name=interactive). This will then avoid the detection of ``SLURMEnvironment`` and another environment can be
        detected automatically.

        )r
   r   Ú_is_srun_usedr   r   r   ÚdetectzSLURMEnvironment.detecte   s   € ô 	×,Ñ,Ô.Ü‹Ðr   c                  ó@   — t         j                  j                  d«      S )NÚSLURM_JOB_NAME)r    r!   r"   r   r   r   Újob_namezSLURMEnvironment.job_names   s   € ä�z‰z�~‰~Ð.Ó/Ð/r   c                  ó’   — t        «       ry t        j                  j                  d«      } | €y 	 t	        | «      S # t
        $ r Y y w xY w)Nr*   ©Ú_is_slurm_interactive_moder    r!   r"   r-   Ú
ValueError)r/   s    r   r/   zSLURMEnvironment.job_idw   sH   € ô &Ô'Øä—‘—‘ Ó/ˆØˆ>Øð	Ü�v“;ÐøÜò 	Ùð	ús   ¯
: º	AÁAc                 ó:   — t        t        j                  d   «      S ©NÚSLURM_NTASKS©r-   r    r!   r   s    r   Ú
world_sizezSLURMEnvironment.world_size…   ó   € ä”2—:‘:˜nÑ-Ó.Ð.r   Úsizec                 ó.   — t         j                  d«       y )Nz[SLURMEnvironment.set_world_size was called, but setting world size is not allowed. Ignored.©r$   r%   )r   rB   s     r   Úset_world_sizezSLURMEnvironment.set_world_size‰   s   € ä�	‰	ÐoÕpr   c                 ó:   — t        t        j                  d   «      S )NÚSLURM_PROCIDr?   r   s    r   Úglobal_rankzSLURMEnvironment.global_rank�   rA   r   Úrankc                 ó.   — t         j                  d«       y )Nz]SLURMEnvironment.set_global_rank was called, but setting global rank is not allowed. Ignored.rD   )r   rI   s     r   Úset_global_rankz SLURMEnvironment.set_global_rank‘   s   € ä�	‰	ÐqÕrr   c                 ó:   — t        t        j                  d   «      S )NÚSLURM_LOCALIDr?   r   s    r   Ú
local_rankzSLURMEnvironment.local_rank•   s   € ä”2—:‘:˜oÑ.Ó/Ð/r   c                 ó:   — t        t        j                  d   «      S )NÚSLURM_NODEIDr?   r   s    r   Ú	node_rankzSLURMEnvironment.node_rank™   rA   r   Únum_devicesÚ	num_nodesc           	      ó,  — t        «       ry t        j                  j                  d«      }|�#t	        |«      |k7  rt        d|› d|› d|› d�«      ‚t        j                  j                  d«      }|�$t	        |«      |k7  rt        d|› d|› d	|› d�«      ‚y y )
NÚSLURM_NTASKS_PER_NODEzYou set `devices=zX` in Lightning, but the number of tasks per node configured in SLURM `--ntasks-per-node=z%` does not match. HINT: Set `devices=z`.ÚSLURM_NNODESzYou set `num_nodes=zE` in Lightning, but the number of nodes configured in SLURM `--nodes=z'` does not match. HINT: Set `num_nodes=r9   )r   rR   rS   Úntasks_per_nodeÚnnodess        r   Úvalidate_settingsz"SLURMEnvironment.validate_settings�   s¼   € ä%Ô'ØÜŸ*™*Ÿ.™.Ð)@ÓAˆØÐ&¬3¨Ó+?À;Ò+NÜØ# K =ð 1'Ø'6Ð&7Ð7\Ð]lÐ\mÐmoðqóð ô —‘—‘ Ó/ˆØÐ¤# f£+°Ò":ÜØ% i [ð 1Ø#˜HÐ$KÈFÈ8ÐSUðWóð ð #;Ðr   Únodesc                 óª   — t        j                  dd| «      } t        j                  dd| «      } | j                  d«      d   j                  d«      d   S )a¢  The node selection format in SLURM supports several formats.

        This function selects the first host name from

        - a space-separated list of host names, e.g., 'host0 host1 host3' yields 'host0' as the root
        - a comma-separated list of host names, e.g., 'host0,host1,host3' yields 'host0' as the root
        - the range notation with brackets, e.g., 'host[5-9]' yields 'host5' as the root

        z\[(.*?)[,-].*\]z\1z	\[(.*?)\]Ú r   Ú,)ÚreÚsubÚsplit)rZ   s    r   r#   z*SLURMEnvironment.resolve_root_node_address®   sM   € ô —‘Ð)¨5°%Ó8ˆÜ—‘�| U¨EÓ2ˆØ�{‰{˜3Ó Ñ"×(Ñ(¨Ó-¨aÑ0Ð0r   c                  ó(  — t         ryt        j                  d«      du} | rrt        «       sgdj	                  dt
        j                  j                  t        j                  «      gt        j                  ¢«      dd }t        d|› d�t        ¬«       yyy)aU  Checks if the `srun` command is available and used.

        Parallel jobs (multi-GPU, multi-node) in SLURM are launched by prepending `srun` in front of the Python command.
        Not doing so will result in processes hanging, which is a frequent user error. Lightning will emit a warning if
        `srun` is found but not used.

        NÚsrunr\   é@   z§The `srun` command is available on your system but is not used. HINT: If your intention is to run Lightning on SLURM, prepend your python command with `srun` like so: z ...)Úcategory)r   ÚshutilÚwhichr3   Újoinr    ÚpathÚbasenameÚsysÚ
executableÚargvr   r   )Úsrun_existsÚhints     r   r   z$SLURMEnvironment._validate_srun_used½   sƒ   € õ Øä—l‘l 6Ó*°$Ð6ˆÙœ}œØ—8‘8˜V¤R§W¡W×%5Ñ%5´c·n±nÓ%EÐQÌÏÉÐQÓRÐSVÐTVÐWˆDÜðYØY]ÐX^Ð^bðdä,öð  /ˆ;r   c                  óª   — t        t        j                  j                  dd«      «      } | dkD  r%dt        j                  vrt	        d| › d| › d�«      ‚yy)	a2  Checks for conflicting or incorrectly set variables set through `srun` and raises a useful error message.

        Right now, we only check for the most common user errors. See
        `the srun docs <https://slurm.schedmd.com/srun.html>`_
        for a complete list of supported srun variables.

        r>   Ú1é   rU   zYou set `--ntasks=z^` in your SLURM bash script, but this variable is not supported. HINT: Use `--ntasks-per-node=z
` instead.N)r-   r    r!   r"   ÚRuntimeError)Úntaskss    r   r   z)SLURMEnvironment._validate_srun_variablesÒ   s^   € ô ”R—Z‘Z—^‘^ N°CÓ8Ó9ˆØ�AŠ:Ð1¼¿¹ÑCÜØ$ V Hð -1Ø17°¸
ðDóð ð Dˆ:r   )TN)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úboolr   r   ÚSignalsr   Úpropertyr   r   r.   r(   r-   r1   Ústaticmethodr4   r7   r/   r@   rE   rH   rK   rN   rQ   rY   r#   r   r   Ú__classcell__)r   s   @r   r
   r
   !   s!  ø„ ññ( Tð (À(È6Ï>É>ÑBZð (Ðfjõ (ð Øð¨dò ó ó ðð Øð˜cò ó ó ðð Øð˜3ò ó ó ðð4 Øð
�Dò 
ó ó ð
ð ð0�h˜s‘mò 0ó ð0ð ð�H˜S‘Mò ó ðð ð/˜Cò /ó ð/ð ðq 3ð q¨4ò qó ðqð ð/˜Sò /ó ð/ð ðs Cð s¨Dò só ðsð ð0˜Cò 0ó ð0ð ð/˜3ò /ó ð/ð ð¨Sð ¸Sð ÀTò ó ðð  ð1¨ð 1°ò 1ó ð1ð òó ðð( òó ôr   r
   r   c                  ó@   — dt         j                  v xr t        «        S r=   )r    r!   r:   r   r   r   r3   r3   ã   s   € ØœRŸZ™ZÐ'ÒLÔ0JÓ0LÐ,LÐLr   c                  ó.   — t         j                  «       dv S )N)ÚbashÚinteractive)r
   r7   r   r   r   r:   r:   ç   s   € Ü×$Ñ$Ó&Ð*AÐAÐAr   )Úloggingr    r^   re   r   rj   Útypingr   Útyping_extensionsr   Ú9lightning_fabric.plugins.environments.cluster_environmentr   Ú"lightning_fabric.utilities.importsr   Ú$lightning_fabric.utilities.rank_zeror   Ú#lightning_fabric.utilities.warningsr   Ú	getLoggerrt   r$   r
   rx   r3   r:   r   r   r   Ú<module>r‰      si   ðó Û 	Û 	Û Û Û 
Ý å &å XÝ :Ý ?Ý Cà€g×Ñ˜Ó!€ôÐ)ô ðDM�tó MðB Dô Br   