Deep Learning Containers (DLC) allows you to quickly create single-node or distributed training jobs without manually provisioning machines or configuring environments. This tutorial demonstrates the end-to-end process for both single-node, single-GPU and multi-node, multi-GPU training using the MNIST handwritten digit recognition task.
MNIST handwritten digit recognition is a classic introductory task in deep learning. The goal is to build a machine learning model to recognize 10 handwritten digits (0 to 9).
1. Prerequisites
Use your main account to activate PAI and create a workspace. Log on to the PAI console, select a region in the upper-left corner, and then follow the prompts to grant permissions and activate the service.
2. Billing
The examples in this tutorial use public resources to create DLC jobs, which are billed on a pay-as-you-go basis. For more information, see Billing for Deep Learning Containers (DLC).
3. Single-node, single-GPU training
3.1 Create a dataset
A dataset stores your training code, data, and model artifacts. This section explains how to use a dataset based on Alibaba Cloud Object Storage Service (OSS).
In the left-side navigation pane of the PAI console, choose Datasets > Custom Dataset > Create datasets.
Configure the following key parameters and keep the default values for the others.
Name: For example, enter
dataset_mnist.Storage Type: Select OSS.
OSS Path: Click the folder icon
, select a bucket, and create a new directory, such as dlc_mnist.If you have not activated OSS or do not have a bucket available in the current region, follow these steps to activate OSS and create a bucket:
Click Confirm to create the dataset.
Upload the training code and data.
Download the provided training script: mnist_train.py. To simplify the process, the script automatically downloads the training data to the dataSet directory of your dataset during runtime.
For your own projects, you can pre-upload your code and training data to a PAI dataset.
Upload the code. On the dataset details page, click View Data to go to the OSS console. Then, click Upload Object >Select Files > Upload Object to upload the training script to OSS.
3.2 Create a DLC job
In the left-side navigation pane of the PAI console, choose Deep Learning Containers (DLC) > Create Job.
Configure the following key parameters and keep the default values for the others. For a full list of parameters, see Create a training job.
Image Configuration: Select Image Address, and then enter the appropriate image address for your Region.
In the top navigation bar of the console, use the Region selector to confirm your current region, such as China (Hangzhou).
Region
Image address
China (Beijing)
dsw-registry-vpc.cn-beijing.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
China (Shanghai)
dsw-registry-vpc.cn-shanghai.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
China (Hangzhou)
dsw-registry-vpc.cn-hangzhou.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
Other
Find your region ID and replace <Region ID> in the image address to get the full URL:
dsw-registry-vpc.<Region ID>.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
This image uses the same verified environment from the Get Started with DSW Interactive Modeling topic. When modeling with PAI, a typical workflow is to first verify the environment and develop code in DSW, and then use DLC for training.
Dataset Mount: Select Custom Dataset and choose the dataset that you created in the previous step. The default Mount Path is
/mnt/data.Startup Command:
python /mnt/data/mnist_train.pyThe startup command is the same as when you run the code in DSW or locally. However, because
mnist_train.pyis now mounted to/mnt/data/, you only need to change the code path to/mnt/data/mnist_train.py.Source: Select Public Resources, and then select
ecs.gn7i-c8g1.2xlargefor the Resource Type.If this instance type is out of stock, you can choose another GPU instance type.
Click OK to create the job. The job takes about 15 minutes to run. During execution, you can click Logs to view the training progress.
The training log shows that the MNIST model reaches a validation accuracy of 9886/10000 (approximately 99%) at Epoch 18. When the job status changes to Succeeded, the log displays the final output:
Training complete. writer.close().After the job completes, DLC saves the best model checkpoint and TensorBoard log to the
outputpath of the mounted dataset.The
dlc_mnist/directory in your OSS bucket now contains adataSet/subdirectory for the training data, anoutput/subdirectory for the model artifacts, and themnist_train.pyscript.
3.3 (Optional) view in TensorBoard
You can use the TensorBoard visualization tool to view the loss curve and better understand the training process.
To use TensorBoard for a DLC job, you must configure a dataset.
On the details page of the DLC job, click the Tensorboard tab, and then click Create TensorBoard.
Set Configuration Type to By Task. For Summary Path, enter the path where the SummaryWriter saves data in the training code:
/mnt/data/output/runs/. Click OK to start.This corresponds to the code snippet:
writer = SummaryWriter('/mnt/data/output/runs/mnist_experiment')Click View TensorBoard to see the train_loss curve (training set loss) and the validation_loss curve (validation set loss).

3.4 Deploy the trained model
For more information, see V. Use EAS to deploy model services.
4. Distributed training with multiple GPUs
If a single GPU lacks sufficient memory for your model or you want to accelerate training, create a multi-GPU or multi-node distributed training job.
This section uses two instances, each with a single GPU, as an example. The same procedure applies to other multi-GPU and multi-node configurations.
4.1 Prepare the dataset and script
If you have already created a dataset in the 3. Single-node, single-GPU training section, simply download mnist_train_distributed.py and upload it. Otherwise, first 3.1 Create a dataset, and then upload the code.
4.2 Create a distributed DLC job
In the left-side navigation pane of the PAI console, choose Deep Learning Containers (DLC) > Create Job.
Configure the following key parameters and keep the default values for the others. For a full list of parameters, see Create a training job.
Image Configuration: Select Image Address, and then enter the appropriate image address for your Region.
Region
Image address
China (Beijing)
dsw-registry-vpc.cn-beijing.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
China (Shanghai)
dsw-registry-vpc.cn-shanghai.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
China (Hangzhou)
dsw-registry-vpc.cn-hangzhou.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
Other
Find your region ID and replace <Region ID> in the image address to get the full URL:
dsw-registry-vpc.<Region ID>.cr.aliyuncs.com/pai/modelscope:1.28.0-pytorch2.3.1tensorflow2.16.1-gpu-py311-cu121-ubuntu22.04
This image uses the same verified environment from the Get Started with DSW Interactive Modeling topic. When modeling with PAI, a typical workflow is to first verify the environment and develop code in DSW, and then use DLC for training.
Dataset Mount: Select Custom Dataset and choose the dataset that you created. The default Mount Path is
/mnt/data.Startup Command:
torchrun --nproc_per_node=1 --nnodes=${WORLD_SIZE} --node_rank=${RANK} --master_addr=${MASTER_ADDR} --master_port=${MASTER_PORT} /mnt/data/mnist_train_distributed.pyDLC automatically injects common environment variables, such as
MASTER_ADDRandWORLD_SIZE, which you can access with the$VARIABLE_NAMEsyntax.Source: Select Public Resources, set Number of Nodes to 2, and select
ecs.gn7i-c8g1.2xlargefor the Resource Type.If this instance type is out of stock, you can choose another GPU instance type.
Click Confirm to create the job. The job takes about 10 minutes to run. During execution, you can view the training Log for both instances on the Overview page.
After the job completes, DLC saves the best model checkpoint and TensorBoard log to the
output_distributedpath of the mounted dataset.
4.3 (Optional) view in TensorBoard
You can use the TensorBoard visualization tool to view the loss curve and better understand the training process.
To use TensorBoard for a DLC job, you must configure a dataset.
On the details page of the DLC job, click the TensorBoard tab, and then click Create TensorBoard.
Set Configuration Type to By Task. For Summary Path, enter the path where the training code saves the summary files:
/mnt/data/output_distributed/runs. Click OK to start.This corresponds to the code snippet:
writer = SummaryWriter('/mnt/data/output_distributed/runs/mnist_experiment')Click View TensorBoard to see the train_loss curve (training set loss) and the validation_loss curve (validation set loss).

4.4 Deploy the trained model
For more information, see V. Deploying model services using EAS.
5. Related information
For more information about DLC features, see Deep Learning Containers (DLC).
To learn how to estimate the GPU memory required for a large model, see Estimate the required GPU memory for a large model.