Issue with torch.git source in pyinstaller

MrElie: <p>Hello i’m compiling a script using torch and torchvision with pyinstaller</p>
<p>Whenever i launch the compiled program i get this error:</p>
<pre><code class="lang-auto">torch\_jit_internal.py:750: UserWarning: Unable to retrieve source for @torch.jit._overload function: &lt;function _DenseLayer.forward at 0x000002909B1B24C0&gt;.
  warnings.warn(f"Unable to retrieve source for @torch.jit._overload function: {func}.")
torch\_jit_internal.py:750: UserWarning: Unable to retrieve source for @torch.jit._overload function: &lt;function _DenseLayer.forward at 0x000002909B1C8790&gt;.
  warnings.warn(f"Unable to retrieve source for @torch.jit._overload function: {func}.")
</code></pre>
<p>any help plz to what im doing wrong?</p>

techkang: <p>It seems like this is only a warning, not an error.</p>

MrElie: <p>yes but torch is not working when i get this error.</p>

Gabriel_Dornelles: <p><strong>WINDOWS SOLUTION</strong></p>
<ul>
<li>I didn’t test the following for anything other than Windows 10.</li>
</ul>
<p>For anyone trying to use PyTorch with PyInstaller I highly recommend you just don’t. I had a lot of issues and even when it worked it was much slower than the application should be. Instead use cx freeze with your application in an if name == main block :</p>
<pre><code class="lang-auto">import sys
from cx_Freeze import setup, Executable

build_exe_options = {"packages": ["onnxruntime","cv2","numpy","torch","PIL", "torchvision"]}

setup(
    name="project_name",
    version="1.0",
    description="your app", # on windows it will use that name if your exe crashes to display a message
    options={"build_exe": build_exe_options},
    executables=[Executable(application_main.py")]
)
</code></pre>
<p>It will generate a lib directory with your packages and your app.exe with the name you passed (project_name in this case) and works just fine.</p>

Gabriel_Dornelles: <h1><a name="solution-to-warningswarnfunable-to-retrieve-source-for-torchjit_overload-function-func-1" class="anchor" href="#solution-to-warningswarnfunable-to-retrieve-source-for-torchjit_overload-function-func-1"></a>Solution to: warnings.warn(f"Unable to retrieve source for <span class="mention">@torch.jit._overload</span> function: {func}.")</h1>
<p>This is just an warning and not an error at all. If by any reason you keep getting this warning and you don’t want it (like I needed to log the project in the exe file for the customer and this messages were annoying) you can just fix by removing the warning message. Here is the original code:<br>
<a href="https://github.com/pytorch/pytorch/blob/master/torch/_jit_internal.py" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/blob/master/torch/_jit_internal.py</a></p>
<p>save this as “_jit_internal.py” under lib/torch. Go at line 750 and remove the warning:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/7/a7f9331f85485e452996adf6c7b1743460074a7c.png" data-download-href="https://discuss.pytorch.org/uploads/default/a7f9331f85485e452996adf6c7b1743460074a7c" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/7/a7f9331f85485e452996adf6c7b1743460074a7c.png" alt="image" data-base62-sha1="nXXE191apWiEmVkmsxVok7RdmcQ" width="690" height="143" data-dominant-color="242423"><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">image</span><span class="informations">1009×210 12.3 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>
<p>Recompile the file (.pyc):</p>
<pre data-code-wrap="bash"><code class="lang-bash">python -m compileall _jit_internal.py
</code></pre>
<p>and your problem should finally be gone.</p>