Runtime error - Unsupported value kind: Tensor

nivtorch: <p>I am getting this runtime error while trying to do scripting using torch.jit.script on my initialized and built model.<br>
The error is not giving any more detail or any description of where exactly the issue is.<br>
Any pointer to what might be causing this would be helpful. Thanks!</p>

abdussamettrkr: <p>Could you provide more information to reproduce the error?</p>

nivtorch: <p>I am not sure exactly how to reproduce the error, as I don’t have any clue what might be causing it.<br>
My code base is quite large, multiple files are involved. I made changes (mostly datatypes, type annotation and wherever I got runtime error from torch.jit.script call) to support Torchscript.<br>
If I get some idea in what cases that error might come, then I may write a custom code snippet to reproduce that error.<br>
For now I have attached a screenshot of the error I got, with the call stack internal to torch.jit.script call (after disabling ‘justMyCode’). Hope this gives some idea!<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/e/ae72f64706adfd4b00f715eb3ebb007fd1601087.png" data-download-href="https://discuss.pytorch.org/uploads/default/ae72f64706adfd4b00f715eb3ebb007fd1601087" title="Unsupported_value_kind_Tensor"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/e/ae72f64706adfd4b00f715eb3ebb007fd1601087_2_690x271.png" alt="Unsupported_value_kind_Tensor" data-base62-sha1="oTfnRnbKUJNGgEalQCYkuBjUUdN" width="690" height="271" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/e/ae72f64706adfd4b00f715eb3ebb007fd1601087_2_690x271.png, https://discuss.pytorch.org/uploads/default/original/3X/a/e/ae72f64706adfd4b00f715eb3ebb007fd1601087.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/e/ae72f64706adfd4b00f715eb3ebb007fd1601087.png 2x" data-dominant-color="342728"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Unsupported_value_kind_Tensor</span><span class="informations">1030×406 168 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Kas: <p>Have you found out? I have met the same problem when use torch_tensorrt.compile.</p>

nivtorch: <p>I could resolve this error through some rigorous debugging. Turned out that there was a method which expected 3 arguments, but only 2 were being passed as the third argument (=None) was not being used in the method which was required to be scripted.<br>
A better error description, at least giving some idea of location of the error, would have been helpful here.</p>

nivtorch: <p>Hi, yes, I found out. Please see my recent post with the description of resolution.</p>

songhao0526: <aside class="quote no-group" data-username="nivtorch" data-post="7" data-topic="190276">
<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/nivtorch/48/64744_2.png" class="avatar"> nivtorch:</div>
<blockquote>
<p>post</p>
</blockquote>
</aside>
<p>Thanks a lot! It solves my problem</p>

alexmath: <blockquote>
<p>A better error description, at least giving some idea of location of the error, would have been helpful here.</p>
</blockquote>
<p>Agree. If anyone else is looking for a hacky way to hunt down a bug, the error will print a path</p>
<pre><code class="lang-auto">miniconda3/lib/python3.12/site-packages/torch/jit/_recursive.py
</code></pre>
<p>You can just <code>vim &lt;your_path&gt;</code> and add</p>
<pre><code class="lang-auto">print('---\n', concrete_type, method_stubs, property_stubs)
</code></pre>
<p>This will give you a hint for where it happens.</p>

shijie137912: <p>For some input parameter which has default value None, try to add <code>typing.Optional</code> like follows</p>
<pre data-code-wrap="python"><code class="lang-python">def forward(self, x: Tensor, context: Optional[Tensor]=None):
     ...
</code></pre>