Help Center/ ModelArts/ SDK Reference/ Training Management/ Training Jobs/ Debugging a Training Job/ Using the SDK to Debug a Single-Node Training Job
Updated on 2024-06-12 GMT+08:00

Using the SDK to Debug a Single-Node Training Job

Replace the OBS paths in the debugging code with your OBS paths.

PyTorch is used to write debugging code in this document. The process is the same for different AI frameworks. You only need to change the framework_type value in 6 and 10. For example, set framework_type to Ascend-Powered-Engine for MindSpore.

  1. Initialize session.

    The following is the sample code.
    from modelarts.session import Session 
    session = Session()

  1. Prepare training data. Three data formats are supported. You can select one of them as required.

    import os
    from modelarts.train_params import InputData
    base_bucket_path = "obs://modelarts-xxx-a0de02a6/dis-train/cifar10/"
    base_local_path = "/home/ma-user/work/cifar10/"
    
    # Format 1: The data is stored in a compressed file in OBS.
    obs_path = os.path.join(base_bucket_path, "dataset-zip/dataset.zip")
    data_local = os.path.join(base_local_path, "dataset/") 
    input_data = InputData(obs_path=obs_path, local_path=data_local, is_local_source=False)
    
    # Format 2: The data is stored in a directory in OBS.
    #obs_path = os.path.join(base_bucket_path, "dataset/")
    #data_local = os.path.join(base_local_path, "dataset/") 
    #input_data = InputData(obs_path=obs_path, local_path=data_local, is_local_source=False)
    
    # Format 3: The data is stored in a directory in an SFS system mounted to a notebook instance. 
    #obs_path = os.path.join(base_bucket_path, "dataset-local/")
    #data_local = os.path.join(base_local_path, "dataset/") 
    #input_data = InputData(obs_path=obs_path, local_path=data_local, is_local_source=True)

    Parameters:

    • is_local_source: Location where the training data is stored. The default value is False and this parameter is optional.
      • False: The training data is stored in the path specified by obs_path.
      • True: The training data is stored in a notebook instance, which is specified by local_path.
    • obs_path: OBS path. It depends on the value of is_local_source.
      • If is_local_source is set to False, this parameter is mandatory, indicating the location where the training data is stored, which can be a folder or a compressed file.
      • If is_local_source is set to True, this parameter is optional. If you set this parameter, the training data in the notebook instance is compressed and uploaded to the location. The data cannot be uploaded repeatedly. After data is uploaded for the first time, change is_local_source to False and set obs_path to the location where the compressed file was uploaded. If you do not set this parameter, the file will not be compressed and uploaded.
    • local_path: Notebook path. Your training script reads data from this path for training. This parameter is mandatory. It depends on the value of is_local_source.
      • If is_local_source is set to True, this parameter indicates the location where the training data is stored, which can be a folder.
      • If is_local_source is set to False, the SDK downloads data to this location during training. If the training data is compressed files, they will be decompressed after being downloaded.

  1. Prepare the training script.

    from modelarts.train_params import TrainingFiles
    code_dir = os.path.join(base_local_path, "train/") 
    
    # The training script has been stored in OBS. The training script can be chosen from any source as long as it can be stored in a notebook instance.
    
    session.obs.download_file(os.path.join(base_bucket_path, "train/test-pytorch.py"), code_dir)
    training_file = TrainingFiles(code_dir=code_dir, boot_file="test-pytorch.py", obs_path=base_bucket_path + 'train/')

    Parameters:

    • code_dir: Code directory where a training script is stored. The directory must be a notebook directory for debugging a training job. This parameter is mandatory.
    • boot_file: Path of the training boot file. Enter the relative path of code_dir. For example, if the absolute path of boot_file is /home/ma-user/work/cifar10/train/test-pytorch.py, set this parameter to test-pytorch.py. This parameter is mandatory.
    • obs_path: OBS path. This parameter must be set only for remote training. The training script is compressed and uploaded to this path.

  1. Prepare the training output. If you do not need to upload the training output to OBS, skip this step.

    from modelarts.train_params import OutputData
    output = OutputData(local_path=os.path.join(base_local_path, "output/"), obs_path=os.path.join(base_bucket_path, 'output/'))
    • local_path: Notebook path, in which the trained model or other training script data is stored.
    • obs_path: OBS path. The SDK automatically uploads the model file in local_path to this OBS path. This parameter is mandatory.

  1. Check the AI frameworks that can be used for training.

    from modelarts.estimatorV2 import Estimator
    Estimator.get_framework_list(session)

    session is the initialized data in 1. Skip this step if the AI framework has been specified.

  1. Initialize the Estimator.

    from modelarts.estimatorV2 import Estimator
    parameters = []
    parameters.append({"name": "data_url", "value": data_local})
    parameters.append({"name": "output_dir", "value": os.path.join(base_local_path, "output/")}) 
    parameters.append({"name": "epoc_num", "value": 2}) 
    estimator = Estimator(session=session,
                          training_files=training_file,
                          outputs=[output],
                          parameters=parameters,
                          framework_type='PyTorch',  
                          train_instance_type='local', 
                          train_instance_count=1,
                          script_interpreter="/home/ma-user/anaconda3/envs/PyTorch-1.4/bin/python",
                          log_url=base_bucket_path + 'log/',
                          job_description='This is a image net train job')

    Parameters:

    • session: Initialized data in 1. This parameter is mandatory.
    • training_files: Initialized training files in 3. This parameter is mandatory.
    • outputs: A list of training outputs. Each element in the list is an initialized training output in 4. This parameter is optional.
    • parameters: A list of parameters. This parameter is optional. Each element in the list is a dictionary that contains the name and value fields, which are transferred to the training boot file in the form of --name=value. value can be a string, an integer, or a Boolean. For Boolean, use action='store_true' in the training script for parsing.
    • framework_type: Type of the AI framework used for a training job. For details, see the output item in 5. This parameter is mandatory.
    • train_instance_type: Training instance type. If this parameter is set to local, the training job is performed in a notebook instance. This parameter is mandatory.
    • train_instance_count: Number of workers in a training job. Set this parameter to 1 for single-node training. The training job runs only in the current notebook instance. This parameter is mandatory.
    • script_interpreter: Python environment used for a training job. If this parameter is not set, the current kernel is used by default. This parameter is optional.
    • log_url: OBS address. The SDK automatically uploads training logs to this address during training. This parameter must be set only when training jobs run on Ascend.
    • job_description: describes a training job. This parameter is optional.

  1. Start training.

    estimator.fit(inputs=[input_data], job_name="cifar10-dis")

    Parameters:

    • inputs: A list of training inputs. Each element in the list is an input imported in 2. This parameter is optional.
    • job_name: Name of a training job. This parameter is optional.
    After a local single-node training job starts, the SDK automatically performs the following operations:
    1. Initializes the training job. If the training data imported in 2 is stored in OBS, the data is downloaded to local_path.
    2. Executes the training job and saves the training outputs in local_path specified in 4.
    3. Uploads the training output to obs_path specified in 4 and the logs to log_url specified in 6.

    In addition, time suffixes are added to the job names.

    from datetime import datetime, timedelta
    import time
    base_name = "cifar10-dis"
    job_name = base_name + '-' + (datetime.now() + timedelta(hours=8)).strftime('%Y%m%d-%H%M%S')
    estimator.fit(inputs=[input_data], job_name=job_name)

  1. Perform debugging.

    In the previous step, the logs of the training script are printed to the console in real time. You can easily detect incorrect code or parameters in the logs. Perform debugging in Notebook until you obtain a desired result, then you can go to the next step.

  1. Obtain the type and maximum number of compute nodes available for training.

    from modelarts.estimatorV2 import Estimator 
    Estimator.get_spec_list(session=session)

    session is the initialized data in 1. A dictionary is returned. flavors is a list that describes all flavors available for training. flavor_id of each element indicates the compute flavors that can be directly used for remote training jobs, and max_num indicates the maximum number of compute nodes of the flavors. Skip this step if the compute flavor has been specified.

  1. Submit the remote training job.

    from modelarts.estimatorV2 import Estimator
    parameters = []
    parameters.append({"name": "data_url", "value": data_local})
    parameters.append({"name": "output_dir", "value": os.path.join(base_local_path, "output/")}) 
    parameters.append({"name": "epoch_num", "value": 2}) 
    estimator = Estimator(session=session,
                          training_files=training_file,
                          outputs=[output],
                          parameters=parameters,
                          framework_type='PyTorch',  
                          train_instance_type='modelarts.vm.cpu.8u', 
                          train_instance_count=1,
                          script_interpreter="/home/ma-user/anaconda3/envs/PyTorch-1.4/bin/python",
                          log_url=base_bucket_path + 'log/',
                          job_description='This is a image net train job')
    estimator.fit(inputs=[input_data], job_name="cifar10-dis")

    After the local debugging is complete, you only need to change train_instance_type to the value of flavor_id obtained in 9 during Estimator initialization. After the fit function is executed, you can submit the remote training job.

    After the training job is submitted, the SDK automatically performs the following operations:

    1. Zips the training script and uploads the ZIP file to obs_path specified in 3.
    2. Zips the data and uploads the ZIP file to the specified obs_path if the training data is stored in Notebook.
    3. Submits the training job created using a custom image to ModelArts. The image is that of the current instance. This ensures that the environment of the remote training job is the same as that of the training job in the notebook instance.
    4. Uploads the training output to obs_path specified in 4 and the logs to the location specified by log_url in this step.

      In this step, note the following:

      If you want to create a directory or file in your training script, create it in the following directories:

      • /home/ma-user/work
      • /cache
      • local_path specified in inputs or outputs. For example, if local_path is set to /home/ma-user/work/xx/yy/ during InputData initialization in 2, you can create directories or files in this directory.