
      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     zz|))+??--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 %)]]HdF3K%L"HdF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 BNNEKK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/    )) MM-  qy

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29 V2r	 	 	r   