a
    d                     @   s  d dl 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mZmZ d dlmZ e eZe	dZdZd	Zd
Zer~d dlZejdedddejdejdddejdeedddejdeedddejdedddejddedddejdded d dejd!d"ed#d$dejd%d&ed'd(dejd)eedd*dejd+d,ej d-edd.d/d0Z!edd1d2d3Z"eeed4d5d6Z#eee dd7d8d9Z$d>eeee  dd7d:d;Z%ed<krese&d= e'de!  dS )?    N)	Namespace)AnyListOptional)RequirementCache)CPUAcceleratorCUDAAcceleratorMPSAccelerator)_parse_gpu_idsclick)cpugpucudampstpu)ZddpdpZ	deepspeed)Z64Z32Z16Zbf16modelT)Zignore_unknown_options)Zcontext_settingsscript)exists)typez--acceleratorz#The hardware accelerator to run on.)r   defaulthelpz
--strategyz0Strategy for how to run across multiple devices.z	--devices1zNumber of devices to run on (``int``), which devices to run on (``list`` or ``str``), or ``'auto'``. The value applies per node.z--num-nodesz--num_nodes   z5Number of machines (nodes) for distributed execution.z--node-rankz--node_rankzrThe index of the machine (node) this command gets started on. Must be a number in the range 0, ..., num_nodes - 1.z--main-addressz--main_addressz	127.0.0.1zTThe hostname or IP address of the main machine (usually the one with node_rank = 0).z--main-portz--main_portir  z-The main port to connect to the main machine.z--precisionznDouble precision (``64``), full precision (``32``), half precision (``16``) or bfloat16 precision (``'bf16'``)script_args)nargsr   )kwargsreturnc                  K   s*   t | dg }ttf i | |d dS )a  Run a Lightning Fabric script.

        SCRIPT is the path to the Python script with the code to run. The script must contain a Fabric object.

        SCRIPT_ARGS are the remaining arguments that you can pass to the script itself and are expected to be parsed
        there.
        r   argsr   N)listpopmainr   )r   r    r$   ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/cli.py
_run_model#   s    Pr&   )r    r   c                 C   s|   dt jd< | jdur$t| jt jd< | jdur>t| jt jd< t| jt jd< t| jt jd< | jdurxt| jt jd< dS )	zvSet the environment variables for the new processes.

    The Fabric connector will parse the arguments set here.
    r   ZLT_CLI_USEDNZLT_ACCELERATORZLT_STRATEGYZ
LT_DEVICESZLT_NUM_NODESZLT_PRECISION)osenvironacceleratorstrstrategydevices	num_nodesZ	precision)r    r$   r$   r%   _set_env_variablesw   s    



r.   )r)   r,   r   c                 C   sp   | dkrt |ddd}nD| dkr,t|}n0| dkr@t|}n| dkrRtdn
t|S |durlt|S d	S )
zhParse the `devices` argument to determine how many processes need to be launched on the current machine.r   T)Zinclude_cudaZinclude_mpsr   r   r   z=Launching processes for TPU through the CLI is not supported.Nr   )r
   r   Zparse_devicesr	   
ValueErrorr   len)r)   r,   Zparsed_devicesr$   r$   r%   _get_num_processes   s    

r1   )r    r   r   c                 C   s   ddl m  m} | jdkr"d}nt| j| j}d| d| j d| j d| j	 d	| j
 | jg}|| tjd
ttdt pd|  || dS )zYThis will invoke `torchrun` programmatically to launch the given script in new processes.r   Nr   r   z--nproc_per_node=z	--nnodes=z--node_rank=z--master_addr=z--master_port=ZOMP_NUM_THREADS)Ztorch.distributed.runZdistributedrunr+   r1   r)   r,   r-   Z	node_rankZmain_addressZ	main_portr   extendr'   r(   
setdefaultr*   max	cpu_countr#   )r    r   ZtorchrunZnum_processesZtorchrun_argsr$   r$   r%   _torchrun_launch   s    





$r7   c                 C   s   t |  t| |pg  d S )N)r.   r7   r   r$   r$   r%   r#      s    r#   __main__zoTo use the Lightning Fabric CLI, you must have `click` installed. Install it by running `pip install -U click`.)N)(loggingr'   argparser   typingr   r   r   Z lightning_utilities.core.importsr   Zlightning_fabric.acceleratorsr   r   r	   Z(lightning_fabric.utilities.device_parserr
   	getLogger__name___logZ_CLICK_AVAILABLEZ_SUPPORTED_ACCELERATORSZ_SUPPORTED_STRATEGIESZ_SUPPORTED_PRECISIONr   commanddictargumentPathoptionZChoicer*   intZUNPROCESSEDr&   r.   r1   r7   r#   error
SystemExitr$   r$   r$   r%   <module>   s   

	
	&
