FrameworkPTAdapter 2.0.1 PyTorch Network Model Porting and Training Guide 01
Replacing PyTorch-related APIs
- To enable the Ascend AI Processor to use the capabilities of the PyTorch framework, the native PyTorch framework needs to be adapted at the device layer. The APIs related to the CPU and CUDA need to be replaced for external presentation. During network porting, some device-related APIs need to be replaced with the APIs related to the Ascend AI Processor. The following table lists the supported device-related APIs.
Table 6-2 Device-related APIs
Original PyTorch API
API Adapted to the Ascend AI Processor
Description
torch.cuda.is_available()
torch.npu.is_available()
Checks whether the device is available in the current environment (not the final result).
torch.cuda.current_device()
torch.npu.current_device()
Obtains the device in use.
torch.cuda.device_count()
torch.npu.device_count()
Obtains the number of devices in the current environment.
torch.cuda.set_device()
torch.npu.set_device()
Sets the device in use.
torch.tensor([1,2,3]).is_cuda
torch.tensor([1,2,3]).is_npu
Checks whether a tensor is in the format on the CUDA or NPU device.
torch.tensor([1,2,3]).cuda()
torch.tensor([1,2,3]).npu()
Converts a tensor to the format on the CUDA or NPU device.
torch.tensor([1,2,3]).to("cuda")
torch.tensor([1,2,3]).to('npu')
Converts a tensor to the format on the CUDA or NPU device.
torch.cuda.synchronize()
torch.npu.synchronize()
Waits until the event is complete.
torch.cuda.device
torch.npu.device
Generates a device class, which can be used to perform device-related operations.
torch.cuda.Stream(device)
torch.npu.Stream(device)
Generates a stream object.
torch.cuda.stream(Stream)
torch.npu.stream(Stream)
Mainly used for scope restriction.
torch.cuda.current_stream()
torch.npu.current_stream()
Obtains the current stream.
torch.cuda.default_stream()
torch.npu.default_stream()
Obtains the default stream.
device = torch.device("cuda:0")
device = torch.device("npu:0")
Specifies a device.
torch.autograd.profiler.profile
(use_cuda=True)
torch.autograd.profiler.profile
(use_npu=True)
Specifies that CUDA/NPU is used during profiler execution.
torch.cuda.Event()
torch.npu.Event()
Returns events on a device.
- When building or porting a network, you need to create tensors of specified data types. The following table lists the tensors created on the Ascend AI Processor.
Table 6-3 Tensor-related APIs
GPU tensor
API Adapted to the Ascend AI Processor
torch.tensor([1,2,3],dtype=torch.long,device='cuda')
torch.tensor([1,2,3],dtype=torch.long,device='npu')
torch.tensor([1,2,3],dtype=torch.int,device='cuda')
torch.tensor([1,2,3],dtype=torch.int,device='npu')
torch.tensor([1,2,3],dtype=torch.half,device='cuda')
torch.tensor([1,2,3],dtype=torch.half,device='npu')
torch.tensor([1,2,3],dtype=torch.float,device='cuda')
torch.tensor([1,2,3],dtype=torch.float,device='npu')
torch.tensor([1,2,3],dtype=torch.bool,device='cuda')
torch.tensor([1,2,3],dtype=torch.bool,device='npu')
torch.cuda.BoolTensor([1,2,3])
torch.npu.BoolTensor([1,2,3])
torch.cuda.FloatTensor([1,2,3])
torch.npu.FloatTensor([1,2,3])
torch.cuda.IntTensor([1,2,3])
torch.npu.IntTensor([1,2,3])
torch.cuda.LongTensor([1,2,3])
torch.npu.LongTensor([1,2,3])
torch.cuda.HalfTensor([1,2,3])
torch.npu.HalfTensor([1,2,3])
For more APIs, see the PyTorch API Support List.