Relation between Tensor and FloatTensor

The doc says " torch.Tensor is an alias for the default tensor type ( torch.FloatTensor )"
However I found it is not that trivial. When I run the following code:

a = torch.zeros(3,4)
isinstance(a, torch.Tensor) # Return True
isinstance(a, torch.FloatTensor) # Return True

Things look good so far. However, if I check the mro of both classes:

torch.Tensor.__ mro__ # Return (torch.Tensor, torch.C.TensorBase, object)
__ mro__ # Return (torch.FloatTensor, object)

There seems to be no inheritence between FloatTensor and Tensor. Then how could isinstance return True for both two classes?

Could someone point out these two classes’ relationship, or anything wrong with my understanding of isinstance() and mro?


It’s non trivial because it also changes the cpp behavior. And so this link is rooted in cpp code.
I am not sure how this works exactly, but I can dig into if you really want to :slight_smile:

Thank you for your reply! I just want to know what magic makes isinstance() return True for both classes even if they do not inherit each other. It would be very helpful if you could answer it!

Well in cpp, you have a single metaclass and then for each tensor type, set the parent type to be the metaclass.
This metaclass is here to make this isinstance magic work using this function that is called when you call isinstance because of this.

Hope this helps :slight_smile:

1 Like