§
    ŠŠtjA  ã                   ó  — 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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/¦  «        Z0e G d„ d¦  «        ¦   «         Z1 G d„ d¦  «        Z2dee3z  dz  de4e	         de3fd„Z5de'de6e3dz  e7dz  f         fd„Z8	 dde1dee3z  dz  de4e	         dedz  de9e7e	f         f
d„Z:dS )é    N)ÚCallable)Ú	dataclassÚfield)ÚAny)Úget_default_numa_optionsÚjustknobs_check)ÚeventsÚmetrics)Ú
WorkerSpec)Úcreate_healthcheck_serverÚHealthCheckServer)Ú_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#dS )&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                 óR  — d}| j         dk    r| j         | j        d<   nd| j        vr
|| j        d<   | j        €t          ¦   «         | _        | j        €xt
          j                             ¦   «         rZt
          j                             ¦   «         | j	        k    r3t          ¦   «         | _        t                               d| j        ¦  «         | j        €4t          t          j                             dd¦  «        ¦  «        | _        d S | j        dk     rt%          d| j        › �¦  «        ‚d S )	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   r6   ÚtorchÚcudaÚis_availableÚdevice_countr   r   ÚloggerÚinfor;   ÚintÚosÚenvironÚgetÚ
ValueError)ÚselfÚdefault_timeouts     ú\/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/distributed/launcher/api.pyÚ__post_init__zLaunchConfig.__post_init__}   s.  € ØˆØÔ Ò"Ð"Ø+/Ô+<ˆDÔ˜iÑ(Ð(Ø˜dÔ/Ð/Ð/Ø+:ˆDÔ˜iÑ(ð Œ?Ð"Ý.Ñ0Ô0ˆDŒOð ÔÐ%Ý”
×'Ò'Ñ)Ô)ð &õ ”
×'Ò'Ñ)Ô)¨TÔ-@Ò@Ð@å 8Ñ :Ô :ˆDÔÝ�KŠKÐ9¸4Ô;LÑMÔMÐMð Ô Ð(Ý$'Ý”
—’Ð?ÀÑFÔFñ%ô %ˆDÔ!Ð!Ð!ð Ô" QÒ&Ð&ÝØU¸dÔ>SÐUÐUñô ð ð 'Ð&ó    )$Ú__name__Ú
__module__Ú__qualname__Ú__doc__rF   Ú__annotations__r    r   r"   Ústrr$   r%   r'   r   Údictr)   r   r+   r-   r.   Úfloatr0   r1   r2   r3   r5   r6   r   r7   r8   Úlistr9   r:   Úboolr;   rN   © rO   rM   r   r   /   sê  € € € € € € ð2ð 2ðh €N€N�NØ€N€N�NØÐÐÑØ#'€J�	˜DÑ Ð'Ð'Ñ'Ø€FˆCÐÐÑØ€Dˆ#ÐÐÑØ€M�3ÐÐÑØ€L�#ÐÐÑØ#( 5¸Ð#>Ñ#>Ô#>€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Ð5Ñ5Ø15Ð˜d 3œi¨$Ñ.Ð5Ð5Ñ5Ø$Ð˜Ð$Ð$Ñ$Ø#'Ð�c˜D‘jÐ'Ð'Ñ'ðð ð ð ð rO   r   c                   óB   — e Zd ZdZ	 ddedeez  dz  dedz  fd„Zd„ Z	dS )	r   a  
    Launches a 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")
    NÚconfigÚ
entrypointÚhealth_check_serverc                 ó0   — || _         || _        || _        d S ©N)Ú_configÚ_entrypointÚ_health_check_server)rK   r\   r]   r^   s       rM   Ú__init__zelastic_launch.__init__·   s!   € ð ˆŒØ%ˆÔØ$7ˆÔ!Ð!Ð!rO   c                 ó`   — t          | j        | j        t          |¦  «        | j        ¬¦  «        S )N)r^   )r   ra   rb   rX   rc   )rK   Úargss     rM   Ú__call__zelastic_launch.__call__Á   s3   € ÝØŒLØÔÝ�‰JŒJØ $Ô 9ð	
ñ 
ô 
ð 	
rO   r`   )
rP   rQ   rR   rS   r   r   rU   r   rd   rg   rZ   rO   rM   r   r   œ   su   € € € € € ðð ð< 9=ð	8ð 8àð8ð ˜s‘N TÑ)ð8ð /°Ñ5ð	8ð 8ð 8ð 8ð
ð 
ð 
ð 
ð 
rO   r   r]   rf   Úreturnc                 óÀ   — t          | t          ¦  «        r| j        S t          | t          ¦  «        r,| t          j        k    rt          d„ |D ¦   «         d¦  «        S | S dS )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k    ¯|V — ŒdS )r   ú-NrZ   )Ú.0Úargs     rM   ú	<genexpr>z'_get_entrypoint_name.<locals>.<genexpr>×   s*   è è € Ð>Ð> °°A´¸#²°˜°°°°Ð>Ð>rO   r!   )Ú
isinstancer   rP   rU   ÚsysÚ
executableÚnext)r]   rf   s     rM   Ú_get_entrypoint_namers   Ê   sh   € õ �*�hÑ'Ô'ð ØÔ"Ð"Ý	�J¥Ñ	$Ô	$ð Ø�œÒ'Ð'ÝÐ>Ð>¨Ð>Ñ>Ô>ÀÑCÔCÐCàÐàˆrrO   Úrdzv_parametersc                 óÖ   — | j         dk    rdS | 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ÚstriprJ   r   )rt   ry   Úmaster_addrÚmaster_ports       rM   Ú_get_addr_and_portr}   Þ   s�   € ð Ô (Ò*Ð*Øˆ|ØÔ'€HØ�~Š~ÑÔ€HØð 
ÝØYñ
ô 
ð 	
õ  9¸ÐPRÐSÑSÔSÑ€K�Ø�bÒÐÝØT¨HÐTÐTÐTñ
ô 
ð 	
ð ˜Ð%Ð%rO   r\   r^   c                 ó~  — | j         sGt          t          j        ¦   «         j        ¦  «        }t
                               d|¦  «         || _         t          ||¦  «        }t
                               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| j        “¦  «         t3          d"| j        | j        | j         | j	        | j
        | j        dœ| j        ¤Ž}t7          |¦  «        \  }}| j        t8          j        d<   |€¶t9          j        t>          ¦  «        }	|	�›tA          dd¬¦  «        rŠ	 tC          tE          ¦   «         t	          |	¦  «        d¬¦  «        }| #                    ¦   «          t
                               d|	¦  «         n.# tH          $ r! t
                               dd¬¦  «         d }Y nw xY wtK          | j&        | j        |tO          |¦  «        tQ          j)        |¦  «        | j        | j        ||| j        | j        | j        | j        | j        | j*        ¬¦  «        }
tW          |
| j        | j,        | j-        | j.        |¬ ¦  «        }|�6|j/        }ta          |tD          ¦  «        r| 1                    |j2        ¦  «         d}	 tg          j4        tg          j5        | j        ¦  «        ¦  «         | 6                    ¦   «         }to          j8        | 9                    ¦   «         | j        ¦  «         | :                    ¦   «         rtw          ||j<        ¬!¦  «        ‚|j=        |r|
j>         ?                    ¦   «          S S # tv          $ r ‚ t€          $ r0 d}to          j8        | A                    ¦   «         | j        ¦  «         ‚ tH          $ r. to          j8        | A                    ¦   «         | j        ¦  «         ‚ w xY w# |r|
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_dirr2   r5   r6   r7   r8   r9   )rx   ry   r"   r   r   r3   Ú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]   rf   Úrdzv_handlerr-   r.   r{   r|   r3   r5   r6   r8   r9   r:   )Úspecr    r0   r1   r;   r^   )ÚnameÚfailuresrZ   )Br"   rU   ÚuuidÚuuid4rF   rD   Úwarningrs   rE   r   r   r   r'   r%   r)   r-   r.   r    Úroot_log_dirr2   r5   r6   r7   r8   r9   r   r3   r}   rG   rH   Úgetenvr   r   r   r   ÚstartÚ	Exceptionr   r$   ÚtupleÚrdzv_registryÚget_rendezvous_handlerr:   r   r0   r1   r;   rƒ   ro   Úset_delegateÚ_get_alive_timer
   Úinitialize_metricsÚMetricsConfigÚrunr	   ÚrecordÚget_event_succeededÚ	is_failedr   rŠ   Úreturn_valuesr‡   Úshutdownr   Úget_event_failed)r\   r]   rf   r^   r"   Úentrypoint_namert   r{   r|   Úhealthcheck_portrˆ   ÚagentÚcbÚshutdown_rdzvÚresults                  rM   r   r   ñ   s  € ð Œ=ð Ý•T”Z‘\”\Ô%Ñ&Ô&ˆÝ�ŠÐLÈfÑUÔUÐUØˆŒå*¨:°tÑ<Ô<€Oå
‡K‚Kð	Fð$	
Ø˜/ð	
à˜Ô)ð	
ð ˜Ô)ð	
ð ˜fÔ3ð		
ð
 �f”mð	
ð ˜FÔ/ð	
ð ˜VÔ1ð	
ð ˜FÔ/ð	
ð ˜FÔ/ð	
ð  Ô 7ð	
ð �vÔ(Ô5ð	
ð ˜6Ô-ð	
ð   Ô!9ð	
ð ˜FÔ/ð	
ð   Ô!9ð	
ð  '¨Ô(Gð!	
ð" '¨Ô(Gð#	
ñ'&ô &ð &õP +ð ØÔ#ØÔ%ØŒ}ØÔ"ØÔ"ØÔ$ðð ð Ô
ðð €Oõ  2°/ÑBÔBÑ€K�ð 4:Ô3K…B„JÐ/Ñ0ð Ð"Ýœ9Õ%CÑDÔDÐØÐ'­OØ\Øð-
ñ -
ô -
Ð'ð+Ý&?Ý#6Ñ#8Ô#8ÝÐ-Ñ.Ô.Øð'ñ 'ô 'Ð#ð
 $×)Ò)Ñ+Ô+Ð+Ý—’ØTØ$ñô ð ð øõ ð +ð +ð +Ý—’Ø?È$ð ñ ô ð ð '+Ð#Ð#Ð#ð	+øøøõ ØŒ[ØÔ.ØÝ�4‰[Œ[Ý"Ô9¸/ÑJÔJØÔ(ØÔ0ØØØÔ$Ø Ô2ØÔ(Ø!'Ô!@Ø!'Ô!@Ø!Ô4ðñ ô €Dõ$ ØØÔ$ØÔ(Ø!'Ô!@ØÔ0Ø/ðñ ô €Eð Ð&Ø Ô/ˆÝ�bÕ-Ñ.Ô.ð 	3Ø�OŠO˜EÔ1Ñ2Ô2Ð2à€Mð )ÝÔ"¥7Ô#8¸Ô9KÑ#LÔ#LÑMÔMÐMà—’‘”ˆåŒ�e×/Ò/Ñ1Ô1°6Ô3KÑLÔLÐLà×ÒÑÔð 	õ
 #Ø$Øœðñ ô ð ð
 Ô#ð ð 	)ØÔ×&Ò&Ñ(Ô(Ð(Ð(ð	)øõ ð ð ð ØÝð ð ð ð ˆÝŒ�e×,Ò,Ñ.Ô.°Ô0HÑIÔIÐIØÝð ð ð ÝŒ�e×,Ò,Ñ.Ô.°Ô0HÑIÔIÐIØðøøøøð ð 	)ØÔ×&Ò&Ñ(Ô(Ð(Ð(ð	)øøøs-   ÆAG( Ç((HÈHË)BN! Î!A:PÐP ÐP<r`   );rG   rp   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   r   Ú: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__rP   rD   r   r   rU   rX   rs   r’   rF   r}   rV   r   rZ   rO   rM   ú<module>rº      s*  ðð 
€	€	€	Ø 
€
€
€
Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ø Ð Ð Ð Ð Ð à €€€Ø EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EØ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KØ 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø AÐ AÐ AÐ AÐ AÐ Aðð ð ð ð ð ð ð ðð ð ð ð ð ð ð ð ð ð
ð ð ð ð ð ð ð ð ð ð
 NÐ MÐ MÐ MÐ MÐ MØ EÐ EÐ EÐ EÐ EÐ EØ PÐ PÐ PÐ PÐ PÐ PØ >Ð >Ð >Ð >Ð >Ð >Ø *Ð *Ð *Ð *Ð *Ð *ð =Ð
<Ð
<€à	ˆ�HÑ	Ô	€ð ðið ið ið ið iñ iô iñ „ðiðX+
ð +
ð +
ð +
ð +
ñ +
ô +
ð +
ð\ X°¡^°dÑ%:ð À$ÀsÄ)ð ÐPSð ð ð ð ð(&Ø)ð&à
ˆ3�‰:�s˜T‘zÐ!Ô"ð&ð &ð &ð &ð. 59ð	b)ð b)Øðb)à˜3‘ Ñ%ðb)ð ˆsŒ)ðb)ð +¨TÑ1ð	b)ð
 
ˆ#ˆsˆ(„^ðb)ð b)ð b)ð b)ð b)ð b)rO   