Ë
    ÿÍ:j²"  ã                   óæ  — d dl Z d dlZd dlZd dlmZ d dlmZmZ d dl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mZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ  e j>                  e «      Z! e
d«      Z" e
d«      Z#dZ$de%e&   fd„Z'e"�rd dl(Z( e(jR                  «       dGd„«       Z*e*jW                  dddi¬«       e(jX                  d e(jZ                  d¬«      ¬«       e(j\                  d e(j^                  e$«      dd¬«       e(j\                  d e(j^                   e'«       «      dd¬«       e(j\                  de&d d!¬«       e(j\                  d"d#e0d$d%¬«       e(j\                  d&d'e0d d(¬«       e(j\                  d)d*e&d+d,¬«       e(j\                  d-d.e0d/d0¬«       e(j\                  d1 e(j^                   ee«       ee«      z   «      dd2¬«       e(jX                  d3d4e(jb                  ¬5«      d6eddfd7„«       «       «       «       «       «       «       «       «       «       «       Z2e*jW                  d8ddi¬«       e(jX                  d9 e(jZ                  d¬«      ¬«       e(j\                  d: e(jZ                  d¬«      dd;¬«      d9e&d<ee&   ddfd=„«       «       «       Z3d>eddfd?„Z4d@e&dAe&de0fdB„Z5d>ed3e%e&   ddfdC„Z6dHd>ed3ee%e&      ddfdD„Z7e dEk(  r#e"se!jq                  dF«        e9d$«      ‚ e2«        yy)Ié    N)Ú	Namespace)ÚAnyÚOptional)ÚRequirementCache)Úget_args)ÚCPUAcceleratorÚCUDAAcceleratorÚMPSAccelerator)Ú_PRECISION_INPUT_STRÚ_PRECISION_INPUT_STR_ALIAS)ÚSTRATEGY_REGISTRY)Ú_process_cli_args)Ú_parse_gpu_idsÚ_select_auto_accelerator)Ú_suggested_max_num_threads)Ú_load_distributed_checkpointÚclickÚlightning_sdk)ÚcpuÚgpuÚcudaÚmpsÚtpuÚautoÚreturnc                  ó†   — t        j                  «       } d}| D �cg c]  }t        j                  ||«      rŒ|‘Œ c}S c c}w )z¯Returns strategy choices from the registry, with the ones removed that are incompatible to be launched from the
    CLI or ones that require further configuration by the user.z).*(spawn|fork|notebook|xla|tpu|offload).*)r   Úavailable_strategiesÚreÚmatch)r   ÚexcludedÚstrategys      úi/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/lightning/fabric/cli.pyÚ_get_supported_strategiesr#   (   s:   € ô -×AÑAÓCÐØ;€HØ%9Ö^˜ÄÇÁÈ(ÐT\ÕA]ŠHÒ^Ð^ùÒ^s   ›>·>c                   ó   — y ©N© r&   ó    r"   Ú_mainr(   3   s   € àr'   ÚrunÚignore_unknown_optionsT)Ú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Ú1z€Number 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_portiØr  z-The main port to connect to the main machine.z--precisionz¬Double precision (``64-true`` or ``64``), full precision (``32-true`` or ``32``), half precision (``16-mixed`` or ``16``) or bfloat16 precision (``bf16-mixed`` or ``bf16``)Úscript_argséÿÿÿÿ)Únargsr.   Úkwargsc                  óf   — t        | j                  dg «      «      }t        t        di | ¤Ž|¬«       y)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.

        r3   ©Úargsr3   Nr&   )ÚlistÚpopÚmainr   )r6   r3   s     r"   Ú_runr=   7   s,   € ôb ˜6Ÿ:™: m°RÓ8Ó9ˆÜ”)Ñ%˜fÑ%°;Ö?r'   ÚconsolidateÚcheckpoint_folderz--output_filezçPath to the file where the converted checkpoint should be saved. The file should not already exist. If no path is provided, the file will be saved next to the input checkpoint folder with the same name and a '.consolidated' suffix.Úoutput_filec                 óž   — t        | |¬«      }t        |«      }t        |j                  «      }t	        j
                  ||j                  «       y)z¬Convert a distributed/sharded checkpoint into a single file that can be loaded with `torch.load()`.

        Only supports FSDP sharded checkpoints at the moment.

        )r?   r@   N)r   r   r   r?   ÚtorchÚsaver@   )r?   r@   r9   ÚconfigÚ
checkpoints        r"   Ú_consolidaterF   ‹   s?   € ô4 Ð+<È+ÔVˆÜ" 4Ó(ˆÜ1°&×2JÑ2JÓKˆ
Ü�
‰
�:˜v×1Ñ1Õ2r'   r9   c                 óð  — dt         j                  d<   | j                  �&t        | j                  «      t         j                  d<   | j                  �&t        | j                  «      t         j                  d<   t        | j
                  «      t         j                  d<   t        | j                  «      t         j                  d<   | j                  �'t        | j                  «      t         j                  d<   yy)	zwSet the environment variables for the new processes.

    The Fabric connector will parse the arguments set here.

    r1   ÚLT_CLI_USEDNÚLT_ACCELERATORÚLT_STRATEGYÚ
LT_DEVICESÚLT_NUM_NODESÚLT_PRECISION)ÚosÚenvironÚacceleratorÚstrr!   ÚdevicesÚ	num_nodesÚ	precision)r9   s    r"   Ú_set_env_variablesrU   «   s­   € ð !$„B‡J�Jˆ}ÑØ×ÑÐ#Ü'*¨4×+;Ñ+;Ó'<Œ�
‰
Ð#Ñ$Ø‡}�}Ð Ü$'¨¯©Ó$6Œ�
‰
�=Ñ!Ü" 4§<¡<Ó0„B‡J�Jˆ|ÑÜ!$ T§^¡^Ó!4„B‡J�Jˆ~ÑØ‡~�~Ð!Ü%(¨¯©Ó%8Œ�
‰
�>Ò"ð "r'   rP   rR   c                 ól  — | dk(  s| €
t        «       } |dk(  r!| dk(  s
| dk(  s| dk(  rd}nt        d| › d�«      ‚| dk(  rt        |d	d	¬
«      }n[| dk(  rt        j                  |«      }n@| dk(  rt        j                  |«      }n%| dk(  rt        d«      ‚t        j                  |«      S |�t        |«      S dS )zhParse the `devices` argument to determine how many processes need to be launched on the current machine.r   r   r   r   r1   z.Cannot default to '1' device for accelerator='ú'r   T)Úinclude_cudaÚinclude_mpsr   z=Launching processes for TPU through the CLI is not supported.r   )r   Ú
ValueErrorr   r	   Úparse_devicesr
   r   Úlen)rP   rR   Úparsed_devicess      r"   Ú_get_num_processesr^   ¼   sÓ   € ð �fÒ Ð 3Ü.Ó0ˆØ�&ÒØ˜&Ò  K°5Ò$8¸KÈ5Ò<PØ‰GäÐMÈkÈ]ÐZ[Ð\Ó]Ð]Ø�eÒÜ'¨¸dÐPTÔU‰Ø	˜Ò	Ü(×6Ñ6°wÓ?‰Ø	˜Ò	Ü'×5Ñ5°gÓ>‰Ø	˜Ò	ÜÐXÓYÐYä×+Ñ+¨GÓ4Ð4Ø"0Ð"<Œ3ˆ~ÓÐCÀ!ÐCr'   c                 ó°  — ddl mc m} | j                  dk(  rdnt	        | j
                  | j                  «      }d|› �d| j                  › �d| j                  › �d| j                  › �d	| j                  › �| j                  g}|j                  |«       t        j                  j                  d
t!        t#        «       «      «       |j%                  |«       y)zYThis will invoke `torchrun` programmatically to launch the given script in new processes.r   NÚdpr2   z--nproc_per_node=z	--nnodes=z--node_rank=z--master_addr=z--master_port=ÚOMP_NUM_THREADS)Útorch.distributed.runÚdistributedr)   r!   r^   rP   rR   rS   Ú	node_rankÚmain_addressÚ	main_portr,   ÚextendrN   rO   Ú
setdefaultrQ   r   r<   )r9   r3   ÚtorchrunÚnum_processesÚtorchrun_argss        r"   Ú_torchrun_launchrl   Ó   sÃ   € ç,Ð,àŸ™¨$Ò.‘AÔ4FÀt×GWÑGWÐY]×YeÑYeÓ4f€Mð ˜M˜?Ð+Ø
�D—N‘NÐ#Ð$Ø
�t—~‘~Ð&Ð'Ø
˜×*Ñ*Ð+Ð,Ø
˜Ÿ™Ð(Ð)Ø�‰ð€Mð ×Ñ˜Ô%ô ‡J�J×ÑÐ+¬SÔ1KÓ1MÓ-NÔOØ‡M�M�-Õ r'   c                 ó:   — t        | «       t        | |xs g «       y r%   )rU   rl   r8   s     r"   r<   r<   è   s   € Ü�tÔÜ�T˜;Ò,¨"Õ-r'   Ú__main__zoTo use the Lightning Fabric CLI, you must have `click` installed. Install it by running `pip install -U click`.)r   Nr%   ):ÚloggingrN   r   Úargparser   Útypingr   r   rB   Ú lightning_utilities.core.importsr   Útyping_extensionsr   Úlightning.fabric.acceleratorsr   r	   r
   Ú,lightning.fabric.plugins.precision.precisionr   r   Úlightning.fabric.strategiesr   Ú1lightning.fabric.utilities.consolidate_checkpointr   Ú(lightning.fabric.utilities.device_parserr   r   Ú&lightning.fabric.utilities.distributedr   Úlightning.fabric.utilities.loadr   Ú	getLoggerÚ__name__Ú_logÚ_CLICK_AVAILABLEÚ_LIGHTNING_SDK_AVAILABLEÚ_SUPPORTED_ACCELERATORSr:   rQ   r#   r   Úgroupr(   ÚcommandÚargumentÚPathÚoptionÚChoiceÚintÚUNPROCESSEDr=   rF   rU   r^   rl   r<   ÚerrorÚ
SystemExitr&   r'   r"   ú<module>r‹      sµ  ðó Û 	Û 	Ý ß  ã Ý =Ý &ç YÑ Yß iÝ 9Ý Oß ]Ý MÝ Hà€w×Ñ˜Ó"€á# GÓ,Ð Ù+¨OÓ<Ð àFÐ ð_ 4¨¡9ó _ò Ûà€U‡[�[ƒ]òó ðð ‡]�]Øà$ dð
ð ó ð €U‡^�^ØØˆU�Z‰Z˜tÔ$ôð €U‡\�\ØØˆU�\‰\Ð1Ó2ØØ2ô	ð €U‡\�\ØØˆU�\‰\Ñ3Ó5Ó6ØØ?ô	ð €U‡\�\ØØØð+ôð €U‡\�\ØØØØØDôð €U‡\�\ØØØØð&ô	ð €U‡\�\ØØØØØcôð €U‡\�\ØØØØØ<ôð €U‡\�\ØØˆU�\‰\™(Ð#7Ó8¹8ÐD^Ó;_Ñ_Ó`Øðiôð €U‡^�^�M¨°%×2CÑ2CÔDð
@�sð 
@˜tò 
@ó Eóóóó	óóóóó	óðP
@ð ‡]�]Øà$ dð
ð ó ð €U‡^�^ØØˆU�Z‰Z˜tÔ$ôð €U‡\�\ØØˆU�Z‰Z˜tÔ$Øð-ô	ð	3¨ð 	3¸(À3¹-ð 	3ÈDò 	3ó	ó	óð(	3ð9˜Yð 9¨4ó 9ð"D Cð D°#ð D¸#ó Dð.!˜9ð !°4¸±9ð !Àó !ñ*.ˆyð . x°°S±	Ñ':ð .Àdó .ð
 ˆzÒÙØ�
‰
ð=ô	
ñ ˜‹mÐá…Fð r'   