Tool-based Migration

Tool-based migration supports migration using the command line.

Constraints

Note

  • If your training script contains the amp_C module, which is not supported on the NPU platform, you need to manually delete it before training.
  • Due to platform differences between the converted script and the original script, the migrated script may throw exceptions during debugging and running due to operator differences or other reasons, causing the process to terminate. Such exceptions need to be further debugged and resolved according to the exception information.

How to Use

The core steps for tool-based migration using the command line are as follows:

  1. Set up the environment.

    1. Install the development suite Ascend-cann-toolkit by referring to CANN Quick Installation.

    2. Install the following dependencies (root user is used as an example; the --user parameter needs to be added for non-root users).

      pip3 install pandas         # pandas version must be equal to or greater than 1.2.4
      pip3 install libcst         # Python syntax tree parser, used to parse Python files
      pip3 install prettytable    # Display tabular data in a formatted table
      pip3 install jedi           # Used for cross-file parsing, recommended to install
      
  2. Go to the path where the migration tool is located.

    cd Ascend-cann-toolkit_installation_directory/cann/tools/ms_fmk_transplt/
    
  3. Go to the path where the analysis and migration tool is located, and execute the script migration task.

    ./pytorch_gpu2npu.sh -i Original_script_path -o Output_path_of_script_migration_result -v original_framework_version [-s] [distributed -m entry_file_of_training_script -t target_model_variable_name]
    

    distributed and its parameters -m and -t are specified at the end of the statement.

    Example:

    # Single-device
    ./pytorch_gpu2npu.sh -i /home/train/ -o /home/out -v 2.1.0 [-s]
    # Distributed
    ./pytorch_gpu2npu.sh -i /home/train/ -o /home/out -v 2.1.0 [-s] distributed -m /home/train/train.py [-t model]
    

    "[]" indicates optional parameters, which can be omitted in actual use.

    Table 1 Parameter description

    Parameter Description Example Value
    -i
    --input
    • Path to the folder containing the original script files to be migrated.
    • Mandatory.
    /home/username/fmktransplt
    -o
    --output
    • Output path for the script migration result files.
    • When distributed is not enabled, i.e., migrating a single-device script, the output directory is named xxx_msft; when distributed is enabled, i.e., migrating a multi-device script, the output directory is named xxx_msft_multi, where xxx is the name of the folder containing the original scripts.
    • Mandatory.
    /home/username/fmktransplt_output
    -v
    --version
    • PyTorch version of the scripts to be migrated.
    • Mandatory.
    • 2.1.0
    • 2.6.0
    • 2.7.1
    • 2.8.0
    -s
    --specify-device
    • Specifies a device via the environment variable DEVICE_ID as an advanced feature, but this may cause the distributed functionality in the original script to become invalid.
    • Optional.
    -
    distributed • Migrates a GPU single-device script to an NPU multi-device script. This is only supported for scenarios where data is loaded using torch.utils.data.DataLoader. For details, refer to the "Migrating Training > Typical Cases > Loading Data Using torch.utils.data.DataLoader" section in the CANN Analysis and Migration Tool User Guide. The -t/--target_model parameter can only be specified when this parameter is set.
    • -m, --main: Entry Python file for the training script. Mandatory.
    • -t, --target_model: Name of the instantiated model variable in the script to be migrated. The default value is model. This parameter is optional. If the variable name is not model, this parameter must be configured. For example, for my_model = Model(), configure it as -t my_model.
    -
    -h
    --help
    Displays help information. -

Precautions

If the distributed parameter is enabled to migrate a GPU single-device script to an NPU multi-device script, perform the following operations to obtain the result files.

Note

After migrating a GPU single-device script to an NPU multi-device script, if the original model training command contains a parameter for specifying a device ID for single-device execution (such as --gpu), you need to delete this parameter to ensure that multi-device execution does not become invalid.

  1. Replace the training script statement.

    Replace the please input your shell script here statement in the run_distributed_npu.sh file generated after executing the migration command with the model's original training shell script. For example, replace please input your shell script here with the model training command bash model_train_script.sh --data_path data_path.

    The run_distributed_npu.sh file is as follows:

    export MASTER_ADDR=127.0.0.1 
    export MASTER_PORT=29688 
    export HCCL_WHITELIST_DISABLE=1    
     
    NPUS=($(seq 0 7)) 
    export RANK_SIZE=${#NPUS[@]} 
    rank=0 
    for i in ${NPUS[@]} 
    do 
        export DEVICE_ID=${i} 
        export RANK_ID=${rank} 
        echo run process ${rank} 
        please input your shell script here > output_npu_${i}.log 2>&1 & 
        let rank++ 
    done
    

    Table 2 run_distributed_npu.sh parameter description

    Parameter Description
    MASTER_ADDR Specifies the IP address of the training server
    MASTER_PORT Specifies the port of the training server
    HCCL_WHITELIST_DISABLE HCCL communication trustlist verification
    NPUS Specifies running on specific NPUs
    RANK_SIZE Specifies the number of devices to call
    DEVICE_ID Specifies the device ID to call
    RANK_ID Specifies the logical ID of the device to call
  2. After replacement, go to the directory where the script is located and run the following command to generate a log file for the specified NPU.

    bash run_distributed_npu.sh
    
  3. View the result files.

    After the script migration is complete, go to the script migration result output path to view the result files. Taking the migration of a GPU single-device script to an NPU multi-device script as an example, the result files include the following:

    ├── xxx_msft/xxx_msft_multi              // Script migration result output directory
    │   ├── Generated script files                 // Consistent with the directory structure of the script files before migration
    │   ├── msFmkTranspltlog.txt         // Script migration process log file. The log file size is limited to 1 MB. If this limit is exceeded, it will be split into multiple files for storage, with a maximum of 10 files.
    │   ├── cuda_op_list.csv            //List of analyzed CUDA operators
    │   ├── unknown_api.csv             //List of APIs with uncertain support status
    │   ├── unsupported_api.csv         //List of unsupported APIs
    │   ├── change_list.csv              // Change record
    │   ├── run_distributed_npu.sh       // Multi-device startup shell script
    

For more information about command-line tool-based migration, see CANN Analysis and Migration Tool User Guide.