该接口计划废弃,可以使用torch.to接口进行替换。
torch_npu.npu_dtype_cast(input, dtype) -> Tensor
执行张量数据类型(dtype)转换。支持FakeTensor模式。
示例一:
1 2 | >>> torch_npu.npu_dtype_cast(torch.tensor([0, 0.5, -1.]).npu(), dtype=torch.int) tensor([ 0, 0, -1], device='npu:0', dtype=torch.int32) |
示例二:
1 2 3 4 5 6 7 8 | //FakeTensor模式 >>> from torch._subclasses.fake_tensor import FakeTensorMode >>> with FakeTensorMode(): ... x = torch.rand(2, dtype=torch.float32).npu() ... res = torch_npu.npu_dtype_cast(x, torch.float16) ... >>> res FakeTensor(..., device='npu:0', size=(2,), dtype=torch.float16) |