Submit Slurm Training Jobs Quickly

Updated at:

Slurm is a commonly used high-performance computing (HPC) job scheduling system. PAI-DLC lets you submit training jobs with Slurm and reuse your existing Slurm scripts to run deep learning training.

Prerequisites

Submit a Slurm training job

Step 1: Prepare the training script and dataset

Prepare the following training dataset and training script, and then upload them to your OSS bucket. For more information, see Console quick start.

  • Training dataset: mnist.npz.

  • Training script: mnist_train.py. The following code provides an example:

    import numpy as np
    import tensorflow as tf
    from tensorflow.keras import layers, models
    
    # Load the MNIST dataset from a local file
    with np.load('/mnt/test/mnist.npz', allow_pickle=True) as data:
        train_images = data['x_train']
        train_labels = data['y_train']
        test_images = data['x_test']
        test_labels = data['y_test']
    
    # Normalize the dataset
    train_images, test_images = train_images / 255.0, test_images / 255.0
    
    # Add a channel dimension
    train_images = train_images[..., tf.newaxis]
    test_images = test_images[..., tf.newaxis]
    
    # Build the model
    model = models.Sequential([
        layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(64, (3, 3), activation='relu'),
        layers.MaxPooling2D((2, 2)),
        layers.Conv2D(64, (3, 3), activation='relu'),
        layers.Flatten(),
        layers.Dense(64, activation='relu'),
        layers.Dense(10)
    ])
    
    # Compile the model
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
    
    # Create a TensorBoard callback
    tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="/mnt/test/mnist_logs", histogram_freq=1)
    
    # Train the model
    model.fit(train_images, train_labels, epochs=5,
              validation_data=(test_images, test_labels),
              callbacks=[tensorboard_callback])

Step 2: Submit the training job

  1. Go to the Create Task page.

    1. Log on to the PAI console, select the target region and target workspace at the top of the page, and then click DLC.

    2. On the Deep Learning Containers (DLC) page, click Create Task.

  2. On the Create Job page, configure the following key parameters. For more information about other parameters, see Create a training job.

    Parameter

    Description

    Basic Information

    Job Name

    Enter a custom job name, such as mnist_cnn_tb.

    Environment Information

    Image Configuration

    Use a Linux-based runtime image. No special requirements apply. This example uses the Alibaba Cloud Image > tensorflow-training:2.11-gpu-py39-cu112-ubuntu20.04.

    Mount storage

    Mount the training script and dataset to a specified directory in the container.

    The following example uses an OSS mount:

    Click OSS, and configure the following parameters:

    • URI: Select the OSS storage path where the script file mnist_train.py is stored.

    • Mount Path: Specify the path in the DLC container where the script file is mounted. For example, /mnt/test/.

    Startup Command

    The command that the job runs:

    • Start the command with #!/bin/bash.

    • You can customize SBATCH parameters. However, avoid manually setting resource allocation parameters, such as the number of CPU cores, the amount of memory, and the number of GPUs. The platform automatically allocates resources.

      # Supported.
      #SBATCH --job-name=mnist_cnn_tb    # Job name.
      #SBATCH --output=/mnt/test/mnist_train_%j.out    # Output and error log file name.
      # Not recommended. Correctness is not guaranteed.
      #SBATCH --cpus-per-task=<N>    # Number of CPU cores per task.
      #SBATCH --mem=<M>    # Memory required, in MB.
      #SBATCH --mem-per-cpu=<M>    # Memory per CPU core.
      #SBATCH --gres=<resource>:<N>    # Specific resources requested, such as GPU (gpu:1) or other generic resources.
      #SBATCH --nodes=<N>    # Number of nodes requested.
      #SBATCH --ntasks=<N>    # Total number of tasks.
      #SBATCH --ntasks-per-node=<N>    # Number of tasks per node.
      #SBATCH --partition=<partition>    # Partition to submit the job to.
      #SBATCH --time=<time>    # Maximum run time for the job.
      #SBATCH --account=<account>    # Account to use.

    This job uses the following startup command:

    #!/bin/bash
    #SBATCH --job-name=mnist_cnn_tb   			# Job name.
    #SBATCH --output=/mnt/test/mnist_train_%j.out  		# Output and error log file name.
    
    # Run the training script.
    srun /usr/bin/python3  /mnt/test/mnist_train.py

    Resource Information

    Source

    Select Public Resources. You can also choose other resource types as needed.

    Framework

    Select Slurm.

    Job Resource

    Configure Master and Worker resources as follows:

    • Number of Nodes: Set to 1.

    • Resource Type: Set to ecs.g6.xlarge.

    Retention Period

    Set the retention period for instances after job completion to 1 hour. This lets you enter the container to troubleshoot issues if the job fails.

    Note

    Enabling job retention consumes system resources continuously.

  3. Click OK.

    The Task Precheck dialog box appears.

    The dialog box contains the results of four prechecks: Total Quota Check, Node Specification Check, Network Connectivity Check, and Mount Path Check.

    The Slurm Sbatch Command is automatically generated by the system to submit the job. After the job is submitted, the system automatically mounts the script file to the /usr/config/user_script directory and runs it with the selected resource parameters.

  4. Click Submit Anyway.

    You are redirected to the Deep Learning Containers (DLC) page, where you can view the submitted training job.

Manage training jobs

You can perform the following operations to manage Slurm jobs. For more information, see Manage training jobs.

  • You can filter jobs by framework type to find Slurm jobs.

  • You can view job details.

    On the Deep Learning Containers (DLC) page, click the name of the target job to go to the Overview page. On that page, you can view the Slurm job's basic, resource, environment, and instance information.

  • You can determine the cause of a job failure.

    In this example, the job's Startup Command specifies the error log path /mnt/test/mnist_train_%j.out. This path is set by the #SBATCH --output directive in the Slurm script. If the job fails, go to the job's Overview page. In the Instance section, find the failed instance, click Actions in its column, and then select Access Container. View the error log file mnist_train_%j.out in the specified directory. You can also view it in the mounted OSS path. The following example shows the result for this job. Your results may differ. After you access the container, run cd /mnt/test/ to switch to the mounted directory. Then run the ll command to view the mnist_logs log directory, the mnist_train.py script file, and the mnist_train_2.out output log file.

  • You can view job results.

    This job's training script presets the output path /mnt/test/mnist_logs to save results. After the job runs successfully, you can view the job results in either of the following ways:

    • On the job's Overview page, go to the Instance section, find the target instance, click Actions in its column, and then select Access Container. View the operational logs in the specified directory.

    • View the job results in the mounted OSS path.

References

DLC also supports training jobs that use various frameworks, such as PyTorch and MPIJob. For more information, see Deep Learning Containers (DLC).