§
    “©ºjV  ã                   óÂ   — d dl Z d dlZd dlmZmZmZmZ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 d dlmZmZmZ d dlmZ d d	lmZ erd dlZ G d
„ de¦  «        ZdS )é    N)ÚTYPE_CHECKINGÚAnyÚCallableÚOptionalÚUnion)Úoverride)Ú_XLA_AVAILABLE)Ú_rank_teardown)Úmove_data_to_device)Ú_GlobalStateSnapshotÚ_MultiProcessingLauncherÚ_WorkerOutput)Ú	TrainerFn)Úrank_zero_debugc                   ó6  ‡ — e Zd ZdZdˆ 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deded         d	e	d
e
de
deej        ej        f         dee         ddfd„¦   «         Zeddde
ded         fd„¦   «         Zˆ xZS )Ú_XLALaunchera€  Launches processes that run a given function in parallel on XLA supported hardware, and joins them all at the
    end.

    The main process in which this launcher is invoked creates N so-called worker processes (using the
    `torch_xla` :func:`xmp.spawn`) that run the given function.
    Worker processes have a rank that ranges from 0 to N - 1.

    Note:
        - This launcher requires all objects to be pickleable.
        - It is important that the entry point to the program/script is guarded by ``if __name__ == "__main__"``.

    Args:
        strategy: A reference to the strategy that is used together with this launcher

    Ústrategyúpl.strategies.XLAStrategyÚreturnNc                 óž   •— t           s!t          t          t           ¦  «        ¦  «        ‚t          ¦   «                              |d¬¦  «         d S )NÚfork)r   Ústart_method)r	   ÚModuleNotFoundErrorÚstrÚsuperÚ__init__)Úselfr   Ú	__class__s     €úz/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/pytorch_lightning/strategies/launchers/xla.pyr   z_XLALauncher.__init__5   sD   ø€ Ýð 	;Ý%¥c­.Ñ&9Ô&9Ñ:Ô:Ð:Ý‰Œ×Ò (¸ÐÑ@Ô@Ð@Ð@Ð@ó    c                 ó   — dS )NT© )r   s    r   Úis_interactive_compatiblez&_XLALauncher.is_interactive_compatible:   s	   € ð ˆtr    )ÚtrainerÚfunctionÚargsr$   z
pl.TrainerÚkwargsc                ó^  — | j         r+|�)|j        j        t          j        k    rt          d¦  «        ‚t          j        ¦   «                              ¦   «         }ddl	m
c m} i }| j        j        }|dk    r||d<    |j        | j        f|||||f| j        ddœ|¤Ž}	|	�5|	j        | _        |	                     ¦   «         s	 |	                     ¦   «         ¯|                     ¦   «         }
|€|
S | xj         |j        j        t          j        k    z  c_         |                      |
|¦  «         |
j        S )aà  Launches processes that run the given function in parallel.

        The function is allowed to have a return value. However, when all processes join, only the return value
        of worker process 0 gets returned from this `launch` method in the main process.

        Arguments:
            function: The entry point for all launched processes.
            *args: Optional positional arguments to be passed to the given function.
            trainer: Optional reference to the :class:`~pytorch_lightning.trainer.trainer.Trainer` for which
                a selected set of attributes get restored in the main process after processes join.
            **kwargs: Optional keyword arguments to be passed to the given function.

        NzÔCalling `trainer.fit()` twice on the same Trainer instance using a spawn-based strategy is not supported. You can work around this by creating a new Trainer instance and passing the `fit(ckpt_path=...)` argument.r   é   ÚnprocsF)r&   r   Újoin)Ú_already_fitÚstateÚfnr   ÚFITTINGÚNotImplementedErrorÚmpÚManagerÚQueueÚ)torch_xla.distributed.xla_multiprocessingÚdistributedÚxla_multiprocessingÚ	_strategyÚnum_processesÚspawnÚ_wrapping_functionÚ_start_methodÚ	processesÚprocsr+   ÚgetÚ _recover_results_in_main_processÚtrainer_results)r   r%   r$   r&   r'   Úreturn_queueÚxmpÚspawn_kwargsr*   Úprocess_contextÚworker_outputs              r   Úlaunchz_XLALauncher.launch?   sv  € ð Ôð 	 Ð!4¸¼Ô9IÍYÔM^Ò9^Ð9^å%ð2ñô ð õ ”z‘|”|×)Ò)Ñ+Ô+ˆà?Ð?Ð?Ð?Ð?Ð?Ð?Ð?Ð?àˆØ”Ô-ˆØ�QŠ;ˆ;ð &,ˆL˜Ñ"à#˜#œ)ØÔ#ð
à˜8 T¨6°<Ð@ØÔ+Øð	
ð 
ð
 ð
ð 
ˆð Ð&Ø(Ô2ˆDŒJØ%×*Ò*Ñ,Ô,ð Øð &×*Ò*Ñ,Ô,ð ð %×(Ò(Ñ*Ô*ˆØˆ?Ø Ð àÐÔ˜Wœ]Ô-µÔ1BÒBÑBÐÔØ×-Ò-¨m¸WÑEÔEÐEØÔ,Ð,r    Úprocess_idxrA   Úglobal_statesc                 ó~  — dd l mc m} t          |                     ¦   «         ¦  «        dk    r"dd l}	|	                     ||||f¦  «        \  }}}} ||i |¤Ž}
|�|                      ||
¦  «        }
| j        j	        dk    r#| 
                    t          |
d¦  «        ¦  «         t          | j        j	        ¦  «         d S )Nr   r)   Úcpu)Útorch_xla.core.xla_modelÚcoreÚ	xla_modelÚlenÚget_xla_supported_devicesÚcopyÚdeepcopyÚ_collect_rank_zero_resultsr7   Ú
local_rankÚputr   r
   )r   rG   r$   r%   r&   r'   rA   rH   ÚxmrP   Úresultss              r   r:   z_XLALauncher._wrapping_functionw   så   € ð 	.Ð-Ð-Ð-Ð-Ð-Ð-Ð-Ð-åˆr×+Ò+Ñ-Ô-Ñ.Ô.°Ò2Ð2ð ˆKˆKˆKà.2¯mªm¸WÀhÐPTÐV\Ð<]Ñ.^Ô.^Ñ+ˆG�X˜t Và�(˜DÐ+ FÐ+Ð+ˆàÐØ×5Ò5°g¸wÑGÔGˆGàŒ>Ô$¨Ò)Ð)Ø×ÒÕ0°¸%Ñ@Ô@ÑAÔAÐAå�t”~Ô0Ñ1Ô1Ð1Ð1Ð1r    rV   r   c                 óÖ  — t          d¦  «         |j        }|rt          |d¦  «        r|j        nd }d }|j        j        t          j        k    r^| j         	                    ¦   «         }t          j                             |j        d¦  «        }| j        j                             ||¦  «         | j        j        dk    rd S |                      |¦  «        }t%          |||j        ||¦  «        S )Nz'Collecting results from rank 0 process.Úbest_model_pathz
.temp.ckptr   )r   Úcheckpoint_callbackÚhasattrrX   r-   r.   r   r/   r7   Úlightning_module_state_dictÚosÚpathr+   Údefault_root_dirÚcheckpoint_ioÚsave_checkpointrS   Úget_extra_resultsr   )r   r$   rV   rY   rX   Úweights_pathÚ
state_dictÚextras           r   rR   z'_XLALauncher._collect_rank_zero_results—   sî   € åÐAÑBÔBÐBØ%Ô9Ðð #ðÝ'.Ð/BÐDUÑ'VÔ'VðÐÔ/Ð/àð 	ð ˆØŒ=Ô�yÔ0Ò0Ð0àœ×CÒCÑEÔEˆJÝœ7Ÿ<š<¨Ô(@À,ÑOÔOˆLØŒNÔ(×8Ò8¸À\ÑRÔRÐRð Œ>Ô$¨Ò)Ð)Ø�4ð ×&Ò& wÑ/Ô/ˆå˜_¨l¸G¼MÈ7ÐTYÑZÔZÐZr    )r   r   r   N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   Úboolr#   r   r   r   rF   Úintr   r1   ÚSimpleQueueÚqueuer3   r   r:   rR   Ú__classcell__)r   s   @r   r   r   $   s°  ø€ € € € € ðð ð Að Að Að Að Að Að
 Øð¨4ð ð ð ñ „Xñ „Xðð ØW[ð 5-ð 5-ð 5-˜xð 5-°ð 5-¸hÀ|Ô>Tð 5-Ðgjð 5-Ðorð 5-ð 5-ð 5-ñ „Xð5-ðn ð 9=ð2ð 2ð ð	2ð
 ˜,Ô'ð2ð ð2ð ð2ð ð2ð ˜BœN¨E¬KÐ7Ô8ð2ð  Ð 4Ô5ð2ð 
ð2ð 2ð 2ñ „Xð2ð> ð[°,ð [Èð [ÐQYÐZiÔQjð [ð [ð [ñ „Xð[ð [ð [ð [ð [r    r   )r\   rm   Útypingr   r   r   r   r   Útorch.multiprocessingÚmultiprocessingr1   Útyping_extensionsr   Ú!lightning_fabric.accelerators.xlar	   Ú)lightning_fabric.strategies.launchers.xlar
   Úlightning_fabric.utilitiesr   Ú6pytorch_lightning.strategies.launchers.multiprocessingr   r   r   Ú pytorch_lightning.trainer.statesr   Ú%pytorch_lightning.utilities.rank_zeror   Úpytorch_lightningÚplr   r"   r    r   ú<module>r{      sR  ðð 
€	€	€	Ø €€€Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @à "Ð "Ð "Ð "Ð "Ð "Ø &Ð &Ð &Ð &Ð &Ð &à <Ð <Ð <Ð <Ð <Ð <Ø DÐ DÐ DÐ DÐ DÐ DØ :Ð :Ð :Ð :Ð :Ð :ðð ð ð ð ð ð ð ð ð ð
 7Ð 6Ð 6Ð 6Ð 6Ð 6Ø AÐ AÐ AÐ AÐ AÐ Aàð #Ø"Ð"Ð"Ð"ðL[ð L[ð L[ð L[ð L[Ð+ñ L[ô L[ð L[ð L[ð L[r    