torch.onnx.errors.UnsupportedOperatorError: Exporting the operator 'aten::randint' to ONNX opset version 18 is not supported

D3cpt: <p>from <a href="https://github.com/pytorch/pytorch/pull/105089" rel="noopener nofollow ugc">here</a>, it seems like aten:randint should be in the onnx exporter. But i am getting the error in the title.<br>
Any advice? Thank you!</p>

ptrblck: <p>You might be using an older PyTorch release. Since the linked PR was merge on July 12, you might need to install a nightly release or build from source.</p>

D3cpt: <p>Thank you for the fast response. I am using the following versions:<br>
pytorch-lightning         1.8.1                    pypi_0    pypi<br>
torch                     2.0.1+cu117              pypi_0    pypi<br>
torchaudio                2.0.2+cu117              pypi_0    pypi<br>
torchmetrics              0.11.4                   pypi_0    pypi<br>
torchvision               0.15.2+cu117             pypi_0    pypi</p>
<p>Is this version still too old? i will try with nightly release first, else build from source.</p>

ptrblck: <p>Yes, <code>torch==2.0.1</code> is still too old as it was released on May 8 and is based on 2.0.0 from March 15 with a few important fixes.</p>

D3cpt: <p>I see. i shall try out the nightly release.</p>
<p>Just to note, i tried copying over the aten::randint operator over to my current optset version, and it ran with the following error:</p>
<blockquote>
<p>File “D:\CH\Anaconda\envs\distraction\lib\site-packages\torch\onnx\symbolic_helper.py”, line 93, in _parse_arg<br>
return [int(v) for v in node_val]<br>
File “D:\CH\Anaconda\envs\distraction\lib\site-packages\torch_tensor.py”, line 930, in <strong>iter</strong><br>
raise TypeError(“iteration over a 0-d tensor”)<br>
TypeError: iteration over a 0-d tensor<br>
(Occurred when translating randint).</p>
</blockquote>

D3cpt: <p>Just tried the nightly release. the operator is available but i got the same error as above. I have verified that the model runs properly when not exporting. I am also using the same inference video to act as the dummy input to export.</p>

ptrblck: <aside class="quote no-group" data-username="D3cpt" data-post="6" data-topic="187401">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/d3cpt/48/63595_2.png" class="avatar"> D3cpt:</div>
<blockquote>
<p>the operator is available but i got the same error as above.</p>
</blockquote>
</aside>
<p>I don’t understand this statement. How did you verify the operator is available and why does is then still fail?</p>

D3cpt: <p>what i meant is that i can find <code>@_onnx_symbolic("aten::randint")</code> in opset 9 as stated <a href="https://github.com/pytorch/pytorch/pull/105089/files#diff-4e77adb023a4e15f75009c08294251dfe9c3b7bb6947e7f7b4609c65edb53b20" rel="noopener nofollow ugc">here</a>, when i installed the nightly version. However, i am getting the above error exporting when there is none during inference.</p>

D3cpt: <p>would you reckon that it is due to error on my end or the export code is missing something?</p>

ptrblck: <p>Were you able to run the unit tests which were added in the linked PR? If not, I would guess that your version might still not contain the needed PR.</p>

D3cpt: <p>libraries version:</p>
<pre><code class="lang-auto">torch                     2.1.0.dev20230830+cu118          pypi_0    pypi
torchaudio                2.1.0.dev20230830+cu118          pypi_0    pypi
torchmetrics              0.11.4                   pypi_0    pypi
torchvision               0.16.0.dev20230830+cu118          pypi_0    pypi
onnx                      1.14.0                   pypi_0    pypi
onnxruntime-gpu           1.15.1                   pypi_0    pypi
</code></pre>
<p>Final output after git clone and running</p>
<blockquote>
<p>python -m pytest test_pytorch_onnx_onnxruntime.py &gt; test_results.txt</p>
</blockquote>
<p><a href="https://pastebin.com/yGKTE5RK" class="inline-onebox" rel="noopener nofollow ugc">================================================================================ - Pastebin.com</a> (too long so only pasted the summary part)</p>

D3cpt: <p>possible to advice?</p>
<hr>

ptrblck: <p>You didn’t follow up on my previous answer but just posted outputs of an ONNXRT test, so did you run the newly added test shown in the linked PR and if so, was it successful?</p>

D3cpt: <p>In the linked PR, 2 files were changed, namely <code>symbolic_opset9.py</code> and <code>test_pytorch_onnx_onnxruntime.py</code>. Hence, i tried running the ONNXRT test.</p>
<p>sorry if my understanding is incorrect. what test am i suppose to run?</p>