Looking for PyTorch op data type promotion and result tensor data type rules

Is there any document/guideline for PyTorch ops and data type conversions and promotion rules? There is a document at Tensor Attributes — PyTorch 1.10.1 documentation, however it does not cover some of the op specific cases. For example, add for int32 arguments returns an int32 dtype tensor, but div returns a float32 tensor -

a = torch.ones(4, dtype=torch.int32)
b = torch.ones(4, dtype=torch.int32)
add = a + b

div = a / b

torch.result_type(a, b) ← This doesn’t return the correct result dtype for div

Similarly, for logical comparator ops like eq, the result tensor is boolean but for in-place eq_, the result tensor type is same as the original. Also, eq_ doesn’t seem to do a dtype conversion -



c = torch.eq(ai, b) ← this works fine
ai.eq_(b) ← this doesn’t work
Traceback (most recent call last):
File “”, line 1, in
RuntimeError: Expected object of scalar type int but got scalar type float for argument ‘other’

It would be great if anyone can share any document/guideline on the rules covering all these cases.

Hey Sujoy,

A couple of helpful resources are:

the table that determines actual promotion behavior:

And a new ‘reference’ implementation in primtorch which should match:

Does this help?