o
    wOiH                     @   sV  d Z ddlZddlZddlZddlZddlmZmZ ddlZddl	m
  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 dd	lmZmZ dd
lmZ ddlm Z  ddl!m"Z" ddl#m$Z$ e$ Z%dd Z&de'fddZ(de'fddZ)dej*fddZ+edddZ,e-dkrej.ej/dd e%0dej1  e,  dS dS ) a  
This module provides similar functionality as ``torch.distributed.launch``,
with the following additional functionalities:

1. Worker failures are handled gracefully by restarting all workers.

2. Worker ``RANK`` and ``WORLD_SIZE`` are assigned automatically.

3. Number of nodes is allowed to change between min and max sizes (elasticity).

**Usage:**

1. Single-node multi-worker (with sidecar etcd server)

::

    >>> python -m torchelastic.distributed.launch
        --standalone
        --nnodes=1
        --nproc_per_node=$NUM_TRAINERS
        YOUR_TRAINING_SCRIPT.py (--arg1 ... train script args...)

2. Fault tolerant (fixed sized number of workers, no elasticity).:

::

    >>> python -m torchelastic.distributed.launch
        --nnodes=$NUM_NODES
        --nproc_per_node=$NUM_TRAINERS
        --rdzv_id=$JOB_ID
        --rdzv_backend=etcd
        --rdzv_endpoint=$ETCD_HOST:$ETCD_PORT
        YOUR_TRAINING_SCRIPT.py (--arg1 ... train script args...)

3. Elastic (``min=1``, ``max=4``):

::

    >>> python -m torchelastic.distributed.launch
        --nnodes=1:4
        --nproc_per_node=$NUM_TRAINERS
        --rdzv_id=$JOB_ID
        --rdzv_backend=etcd
        --rdzv_endpoint=$ETCD_HOST:$ETCD_PORT
        YOUR_TRAINING_SCRIPT.py (--arg1 ... train script args...)

**Note on rendezvous backend**:

For multi-node training you need to specify:

1. ``--rdzv_id``: a unique job id (shared by all nodes participating in the job)
2. ``--rdzv_backend``: an implementation of ``torchelastic.rendevous.RendezvousHandler``
3. ``--rdzv_endpoint``: ``host:port``-style endpoint where the rdzv backend is running.

Currently only ``etcd`` rdzv backend is supported out of the box.
To use ``etcd``, setup an etcd server with the ``v2`` api enabled
(e.g. ``--enable-v2``).

.. warning:: ``EtcdRendezvous`` uses etcd api v2. You MUST enable the v2
             api on the etcd server. Our tests use etcd v3.4.3.

**Definitions:**

1. ``Node`` - Physical instance or container.
    Maps to the unit that the job manager works with.

2. ``Worker`` - A worker in the context of distributed training.

3. ``Worker Group`` - Workers that execute the same function (e.g. trainers)

4. ``Local Worker Group`` - Subset of the workers in the
    worker group running on the same Node

5. ``RANK`` - rank of the worker within a worker group.

6. ``WORLD_SIZE`` - total number of workers in a worker group.

7. ``LOCAL_RANK`` - rank of the worker within a local worker group

8. ``LOCAL_WORLD_SIZE`` - size of the local worker group

9. ``rdzv_id`` - user defined id that uniquely identifies the worker group
      for a job. This id is used by each node to join as a member of a particular
      worker group.

9. ``rdzv_backend`` - the backend store of rendezvous (e.g. etcd). This is
    typically a strongly consistent key-value store.

10. ``rdzv_endpoint`` - rdzv backend server endpoint in ``host:port`` format.

A ``Node`` runs ``LOCAL_WORLD_SIZE`` workers which comprise a ``LocalWorkerGroup``.
The union of all ``LocalWorkerGroups`` in the nodes in the job comprise the
``WorkerGroup``.

**Environment Variables:**

The following environment variables are made available to you in your
script:

1. ``LOCAL_RANK`` -  local rank

2. ``RANK`` -  global rank

3. ``GROUP_RANK`` - rank of the worker group. A number between 0 - ``max_nnodes``.
        When running a single worker group per node, this is the rank of the node.

4. ``ROLE_RANK`` -  the rank of the worker across all the workers tha have the same
        role. The role of the worker is specified in the ``WorkerSpec``.

5. ``LOCAL_WORLD_SIZE`` - local world size (e.g. number of workers running locally).
       Equal to ``--nproc_per_node`` specified on ``torchelastic.distributed.launch``.

6. ``WORLD_SIZE`` - world size (total number of workers in the job).

7. ``ROLE_WORLD_SIZE`` - the total number of workers that was launched with the same
        role specified in ``WorkerSpec``.

8. ``MASTER_ADDR`` - fqdn of the host that is running worker with rank 0.
   Used to initialize torch distributed backend.

9. ``MASTER_PORT`` - port on the ``MASTER_ADDR`` that can be used to
   host the tcp ``c10d`` store.

10. ``TORCHELASTIC_RESTART_COUNT`` - number of worker group restarts so far.

11. ``TORCHELASTIC_MAX_RESTARTS`` - configured max number of restarts.

12. ``TORCHELASTIC_RUN_ID`` - equal to rdzv run_id (e.g. unique job id).

**Deployment:**

1. Start the rdzv backend server and get the endpoint
   (to be passed as ``--rdzv_endpoint`` to the launcher script)

2. Single-node multi-worker - start the launcher on the host to start
   the agent process which creates and monitors a local worker group.

3. Multi-node multi-worker - Start the launcher with the same arguments
   on all the nodes participating in training.

When using a job/cluster manager the entry point command to the multi-node
job is invoking this launcher.

**Failure Modes:**

1. Worker failure - For a training job with ``n`` workers, if ``k <= n`` workers fail
   all workers are stopped and restarted up to ``max_restarts``.

2. Agent failure - An agent failure results in local worker group failure,
   it is up to the job manager to fail the entire job (gang semantics) or attempt
   to replace the node. Both behaviors are supported by the agent.

3. Node failure - Same as agent failure.

**Membership Changes:**

1. Node departure (scale-down) - agent is notified of the departure,
   all existing workers are stopped, a new ``Worker Group`` is formed and all
   workers are started with a new ``RANK`` and ``WORLD_SIZE``.

2. Node arrival (scale-up) - the new node is admitted to the job,
   all existing workers are stopped, a new ``Worker Group`` is formed and all
   workers are started with a new ``RANK`` and ``WORLD_SIZE``.


**Important Notices:**

1. All the items in the important notices section of ``torch.distributed.launch``
   apply to this module as well

2. The environment variables necessary to initialize a torch process group
   are provided to you by this module, no need for you to pass ``RANK`` manually.
   To initialize a process group in your training script, simply run

::

 >>> import torch.distributed as dist
 >>> dist.init_process_group(backend="gloo|nccl")

3. On failures or membership changes ALL surviving workers are killed
   immediately. Make sure to checkpoint your progress. The frequency of
   checkpoints should depend on your job's tolerance for lost work.

4. This module only supports homogeneous ``LOCAL_WORLD_SIZE``. That is,
   it is assumed that all nodes run the same number of local workers (per role).

5. ``RANK`` is NOT stable. Between restarts, the local workers on a node
   can be assgined a different range of ranks than before. NEVER hard code
   any assumptions about the stable-ness of ranks or some correlation between
   ``RANK`` and ``LOCAL_RANK``.

6. When using elasticity (``min_size != max_size``) DO NOT hard code
   assumptions about ``WORLD_SIZE`` as the world size can change as
   nodes are allowed to leave and join.

7. It is recommended your script have the following structure

::

  def main():
    load_checkpoint(checkpoint_path)
    initialize()
    train()

  def train():
    for batch in iter(dataset):
      train_step(batch)

      if should_checkpoint:
        save_checkpoint(checkpoint_path)
    N)	REMAINDERArgumentParser)metricsevents)
WorkerSpecWorkerState)LocalElasticAgent)	check_envenv)Std)ChildFailedErrorrecord)RendezvousParameters)
EtcdServer)_parse_rendezvous_config)
get_loggerc                 C   sp  t dd}|jdttddd |jdttdd	d |jd
ttddd |jdttddd |jdttdd |jdttddd |jdtdd |jdttddd |jdttddd |jdttdg d d!d" |jd#ttd$d%d |jd&d'td(d |jd)td*d |jd+ttd,d-d |jd.d/ttd0d1d |jd2d3ttd0d4d |jd5td6d7 |jd8td9 || S ):z;
    Helper function parsing the command line options.
    z&torchelastic elastic training launcher)descriptionz--nnodesz1:1z&number of nodes or MIN_NODES:MAX_NODES)actiontypedefaulthelpz--nproc_per_nodeautozCnumber of workers per node, supported values: [auto, cpu, gpu, int]z--rdzv_backendetcdzrendezvous backendz--rdzv_endpoint z#rendezvous backend server host:portz	--rdzv_idzuser defined group id)r   r   r   z--rdzv_confz5additional rdzv configuration (conf1=v1,conf2=v2,...)z--standaloneag  starts a local, standalone rdzv backend that is represented by etcd server on a random free portusing the etcd binary specified in TORCHELASTIC_ETCD_BINARY_PATH env var or the one found in PATH. Useful when launching single-node, multi-worker job. If specified --rdzv_backend, --rdzv_endpoint, --rdzv_id are autoassigned, any explicitly set values are ignored)r   r   z--max_restarts   z2max number of worker group restarts before failingz--monitor_interval   z5interval (in seconds) to monitor the state of workersz--start_methodspawn)r   fork
forkserverz9multiprocessing start_method to use when creating workers)r   r   r   choicesr   z--roler   z!user-defined role for the workers-mz--modulezwChanges each process to interpret the launch script as a python module, executing with the same behavior as'python -m'.z--no_pythonzxDo not prepend the training script with "python" - just exec it directly. Useful when the script is not a Python script.z	--log_dirNzbase dir to use for log files (e.g. /var/log/torchelastic) can reuse the same dir for multiple runs (a unique job-level subdir is created with rdzv_id as the prefix)z-rz--redirects0zstd streams to redirect into a log file in the log_dir (e.g. [-r 3] redirects both stdout+stderr for all workers, [-r 0:1,1:2] redirects stdout for local rank 0 and stderr for local rank 1)z-tz--teezPtee std streams into a log file and also to console (see --redirects for format)training_scriptzThe full path to the single GPU training program/script to be launched in parallel, followed by all the arguments for the training script)r   r   training_script_args)nargs)	r   add_argumentr
   strr	   intfloatr   
parse_args)argsparser r,   S/home/ubuntu/.local/lib/python3.10/site-packages/torchelastic/distributed/launch.pyr)      s   
		



r)   nnodesc                 C   sj   |  d}t|dkrt|d  }}||fS t|dkr-t|d }t|d }||fS td|  d)N:   r      znnodes=z is not in "MIN:MAX" format)splitlenr'   RuntimeError)r.   arr	min_nodes	max_nodesr,   r,   r-   parse_min_max_nnodes  s   
r8   nproc_per_nodec                 C   s   zt d|  d t| W S  tyk   | dkr t }d}n4| dkr5tj s-tdd}tj	 }n| dkrMtj rFtj	 }d}nt }d}ntd|  t
d|  d| d	t  d
|  | Y S w )NzUsing nproc_per_node=.cpugpuzCuda is not available.r   z"Unsupported nproc_per_node value: z, seting to z since the instance has  )logginginfor'   
ValueErroros	cpu_counttorchcudais_availabledevice_countlog)r9   num_procdevice_typer,   r,   r-   determine_local_world_size  s<   



rJ   returnc                 C   s&   | j | j| jd}tjdtjj|dS )N)rdzv_backendrun_idroleztorchelastic.main)namesourcemetadata)rL   rM   rN   r   EventEventSourceAGENT)r*   rQ   r,   r,   r-   _construct_event  s   
rU   c                 C   s|  t | } t| j\}}d|  k r|ksJ  J | jdks J d }| jrMt }|  d| _| | _	t
t | _td| j d| j	 d| j d t| j}dtjvrl|dkrld}td	| d
 t
|tjd< | j }g }|rtjdg}| jr|d n| jrtd|| j || j td| j| j	| j||dt| j }	t!"|	}
zzKt#| j$||d g |dd  R |
| j| j%t&'| j(t&'| j)d	}t*+  t,|| j-| j.d}|/|j$}t01|2t3j4 |5 rt6| j|j7dW n% t6y     t8y   |rt01|2t3j9  t01t:|   w W |
;  | jr.|<  d S d S |
;  | jr=|<  w w )Nr   r   zH
**************************************
Rendezvous info:
--rdzv_backend=z --rdzv_endpoint=z --rdzv_id=z(
**************************************
OMP_NUM_THREADSr0   zn*****************************************
Setting OMP_NUM_THREADS environment variable for each process to be z in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed. 
*****************************************z-ur    zODon't use both the '--no_python' flag and the '--module' flag at the same time.)backendendpointrM   r6   r7   )	rN   local_world_size
entrypointr*   rdzv_handlermax_restartsmonitor_interval	redirectstee)specstart_methodlog_dir)rO   failuresr,   )=r)   r8   r.   r\   
standaloner   startrL   get_endpointrdzv_endpointr&   uuiduuid4rdzv_idrG   r?   rJ   r9   rA   environprint	no_pythonsys
executablemoduleappendr@   r"   extendr#   r   r   	rdzv_confrdzv_registryget_rendezvous_handlerr   rN   r]   r   from_strr^   r_   r   initialize_metricsr   ra   rb   runr   r   get_agent_status_eventr   	SUCCEEDED	is_failedr   rc   	ExceptionFAILEDrU   shutdownstop)r*   r6   r7   elastic_agentetcd_serverr9   omp_num_threadswith_pythoncmdrdzv_parametersr[   r`   
run_resultr,   r,   r-   main  s   

		


	


	
r   __main__z3[%(levelname)s] %(asctime)s %(module)s: %(message)s)levelformatz3Running torchelastic.distributed.launch with args: )N)2__doc__r>   rA   rn   rh   argparser   r   rC    torchelastic.rendezvous.registry
rendezvousregistryrt   torchelasticr   r   torchelastic.agent.server.apir   r   -torchelastic.agent.server.local_elastic_agentr   &torchelastic.distributed.argparse_utilr	   r
   torchelastic.multiprocessingr   #torchelastic.multiprocessing.errorsr   r   torchelastic.rendezvousr   #torchelastic.rendezvous.etcd_serverr   torchelastic.rendezvous.utilsr   torchelastic.utils.loggingr   rG   r)   r&   r8   rJ   rR   rU   r   __name__basicConfigINFOr?   argvr,   r,   r,   r-   <module>   sD    T i
