Updated on 2025-08-18 GMT+08:00

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

PyTorch's DDP enables efficient distributed training on Ascend accelerator cards. Implementing this with custom images and boot commands can pose challenges.

This guide offers a solution: configure custom images and boot commands to run PyTorch DDP training on Ascend accelerator cards. It supports user-specific needs and adapts to various training scenarios.

With this approach, users can streamline PyTorch DDP training for better performance on 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. For details, see Enabling Dynamic Route Acceleration for Training Jobs.

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