Return dtype of aten functions

username: nunoplopes
cooked: <p>Hi,</p>
<p>How can I find the return dtype of every aten function? I’m working on a delayed tensor, and I need the dtype to initialize my TensorImpl. But without executing the operation, I don’t have a way to knowing its dtype.<br>
I’ve seen the file torch/share/ATen/Declarations.yaml, but it only mentions Tensor/Scalar as return type, but not the dtype.<br>
The dtype of an op is mostly equal to its input tensor arguments, except for some that return booleans (e.g., eq/gt/ne, is*). Some ops also upcast, e.g. sum(bool) → long.</p>
<p>Is this information specified in a machine-readable way somewhere?<br>
Worst case I was thinking of maybe writing a program that executes every single op with a singleton tensor of each of the dtypes and records the result. Could that work?</p>
<p>Thanks,<br>
Nuno</p>

username: bdhirsh
cooked: <p>I’m not sure if there’s a better option out there, but <a href="https://github.com/pytorch/rfcs/blob/rfc-0005/RFC-0005-structured-kernel-definitions.md#how-to-get-involved-with-structured-kernels" rel="noopener nofollow ugc">structured kernels</a> (and specifically meta tensors) might help with this a bit. If you construct meta tensors and pass them to an aten op, the returned tensor will have the correct size/dtype, but without any actual data allocation / computation being performed.</p>
<p>e.g.:</p>
<pre><code class="lang-auto">a = torch.ones(2, device='meta', dtype=torch.int32)
b = torch.ones(2, device='meta', dtype=torch.int64)
c = a + b # returns a meta tensor with correct output size / dtype, no actual computation is performed.
c.dtype # torch.int64
</code></pre>
<p>The wrinkle is that only operators that have been ported to structured support meta tensors, so if your goal is to use this for 100% of ops then this won’t work in the near term. There’s an open issue with a running list of supported/unsupported ops <a href="https://github.com/pytorch/pytorch/issues/55070" rel="noopener nofollow ugc">here</a>.</p>

username: nunoplopes
cooked: <p>Thanks for pointing out to this meta device. I was not aware of it. Very interesting indeed!<br>
Though I guess for this specific question it’s maybe not super useful as the cost of running a kernel on a singleton tensor is fine. And I just need to run this once in a while, so not terrible even if it takes half an hour to go through all the ops.<br>
I was really hoping the typing rules were available somewhere… Since there are all these yaml files with a lot of info, I was hopeful <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=9" title=":blush:" class="emoji" alt=":blush:"></p>

username: nunoplopes
cooked: <p>Hi,</p>
<p>I went ahead and created a little program that calls all ATen functions and produces a list with the typing rule used by each function.<br>
Some interesting statistics:</p>
<pre><code class="lang-auto">ALL Bool:           37
ALL Byte:           4
ALL Char:           1
ALL Double:         3
ALL Float:          2
ALL Half:           1
ALL Int:            1
ALL Long:           17
ALL Short:          1
BOOL2INT:           7
BOOL2INT2:          1
BOOLBYTE:           2
EQ_FIRST:           389
EQ_PROMOTED:        57
EQ_SECOND:          11
EQ_THIRD:           1
INTEGRAL2INT:       7
NO_SAMPLES:         502
TO_COMPLEX:         5
TO_DOUBLE:          2
TO_DOUBLE2:         1
TO_FLOAT:           73
TO_FLOAT2:          12
TO_FLOAT2_2:        1
TO_FLOAT2_3:        1
TO_FLOAT2_4:        1
TO_FLOAT3:          1
TO_FLOAT3_2:        1
TO_FLOAT_DOUBLE:    1
TO_QINT:            2
TO_REAL2:           1
TO_REAL_FLOAT:      1
TO_VALUE_TYPE:      19

Num Types: 33
</code></pre>
<p>These are names I gave to each typing rule. It’s interesting that a few rules are used only once. This may point to bugs, as there should be no reason to have such exceptions.</p>
<p>Why I’m posting this stuff here is because it may be interesting for you guys. For example, it would be possible to generate the code for the Meta tensors automatically from this data. AFAIU, Meta tensors only compute dtype + shape information. Here I have the dtype, and I’m working next on the shape inference.<br>
Another use is for testing. Some of the calls crash with some inputs, for example, so you can use this driver for fuzzing. Might be also helpful to compare the returned dtypes across different devices.</p>
<p>Anyway, I’ll leave that to you. I need this data for other purposes.</p>
<p>How it works:</p>
<ul>
<li>I use a <a href="https://gist.github.com/nunoplopes/3f036718254123da19901cc3345d950b" rel="noopener nofollow ugc">script</a> to generate a <a href="https://gist.github.com/nunoplopes/a4a8b3fe5431a226463336c94b233ae3" rel="noopener nofollow ugc">file</a> with calls to every ATen function using default parameters. This script also generates a ninja file so I can run things in parallel.</li>
<li>Then I wrote a driver that goes through each function and calls them with different tensors (one call per each combination of dtypes)</li>
<li>The C++ code with the typing rules I wrote by hand</li>
<li>The result after running ninja is the types.txt file indicating the typing rule per ATen function.</li>
</ul>
<p>cc <a class="mention" href="/u/ailzhang">@ailzhang</a> <a class="mention" href="/u/ezyang">@ezyang</a></p>

username: nunoplopes
cooked: <p>I’m only allowed to post 2 links at a time.<br>
The driver code: <a href="https://gist.github.com/nunoplopes/f3eb6eda4f68f7a4e2ab098ccbbb88c8" class="inline-onebox" rel="noopener nofollow ugc">Driver to call all ATen functions · GitHub</a><br>
C++ code with typing rules: <a href="https://gist.github.com/nunoplopes/65400638bc82a85de8e9ca7e5968149d" class="inline-onebox" rel="noopener nofollow ugc">PyTorch Aten typing rules implementation · GitHub</a></p>

username: nunoplopes
cooked: <p>Resulting types.txt: <a href="https://gist.github.com/nunoplopes/2df80279b7b5f20f7f70f61ce9556d35" class="inline-onebox" rel="noopener nofollow ugc">PyTorch ATen typing rules · GitHub</a></p>

username: nunoplopes
cooked: <p>FWIW I’ve filled a bug report with an inconsistency I’ve detected vs the documentation: <a href="https://github.com/pytorch/pytorch/issues/60941" class="inline-onebox" rel="noopener nofollow ugc">Incorrect dtype cast for binary ops w/ mixed ComplexFloat + Double operands · Issue #60941 · pytorch/pytorch · GitHub</a></p>