Ë
    M²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m	Z	 d dl
Z
d dlmc mc mc mZ d dl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mZ d d
l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, g d¢Z- e*e.«      Z/e G d„ d«      «       Z0 G d„ d«      Z1dee2z  dz  de3e	   de2fd„Z4de&de5e2dz  e6dz  f   fd„Z7de0dee2z  dz  de3e	   de8e6e	f   fd„Z9y)é    N)ÚCallable)Ú	dataclassÚfield)ÚAny)Úget_default_numa_optionsÚjustknobs_check)ÚeventsÚmetrics)Ú
WorkerSpec)Úcreate_healthcheck_server)Ú_AliveCallbackProxyÚLocalElasticAgentÚTORCHELASTIC_HEALTH_CHECK_PORT)ÚDefaultLogsSpecsÚ	LogsSpecsÚSignalException)ÚChildFailedError)ÚRendezvousParameters)Úparse_rendezvous_endpoint)Ú
get_logger)ÚNumaOptions)ÚLaunchConfigÚelastic_launchÚlaunch_agentc                   ó´  — e Zd ZU dZeed<   eed<   eed<   dZedz  ed<   dZe	ed<   d	Z
e	ed
<   dZe	ed<   dZe	ed<    ee¬«      Zee	ef   ed<   dZeed<   dZeed<   dZeed<   dZe	ed<   dZe	dz  ed<    ee¬«      Zee	e	f   ed<   dZe	dz  ed<   dZe	ed<   dZedz  ed<   dZe	ed<   dZee	   dz  ed <   dZee	   dz  ed!<   d"Z e!ed#<   dZ"edz  ed$<   d%„ Z#y)&r   a–  
    Creates a rendezvous config.

    Args:
        min_nodes: Minimum amount of nodes that the user function will
                        be launched on. Elastic agent ensures that the user
                        function start only when the min_nodes amount enters
                        the rendezvous.
        max_nodes: Maximum amount of nodes that the user function
                        will be launched on.
        nproc_per_node: On each node the elastic agent will launch
                            this amount of workers that will execute user
                            defined function.
        rdzv_backend: rdzv_backend to use in the rendezvous (zeus-adapter, etcd).
        rdzv_endpoint: The endpoint of the rdzv sync. storage.
        rdzv_configs: Key, value pair that specifies rendezvous specific configuration.
        rdzv_timeout: Legacy argument that specifies timeout for the rendezvous. It is going
            to be removed in future versions, see the note below. The default timeout is 900 seconds.
        run_id: The unique run id of the job (if not passed a unique one will be
                deduced from run environment - flow workflow id in flow - or auto generated).
        role: User defined role of the worker (defaults to "trainer").
        max_restarts: The maximum amount of restarts that elastic agent will conduct
                    on workers before failure.
        monitor_interval: The interval in seconds that is used by the elastic_agent
                        as a period of monitoring workers.
        start_method: The method is used by the elastic agent to start the
                    workers (spawn, fork, forkserver).
        metrics_cfg: configuration to initialize metrics.
        local_addr: address of the local node if any. If not set, a lookup on the local
                machine's FQDN will be performed.
        local_ranks_filter: ranks for which to show logs in console. If not set, show from all.
        event_log_handler: name of the event logging handler as registered in
          `elastic/events/handlers.py <https://docs.pytorch.org/docs/stable/elastic/events.html>`_.
        duplicate_stdout_filters: If non-empty, duplicates stdout to a file containing only lines
                                that match _any_ of the filter strings.
        duplicate_stderr_filters: If non-empty, duplicates stderr to a file containing only lines
                                that match _any_ of the filter strings.
        virtual_local_rank: Enable virtual local rank mode for workers (defaults to False).
                           When enabled, LOCAL_RANK is set to 0 for all workers and
                           CUDA_VISIBLE_DEVICES is adjusted so each worker accesses its
                           assigned GPU at device index 0.
        shutdown_timeout: Time in seconds to wait for graceful shutdown of workers before
                        sending SIGKILL. Can also be set via TORCH_ELASTIC_SHUTDOWN_TIMEOUT
                        environment variable. Defaults to 30 seconds.


    .. note::
        `rdzv_timeout` is a legacy argument that will be removed in future.
        Set the timeout via `rdzv_configs['timeout']`

    Ú	min_nodesÚ	max_nodesÚnproc_per_nodeNÚ
logs_specsÚ Úrun_idÚdefault_roleÚroleÚrdzv_endpointÚetcdÚrdzv_backend)Údefault_factoryÚrdzv_configséÿÿÿÿÚrdzv_timeouté   Úmax_restartsgš™™™™™¹?Úmonitor_intervalÚspawnÚstart_methodÚlog_line_prefix_templateÚmetrics_cfgÚ
local_addrÚnullÚevent_log_handlerÚnuma_optionszSIGTERM,SIGINT,SIGHUP,SIGQUITÚsignals_to_handleÚduplicate_stdout_filtersÚduplicate_stderr_filtersFÚvirtual_local_rankÚshutdown_timeoutc                 ó–  — d}| j                   dk7  r| j                   | j                  d<   nd| j                  vr|| j                  d<   | j                  €t        «       | _        | j                  €xt
        j                  j                  «       rZt
        j                  j                  «       | j                  k(  r/t        «       | _        t        j                  d| j                  «       | j                  €/t        t        j                   j#                  dd«      «      | _        y | j                  dk  rt%        d| j                  › �«      ‚y )	Ni„  r)   ÚtimeoutzUsing default numa options = %rÚTORCH_ELASTIC_SHUTDOWN_TIMEOUTÚ30r   z+shutdown_timeout must be non-negative, got )r*   r(   r   r   r5   ÚtorchÚcudaÚis_availableÚdevice_countr   r   ÚloggerÚinfor:   ÚintÚosÚenvironÚgetÚ
ValueError)ÚselfÚdefault_timeouts     ú]/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/distributed/launcher/api.pyÚ__post_init__zLaunchConfig.__post_init__|   s  € ØˆØ×Ñ Ò"Ø+/×+<Ñ+<ˆD×Ñ˜iÒ(Ø˜d×/Ñ/Ñ/Ø+:ˆD×Ñ˜iÑ(ð �?‰?Ð"Ü.Ó0ˆDŒOð ×ÑÐ%Ü—
‘
×'Ñ'Ô)ä—
‘
×'Ñ'Ó)¨T×-@Ñ-@Ò@ä 8Ó :ˆDÔÜ�K‰KÐ9¸4×;LÑ;LÔMð × Ñ Ð(Ü$'Ü—
‘
—‘Ð?ÀÓFó%ˆDÕ!ð ×"Ñ" QÒ&ÜØ=¸d×>SÑ>SÐ=TÐUóð ð 'ó    )$Ú__name__Ú
__module__Ú__qualname__Ú__doc__rE   Ú__annotations__r   r   r!   Ústrr#   r$   r&   r   Údictr(   r   r*   r,   r-   Úfloatr/   r0   r1   r2   r4   r5   r   r6   r7   Úlistr8   r9   Úboolr:   rM   © rN   rL   r   r   .   s1  … ñ2ðh ƒNØƒNØÓØ#'€J�	˜DÑ Ó'Ø€FˆCÓØ€Dˆ#ÓØ€M�3ÓØ€L�#ÓÙ#(¸Ô#>€L�$�s˜C�x‘.Ó>Ø€L�#ÓØ€L�#ÓØ!Ð�eÓ!Ø€L�#ÓØ+/Ð˜c D™jÓ/Ù"'¸Ô"=€K��c˜3�h‘Ó=Ø!€J��d‘
Ó!Ø#Ð�sÓ#Ø'+€L�+ Ñ$Ó+Ø<Ð�sÓ<Ø15Ð˜d 3™i¨$Ñ.Ó5Ø15Ð˜d 3™i¨$Ñ.Ó5Ø$Ð˜Ó$Ø#'Ð�c˜D‘jÓ'órN   r   c                   ó2   — e Zd ZdZdedeez  dz  fd„Zd„ Zy)r   a  
    Launches an torchelastic agent on the container that invoked the entrypoint.

        1. Pass the ``entrypoint`` arguments as non ``kwargs`` (e.g. no named parameters)/
           ``entrypoint`` can be a function or a command.
        2. The return value is a map of each worker's output mapped
           by their respective global rank.

    Usage

    ::

    def worker_fn(foo):
        # ...

    def main():
        # entrypoint is a function.
        outputs = elastic_launch(LaunchConfig, worker_fn)(foo)
        # return rank 0's output
        return outputs[0]

        # entrypoint is a command and ``script.py`` is the python module.
        outputs = elastic_launch(LaunchConfig, "script.py")(args)
        outputs = elastic_launch(LaunchConfig, "python")("script.py")
    ÚconfigÚ
entrypointNc                 ó    — || _         || _        y ©N)Ú_configÚ_entrypoint)rJ   r[   r\   s      rL   Ú__init__zelastic_launch.__init__¶   s   € ð
 ˆŒØ%ˆÕrN   c                 óV   — t        | j                  | j                  t        |«      «      S r^   )r   r_   r`   rW   )rJ   Úargss     rL   Ú__call__zelastic_launch.__call__¾   s   € Ü˜DŸL™L¨$×*:Ñ*:¼DÀ»JÓGÐGrN   )	rO   rP   rQ   rR   r   r   rT   ra   rd   rY   rN   rL   r   r   ›   s-   „ ñð4&àð&ð ˜s‘N TÑ)ó&óHrN   r   r\   rc   Úreturnc                 ó¬   — t        | t        «      r| j                  S t        | t        «      r(| t        j
                  k(  rt        d„ |D «       d«      S | S y)aÀ  Retrieve entrypoint name with the rule:
    1. If entrypoint is a function, use ``entrypoint.__qualname__``.
    2. If entrypoint is a string, check its value:
        2.1 if entrypoint equals to ``sys.executable`` (like "python"), use the first element from ``args``
            which does not start with hifen letter (for example, "-u" will be skipped).
        2.2 otherwise, use ``entrypoint`` value.
    3. Otherwise, return empty string.
    c              3   ó2   K  — | ]  }|d    dk7  sŒ|–— Œ y­w)r   ú-NrY   )Ú.0Úargs     rL   ú	<genexpr>z'_get_entrypoint_name.<locals>.<genexpr>Ï   s   è ø€ Ò> °°A±¸#³œÑ>ùs   ‚�r    )Ú
isinstancer   rO   rT   ÚsysÚ
executableÚnext)r\   rc   s     rL   Ú_get_entrypoint_namerp   Â   sL   € ô �*œhÔ'Ø×"Ñ"Ð"Ü	�J¤Ô	$ØœŸ™Ò'ÜÑ>¨Ô>ÀÓCÐCàÐàrN   Úrdzv_parametersc                 óÄ   — | j                   dk7  ry| j                  }|j                  «       }|st        d«      ‚t	        |d¬«      \  }}|dk(  rt        d|› d�«      ‚||fS )NÚstatic)NNzKEndpoint is missing in endpoint. Try to add --master-addr and --master-portr)   )Údefault_portzport is missing in endpoint: z. Try to specify --master-port)ÚbackendÚendpointÚstriprI   r   )rq   rv   Úmaster_addrÚmaster_ports       rL   Ú_get_addr_and_portrz   Ö   s‚   € ð ×Ñ (Ò*ØØ×'Ñ'€HØ�~‰~Ó€HÙÜØYó
ð 	
ô  9¸ÐPRÔSÑ€K�Ø�bÒÜØ+¨H¨:Ð5SÐTó
ð 	
ð ˜Ð%Ð%rN   r[   c                 óŠ	  — | j                   sDt        t        j                  «       j                  «      }t
        j                  d|«       || _         t        ||«      }t
        j                  di d|“d| j                  “d| j                  “d| j                  “d| j                   “d| j                  “d	| j                  “d
| j                  “d| j                  “d| j                   “d| j"                  j$                  “d| j&                  “d| j(                  “d| j*                  “d| j,                  “d| j.                  “d| j0                  “«       t3        d"| j                  | j                  | j                   | j                  | j                  | j4                  dœ| j                  ¤Ž}t7        |«      \  }}| j,                  t8        j:                  d<   d }d }	t9        j<                  t>        «      }
|
�UtA        dd¬«      rH	 tC        «       }	tE        |	t	        |
«      d¬«      }|jG                  «        t
        j                  d|
«       tK        | jL                  | j                  |tO        |«      tQ        jR                  |«      | j                  | j                   ||| j4                  | j(                  | j*                  | j.                  | j0                  | jT                  ¬«      }tW        || j"                  | jX                  | jZ                  | j\                  |¬ «      }|	�|	j_                  |j`                  «       d}	 tc        jd                  tc        jf                  | j&                  «      «       |ji                  «       }tk        jl                  |jo                  «       | j(                  «       |jq                  «       rts        ||jt                  ¬!«      ‚|jv                  |r|jx                  j{                  «        S S # tH        $ r t
        j                  dd¬«       d }d }	Y �ŒÚw xY w# tr        $ r ‚ t|        $ r2 d}tk        jl                  |j                  «       | j(                  «       ‚ tH        $ r0 tk        jl                  |j                  «       | j(                  «       ‚ w xY w# |r|jx                  j{                  «        w w xY w)#Nz3config has no run_id, generated a random run_id: %saR  Starting elastic_operator with launch configs:
  entrypoint               : %(entrypoint)s
  min_nodes                : %(min_nodes)s
  max_nodes                : %(max_nodes)s
  nproc_per_node           : %(nproc_per_node)s
  run_id                   : %(run_id)s
  rdzv_backend             : %(rdzv_backend)s
  rdzv_endpoint            : %(rdzv_endpoint)s
  rdzv_configs             : %(rdzv_configs)s
  max_restarts             : %(max_restarts)s
  monitor_interval         : %(monitor_interval)s
  log_dir                  : %(log_dir)s
  metrics_cfg              : %(metrics_cfg)s
  event_log_handler        : %(event_log_handler)s
  numa_options             : %(numa_options)s
  signals_to_handle        : %(signals_to_handle)s
  duplicate_stdout_filters : %(duplicate_stdout_filters)s
  duplicate_stderr_filters : %(duplicate_stderr_filters)s
r\   r   r   r   r!   r&   r$   r(   r,   r-   Úlog_dirr1   r4   r5   r6   r7   r8   )ru   rv   r!   r   r   r2   ÚTORCHELASTIC_SIGNALS_TO_HANDLEzNai_infra/pytorch_distributed:torchelastic_enable_healthcheck_before_rendezvousF)Údefaulté<   )Úalive_callbackÚportr<   z>Started early health check server on port %s before rendezvousz)Failed to start early health check serverT)Úexc_info)r#   Úlocal_world_sizer\   rc   Úrdzv_handlerr,   r-   rx   ry   r2   r4   r5   r7   r8   r9   )Úspecr   r/   r0   r:   Úhealth_check_server)ÚnameÚfailuresrY   )@r!   rT   ÚuuidÚuuid4rE   rC   Úwarningrp   rD   r   r   r   r&   r$   r(   r,   r-   r   Úroot_log_dirr1   r4   r5   r6   r7   r8   r   r2   rz   rF   rG   Úgetenvr   r   r   r   ÚstartÚ	Exceptionr   r#   ÚtupleÚrdzv_registryÚget_rendezvous_handlerr9   r   r/   r0   r:   Úset_delegateÚ_get_alive_timer
   Úinitialize_metricsÚMetricsConfigÚrunr	   ÚrecordÚget_event_succeededÚ	is_failedr   rˆ   Úreturn_valuesr„   Úshutdownr   Úget_event_failed)r[   r\   rc   r!   Úentrypoint_namerq   rx   ry   r†   Úalive_callback_proxyÚhealthcheck_portr…   ÚagentÚshutdown_rdzvÚresults                  rL   r   r   é   s¦  € ð
 �=Š=Ü”T—Z‘Z“\×%Ñ%Ó&ˆÜ�‰ÐLÈfÔUØˆŒä*¨:°tÓ<€Oä
‡K�Kð	Fð$	
Ø˜/ð	
à˜×)Ñ)ð	
ð ˜×)Ñ)ð	
ð ˜f×3Ñ3ð		
ð
 �f—m‘mð	
ð ˜F×/Ñ/ð	
ð ˜V×1Ñ1ð	
ð ˜F×/Ñ/ð	
ð ˜F×/Ñ/ð	
ð  × 7Ñ 7ð	
ð �v×(Ñ(×5Ñ5ð	
ð ˜6×-Ñ-ð	
ð   ×!9Ñ!9ð	
ð ˜F×/Ñ/ð	
ð   ×!9Ñ!9ð	
ð  '¨×(GÑ(Gð!	
ð" '¨×(GÑ(Gð#	
ô'&ôP +ð Ø×#Ñ#Ø×%Ñ%Ø�}‰}Ø×"Ñ"Ø×"Ñ"Ø×$Ñ$ñð ×
Ñ
ñ€Oô  2°/ÓBÑ€K�ð 4:×3KÑ3K„B‡J�JÐ/Ñ0ð ÐØÐÜ—y‘yÔ!?Ó@ÐØÐ#¬ØXØõ)ð	(Ü#6Ó#8Ð Ü";Ø3ÜÐ)Ó*Øô#Ðð
  ×%Ñ%Ô'Ü�K‰KØPØ ôô Ø�[‰[Ø×.Ñ.ØÜ�4‹[Ü"×9Ñ9¸/ÓJØ×(Ñ(Ø×0Ñ0ØØØ×$Ñ$Ø ×2Ñ2Ø×(Ñ(Ø!'×!@Ñ!@Ø!'×!@Ñ!@Ø!×4Ñ4ô€Dô$ ØØ×$Ñ$Ø×(Ñ(Ø!'×!@Ñ!@Ø×0Ñ0Ø/ô€Eð Ð'Ø×)Ñ)¨%×*?Ñ*?Ô@à€Mð )Ü×"Ñ"¤7×#8Ñ#8¸×9KÑ9KÓ#LÔMà—‘“ˆä�‰�e×/Ñ/Ó1°6×3KÑ3KÔLà×ÑÔô
 #Ø$ØŸ™ôð ð
 ×#Ñ#ñ Ø×Ñ×&Ñ&Õ(ð øôG ò 	(Ü�N‰NÐFÐQUˆNÔVØ"&ÐØ#'Ó ð	(ûôl ò ØÜò ð ˆÜ�‰�e×,Ñ,Ó.°×0HÑ0HÔIØÜò Ü�‰�e×,Ñ,Ó.°×0HÑ0HÔIØðûñ Ø×Ñ×&Ñ&Õ(ð ús-   È AO: Ì:B"P% Ï:$P"Ð!P"Ð%A;R Ò R# Ò#S):rF   rm   r‰   Úcollections.abcr   Údataclassesr   r   Útypingr   r?   Ú-torch.distributed.elastic.rendezvous.registryÚdistributedÚelasticÚ
rendezvousÚregistryr‘   Útorch._utils_internalr   r   Útorch.distributed.elasticr	   r
   Ú*torch.distributed.elastic.agent.server.apir   Ú:torch.distributed.elastic.agent.server.health_check_serverr   Ú:torch.distributed.elastic.agent.server.local_elastic_agentr   r   r   Ú)torch.distributed.elastic.multiprocessingr   r   r   Ú0torch.distributed.elastic.multiprocessing.errorsr   Ú$torch.distributed.elastic.rendezvousr   Ú*torch.distributed.elastic.rendezvous.utilsr   Ú'torch.distributed.elastic.utils.loggingr   Útorch.numa.bindingr   Ú__all__rO   rC   r   r   rT   rW   rp   r�   rE   rz   rU   r   rY   rN   rL   ú<module>r¸      s#  ðó 
Û 
Û Ý $ß (Ý ã ß EÖ Eß Kß 5Ý Aõ÷ñ ÷
ñ õ
 NÝ EÝ PÝ >Ý *ò =€á	�HÓ	€ð ÷ið ió ði÷X$Hñ $HðN X°¡^°dÑ%:ð À$ÀsÁ)ð ÐPSó ð(&Ø)ð&à
ˆ3�‰:�s˜T‘zÐ!Ñ"ó&ð&^)Øð^)à˜3‘ Ñ%ð^)ð ˆs‰)ð^)ð 
ˆ#ˆsˆ(�^ô	^)rN   