Help Center/ ModelArts/ ModelArts User Guide (Standard)/ Model Training/ Distributed Model Training/ Example: Creating a DDP Distributed Training Job (PyTorch + NPU)
Updated on 2024-10-29 GMT+08:00

Example: Creating a DDP Distributed Training Job (PyTorch + NPU)

This section describes how to use a custom image and boot command to start PyTorch DDP training powered by Ascend accelerator cards.

Prerequisites

An Ascend accelerator card resource pool is available.

Creating a Training Job

The following table describes the parameters you need to configure during training job creation.

Table 1 Parameters for creating a training job

Parameter

Description

Algorithm Type

Select Custom algorithm.

Boot Mode

Select Custom image.

Image

Select a custom image for training.

Code Directory

Select the code directory required for this training job, for example, obs://test-modelarts/ascend/code/ in this case.

Boot Command

Python boot command of the image, for example, bash ${MA_JOB_DIR}/code/run_torch_ddp_npu.sh in this case. For details about the complete code of the boot script, see Code Example.

(Optional) Enabling Ranktable Dynamic Routing

To use ranktable dynamic routing for network acceleration, contact technical support to enable cabinet scheduling permission. Additionally, the training job must meet the following requirements:

  • The training job must use Python 3.7 or 3.9.
  • The training job must have at least 3 task nodes.
  • The training job must use the same rank number in the code. The algorithm accelerates routing by changing the rank number.

Follow these steps and start the training job to accelerate the network.

  • Change NODE_RANK="$VC_TASK_INDEX" in the training boot script to NODE_RANK="$RANK_AFTER_ACC".
  • Change MASTER_ADDR="${VC_WORKER_HOSTS%%,*}" in the training boot script to MASTER_ADDR="${MA_VJ_NAME}-${MA_TASK_NAME}-${MA_MASTER_INDEX}.${MA_VJ_NAME}".
  • When creating the training job, set the environment variable ROUTE_PLAN to true. For details, see Managing Environment Variables of a Training Container.

Code Example

The following shows an example boot script of a training job.

To store the generated plog data, you need to specify the path in the startup script as /home/ma-user/modelarts/log/modelarts-job-{id}/worker-{index}/. The system will automatically upload the *.log file in the /home/ma-user/modelarts/log/ directory to the OBS log directory of your training job. The system will only upload the log files (larger than 0 MB) in the local directory to the corresponding parent directory. Unlike MindSpore, PyTorch NPU plog logs are organized by worker instead of rank ID. PyTorch NPU does not rely on the rank table file.

#!/bin/bash

# MA preset envs
MASTER_HOST="$VC_WORKER_HOSTS"
MASTER_ADDR="${VC_WORKER_HOSTS%%,*}"
NNODES="$MA_NUM_HOSTS"
NODE_RANK="$VC_TASK_INDEX"
# also indicates NPU per node
NGPUS_PER_NODE="$MA_NUM_GPUS"

# self-define, it can be changed to >=10000 port
MASTER_PORT="38888"

# replace ${MA_JOB_DIR}/code/torch_ddp.py to the actutal training script
PYTHON_SCRIPT=${MA_JOB_DIR}/code/torch_ddp.py
PYTHON_ARGS=""

export HCCL_WHITELIST_DISABLE=1

# set npu plog env
ma_vj_name=`echo ${MA_VJ_NAME} | sed 's:ma-job:modelarts-job:g'`
task_name="worker-${VC_TASK_INDEX}"
task_plog_path=${MA_LOG_DIR}/${ma_vj_name}/${task_name}

mkdir -p ${task_plog_path}
export ASCEND_PROCESS_LOG_PATH=${task_plog_path}

echo "plog path: ${ASCEND_PROCESS_LOG_PATH}"

# set hccl timeout time in seconds
export HCCL_CONNECT_TIMEOUT=1800

# replace ${ANACONDA_DIR}/envs/${ENV_NAME}/bin/python to the actual python
CMD="${ANACONDA_DIR}/envs/${ENV_NAME}/bin/python -m torch.distributed.launch \
    --nnodes=$NNODES \
    --node_rank=$NODE_RANK \
    --nproc_per_node=$NGPUS_PER_NODE \
    --master_addr=$MASTER_ADDR \
    --master_port=$MASTER_PORT \
    --use_env \
    $PYTHON_SCRIPT \
    $PYTHON_ARGS
"
echo $CMD
$CMD