Deep Learning Containers (DLC) lets you quickly create and run single-node or distributed training jobs. Built on Kubernetes, DLC saves you from manually provisioning machines and configuring runtime environments, allowing you to get started quickly without changing your usual workflow. This tutorial uses the MNIST handwritten digit recognition task to demonstrate how to perform single-node, single-GPU training and multi-node, multi-GPU distributed training with DLC.
The MNIST handwritten digit recognition task is one of the most classic introductory tasks in deep learning. The goal is to build a machine learning model to recognize 10 handwritten digits (from 0 to 9).

Prerequisites
Use your Alibaba Cloud 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 product.
Billing
The examples in this tutorial use public resources to create DLC jobs. These jobs are billed on a pay-as-you-go basis. For more information about the billing rules, see Billing of Deep Learning Containers (DLC).
Single-node, single-GPU training
Create a dataset
A dataset stores the code, data, and training results. This tutorial uses a dataset from Object Storage Service (OSS) as an example.
-
In the left-side navigation pane of the PAI console, choose Datasets > Custom Dataset > Create datasets.
-
Configure the dataset parameters. The following are the key parameters. You can leave the other parameters at their default values.
-
Name: For example,
dataset_mnist. -
Storage Type: OSS.
-
OSS Path: Click the
icon, select a bucket, and create a directory, such as dlc_mnist.If you have not activated OSS or if no bucket is available in the current region, follow these steps to activate OSS and create a bucket:
Click OK to create the dataset.
-
-
Upload the training code and data.
-
Download the code. This tutorial provides the required training script. Click mnist_train.py to download it. To reduce manual steps, the script automatically downloads the training data to the dataSet directory of the dataset at runtime.
For production use, we recommend that you upload your code and training data to your PAI dataset in advance.
-
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.
-
Create a DLC job
-
In the left-side navigation pane of the PAI console, choose Deep Learning Containers (DLC) > Create Job.
-
Configure the DLC job parameters. The following are the key parameters. You can use the default values for the remaining parameters. For a full list of parameters, see Create a training job.
-
Image Configuration: Select Image Address, and then enter the image URL that corresponds to your Region.
In the top navigation bar of the console, check your current region in the Region selector, for example, China (Hangzhou).
Region
Image URL
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
Look up your region ID and replace <region ID> in the image URL 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 has been verified in Quick Start for Interactive Modeling with PAI-DSW. A typical PAI workflow is to develop and verify code in PAI-DSW before using 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.pyThis startup command is the same as the one used when running in PAI-DSW or locally. However, because
mnist_train.pyis now mounted to/mnt/data/, the script path in the command is updated to/mnt/data/mnist_train.pyto reflect the mount location. -
Source: Select Public Resources. For Resource Type, select
ecs.gn7i-c8g1.2xlarge.If this instance type is out of stock, you can select another GPU instance type.
Click OK to create the job. The job takes about 15 minutes to complete. During execution, you can click Logs to view the training progress.
The training logs show that the MNIST model achieved a validation accuracy of 9886/10000 (about 99%) at Epoch 18. The job status is Succeeded, and the final output is
Training complete. writer.close().After the job is complete, the best model checkpoint and the TensorBoard logs are saved to the
outputpath of the mounted dataset.The
dlc_mnist/directory in the OSS bucket contains thedataSet/subdirectory (training data), theoutput/subdirectory (model output), and themnist_train.pytraining script. -
View TensorBoard (optional)
You can use the TensorBoard visualization tool to view the loss curve and learn more about the training process.
To use TensorBoard for a DLC job, you must configure a dataset.
-
On the DLC job details page, click the Tensorboard tab and then click Create TensorBoard.
-
Set Configuration Type to By Task. For Summary Path, enter the summary path specified 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 view the train_loss curve (which reflects the training set loss) and the validation_loss curve (which reflects the validation set loss).

Deploy the trained model
For more information, see Deploy a model as a service by using PAI-EAS.
Distributed training
If the video memory of a single GPU is insufficient for your training requirements, or if you want to accelerate the training process, you can create a single-node, multi-GPU or multi-node, multi-GPU distributed training job.
This tutorial uses an example of two instances, each with one GPU. This example also applies to other single-node, multi-GPU or multi-node, multi-GPU training configurations.
Create a dataset
If you already created a dataset for the single-node, single-GPU training, you only need to download and upload the mnist_train_distributed.py script. Otherwise, first create a dataset and then upload the script.
Create a DLC job
-
In the left-side navigation pane of the PAI console, choose Deep Learning Containers (DLC) > Create Job.
-
Configure the DLC job parameters. The following are the key parameters. You can use the default values for the remaining parameters. For a full list of parameters, see Create a training job.
-
Image Configuration: Select Image Address, and then enter the image URL that corresponds to your Region.
Region
Image URL
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
Look up your region ID and replace <region ID> in the image URL 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 has been verified in Quick Start for Interactive Modeling with PAI-DSW. A typical PAI workflow is to develop and verify code in PAI-DSW before using 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:
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. You can access them by using the$VARIABLE_NAMEformat. -
For Source, select Public Resources. Set Number of Nodes to 2. For Resource Type, select
ecs.gn7i-c8g1.2xlarge.If this instance type is out of stock, you can select another GPU instance type.
Click Confirm to create the job. The job takes about 10 minutes to complete. During execution, you can view the training Log for both instances on the Overview page.
After the job is complete, the best model checkpoint and the TensorBoard logs are saved to the
output_distributedpath of the mounted dataset. -
View TensorBoard (optional)
You can use the TensorBoard visualization tool to view the loss curve and learn more about the training process.
To use TensorBoard for a DLC job, you must configure a dataset.
-
On the DLC job details page, click the TensorBoard tab and then click Create TensorBoard.
-
Set Configuration Type to By Task. For Summary Path, enter the summary path specified in the training code:
/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 view the train_loss curve (which reflects the training set loss) and the validation_loss curve (which reflects the validation set loss).

Deploy the trained model
For more information, see Deploy a model as a service by using PAI-EAS.
Related documents
-
For more information about DLC features, see Deep Learning Containers (DLC).