Automatic Migration
Introduction
Automatic migration is simple to perform and requires minimal modifications. You only need to add from torch_npu.contrib import transfer_to_npu to the training script for script migration.
Constraints
-
Automatic migration currently does not support the
channel_lastfeature. It is recommended to usecontiguousinstead. -
If the backend used in the original script is NCCL, after
init_process_groupinitializes the process group, the backend will be replaced with HCCL by the automatic migration tool. If the subsequent code logic includes a check on whether the backend is NCCL, such asassert backend in ['gloo', 'nccl']or ifbackend == 'nccl', please manually change thencclstring tohccl. -
Automatic migration leverages Python's dynamic features, but
torch.jit.scriptdoes not support Python's dynamic syntax. Therefore, using the automatic migration feature will cause conflicts when your original training script containstorch.jit.script. Currently, thetorch.jit.scriptfeature is disabled during automatic migration. Iftorch.jit.scriptis required in your script, please refer to Tool-based Migration for migration.
How to Use
-
Add the migration code
from torch_npu.contrib import transfer_to_nputo the training script.Note
- The code
from torch_npu.contrib import transfer_to_npuonly needs to be added under the PyTorch framework. - Automatic migration may have functional conflicts with the adapted suites. If a conflict occurs, please refer to Tool-based Migration.
-
For PyTorch 2.4.0 and earlier versions, add the following migration code:
import torch import torch_npu ... from torch_npu.contrib import transfer_to_npu -
For PyTorch 2.5.1 and later versions, add the following migration code:
import torch ... from torch_npu.contrib import transfer_to_npuNote
PyTorch 2.5.1 and later versions support automatic device plugin loading, allowing you to use NPU devices without executing
import torch_npu. For specific usage and limitations, see Autoloading Out-of-Tree Extension.
- The code
-
Perform training by referring to Model Training. If training proceeds normally and iteration logs begin to print, training functionality migration is successful.
-
Save weights by referring to Model Saving. If the weights are saved successfully, weight saving functionality migration is successful.
Precautions
-
If your model includes evaluation or online inference functionality, you can also import the automatic migration library code in the corresponding scripts and determine whether the migration is successful by comparing the evaluation/inference results and log outputs with those from GPU/CPU.
-
If some CUDA API errors are reported during training, it may be caused by unsupported APIs (operator APIs or framework APIs). You can refer to the following solutions to resolve the issue.
Use the PyTorch Analyse tool to analyze the model script and obtain a list of APIs whose support status is uncertain. Then, go to the Ascend Open-Source Community to raise an issue for assistance. For unsupported operators, refer to the "Custom Operator Adaptation Development" section in the TorchNPU Framework Feature Guide for adaptation.