§
    “©ºjg  ã                   óè   — d dl Z d dlZd dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlZd dlmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZ  e j        e¦  «        Z ed
¦  «        Z G d„ de¦  «        ZdS )é    N)ÚAnyÚCallableÚOptional)ÚRequirementCache)Úoverride)ÚClusterEnvironment)Ú_basic_subprocess_cmdÚ_hydra_subprocess_cmdÚ_launch_process_observer)Ú_set_num_threads_if_needed)Ú	_Launcher)Ú_SIGNUMz
hydra-corec                   óÎ   ‡ — e Zd ZdZdedededdfˆ fd„Zeede	fd„¦   «         ¦   «         Z
edd	œd
ededed         dedef
d„¦   «         Zededdfd„¦   «         Zdd„Zdd„Zˆ xZS )Ú_SubprocessScriptLaunchera£  A process launcher that invokes the current script as many times as desired in a single node.

    This launcher needs to be invoked on each node.
    In its default behavior, the main process in each node then spawns N-1 child processes via :func:`subprocess.Popen`,
    where N is the number of devices (e.g. GPU) per node. It is very similar to how :mod:`torch.distributed.run`
    launches processes.

    For example, if the script gets invoked with the command

    .. code-block:: bash

        python train.py --devices 4

    The launcher will create three additional subprocesses that get called like so:

    .. code-block:: bash

        LOCAL_RANK=1 python train.py --devices 4
        LOCAL_RANK=2 python train.py --devices 4
        LOCAL_RANK=3 python train.py --devices 4

    It is implied that the main process which launched the others has ``LOCAL_RANK=0``.
    Beside the local rank, the following other environment variables also get set, but unlike the local rank, these
    get determined by the cluster environment:

    1. `MASTER_ADDR`: The IP address of the main node.
    2. `MASTER_PORT`: The port number of the main node through which all processes communicate.
    3. `NODE_RANK`: The index of the node the current process is running on. Ranges from 0 to ``num_nodes - 1``.
    4. `WORLD_SIZE`: The total number of processes across all nodes, i.e., ``num_processes * num_nodes``.

    Arguments:
        cluster_environment: A cluster environment that provides access to world size, node rank, etc.
        num_processes: The number of processes to launch in the current node.
        num_nodes: The total number of nodes that participate in this process group.

    Úcluster_environmentÚnum_processesÚ	num_nodesÚreturnNc                 ó€   •— t          ¦   «                              ¦   «          || _        || _        || _        g | _        d S )N)ÚsuperÚ__init__r   r   r   Úprocs)Úselfr   r   r   Ú	__class__s       €úˆ/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/strategies/launchers/subprocess_script.pyr   z"_SubprocessScriptLauncher.__init__K   s;   ø€ Ý‰Œ×ÒÑÔÐØ#6ˆÔ Ø*ˆÔØ"ˆŒØ-/ˆŒ
ˆ
ˆ
ó    c                 ó   — dS )NF© ©r   s    r   Úis_interactive_compatiblez3_SubprocessScriptLauncher.is_interactive_compatibleR   s	   € ð ˆur   )ÚtrainerÚfunctionÚargsr!   z
pl.TrainerÚkwargsc                óð   — | j                              | j        | j        ¬¦  «         | j         j        s(|                      ¦   «          t          | j        ¦  «         t          | j        ¬¦  «          ||i |¤ŽS )a0  Creates new processes, then calls the given function.

        Arguments:
            function: A callback function to execute after all processes have been created.
                It is up to the implementation of this function to synchronize the processes, e.g., with barriers.
            *args: Optional positional arguments to be passed to the given function.
            trainer: Optional reference to the :class:`~pytorch_lightning.trainer.trainer.Trainer`.
            **kwargs: Optional keyword arguments to be passed to the given function.

        )Únum_devicesr   )r   )	r   Úvalidate_settingsr   r   Úcreates_processes_externallyÚ_call_children_scriptsr   r   r   )r   r"   r!   r#   r$   s        r   Úlaunchz _SubprocessScriptLauncher.launchW   s€   € ð 	Ô ×2Ò2¸tÔ?QÐ]aÔ]kÐ2ÑlÔlÐlØÔ'ÔDð 	1Ø×'Ò'Ñ)Ô)Ð)Ý$ T¤ZÑ0Ô0Ð0å"°Ô1CÐDÑDÔDÐDØˆx˜Ð( Ð(Ð(Ð(r   Úsignumc           	      ó¶   — | j         D ]P}t                               dt          j        ¦   «         › d|j        › d|› �¦  «         |                     |¦  «         ŒQd S )NzProcess z is terminating z with )r   ÚlogÚdebugÚosÚgetpidÚpidÚsend_signal)r   r+   Úprocs      r   Úkillz_SubprocessScriptLauncher.killk   sg   € à”Jð 	%ð 	%ˆDÝ�IŠIÐV¥¤¡¤ÐVÐV¸d¼hÐVÐVÈfÐVÐVÑWÔWÐWà×Ò˜VÑ$Ô$Ð$Ð$ð	%ð 	%r   c                 óv  — |                       ¦   «          g | _        | j        j        t          j        d<   t          | j        j        ¦  «        t          j        d<   t          | j                             ¦   «         ¦  «        t          j        d<   t          | j         	                    ¦   «         ¦  «        t          j        d<   | j
        | j        z  › t          j        d<   t          d| j
        ¦  «        D ]Å}t          j                             ¦   «         }|› |d<   t          j                             d¦  «        €d|v r|d= d}d }t          rd	d
lm} |                     ¦   «         }|rt'          |¦  «        \  }}nt)          ¦   «         }t+          j        |||¬¦  «        }| j                             |¦  «         ŒÆd S )NÚMASTER_ADDRÚMASTER_PORTÚ	NODE_RANKÚ
LOCAL_RANKÚ
WORLD_SIZEé   ÚPL_GLOBAL_SEEDFr   )ÚHydraConfig)ÚenvÚcwd)Ú_check_can_spawn_childrenr   r   Úmain_addressr/   ÚenvironÚstrÚ	main_portÚ	node_rankÚ
local_rankr   r   ÚrangeÚcopyÚgetÚ_HYDRA_AVAILABLEÚhydra.core.hydra_configr=   Úinitializedr
   r	   Ú
subprocessÚPopenÚappend)r   rF   Úenv_copyÚhydra_in_user?   r=   ÚcommandÚnew_processs           r   r)   z0_SubprocessScriptLauncher._call_children_scriptsr   s¤  € à×&Ò&Ñ(Ô(Ð(ØˆŒ
ð %)Ô$<Ô$I�Œ
�=Ñ!Ý$'¨Ô(@Ô(JÑ$KÔ$K�Œ
�=Ñ!õ #& dÔ&>×&HÒ&HÑ&JÔ&JÑ"KÔ"K�Œ
�;ÑÝ#& tÔ'?×'JÒ'JÑ'LÔ'LÑ#MÔ#M�Œ
�<Ñ Ø&*Ô&8¸4¼>Ñ&IÐ#K�Œ
�<Ñ å  4Ô#5Ñ6Ô6ð 	+ð 	+ˆJÝ”z—’Ñ(Ô(ˆHØ(2 _ˆH�\Ñ"õ Œz�~Š~Ð.Ñ/Ô/Ð7Ð<LÐPXÐ<XÐ<XØÐ-Ð.à ˆLØ!%ˆCÝð 9Ø?Ð?Ð?Ð?Ð?Ð?à*×6Ò6Ñ8Ô8�àð 2Ý4°ZÑ@Ô@‘�˜˜å/Ñ1Ô1�å$Ô*¨7¸ÀcÐJÑJÔJˆKØŒJ×Ò˜kÑ*Ô*Ð*Ð*ð+	+ð 	+r   c                 ó¬   — t          | j        ¦  «        dk    rt          d¦  «        ‚| j                             ¦   «         dk    rt          d¦  «        ‚d S )Nr   z/The launcher can only create subprocesses once.a  Lightning attempted to launch new distributed processes with `local_rank > 0`. This should not happen. Possible reasons: 1) LOCAL_RANK environment variable was incorrectly modified by the user, 2) `ClusterEnvironment.creates_processes_externally` incorrectly implemented.)Úlenr   ÚRuntimeErrorr   rF   r   s    r   r@   z3_SubprocessScriptLauncher._check_can_spawn_children—   s^   € ÝˆtŒz‰?Œ?˜QÒÐÝÐPÑQÔQÐQØÔ#×.Ò.Ñ0Ô0°AÒ5Ð5Ýðañô ð ð 6Ð5r   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úintr   Úpropertyr   Úboolr    r   r   r   r*   r   r4   r)   r@   Ú__classcell__)r   s   @r   r   r   %   sF  ø€ € € € € ð#ð #ðJ0Ð,>ð 0Èsð 0Ð_bð 0Ðgkð 0ð 0ð 0ð 0ð 0ð 0ð Øð¨4ð ð ð ñ „Xñ „Xðð ØW[ð )ð )ð )˜xð )°ð )¸hÀ|Ô>Tð )Ðgjð )Ðorð )ð )ð )ñ „Xð)ð& ð%˜7ð % tð %ð %ð %ñ „Xð%ð#+ð #+ð #+ð #+ðJð ð ð ð ð ð ð r   r   )Úloggingr/   rM   Útypingr   r   r   Ú lightning_utilities.core.importsr   Útyping_extensionsr   Úpytorch_lightningÚplÚlightning_fabric.pluginsr   Ú7lightning_fabric.strategies.launchers.subprocess_scriptr	   r
   r   Ú&lightning_fabric.utilities.distributedr   Ú/pytorch_lightning.strategies.launchers.launcherr   Ú5pytorch_lightning.trainer.connectors.signal_connectorr   Ú	getLoggerrW   r-   rJ   r   r   r   r   ú<module>rk      sQ  ðð €€€Ø 	€	€	€	Ø Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ð *à =Ð =Ð =Ð =Ð =Ð =Ø &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7ðð ð ð ð ð ð ð ð ð ð
 NÐ MÐ MÐ MÐ MÐ MØ EÐ EÐ EÐ EÐ EÐ EØ IÐ IÐ IÐ IÐ IÐ Ià€gÔ˜Ñ!Ô!€Ø#Ð# LÑ1Ô1Ð ðzð zð zð zð z 	ñ zô zð zð zð zr   