Ë
      çiŠ  ã                   ó²   — 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 erd dlmZmZ  G d	„ d
e«      Zdeddfd„Zy)é    N)ÚTYPE_CHECKINGÚAnyÚCallableÚOptionalÚUnion)Úoverride)Ú_XLA_AVAILABLE)Ú	_Launcher)Ú_GlobalStateSnapshot)Úmove_data_to_device)ÚXLAFSDPStrategyÚXLAStrategyc                   óÈ   — e Zd ZdZded   ddfd„Zeedefd„«       «       Z	ede
d	ed
edefd„«       Z	 ddede
d	ed
edeej                  ej"                  f   dee   ddfd„Zy)Ú_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)r   r   ÚreturnNc                 ó\   — t         st        t        t         «      «      ‚|| _        d| _        y )NÚfork)r	   ÚModuleNotFoundErrorÚstrÚ	_strategyÚ_start_method)Úselfr   s     ú~/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning_fabric/strategies/launchers/xla.pyÚ__init__z_XLALauncher.__init__/   s$   € ÝÜ%¤c¬.Ó&9Ó:Ð:Ø!ˆŒØ#ˆÕó    c                  ó   — y)NT© )r   s    r   Úis_interactive_compatiblez&_XLALauncher.is_interactive_compatible5   s   € ð r   ÚfunctionÚargsÚkwargsc                 ó  — t        j                  «       j                  «       }ddlmc m} i }| j                  j                  }|dk(  r||d<    |j                  | j                  f||||f| j                  dœ|¤Ž |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.
            **kwargs: Optional keyword arguments to be passed to the given function.

        r   Né   Únprocs)r!   Ústart_method)ÚmpÚManagerÚQueueÚ)torch_xla.distributed.xla_multiprocessingÚdistributedÚxla_multiprocessingr   Únum_processesÚspawnÚ_wrapping_functionr   Úget)r   r    r!   r"   Úreturn_queueÚxmpÚspawn_kwargsr%   s           r   Úlaunchz_XLALauncher.launch:   s‘   € ô —z‘z“|×)Ñ)Ó+ˆç?Ð?àˆØ—‘×-Ñ-ˆØ�QŠ;ð &,ˆL˜Ñ"àˆ�	‰	Ø×#Ñ#ð	
à˜D &¨,Ð7Ø×+Ñ+ñ	
ð ò		
ð ×ÑÓ!Ð!r   Úprocess_idxr1   Úglobal_statesc                 ó<  — dd l mc m} t        |j	                  «       «      dkD  rdd l}|j                  |||f«      \  }}} ||i |¤Ž}	| j                  j                  dk(  r|j                  t        |	d«      «       t        | j                  j                  «       y )Nr   r$   Úcpu)Útorch_xla.core.xla_modelÚcoreÚ	xla_modelÚlenÚget_xla_supported_devicesÚcopyÚdeepcopyr   Ú
local_rankÚputr   Ú_rank_teardown)
r   r5   r    r!   r"   r1   r6   Úxmr>   Úresultss
             r   r/   z_XLALauncher._wrapping_function[   sŠ   € ÷ 	.Ð-äˆr×+Ñ+Ó-Ó.°Ò2ó à%)§]¡]°H¸dÀFÐ3KÓ%LÑ"ˆH�d˜Fá˜DÐ+ FÑ+ˆà�>‰>×$Ñ$¨Ò)Ø×ÑÔ0°¸%Ó@ÔAä�t—~‘~×0Ñ0Õ1r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úpropertyr   Úboolr   r   r   r4   Úintr'   ÚSimpleQueueÚqueuer)   r   r   r/   r   r   r   r   r      sÜ   „ ñð $ Ð'GÑ!Hð $ÈTó $ð Øð¨4ò ó ó ðð ð"˜xð "°ð "¸sð "Àsò "ó ð"ðR 9=ñ2ð ð	2ð
 ð2ð ð2ð ð2ð ˜BŸN™N¨E¯K©KÐ7Ñ8ð2ð  Ð 4Ñ5ð2ð 
ô2r   r   Úrankr   c                 ón   — dd l mc m} |j                  d«       | dk(  rt	        j
                  d«       y y )Nr   zend-processr$   )r9   r:   r;   Ú
rendezvousÚtimeÚsleep)rN   rC   s     r   rB   rB   w   s/   € ß)Ð)ð ‡M�M�-Ô ð ˆq‚yÜ�
‰
�1�ð r   )rM   rQ   Útypingr   r   r   r   r   Útorch.multiprocessingÚmultiprocessingr'   Útyping_extensionsr   Ú!lightning_fabric.accelerators.xlar	   Ú.lightning_fabric.strategies.launchers.launcherr
   Ú5lightning_fabric.strategies.launchers.multiprocessingr   Ú%lightning_fabric.utilities.apply_funcr   Úlightning_fabric.strategiesr   r   r   rK   rB   r   r   r   Ú<module>r\      sM   ðó Û ß @Õ @å "Ý &å <Ý DÝ VÝ EáßHôV2�9ô V2ðr	˜ð 	 ô 	r   