Issue with torch.git source in pyinstaller

Hello i’m compiling a script using torch and torchvision with pyinstaller

Whenever i launch the compiled program i get this error:

torch\_jit_internal.py:750: UserWarning: Unable to retrieve source for @torch.jit._overload function: <function _DenseLayer.forward at 0x000002909B1B24C0>.
  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: <function _DenseLayer.forward at 0x000002909B1C8790>.
  warnings.warn(f"Unable to retrieve source for @torch.jit._overload function: {func}.")

any help plz to what im doing wrong?

It seems like this is only a warning, not an error.

yes but torch is not working when i get this error.

WINDOWS SOLUTION

  • I didn’t test the following for anything other than Windows 10.

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 :

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")]
)

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.

Solution to: warnings.warn(f"Unable to retrieve source for @torch.jit._overload function: {func}.")

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:
https://github.com/pytorch/pytorch/blob/master/torch/_jit_internal.py

save this as “_jit_internal.py” under lib/torch. Go at line 750 and remove the warning:

Recompile the file (.pyc):

python -m compileall _jit_internal.py

and your problem should finally be gone.