PyInstaller executable fails to get source code of TorchScript

Question:

I’m trying to make Windows executable of my script that includes PyTorch. My script’s imports are:

import numpy.core.multiarray  # which is a workaround for "ImportError: numpy.core.multiarray failed to import"
import six # which is workaround for "ModuleNotFoundError: No module named 'six'"
import torch
import torch.nn as nn
import warnings
import argparse
import json
import math
import numpy as np
import jsonschema
import os
from datetime import datetime
from sklearn.mixture import GaussianMixture
from scipy.io import wavfile
from scipy.signal import get_window
from scipy.signal import spectrogram

I’m using command:

pyinstaller --hidden-import pkg_resources.py2_warn extractor.py

PyInstaller throws no error while creating .exe, but when I run the .exe i get:

Traceback (most recent call last):
  File "site-packagestorch_utils_internal.py", line 46, in get_source_lines_and_file
  File "inspect.py", line 967, in getsourcelines
  File "inspect.py", line 798, in findsource
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "extractor.py", line 3, in <module>
    import torch
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:ProgramDataAnaconda3envsforexe2libsite-packagesPyInstallerloaderpyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagestorch__init__.py", line 367, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:ProgramDataAnaconda3envsforexe2libsite-packagesPyInstallerloaderpyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagestorchdistributions__init__.py", line 112, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:ProgramDataAnaconda3envsforexe2libsite-packagesPyInstallerloaderpyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagestorchdistributionsvon_mises.py", line 55, in <module>
  File "site-packagestorchjit__init__.py", line 1287, in script
  File "site-packagestorchjitfrontend.py", line 164, in get_jit_def
  File "site-packagestorch_utils_internal.py", line 53, in get_source_lines_and_file
OSError: Can't get source for <function _rejection_sample at 0x0000000006892F70>. TorchScript requires source access in order to carry out compilation, make sure original .py files are available. Original error: could not get source code
[5704] Failed to execute script extractor

Which I don’t understand. This may be similar issue to this question. What is causing the problem?

I’m using conda env, with torch installed via pip (which is a workaround for the torch to be correctly hooked).

  • Windows 10
  • Python 3.8.2
  • torch 1.5.0+cu101
  • torchvision 0.6.0+cu101 (also tried with 0.2.2)
  • PyInstaller 3.6
Asked By: MMis

||

Answers:

Torch is open source, so you can search for the function _rejection_sample on the torch GitHub. This identifies the problematic file as torch.distributions.von_mises. If your program is not using the torch.distributions module, you can simply exclude it by changing the .spec file generated by pyinstaller.

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None
excluded_modules = ['torch.distributions'] # <<< ADD THIS LINE

a = Analysis(['C:/your/path/here'],
             pathex=['C:\your\path\here'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=excluded_modules,    # <<< CHANGE THIS LINE
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
# remaining code omitted for brevity

You should only need to make changes in the two places specified above. The rest should already be there.

Then, build from the .spec file using

pyinstaller your_file.spec

In the future, consider using debug flags when you build your project. This also identifies the location of the file causing problems.

Answered By: emgoinv

In addition to the given answer if it still doesn’t work the solution below worked for me.

Just add a bit of code to the import torch. The module I reported the error is the main file, and the package is also the main file, so I directly added the code to the import torch of the main. My other modules also use import torch, but they don’t need to do this

def script_method(fn, _rcb=None):
return fn

def script(obj, optimize=True, _frames_up=0, _rcb=None):
    return obj

import torch.jit
script_method1 = torch.jit.script_method
script1 = torch.jit.script_if_tracing
torch.jit.script_method = script_method
torch.jit.script = script
Answered By: Yağız
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.