@ptrblck  Just now, I change that line to
int64_t curDevice = at::device_of(a).value().index();
int64_t bDevice = at::device_of(b).value().index();

and add
std::cout << curDevice << bDevice << std::endl;

before #define HANDLE_CASE.
Then run this test code,
x = torch.rand(3, 4)
y = x.cuda()
z = x.to(1)
r = my_extension(y) # correct result and print 0 0
k = my_extension(z)  # incorrect result and print 1 1
print(y)  # RuntimeError

PS: If I print y without running my_extension(z), that’s no problem. my_extension(z) always return a tensor which all elements are 0.
PS: my function CUDA_tensor_apply22 is modified from CUDA_tensor_apply2 in PyTorch source file include/ATen/cuda/CUDAApplyUtils.cuh.