亲和API替换

torch_npu.npu_confusion_transpose

npu_confusion_transpose可以代替Transpose + view的操作。
  • torch原生代码示例
    1
    2
    3
    4
    import torch  
    data = torch.rand(64, 3, 64, 128).cuda() 
    batch, channel, height, width = data.shape 
    result = data.view(batch, height, channel*width).transpose(1, 0)
    
  • torch_npu代码示例
    1
    2
    3
    4
    5
    6
    import torch 
    import torch_npu 
    from torch_npu.contrib import transfer_to_npu  
    data = torch.rand(64, 3, 64, 128).cuda() 
    batch, channel, height, width = data.shape 
    result = data.transpose(1, 2).contiguous().view(batch, height, channel*width)
    

optimizer.clip_grad_norm_fused_

在替换为NPU亲和梯度裁剪API之前,请确保代码中已使用NPU亲和优化器。