Type_as also changes tensor's device

As per the title: type_as, surprisingly, also changes the tensor’s device, and not just its type.
That’s not what I expected from the documentation.

What is the correct way to change a tensor’s type to the type of another tensor, without changing its device?

Cheers,
Enrico

Hi Enrico!

That seems indeed a bit strange.
Does the alternative work fine?

a = a.type(b.type())

If so, the documentation (and method naming) seems to be inconsistent.

I tried the tpye_as method, it does not change the tensor’s device.:thinking:

a = torch.randint(0,5,(2,2),device='cuda:0')
b = torch.randn(2,2,device='cuda:1')
a = a.type_as(b)
a.device 
-> device(type='cuda', index=0)
b.device
-> device(type='cuda', index=1)
1 Like

It seems like the tensor device will be changed to cuda:0 if is originally on the CPU. Is that intended behavior?

# a gets moved to cuda:0
a = torch.randint(0,5,(2,2),device='cpu')
b = torch.randn(2,2,device='cuda:0')
a = a.type_as(b)
a.device
-> device(type='cuda', index=0)
b.device
-> device(type='cuda', index=0)

# a is not moved
a = torch.randint(0,5,(2,2),device='cuda:0')
b = torch.randn(2,2,device='cuda:1')
a = a.type_as(b)
a.device
-> device(type='cuda', index=0)
b.device
-> device(type='cuda', index=1)

This behavior is tracked in this issue.
Note that this issue suggests to deprecated type_as and type as also discussed in the first link.