a
    dl                     @   s   d dl Z d dlmZ d dl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 erxd d	lmZ G d
d deZeddddZdS )    N)SimpleQueue)AnyCallableOptionalTYPE_CHECKING)get_context)_XLA_AVAILABLE)	_Launcher)_GlobalStateSnapshot)move_data_to_device)XLAStrategyc                	   @   sf   e Zd ZdZdddddZeeddd	Zee	e	e	d
ddZ
deee	e	eee ddddZd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
    r   N)strategyreturnc                 C   s    t sttt || _d| _d S )Nfork)r   ModuleNotFoundErrorstr	_strategy_start_method)selfr    r   r/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/strategies/launchers/xla.py__init__-   s    z_XLALauncher.__init__)r   c                 C   s   dS )NTr   )r   r   r   r   is_interactive_compatible3   s    z&_XLALauncher.is_interactive_compatible)functionargskwargsr   c                 O   sN   t | j}| }ddlm  m} |j| j||||f| jj	| jd |
 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)r   ZnprocsZstart_method)r   r   r   Z)torch_xla.distributed.xla_multiprocessingZdistributedZxla_multiprocessingspawn_wrapping_functionr   Znum_processesget)r   r   r   r   contextreturn_queuexmpr   r   r   launch7   s    

z_XLALauncher.launch)process_idxr   r   r   r!   global_statesr   c                 C   s:   ||i |}| j jdkr*|t|d t| j j d S )Nr   cpu)r   Z
local_rankputr   _rank_teardown)r   r$   r   r   r   r!   r%   resultsr   r   r   r   N   s    z_XLALauncher._wrapping_function)N)__name__
__module____qualname____doc__r   propertyboolr   r   r   r#   intr   r   r
   r   r   r   r   r   r      s     r   )rankr   c                 C   s2   dd l m  m} |d | dkr.td d S )Nr   zend-process   )Ztorch_xla.core.xla_modelcoreZ	xla_modelZ
rendezvoustimesleep)r1   Zxmr   r   r   r(   a   s    
r(   )r4   Zmultiprocessing.queuesr   typingr   r   r   r   Ztorch.multiprocessingr   Z!lightning_fabric.accelerators.tpur   Z*lightning_fabric.strategies.launchers.baser	   Z5lightning_fabric.strategies.launchers.multiprocessingr
   Z%lightning_fabric.utilities.apply_funcr   Zlightning_fabric.strategiesr   r   r0   r(   r   r   r   r   <module>   s   D