Help Center/ ModelArts/ Image Management/ Using a Custom Image to Train Models (Model Training)/ Using a Custom Image to Create a CPU- or GPU-based Training Job
Updated on 2024-08-14 GMT+08:00

Using a Custom Image to Create a CPU- or GPU-based Training Job

Model training is an iterative optimization process. Through unified training management, you can flexibly select algorithms, data, and hyperparameters to obtain the optimal input configuration and model. After comparing metrics between job versions, you can determine the most satisfactory training job.

Prerequisites

Creating a Training Job

  1. Log in to the ModelArts management console. In the left navigation pane, choose Model Training > Training Jobs. The training job list is displayed by default.
  2. Click Create Training Job and set parameters.
    Table 1 Creating a training job using a custom image

    Parameter

    Description

    Algorithm Type

    Select Custom algorithm. This parameter is mandatory.

    Boot Mode

    Select Custom image. This parameter is mandatory.

    Image

    Container image path. This parameter is mandatory.

    You can set the container image path in either of the following ways:
    • To select your image or an image shared by others, click Select on the right and select a container image for training. The required image must be uploaded to SWR beforehand.
    • To select a public image, enter the address of the public image in SWR. Enter the image path in the format of "Organization name/Image name:Version name". Do not contain the domain name in the path because the system will automatically add the domain name to the path.

    Code Directory

    Select the OBS directory where the training code file is stored. If the custom image does not contain training code, you need to set this parameter. If the custom image contains training code, you do not need to set this parameter.

    • Upload code to the OBS bucket beforehand. The total size of files in the directory cannot exceed 5 GB, the number of files cannot exceed 1000, and the folder depth cannot exceed 32.
    • The training code file is automatically downloaded to the ${MA_JOB_DIR}/demo-code directory of the training container when the training job is started. demo-code is the last-level OBS directory for storing the code. For example, if Code Directory is set to /test/code, the training code file is downloaded to the ${MA_JOB_DIR}/code directory of the training container.

    User ID

    User ID for running the container. The default value 1000 is recommended.

    If the UID needs to be specified, its value must be within the specified range. The UID ranges of different resource pools are as follows:

    • Public resource pool: 1000 to 65535
    • Dedicated resource pool: 0 to 65535

    Boot Command

    Command for booting an image. This parameter is mandatory.

    When a training job is running, the boot command is automatically executed after the code directory is downloaded.
    • If the training boot script is a .py file, train.py for example, the boot command is as follows:
      python ${MA_JOB_DIR}/demo-code/train.py
    • If the training boot script is a .sh file, main.sh for example, the boot command is as follows:
      bash ${MA_JOB_DIR}/demo-code/main.sh

    You can use semicolons (;) and ampersands (&&) to combine multiple commands. demo-code in the command is the last-level OBS directory where the code is stored. Replace it with the actual one.

    NOTE:

    To ensure data security, do not enter sensitive information, such as plaintext passwords.

    Local Code Directory

    Specify the local directory of a training container. When a training starts, the system automatically downloads the code directory to this directory.

    The default local code directory is /home/ma-user/modelarts/user-job-dir. This parameter is optional.

    Work Directory

    During training, the system automatically runs the cd command to execute the boot file in this directory.

    Table 2 Parameters for creating a training job

    Parameter

    Sub-Parameter

    Description

    Input

    Parameter

    The algorithm code reads the training input data based on the input parameter name.

    Set this parameter to data_url, which is the same as the parameter for parsing the input data in the training code. You can set multiple training input parameters. The name of each training input parameter must be unique.

    For example, if you use argparse in the training code to parse data_url into the data input, set the parameter name of the training input to data_url.

    import argparse
    # Create a parsing task.
    parser = argparse.ArgumentParser(description="train mnist", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    # Add parameters.
    parser.add_argument('--train_url', type=str, help='the path model saved')
    parser.add_argument('--data_url', type=str, help='the training data')
    # Parse the parameters.
    args, unknown = parser.parse_known_args()

    Dataset

    Click Dataset and select the target dataset and its version in the ModelArts dataset list.

    When the training job is started, ModelArts automatically downloads the data in the input path to the training container.

    NOTE:

    ModelArts data management is being upgraded and is invisible to users who have not used data management. It is recommended that new users store their training data in OBS buckets.

    Data path

    Click Data path and select the storage path to the training input data from an OBS bucket.

    When the training job is started, ModelArts automatically downloads the data in the input path to the training container.

    How to Obtain

    The following uses training input data_path as an example.

    • If you select Hyperparameters, use this code to obtain the data:
      import argparse
      parser = argparse.ArgumentParser()
      parser.add_argument('--data_path')
      args, unknown = parser.parse_known_args()
      data_path = args.data_path 
    • If you select Environment variables, use this code to obtain the data:
      import os
      data_path = os.getenv("data_path", "")

    Output

    Parameter

    The algorithm code reads the training output data based on the output parameter name.

    Set this parameter to train_url, which is the same as the parameter for parsing the output data in the training code. You can set multiple training output parameters. The name of each training output parameter must be unique.

    Data path

    Click Data path and select the storage path to the training output data from an OBS bucket. During training, the system automatically synchronizes files from the local code directory of the training container to the data path.

    NOTE:

    The data path can only be an OBS path. To prevent any issues with data storage, choose an empty directory as the data path.

    How to Obtain

    The following uses the training output train_url as an example.

    • If you select Hyperparameters, use this code to obtain the data:
      import argparse
      parser = argparse.ArgumentParser()
      parser.add_argument('--train_url')
      args, unknown = parser.parse_known_args()
      train_url = args.train_url 
    • If you select Environment variables, use this code to obtain the data:
      import os
      train_url = os.getenv("train_url", "")

    Predownload

    Indicates whether to pre-download the files in the output directory to a local directory.

    • If you set Predownload to No, the system does not download the files in the training output data path to a local directory of the training container when the training job is started.
    • If you set Predownload to Yes, the system automatically downloads the files in the training output data path to a local directory of the training container when the training job is started. The larger the file size, the longer the download time. To avoid excessive training time, remove any unneeded files from the local code directory of the training container as soon as possible. To use resumable training and incremental training, Download must be selected.

    Hyperparameters

    -

    Used for training tuning. This parameter is optional.

    Environment Variable

    -

    Add environment variables based on service requirements. For details about preset environment variables in the training container, see Viewing Environment Variables of a Training Container.

    Auto Restart

    -

    Once this function is enabled, you can set the number of restarts and whether to enable Unconditional auto restart.

    If a training job encounters an exception due to environment issues, ModelArts automatically handles the exception or isolates the abnormal node and then restarts the training job, improving the training success rate. To avoid training progress loss and computing power waste, ensure that the code has adapted to resumable training before enabling this function.

    The value ranges from 1 to 128. The default value is 3. The value cannot be changed once the training job is created. Set this parameter based on your needs.

    If Unconditional auto restart is selected, the training job will be restarted unconditionally once the system detects a training exception. To prevent invalid restarts, the training job can be unconditionally restarted three times consecutively.

    If auto restart is triggered during training, the system records the restart information. You can check the fault recovery details on the training job details page.

  3. Select an instance flavor. The value range of the training parameters is consistent with the constraints of existing custom images. Select a public resource pool or dedicated resource pool as required. For details about the parameters, see Creating a Training Job.
  1. Click Submit to create the training job.

    It takes a period of time to create a training job.

    To view the real-time status of a training job, go to the training job list and click the name of the training job. On the training job details page that is displayed, view the basic information of the training job. For details, see Viewing Training Job Details.