a
    Þñh7*  ã                   @   sN   d Z ddlZddlZddlZddlmZmZ dd„ Zdd„ Ze	dkrJeƒ  dS )	a}  
`torch.distributed.launch` is a module that spawns up multiple distributed
training processes on each of the training nodes.

The utility can be used for single-node distributed training, in which one or
more processes per node will be spawned. The utility can be used for either
CPU training or GPU training. If the utility is used for GPU training,
each distributed process will be operating on a single GPU. This can achieve
well-improved single-node training performance. It can also be used in
multi-node distributed training, by spawning up multiple processes on each node
for well-improved multi-node distributed training performance as well.
This will especially be benefitial for systems with multiple Infiniband
interfaces that have direct-GPU support, since all of them can be utilized for
aggregated communication bandwidth.

In both cases of single-node distributed training or multi-node distributed
training, this utility will launch the given number of processes per node
(``--nproc_per_node``). If used for GPU training, this number needs to be less
or equal to the number of GPUs on the current system (``nproc_per_node``),
and each process will be operating on a single GPU from *GPU 0 to
GPU (nproc_per_node - 1)*.

**How to use this module:**

1. Single-Node multi-process distributed training

::

    >>> python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
               YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3 and all other
               arguments of your training script)

2. Multi-Node multi-process distributed training: (e.g. two nodes)


Node 1: *(IP: 192.168.1.1, and has a free port: 1234)*

::

    >>> python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
               --nnodes=2 --node_rank=0 --master_addr="192.168.1.1"
               --master_port=1234 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
               and all other arguments of your training script)

Node 2:

::

    >>> python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_YOU_HAVE
               --nnodes=2 --node_rank=1 --master_addr="192.168.1.1"
               --master_port=1234 YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3
               and all other arguments of your training script)

3. To look up what optional arguments this module offers:

::

    >>> python -m torch.distributed.launch --help


**Important Notices:**

1. This utility and multi-process distributed (single-node or
multi-node) GPU training currently only achieves the best performance using
the NCCL distributed backend. Thus NCCL backend is the recommended backend to
use for GPU training.

2. In your training program, you must parse the command-line argument:
``--local_rank=LOCAL_PROCESS_RANK``, which will be provided by this module.
If your training program uses GPUs, you should ensure that your code only
runs on the GPU device of LOCAL_PROCESS_RANK. This can be done by:

Parsing the local_rank argument

::

    >>> import argparse
    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument("--local_rank", type=int)
    >>> args = parser.parse_args()

Set your device to local rank using either

::

    >>> torch.cuda.set_device(arg.local_rank)  # before your code runs

or

::

    >>> with torch.cuda.device(arg.local_rank):
    >>>    # your code to run

3. In your training program, you are supposed to call the following function
at the beginning to start the distributed backend. You need to make sure that
the init_method uses ``env://``, which is the only supported ``init_method``
by this module.

::

    torch.distributed.init_process_group(backend='YOUR BACKEND',
                                         init_method='env://')

4. In your training program, you can either use regular distributed functions
or use :func:`torch.nn.parallel.DistributedDataParallel` module. If your
training program uses GPUs for training and you would like to use
:func:`torch.nn.parallel.DistributedDataParallel` module,
here is how to configure it.

::

    model = torch.nn.parallel.DistributedDataParallel(model,
                                                      device_ids=[arg.local_rank],
                                                      output_device=arg.local_rank)

Please ensure that ``device_ids`` argument is set to be the only GPU device id
that your code will be operating on. This is generally the local rank of the
process. In other words, the ``device_ids`` needs to be ``[args.local_rank]``,
and ``output_device`` needs to be ``args.local_rank`` in order to use this
utility

5. Another way to pass ``local_rank`` to the subprocesses via environment variable
``LOCAL_RANK``. This behavior is enabled when you launch the script with
``--use_env=True``. You must adjust the subprocess example above to replace
``args.local_rank`` with ``os.environ['LOCAL_RANK']``; the launcher
will not pass ``--local_rank`` when you specify this flag.

.. warning::

    ``local_rank`` is NOT globally unique: it is only unique per process
    on a machine.  Thus, don't use it to decide if you should, e.g.,
    write to a networked filesystem.  See
    https://github.com/pytorch/pytorch/issues/12042 for an example of
    how things can go wrong if you don't do this correctly.

é    N)ÚArgumentParserÚ	REMAINDERc                  C   sÂ   t dd�} | jdtddd� | jdtdd	d� | jd
tddd� | jddtdd� | jddtdd� | jddddd� | jdddddd� | jddddd� | jdtdd� | jd td!� |  ¡ S )"zU
    Helper function parsing the command line options
    @retval ArgumentParser
    zdPyTorch distributed training launch helper utility that will spawn up multiple distributed processes)Údescriptionz--nnodesé   z3The number of nodes to use for distributed training)ÚtypeÚdefaultÚhelpz--node_rankr   z8The rank of the node for multi-node distributed trainingz--nproc_per_nodez·The number of processes to launch on each node, for GPU training, this is recommended to be set to the number of GPUs in your system so that each process can be bound to a single GPU.z--master_addrz	127.0.0.1z©Master node (rank 0)'s address, should be either the IP address or the hostname of node 0, for single node multi-proc training, the --master_addr can simply be 127.0.0.1)r   r   r   z--master_porti<s  zdMaster node (rank 0)'s free port that needs to be used for communication during distributed trainingz	--use_envFÚ
store_truezÂUse environment variable to pass 'local rank'. For legacy reasons, the default value is False. If set to True, the script will not pass --local_rank as argument, and will instead set LOCAL_RANK.)r   Úactionr   ú-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.Útraining_scriptz‰The 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_argumentÚintÚstrr   Ú
parse_args)Úparser© r   úX/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/distributed/launch.pyr   ’   s<    

ÿ
ÿ
ÿ
ÿ
ÿ
ÿÿ
ÿÿr   c            	      C   st  t ƒ } | j| j }tj ¡ }| j|d< t| jƒ|d< t|ƒ|d< g }dtjvrv| jdkrvtdƒ|d< t	d 
|d ¡ƒ td| jƒD ]¼}| j| j | }t|ƒ|d< t|ƒ|d	< | j }g }|rÚtjd
g}| jrö| d¡ n| jsètdƒ‚| jrötdƒ‚| | j¡ | j�s| d 
|¡¡ | | j¡ tj||d�}| |¡ q‚|D ]*}| ¡  |jdk�rDtj|j|d�‚�qDd S )NZMASTER_ADDRZMASTER_PORTZ
WORLD_SIZEZOMP_NUM_THREADSr   a&  *****************************************
Setting OMP_NUM_THREADS environment variable for each process to be {} in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed. 
*****************************************r   ZRANKZ
LOCAL_RANKz-ur   zJWhen using the '--no_python' flag, you must also set the '--use_env' flag.zODon't use both the '--no_python' flag and the '--module' flag at the same time.z--local_rank={})Úenv)Ú
returncodeÚcmd)r   Znproc_per_nodeZnnodesÚosÚenvironÚcopyZmaster_addrr   Zmaster_portÚprintÚformatÚrangeZ	node_rankZ	no_pythonÚsysÚ
executableÚmoduleÚappendZuse_envÚ
ValueErrorr   Úextendr   Ú
subprocessÚPopenÚwaitr   ÚCalledProcessError)	ÚargsZdist_world_sizeZcurrent_envZ	processesZ
local_rankZ	dist_rankZwith_pythonr   Úprocessr   r   r   ÚmainÈ   sL    

û
ÿr+   Ú__main__)
Ú__doc__r   r%   r   Úargparser   r   r   r+   Ú__name__r   r   r   r   Ú<module>   s    6;